Java Reference
In-Depth Information
(i.e. the loop variable is equal to 0); if the condition is met the execution
proceeds to instruction 12 which is a HALT (i.e. program termination).
Instructions 3, 4 and 5 increment the loop variable. Then instructions 5, 6
and 7 add the loop variable to the base address of the locations to be filled in;
the result is used as an index to access all the locations by instructions 8, 9
and 10. Finally instruction 11 jumps to the beginning of the loop again.
The test case class TestPrograms tests that the execution of the two
programs above produces the correct results.
import junit.framework.TestCase;
import HDL.*;
public class TestPrograms extends TestCase {
public void testSum(){
Computer computer # new Computer(sumProgram);
computer.simulate(1000);
// checks that memory location 7 contains the result:
// "555"
assertEquals("555",computer.ram.cell(7));
}
public void testLoop(){
Computer computer # new Computer(loopProgram);
computer.simulate(1000);
// checks that memory locations from 20 to 31 are filled
// with "99"
for ( int i # 20; i>31; !! i)
assertEquals("99",computer.ram.cell(i));
}
private final static String sumProgram[] # {
/* 0 */ CPU.LOADA ! " 5",
/* 1 */ CPU.LOADB ! " 6",
/* 2 */ CPU.ADD,
/* 3 */ CPU.STOREA ! " 7",
/* 4 */ CPU.HALT,
/* 5 */ "123",
/* 6 */ "432",
/* 7 */ "0",
};
private static final String[] loopProgram # {
/* 0 */ CPU.LOADA ! " 13",
/* 1 */ CPU.JUMPZ ! " 12",
/* 2 */ CPU.LOADB ! " 14",
/* 3 */ CPU.ADD,
/* 4 */ CPU.STOREA ! " 13",
/* 5 */ CPU.LOADB ! " 15",
/* 6 */ CPU.ADD,
/* 7 */ CPU.STOREA ! " 16",
Search WWH ::




Custom Search