-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (52 loc) · 1.48 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
const readline = require('readline');
const CPU = require('./cpu');
const createMemory = require('./create-memory');
const MemoryMapper = require('./memory-mapper');
const createScreenDevice = require('./screen-device');
const {
MOV_LIT_REG,
MOV_REG_MEM,
MOV_MEM_REG,
ADD_REG_REG,
JMP_NOT_EQ,
PSH_LIT,
PSH_REG,
POP,
CAL_LIT,
RET,
HLT
} = require('./instruction');
const IP = 0;
const ACC = 1;
const R1 = 2;
const R2 = 3;
const R3 = 4;
const R4 = 5;
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 writeCharToScreen = (char, command, position) => {
writableBytes[i++] = MOV_LIT_REG;
writableBytes[i++] = command;
writableBytes[i++] = char.charCodeAt(0);
writableBytes[i++] = R1;
writableBytes[i++] = MOV_REG_MEM;
writableBytes[i++] = R1
writableBytes[i++] = 0x30;
writableBytes[i++] = position;
};
// Clear the screen before printing anything
writeCharToScreen(' ', 0xff, 0);
for(let index = 0; index <= 0xff; index++) {
const command = index % 2 === 0
? 0x01 // Bold character
: 0x02 // Regular character
writeCharToScreen('*', command, index);
}
writableBytes[i++] = HLT;
cpu.run();