summaryrefslogtreecommitdiff
path: root/tool/p8bin2hex.c
diff options
context:
space:
mode:
authorJulian Blake Kongslie2022-03-13 16:52:16 -0700
committerJulian Blake Kongslie2022-03-13 16:52:16 -0700
commit1b3915e330384ea0aacd59e8f9152288dcf70c50 (patch)
tree34f043eacc84eb530d4c1b2071b1018b51322e73 /tool/p8bin2hex.c
parentChange command parser to support bulk download script. (diff)
downloadmultipdp8-1b3915e330384ea0aacd59e8f9152288dcf70c50.tar.xz
Import p8bin2* tools into repo.
Diffstat (limited to '')
-rw-r--r--tool/p8bin2hex.c160
1 files changed, 160 insertions, 0 deletions
diff --git a/tool/p8bin2hex.c b/tool/p8bin2hex.c
new file mode 100644
index 0000000..4ef10fc
--- /dev/null
+++ b/tool/p8bin2hex.c
@@ -0,0 +1,160 @@
1/*
2 * PDP-8 format converter.
3 *
4 * Usage: p8bin2hex tape.bin > tape.hex
5 *
6 * Assumes tape.bin is a SIMH-compatible paper tape image in "BIN" or "RIM"
7 * formats.
8 *
9 * RIM format:
10 * 10.000.000 leader/trailer (at least an inch of these)
11 * 01.aaa.aaa address hi (bits 5:0 contain address 11:6)
12 * 00.aaa.aaa address lo (bits 5:0 contain address 5:0)
13 * 00.xxx.xxx data hi (bits 5:0 contain data 11:6)
14 * 00.xxx.xxx data lo (bits 5:0 contain data 5:0)
15 * <repeat any number of [address, data] pairs>
16 *
17 * FIXME: RIM format currently almost always fails with bad checksum
18 *
19 * BIN format:
20 * 10.000.000 leader/trailer (at least an inch of these)
21 * 11.011.000 "field" (bits 6:4 specify address 14:12)
22 * 01.000.010 origin hi bits: bits 5:0 contain origin 11:6
23 * 00.011.100 origin lo bits: bits 5:0 contain origin 5:0
24 * 00.xxx.xxx data hi bits: bits 5:0 contain data word 11:6
25 * 00.xxx.xxx data lo bits: bits 5:0 contain data word 5:0
26 * <repeat any number of [origin, data+] sequences>
27 * 00.xxx.xxx chucksum hi - sum of all data and origin frames, but
28 * 00.xxx.xxx chucksum lo - excluding field and leader/trailer
29 *
30 * Some tapes may contain assembler error messages. These are are text
31 * delimited by 0377 bytes. The BIN loader ignores bytes from starting
32 * at any 0377 byte, until another 0377 byte is seen.
33 */
34
35#include <inttypes.h>
36#include <stdio.h>
37#include <stdlib.h>
38
39uint16_t mem[32768];
40
41int
42load(FILE *fp)
43{
44 enum { Addr, Data, Leader, Fetch, Skip , Start } s = Start;
45 int c = 0, n, v = 0;
46 uint16_t a = 0, d = 0, t = 0, u = 0;
47 for (n = 0; (c = getc(fp)) != EOF; ++n) {
48 switch (s) {
49 case Addr:
50 u += c;
51 a |= c;
52 s = Fetch;
53 break;
54 case Data:
55 u += c;
56 d |= 0x8000 | c;
57 s = Fetch;
58 break;
59 case Fetch:
60 fetch:
61 if (c == 0200) {
62 if ((d & 0x8000) != 0) {
63 if ((d & 07777) != (t & 07777))
64 fprintf(stderr, "\nbad checksum %05o\n", t & 07777);
65 else
66 v = 1;
67 }
68 goto leadout;
69 }
70 t += u;
71 u = 0;
72 if (d & 0x8000) {
73 mem[a] = d & 07777;
74 a = (a & ~07777) | ((a + 1) & 07777);
75 d = 0;
76 }
77 if ((c & 0300) == 0000) {
78 u = c;
79 d = (c & 077) << 6;
80 s = Data;
81 break;
82 }
83 if ((c & 0300) == 0100) {
84 u = c;
85 a = (a & ~07777) | (c & 077) << 6;
86 s = Addr;
87 break;
88 }
89 if ((c & 0307) == 0300) {
90 a = ((c & 0070) << 9) | (a & 07777);
91 break;
92 }
93 if (c == 0377) {
94 s = Skip;
95 break;
96 }
97 break;
98 case Leader:
99 leader:
100 if (c != 0200) {
101 s= Fetch;
102 goto fetch;
103 }
104 break;
105 case Skip:
106 if (c == 0377) {
107 s = Fetch;
108 putchar('\n');
109 } else {
110 putchar(c);
111 }
112 break;
113 case Start:
114 if (c == 0200) {
115 s = Leader;
116 goto leader;
117 }
118 break;
119 }
120 }
121 leadout:
122 while ((c = getc(fp)) != EOF)
123 ;
124 putchar('\n');
125 return v;
126}
127
128void
129dump()
130{
131 printf("// Generated by p8bin2hex");
132 unsigned int nextaddr = -1;
133 for (uint_fast32_t a = 0; a < 32768; ++a) {
134 if (mem[a] == 0) continue;
135 if (nextaddr != a)
136 printf("\n@%x", (unsigned int)a);
137 nextaddr = a + 1;
138 printf(" %03x", mem[a]);
139 }
140 printf("\n");
141}
142
143int
144main(int argc, char *argv[])
145{
146 if (argc != 2) {
147 fprintf(stderr, "usage: p8 filename.bin\n");
148 exit(EXIT_FAILURE);
149 }
150 FILE *fp = fopen(argv[1], "rb");
151 if (!fp) {
152 perror(argv[1]);
153 exit(EXIT_FAILURE);
154 }
155 if (!load(fp))
156 exit(EXIT_FAILURE);
157 fclose(fp);
158 dump();
159 exit(EXIT_SUCCESS);
160}