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