-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathiml-file.c
190 lines (160 loc) · 4.09 KB
/
iml-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
/* Copyright (C) 2021 Lars Brinkhoff <[email protected]>
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/>. */
#include <stdio.h>
#include "dis.h"
#include "memory.h"
static int checksum = 0;
/* "Special TTY" boostrap loader. */
static char *loader =
"@BAJH@@IBGOLBGOMCONGCONGH@@FBGONCOMOGOOHAGLL@@@@BGOOCOMOJGOOCOMHCGOO\r\n"
"CGONAGLMCOMOGGOLHD@AAGL@@@@@@@@@H@@HFOOL@D@DH@@DBGOLIGMH@@@@H@@IBGOM\r\n"
"CONGCONGCONGCONGIGMO@@@@@DB@AGNHH@@I@BAKBGKODOOIGOOJAGNHFGKODOOKBOOM\r\n"
"@F@C@F@AEGOMBGOMIGNGOOOO@@G@@@D@@@@O@@@@@@@@@@@@@@@@OOL@\r\n\r\n";
static void
update_sum (int word)
{
checksum += word;
if (checksum & 0200000)
{
checksum++;
checksum &= 0177777;
}
}
static int
get_4 (FILE *f)
{
for (;;)
{
int c = fgetc (f);
if (c == EOF)
{
fprintf (stderr, "Unexpected end of input file.\n");
exit (1);
}
if ((c & 0160) == 0100)
return c & 017;
}
}
static int
get_8 (FILE *f)
{
return (get_4 (f) << 4) | get_4 (f);
}
static int
get_16 (FILE *f)
{
int data = (get_8 (f) << 8) | get_8 (f);
update_sum (data);
return data;
}
static void
read_iml (FILE *f, struct pdp10_memory *memory, int cpu_model)
{
int length, address, data;
word_t *core;
int i;
(void)cpu_model;
start_instruction = 0;
/* Discard block loader. */
for (i = 0; i < 65; i++)
get_16 (f);
for (;;)
{
length = get_8 (f);
address = get_16 (f);
/* All ones signals end of data. */
if (address == 0177777)
{
/* Search for a start address at 037714 modulo 4K. */
address = 037714;
while (address >= 07714)
{
start_instruction = get_word_at (memory, address);
if (start_instruction == -1)
start_instruction = 0;
else
break;
address -= 4096;
}
return;
}
core = malloc (length * sizeof (word_t));
if (core == NULL)
{
fprintf (stderr, "Out of memory.\n");
exit (1);
}
checksum = 0;
for (i = address; i < address + length; i++)
core[i] = get_16 (f);
add_memory (memory, address, length, core);
data = checksum;
if (data != get_16 (f))
fprintf (stderr, "Bad checksum: %04X.\n", data);
}
}
static void
out_4 (FILE *f, int word)
{
fputc ((word & 017) + 0100, f);
}
static void
out_8 (FILE *f, int word)
{
out_4 (f, word >> 4);
out_4 (f, word);
}
static void
out_16 (FILE *f, int word)
{
update_sum (word);
out_8 (f, word >> 8);
out_8 (f, word);
}
static void
write_iml (FILE *f, struct pdp10_memory *memory)
{
int start, length;
int i, j;
for (i = 0; i < (int)sizeof loader; i++)
fputc (loader[i], f);
for (i = 0; i < memory->areas; i++)
{
start = memory->area[i].start;
length = memory->area[i].end - start;
out_8 (f, length);
out_16 (f, start);
checksum = 0;
for (j = 0; j < length; j++)
out_16 (f, get_word_at (memory, start + j));
out_16 (f, checksum);
}
if (start_instruction != 0)
{
/* The special TTY loader will end up executing a jump indrected
through 37714. */
out_8 (f, 2);
out_16 (f, 037713);
checksum = 0;
out_16 (f, 0113714);
out_16 (f, start_instruction & 037777);
out_16 (f, checksum);
}
out_8 (f, 0377);
out_16 (f, 0177777);
fprintf (f, "\r\n");
}
struct file_format iml_file_format = {
"iml",
read_iml,
write_iml
};