forked from Larswad/uno2iec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2idriver.cpp
executable file
·579 lines (471 loc) · 11.3 KB
/
m2idriver.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//
// Title : M2IDRIVER
// Author : Lars Pontoppidan
// Date : May. 2007
// Version : 0.1
// Target MCU : AtMega32(L) at 8 MHz
//
//
// DESCRIPTION:
// This module implements support for the file format with extension .m2i
// With an .m2i file, it is possible to mimic an unlimited size diskette with
// read/write support and realistic filenames, while actually using FAT files.
//
// .m2i format:
//
// <DISK TITLE (16 chars)><cr><lf>
// <file type 1 char>:<dos filename 12chars>:<cbm filename 16 chars><cr><lf>
// <file type 1 char>:<dos filename 12chars>:<cbm filename 16 chars><cr><lf>
// <file type 1 char>:<dos filename 12chars>:<cbm filename 16 chars><cr><lf>
//
// file types: P means prg file
// D means del file
// - means deleted file
//
// DISCLAIMER:
// The author is in no way responsible for any problems or damage caused by
// using this code. Use at your own risk.
//
// LICENSE:
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
#include <string.h>
#include "m2idriver.hpp"
// Local vars, driver state:
namespace {
const QString strError1("M2I FILE ERROR");
const QString strPRG("PRG");
const QString strDEL("DEL");
const QString strID(" M2I");
const QString strDotPRG(".PRG\0");
const QString strDotM2I(".M2I\0");
}
// method below performs init of the driver with the given ATN command string.
bool M2I::openHostFile(const QString& fileName)
{
closeHostFile();
// Interface has just opened the m2i file, save filename
m_hostFile.setFileName(fileName);
if(!m_hostFile.open(QIODevice::ReadOnly))
return false;
// We accept m2i file if first line is OK:
return readFirstLine();
} // openHostFile
void M2I::closeHostFile()
{
if(!m_hostFile.fileName().isEmpty() and m_hostFile.isOpen())
m_hostFile.close();
m_status = NOT_READY;
} // closeHostFile
bool M2I::sendListing(ISendLine &cb)
{
m_status = NOT_READY;
// disk title
QString parsed;
// Reseek from beginning of M2I.
if(!m_hostFile.seek(0) or !readFirstLine(&parsed)) {
cb.send(0, strError1);
return false;
}
// First line is disk title
QString line = QString("\x12\x22%1\x22%2").arg(parsed, strID);
cb.send(0, line);
while(!m_hostFile.atEnd()) {
// Write lines
uchar ftype(parseLine(0, &parsed));
if('P' == ftype or 'D' == ftype) {
line = QString(" \x22%1\x22 %2").arg(parsed, 'p' == ftype ? strDotPRG : strDEL);
cb.send(0, line);
}
}
return true;
} // sendListing
// Seek through M2I index. dosname and dirname must be pointers to
// 12 and 16 byte buffers. Dosname can be NULL.
//
bool M2I::seekFile(QString* dosName, QString& dirName, const QString& seekName,
bool doOpen)
{
Q_UNUSED(dosName);
Q_UNUSED(dirName);
Q_UNUSED(seekName);
Q_UNUSED(doOpen);
bool found = false;
/*
uchar i;
uchar seeklen, dirlen;
if(doOpen) {
// Open the .m2i file
if (!fatFopen(m_hostFile))
return false;
}
else {
// Just ensure we are at beginning
fatFseek(0, SEEK_SET);
}
if(!readFirstLine(0))
return false;
// Find length of seek name, disregard ending spaces
seeklen = strnlen(seekname, 16);
for(; (seeklen > 0) and (seekname[seeklen-1] == ' '); seeklen--);
while(!found and !fatFeof()) {
i = parseLine(dosname, dirname);
if('P' == i) {
// determine length of dir filename
for(dirlen = 16; (dirlen > 0) and (dirname[dirlen - 1] == ' '); dirlen--);
// Compare filename respecting * and ? wildcards
found = true;
for(i = 0; i < seeklen and found; i++) {
if(seekname[i] == '?') {
// This character is ignored
}
else if(seekname[i] == '*') {
// No need to check more chars
break;
}
else
found = seekname[i] == dirname[i];
}
// If searched to end of filename, dir filename must end here also
if(found and (i == seeklen))
found = (seeklen == dirlen);
}
}
// Close m2i file
if(doOpen)
closeHostFile();
*/
return found;
} // seekFile
bool M2I::remove(char* fileName)
{
Q_UNUSED(fileName);
bool ret = false;
/*
char dosname[12];
char dirname[16];
uchar found;
m_status and_eq compl FILE_OPEN;
// Open m2i index
if(!fatFopen(m2i_filename))
return false;
do {
// Seek for filename
found = seekFile(dosname, dirname, filename, false);
if (found) {
ret = true;
// File found, delete entry in m2i, seek back length of one entry
fatFseek((ulong)(-33), SEEK_CUR);
// write a -
fatFputc('-');
// Close m2i
fatFclose();
// Delete fat file
fatRemove(dosname);
// Open m2i again
fatFopen(m2i_filename);
}
} while(found);
// Finally, close m2i
fatFclose();
*/
return ret;
} // remove
bool M2I::rename(char* oldName, char* newName)
{
Q_UNUSED(oldName);
Q_UNUSED(newName);
bool ret = false;
/*
char dirName[16];
uchar j;
m_status and_eq compl FILE_OPEN;
// Open m2i index
if(!fatFopen(m2i_filename))
return false;
// Look for oldname
if(seekFile(NULL, dirName, oldName, false)) {
// File found, rename entry in m2i
// Seek back until name
fatFseek((ulong)(-33 + 15), SEEK_CUR);
// write new name, space padded
j = newName[0];
for(uchar i = 0; i < 16;i++) {
if (j) {
fatFputc(j);
j = newName[i + 1];
}
else
fatFputc(' ');
}
ret = true;
}
// Close m2i
fatFclose();
*/
return ret;
} // rename
uchar M2I::newDisk(char* diskName)
{
Q_UNUSED(diskName);
/*
char dosName[13];
uchar i,j;
m_status and_eq compl FILE_OPEN;
// Find new .m2i filename
// Take starting 8 chars padded with space ending with .m2i
strncpy(dosName, diskName, 8);
i = strnlen(dosName, 8);
if (i < 8)
memset(dosName + i, ' ', 8 - i);
memcpy(dosName + 8, pstr_dot_m2i, 5);
// Is this filename free
if(fatFopen(dosName)) {
fatFclose();
return ErrFileExists;
}
// It is free, create new file
if(!fatFcreate(dosName))
return ErrWriteProtectOn;
// Write m2i header with the diskname
j = diskName[0];
for(i = 0; i < 16; i++) {
if(j) {
fatFputc(j);
j = diskName[i + 1];
}
else
fatFputc(' ');
}
// Write newline
fatFputc(13);
fatFputc(10);
fatFclose();
// Now this is the open m2i
memcpy(m2i_filename, dosName, 13);
*/
return ErrOK;
} // newDisk
// Respond to command channel command
//
uchar M2I::cmd(char* cmd)
{
Q_UNUSED(cmd);
uchar ret = ErrOK;
/*
// Get command letter and argument
char cmdLetter;
char *arg, *p;
cmdLetter = cmd[0];
arg = strchr(cmd,':');
if(0 not_eq arg) {
arg++;
if(cmdLetter == 'S') {
// Scratch file(s)
if(!remove(arg))
ret = ErrFileNotFound;
}
else if(cmdLetter == 'R') {
// Rename, find = and replace with 0 to get cstr
p = strchr(arg, '=');
if(0 not_eq p) {
*p = 0;
p++;
if(!rename(p, arg))
ret = ErrFileNotFound;
}
}
else if(cmdLetter == 'N')
ret = newDisk(arg);
else
ret = ErrSyntaxError;
}
*/
return ret;
} // cmd
bool M2I::fopen(const QString& fileName)
{
Q_UNUSED(fileName);
/*
char dirname[16];
char dosname[12];
m_status = NOT_READY;
// Look for filename
if(seekFile(dosname, dirname, filename, true)) {
// Open the dos name corresponding!
if(fatFopen(dosname))
m_status or_eq FILE_OPEN;
}
*/
return(m_status bitand FILE_OPEN);
} // open
// The new function ensures filename exists and is empty.
//
uchar M2I::newFile(char* fileName)
{
Q_UNUSED(fileName);
/*
char dirName[16];
char dosName[12];
m_status = NOT_READY;
// Look for filename
if(seekFile(dosName, dirName, fileName, true)) {
// File exists, delete it
fatRemove(dosName);
// And create empty
fatFcreate(dosName);
m_status or_eq FILE_OPEN;
}
else {
// File does not exist, create it
createFile(fileName);
}
*/
return(m_status bitand FILE_OPEN);
} // newFile
QString M2I::openedFileName() const
{
// TODO: implement!
return QString();
} // openedFileName
ushort M2I::openedFileSize() const
{
// TODO: implement!
return 0;
} // openedFileSize
char M2I::getc(void)
{
/*
if(m_status bitand FILE_OPEN)
return fatFgetc();
*/
return 0;
} // getc
bool M2I::isEOF(void) const
{
/*
if(m2i_status bitand FILE_OPEN)
return fatFeof();
*/
return true;
} // isEOF
// write char to open file, returns false if failure
bool M2I::putc(char c)
{
Q_UNUSED(c);
/*
if(m_status bitand FILE_OPEN)
return fatFputc(c);
*/
return false;
} // putc
// close file
bool M2I::close(void)
{
m_status and_eq compl FILE_OPEN;
closeHostFile();
return true;
} // close
// Get first line of m2i file, writes 16 chars in dest
// Returns FALSE if there is a problem in the file
bool M2I::readFirstLine(QString* dest)
{
bool res = false;
char data[18];
if(sizeof(data) == m_hostFile.read(data, sizeof(data))) {
QByteArray qdata(data, sizeof(data));
if(0 not_eq dest)
*dest = qdata.left(sizeof(data) - 2);
res = qdata.endsWith("\r");
}
return res;
} // readFirstLine
// Parses one line from the .m2i file.
// File pos must be after reading the initial status char
//
// <file type char>:<dosname>:<cbmname><cr><lf>
//
// at dosname 12 chars is written, at cbmname 16 chars
//
uchar M2I::parseLine(QString* dosName, QString* cbmName)
{
if(m_hostFile.atEnd())
return 0; // no more records
// ftype + fsName + cbmName + separators + LF
QList<QByteArray> qData = m_hostFile.read(1 + 256 + 16 + 3 + 1).split(':');
// must be three splits.
if(qData.count() < 3)
return 0;
// first one ONLY the fs type.
if(qData.at(0).count() not_eq 1)
return 0;
uchar fsType(qData.at(0).at(0));
if(qData.at(1).isEmpty())
return 0;
if(0 not_eq dosName)
*dosName = qData.at(1);
// must be ending with a CR, the cbm string must be 16 chars long.
if(!qData.at(2).endsWith("\r") or qData.at(2).length() not_eq 17)
return 0;
QString cbmStr(qData.at(2));
cbmStr.chop(1);
if(0 not_eq cbmName)
*cbmName = cbmStr;
return fsType;
} // parseLine
bool M2I::createFile(char* fileName)
{
Q_UNUSED(fileName);
// TODO: Implement
m_status = NOT_READY;
/*
// Find dos filename
char dosName[13];
uchar i,j;
// Initial guess is starting 8 chars padded with space ending with .prg
strncpy(dosName, fileName, 8);
i = strnlen(dosName, 8);
if(i < 8)
memset(dosName + i, ' ', 8 - i);
memcpy(dosName + 8, pstr_dot_prg, 5);
// Is this filename free?
i = 0;
while(fatFopen(dosName)) {
if(i > 99)
return false;
// No, modify it
j = i % 10;
dosName[6] = i - j + '0';
dosName[7] = j + '0';
i++;
}
// Now we have a suitable dos filename, create entry in m2i index
if(!fatFopen(m2i_filename))
return false;
if(!read_first_line(NULL))
return false;
fatFseek(0,SEEK_END);
fatFputc('P');
fatFputc(':');
for(i = 0; i < 12; i++)
fatFputc(dosName[i]);
fatFputc(':');
j = fileName[0];
for(i = 0; i < 16; i++) {
if(j) {
fatFputc(j);
j = fileName[i + 1];
}
else
fatFputc(' ');
}
fatFputc(13);
fatFputc(10);
fatFclose();
// Finally, create the target file
fatFcreate(dosName);
m_status or_eq FILE_OPEN;
*/
return true;
} // createFile