-
Notifications
You must be signed in to change notification settings - Fork 61
/
cpu.js
330 lines (286 loc) · 10.4 KB
/
cpu.js
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Possible optimizations: choose/attach load/store methods at time of object
// creation instead of if/elses comparing strings at every call - however
// need to remember to update methods if changing endianness after instantiation
// is allowed.
// CPU class. Contains regfile, memory, and special registers
// memamt is memory size in Mebibytes, default to 32
function CPU(memamt) {
memamt = typeof memamt !== 'undefined' ? memamt : 10;
this.memamount = memamt; // for use by the kernel
memamt *= 1048576 // convert to Bytes
this.memory = new Uint32Array(memamt >> 2);
this.excpTrigg = undefined
// PC, defaults to 0x2000 according to the ISA, documented in
// processor.cc
// Even in RV64, this must remain as a Number (not a Long) because
// of Array indexing requirements.
// Possibly improve on this later by nesting arrays, but trying to address/
// store more than ~4GiB is probably not a good idea anyways
this.pc = 0x2000;
// catch loops
this.oldpc = 0x0;
// special testing flag
this.testSuccess = false;
// file management fields
// program name => fd lookup
this.pname_fd = new Object();
// fd => program name lookup
this.fd_pname = new Object();
// actually store binaries. index on file descriptor
this.binaries = [];
// next unused file-descriptor. start at 0x3
this.next_fd = 3;
// general-purpose registers, gen_reg[0] is x0, etc.
this.gen_reg = [];
for (var i = 0; i < 32; i++) {
this.gen_reg[i] = new Long(0x0, 0x0);
}
// privileged control registers
this.priv_reg = new Array(3075);
for (var key in PCR) {
if (PCR.hasOwnProperty(key)) {
if (PCR[key]["width"] == 32) {
this.priv_reg[PCR[key]["num"]] = 0x0;
} else {
// 64 bit
this.priv_reg[PCR[key]["num"]] = new Long(0x0, 0x0);
}
}
}
// init status register
this.priv_reg[PCR["CSR_STATUS"]["num"]] = status_reg_init();
this.instcount = 0x1; // special counter for MIPS measurement, start at one
// to avoid incorrect first result
// endianness: "big" and "little" allowed
this.endianness = "little";
// record cpu boot time (in ms since jan 1, 1970) for rdtime instruction
// for better measurement, this should be reset right before first instruction
// is exec'd
var start = new Date();
this.priv_reg[PCR["CSR_TIME"]["num"]] = Long.fromNumber(start.getTime());
function reset_wall_clock() {
// this should be called once, right before exec of first instruction
var start = new Date();
this.priv_reg[PCR["CSR_TIME"]["num"]] = Long.fromNumber(start.getTime());
}
// for the following calls - by default, will use VM bit to determine if
// address translation should be used. however sometimes, it must be forced
// off, so can be passed in as an arg
// unlike word, half, byte, the val arg here is a Long
function store_double_to_mem(addr, val) {
var vmOn = ((this.priv_reg[PCR["CSR_STATUS"]["num"]] & SR["SR_VM"]) != 0x0);
if (vmOn) {
addr = translate(addr, 1);
if (RISCV.excpTrigg) {
return;
}
} else {
addr = addr.getLowBitsUnsigned();
}
if (addr & 0x7) {
RISCV.excpTrigg = new RISCVTrap("Store Address Misaligned", addr);
return;
}
addr = addr >> 2;
var lowbits = val.getLowBits()|0;
var highbits = val.getHighBits()|0;
this.memory[addr] = lowbits;
this.memory[addr+1] = highbits;;
}
function store_word_to_mem(addr, val) {
var vmOn = ((this.priv_reg[PCR["CSR_STATUS"]["num"]] & SR["SR_VM"]) != 0x0);
if (vmOn) {
addr = translate(addr, 1);
if (RISCV.excpTrigg) {
return;
}
} else {
addr = addr.getLowBitsUnsigned();
}
if (addr & 0x3) {
RISCV.excpTrigg = new RISCVTrap("Store Address Misaligned", addr);
return;
}
this.memory[addr >> 2] = val;
}
function store_half_to_mem(addr, val) {
var vmOn = ((this.priv_reg[PCR["CSR_STATUS"]["num"]] & SR["SR_VM"]) != 0x0);
if (vmOn) {
addr = translate(addr, 1);
if (RISCV.excpTrigg) {
return;
}
} else {
addr = addr.getLowBitsUnsigned();
}
if (addr & 0x1) {
RISCV.excpTrigg = new RISCVTrap("Store Address Misaligned", addr);
return;
}
this.memory[(addr >> 2)] &= ~(0xFFFF << ((addr & 0x2) << 3));
this.memory[(addr >> 2)] |= (val & 0xFFFF) << ((addr & 0x2) << 3);
}
function store_byte_to_mem(addr, val) {
var vmOn = ((this.priv_reg[PCR["CSR_STATUS"]["num"]] & SR["SR_VM"]) != 0x0);
if (vmOn) {
addr = translate(addr, 1);
if (RISCV.excpTrigg) {
return;
}
} else {
addr = addr.getLowBitsUnsigned();
}
this.memory[(addr >> 2)] &= ~(0xFF << ((addr & 0x3) << 3));
this.memory[(addr >> 2)] |= ((val & 0xFF) << ((addr & 0x3) << 3));
}
function load_double_from_mem(addr) {
var vmOn = ((this.priv_reg[PCR["CSR_STATUS"]["num"]] & SR["SR_VM"]) != 0x0);
if (vmOn) {
addr = translate(addr, 0);
if (RISCV.excpTrigg){
return;
}
} else {
addr = addr.getLowBitsUnsigned();
}
if (addr & 0x7) {
RISCV.excpTrigg = new RISCVTrap("Load Address Misaligned", addr);
return;
}
addr = addr >> 2;
return new Long(this.memory[addr], this.memory[addr+1]);
}
function load_double_from_mem_raw(addr) {
addr = addr.getLowBitsUnsigned();
if (addr & 0x7) {
RISCV.excpTrigg = new RISCVTrap("Load Address Misaligned", addr);
return;
}
addr = addr >> 2;
return new Long(this.memory[addr], this.memory[addr+1]);
}
function load_word_from_mem(addr) {
var vmOn = ((this.priv_reg[PCR["CSR_STATUS"]["num"]] & SR["SR_VM"]) != 0x0);
if (vmOn) {
addr = translate(addr, 0);
if (RISCV.excpTrigg) {
return;
}
} else {
addr = addr.getLowBitsUnsigned();
}
if (addr & 0x3) {
RISCV.excpTrigg = new RISCVTrap("Load Address Misaligned", addr);
return;
}
return this.memory[addr >> 2];
}
function load_half_from_mem(addr) {
var vmOn = ((this.priv_reg[PCR["CSR_STATUS"]["num"]] & SR["SR_VM"]) != 0x0);
if (vmOn) {
addr = translate(addr, 0);
if (RISCV.excpTrigg) {
return;
}
} else {
addr = addr.getLowBitsUnsigned();
}
if (addr & 0x1) {
RISCV.excpTrigg = new RISCVTrap("Load Address Misaligned", addr);
}
return (this.memory[addr >> 2] >> ((addr & 0x2) << 3)) & 0xFFFF;
}
function load_byte_from_mem(addr) {
var vmOn = ((this.priv_reg[PCR["CSR_STATUS"]["num"]] & SR["SR_VM"]) != 0x0);
if (vmOn) {
addr = translate(addr, 0);
if (RISCV.excpTrigg) {
return;
}
} else {
addr = addr.getLowBitsUnsigned();
}
return (this.memory[addr >> 2] >> ((addr & 0x3) << 3)) & 0xFF;
}
// vals[0] is loaded into 0x0000, vals[1] is program, loaded into 0x2000
function load_to_mem(vals) {
prog = vals[1];
for (var i = 0; i < prog.length*4; i+=4) {
this.store_word_to_mem(i+0x2000, prog[i/4]);
}
}
// set indicated PCR - need to make sure to prevent changes to hardwired vals
function set_pcr(num, val) {
switch(num) {
case PCR["CSR_STATUS"]["num"]:
// assuming 32 bit status reg
this.priv_reg[num] = status_reg_force(val);
break;
// need to fill in all cases here (i.e. when implementing interrupts)
case PCR["CSR_TOHOST"]["num"]:
if (this.priv_reg[num].isZero()) {
this.priv_reg[num] = val;
}
break;
default:
this.priv_reg[num] = val;
break;
}
}
/* wrapper for instruction fetch, converts Load Addr Misaligned to Instruction
* Address Misaligned
*/
function load_inst_from_mem(addr) {
var vmOn = ((this.priv_reg[0x50A] & 0x80));
if (vmOn) {
addr = insttranslate(addr, 2);
if (RISCV.excpTrigg) {
return;
}
}
/* UNSAFE when removed
if (addr & 0x3) {
RISCV.excpTrigg = new RISCVTrap("Instruction Address Misaligned", addr);
return;
}
*/
return this.memory[addr >> 2];
}
this.reset_wall_clock = reset_wall_clock;
this.store_double_to_mem = store_double_to_mem;
this.store_word_to_mem = store_word_to_mem;
this.store_half_to_mem = store_half_to_mem;
this.store_byte_to_mem = store_byte_to_mem;
this.load_double_from_mem = load_double_from_mem;
this.load_double_from_mem_raw = load_double_from_mem_raw;
this.load_word_from_mem = load_word_from_mem;
this.load_half_from_mem = load_half_from_mem;
this.load_byte_from_mem = load_byte_from_mem;
this.load_to_mem = load_to_mem;
this.set_pcr = set_pcr;
this.load_inst_from_mem = load_inst_from_mem;
}
function status_reg_init() {
// at RESET, processor starts with ET=0, S=1, VM=0
var srinit = 0x0;
// set EI to zero
srinit = srinit & (~SR["SR_EI"]);
// set S = 1 here
srinit = srinit | SR["SR_S"];
// set PS = 0 here
srinit = srinit & (~SR["SR_PS"]);
// VM is off at boot, turned on by kernel
srinit = srinit & (~SR["SR_VM"]);
// now force implementation defined presets
srinit = status_reg_force(srinit);
return srinit;
}
// "hardwired" values that need to be forced every time status reg is modified
function status_reg_force(input) {
// force EF to zero here (no FP insts)
// force U64 to 1 here
// force S64 to 1 here
input = input & (~SR["SR_EF"]);
input = input | SR["SR_U64"] | SR["SR_S64"];
return input;
}