-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiskfs.cpp
More file actions
491 lines (413 loc) · 9.31 KB
/
Copy pathdiskfs.cpp
File metadata and controls
491 lines (413 loc) · 9.31 KB
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
#include "device.h"
#include "iec.h"
#include "diskfs.h"
#include "1541rom.h"
#include <string.h>
#include <ctype.h>
CIECFSDrive::CIECFSDrive(const char *path)
{
strcpy(orig_dir_path, path);
dir_path[0] = 0;
if (ChangeDir(orig_dir_path)) {
for (int i=0; i<16; i++)
file[i] = NULL;
Reset();
}
}
CIECFSDrive::~CIECFSDrive()
{
CloseAllChannels();
}
void CIECFSDrive::Reset(void)
{
CloseAllChannels();
cmd_len = 0;
name_length = 0;
SetError(ERR_STARTUP, 0, 0);
}
bool CIECFSDrive::ChangeDir(char *dirpath)
{
if ( dirpath[0] ) {
strcpy(dir_path, dirpath);
strncpy(dir_title, dir_path, 16);
return !!ad_set_curr_dir(dir_path);
} else
return false;
}
unsigned char CIECFSDrive::Open(int channel)
{
SetError(ERR_OK, 0, 0);
if (channel == 15) {
ExecuteCommand(cmd_buffer);
return ST_OK;
}
if (file[channel]) {
fclose(file[channel]);
file[channel] = NULL;
}
switch ( name_buf[0] ) {
case '$':
return OpenDirectory(channel, name_buf+1);
case '#':
SetError(ERR_NOCHANNEL, 0, 0);
return ST_OK;
default:
return OpenFile(channel, name_buf);
}
}
unsigned char CIECFSDrive::Open(int channel, char *nameBuf)
{
if (nameBuf)
strcpy(name_buf, nameBuf);
return Open(channel);
}
unsigned char CIECFSDrive::OpenFile(int channel, char *filename)
{
char plainname[NAMEBUF_LENGTH];
int filemode;
int filetype;
bool wildflag = false;
char mode[4];
char extension[5]; /* File extension */
ParseFileName(filename, plainname, &filemode, &filetype, &wildflag);
// Channel 0 is READ PRG, channel 1 is WRITE PRG
if (channel <= 1) {
filemode = channel ? FMODE_WRITE : FMODE_READ;
if (filetype == FTYPE_DEL)
filetype = FTYPE_PRG;
}
if (wildflag) {
if (filemode != FMODE_READ) {
SetError(ERR_SYNTAX33, 0, 0);
return ST_OK;
}
findFirstFile(plainname);
} else {
switch (filetype) {
case FTYPE_PRG:
strcpy(extension, ".prg"); // Add .prg extension
break;
/*case FTYPE_SEQ:
strcpy( extension, ".seq"); // Add .seq extension
break;*/
default:
strcpy(extension, ".prg");
}
strcat(plainname, extension); /* Add extension */
}
//fprintf(stderr,"Searching for: %s\n", plainname);
switch (filemode) {
default:
case FMODE_READ:
strcpy(mode, "rb");
break;
case FMODE_WRITE:
strcpy(mode, "wb");
break;
case FMODE_APPEND:
strcpy(mode, "ab");
break;
}
if ((file[channel] = fopen(plainname, mode)) != NULL) {
if (filemode == FMODE_READ) // Read and buffer first byte
read_char[channel] = fgetc(file[channel]);
} else {
SetError(ERR_FILENOTFOUND, 0, 0);
return ST_ERROR;
}
return ST_OK;
}
void CIECFSDrive::ParseFileName(char *srcname, char *destname, int *filemode, int *filetype, bool *wildflag)
{
char *p, *q;
int i;
if ((p = strchr(srcname, ':')) != NULL)
p++;
else
p = srcname;
q = destname;
for (i=0; i<NAMEBUF_LENGTH && (*q++ = ToPETSCII(*p++)); i++) ;
p = destname;
while ((p = strchr(p, ',')) != NULL) {
*p++ = 0;
switch (*p) {
case 'p':
*filetype = FTYPE_PRG;
break;
case 's':
*filetype = FTYPE_SEQ;
break;
case 'u':
*filetype = FTYPE_USR;
break;
case 'r':
*filemode = FMODE_READ;
break;
case 'w':
*filemode = FMODE_WRITE;
break;
case 'a':
*filemode = FMODE_APPEND;
break;
}
}
*wildflag = strpbrk(destname, "?*") != NULL;
}
bool CIECFSDrive::findFirstFile(char *name)
{
bool found = true;
char *filename, cmpname[NAMEBUF_LENGTH];
char fname[NAMEBUF_LENGTH];
// convert to uppercase
for ( filename = name; filename<name+strlen(name); filename++)
*filename=toupper(*filename);
if ( !(ad_set_curr_dir(dir_path)) )
return false;
strcat(name, ".prg");
if (!ad_find_first_file(name))
return false;
found = ad_return_current_filename() != 0;
while (found) {
char *currfname = ad_return_current_filename();
if (currfname) {
char *ext;
strncpy( fname, currfname, NAMEBUF_LENGTH);
strcpy( cmpname, currfname);
ext = strrchr( cmpname, '.' );
// FIXME! filetype check!
if ( ext ) {
*ext++ = '\0';
}
// Match found? Then copy real file name
if (Match(name, cmpname)) {
strncpy(name, fname, strlen(fname)+1 );
return true;
}
// Get next directory entry
}
found = !!ad_find_next_file();
}
ad_find_file_close();
return found;
}
unsigned char CIECFSDrive::OpenDirectory(int channel, char *filename)
{
char buf[] = "\001\004\001\001\0\0\022\042 \042 00 2A";
char str[NAMEBUF_LENGTH];
char pattern[NAMEBUF_LENGTH];
char *p, *q;
int i;
int filemode;
int filetype;
bool wildflag;
bool found = true;
char fname[NAMEBUF_LENGTH] = "";
if (filename[0] == '0' && filename[1] == 0)
filename += 1;
ParseFileName(filename, pattern, &filemode, &filetype, &wildflag);
if ( ad_find_first_file("*.prg"))
strcpy( fname, ad_return_current_filename());
if ( !fname[0] )
found = false;
if ((file[channel] = tmpfile()) == NULL) {
return ST_OK;
}
p = &buf[8];
for (i=0; i<16 && dir_title[i] ; i++)
*p++ = ToPETSCII(dir_title[i]);
fwrite(buf, 1, 32, file[channel]);
while (found) {
if (Match(pattern, fname)) {
memset(buf, ' ', 31);
buf[31] = 0;
p = buf;
*p++ = 0x01;
*p++ = 0x01;
int size = ad_get_current_filesize();
// Size in blocks
i = (size + 254) / 254;
*p++ = i & 0xff;
*p++ = (i >> 8) & 0xff;
p++;
if (i < 10) p++;
if (i < 100) p++;
strcpy(str, fname);
str[ strlen(str)-4 ] = '\0';
*p++ = '\"';
q = p;
for (i=0; i<16 && str[i]; i++)
*q++ = ToPETSCII(str[i]);
*q++ = '\"';
p += 18;
strncpy( p, "PRG", 3);
p += 3;
fwrite(buf, 1, 32, file[channel]);
}
// Take next directory entry
if ((found = !!ad_find_next_file())) {
strcpy( fname, ad_return_current_filename());
}
}
fwrite("\001\001\0\0BLOCKS FREE. \0\0", 1, 32, file[channel]);
rewind(file[channel]);
read_char[channel] = fgetc(file[channel]);
ad_find_file_close();
return ST_OK;
}
unsigned char CIECFSDrive::Close(int channel)
{
// Closing channel 15 closes all other channels
if (channel == 15) {
CloseAllChannels();
return ST_OK;
}
if (file[channel]) {
fclose(file[channel]);
file[channel] = NULL;
}
return ST_OK;
}
unsigned char CIECFSDrive::Read(int channel, unsigned char *byte)
{
int c;
if (channel == 15) {
*byte = *errorPtr++;
errorLength--;
if (*byte != '\r' || !errorLength)
return ST_OK;
else { // End of message
SetError(ERR_OK, 0, 0);
return ST_ERROR; // not ST_EOF!
}
}
if (!file[channel]) return ST_ERROR;
// Read one byte
*byte = read_char[channel];
c = fgetc(file[channel]);
if (c == EOF)
return ST_EOF;
else {
read_char[channel] = c;
return ST_OK;
}
}
void CIECFSDrive::setEoI(unsigned int channel)
{
if (channel == 15) {
cmd_buffer[cmd_len] = 0;
cmd_len = 0;
}
else {
name_buf[name_length] = 0;
name_length = 0;
}
}
Uint8 CIECFSDrive::Write(int channel, unsigned char data, unsigned int cmd, bool eoi)
{
if (channel == 15) {
if (cmd_len >= 58)
return ST_ERROR;
cmd_buffer[cmd_len++] = data;
return ST_OK;
}
switch (cmd) {
case IEC_CMD_OPEN:
name_buf[name_length++] = data;
if (name_length>=16)
return ST_ERROR;
return ST_OK;
case IEC_CMD_DATA:
if (!file[channel]) {
SetError(ERR_FILENOTOPEN, 0, 0);
return ST_ERROR;
}
if (fputc(data, file[channel]) == EOF && !eoi) {
SetError(ERR_WRITEERROR, 0, 0);
return ST_ERROR;
}
return ST_OK;
default:
return ST_ERROR;
}
}
void CIECFSDrive::ExecuteCommand(char *command)
{
unsigned short adr;
int len, i;
switch (command[0]) {
case 'B':
if (command[1] != '-')
SetError(ERR_SYNTAX30, 0, 0);
else
switch (command[2]) {
case 'E':
adr = ((unsigned char)command[4] << 8) | ((unsigned char)command[3]);
fprintf( stderr, "B-E ($%04X) : not supported with DOS level emulation.\n", adr);
break;
default:
SetError(ERR_SYNTAX30, 0, 0);
break;
}
break;
case 'I':
CloseAllChannels();
SetError(ERR_OK, 0, 0);
break;
case 'U':
if ((command[1] & 0x0f) == 0x0a) {
Reset();
} else
SetError(ERR_SYNTAX30, 0, 0);
break;
case 'G':
if (command[1] != ':')
SetError(ERR_SYNTAX30, 0, 0);
else
ChangeDirCmd(&command[2]);
break;
case 'M':
if (command[1] != '-')
SetError(ERR_SYNTAX30, 0, 0);
else
switch (command[2]) {
case 'R':
adr = (command[4] << 8) | (command[3]);
if (adr < 0xC000)
errorPtr = (char*)(ram + (adr & 0x07ff));
else
errorPtr = (char *)(rom1541 + (adr & 0x3fff));
if (!(errorLength = command[5]))
errorLength = 1;
break;
case 'W':
adr = (command[4] << 8) | (command[3]);
len = command[5];
if (adr<0x1000)
for (i=0; i<len; i++)
ram[(adr+i)&0x0FFF] = command[i+6];
break;
case 'E':
adr = (command[4] << 8) | (command[3]);
fprintf( stderr, "M-E ($%04X) : not supported with DOS level emulation.\n", adr);
default:
SetError(ERR_SYNTAX30, 0, 0);
}
break;
default:
SetError(ERR_SYNTAX30, 0, 0);
}
}
void CIECFSDrive::ChangeDirCmd(char *dirpath)
{
char str[NAMEBUF_LENGTH];
char *p = str;
CloseAllChannels();
if (dirpath[0] == '.' && dirpath[1] == 0) {
ChangeDir(orig_dir_path);
} else {
// Convert directory name
for (int i=0; i<NAMEBUF_LENGTH && (*p++ = ToASCII(*dirpath++)); i++) ;
if (!ChangeDir(str))
SetError(ERR_NOTREADY, 0, 0);
}
}