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 fs = require('fs');
|
||||||
const computer = require('./simulator.js');
|
const computer = require('./simulator.js');
|
||||||
const assembler = require('./assembler.js');
|
const assembler = require('./assembler.js');
|
||||||
const { logMemory } = require('./logging.js');
|
const { logRunningHeader, logMemory } = require('./logging.js');
|
||||||
|
|
||||||
const mode = process.argv[2];
|
const mode = process.argv[2];
|
||||||
|
|
||||||
|
|
@ -12,6 +12,7 @@ const mode = process.argv[2];
|
||||||
const filename = process.argv[3];
|
const filename = process.argv[3];
|
||||||
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
const inputFile_str = fs.readFileSync(filename, 'utf8');
|
||||||
|
|
||||||
|
logRunningHeader();
|
||||||
let machineCode;
|
let machineCode;
|
||||||
if (mode === "debug") {
|
if (mode === "debug") {
|
||||||
machineCode = assembler.assemble(inputFile_str);
|
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 { INITIAL_IP_ADDRESS, CYCLE_LIMIT } = require('./machine.config');
|
||||||
const { logRunningHeader, num2hex } = require('./logging.js');
|
const { num2hex } = require('./logging.js');
|
||||||
const display = require('./display.js');
|
const display = require('./display.js');
|
||||||
|
|
||||||
// STATE
|
// STATE
|
||||||
|
|
@ -167,6 +167,11 @@ const opcodes2mnemonics = {
|
||||||
14: (arg) => Instructions.carry_hop(arg),
|
14: (arg) => Instructions.carry_hop(arg),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function startCPU(code) {
|
||||||
|
CPU.loadMemory(code);
|
||||||
|
CPU.running = true;
|
||||||
|
}
|
||||||
|
|
||||||
function stepCPU() {
|
function stepCPU() {
|
||||||
CPU.currentInstruction.opcode = CPU.memory[CPU.IP];
|
CPU.currentInstruction.opcode = CPU.memory[CPU.IP];
|
||||||
CPU.currentInstruction.argument = CPU.memory[CPU.IP+1];
|
CPU.currentInstruction.argument = CPU.memory[CPU.IP+1];
|
||||||
|
|
@ -175,37 +180,32 @@ function stepCPU() {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.runProgram = async (code, debug = false) => {
|
exports.runProgram = async (code, debug = false) => {
|
||||||
if (debug) logRunningHeader();
|
startCPU(code);
|
||||||
|
|
||||||
CPU.loadMemory(code);
|
|
||||||
CPU.running = true;
|
|
||||||
let step = 0;
|
let step = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
step = step + 1;
|
step = step + 1;
|
||||||
if (CYCLE_LIMIT && (step > CYCLE_LIMIT)) { break; } // 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 (!CPU.running) break;
|
if (!CPU.running) break;
|
||||||
if (CPU.IP >= CPU.memory.length) break;
|
if (CPU.IP >= CPU.memory.length) break;
|
||||||
|
|
||||||
stepCPU();
|
stepCPU();
|
||||||
|
await logCPUState(debug);
|
||||||
console.group(`Step`);
|
|
||||||
if (!debug) console.clear();
|
|
||||||
display.printDisplay(CPU.memory);
|
|
||||||
|
|
||||||
logCPUState();
|
|
||||||
if (!debug) await new Promise(resolve => setTimeout(resolve, 75));
|
|
||||||
console.groupEnd('Step');
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// FUNCTIONS THAT PULL INFO FROM STATE TO DISPLAY
|
// FUNCTIONS THAT PULL INFO FROM STATE TO DISPLAY
|
||||||
|
|
||||||
function logCPUState() {
|
async function logCPUState(debug = false) {
|
||||||
console.log();
|
console.group(`Step`);
|
||||||
console.log('Mnemonic:', CPU.currentInstruction.mnemonic);
|
if (!debug) console.clear();
|
||||||
console.log(`Machine: $${num2hex(CPU.currentInstruction.opcode)} $${num2hex(CPU.currentInstruction.argument)}`);
|
display.printDisplay(CPU.memory);
|
||||||
console.log();
|
console.log();
|
||||||
console.log( `IP: $${num2hex(CPU.IP)} Acc: $${num2hex(CPU.Acc)} CF: ${CPU.CF} ${CPU.running ? "running" : "halted" }` );
|
console.log('Mnemonic:', CPU.currentInstruction.mnemonic);
|
||||||
console.log();
|
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