// Assembles the CoCo boot ROM (src/machine/cocoboot/boot.lwasm) and writes it // as src/machine/cocorom.ts, which the CoCo machine embeds. // // npm run tsbuild && node scripts/build-coco-rom.mjs // // The assembly goes through the same lwasm/lwlink the IDE uses (the worker's // build), with src/machine/cocoboot/coco.scr placing each section at its ROM address. // lwlink writes a DECB .bin; we lay its blocks out into the 8K image that // appears at $A000-$BFFF. import { readFileSync, writeFileSync } from 'fs'; import { createRequire } from 'module'; const require = createRequire(import.meta.url); const { compile, initialize, preload } = require('../gen/tools/testlib.js'); const ROM_BASE = 0xa000; const ROM_SIZE = 0x2000; const FILL = 0x39; // RTS, so a stray call into unused ROM just returns // The project's own coco.scr takes the place of the library's (which loads // programs at $2800), so it has to be handed to the build explicitly. const src = (name) => readFileSync(new URL('../src/machine/cocoboot/' + name, import.meta.url), 'utf8'); await initialize(); await preload('lwasm', 'coco'); const log = console.log; console.log = () => {}; // the tools are chatty const result = await compile({ tool: 'lwasm', platform: 'coco', files: [{ path: 'boot.lwasm', data: src('boot.lwasm') }, { path: 'coco.scr', data: src('coco.scr') }], buildsteps: [{ path: 'boot.lwasm', platform: 'coco', tool: 'lwasm' }], }); console.log = log; if (!result.output) { console.error(JSON.stringify(result.errors, null, 2)); process.exit(1); } const decb = result.output; const rom = new Uint8Array(ROM_SIZE).fill(FILL); let p = 0; while (p < decb.length) { const type = decb[p]; const len = (decb[p + 1] << 8) | decb[p + 2]; const addr = (decb[p + 3] << 8) | decb[p + 4]; p += 5; if (type == 0xff) break; if (type != 0) throw new Error('bad DECB block type ' + type); if (addr < ROM_BASE || addr + len > ROM_BASE + ROM_SIZE) throw new Error(`block $${addr.toString(16)}+${len} is outside the ROM`); rom.set(decb.subarray(p, p + len), addr - ROM_BASE); p += len; } let out = `// GENERATED by scripts/build-coco-rom.mjs from src/machine/cocoboot/boot.lwasm // -- do not edit. The 8K boot ROM that appears at $A000-$BFFF. export const COCO_BOOT_ROM = new Uint8Array([ `; for (let i = 0; i < ROM_SIZE; i += 16) { out += ' ' + Array.from(rom.subarray(i, i + 16)).map(b => '0x' + b.toString(16).padStart(2, '0')).join(',') + ',\n'; } out += ']);\n'; writeFileSync(new URL('../src/machine/cocorom.ts', import.meta.url), out); console.log(`wrote src/machine/cocorom.ts (${decb.length} bytes of code)`);