-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (55 loc) · 1.6 KB
/
index.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
import './style.css';
const createMemory = require('./VM/create-memory');
const CPU = require('./VM/cpu');
const instructions = require('./VM/instructions');
const MemoryMapper = require('./VM/memory-mapper');
const createScreenDevice = require('./VM/screen-device');
const IP = 0;
const ACC = 1;
const R1 = 2;
const R2 = 3;
const R3 = 4;
const R4 = 5;
const R5 = 6;
const R6 = 7;
const R7 = 8;
const R8 = 9;
const SP = 10;
const FP = 11;
const MM = new MemoryMapper();
const memory = createMemory(256 * 256);
MM.map(memory, 0, 0xffff);
//Map 0xFF bytes of the address space to an "output device" - just stdout
MM.map(createScreenDevice(), 0x3000, 0x30ff, true);
const writableBytes = new Uint8Array(memory.buffer);
const cpu = new CPU(MM);
let i = 0;
const writeChatIntoTerminal = (char, position) => {
writableBytes[i++] = instructions.MOV_LIT_REG;
writableBytes[i++] = 0x00;
writableBytes[i++] = char.charCodeAt(0);
writableBytes[i++] = R1;
writableBytes[i++] = instructions.MOV_REG_MEM;
writableBytes[i++] = R1;
writableBytes[i++] = 0x30;
writableBytes[i++] = position;
};
'Hello World!'.split('').forEach((char, index) => {
writeChatIntoTerminal(char, index);
});
writableBytes[i++] = instructions.HLT;
cpu.run();
// // Run the Code step by step
// {
// cpu.debug();
// cpu.viewMemoryAt(cpu.getRegister('ip'));
// cpu.viewMemoryAt(0xffff - 1 - 42, 44);
// document.addEventListener('keydown', (e) => {
// if (e.key == 'Enter') {
// cpu.step();
// cpu.debug();
// cpu.viewMemoryAt(cpu.getRegister('ip'));
// cpu.viewMemoryAt(0xffff - 1 - 42, 44);
// }
// });
// }