Add JSdoc annotatinos

This commit is contained in:
n loewen 2023-08-03 16:42:19 +01:00
parent 0cd770e4c9
commit 6ebebe294e
3 changed files with 35 additions and 7 deletions

View File

@ -27,8 +27,11 @@ const mnemonics2opcodes = {
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);
machineCode[0] = POINTER_TO_START_OF_DISPLAY_MEM;
@ -155,7 +158,7 @@ function decodeInstructions(str) {
}
// DECODE!
op = mnemonics2opcodes[opName][addressingMode];
const op = mnemonics2opcodes[opName][addressingMode];
machineCode.push(op);
machineCode.push(arg_num);
@ -166,7 +169,7 @@ function decodeInstructions(str) {
dbg(1, '');
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');
// Backfill label references

View File

@ -1,6 +1,10 @@
const { POINTER_TO_START_OF_DISPLAY_MEM } = require('./machine.config');
const { num2hex } = require('./logging.js');
/**
* Print the contents of display memory as hex number
* @param {Uint8Array} mem - CPU memory
**/
const printDisplay = (mem) => {
const disp = mem[POINTER_TO_START_OF_DISPLAY_MEM];
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 disp = mem[POINTER_TO_START_OF_DISPLAY_MEM];
const num2pic = (n) => n > 0 ? '⚫' : '⚪';

View File

@ -9,10 +9,13 @@ const CPU = {
CF: 0,
Acc: 0,
memory: null,
loadMemory: (data) => { // data: Uint8Array
if (data.length > 256) { throw new Error("Out of memory error (program too long)"); }
CPU.memory = data;
/** @param {Uint8Array} data */
loadMemory: (data) => {
if (data.length > 256) { throw new Error("Out of memory error (program too long)"); }
CPU.memory = data;
},
currentInstruction: {
opcode: null,
argument: null,
@ -173,11 +176,18 @@ const opcodes2mnemonics = {
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) {
CPU.loadMemory(code);
CPU.running = true;
}
/**
* Execute just the next instruction in memory
**/
function stepCPU() {
CPU.currentInstruction.opcode = CPU.memory[CPU.IP];
CPU.currentInstruction.argument = CPU.memory[CPU.IP+1];
@ -185,6 +195,10 @@ function stepCPU() {
executeInstruction(CPU.currentInstruction.argument);
}
/**
* @param {Uint8Array} code - Machine code to run
* @param {Boolean} [debug] - Enable/disable debugging printouts
**/
exports.runProgram = async (code, debug = false) => {
startCPU(code);
let step = 0;
@ -201,6 +215,9 @@ exports.runProgram = async (code, debug = false) => {
// FUNCTIONS THAT PULL INFO FROM STATE TO DISPLAY
/**
* @param {Boolean} [debug] - Enable/disable debugging printouts
**/
async function logCPUState(debug = false) {
console.group(`Step`);
if (!debug) console.clear();