tests/sims/hack.test.ts
The automated tests for the /simulators/nand-cpu simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- The gate-level ALU computes all 18 Hack comp functions, and agrees with the arithmetic ALU.
- Basic gates use the textbook NAND counts and truth tables.
- The assembler produces the published Add.hack for Add.asm.
- Every example program assembles and halts, and the multiply program is right for all inputs up to 29 × 29.
// /simulators/nand-cpu: the Hack ALU built from NAND gates, the assembler and the CPU (Nisan and Schocken, The Elements of Computing Systems).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { assemble, bin16, newCpu, run, aluGates, alu, COMP, Nand } from '../../src/lib/sims/hack.ts';
import { HACK_PRESETS } from '../../src/lib/sims/hackPresets.ts';
import { lcgInt } from './check.ts';
const s16 = (v: number) => (v << 16) >> 16;
test('the gate-level ALU computes all 18 Hack comp functions, and agrees with the arithmetic ALU', () => {
const r = lcgInt(1);
const val = () => s16(r() >>> 8);
let n = 0;
for (const [k, bits] of Object.entries(COMP)) {
if (bits[0] === '1') continue; // the a=1 forms are the same functions with M in place of A
const c = { zx: +bits[1], nx: +bits[2], zy: +bits[3], ny: +bits[4], f: +bits[5], no: +bits[6] };
for (let i = 0; i < 300; i++) {
const x = val(), y = val();
const want: Record<string, number> = { '0': 0, '1': 1, '-1': -1, D: x, A: y, '!D': ~x, '!A': ~y, '-D': -x, '-A': -y, 'D+1': x + 1, 'A+1': y + 1, 'D-1': x - 1, 'A-1': y - 1, 'D+A': x + y, 'D-A': x - y, 'A-D': y - x, 'D&A': x & y, 'D|A': x | y };
const g = aluGates(x, y, c), a = alu(x, y, c);
assert.equal(a.out, s16(want[k]), `${k} with D=${x} A=${y}`);
assert.deepEqual([g.out, g.zr, g.ng], [a.out, a.zr, a.ng], `${k} gates vs arithmetic`);
assert.equal(a.zr, a.out === 0 ? 1 : 0);
assert.equal(a.ng, a.out < 0 ? 1 : 0);
n++;
}
}
assert.equal(n, 18 * 300);
});
test('basic gates use the textbook NAND counts and truth tables', () => {
const g = new Nand();
const count = (f: () => unknown) => { g.count = 0; f(); return g.count; };
assert.equal(count(() => g.not(1)), 1);
assert.equal(count(() => g.and(1, 1)), 2);
assert.equal(count(() => g.or(0, 1)), 3);
assert.equal(count(() => g.xor(1, 0)), 4);
for (let a = 0; a < 2; a++) for (let b = 0; b < 2; b++) {
assert.equal(g.and(a, b), a & b);
assert.equal(g.or(a, b), a | b);
assert.equal(g.xor(a, b), a ^ b);
for (let cin = 0; cin < 2; cin++) assert.deepEqual(g.full(a, b, cin), [(a + b + cin) & 1, (a + b + cin) >> 1]);
}
});
test('the assembler produces the published Add.hack for Add.asm', () => {
// nand2tetris project 6, add/Add.asm and its comparison file
const a = assemble('@2\nD=A\n@3\nD=D+A\n@0\nM=D');
assert.deepEqual(a.errors, []);
assert.deepEqual(a.code.map(bin16), ['0000000000000010', '1110110000010000', '0000000000000011', '1110000010010000', '0000000000000000', '1110001100001000']);
// symbols: variables from 16 up, labels to instruction addresses, predefined registers
const b = assemble('@i\n@j\n@i\n(LOOP)\n@LOOP\n0;JMP\n@R15\n@SCREEN');
assert.deepEqual(b.code.slice(0, 3), [16, 17, 16]);
assert.equal(b.code[3], 3);
assert.equal(b.code[5], 15);
assert.equal(b.code[6], 16384);
});
test('every example program assembles and halts, and the multiply program is right for all inputs up to 29 × 29', () => {
for (const p of HACK_PRESETS) {
const as = assemble(p.src);
assert.deepEqual(as.errors, [], p.id);
const c = newCpu();
for (const [k, v] of Object.entries(p.ram ?? {})) c.ram[+k] = v;
run(c, as.code, 1e6);
assert.ok(c.halted, `${p.id} did not halt`);
}
const mult = HACK_PRESETS.find((p) => /mult/i.test(p.id));
assert.ok(mult, 'no multiply preset');
const m = assemble(mult!.src);
for (let x = 0; x < 30; x++) for (let y = 0; y < 30; y++) {
const c = newCpu();
c.ram[0] = x; c.ram[1] = y;
run(c, m.code, 1e6);
assert.equal(c.ram[2], x * y, `${x} × ${y}`);
}
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.