Enforce greater separation between core logic functions and display output
This commit is contained in:
parent
ac14aa37c2
commit
cec80d7054
|
|
@ -4,7 +4,7 @@
|
|||
const fs = require('fs');
|
||||
const computer = require('./simulator.js');
|
||||
const assembler = require('./assembler.js');
|
||||
const { logMemory } = require('./logging.js');
|
||||
const { logRunningHeader, logMemory } = require('./logging.js');
|
||||
|
||||
const mode = process.argv[2];
|
||||
|
||||
|
|
@ -12,6 +12,7 @@ const mode = process.argv[2];
|
|||
const filename = process.argv[3];
|
||||
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
||||
|
||||
logRunningHeader();
|
||||
let machineCode;
|
||||
if (mode === "debug") {
|
||||
machineCode = assembler.assemble(inputFile_str);
|
||||
|
|
|
|||
42
simulator.js
42
simulator.js
|
|
@ -1,5 +1,5 @@
|
|||
const { INITIAL_IP_ADDRESS, CYCLE_LIMIT } = require('./machine.config');
|
||||
const { logRunningHeader, num2hex } = require('./logging.js');
|
||||
const { num2hex } = require('./logging.js');
|
||||
const display = require('./display.js');
|
||||
|
||||
// STATE
|
||||
|
|
@ -167,6 +167,11 @@ const opcodes2mnemonics = {
|
|||
14: (arg) => Instructions.carry_hop(arg),
|
||||
};
|
||||
|
||||
function startCPU(code) {
|
||||
CPU.loadMemory(code);
|
||||
CPU.running = true;
|
||||
}
|
||||
|
||||
function stepCPU() {
|
||||
CPU.currentInstruction.opcode = CPU.memory[CPU.IP];
|
||||
CPU.currentInstruction.argument = CPU.memory[CPU.IP+1];
|
||||
|
|
@ -175,37 +180,32 @@ function stepCPU() {
|
|||
}
|
||||
|
||||
exports.runProgram = async (code, debug = false) => {
|
||||
if (debug) logRunningHeader();
|
||||
|
||||
CPU.loadMemory(code);
|
||||
CPU.running = true;
|
||||
startCPU(code);
|
||||
let step = 0;
|
||||
while (true) {
|
||||
step = step + 1;
|
||||
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;
|
||||
|
||||
stepCPU();
|
||||
|
||||
console.group(`Step`);
|
||||
if (!debug) console.clear();
|
||||
display.printDisplay(CPU.memory);
|
||||
|
||||
logCPUState();
|
||||
if (!debug) await new Promise(resolve => setTimeout(resolve, 75));
|
||||
console.groupEnd('Step');
|
||||
await logCPUState(debug);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// FUNCTIONS THAT PULL INFO FROM STATE TO DISPLAY
|
||||
|
||||
function logCPUState() {
|
||||
console.log();
|
||||
console.log('Mnemonic:', CPU.currentInstruction.mnemonic);
|
||||
console.log(`Machine: $${num2hex(CPU.currentInstruction.opcode)} $${num2hex(CPU.currentInstruction.argument)}`);
|
||||
console.log();
|
||||
console.log( `IP: $${num2hex(CPU.IP)} Acc: $${num2hex(CPU.Acc)} CF: ${CPU.CF} ${CPU.running ? "running" : "halted" }` );
|
||||
console.log();
|
||||
async function logCPUState(debug = false) {
|
||||
console.group(`Step`);
|
||||
if (!debug) console.clear();
|
||||
display.printDisplay(CPU.memory);
|
||||
console.log();
|
||||
console.log('Mnemonic:', CPU.currentInstruction.mnemonic);
|
||||
console.log(`Machine: $${num2hex(CPU.currentInstruction.opcode)} $${num2hex(CPU.currentInstruction.argument)}`);
|
||||
console.log();
|
||||
console.log(`IP: $${num2hex(CPU.IP)} Acc: $${num2hex(CPU.Acc)} CF: ${CPU.CF} ${CPU.running ? "running" : "halted" }`);
|
||||
console.log();
|
||||
// Pause to show animated display:
|
||||
if (!debug) await new Promise(resolve => setTimeout(resolve, 75));
|
||||
console.groupEnd('Step');
|
||||
};
|
||||
Loading…
Reference in New Issue