Java Reference
In-Depth Information
case
READ_B_entry:
RegB
#
bus.getData();
state
!!
;
case
READ_B:
state
#
ADD_entry;
break
;
case
ADD_entry:
RegA
#
Integer.toString(
Integer.parseInt(RegA)
!
Integer.parseInt(RegB)
);
state
!!
;
case
ADD:
state
#
WRITE_MEM2_entry;
break
;
case
WRITE_MEM2_entry:
bus.setCommand(RAM.RAM_WRITE);
bus.setData(RegA);
bus.setAddress(2);
state
!!
;
case
WRITE_MEM2:
if
(bus.getCommand().equals(RAM.ACK))
state
#
HALT_entry;
break
;
case
HALT_entry:
state
!!
;
case
HALT:
break
;
}
}
It is important to detect when the CPU enters the halt state, because after
this step the simulation stops. Since the state attribute must remain private,
a function can be used to this purpose.
public boolean
halted() {
return
state
##
HALT; }
Let's now refine the implementation of class
Computer
. The
halted
method
of the CPU class should be checked in the
simulate
method in order to stop
the simulation when the CPU enters the HALT state. In addition we define a
constructor that takes as argument the initial content of the memory.
public class
Computer {
final
Bus bus
#
new
Bus();
final
CPU cpu
#
new
CPU(bus);
final
RAM ram
#
new
RAM(bus,3);
public
Computer(String[] initialMemory){
ram.initialize(initialMemory);
}