-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
41 lines (31 loc) · 902 Bytes
/
code.js
File metadata and controls
41 lines (31 loc) · 902 Bytes
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
import CPU from "./cpu.js";
import initMemory from "./memory.js";
import {MOV_R0, MOV_R1, ADD} from './instructions.js';
function buf2hex(buffer) {
const res = {};
[...new Uint8Array(buffer)].map((x, idx) => {
let idxstr = idx.toString(16).toUpperCase();
idxstr = idxstr.length === 1 ? `0${idxstr}` : idxstr;
res[idxstr.padStart(2, '0')] = `0x${x.toString(16).padStart(2, '0')}`;
});
console.log('res :>> ', res);
return res;
}
const mem = initMemory(256);
const bytes = new Uint8Array(mem.buffer);
bytes[0] = MOV_R0;
bytes[1] = 0x12;
bytes[2] = MOV_R1;
bytes[3] = 0x34;
bytes[4] = ADD;
bytes[5] = 2;
bytes[6] = 3;
const cpu = new CPU(mem);
cpu.debug('initial values');
cpu.step();
cpu.debug('r0 value loaded');
cpu.step();
cpu.debug('r1 value loaded');
cpu.step();
cpu.debug('r0 and r1 are added into acc');
console.table(buf2hex(mem.buffer));