forked from RespeQt/RespeQt
/
autoboot.cpp
367 lines (335 loc) · 12.3 KB
/
autoboot.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* autoboot.cpp
*
* Copyright 2015 Joseph Zatarski
* Copyright 2017 blind
*
* This file is copyrighted by either Fatih Aygun, Ray Ataergin, or both.
* However, the years for these copyrights are unfortunately unknown. If you
* know the specific year(s) please let the current maintainer know.
*/
#include "autoboot.h"
#include "respeqtsettings.h"
#include <QtDebug>
AutoBoot::~AutoBoot()
{
close();
}
void AutoBoot::passToOldHandler(quint8 command, quint16 aux)
{
if (oldDevice) {
oldDevice->handleCommand(command, aux);
} else {
sio->port()->writeCommandNak();
qWarning() << "!w" << tr("[%1] command: $%2, aux: $%3 NAKed.")
.arg(deviceName())
.arg(command, 2, 16, QChar('0'))
.arg(aux, 4, 16, QChar('0'));
}
}
void AutoBoot::handleCommand(quint8 command, quint16 aux)
{
switch (command) {
case 0x3F: // Speed poll
{
if (!sio->port()->writeCommandAck()) {
return;
}
sio->port()->writeComplete();
QByteArray speed(1, 0);
speed[0] = sio->port()->speedByte();
sio->port()->writeDataFrame(speed);
qDebug() << "!n" << tr("[%1] Speed poll.").arg(deviceName());
break;
}
case 0x52:
{ /* Read sector */
if (loaded) {
passToOldHandler(command, aux);
return;
}
if (aux >= 1 && aux <= sectorCount) {
if (!sio->port()->writeCommandAck()) {
return;
}
if (!started) {
emit booterStarted();
started = true;
}
QByteArray data;
if (readSector(aux, data)) {
sio->port()->writeComplete();
sio->port()->writeDataFrame(data);
qDebug() << "!n" << tr("[%1] Read sector %2 (%3 bytes).")
.arg(deviceName())
.arg(aux)
.arg(data.size());
} else {
sio->port()->writeError();
qCritical() << "!e" << tr("[%1] Read sector %2 failed.")
.arg(deviceName())
.arg(aux);
}
} else {
passToOldHandler(command, aux);
}
break;
}
case 0x53:
{ /* Get status */
if (loaded) {
passToOldHandler(command, aux);
return;
}
if (!sio->port()->writeCommandAck()) {
return;
}
QByteArray status(4, 0);
status[0] = 8;
status[3] = 1;
sio->port()->writeComplete();
sio->port()->writeDataFrame(status);
qDebug() << "!n" << tr("[%1] Get status.")
.arg(deviceName());
break;
}
case 0xFD:
{
if (!sio->port()->writeCommandAck()) {
return;
}
qDebug() << "!n" << tr("[%1] Atari is jumping to %2.")
.arg(deviceName())
.arg(aux);
emit loaderDone();
sio->port()->writeComplete();
break;
}
case 0xFE:
{ /* Get chunk */
if(aux >= chunks.count()) {
qDebug() << "!e" << tr("[%1] Invalid chunk in get chunk: aux = %2")
.arg(deviceName())
.arg(aux);
return;
}
if (!sio->port()->writeCommandAck()) {
return;
}
qDebug() << "!n" << tr("[%1] Get chunk %2 (%3 bytes).")
.arg(deviceName())
.arg(aux)
.arg(chunks.at(aux).data.size());
sio->port()->writeComplete();
sio->port()->writeDataFrame(chunks.at(aux).data);
emit blockRead(aux + 1, chunks.count());
break;
}
case 0xFF:
{ /* Get chunk info */
if(aux >= chunks.count()) {
qDebug() << "!e" << tr("[%1] Invalid chunk in get chunk info: aux = %2")
.arg(deviceName())
.arg(aux);
return;
}
if (!sio->port()->writeCommandAck()) {
return;
}
if (!loaded) {
loaded = true;
emit booterLoaded();
}
QByteArray data;
data[0] = chunks.at(aux).address % 256;
data[1] = chunks.at(aux).address / 256;
data[2] = 1;
data[3] = chunks.size() != aux + 1;
data[4] = chunks.at(aux).data.size() % 256;
data[5] = chunks.at(aux).data.size() / 256;
qDebug() << "!d" << tr("[%1] Get chunk info %2 (%3 bytes at %4).")
.arg(deviceName())
.arg(aux)
.arg(chunks.at(aux).data.size())
.arg(chunks.at(aux).address);
sio->port()->writeComplete();
sio->port()->writeDataFrame(data);
break;
}
default:
passToOldHandler(command, aux);
return;
break;
}
}
bool AutoBoot::readExecutable(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
qCritical() << "!e" << tr("Cannot open file '%1': %2")
.arg(fileName)
.arg(file.errorString());
return false;
}
int start, end;
QByteArray data;
/* Read the $FFFF header */
data = file.read(2);
if (data.size() < 2) {
QString error;
if (file.atEnd()) {
error = tr("Unexpected end of file, needed %1 more").arg(2 - data.size());
} else {
error = file.errorString();
}
qCritical() << "!e" << tr("Cannot read from file '%1': %2.")
.arg(fileName)
.arg(error);
return false;
}
start = (quint8) data.at(0) + (quint8) data.at(1) * 256;
if (start != 0xffff) {
QString error;
qCritical() << "!e" << tr("Cannot load file '%1': The file doesn't seem to be an Atari DOS executable.")
.arg(fileName)
.arg(error);
return false;
}
/* Read the start address */
data = file.read(2);
if (data.size() < 2) {
QString error;
if (file.atEnd()) {
error = tr("Unexpected end of file, needed %1 more").arg(2 - data.size());
} else {
error = file.errorString();
}
qCritical() << "!e" << tr("Cannot read from file '%1': %2.").arg(fileName).arg(error);
return false;
}
start = (quint8) data.at(0) + (quint8) data.at(1) * 256;
do {
/* Read the end address */
data = file.read(2);
/* Check consistency */
if (data.size() < 2) {
if (file.atEnd()) {
qCritical() << "!e" << tr("The executable '%1' is broken: Unexpected end of file, needed %2 more.")
.arg(fileName)
.arg(2 - data.size());
if (chunks.count() == 0) {
return false;
} else {
break;
}
} else {
qCritical() << "!e" << tr("Cannot read from file '%1': %2.")
.arg(fileName)
.arg(file.errorString());
return false;
}
}
end = (quint8) data.at(0) + (quint8) data.at(1) * 256;
qDebug() << "!d" << "Exe segment" << start << ".." << end;
if (end < start) {
qWarning() << tr("The executable '%1' is broken: The end address is less than the start address.")
.arg(fileName);
break;
}
/* Read the chunk */
int size = end - start + 1;
data = file.read(size);
if (data.size() < size) {
if (file.atEnd()) {
qWarning() << "!w" << tr("The executable '%1' is broken: Unexpected end of file, needed %2 more.")
.arg(fileName)
.arg(size - data.size());
} else {
qWarning() << "!w" << tr("Cannot read from file '%1': %2.")
.arg(fileName)
.arg(file.errorString());
return false;
}
}
int maxChunkSize = 1024;
for (int i = 0; i < data.size(); i += maxChunkSize) {
AtariExeChunk ch;
ch.data = data.mid(i, maxChunkSize);
ch.address = start;
start += maxChunkSize;
chunks.append(ch);
}
/* Exit if done */
if (file.atEnd()) {
break;
}
/* Read the next start address */
data = file.read(2);
if (data.size() < 2) {
if (file.atEnd()) {
qWarning() << "!w" << tr("The executable '%1' is broken: Unexpected end of file, needed %2 more.")
.arg(fileName)
.arg(2 - data.size());
break;
} else {
qCritical() << "!e" << tr("Cannot read from file '%1': %2.")
.arg(fileName)
.arg(file.errorString());
return false;
}
}
start = (quint8) data.at(0) + (quint8) data.at(1) * 256;
/* Skip if it's a $FFFF */
if (start == 0xffff) {
data = file.read(2);
if (data.size() < 2) {
if (file.atEnd()) {
qWarning() << "!w" << tr("The executable '%1' is broken: Unexpected end of file, needed %2 more.")
.arg(fileName)
.arg(2 - data.size());
break;
} else {
qCritical() << "!e" << tr("Cannot read from file '%1': %2.")
.arg(fileName)
.arg(file.errorString());
return false;
}
}
start = (quint8) data.at(0) + (quint8) data.at(1) * 256;
}
} while (true);
return true;
}
bool AutoBoot::open(const QString &fileName, bool highSpeed)
{
close();
QFile boot;
if (highSpeed) {
boot.setFileName(":/binaries/atari/autoboot/autoboot-hispeed.bin");
} else {
boot.setFileName(":/binaries/atari/autoboot/autoboot.bin");
}
if (!boot.open(QFile::ReadOnly)) {
qCritical() << "!e" << tr("Cannot open the boot loader: %1")
.arg(boot.errorString());
return false;
}
bootSectors = boot.readAll();
boot.close();
sectorCount = (boot.size() + 127) * 128;
chunks = QList <AtariExeChunk> ();
return readExecutable(fileName);
}
void AutoBoot::close()
{
}
bool AutoBoot::readSector(quint16 sector, QByteArray &data)
{
data = bootSectors.mid((sector - 1) * 128, 128);
data.resize(128);
return true;
}
QString AutoBoot::deviceName()
{
return "AutoBoot";
}