From 5742e1f66c4e70151865de7092547223898bbf6b Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Sun, 18 Apr 2021 16:00:54 -0700 Subject: Support a proper PDP-8 assembler. --- tool/p8bin2hex.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tool/pll.rb | 23 ++++++++ 2 files changed, 193 insertions(+) create mode 100644 tool/p8bin2hex.c create mode 100755 tool/pll.rb (limited to 'tool') diff --git a/tool/p8bin2hex.c b/tool/p8bin2hex.c new file mode 100644 index 0000000..8d28d19 --- /dev/null +++ b/tool/p8bin2hex.c @@ -0,0 +1,170 @@ +/* + * PDP-8 format converter. + * + * Usage: p8bin2hex tape.bin > tape.hex + * + * Assumes tape.bin is a SIMH-compatible paper tape image in "BIN" or "RIM" + * formats. + * + * RIM format: + * 10.000.000 leader/trailer (at least an inch of these) + * 01.aaa.aaa address hi (bits 5:0 contain address 11:6) + * 00.aaa.aaa address lo (bits 5:0 contain address 5:0) + * 00.xxx.xxx data hi (bits 5:0 contain data 11:6) + * 00.xxx.xxx data lo (bits 5:0 contain data 5:0) + * + * + * BIN format: + * 10.000.000 leader/trailer (at least an inch of these) + * 11.011.000 "field" (bits 6:4 specify address 14:12) + * 01.000.010 origin hi bits: bits 5:0 contain origin 11:6 + * 00.011.100 origin lo bits: bits 5:0 contain origin 5:0 + * 00.xxx.xxx data hi bits: bits 5:0 contain data word 11:6 + * 00.xxx.xxx data lo bits: bits 5:0 contain data word 5:0 + * + * 00.xxx.xxx chucksum hi - sum of all data and origin frames, but + * 00.xxx.xxx chucksum lo - excluding field and leader/trailer + * + * Some tapes may contain assembler error messages. These are are text + * delimited by 0377 bytes. The BIN loader ignores bytes from starting + * at any 0377 byte, until another 0377 byte is seen. + */ + +#include +#include +#include + +uint16_t mem[32768]; + +int +load(FILE *fp) +{ + enum { Addr, Data, Leader, Fetch, Skip , Start } s = Start; + int c = 0, n, v = 0; + uint16_t a = 0, d = 0, t = 0, u = 0; + for (n = 0; (c = getc(fp)) != EOF; ++n) { + switch (s) { + case Addr: + u += c; + a |= c; + fprintf(stderr, " %04o: address %06o\n", c, a & 077777); + s = Fetch; + break; + case Data: + u += c; + d |= 0x8000 | c; + /* fprintf(stderr, " %04o: data %05o\n", c, d & 07777); */ + fprintf(stderr, " %04o", c); + s = Fetch; + break; + case Fetch: + fetch: + if (c == 0200) { + if ((d & 0x8000) != 0) { + if ((d & 07777) != (t & 07777)) + fprintf(stderr, "\nbad checksum %05o\n", t & 07777); + else + v = 1, fprintf(stderr, "\nvalid checksum %05o\n", t & 07777); + } + fprintf(stderr, " %04o", c); + goto leadout; + } + t += u; + u = 0; + if (d & 0x8000) { + fprintf(stderr, ": mem[%06o]=%05o\n", a & 077777, d & 07777); + mem[a] = d & 07777; + a = (a & ~07777) | ((a + 1) & 07777); + d = 0; + } + fprintf(stderr, " %04o", c); + if ((c & 0300) == 0000) { + u = c; + d = (c & 077) << 6; + s = Data; + break; + } + if ((c & 0300) == 0100) { + u = c; + a = (a & ~07777) | (c & 077) << 6; + s = Addr; + break; + } + if ((c & 0307) == 0300) { + a = ((c & 0070) << 9) | (a & 07777); + fprintf(stderr, ": field %o address %06o\n", (c & 070) >> 3, a & 077777); + break; + } + if (c == 0377) { + fprintf(stderr, ": skipping: "); + s = Skip; + break; + } + fprintf(stderr, ": unknown byte at %d\n", n); + break; + case Leader: + leader: + if (c != 0200) { + fprintf(stderr, ": end leader\n"); + s= Fetch; + goto fetch; + } + fprintf(stderr, " %04o", c); + break; + case Skip: + if (c == 0377) { + s = Fetch; + putchar('\n'); + } else { + putchar(c); + } + break; + case Start: + if (c == 0200) { + s = Leader; + goto leader; + } + fprintf(stderr, " %04o", c); + break; + } + } + leadout: + while ((c = getc(fp)) != EOF) + fprintf(stderr, " %04o", c); + putchar('\n'); + return v; +} + +void +dump() +{ + printf("// Generated by p8bin2hex"); + unsigned int nextaddr = -1; + for (uint_fast32_t a = 0; a < 32768; ++a) { + if (mem[a] == 0) continue; + if (nextaddr != a) + printf("\n@%x", (unsigned int)a); + nextaddr = a + 1; + printf(" %03x", mem[a]); + } + printf("\n"); +} + +int +main(int argc, char *argv[]) +{ + if (argc != 2) { + fprintf(stderr, "usage: p8 filename.bin\n"); + exit(EXIT_FAILURE); + } + FILE *fp = fopen(argv[1], "rb"); + if (!fp) { + perror(argv[1]); + exit(EXIT_FAILURE); + } + if (!load(fp)) + exit(EXIT_FAILURE); + fclose(fp); + dump(); + exit(EXIT_SUCCESS); +} diff --git a/tool/pll.rb b/tool/pll.rb new file mode 100755 index 0000000..49464d4 --- /dev/null +++ b/tool/pll.rb @@ -0,0 +1,23 @@ +#!/usr/bin/ruby -w + +TARGET_FREQ = ARGV.shift.to_f +NATIVE_FREQ = 50.0 + +CLOCK_WIDTH = 5 + +best = nil +best_mult = nil +best_div = nil +1.upto(2**CLOCK_WIDTH) do | mult | + 1.upto(2**CLOCK_WIDTH) do | div | + new = NATIVE_FREQ * mult / div + if not best or (new - TARGET_FREQ).abs < (best - TARGET_FREQ).abs or ((new - TARGET_FREQ).abs == (best - TARGET_FREQ).abs and (mult + div) < (best_mult + best_div)) + best = new + best_mult = mult + best_div = div + end + end +end + +error = (best - TARGET_FREQ).abs / TARGET_FREQ +$stdout.write("Closest I can get is #{best}: *#{best_mult} /#{best_div} (#{(error * 100).round}% error)\n") -- cgit v1.2.3