Java Reference
In-Depth Information
The class RAM is connected to the Bus . The relationship can be imple-
mented as usual with an attribute. Since it is a mandatory link, which is
established at the creation of the computer and never changed, we initialize
it in the constructor, which will have a parameter of type Bus .
The main feature of the RAM is to contain cells of data. The cells must be
addressable by an integer index (taken from the address bus). The obvious
implementation is an array of String objects. The default size is defined by
the constant value DEFAULT_SIZE . A constant is implemented as a static final
attribute; the static qualifier ensures that only one copy of the constant is
seen by everyone; the final qualifier ensures that the attribute cannot be
changed at run time (see idiom Constant Attribute , p. 120).
In addition it can be useful to have the capability of specifying the size as
a parameter of a constructor. The constructors have two responsibilities:
initialize the reference to the bus and create the memory cells.
public class RAM {
final static int DEFAULT_SIZE # 256;
private String [] cells; // the sequence of memory cells
private Bus bus;
RAM(Bus bus) {
this .bus # bus;
cells # new String[DEFAULT_SIZE];
}
RAM(Bus bus, size) {
this .bus # bus;
cells # new String[size];
}
The initialize() method receives a String array that is used to initialize the
RAM. The size of the initial values array must be less than or equal to the
size of the memory.
public void initialize(String [] initValues){
// check precondition
if (initValues.length > cells.length){
System.err.println("Error initializing RAM: " !
"initial values array bigger than RAM.");
System.exit(0);
}
// initialized memory cells
for ( int i # 0; i<initValues.length; !! i){
cells[i] # initValues[i];
}
}
An important feature of the RAM concerning the monitoring of the
simulation is the capability of reading the contents of the memory cells.
Search WWH ::




Custom Search