Tidy up and de-duplicate setup for switching between running for debug output vs. display output
This commit is contained in:
parent
daf3793f90
commit
5fecefdc91
|
|
@ -1,12 +1,15 @@
|
||||||
// Usage: `node assemble-and-run.js assembly.asm`
|
// Run: `node assemble-and-run.js run assembly.asm`
|
||||||
|
// Debug: `node assemble-and-run.js debug assembly.asm`
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const computer = require('./simulator.js');
|
const computer = require('./simulator.js');
|
||||||
const assembler = require('./assembler.js');
|
const assembler = require('./assembler.js');
|
||||||
const printMemory = require('./print-memory.js');
|
const printMemory = require('./print-memory.js');
|
||||||
|
|
||||||
|
const mode = process.argv[2];
|
||||||
|
|
||||||
// console.log(`Reading ${filename}`);
|
// console.log(`Reading ${filename}`);
|
||||||
const filename = process.argv[2];
|
const filename = process.argv[3];
|
||||||
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
||||||
let machineCode = assembler.assemble(inputFile_str);
|
let machineCode = assembler.assemble(inputFile_str);
|
||||||
|
|
||||||
|
|
@ -14,5 +17,8 @@ let machineCode = assembler.assemble(inputFile_str);
|
||||||
// printMemory.printTable(machineCode);
|
// printMemory.printTable(machineCode);
|
||||||
// console.groupEnd('Machine code output');
|
// console.groupEnd('Machine code output');
|
||||||
|
|
||||||
computer.debugProgram(machineCode);
|
if (mode === "debug") {
|
||||||
//computer.displayProgram(machineCode);
|
computer.runProgram(machineCode, true);
|
||||||
|
} else {
|
||||||
|
computer.runProgram(machineCode, false);
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
{
|
{
|
||||||
"name": "paper-computer",
|
"name": "paper-computer",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"assemble-and-run": "node assemble-and-run.js",
|
"display": "node assemble-and-run.js run",
|
||||||
|
"debug": "node assemble-and-run.js debug",
|
||||||
"assemble": "node assemble.js"
|
"assemble": "node assemble.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
46
simulator.js
46
simulator.js
|
|
@ -174,49 +174,25 @@ function stepCPU() {
|
||||||
// console.groupEnd("Step CPU");
|
// console.groupEnd("Step CPU");
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.debugProgram = (code) => {
|
exports.runProgram = async (code, debug = false) => {
|
||||||
console.log();
|
if (debug) logRunningHeader();
|
||||||
let time = new Date();
|
|
||||||
console.log( `┌─────────────────────┐`);
|
|
||||||
// Running at 11:48:15 AM
|
|
||||||
console.log( `│ Running at ${time.toLocaleTimeString('en-GB')} │` );
|
|
||||||
console.log( `└─────────────────────┘`);
|
|
||||||
|
|
||||||
CPU.loadMemory(code);
|
CPU.loadMemory(code);
|
||||||
const initialMemory = JSON.parse(JSON.stringify(CPU.memory)); // Hack to make a copy-by-value -- https://stackoverflow.com/questions/18829099/copy-a-variables-value-into-another
|
if (debug) logCPUState();
|
||||||
logCPUState();
|
|
||||||
CPU.running = true;
|
CPU.running = true;
|
||||||
let step = 0;
|
let step = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
step = step + 1;
|
step = step + 1;
|
||||||
// FIXME: temporary limit as a lazy way to halt infinite loops:
|
if (CYCLE_LIMIT && (step > CYCLE_LIMIT)) { break; } // Temporary limit as a lazy way to halt infinite loops:
|
||||||
if (CYCLE_LIMIT && (step > CYCLE_LIMIT)) { break; }
|
|
||||||
if (!CPU.running) break;
|
if (!CPU.running) break;
|
||||||
if (CPU.IP >= CPU.memory.length) break;
|
if (CPU.IP >= CPU.memory.length) break;
|
||||||
|
|
||||||
|
if (!debug) console.clear();
|
||||||
console.group('Display')
|
console.group('Display')
|
||||||
display.printDisplay(CPU.memory);
|
display.printDisplay(CPU.memory);
|
||||||
console.log();
|
|
||||||
console.groupEnd('Display');
|
console.groupEnd('Display');
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.displayProgram = async (code) => {
|
|
||||||
CPU.loadMemory(code);
|
|
||||||
const initialMemory = JSON.parse(JSON.stringify(CPU.memory)); // Hack to make a copy-by-value -- https://stackoverflow.com/questions/18829099/copy-a-variables-value-into-another
|
|
||||||
CPU.running = true;
|
|
||||||
|
|
||||||
let step = 0;
|
|
||||||
while (true) {
|
|
||||||
step = step + 1;
|
|
||||||
// FIXME: temporary limit as a lazy way to halt infinite loops:
|
|
||||||
if (CYCLE_LIMIT && (step > CYCLE_LIMIT)) { break; }
|
|
||||||
if (!CPU.running) break;
|
|
||||||
if (CPU.IP >= CPU.memory.length) break;
|
|
||||||
stepCPU();
|
|
||||||
console.clear();
|
|
||||||
display.printDisplay(CPU.memory);
|
|
||||||
logCPUState();
|
logCPUState();
|
||||||
await new Promise(resolve => setTimeout(resolve, 75));
|
if (!debug) await new Promise(resolve => setTimeout(resolve, 75));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -241,3 +217,11 @@ logTableTitled = (memory, tableTitle) => {
|
||||||
console.table(memory);
|
console.table(memory);
|
||||||
console.groupEnd(tableTitle);
|
console.groupEnd(tableTitle);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function logRunningHeader() {
|
||||||
|
console.log();
|
||||||
|
let time = new Date();
|
||||||
|
console.log( `┌─────────────────────┐`);
|
||||||
|
console.log( `│ Running at ${time.toLocaleTimeString('en-GB')} │` );
|
||||||
|
console.log( `└─────────────────────┘`);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue