forked from s-macke/jor1k
-
Notifications
You must be signed in to change notification settings - Fork 2
/
system.js
393 lines (338 loc) · 13.5 KB
/
system.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// -------------------------------------------------
// ------------------- SYSTEM ----------------------
// -------------------------------------------------
"use strict";
// common
var message = require('./messagehandler'); // global variable
var utils = require('./utils');
var RAM = require('./ram');
var bzip2 = require('./bzip2');
var elf = require('./elf');
var Timer = require('./timer');
var HTIF = require('./riscv/htif');
// CPU
var OR1KCPU = require('./or1k');
var RISCVCPU = require('./riscv');
// Devices
var UARTDev = require('./dev/uart');
var IRQDev = require('./dev/irq');
var TimerDev = require('./dev/timer');
var FBDev = require('./dev/framebuffer');
var EthDev = require('./dev/ethmac');
var ATADev = require('./dev/ata');
var RTCDev = require('./dev/rtc');
var TouchscreenDev = require('./dev/touchscreen');
var KeyboardDev = require('./dev/keyboard');
var SoundDev = require('./dev/sound');
var VirtIODev = require('./dev/virtio');
var Virtio9p = require('./dev/virtio/9p');
var VirtioDummy = require('./dev/virtio/dummy');
var VirtioInput = require('./dev/virtio/input');
var VirtioNET = require('./dev/virtio/net');
var VirtioBlock = require('./dev/virtio/block');
var VirtioGPU = require('./dev/virtio/gpu');
var VirtioConsole = require('./dev/virtio/console');
var FS = require('./filesystem/filesystem');
/*
Heap Layout
===========
The heap is needed by the asm.js CPU.
For compatibility all CPUs use the same layout
by using the different views of typed arrays
------ Core 1 ------
0x0 - 0x7F 32 CPU registers
0x80 - 0x1FFF CPU specific, usually unused or temporary data
0x2000 - 0x3FFF group 0 (system control and status)
0x4000 - 0x5FFF group 1 (data MMU)
0x6000 - 0x7FFF group 2 (instruction MMU)
------ Core 2 ------
0x8000 - 0x807F 32 CPU registers
0x8080 - 0x9FFF CPU specific, usually unused or temporary data
0xA000 - 0xBFFF group 0 (system control and status)
0xC000 - 0xDFFF group 1 (data MMU)
0xE000 - 0xFFFF group 2 (instruction MMU)
------ Core 3 ------
...
------- RAM --------
0x100000 - ... RAM
*/
var SYSTEM_RUN = 0x1;
var SYSTEM_STOP = 0x2;
var SYSTEM_HALT = 0x3; // Idle
function System() {
// the Init function is called by the master thread.
message.Register("LoadAndStart", this.LoadImageAndStart.bind(this) );
message.Register("execute", this.MainLoop.bind(this));
message.Register("Init", this.Init.bind(this) );
message.Register("Reset", this.Reset.bind(this) );
message.Register("ChangeCore", this.ChangeCPU.bind(this) );
message.Register("PrintOnAbort", this.PrintState.bind(this) );
message.Register("GetIPS", function(data) {
message.Send("GetIPS", this.ips);
this.ips=0;
}.bind(this));
}
System.prototype.CreateCPU = function(cpuname, arch) {
try {
if (arch == "or1k") {
this.cpu = new OR1KCPU(cpuname, this.ram, this.heap, this.ncores);
} else
if (arch == "riscv") {
this.cpu = new RISCVCPU(cpuname, this.ram, this.htif, this.heap, this.ncores);
} else
throw "Architecture " + arch + " not supported";
} catch (e) {
message.Debug("Error: failed to create CPU:" + e);
}
};
System.prototype.ChangeCPU = function(cpuname) {
this.cpu.switchImplementation(cpuname);
};
System.prototype.Reset = function() {
this.status = SYSTEM_STOP;
for(var i=0; i<this.devices.length; i++) {
this.devices[i].Reset();
}
this.ips = 0;
};
System.prototype.Init = function(system) {
this.status = SYSTEM_STOP;
this.memorysize = system.memorysize;
this.ncores = system.ncores;
if (!system.ncores) system.ncores = 1;
// this must be a power of two.
var ramoffset = 0x100000;
this.heap = new ArrayBuffer(this.memorysize*0x100000);
this.memorysize--; // - the lower 1 MB are used for the cpu cores
this.ram = new RAM(this.heap, ramoffset);
if (system.arch == "riscv") {
this.htif = new HTIF(this.ram, this);
}
this.CreateCPU(system.cpu, system.arch);
this.devices = [];
this.devices.push(this.cpu);
if (system.arch == "or1k") {
this.irqdev = new IRQDev(this);
this.timerdev = new TimerDev();
this.uartdev0 = new UARTDev(0, this, 0x2);
this.uartdev1 = new UARTDev(1, this, 0x3);
this.ethdev = new EthDev(this.ram, this);
this.ethdev.TransmitCallback = function(data){
message.Send("ethmac", data);
};
this.fbdev = new FBDev(this.ram);
this.atadev = new ATADev(this);
this.tsdev = new TouchscreenDev(this);
this.kbddev = new KeyboardDev(this);
this.snddev = new SoundDev(this, this.ram);
this.rtcdev = new RTCDev(this);
this.filesystem = new FS();
this.virtio9pdev = new Virtio9p(this.ram, this.filesystem);
this.virtiodev1 = new VirtIODev(this, 0x6, this.ram, this.virtio9pdev);
this.virtioinputdev = new VirtioInput(this.ram);
this.virtionetdev = new VirtioNET(this.ram);
this.virtioblockdev = new VirtioBlock(this.ram);
this.virtiodummydev = new VirtioDummy(this.ram);
this.virtiogpudev = new VirtioGPU(this.ram);
this.virtioconsoledev = new VirtioConsole(this.ram);
this.virtiodev2 = new VirtIODev(this, 0xB, this.ram, this.virtiodummydev);
this.virtiodev3 = new VirtIODev(this, 0xC, this.ram, this.virtiodummydev);
this.devices.push(this.irqdev);
this.devices.push(this.timerdev);
this.devices.push(this.uartdev0);
this.devices.push(this.uartdev1);
this.devices.push(this.ethdev);
this.devices.push(this.fbdev);
this.devices.push(this.atadev);
this.devices.push(this.tsdev);
this.devices.push(this.kbddev);
this.devices.push(this.snddev);
this.devices.push(this.rtcdev);
this.devices.push(this.virtio9pdev);
this.devices.push(this.virtiodev1);
this.devices.push(this.virtiodev2);
this.devices.push(this.virtiodev3);
this.devices.push(this.virtioinputdev);
this.devices.push(this.virtionetdev);
this.devices.push(this.virtioblockdev);
this.devices.push(this.virtiodummydev);
this.devices.push(this.virtiogpudev);
this.devices.push(this.virtioconsoledev);
this.ram.AddDevice(this.uartdev0, 0x90000000, 0x7);
this.ram.AddDevice(this.fbdev, 0x91000000, 0x1000);
this.ram.AddDevice(this.ethdev, 0x92000000, 0x1000);
this.ram.AddDevice(this.tsdev, 0x93000000, 0x1000);
this.ram.AddDevice(this.kbddev, 0x94000000, 0x100);
this.ram.AddDevice(this.uartdev1, 0x96000000, 0x7);
this.ram.AddDevice(this.virtiodev1, 0x97000000, 0x1000);
this.ram.AddDevice(this.snddev, 0x98000000, 0x400);
this.ram.AddDevice(this.rtcdev, 0x99000000, 0x1000);
this.ram.AddDevice(this.irqdev, 0x9A000000, 0x1000);
this.ram.AddDevice(this.timerdev, 0x9B000000, 0x1000);
this.ram.AddDevice(this.virtiodev2, 0x9C000000, 0x1000);
this.ram.AddDevice(this.virtiodev3, 0x9D000000, 0x1000);
this.ram.AddDevice(this.atadev, 0x9E000000, 0x1000);
} else
if (system.arch == "riscv") {
// at the moment the htif interface is part of the CPU initialization.
// However, it uses uartdev0
this.uartdev0 = new UARTDev(0, this, 0x2);
this.devices.push(this.uartdev0);
this.ram.AddDevice(this.uartdev0, 0x90000000, 0x7);
}
this.ips = 0; // external instruction per second counter
this.idletime = 0; // start time of the idle routine
this.idlemaxwait = 0; // maximum waiting time in cycles
// constants
this.ticksperms = 20000; // 20 MHz
this.loopspersecond = 100; // main loops per second, to keep the system responsive
this.timer = new Timer(this.ticksperms, this.loopspersecond);
};
System.prototype.RaiseInterrupt = function(line) {
//message.Debug("Raise " + line);
this.cpu.RaiseInterrupt(line, -1); // raise all cores
if (this.status == SYSTEM_HALT)
{
this.status = SYSTEM_RUN;
clearTimeout(this.idletimeouthandle);
var delta = (utils.GetMilliseconds() - this.idletime) * this.ticksperms;
if (delta > this.idlemaxwait) delta = this.idlemaxwait;
this.cpu.ProgressTime(delta);
this.MainLoop();
}
};
System.prototype.ClearInterrupt = function (line) {
this.cpu.ClearInterrupt(line, -1); // clear all cores
};
System.prototype.RaiseSoftInterrupt = function(line, cpuid) {
// the cpu cannot be halted when this function is called, so skip this check
this.cpu.RaiseInterrupt(line, cpuid);
};
System.prototype.ClearSoftInterrupt = function (line, cpuid) {
this.cpu.ClearInterrupt(line, cpuid);
};
System.prototype.PrintState = function() {
// Flush the buffer of the terminal
this.uartdev0 && this.uartdev0.Step();
this.uartdev1 && this.uartdev1.Step();
message.Debug(this.cpu.toString());
};
System.prototype.SendStringToTerminal = function(str)
{
var chars = [];
for (var i = 0; i < str.length; i++) {
chars.push(str.charCodeAt(i));
}
message.Send("tty0", chars);
};
System.prototype.LoadImageAndStart = function(url) {
this.SendStringToTerminal("\r================================================================================");
if (typeof url == 'string') {
this.SendStringToTerminal("\r\nLoading kernel and hard and basic file system from web server. Please wait ...\r\n");
utils.LoadBinaryResource(
url,
this.OnKernelLoaded.bind(this),
function(error){throw error;}
);
} else {
this.OnKernelLoaded(url);
}
};
System.prototype.PatchKernel = function(length)
{
var m = this.ram.uint8mem;
// set the correct memory size
for(var i=0; i<length; i++) { // search for the compiled dts file in the kernel
if (m[i+0] === 0x6d) // find "memory\0"
if (m[i+1] === 0x65)
if (m[i+2] === 0x6d)
if (m[i+3] === 0x6f)
if (m[i+4] === 0x72)
if (m[i+5] === 0x79)
if (m[i+6] === 0x00)
if (m[i+24] === 0x01)
if (m[i+25] === 0xF0)
if (m[i+26] === 0x00)
if (m[i+27] === 0x00) {
m[i+24] = (this.memorysize*0x100000)>>24;
m[i+25] = (this.memorysize*0x100000)>>16;
m[i+26] = 0x00;
m[i+27] = 0x00;
}
}
};
System.prototype.OnKernelLoaded = function(buffer) {
this.SendStringToTerminal("Decompressing kernel...\r\n");
var buffer8 = new Uint8Array(buffer);
var length = 0;
if (elf.IsELF(buffer8)) {
elf.Extract(buffer8, this.ram.uint8mem);
} else
if (bzip2.IsBZIP2(buffer8)) {
bzip2.simple(buffer8, function(x){this.ram.uint8mem[length++] = x;}.bind(this));
if (elf.IsELF(this.ram.uint8mem)) {
var temp = new Uint8Array(length);
for(var i=0; i<length; i++) {
temp[i] = this.ram.uint8mem[i];
}
elf.Extract(temp, this.ram.uint8mem);
}
} else {
length = buffer8.length;
for(var i=0; i<length; i++) this.ram.uint8mem[i] = buffer8[i];
}
this.PatchKernel(length);
if (this.cpu.littleendian == false) {
this.ram.Little2Big(length);
}
message.Debug("Kernel loaded: " + length + " bytes");
this.SendStringToTerminal("Booting\r\n");
this.SendStringToTerminal("================================================================================");
// we can start the boot process already, even if the filesystem is not yet ready
this.cpu.Reset();
this.cpu.AnalyzeImage();
message.Debug("Starting emulation");
this.status = SYSTEM_RUN;
message.Send("execute", 0);
};
// the kernel has sent a halt signal, so stop everything until the next interrupt is raised
System.prototype.HandleHalt = function() {
var delta = this.cpu.GetTimeToNextInterrupt();
if (delta == -1) return;
this.idlemaxwait = delta;
var mswait = Math.floor(delta / this.ticksperms / this.timer.correction + 0.5);
//message.Debug("wait " + mswait);
if (mswait <= 1) return;
if (mswait > 1000) message.Debug("Warning: idle for " + mswait + "ms");
this.idletime = utils.GetMilliseconds();
this.status = SYSTEM_HALT;
this.idletimeouthandle = setTimeout(function() {
if (this.status == SYSTEM_HALT) {
this.status = SYSTEM_RUN;
this.cpu.ProgressTime(delta);
//this.snddev.Progress();
this.MainLoop();
}
}.bind(this), mswait);
};
System.prototype.MainLoop = function() {
if (this.status != SYSTEM_RUN) return;
message.Send("execute", 0);
// execute the cpu loop for "instructionsperloop" instructions.
var stepsleft = this.cpu.Step(this.timer.instructionsperloop, this.timer.timercyclesperinstruction);
//message.Debug(stepsleft);
var totalsteps = this.timer.instructionsperloop - stepsleft;
totalsteps++; // at least one instruction
this.ips += totalsteps;
this.uartdev0 && this.uartdev0.Step();
this.uartdev1 && this.uartdev1.Step();
//this.snddev.Progress();
// stepsleft != 0 indicates CPU idle
var gotoidle = stepsleft?true:false;
this.timer.Update(totalsteps, this.cpu.GetTicks(), gotoidle);
if (gotoidle) {
this.HandleHalt();
}
// go to worker thread idle state that onmessage is executed
};
module.exports = System;