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 computer = require('./simulator.js');
|
||||
const assembler = require('./assembler.js');
|
||||
const printMemory = require('./print-memory.js');
|
||||
|
||||
const mode = process.argv[2];
|
||||
|
||||
// console.log(`Reading ${filename}`);
|
||||
const filename = process.argv[2];
|
||||
const filename = process.argv[3];
|
||||
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
||||
let machineCode = assembler.assemble(inputFile_str);
|
||||
|
||||
|
|
@ -14,5 +17,8 @@ let machineCode = assembler.assemble(inputFile_str);
|
|||
// printMemory.printTable(machineCode);
|
||||
// console.groupEnd('Machine code output');
|
||||
|
||||
computer.debugProgram(machineCode);
|
||||
//computer.displayProgram(machineCode);
|
||||
if (mode === "debug") {
|
||||
computer.runProgram(machineCode, true);
|
||||
} else {
|
||||
computer.runProgram(machineCode, false);
|
||||
}
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
{
|
||||
"name": "paper-computer",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
46
simulator.js
46
simulator.js
|
|
@ -174,49 +174,25 @@ function stepCPU() {
|
|||
// console.groupEnd("Step CPU");
|
||||
}
|
||||
|
||||
exports.debugProgram = (code) => {
|
||||
console.log();
|
||||
let time = new Date();
|
||||
console.log( `┌─────────────────────┐`);
|
||||
// Running at 11:48:15 AM
|
||||
console.log( `│ Running at ${time.toLocaleTimeString('en-GB')} │` );
|
||||
console.log( `└─────────────────────┘`);
|
||||
exports.runProgram = async (code, debug = false) => {
|
||||
if (debug) logRunningHeader();
|
||||
|
||||
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
|
||||
logCPUState();
|
||||
if (debug) logCPUState();
|
||||
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 (CYCLE_LIMIT && (step > CYCLE_LIMIT)) { break; } // Temporary limit as a lazy way to halt infinite loops:
|
||||
if (!CPU.running) break;
|
||||
if (CPU.IP >= CPU.memory.length) break;
|
||||
|
||||
if (!debug) console.clear();
|
||||
console.group('Display')
|
||||
display.printDisplay(CPU.memory);
|
||||
console.log();
|
||||
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();
|
||||
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.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