-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
255 lines (201 loc) · 8.01 KB
/
Copy pathMain.java
File metadata and controls
255 lines (201 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
public class Main {
//Machine error variable (Separate from CPU error variable)
private static long error;
//HARDWARE
//Hypo main memory
private static long[] hypoMemory; //Each word should contain a 6 integer value
//Hypo memory registers
private static long mar; //Memory address register
private static long mbr; //Memory buffer register
//Clock
private static long clock;
//CPU registers (GPR)
private static long[] gpr;
//CPU registers
private static long ir; //Instruction register
private static long psr; //Processor status register
private static long pc; //Program counter
private static long sp; //Stack pointer
//Main method
public static void main(String[] args) {
//Informational messages
System.out.printf("%n%n[HYPO]");
//Initialize everything to 0
initializeSystem();
//Call the AbsoluteLoader
AbsoluteLoader absoluteLoader = new AbsoluteLoader(); //New instance
pc = absoluteLoader.load(); //Set program counter to the AbsoluteLoader return value
//Note that the mutator is not used. This is to suppress
//the program counter error message if the file is not found
//Proceed only if there has been no error from the AbsoluteLoader (error indicated by a negative PC)
if (pc > 0) {
System.out.printf("%n$ Program counter set to %d...%n", pc);
dumpMemory("Dump after loading user program", 0, 100);
//Instantiate a new CPU
CPU cpu = new CPU();
cpu.cycle(); //Perform the CPU cycle
//Dump memory after X CPU cycles
String dumpAfterX = String.format("Dump after %d CPU cycle(s)", cpu.getCycles());
dumpMemory(dumpAfterX, 0, 100);
//Display MAR, MBR, and IR values
System.out.printf("%nMAR: %d, MBR: %d, IR: %d%n", mar, mbr, ir);
}
System.out.printf("%n%n");
}
//Initialize the system - Set everything to 0
public static void initializeSystem() {
error = 0;
hypoMemory = new long[SystemConstants.WORDSIZE];
mar = 0;
mbr = 0;
clock = 0;
gpr = new long[SystemConstants.GPRSIZE];
ir = 0;
psr = 0;
pc = 0;
sp = 0;
System.out.printf("%nSystem initialized...%n");
}
//Dump memory
public static void dumpMemory(String inputParameterString, long startAddress, long size) {
//inputParameterString = a string to print out when the dump occurs
//startAddress = beginning address of memory to dump
//size = used to calculate the last address of memory to dump
//Calculate last address
long addr = startAddress;
long endAddress = startAddress + size;
System.out.printf("%n========================================================================================");
//State that a memory dump is occurring and state the input parameter string
//Also print out "GPRs"
System.out.printf("%n[MEMORY DUMP]%nInput parameter string: \"%s\"%n%nGPRs:\t", inputParameterString);
//Print GPR labels
for (int i = 0; i < gpr.length; i++) {
System.out.printf("G%d\t", i);
}
//Print SP and PC labels
System.out.printf("SP\tPC\t");
//Print values stored in the GPRs
System.out.printf("%n\t");
for (int i = 0; i < gpr.length; i++) {
System.out.printf(" %d\t", gpr[i]);
}
//Print SP and PC values
System.out.printf(" %d\t %d\t", sp, pc);
System.out.printf("%n");
//Print address labels
System.out.printf("%nAddr:\t");
for (int i = 0; i < 10; i++) {
System.out.printf("+%d\t", i);
}
//While the address is within the word size and within the end address
while (addr < hypoMemory.length && addr < endAddress) {
//Print the address number
System.out.printf("%n%d\t", addr);
for (int i = 0; i < 10; i++) {
//If the address is within the word size limit and the end address limit
//print out its value
if (addr < hypoMemory.length && addr < endAddress) {
System.out.printf(" %d\t", hypoMemory[(int)addr]);
}
//Move the address forward - repeat 10 times
addr++;
}
}
//Print clock and PSR values
System.out.printf("%n%nClock: %d%nPSR: %d", clock, psr);
System.out.printf("%n");
System.out.printf("========================================================================================%n");
}
//Accessors
//This goes to a specified location in memory
//And returns whatever is stored there if the location is valid
public static long getHypoMemory(long location) {
//Note that the isValidProgramArea method is not used.
//This is because accessing memory outside the valid area may be needed
if (location < hypoMemory.length && location > -1) {
return hypoMemory[(int)location];
} else {
error = SystemConstants.ERROR_INVALID_ADDRESS;
System.out.printf("%n$ ERROR %d: Location %d is greater than the word size or negative and cannot be accessed!%n", error, location);
return error;
}
}
public static long getGPR(long register) {
if (register < gpr.length) {
return gpr[(int)register];
} else {
error = SystemConstants.ERROR_INVALID_ADDRESS;
System.out.printf("%n$ ERROR %d: GPR %d does not exist and cannot be accessed!%n", error, register);
return error;
}
}
public static long getMBR() {
return mbr;
}
public static long getMAR() {
return mar;
}
public static long getIR() {
return ir;
}
public static long getSP() {
return sp;
}
public static long getPC() {
return pc;
}
//Mutators
public static void setError(long code) {
error = code;
}
public static void setHypoMemory(long location, long instruction) {
//Note that the isValidProgramArea method is not used.
//This is because allocating memory outside the valid area may be needed
if (location < hypoMemory.length && location > -1) {
hypoMemory[(int)location] = instruction;
} else {
error = SystemConstants.ERROR_INVALID_ADDRESS;
System.out.printf("%n$ ERROR %d: Location %d is greater than the word size and cannot be accessed!%n", error, location);
}
}
public static void setGPR(long register, long value) {
if (register < gpr.length) {
gpr[(int)register] = value;
} else {
error = SystemConstants.ERROR_INVALID_ADDRESS;
System.out.printf("%n$ ERROR %d: GPR %d does not exist and cannot be set!%n", error, register);
}
}
public static void setMBR(long word) {
mbr = word;
}
public static void setMAR(long word) {
mar = word;
}
public static void setIR(long word) {
ir = word;
}
//Sets the program counter if the provided address is within the valid program area
public static void setPC(long addr) {
if (isValidProgramArea(addr)) {
pc = addr;
} else {
error = SystemConstants.ERROR_INVALID_ADDRESS;
pc = error;
System.out.printf("%n$ ERROR %d: PC cannot be set to %d.%n", error, addr);
}
}
public static void incrementPC() {
setPC(pc+1);
}
public static void incrementClock(long time) {
clock += time;
}
public static void incrementClock() {
incrementClock(1);
}
//Boolean methods
public static boolean isValidProgramArea(long addr) {
return addr < SystemConstants.VALID_PROGRAM_AREA+1 && addr > -1;
}
}