Java Reference
In-Depth Information
constants. They are public in order also to be visible outside the package;
this allows their use to define the programs.
public class CPU {
public final static String HALT # "HALT";
public final static String JUMP # "JUMP";
public final static String JUMPZ # "JUMPZ";
public final static String LOADA # "LOADA";
public final static String LOADB # "LOADB";
public final static String STOREA # "STOREA";
public final static String STOREB # "STOREB";
public final static String MOVEAB # "MOVEAB";
public final static String ADD # "ADD";
public final static String INPUT # "INPUT";
public final static String OUTPUT # "OUTPUT";
public final static String FOPEN # "FOPEN";
public final static String FREAD # "FREAD";
public final static String FCLOSE # "FCLOSE";
In order to support these instructions the CPU requires a set of registers:
private int PC # 0; // Program counter
private String IR; // Instruction register
private String RegA; // Register A
private String RegB; // Register B
It is important to notice that since the PC is initialized with the value 0 the
execution of the programs will always start from that location. In other words,
the first instruction executed by the CPU is the one in memory cell 0.
To execute instructions it is important to separate the two components of
the instruction contained in the instruction register: the code and the argu-
ment. This can be achieved by means of two methods that return each part
of the IR.
private String code(String instruction){
int separator # instruction.indexOf(' ');
if ( separator > 0)
return instruction.substring(0,separator);
else
return instruction;
}
private int arg(String instruction){
int separator # instruction.indexOf(' ');
if ( separator > 0)
return Integer.parseInt(instruction.substring
(separator ! 1,instruction.length()));
else
return 0;
}
Search WWH ::




Custom Search