-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
loaders.cpp
295 lines (243 loc) · 5.82 KB
/
loaders.cpp
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
// (C) 2018-2024 by Folkert van Heusden
// Released under MIT license
#if defined(ESP32)
#include <Arduino.h>
#include "esp32.h"
#endif
#include <stdint.h>
#include <stdio.h>
#include <string>
#include "bus.h"
#include "cpu.h"
#include "error.h"
#include "gen.h"
#include "loaders.h"
#include "log.h"
#include "utils.h"
void loadbin(bus *const b, uint16_t base, const char *const file)
{
FILE *fh = fopen(file, "rb");
while(!feof(fh))
b->write_byte(base++, fgetc(fh));
fclose(fh);
}
void set_boot_loader(bus *const b, const bootloader_t which)
{
cpu *const c = b->getCpu();
uint16_t offset = 0;
uint16_t start = 0;
const uint16_t *bl = nullptr;
int size = 0;
if (which == BL_RK05) {
DOLOG(debug, false, "Enabling RK05 bootloader");
start = offset = 01000;
static const uint16_t rk05_code[] = {
0012700,
0177406,
0012710,
0177400,
0012740,
0000005,
0105710,
0100376,
0005007
};
#if 0
// from https://github.com/amakukha/PyPDP11.git
offset = 02000;
start = 02002;
static uint16_t rk05_code[] = {
0042113, // "KD"
0012706, 02000, // MOV #boot_start, SP
0012700, 0000000, // MOV #unit, R0 ; unit number
0010003, // MOV R0, R3
0000303, // SWAB R3
0006303, // ASL R3
0006303, // ASL R3
0006303, // ASL R3
0006303, // ASL R3
0006303, // ASL R3
0012701, 0177412, // MOV #RKDA, R1 ; csr
0010311, // MOV R3, (R1) ; load da
0005041, // CLR -(R1) ; clear ba
0012741, 0177000, // MOV #-256.*2, -(R1) ; load wc
0012741, 0000005, // MOV #READ+GO, -(R1) ; read & go
0005002, // CLR R2
0005003, // CLR R3
0012704, 02020, // MOV #START+20, R4
0005005, // CLR R5
0105711, // TSTB (R1)
0100376, // BPL .-2
0105011, // CLRB (R1)
0005007 // CLR PC
};
#endif
bl = rk05_code;
size = sizeof(rk05_code)/sizeof(rk05_code[0]);
}
else if (which == BL_RL02) {
DOLOG(debug, false, "Enabling RL02 bootloader");
start = offset = 01000;
/* from https://www.pdp-11.nl/peripherals/disk/rl-info.html
static const uint16_t rl02_code[] = {
0012701,
0174400,
0012761,
0000013,
0000004,
0012711,
0000004,
0105711,
0100376,
0005061,
0000002,
0005061,
0000004,
0012761,
0177400,
0000006,
0012711,
0000014,
0105711,
0100376,
0005007
};
size = 21;
*/
// from http://gunkies.org/wiki/RL11_disk_controller
static const uint16_t rl02_code[] = {
0012700,
0174400,
0012760,
0177400,
0000006,
0012710,
0000014,
0105710,
0100376,
0005007,
};
size = sizeof(rl02_code)/sizeof(rl02_code[0]);
bl = rl02_code;
}
else if (which == BL_RP06) {
DOLOG(debug, false, "Enabling RP06 bootloader");
start = offset = 02000;
static const uint16_t rp06_code[] = {
012700, 0176704, 012740, 0177000, 012740, 000071, 012700, 0, 000110, 000000
};
size = sizeof(rp06_code)/sizeof(rp06_code[0]);
bl = rp06_code;
}
for(uint16_t i=0; i<size; i++)
b->write_word(uint16_t(offset + i * 2), bl[i]);
c->set_register(7, start);
}
std::optional<uint16_t> load_tape(bus *const b, const std::string & file)
{
#if defined(ESP32)
File32 fh;
if (!fh.open(file.c_str(), O_RDONLY)) {
DOLOG(ll_error, true, "Cannot open %s", file.c_str());
return { };
}
#else
FILE *fh = fopen(file.c_str(), "rb");
if (!fh) {
DOLOG(ll_error, true, "Cannot open %s", file.c_str());
return { };
}
#endif
std::optional<uint16_t> start;
for(;;) {
uint8_t buffer[6];
#if defined(ESP32)
if (fh.read(buffer, 6) != 6)
break;
#else
if (fread(buffer, 1, 6, fh) != 6)
break;
#endif
int count = (buffer[3] << 8) | buffer[2];
int p = (buffer[5] << 8) | buffer[4];
uint8_t csum = 0;
for(int i=2; i<6; i++)
csum += buffer[i];
if (count == 0 || p == 1)
break;
if (count == 6) { // eg no data
if (p != 1) {
DOLOG(info, true, "Setting start address to %o", p);
start = p;
}
}
#if !defined(ESP32)
TRACE("%ld] reading %d (dec) bytes to %o (oct)", ftell(fh), count - 6, p);
#endif
for(int i=0; i<count - 6; i++) {
#if defined(ESP32)
uint8_t c = 0;
if (fh.read(&c, 1) != 1) {
DOLOG(warning, true, "short read");
break;
}
#else
if (feof(fh)) {
DOLOG(warning, true, "short read");
break;
}
int c = fgetc(fh);
if (c == -1) {
DOLOG(warning, true, "read failure");
break;
}
#endif
csum += c;
b->write_byte(p++, c);
}
#if defined(ESP32)
uint8_t fcs = 0;
fh.read(&fcs, 1);
csum += fcs;
#else
csum += fgetc(fh);
#endif
if (csum != 255)
DOLOG(warning, true, "checksum error %d", csum);
}
#if defined(ESP32)
fh.close();
#else
fclose(fh);
#endif
if (start.has_value() == false)
start = 0200; // assume BIC file
return start;
}
void load_p11_x11(bus *const b, const std::string & file)
{
FILE *fh = fopen(file.c_str(), "rb");
if (!fh)
error_exit(true, "Cannot open %s", file.c_str());
uint16_t addr = 0;
int n = 0;
while(!feof(fh)) {
char buffer[4096];
if (!fgets(buffer, sizeof buffer, fh))
break;
if (n) {
uint8_t byte = strtol(buffer, nullptr, 16);
b->write_byte(addr, byte);
n--;
addr++;
}
else {
std::vector<std::string> parts = split(buffer, " ");
addr = strtol(parts[0].c_str(), nullptr, 16);
n = strtol(parts[1].c_str(), nullptr, 16);
}
}
fclose(fh);
cpu *const c = b->getCpu();
c->set_register(7, 0);
}