forked from dhansel/Altair8800
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesys.cpp
679 lines (562 loc) · 17.5 KB
/
filesys.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// -----------------------------------------------------------------------------
// Altair 8800 Simulator
// Copyright (C) 2017 David Hansel
//
// 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 3 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, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------
#include "filesys.h"
#include "host.h"
#include "numsys.h"
#include "serial.h"
struct DirEntryStruct
{
byte name1, name2;
uint16_t len;
uint32_t pos;
};
#define MAX_OPEN_FILES 3
static byte num_open_files = 0;
static uint32_t dir_start = 0;
static struct DirEntryStruct file_data[MAX_OPEN_FILES];
#ifndef HOST_BUFFERSIZE
#define HOST_BUFFERSIZE 0
#endif
#if HOST_BUFFERSIZE>1
static uint32_t write_buffer_len = 0;
static byte write_buffer[HOST_BUFFERSIZE];
#endif
static byte dir_get_num_entries()
{
byte n;
host_read_data(&n, HOST_STORAGESIZE-4, 1);
return n;
}
static void dir_set_num_entries(byte n)
{
host_write_data(&n, HOST_STORAGESIZE-4, 1);
dir_start = HOST_STORAGESIZE-4-(n*sizeof(struct DirEntryStruct));
}
static void dir_write_entry(byte num, struct DirEntryStruct *entry)
{
uint32_t entrypos = HOST_STORAGESIZE-4-((num+1)*sizeof(struct DirEntryStruct));
host_write_data(entry, entrypos, sizeof(struct DirEntryStruct));
}
static void dir_read_entry(byte num, struct DirEntryStruct *entry)
{
uint32_t entrypos = HOST_STORAGESIZE-4-((num+1)*sizeof(struct DirEntryStruct));
host_read_data(entry, entrypos, sizeof(struct DirEntryStruct));
}
static byte dir_find_file(char nm1, char nm2, struct DirEntryStruct *entry)
{
//printf("filesys_find_file(%c, %c)\n", nm1, nm2);
byte num_entries = dir_get_num_entries();
for(byte i=0; i<num_entries; i++)
{
dir_read_entry(i, entry);
if( entry->name1==(byte) nm1 && entry->name2==(byte) nm2 )
return i;
}
return 0xff;
}
static uint32_t dir_get_filespace_end()
{
byte numentries = dir_get_num_entries();
struct DirEntryStruct entry;
uint32_t filespaceend = 0;
for(byte i=0; i<numentries; i++)
{
//printf("%i\n", i);
dir_read_entry(i, &entry);
if( entry.pos+entry.len > filespaceend )
filespaceend = entry.pos+entry.len;
}
return filespaceend;
}
static byte alloc_file_id()
{
for(byte i=0; i<MAX_OPEN_FILES; i++)
if( file_data[i].name1==0 )
{
num_open_files++;
return i;
}
return 0;
}
static void free_file_id(byte fid)
{
if( num_open_files>0 && fid>0 && fid<=MAX_OPEN_FILES && file_data[fid-1].name1 != 0 )
{
file_data[fid-1].name1 = 0;
num_open_files--;
}
}
static void filesys_delete(byte dirindex)
{
struct DirEntryStruct entry, entry2;
byte numentries = dir_get_num_entries();
if( dirindex < numentries )
{
uint32_t filespaceend = dir_get_filespace_end();
dir_read_entry(dirindex, &entry);
// move all other data up
host_move_data(entry.pos, entry.pos+entry.len, filespaceend-entry.len);
// update directory entries for all files located past the deleted one
filespaceend = 0;
for(byte i=0; i<numentries; i++)
if( i != dirindex )
{
dir_read_entry(i, &entry2);
if( entry2.pos > entry.pos )
{
entry2.pos -= entry.len;
dir_write_entry(i, &entry2);
}
if( entry2.pos+entry2.len > filespaceend )
filespaceend = entry2.pos+entry2.len;
}
// move the directory entries
host_move_data(HOST_STORAGESIZE-4-((numentries-1)*sizeof(struct DirEntryStruct)),
HOST_STORAGESIZE-4-(numentries*sizeof(struct DirEntryStruct)),
(numentries-dirindex-1)*sizeof(struct DirEntryStruct));
// set new number of directory entries
dir_set_num_entries(numentries-1);
}
}
void filesys_delete(char nm1, char nm2)
{
struct DirEntryStruct entry;
filesys_delete(dir_find_file(nm1, nm2, &entry));
}
byte filesys_open_write(char nm1, char nm2)
{
byte fid = 0;
//printf("filesys_open_write(%c, %c)\n", nm1, nm2);
if( num_open_files < MAX_OPEN_FILES )
{
// current limitation: can not start writing while another file is open
for(int i=0; i<MAX_OPEN_FILES; i++)
if( file_data[i].name1!=0 )
return false;
byte numentries = dir_get_num_entries();
uint32_t filespaceend = dir_get_filespace_end();
struct DirEntryStruct entry, entry2;
byte dirindex = dir_find_file(nm1, nm2, &entry);
if( dirindex==0xff )
{
// check that there's enough space to create the directory entry
if( filespaceend + (numentries+1)*sizeof(struct DirEntryStruct) + 4 >= HOST_STORAGESIZE )
return 0;
// new file => add directory entry
dirindex = numentries;
dir_set_num_entries(numentries+1);
}
else
{
// file exists => move all other data up (i.e. delete file)
host_move_data(entry.pos, entry.pos+entry.len, filespaceend-entry.len);
// update directory entries for all files located past the deleted one
filespaceend = 0;
for(byte i=0; i<numentries; i++)
if( i != dirindex )
{
dir_read_entry(i, &entry2);
if( entry2.pos > entry.pos )
{
entry2.pos -= entry.len;
dir_write_entry(i, &entry2);
}
if( entry2.pos+entry2.len > filespaceend )
filespaceend = entry2.pos+entry2.len;
}
}
fid = alloc_file_id();
// update the directory information for this file
file_data[fid].name1 = nm1;
file_data[fid].name2 = nm2;
file_data[fid].pos = filespaceend;
file_data[fid].len = 0;
dir_write_entry(dirindex, file_data+fid);
// file mode is 'write' and remember directory index
file_data[fid].name1 = 'W';
file_data[fid].name2 = dirindex;
fid++;
#if HOST_BUFFERSIZE>1
write_buffer_len = 0;
#endif
}
return fid;
}
byte filesys_open_read(char nm1, char nm2)
{
byte fid = 0, dirindex;
//printf("filesys_open_read(%c, %c)\n", nm1, nm2);
struct DirEntryStruct entry;
if( num_open_files < MAX_OPEN_FILES )
if( (dirindex=dir_find_file(nm1, nm2, &entry))!=0xff && entry.len>0 )
{
fid = alloc_file_id();
file_data[fid].name1 = 'R';
file_data[fid].name2 = dirindex;
file_data[fid].pos = entry.pos;
file_data[fid].len = entry.len;
fid++;
}
//printf("filesys_open_read(%c, %c) = %i\n", nm1, nm2, fid);
return fid;
}
bool filesys_write_char(byte fid, byte c)
{
return filesys_write_data(fid, &c, 1);
}
bool filesys_write_data(byte fid, const void *data, uint16_t len)
{
//printf("filesys_write_data(%i,%i)\n", fid, len);
struct DirEntryStruct *entry = file_data+(fid-1);
if( entry->name1!='W' )
return false;
#if HOST_BUFFERSIZE>1
if( entry->pos + entry->len + write_buffer_len + len < dir_start )
{
if( write_buffer_len + len > HOST_BUFFERSIZE || len > HOST_BUFFERSIZE/2 )
{
host_write_data(write_buffer, entry->pos + entry->len, write_buffer_len);
entry->len += write_buffer_len;
write_buffer_len = 0;
}
if( len > HOST_BUFFERSIZE/2 )
{
host_write_data(data, entry->pos + entry->len, len);
entry->len += len;
}
else
{
memcpy(write_buffer+write_buffer_len, data, len);
write_buffer_len += len;
}
return true;
}
#else
if( entry->pos + entry->len + len < dir_start )
{
host_write_data(data, entry->pos + entry->len, len);
entry->len += len;
return true;
}
#endif
return false;
}
bool filesys_write_file(char nm1, char nm2, const void *data, uint16_t len)
{
bool res = false;
struct DirEntryStruct entry;
//printf("filesys_write_file(%c, %c)\n", nm1, nm2);
byte dirindex = dir_find_file(nm1, nm2, &entry);
if( dirindex!=0xff && entry.len==len )
{
// file exists and has same length
host_write_data(data, entry.pos, entry.len);
res = true;
}
else
{
byte fid = filesys_open_write(nm1, nm2);
if( fid )
{
res = filesys_write_data(fid, data, len);
filesys_close(fid);
}
}
return res;
}
uint16_t filesys_read_file(char nm1, char nm2, void *data, uint16_t len)
{
uint16_t res = 0;
byte fid = filesys_open_read(nm1, nm2);
if( fid )
{
res = filesys_read_data(fid, data, len);
filesys_close(fid);
}
return res;
}
bool filesys_eof(byte fid)
{
struct DirEntryStruct *entry = file_data+(fid-1);
if( entry->name1=='R' )
return entry->len==0;
else if( entry->name1=='W' )
{
#if HOST_BUFFERSIZE>1
return entry->pos + entry->len + write_buffer_len + 1 >= dir_start;
#else
return entry->pos + entry->len + 1 >= dir_start;
#endif
}
else
return true;
}
bool filesys_is_write(byte fid)
{
return file_data[fid-1].name1=='W';
}
bool filesys_is_read(byte fid)
{
return file_data[fid-1].name1=='R';
}
bool filesys_read_char(byte fid, byte *c)
{
return filesys_read_data(fid, c, 1)>0;
}
uint16_t filesys_read_data(byte fid, void *d, uint16_t len)
{
struct DirEntryStruct *entry = file_data+(fid-1);
if( entry->name1!='R' )
return false;
if( entry->len < len )
len = entry->len;
if( len>0 )
{
host_read_data(d, entry->pos, len);
entry->pos += len;
entry->len -= len;
}
return len;
}
void filesys_close(byte fid)
{
//printf("filesys_close()\n");
struct DirEntryStruct *fileinfo = file_data+(fid-1);
if( fileinfo->name1 == 'W' )
{
// file was opened for writing
#if HOST_BUFFERSIZE>1
if( write_buffer_len>0 )
{
host_write_data(write_buffer, fileinfo->pos + fileinfo->len, write_buffer_len);
fileinfo->len += write_buffer_len;
write_buffer_len = 0;
}
#endif
// update length in directory entry
struct DirEntryStruct entry;
dir_read_entry(fileinfo->name2, &entry);
entry.len = fileinfo->len;
dir_write_entry(fileinfo->name2, &entry);
}
free_file_id(fid);
}
void filesys_print_dir()
{
// ID Type Name Size
// -----------------------------------
// 01 memory page C000-C0FF 256
// 02 captured data file #00 30
// 03 BASIC program a 800
// 04 <80> <81>
Serial.println(F("\033[2J\033[0;0H"));
Serial.println(F("ID Type Name Size"));
Serial.println(F("-----------------------------------"));
byte numEntries = dir_get_num_entries();
struct DirEntryStruct entry;
uint16_t totalUsed = 4+numEntries*sizeof(struct DirEntryStruct);
for(byte i=0; i<numEntries; i++)
{
dir_read_entry(i, &entry);
numsys_print_byte(i);
Serial.print(' ');
switch( entry.name1 )
{
case 'M':
{
Serial.print(F("Memory page #"));
numsys_print_byte(entry.name2);
Serial.print(F(" "));
break;
}
case 'D':
{
Serial.print(F("Captured data #"));
numsys_print_byte(entry.name2);
Serial.print(F(" "));
break;
}
case 'B':
{
Serial.print(F("BASIC program "));
Serial.print((char) entry.name2);
Serial.print(F(" "));
break;
}
case 'C':
{
Serial.print(F("Configuration #"));
numsys_print_byte(entry.name2);
Serial.print(F(" "));
break;
}
default:
{
Serial.print('<');
numsys_print_byte(entry.name1);
Serial.print('>');
Serial.print(F(" "));
Serial.print('<');
numsys_print_byte(entry.name2);
Serial.print('>');
Serial.print(F(" "));
break;
}
}
if( entry.len<10000 ) Serial.print(' ');
if( entry.len<1000 ) Serial.print(' ');
if( entry.len<100 ) Serial.print(' ');
if( entry.len<10 ) Serial.print(' ');
Serial.print(entry.len);
for(byte j=0; j<MAX_OPEN_FILES; j++)
if( file_data[j].name1!=0 && file_data[j].name2==i )
{
switch( file_data[j].name1 )
{
case 'W': Serial.print(F(" [currently open for writing]")); break;
case 'R': Serial.print(F(" [currently open for reading]")); break;
default: Serial.print(F(" ???")); break;
}
}
Serial.println();
totalUsed += entry.len;
}
Serial.println();
Serial.print(F("Filesystem size: ")); Serial.print(HOST_STORAGESIZE); Serial.println(F(" bytes"));
Serial.print(F(" in use: ")); Serial.print(totalUsed); Serial.println(F(" bytes"));
Serial.print(F(" available: ")); Serial.print(HOST_STORAGESIZE-totalUsed); Serial.println(F(" bytes"));
}
void filesys_manage()
{
bool redraw = true;
if( num_open_files>0 )
{
Serial.println(F("\n"));
filesys_print_dir();
Serial.println(F("\n\nFile system is locked."));
Serial.println(F("Press any key to return to main menu..."));
while( !serial_available() ) delay(50);
serial_read();
return;
}
while( true )
{
if( redraw )
{
Serial.print(F("\n\n"));
filesys_print_dir();
Serial.print(F("\n\nCommand (dFrx): "));
}
while( !serial_available() ) delay(50);
redraw = true;
switch( serial_read() )
{
case 27:
case 'x':
return;
case 'd':
{
Serial.println();
Serial.print(F("\nDelete file with id: "));
byte i = (byte) numsys_read_word();
Serial.println();
Serial.print(F("Really delete file with id "));
numsys_print_byte(i);
Serial.print(F(" (y/n)? "));
while( !serial_available() );
if( serial_read()=='y' )
filesys_delete(i);
break;
}
case 'r':
{
Serial.println();
Serial.print(F("\nRead file with id: "));
byte i = (byte) numsys_read_word();
Serial.println();
struct DirEntryStruct entry;
dir_read_entry(i, &entry);
byte fid = filesys_open_read(entry.name1, entry.name2);
if( fid )
{
uint16_t addr = 0;
byte n, i, buf[16];
while( (n=(byte) filesys_read_data(fid, &buf, 16))>0 )
{
numsys_print_word(addr); Serial.print(':'); Serial.print(' ');
for(i=0; i<n; i++)
{
numsys_print_byte(buf[i]);
Serial.print(' ');
if( i==7 ) Serial.print(' ');
}
for(i=n; i<16; i++)
{
Serial.print(' '); Serial.print(' '); Serial.print(' ');
if( i==8 ) Serial.print(' ');
}
Serial.print(' '); Serial.print(' ');
for(i=0; i<n; i++)
{
Serial.print(buf[i]>=32 && buf[i]<127 ? char(buf[i]) : '.');
if( i==7 ) Serial.print(' ');
}
addr += n;
Serial.println();
}
filesys_close(fid);
while( !serial_available() );
serial_read();
}
break;
}
case 'F':
{
Serial.println();
Serial.print(F("\nReally re-format and lose all data (y/n)? "));
while( !serial_available() );
if( serial_read()=='y' )
dir_set_num_entries(0);
break;
}
default:
redraw = false;
break;
}
}
}
void filesys_setup()
{
// check id
char buf[4];
host_read_data(buf, HOST_STORAGESIZE-4, 4);
if( buf[1]!='A' || buf[2]!='F' || buf[3]!='S' )
{
buf[0] = 0;
buf[1] = 'A';
buf[2] = 'F';
buf[3] = 'S';
// write id + zero directoty entries
host_write_data(buf, HOST_STORAGESIZE-4, 4);
}
// set the current lower border of the directory
dir_start = HOST_STORAGESIZE-4-(dir_get_num_entries()*sizeof(struct DirEntryStruct));
// zero out file_data structure
memset(file_data, 0, MAX_OPEN_FILES*sizeof(struct DirEntryStruct));
}