diff --git a/mamelink/Makefile b/mamelink/Makefile index 830b941..6df41cd 100644 --- a/mamelink/Makefile +++ b/mamelink/Makefile @@ -1,3 +1,6 @@ all: mamelink.o down down: mamelink.o down.o + +clean: + rm -f *.o down \ No newline at end of file diff --git a/mamelink/mamelink.c b/mamelink/mamelink.c index 609ab2b..b1d3b78 100644 --- a/mamelink/mamelink.c +++ b/mamelink/mamelink.c @@ -77,15 +77,18 @@ static void simple_cmd(char command) { send_cmd_and_close(prepare_cmd(command)); } -void Init(char *initial_link_dir) { +int Init(char *initial_link_dir) { if (initial_link_dir == NULL) { initial_link_dir = getenv("MAMELINK"); } - assert(initial_link_dir != NULL); + if (initial_link_dir == NULL) { + return 0; + } if (linkdir != NULL) { free(linkdir); } linkdir = strdup(initial_link_dir); + return 1; } void Finish() { diff --git a/mamelink/mamelink.h b/mamelink/mamelink.h index 7efe87a..7d66e9e 100644 --- a/mamelink/mamelink.h +++ b/mamelink/mamelink.h @@ -1,4 +1,4 @@ -void Init(char *initial_link_dir); +int Init(char *initial_link_dir); void Finish(); void down(char *buf, unsigned short bytes, unsigned short c64Addr); void up(char *buf, unsigned short bytes, unsigned short c64Addr); diff --git a/mamelink/out2prg.js b/mamelink/out2prg.js new file mode 100644 index 0000000..527abf1 --- /dev/null +++ b/mamelink/out2prg.js @@ -0,0 +1,55 @@ +const fs = require('node:fs/promises'); +const assert = require('node:assert/strict'); + +const die = (...message) => { + console.error(...message) + process.exit(1) +} + +if (process.argv.length != 3) { + die("Usage: node out2prg.js FILE.out") +} + +const replaceExtension = (filename, ext) => { + const idot = filename.lastIndexOf(".") + return `${idot < 0 ? filename : filename.substr(0, idot)}.${ext}` +} +(async () => { + const outfilename = process.argv[2] + const data = await fs.readFile(outfilename) + assert.equal(data.readUint16LE(0), 0xffff, `${outfilename} does not start with magic FFFF bytes`) + const prgfilename = replaceExtension(outfilename, "prg") + const prgfile = await fs.open(prgfilename, "w") + try { + let off = 2 + let nextAddress = null + let bootAddr = null + while (off < data.length) { + const startAddr = data.readUint16LE(off) + const endAddr = data.readUint16LE(off + 2) + if (startAddr == endAddr && bootAddr == null) { + bootAddr = startAddr + console.log(`To start, type SYS${bootAddr}`) + console.log(`Skipping byte: ${data.readUint8(off + 4)}`) + off += 5 + continue + } + assert.ok(endAddr >= startAddr, `Invalid address range ${startAddr}:${endAddr}`) + assert.ok(nextAddress == null || startAddr >= nextAddress, `Expected ${startAddr} to come after previous segment ending at ${nextAddress}`) + const paddingLength = nextAddress == null ? 0 : startAddr - nextAddress + const blobLength = endAddr - startAddr + 1 + if (nextAddress == null) { + await fs.writeFile(prgfile, data.subarray(off, off + 2)) + } + if (paddingLength > 0) { + await fs.writeFile(prgfile, Buffer.alloc(paddingLength)) + } + off += 4 + await fs.writeFile(prgfile, data.subarray(off, off + blobLength)) + nextAddress = endAddr + 1 + off += blobLength + } + } finally { + await prgfile.close() + } +})() \ No newline at end of file diff --git a/mamelink/reno.d64 b/mamelink/reno.d64 new file mode 100644 index 0000000..886649b Binary files /dev/null and b/mamelink/reno.d64 differ diff --git a/mamelink/reno.prg b/mamelink/reno.prg new file mode 100644 index 0000000..918825e Binary files /dev/null and b/mamelink/reno.prg differ