forked from dhansel/Altair8800
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdmanager.cpp
491 lines (418 loc) · 12.1 KB
/
sdmanager.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
// -----------------------------------------------------------------------------
// 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 "config.h"
#if (NUM_DRIVES>0 || NUM_HDSK_UNITS>0) && defined(__SAM3X8E__)
#include <Arduino.h>
#include "sdmanager.h"
#include "XModem.h"
#include "host.h"
#include <SD.h>
File datafile;
int recvChar(int msDelay)
{
unsigned long start = millis();
while( millis()-start < msDelay)
{
if( Serial.available() ) return (uint8_t) Serial.read();
delay(1);
}
return -1;
}
void sendChar(char sym)
{
Serial.write(sym);
}
bool dataHandlerSend(unsigned long no, char* data, int size)
{
if( datafile )
{
unsigned long offset = (no-1)*128;
if( datafile.seek(offset) && datafile.position()==offset )
{
int n = datafile.read((uint8_t *) data, size);
// if there was nothing more to read then signal end-of-file
if( n==0 ) return false;
// XMODEM sends blocks of 128 bytes so if we have less than that
// fill the rest of the buffer with EOF (ASCII 26) characters
while( n<size ) data[n++]=26;
return true;
}
}
// either file was not open or we got past the end
return false;
}
bool dataHandlerReceive(unsigned long no, char* data, int size)
{
if( datafile )
{
unsigned long offset = (no-1)*128;
if( datafile.seek(offset) && datafile.position()==offset )
return datafile.write((uint8_t *) data, size) == size;
}
return false;
}
static char *getFilename(const char *prompt)
{
static char buf[16];
int l = 0, dot_pos = -1;
Serial.print(prompt);
while(1)
{
int c = Serial.read();
if( (c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') || c=='_' )
{
if( c>='a' && c<='z' ) c -= 32;
if( (dot_pos<0 && l<8) || (dot_pos>=0 && l-dot_pos<4) )
{ buf[l++] = c; Serial.write(c); }
}
else if( c=='.' && dot_pos<0 )
{
dot_pos = l;
buf[l++]=c;
Serial.write(c);
}
else if( c==8 || c==127 )
{
if( l>0 )
{
l--;
if( buf[l]=='.' ) dot_pos = -1;
Serial.print(F("\010 \010"));
}
}
else if( c==9 )
{
// TAB=auto-complete
bool first = true;
File entry, dir = SD.open("/");
dir.rewindDirectory();
// go through directory and try to find a match
while( entry=dir.openNextFile() )
{
if( !entry.isDirectory() && strncmp(buf, entry.name(), l)==0 )
{
if( first )
{
// first match => take full file name
strcpy(buf, entry.name());
first = false;
}
else
{
// subsequent match => reduce to matching characters
for(int i=l; buf[i]; i++)
if( entry.name()[i] != buf[i] )
{ buf[i] = 0; break; }
}
}
entry.close();
}
dir.close();
host_clr_status_led_HLDA();
if( first )
{
// no match found => send BELL
Serial.write(7);
}
else if( buf[l]==0 )
{
// nothing added => print matching files
File entry, dir = SD.open("/");
dir.rewindDirectory();
int c=4;
while( entry=dir.openNextFile() )
{
if( !entry.isDirectory() && strncmp(buf, entry.name(), l)==0 )
{
if( c++ == 4 ) { c=0; Serial.println(); }
Serial.print(entry.name());
for(int i=strlen(entry.name()); i<14; i++) Serial.print(' ');
}
entry.close();
}
dir.close();
host_clr_status_led_HLDA();
Serial.println('\n');
Serial.print(prompt);
Serial.print(buf);
}
else
{
// print added characters, set new length and dot position
while( buf[l] ) { Serial.write(buf[l]); if( buf[l]=='.' ) dot_pos = l; l++; }
}
}
else if( c==13 )
{
Serial.println();
buf[l]=0;
return buf;
}
else if( c==27 )
{
Serial.println();
return NULL;
}
}
}
static void consume_input()
{
while( true )
{ if( Serial.read()<0 ) { delay(15); if( Serial.read()<0 ) { delay(150); if( Serial.read()<0 ) break; } } }
}
static bool checkOverwrite(char *fname)
{
if( SD.exists(fname) )
{
char c=0;
Serial.print(F("File ")); Serial.print(fname); Serial.print(F(" exists. Overwrite (y/n)? "));
while(c!='y' && c!='n' && c!=27) c = Serial.read();
Serial.println(c);
return c=='y';
}
return true;
}
static void receiveFile()
{
Serial.println();
char *fname = getFilename("File name to receive: ");
if( fname!=NULL && checkOverwrite(fname) )
{
SD.remove(fname);
datafile = SD.open(fname, FILE_WRITE);
if( datafile )
{
XModem modem(recvChar, sendChar, dataHandlerReceive);
bool isempty = false;
Serial.println(F("\nInitiate XMODEM send now."));
if( modem.receive() )
Serial.println(F("\r\nSuccess!"));
else
{
Serial.println(F("\r\nERROR!"));
isempty = (datafile.size()==0);
}
datafile.close();
// if nothing was received then delete the empty file
if( isempty ) SD.remove(fname);
consume_input();
}
else
{ Serial.print(F("Can not write file: ")); Serial.println(fname); }
}
host_clr_status_led_HLDA();
}
static void sendFile()
{
Serial.println();
char *fname = getFilename("File name to send: ");
if( fname!=NULL )
{
datafile = SD.open(fname, FILE_READ);
if( datafile )
{
XModem modem(recvChar, sendChar, dataHandlerSend);
Serial.println(F("\nInitiate XMODEM receive now."));
if( modem.transmit() )
Serial.println(F("Success!"));
else
Serial.println(F("ERROR!"));
datafile.close();
consume_input();
}
else
{ Serial.print(F("Can not read file: ")); Serial.println(fname); }
}
host_clr_status_led_HLDA();
}
static void deleteFile()
{
Serial.println();
char *fname = getFilename("File name to delete: ");
if( fname!=NULL )
{
if( SD.exists(fname) )
{
char c=0;
Serial.print(F("Really delete file ")); Serial.print(fname); Serial.print(F(" (y/n)? "));
while(c!='y' && c!='n') c = Serial.read();
Serial.println(c);
if( c=='y' )
{
if( !SD.remove(fname) )
{ Serial.print(F("Can not delete file:")); Serial.println(fname); }
else
Serial.println(F("Ok."));
}
}
else
{ Serial.print(F("File does not exist: ")); Serial.println(fname); }
}
host_clr_status_led_HLDA();
}
static void copyFile()
{
Serial.println();
char *fname = getFilename("File name to copy: ");
if( fname!=NULL )
{
File from = SD.open(fname, FILE_READ);
if( from )
{
fname = getFilename("Copy to: ");
if( fname!=NULL && checkOverwrite(fname) )
{
SD.remove(fname);
File to = SD.open(fname, FILE_WRITE);
if( to )
{
int n;
byte buffer[1024];
Serial.print(F("Copying..."));
unsigned long t = millis();
while( from && to && (n=from.read(buffer, 1024))>0 )
{
if( (millis()-t)>500 ) { Serial.print('.'); t = millis(); }
to.write(buffer, n);
}
if( !from || !to )
Serial.println(F("ERROR!"));
else
Serial.println(F("Ok."));
from.close();
to.close();
}
else
{ Serial.print(F("Can not write file: ")); Serial.println(fname); }
}
}
else
{ Serial.print(F("Can not read file: ")); Serial.println(fname); }
}
host_clr_status_led_HLDA();
}
static void typeFile()
{
Serial.println();
char *fname = getFilename("File name to type out: ");
if( fname!=NULL )
{
File f = SD.open(fname, FILE_READ);
if( f )
{
char c;
while( f.read(&c, 1) )
{
Serial.write(c);
c = Serial.read();
if( c==27 || c==3 ) break;
}
Serial.println("\033[0m");
}
else
{ Serial.print(F("Can not read file: ")); Serial.println(fname); }
}
host_clr_status_led_HLDA();
}
static void printDirectory()
{
File entry, dir = SD.open("/");
Serial.println();
dir.rewindDirectory();
while( entry=dir.openNextFile() )
{
if( !entry.isDirectory() )
{
Serial.print(entry.name());
int n=16-strlen(entry.name());
while(n-->0) Serial.print(' ');
Serial.println(entry.size(), DEC);
}
entry.close();
}
dir.rewindDirectory();
dir.close();
host_clr_status_led_HLDA();
}
void sd_manager()
{
host_serial_interrupts_pause();
Serial.println(F("\033[2J\033[0;0H"));
Serial.println(F("SD card file manager"));
bool redraw = true;
while(1)
{
if( redraw )
{
Serial.println(F("\n(d)irectory"));
Serial.println(F("(t)ype out file"));
Serial.println(F("(r)eceive file via XMODEM"));
Serial.println(F("(s)end file via XMODEM"));
Serial.println(F("(c)opy file"));
Serial.println(F("(e)rase file"));
Serial.println(F("(h)elp"));
Serial.println(F("E(x)it"));
redraw = false;
}
Serial.print(F("\nCommand: "));
byte c = 0;
while( !((c>=32 && c<=126) || c==27) )
{
while( !Serial.available() ) delay(50);
c = Serial.read();
}
Serial.println((char) c);
switch( c )
{
case 'd':
printDirectory();
break;
case 'r':
receiveFile();
break;
case 's':
sendFile();
break;
case 'e':
deleteFile();
break;
case 'c':
copyFile();
break;
case 't':
typeFile();
break;
case 'x':
case 27:
{
host_serial_interrupts_resume();
return;
}
default:
{
redraw = true;
break;
}
}
}
}
#else
void sd_manager()
{}
#endif