Add JSdoc annotatinos
This commit is contained in:
parent
0cd770e4c9
commit
6ebebe294e
11
assembler.js
11
assembler.js
|
|
@ -27,8 +27,11 @@ const mnemonics2opcodes = {
|
||||||
nop: { direct: 15, indirect: 15 },
|
nop: { direct: 15, indirect: 15 },
|
||||||
};
|
};
|
||||||
|
|
||||||
function decodeInstructions(str) {
|
/**
|
||||||
let lines = str.split(/\n/); // returns an array of lines
|
* @param {String} line - One line of assembly to decode
|
||||||
|
**/
|
||||||
|
function decodeInstructions(line) {
|
||||||
|
let lines = line.split(/\n/); // returns an array of lines
|
||||||
|
|
||||||
let machineCode = new Array(INITIAL_IP_ADDRESS).fill(0);
|
let machineCode = new Array(INITIAL_IP_ADDRESS).fill(0);
|
||||||
machineCode[0] = POINTER_TO_START_OF_DISPLAY_MEM;
|
machineCode[0] = POINTER_TO_START_OF_DISPLAY_MEM;
|
||||||
|
|
@ -155,7 +158,7 @@ function decodeInstructions(str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DECODE!
|
// DECODE!
|
||||||
op = mnemonics2opcodes[opName][addressingMode];
|
const op = mnemonics2opcodes[opName][addressingMode];
|
||||||
|
|
||||||
machineCode.push(op);
|
machineCode.push(op);
|
||||||
machineCode.push(arg_num);
|
machineCode.push(arg_num);
|
||||||
|
|
@ -166,7 +169,7 @@ function decodeInstructions(str) {
|
||||||
|
|
||||||
dbg(1, '');
|
dbg(1, '');
|
||||||
dbgGroup(1, 'Memory before filling in label constants');
|
dbgGroup(1, 'Memory before filling in label constants');
|
||||||
dbgExec(1, () => logMemory(machineCode));
|
dbgExec(1, () => logMemory(new Uint8Array(machineCode)));
|
||||||
dbgGroupEnd(1, 'Memory before filling in label constants');
|
dbgGroupEnd(1, 'Memory before filling in label constants');
|
||||||
|
|
||||||
// Backfill label references
|
// Backfill label references
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
const { POINTER_TO_START_OF_DISPLAY_MEM } = require('./machine.config');
|
const { POINTER_TO_START_OF_DISPLAY_MEM } = require('./machine.config');
|
||||||
const { num2hex } = require('./logging.js');
|
const { num2hex } = require('./logging.js');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print the contents of display memory as hex number
|
||||||
|
* @param {Uint8Array} mem - CPU memory
|
||||||
|
**/
|
||||||
const printDisplay = (mem) => {
|
const printDisplay = (mem) => {
|
||||||
const disp = mem[POINTER_TO_START_OF_DISPLAY_MEM];
|
const disp = mem[POINTER_TO_START_OF_DISPLAY_MEM];
|
||||||
for (let i = disp; i < disp + 16; i += 4) {
|
for (let i = disp; i < disp + 16; i += 4) {
|
||||||
|
|
@ -8,6 +12,10 @@ const printDisplay = (mem) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print the contents of display memory using black and white emoji circles
|
||||||
|
* @param {Uint8Array} mem - CPU memory
|
||||||
|
**/
|
||||||
const prettyPrintDisplay = (mem) => {
|
const prettyPrintDisplay = (mem) => {
|
||||||
const disp = mem[POINTER_TO_START_OF_DISPLAY_MEM];
|
const disp = mem[POINTER_TO_START_OF_DISPLAY_MEM];
|
||||||
const num2pic = (n) => n > 0 ? '⚫' : '⚪';
|
const num2pic = (n) => n > 0 ? '⚫' : '⚪';
|
||||||
|
|
|
||||||
19
simulator.js
19
simulator.js
|
|
@ -9,10 +9,13 @@ const CPU = {
|
||||||
CF: 0,
|
CF: 0,
|
||||||
Acc: 0,
|
Acc: 0,
|
||||||
memory: null,
|
memory: null,
|
||||||
loadMemory: (data) => { // data: Uint8Array
|
|
||||||
|
/** @param {Uint8Array} data */
|
||||||
|
loadMemory: (data) => {
|
||||||
if (data.length > 256) { throw new Error("Out of memory error (program too long)"); }
|
if (data.length > 256) { throw new Error("Out of memory error (program too long)"); }
|
||||||
CPU.memory = data;
|
CPU.memory = data;
|
||||||
},
|
},
|
||||||
|
|
||||||
currentInstruction: {
|
currentInstruction: {
|
||||||
opcode: null,
|
opcode: null,
|
||||||
argument: null,
|
argument: null,
|
||||||
|
|
@ -173,11 +176,18 @@ const opcodes2mnemonics = {
|
||||||
15: (arg) => Instructions.no_op(),
|
15: (arg) => Instructions.no_op(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load code into memory and set CPU state to "running"
|
||||||
|
* @param {Uint8Array} code - Machine code to load
|
||||||
|
**/
|
||||||
function startCPU(code) {
|
function startCPU(code) {
|
||||||
CPU.loadMemory(code);
|
CPU.loadMemory(code);
|
||||||
CPU.running = true;
|
CPU.running = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute just the next instruction in memory
|
||||||
|
**/
|
||||||
function stepCPU() {
|
function stepCPU() {
|
||||||
CPU.currentInstruction.opcode = CPU.memory[CPU.IP];
|
CPU.currentInstruction.opcode = CPU.memory[CPU.IP];
|
||||||
CPU.currentInstruction.argument = CPU.memory[CPU.IP+1];
|
CPU.currentInstruction.argument = CPU.memory[CPU.IP+1];
|
||||||
|
|
@ -185,6 +195,10 @@ function stepCPU() {
|
||||||
executeInstruction(CPU.currentInstruction.argument);
|
executeInstruction(CPU.currentInstruction.argument);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Uint8Array} code - Machine code to run
|
||||||
|
* @param {Boolean} [debug] - Enable/disable debugging printouts
|
||||||
|
**/
|
||||||
exports.runProgram = async (code, debug = false) => {
|
exports.runProgram = async (code, debug = false) => {
|
||||||
startCPU(code);
|
startCPU(code);
|
||||||
let step = 0;
|
let step = 0;
|
||||||
|
|
@ -201,6 +215,9 @@ exports.runProgram = async (code, debug = false) => {
|
||||||
|
|
||||||
// FUNCTIONS THAT PULL INFO FROM STATE TO DISPLAY
|
// FUNCTIONS THAT PULL INFO FROM STATE TO DISPLAY
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Boolean} [debug] - Enable/disable debugging printouts
|
||||||
|
**/
|
||||||
async function logCPUState(debug = false) {
|
async function logCPUState(debug = false) {
|
||||||
console.group(`Step`);
|
console.group(`Step`);
|
||||||
if (!debug) console.clear();
|
if (!debug) console.clear();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue