diff --git a/sketches/assemble-and-run.js b/sketches/assemble-and-run.js index 0882084..832be48 100644 --- a/sketches/assemble-and-run.js +++ b/sketches/assemble-and-run.js @@ -10,9 +10,9 @@ const filename = process.argv[2]; const inputFile_str = fs.readFileSync(filename, 'utf8'); let machineCode = assembler.assemble(inputFile_str); -console.log(); -console.group("Machine code output"); -printMemory.printTable(machineCode); -console.groupEnd('Machine code output'); +// console.group("Machine code output"); +// printMemory.printTable(machineCode); +// console.groupEnd('Machine code output'); -computer.runProgram(machineCode); \ No newline at end of file +//computer.debugProgram(machineCode); +computer.displayProgram(machineCode); \ No newline at end of file diff --git a/sketches/simulator-sketch-v3.js b/sketches/simulator-sketch-v3.js index 0df71c3..4401a2c 100644 --- a/sketches/simulator-sketch-v3.js +++ b/sketches/simulator-sketch-v3.js @@ -175,7 +175,7 @@ function stepCPU() { // console.groupEnd("Step CPU"); } -exports.runProgram = (code) => { +exports.debugProgram = (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 console.log(); @@ -197,6 +197,19 @@ exports.runProgram = (code) => { 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; + for (let i = 0; i < CYCLE_LIMIT; i++) { // FIXME: temporary limit as a lazy way to halt infinite loops + 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)); }; }