-
Notifications
You must be signed in to change notification settings - Fork 0
/
mimari.js
355 lines (313 loc) · 9.8 KB
/
mimari.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
// MimariJS main script file
// Copyright (c) 2024 Erdem Ersoy (eersoy93)
// Licensed with MIT License
"use strict";
// Class definitions
class Opcode {
constructor(name, code) {
this.name = name;
this.code = code;
}
}
class Instruction {
constructor(opcode, operands) {
this.opcode = opcode;
this.operands = operands;
}
}
class Register {
constructor(name, value) {
this.name = name;
this.value = value;
}
}
class Stack {
constructor() {
this.stack = [];
}
push(value) {
this.stack.push(value);
}
pop() {
return this.stack.pop();
}
peek() {
return this.stack[this.stack.length - 1];
}
}
class Traps {
constructor(name, value, args, func) {
this.name = name;
this.value = value;
this.args = args;
this.func = func;
}
}
class Program {
constructor(instructions) {
this.instructions = instructions;
}
}
class Machine {
constructor(memory, registers, stack, traps, program) {
this.memory = memory;
this.registers = registers;
this.stack = stack;
this.traps = traps;
this.program = program;
}
}
// Opcodes
const ADD = new Opcode('ADD', 0x0001);
const SUB = new Opcode('SUB', 0x0002);
const MUL = new Opcode('MUL', 0x0003);
const DIV = new Opcode('DIV', 0x0004);
const MOV = new Opcode('MOV', 0x0005);
const JMP = new Opcode('JMP', 0x0006);
const JZ = new Opcode('JZ', 0x0007);
const JNZ = new Opcode('JNZ', 0x0008);
const JE = new Opcode('JE', 0x0009);
const JNE = new Opcode('JNE', 0x000A);
const JL = new Opcode('JL', 0x000B);
const JLE = new Opcode('JLE', 0x000C);
const JG = new Opcode('JG', 0x000D);
const JGE = new Opcode('JGE', 0x000E);
const CMP = new Opcode('CMP', 0x000F);
const PUSH = new Opcode('PUSH', 0x0010);
const POP = new Opcode('POP', 0x0011);
const CALL = new Opcode('CALL', 0x0012);
const RET = new Opcode('RET', 0x0013);
const HLT = new Opcode('HLT', 0x0014);
const NOP = new Opcode('NOP', 0x0015);
const TRAP = new Opcode('TRAP', 0x0016);
// Instructions
var instructions = [
new Instruction(ADD, 2),
new Instruction(SUB, 2),
new Instruction(MUL, 2),
new Instruction(DIV, 2),
new Instruction(MOV, 2),
new Instruction(JMP, 1),
new Instruction(JZ, 1),
new Instruction(JNZ, 1),
new Instruction(JE, 1),
new Instruction(JNE, 1),
new Instruction(JL, 1),
new Instruction(JLE, 1),
new Instruction(JG, 1),
new Instruction(JGE, 1),
new Instruction(CMP, 2),
new Instruction(PUSH, 1),
new Instruction(POP, 1),
new Instruction(CALL, 1),
new Instruction(RET, 0),
new Instruction(HLT, 0),
new Instruction(NOP, 0),
new Instruction(TRAP, 1)
];
// Memory
var memory = new Array(1 * 1024 ^ 3);
// Registers
var registers = [
new Register('R0', 0), // Accumulator
new Register('R1', 0),
new Register('R2', 0),
new Register('R3', 0),
new Register('R4', 0),
new Register('R5', 0),
new Register('R6', 0),
new Register('R7', 0),
new Register('R8', 0),
new Register('R9', 0),
new Register('R10', 0),
new Register('R11', 0),
new Register('R12', 0),
new Register('R13', 0),
new Register('R14', 0),
new Register('R15', 0) // Program counter
];
// Stack
var stack = new Stack();
// Traps
var traps = [
new Traps('TRAP', 0, new Array(5), function(args) {
var screenCanvas = document.getElementById('screenCanvas');
var screenCanvasContext = screenCanvas.getContext('2d');
screenCanvasContext.fillStyle = 'rgb(' + args[2] + ', ' + args[3] + ', ' + args[4] + ')';
screenCanvasContext.fillRect(args[0], args[1], 1, 1);
}),
];
// The instructions to run
var instructions = [
new Instruction(ADD, [0, 5]),
new Instruction(ADD, [1, 6]),
new Instruction(MOV, [0, 1]),
new Instruction(TRAP, [0, [5, 5, 255, 255, 255]]),
new Instruction(HLT, []),
];
// Execute
function execute(instruction) {
switch (instruction.opcode) {
case ADD:
var operand1 = instruction.operands[0];
var operand2 = instruction.operands[1];
console.log("ADD " + operand1 + " " + operand2);
registers[operand1].value += operand2.value;
break;
case SUB:
var operand1 = instruction.operands[0];
var operand2 = instruction.operands[1];
console.log("SUB " + operand1 + " " + operand2);
registers[operand1].value -= operand2.value;
break;
case MUL:
var operand1 = instruction.operands[0];
var operand2 = instruction.operands[1];
console.log("MUL " + operand1 + " " + operand2);
registers[operand1].value *= operand2.value;
break;
case DIV:
var operand1 = instruction.operands[0];
var operand2 = instruction.operands[1];
console.log("DIV " + operand1 + " " + operand2);
registers[operand1].value /= operand2.value;
break;
case MOV:
var operand1 = instruction.operands[0];
var operand2 = instruction.operands[1];
console.log("MOV " + operand1 + " " + operand2);
registers[operand1].value = operand2.value;
break;
case JMP:
var operand1 = instruction.operands[0];
console.log("JMP " + operand1.value);
registers[15].value = operand1.value;
break;
case JZ:
var operand1 = instruction.operands[0];
console.log("JZ " + operand1);
if (registers[0].value == 0) {
registers[15].value = operand1;
}
break;
case JNZ:
var operand1 = instruction.operands[0];
console.log("JNZ " + operand1);
if (registers[0].value != 0) {
registers[15].value = operand1;
}
break;
case JE:
var operand1 = instruction.operands[0];
console.log("JE " + operand1);
if (registers[0].value == registers['R1'].value) {
registers[16].value = operand1;
}
break;
case JNE:
var operand1 = instruction.operands[0];
console.log("JNE " + operand1);
if (registers[0].value != registers['R1'].value) {
registers[15].value = operand1;
}
break;
case JL:
var operand1 = instruction.operands[0];
console.log("JL " + operand1);
if (registers[0].value < registers['R1'].value) {
registers[15].value = operand1;
}
break;
case JLE:
var operand1 = instruction.operands[0];
console.log("JLE " + operand1);
if (registers[0].value <= registers['R1'].value) {
registers[15].value = operand1;
}
break;
case JG:
var operand1 = instruction.operands[0];
console.log("JG " + operand1);
if (registers[0].value > registers['R1'].value) {
registers[15].value = operand1;
}
break;
case JGE:
var operand1 = instruction.operands[0];
console.log("JGE " + operand1);
if (registers[0].value >= registers['R1'].value) {
registers[15].value = operand1;
}
break;
case CMP:
var operand1 = instruction.operands[0];
var operand2 = instruction.operands[1];
console.log("CMP " + operand1 + " " + operand2);
if (operand1 == operand2) {
registers[0].value = 0;
} else if (operand1 < operand2) {
registers[0].value = -1;
} else {
registers[0].value = 1;
}
break;
case PUSH:
var operand1 = instruction.operands[0];
console.log("PUSH " + operand1);
stack.push(operand1);
break;
case POP:
var operand1 = instruction.operands[0];
console.log("POP " + operand1);
operand1 = stack.pop
break;
case CALL:
var operand1 = instruction.operands[0];
console.log("CALL " + operand1);
stack.push(registers[16].value);
registers[15].value = operand1;
break;
case RET:
console.log("RET");
registers[15].value = stack.pop();
break;
case HLT:
console.log("HLT");
break;
case NOP:
console.log("NOP");
break;
case TRAP:
var operand1 = instruction.operands[0];
var operand2 = instruction.operands[1];
console.log("TRAP " + operand1 + " " + operand2);
traps[operand1].func(operand2);
break;
}
registers[15].value++;
}
// Main procedure
function main(machine) {
while (true) {
execute(machine.program.instructions[machine.registers[15].value]);
if (machine.program.instructions[machine.registers[15].value].opcode == HLT) {
alert("Machine halted!");
break;
}
}
}
// Initialize a new program
var program = new Program(instructions);
// Load the program to memory
for (var i = 0; i < program.instructions.length; i++) {
memory[i] = program.instructions[i].opcode.code;
}
// Initialize the machine
var machine = new Machine(memory, registers, stack, traps, program);
// Initialize the screen
var screenCanvas = document.getElementById('screenCanvas');
var screenCanvasContext = screenCanvas.getContext('2d');
screenCanvasContext.fillStyle = 'black';
screenCanvasContext.fillRect(0, 0, screenCanvas.width, screenCanvas.height);
// Run the machine
main(machine);