439 lines
13 KiB
JavaScript
439 lines
13 KiB
JavaScript
const readline = require('readline');
|
||
const readlineSync = require('readline-sync');
|
||
|
||
const {
|
||
INITIAL_IP_ADDRESS,
|
||
DEFAULT_CYCLE_LIMIT,
|
||
KEYPAD_ADDR,
|
||
KEY_MAP,
|
||
} = require('./machine.config');
|
||
|
||
const {
|
||
num2hex,
|
||
bool2bit,
|
||
} = require('./logging.js');
|
||
const display = require('./display.js');
|
||
|
||
// STATE
|
||
const CPU = {
|
||
|
||
/** Core state **/
|
||
state: {
|
||
running: false,
|
||
IP: INITIAL_IP_ADDRESS,
|
||
flags: {'C': false, 'Z': false, 'N': false, 'O': false},
|
||
FLAGNUMS2NAMES: {0: 'C', 1: 'Z', 2: 'N', 3: 'O'},
|
||
acc: 0,
|
||
memory: null,
|
||
},
|
||
|
||
/** Debug info **/
|
||
debug: {
|
||
previousIP: 0,
|
||
currentInstruction: {
|
||
opcode: null,
|
||
operand: null,
|
||
mnemonic: null,
|
||
},
|
||
cycleCounter: 0,
|
||
},
|
||
|
||
/** Functions that update state **/
|
||
|
||
/** @param {Uint8Array} data */
|
||
loadMemory(data) {
|
||
this.state.memory = new Uint8Array(256);
|
||
this.state.memory.set(data, 0);
|
||
},
|
||
|
||
incrementIP(offset) {
|
||
this.state.previousIP = this.state.IP;
|
||
this.state.IP = this.state.IP + offset;
|
||
},
|
||
|
||
setIP(address) {
|
||
this.state.previousIP = this.state.IP;
|
||
this.state.IP = address;
|
||
},
|
||
|
||
updateFlagZero() { this.state.flags.Z = this.state.acc === 0; },
|
||
updateFlagNegative() { this.state.acc & 128 ? this.state.flags.N = true : this.state.flags.N = false },
|
||
|
||
|
||
/** Hooks **/
|
||
|
||
onTickHooks: [],
|
||
|
||
/** @param {function} fn **/
|
||
onTick(fn) { this.onTickHooks.push(fn) },
|
||
}
|
||
|
||
|
||
// FUNCTIONS THAT MODIFY STATE
|
||
|
||
const Instructions = {
|
||
end: () => {
|
||
CPU.debug.currentInstruction.mnemonic = 'END';
|
||
CPU.state.running = false;
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
store_lit: (lit) => {
|
||
CPU.debug.currentInstruction.mnemonic = 'STO lit';
|
||
CPU.state.memory[lit] = CPU.state.acc;
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
store_addr: (addr) => {
|
||
CPU.debug.currentInstruction.mnemonic = `STO addr; @addr: ${num2hex(CPU.state.memory[addr])}`;
|
||
CPU.state.memory[CPU.state.memory[addr]] = CPU.state.acc;
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
load_lit: (lit) => {
|
||
CPU.debug.currentInstruction.mnemonic = 'LDA lit';
|
||
CPU.state.acc = lit;
|
||
CPU.updateFlagNegative();
|
||
CPU.updateFlagZero();
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
load_addr: (addr) => {
|
||
CPU.debug.currentInstruction.mnemonic = `LDA addr; @ addr: ${num2hex(CPU.state.memory[addr])}`;
|
||
CPU.state.acc = CPU.state.memory[addr];
|
||
CPU.updateFlagNegative();
|
||
CPU.updateFlagZero();
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
add_lit: (lit) => {
|
||
CPU.debug.currentInstruction.mnemonic = 'ADD lit';
|
||
// Calculate sum
|
||
let sum = CPU.state.acc + lit;
|
||
if (sum > 255) {
|
||
CPU.state.flags.C = true;
|
||
sum = (sum % 255) - 1;
|
||
} else {
|
||
CPU.state.flags.C = false;
|
||
}
|
||
// Calculate overflow flag status
|
||
let bitSixCarry = 0;
|
||
if ((CPU.state.acc & 64) && (lit & 64)) { bitSixCarry = 1; }
|
||
// let overflow = bitSixCarry ^ (CPU.state.flags & 8);
|
||
// FIXME FIXME FIXME
|
||
// I'm on a plane and can't remember how this works
|
||
let overflow = 0;
|
||
if (overflow) {
|
||
CPU.state.flags.O = true;
|
||
} else {
|
||
CPU.state.flags.O = false;
|
||
}
|
||
CPU.state.acc = sum;
|
||
CPU.updateFlagNegative();
|
||
CPU.updateFlagZero();
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
add_addr: (addr) => {
|
||
CPU.debug.currentInstruction.mnemonic = 'ADD addr';
|
||
// Calculate sum
|
||
let sum = CPU.state.acc + CPU.state.memory[addr];
|
||
if (sum > 255) {
|
||
CPU.state.flags.C = true;
|
||
sum = (sum % 255) - 1;
|
||
} else {
|
||
CPU.state.flags.C = false;
|
||
}
|
||
// Calculate overflow flag status
|
||
let bitSixCarry = 0;
|
||
if ((CPU.state.acc & 64) && (addr & 64)) { bitSixCarry = 1; }
|
||
// let overflow = bitSixCarry ^ (CPU.state.flags & 8);
|
||
// FIXME FIXME FIXME
|
||
// I'm on a plane and can't remember how this works
|
||
let overflow = 0;
|
||
if (overflow) {
|
||
CPU.state.flags.O = true;
|
||
} else {
|
||
CPU.state.flags.O = false;
|
||
}
|
||
CPU.state.acc = sum;
|
||
CPU.updateFlagNegative();
|
||
CPU.updateFlagZero();
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
sub_lit: (lit) => {
|
||
CPU.debug.currentInstruction.mnemonic = 'SUB lit';
|
||
// Calculate sum
|
||
let sum = CPU.state.acc - lit;
|
||
if (sum < 0) {
|
||
CPU.state.flags.C = true;
|
||
sum = sum + 256;
|
||
} else {
|
||
CPU.state.flags.C = false;
|
||
}
|
||
// Calculate overflow flag status
|
||
let bitSixCarry = 0;
|
||
if ((CPU.state.acc & 64) && (lit & 64)) { bitSixCarry = 1; }
|
||
// let overflow = bitSixCarry ^ (CPU.state.flags & 8);
|
||
// FIXME FIXME FIXME
|
||
// I'm on a plane and can't remember how this works
|
||
let overflow = 0;
|
||
if (overflow) {
|
||
CPU.state.flags.O = true;
|
||
} else {
|
||
CPU.state.flags.O = false;
|
||
}
|
||
CPU.state.acc = sum;
|
||
CPU.updateFlagNegative();
|
||
CPU.updateFlagZero();
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
sub_addr: (addr) => {
|
||
CPU.debug.currentInstruction.mnemonic = 'SUB addr';
|
||
// Calculate sum
|
||
let sum = CPU.state.acc - CPU.state.memory[addr];
|
||
if (sum < 0) {
|
||
CPU.state.flags.C = true;
|
||
sum = sum + 256;
|
||
} else {
|
||
CPU.state.flags.C = false;
|
||
}
|
||
// Calculate overflow flag status
|
||
let bitSixCarry = 0;
|
||
if ((CPU.state.acc & 64) && (addr & 64)) { bitSixCarry = 1; }
|
||
// let overflow = bitSixCarry ^ (CPU.state.flags & 8);
|
||
// FIXME FIXME FIXME
|
||
// I'm on a plane and can't remember how this works
|
||
let overflow = 0;
|
||
if (overflow) {
|
||
CPU.state.flags.O = true;
|
||
} else {
|
||
CPU.state.flags.O = false;
|
||
}
|
||
CPU.state.acc = sum;
|
||
CPU.updateFlagNegative();
|
||
CPU.updateFlagZero();
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
hop_lit: (lit) => {
|
||
CPU.debug.currentInstruction.mnemonic = `HOP lit; IP+2: ${CPU.state.memory[CPU.state.IP+2]}, IP+3: ${CPU.state.memory[CPU.state.IP+3]}`;
|
||
if (CPU.state.acc === lit) {
|
||
CPU.incrementIP(4);
|
||
} else {
|
||
CPU.incrementIP(2);
|
||
}
|
||
},
|
||
|
||
hop_addr: (addr) => {
|
||
CPU.debug.currentInstruction.mnemonic = 'HOP addr';
|
||
if (CPU.state.acc === CPU.state.memory[addr]) {
|
||
CPU.incrementIP(4);
|
||
} else {
|
||
CPU.incrementIP(2);
|
||
}
|
||
},
|
||
|
||
jump_lit: (lit) => {
|
||
CPU.debug.currentInstruction.mnemonic = 'JMP lit';
|
||
CPU.setIP(lit);
|
||
},
|
||
|
||
jump_addr: (addr) => {
|
||
CPU.debug.currentInstruction.mnemonic = 'JMP addr';
|
||
CPU.setIP(CPU.state.memory[addr]);
|
||
},
|
||
|
||
flag_toggle: (flagNum) => {
|
||
if (flagNum === null) {
|
||
console.error('Invalid flag number');
|
||
process.exit();
|
||
}
|
||
const flagName = CPU.FLAGNUMS2NAMES[flagNum];
|
||
CPU.debug.currentInstruction.mnemonic = `FTG ${flagName}`;
|
||
CPU.state.flags[flagName] = !CPU.state.flags[flagName];
|
||
CPU.incrementIP(2);
|
||
},
|
||
|
||
flag_hop: (flagNum) => {
|
||
if (flagNum === null) {
|
||
console.error('Invalid flag number');
|
||
process.exit();
|
||
}
|
||
const flagName = CPU.FLAGNUMS2NAMES[flagNum];
|
||
CPU.debug.currentInstruction.mnemonic = `FHP ${flagName}; IP+2: ${CPU.state.memory[CPU.state.IP+2]}, IP+3: ${CPU.state.memory[CPU.state.IP+3]}`;
|
||
if (CPU.state.flags[CPU.FLAGNUMS2NAMES[flagNum]]) {
|
||
CPU.incrementIP(4);
|
||
} else {
|
||
CPU.incrementIP(2);
|
||
}
|
||
},
|
||
|
||
no_op: () => {
|
||
CPU.debug.currentInstruction.mnemonic = `NOP`;
|
||
CPU.incrementIP(2);
|
||
},
|
||
}
|
||
|
||
const opcodes2mnemonics = {
|
||
0: (operand) => Instructions.end(),
|
||
1: (operand) => Instructions.store_lit(operand),
|
||
2: (operand) => Instructions.store_addr(operand),
|
||
3: (operand) => Instructions.load_lit(operand),
|
||
4: (operand) => Instructions.load_addr(operand),
|
||
5: (operand) => Instructions.add_lit(operand),
|
||
6: (operand) => Instructions.add_addr(operand),
|
||
7: (operand) => Instructions.sub_lit(operand),
|
||
8: (operand) => Instructions.sub_addr(operand),
|
||
9: (operand) => Instructions.hop_lit(operand),
|
||
10: (operand) => Instructions.hop_addr(operand),
|
||
11: (operand) => Instructions.jump_lit(operand),
|
||
12: (operand) => Instructions.jump_addr(operand),
|
||
13: (operand) => Instructions.flag_toggle(operand),
|
||
14: (operand) => Instructions.flag_hop(operand),
|
||
15: (operand) => Instructions.no_op(),
|
||
};
|
||
|
||
/**
|
||
* Load code into memory and set CPU state to "state.running"
|
||
* @param {Uint8Array} code - Machine code to load
|
||
**/
|
||
function startCPU(code) {
|
||
CPU.loadMemory(code);
|
||
CPU.debug.cycleCounter = 0;
|
||
CPU.state.running = true;
|
||
|
||
// FIXME: This conflicts with single-stepping
|
||
// (you can single-step, but keys aren't passed
|
||
// through to the Cardiograph)
|
||
//
|
||
// -> The fix is maybe to remove readlineSync,
|
||
// and instead stash the keypress into a buffer variable.*
|
||
// Then have the stepping function check that buffer,
|
||
// and then clear the buffer, each time it runs.
|
||
//
|
||
// * If it's in the set of keys that are relevant
|
||
// to single-stepping.
|
||
|
||
// Start listening for keypresses...
|
||
readline.emitKeypressEvents(process.stdin);
|
||
if (process.stdin.setRawMode != null) {
|
||
process.stdin.setRawMode(true);
|
||
}
|
||
process.stdin.on('keypress', (str, key) => { // TODO: is it possible to turn this off again?
|
||
if (key.sequence === '\x03') process.exit();
|
||
let name = key.name.toUpperCase();
|
||
if (name in KEY_MAP) {
|
||
CPU.state.memory[KEYPAD_ADDR] = KEY_MAP[name];
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Execute just the next instruction in memory
|
||
* @param {Object} debugInfo
|
||
* @param {Boolean} [debug] - Print machine status and the line of code being executed
|
||
**/
|
||
async function stepCPU(debugInfo, debug = false, prettyPrintDisplay = false) {
|
||
if (CPU.state.IP >= CPU.state.memory.length) {
|
||
console.error('HALTING - IP greater than memory size');
|
||
CPU.state.running = false;
|
||
process.exit();
|
||
} else {
|
||
CPU.debug.currentInstruction.opcode = CPU.state.memory[CPU.state.IP];
|
||
CPU.debug.currentInstruction.operand = CPU.state.memory[CPU.state.IP+1];
|
||
let executeInstruction = opcodes2mnemonics[CPU.debug.currentInstruction.opcode];
|
||
if (typeof executeInstruction === 'undefined') {
|
||
let info = debugInfo[CPU.previousIP];
|
||
console.error();
|
||
console.error(`Error: Invalid opcode`);
|
||
console.error(` Executing $${num2hex(info.machine[0])} $${num2hex(info.machine[1])}`);
|
||
console.error(` from line ${info.lineNumber}: ${info.source}`);
|
||
process.exit();
|
||
}
|
||
executeInstruction(CPU.debug.currentInstruction.operand);
|
||
CPU.debug.cycleCounter += 1;
|
||
}
|
||
logCPUState(debugInfo, debug, prettyPrintDisplay);
|
||
if (DEFAULT_CYCLE_LIMIT) { // Temporary limit as a lazy way to halt infinite loops
|
||
if (CPU.debug.cycleCounter >= DEFAULT_CYCLE_LIMIT) {
|
||
console.warn(' HALTING - reached cycle limit');
|
||
CPU.state.running = false;
|
||
}
|
||
}
|
||
if (!CPU.state.running) process.exit();
|
||
}
|
||
|
||
/**
|
||
* @param {Uint8Array} code - Machine code to run
|
||
* @param {Object} debugInfo TODO type
|
||
* @param {Boolean} [debug] - Enable/disable debugging printouts
|
||
* @param {Boolean} [singleStep]
|
||
* @param {Boolean} [prettyPrint] - Print display with black and white emoji, instead of in hex
|
||
* @param {Number} [clockSpeed] - CPU clock speed in milliseconds
|
||
**/
|
||
exports.runProgram =
|
||
(code, debugInfo, debug=false, singleStep=false, prettyPrint=false, clockSpeed=100) => {
|
||
if (singleStep) {
|
||
this.singleStepProgram(code, debugInfo, debug, prettyPrint);
|
||
} else {
|
||
startCPU(code);
|
||
// Animate the output by pausing between steps
|
||
const loop = setInterval(async () => {
|
||
stepCPU(debugInfo, debug, prettyPrint);
|
||
if (!CPU.state.running) {
|
||
logCPUState(debugInfo, debug, prettyPrint);
|
||
console.log('Halted');
|
||
process.exit();
|
||
}
|
||
}, clockSpeed);
|
||
}
|
||
};
|
||
|
||
/**
|
||
* @param {Uint8Array} code - Machine code to run
|
||
* @param {any} debugInfo - TODO
|
||
* @param {Boolean} [debug] - Enable/disable debugging printouts
|
||
* @param {Boolean} [prettyPrintDisplay] - Print display using black and white emoji
|
||
**/
|
||
exports.singleStepProgram = (code, debugInfo, debug = false, prettyPrintDisplay = false) => {
|
||
startCPU(code);
|
||
while (CPU.state.running) {
|
||
stepCPU(debugInfo, debug, prettyPrintDisplay);
|
||
// FIXME: this prevents exiting with Ctrl+C:
|
||
let key = readlineSync.keyIn('S to step, Q to quit > ', {
|
||
limit: ['s', 'S', 'q', 'Q'],
|
||
});
|
||
if (key.toLowerCase() === 'q') { process.exit(); }
|
||
console.log();
|
||
}
|
||
}
|
||
|
||
|
||
// FUNCTIONS THAT PULL INFO FROM STATE TO DISPLAY
|
||
|
||
/**
|
||
* @param {Boolean} [debug] - Enable/disable debugging printouts
|
||
**/
|
||
function logCPUState(debugInfo, debug = false, prettyPrintDisplay = false) {
|
||
debugInfo = debugInfo[CPU.previousIP] !== 'undefined' ? debugInfo[CPU.previousIP] : false;
|
||
console.group(`Step ${CPU.debug.cycleCounter}`);
|
||
console.log();
|
||
if (!debug) console.clear();
|
||
display.show(CPU.state.memory, prettyPrintDisplay);
|
||
console.log();
|
||
if (debugInfo) {
|
||
console.log(`Line ${debugInfo.lineNumber}: ${debugInfo.source}`);
|
||
console.log();
|
||
}
|
||
console.log('Mnemonic:', CPU.debug.currentInstruction.mnemonic);
|
||
console.log(`Machine: $${num2hex(CPU.debug.currentInstruction.opcode)} $${num2hex(CPU.debug.currentInstruction.operand)}`);
|
||
console.log();
|
||
console.log(`IP: $${num2hex(CPU.state.IP)} Acc: $${num2hex(CPU.state.acc)} ONZC ${bool2bit(CPU.state.flags.O)}${bool2bit(CPU.state.flags.N)}${bool2bit(CPU.state.flags.Z)}${bool2bit(CPU.state.flags.C)}`);
|
||
console.log(`KEY: $${num2hex(CPU.state.memory[KEYPAD_ADDR])} ${CPU.state.running ? "state.running" : "halted" }`);
|
||
console.log();
|
||
console.log();
|
||
console.groupEnd();
|
||
}; |