-
Notifications
You must be signed in to change notification settings - Fork 13
/
rim10-file.c
307 lines (278 loc) · 8.64 KB
/
rim10-file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* Copyright (C) 2021 Lars Brinkhoff <lars@nocrew.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* This file format will read in a paper tape with a hardware read-in
loader. To do this, it will read in the loader and run it by
emulating a tiny subset of PDP-10 instructions. The subset is enough to
support the ITS RIM10 and DEC RIM10B loaders. */
#include <stdio.h>
#include <stdlib.h>
#include "dis.h"
#include "memory.h"
/* Instructions used by the ITS RIM10 and DEC RIM10B loaders. */
#define OP_ROT 0241
#define OP_JRST 0254
#define OP_XCT 0256
#define OP_AOBJN 0253
#define OP_ADD 0270
#define OP_CAME 0312
#define OP_SKIPL 0331
#define OP_SOJA 0364
#define OP_HRRI 0541
#define OP_DATAI 070004
#define OP_CONO 070020
#define OP_CONSO 070034
static word_t midas_rim10[] =
{
/*00*/ 0777761000000LL, /* -17,,0 */
/*01*/ 0710600000060LL, /* CONO PTR,60 */
/*02*/ 0541440000004LL, /* HRRI 11,4 */
/*03*/ 0710740000010LL, /* CONSO PTR,10 */
/*04*/ 0254000000003LL, /* JRST 3 */
/*05*/ 0241011777776LL, /* ROT 0,-2(11) */
/*06*/ 0710471000010LL, /* DATAI PTR,@10(11) */
/*07*/ 0256011000010LL, /* XCT 10(11) */
/*10*/ 0256011000013LL, /* XCT 13(11) */
/*11*/ 0364440000000LL, /* SOJA 11,0 */
/*12*/ 0312000000017LL, /* CAME 0,17 */
/*13*/ 0270017000000LL, /* ADD 0,(17) */
/*14*/ 0331740000000LL, /* SKIPL 17,0 */
/*15*/ 0254200000001LL, /* JRST 4,1 */
/*16*/ 0253740000003LL, /* AOBJN 17,3 */
/*17*/ 0254000000002LL /* JRST 2 */
};
static word_t macro_rim10b[] =
{
/*00*/ 0777762000000LL, /* -16,,0 */
/*01*/ 0710600000060LL, /* ST: CONO PTR,60 */
/*02*/ 0541400000004LL, /* ST1: HRRI A,RD+1 */
/*03*/ 0710740000010LL, /* RD: CONSO PTR,10 */
/*04*/ 0254000000003LL, /* JRST .-1 */
/*05*/ 0710470000007LL, /* DATAI PTR,@TBL1-RD+1(A) */
/*06*/ 0256010000007LL, /* XCT TBL1-RD+1(A) */
/*07*/ 0256010000012LL, /* XCT TBL2-RD+1(A) */
/*10*/ 0364400000000LL, /* A: SOJA A, */
/*11*/ 0312740000016LL, /* TBL1: CAME CKSM,ADR */
/*12*/ 0270756000001LL, /* ADD CKSM,1(ADR) */
/*13*/ 0331740000016LL, /* SKIPL CKSM,ADR */
/*14*/ 0254200000001LL, /* TBL2: JRST 4,ST */
/*15*/ 0253700000003LL, /* AOBJN ADR,RD */
/*16*/ 0254000000002LL /* ADR: JRST ST1 */
};
static int
effective_address (word_t insn, struct pdp10_memory *memory)
{
for (;;)
{
int x = (insn >> 18) & 017;
int y = insn;
if (x != 0)
y += get_word_at (memory, x);
y &= 0777777;
if ((insn & 020000000) == 0)
return y;
insn = get_word_at (memory, y);
}
}
static int
execute_iot (word_t insn, int ea, struct pdp10_memory *memory, FILE *f)
{
switch ((insn >> 21) & 070034)
{
case OP_DATAI:
insn = get_word (f);
//fprintf (stderr, "DATAI %012llo -> %06o\n", insn, ea);
set_word_at (memory, ea, insn);
break;
case OP_CONO:
//fprintf (stderr, "CONO\n");
break;
case OP_CONSO:
//fprintf (stderr, "CONSO\n");
return 1;
}
return 0;
}
static int
execute (word_t insn, int pc, struct pdp10_memory *memory, FILE *f)
{
int ea, ac;
word_t ma;
//fprintf (stderr, "%012llo\n", insn);
ea = effective_address (insn, memory);
ac = (insn >> 23) & 017;
switch (insn >> 27) {
case OP_ROT:
ma = get_word_at (memory, ac);
//fprintf (stderr, "ROT %o, %012llo -> ", ac, ma);
if (ea & 0400000)
{
ea = 01000000 - ea;
ma = (ma >> ea) | (ma << (36 - ea));
}
else
ma = (ma << ea) | (ma >> (36 - ea));
ma &= WORDMASK;
//fprintf (stderr, "%012llo\n", ma);
set_word_at (memory, ac, ma);
break;
case OP_AOBJN:
ma = get_word_at (memory, ac);
//fprintf (stderr, "AOBJN %o, %012llo -> ", ac, ma);
ma += 01000001LL;
ma &= WORDMASK;
//fprintf (stderr, "%012llo\n", ma);
set_word_at (memory, ac, ma);
if (ma & 0400000000000LL)
pc = ea;
break;
case OP_JRST:
//fprintf (stderr, "JRST\n");
switch ((insn >> 23) & 017)
{
case 0: pc = ea; break;
case 4: fprintf (stderr, "HALT\n");
default: exit (1);
}
break;
case OP_XCT:
insn = get_word_at (memory, ea);
//fprintf (stderr, "XCT ");
pc = execute (insn, pc, memory, f);
break;
case OP_ADD:
ma = get_word_at (memory, ea);
//fprintf (stderr, "ADD %o, %012llo + %012llo -> ", ac, get_word_at (memory, ac), ma);
ma += get_word_at (memory, ac);
ma &= WORDMASK;
//fprintf (stderr, "%012llo\n", ma);
set_word_at (memory, ac, ma);
break;
case OP_CAME:
ma = get_word_at (memory, ea);
//fprintf (stderr, "CAME %o, %012llo\n", ac, ma);
if (ma == get_word_at (memory, ac))
pc++;
break;
case OP_SKIPL:
ma = get_word_at (memory, ea);
//fprintf (stderr, "SKIPL %o,%012llo\n", ac, ma);
if (ac)
set_word_at (memory, ac, ma);
if (ma & 0400000000000LL)
pc++;
break;
case OP_SOJA:
ma = get_word_at (memory, ac);
//fprintf (stderr, "SOJA %o, %012llo -> ", ac, ma);
ma--;
ma &= WORDMASK;
//fprintf (stderr, "%012llo\n", ma);
set_word_at (memory, ac, ma);
pc = ea;
break;
case OP_HRRI:
ma = get_word_at (memory, ac);
//fprintf (stderr, "HRRI %o, %012llo ->", ac, ma);
ma &= 0777777000000LL;
ma |= ea;
//fprintf (stderr, "%012llo\n", ma);
set_word_at (memory, ac, ma);
break;
case 0700: case 0701: case 0702: case 0703: case 0704: case 0705: case 0706:
case 0707: case 0710: case 0711: case 0712: case 0713: case 0714: case 0715:
case 0716: case 0717: case 0720: case 0721: case 0722: case 0723: case 0724:
case 0725: case 0726: case 0727: case 0730: case 0731: case 0732: case 0733:
case 0734: case 0735: case 0736: case 0737: case 0740: case 0741: case 0742:
case 0743: case 0744: case 0745: case 0746: case 0747: case 0750: case 0751:
case 0752: case 0753: case 0754: case 0755: case 0756: case 0757: case 0760:
case 0761: case 0762: case 0763: case 0764: case 0765: case 0766: case 0767:
case 0770: case 0771: case 0772: case 0773: case 0774: case 0775: case 0776:
case 0777:
pc += execute_iot (insn, ea, memory, f);
break;
default:
fprintf (stderr, "Unsupported RIM10 loader instruction: %012llo\n", insn);
exit (1);
}
return pc;
}
static void
read_rim10 (FILE *f, struct pdp10_memory *memory, int cpu_model)
{
word_t insn;
word_t *loader;
int pc, address, length;
word_t word;
int i;
fprintf (output_file, "RIM10 format\n");
word = get_word (f);
address = (word & 0777777) + 1;
length = 01000000 - (word >> 18);
if (length > 16)
{
fprintf (stderr, "RIM10 loader longer than 16 words.\n");
exit (1);
}
if (address + length > 16)
{
fprintf (stderr, "RIM10 loader doesn't fit in accumulators.\n");
exit (1);
}
loader = malloc (16 * sizeof (word_t));
if (loader == NULL)
{
fprintf (stderr, "out of memory\n");
exit (1);
}
for (i = address; i < address + length; i++)
loader[i] = get_word (f);
add_memory (memory, 0, 16, loader);
pc = address + length - 1;
while (pc < 16)
{
//fprintf (stderr, "\n%06o: ", pc);
insn = get_word_at (memory, pc++);
pc = execute (insn, pc, memory, f);
}
/* Try to find start instruction even if was executed by XCT. */
if ((insn >> 27) == OP_XCT)
insn = get_word_at (memory, effective_address (insn, memory));
switch (insn & 0777777000000LL)
{
case JRST:
case JUMPA:
start_instruction = insn;
break;
default:
start_instruction = JRST + pc;
break;
}
fprintf (output_file, "Start instruction: %012llo\n", start_instruction);
/* Remove IOWD. */
remove_memory (memory, 0, 1);
/* Remove loader. */
remove_memory (memory, address, length);
}
static void
write_rim10 (FILE *f, struct pdp10_memory *memory)
{
int i;
for (i = 0; i < sizeof midas_rim10 / sizeof midas_rim10[0]; i++)
write_word (f, midas_rim10[i]);
write_sblk_core (f, memory, 020);
write_word (f, start_instruction);
}
struct file_format rim10_file_format = {
"rim10",
read_rim10,
write_rim10
};