-
Notifications
You must be signed in to change notification settings - Fork 48
/
tdrive.cpp
559 lines (474 loc) · 15.7 KB
/
tdrive.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
// -----------------------------------------------------------------------------
// Altair 8800 Simulator
// Copyright (C) 2019 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 "tdrive.h"
#include "config.h"
#include "host.h"
#include "cpucore.h"
#include "Altair8800.h"
#include "timer.h"
#include "image.h"
#include "io.h"
#if NUM_TDRIVES == 0
void tdrive_setup() {}
void tdrive_dir() {}
const char *tdrive_get_image_filename(byte image_num, bool check_exist) { return NULL; }
const char *tdrive_get_image_description(byte image_num) { return NULL; }
bool tdrive_mount(byte drive_num, byte image_num) { return false; }
bool tdrive_unmount(byte drive_num) { return false; }
byte tdrive_get_mounted_image(byte drive_num) { return 0; }
void tdrive_reset() {}
#elif NUM_TDRIVES>4
#error Tarbell disk controller can only address 4 drives. Set NUM_TDRIVES<=4 in config.h
#elif !defined(HOST_HAS_FILESYS)
#error Disk drive emulation requires host filesystem
#else
#define DEBUGLVL 0
#define DRIVE_SECTOR_LENGTH 128
#define DRIVE_NUM_SECTORS 26
#define DRIVE_NUM_TRACKS 77
#define DRIVE_STATUS_NOTREADY 0x80
#define DRIVE_STATUS_T1_SEEKERR 0x10
#define DRIVE_STATUS_T2_RECNOTFOUND 0x10
#define DRIVE_STATUS_T1_TRACK0 0x04
#define DRIVE_STATUS_T2_DRQ 0x02
#define DRIVE_STATUS_BUSY 0x01
static byte drive_selected = 0;
static byte drive_mounted_disk[NUM_TDRIVES];
static byte drive_current_track[NUM_TDRIVES];
static HOST_FILESYS_FILE_TYPE drive_file[NUM_TDRIVES];
static byte drive_current_sector;
static bool drive_data_request;
static byte drive_track, drive_sector, drive_data, drive_status;
static byte drive_command, drive_aux;
static byte drive_data_idx, drive_data_count;
static byte drive_data_buffer[DRIVE_SECTOR_LENGTH];
static void tdrive_register_ports();
static uint32_t drive_get_file_pos(byte drive_num)
{
return drive_current_track[drive_num] * DRIVE_NUM_SECTORS * DRIVE_SECTOR_LENGTH + (drive_current_sector-1) * DRIVE_SECTOR_LENGTH;
}
static void drive_read_sector(byte drive_num)
{
if( drive_num < NUM_TDRIVES && drive_mounted_disk[drive_num]>0 )
{
#if DEBUGLVL>=1
printf("read drive %i track %i sector %i\n", drive_num, drive_current_track[drive_num], drive_current_sector);
#endif
host_filesys_file_seek(drive_file[drive_num], drive_get_file_pos(drive_num));
byte n = host_filesys_file_read(drive_file[drive_num], DRIVE_SECTOR_LENGTH, drive_data_buffer);
if( n<DRIVE_SECTOR_LENGTH ) memset(drive_data_buffer+n, 0, DRIVE_SECTOR_LENGTH-n);
}
}
static void drive_write_sector(byte drive_num)
{
if( drive_data_idx>0 && drive_num < NUM_TDRIVES && drive_mounted_disk[drive_num]>0 )
{
#if DEBUGLVL>=1
printf("write drive %i track %i sector %i\n", drive_num, drive_current_track[drive_num], drive_current_sector);
#endif
host_filesys_file_seek(drive_file[drive_num], drive_get_file_pos(drive_num));
host_filesys_file_write(drive_file[drive_num], drive_data_idx, drive_data_buffer);
host_filesys_file_flush(drive_file[drive_num]);
}
}
void tdrive_reset()
{
drive_selected = 0;
for(byte i=0; i<NUM_DRIVES; i++)
drive_current_track[i] = 0;
drive_status = 0;
drive_data_request = false;
drive_sector = 0;
drive_data = 0;
drive_data_idx = 0;
drive_command = 0;
drive_current_sector = 0;
}
void tdrive_dir()
{
Serial.print(image_get_dir_content(IMAGE_TARBELL));
}
bool tdrive_unmount(byte drive_num)
{
if( drive_num<NUM_TDRIVES && drive_mounted_disk[drive_num]>0 )
{
if( (drive_command&0xE0)==0xA0 || (drive_command&0xF0)==0xF0 ) drive_write_sector(drive_selected);
drive_mounted_disk[drive_num] = 0;
host_filesys_file_close(drive_file[drive_num]);
tdrive_register_ports();
}
return true;
}
byte tdrive_get_mounted_image(byte drive_num)
{
return drive_mounted_disk[drive_num];
}
const char *tdrive_get_image_description(byte disk_num)
{
return image_get_description(IMAGE_TARBELL, disk_num);
}
const char *tdrive_get_image_filename(byte image_num, bool check_exist)
{
return image_get_filename(IMAGE_TARBELL, image_num, check_exist);
}
bool tdrive_mount(byte drive_num, byte image_num)
{
if( drive_num<NUM_TDRIVES )
{
tdrive_unmount(drive_num);
if( image_num>0 )
{
char filename[13];
image_get_filename(IMAGE_TARBELL, image_num, filename, 13, false);
drive_mounted_disk[drive_num] = image_num;
drive_file[drive_num] = host_filesys_file_open(filename, true);
tdrive_register_ports();
return true;
}
}
return false;
}
static void tdrive_out_data(byte data)
{
drive_data = data;
if( (drive_command & 0xE0) == 0xA0 )
{
// currently executing write sector command
drive_data_buffer[drive_data_idx++] = drive_data;
// check if we have received one sector of data
if( drive_data_idx == DRIVE_SECTOR_LENGTH )
{
// write data out to file on host
drive_write_sector(drive_selected);
if( (drive_command & 0xF0) == 0xB0 && drive_sector<DRIVE_NUM_SECTORS )
{
// multi-sector write => wait for more data for next sector
drive_sector++;
drive_data_idx=0;
}
else
{
// done writing sector
drive_data_request = false;
drive_status = 0;
drive_command = 0;
}
}
}
else if( (drive_command&0xF0) == 0xF0 )
{
// currently executing write track command
drive_aux++;
if( drive_current_sector==0 )
{
// lead-in before first sector is 73 bytes long => skip
if( drive_aux==73 ) { drive_current_sector++; drive_aux = 0; }
}
else
{
// there are 30 extra bytes before and 28 after the actual data => skip
const byte skipBefore = 30, skipAfter = 28;
// drive_data_idx must be consistent with number of data bytes received
// in case the write track command gets canceled
if( drive_aux>skipBefore && drive_data_idx<DRIVE_SECTOR_LENGTH )
drive_data_buffer[drive_data_idx++] = drive_data;
if( drive_aux == skipBefore+DRIVE_SECTOR_LENGTH+skipAfter )
{
// sector complete => write data out to host
drive_data_idx = DRIVE_SECTOR_LENGTH;
drive_write_sector(drive_selected);
drive_aux = 0;
drive_data_idx = 0;
drive_current_sector++;
if( drive_current_sector>DRIVE_NUM_SECTORS )
{
// done writing track
drive_current_sector = 1;
drive_data_request = false;
drive_status = 0;
drive_command = 0;
}
}
}
}
}
static void tdrive_out_command(byte cmd)
{
if( (cmd&0xF0) == 0xD0 )
{
// force interrupt (type 4)
if( drive_command!=0 )
{
// if we are currently in write mode we still need to write all received
// data out to the disk file on the host
if( (drive_command&0xE0)==0xA0 || (drive_command&0xF0)==0xF0 )
drive_write_sector(drive_selected);
// stop executing current command
drive_command = 0;
drive_data_request = false;
}
else
{
// if not executing a command then clear status
drive_status = 0;
}
}
else if( drive_command!=0 )
{ /* ignore new commands while busy */ }
else if( (cmd&0xF0)==0x00 )
{
// restore/home (type 1)
drive_track = 0;
drive_status = DRIVE_STATUS_T1_TRACK0;
if( drive_selected<NUM_TDRIVES && drive_mounted_disk[drive_selected]>0 )
drive_current_track[drive_selected] = 0;
else if( (cmd&0x04)!=0 )
drive_status = DRIVE_STATUS_T1_SEEKERR;
}
else if( (cmd&0xF0)==0x10 )
{
// seek (type 1)
drive_track = drive_data;
if( drive_track==0 ) drive_status = DRIVE_STATUS_T1_TRACK0;
if( drive_selected<NUM_TDRIVES && drive_mounted_disk[drive_selected]>0 )
{
if( drive_track < DRIVE_NUM_TRACKS )
drive_current_track[drive_selected] = drive_track;
else
drive_current_track[drive_selected] = DRIVE_NUM_TRACKS-1;
}
else if( (cmd&0x04)!=0 )
drive_status = DRIVE_STATUS_T1_SEEKERR;
}
else if( (cmd&0xE0)<0x80 )
{
// step in/out/again (type 1)
static bool stepOut = false;
if( (cmd&0xE0)==0x40 )
stepOut = false;
else if( (cmd&0xE0)==0x60 )
stepOut = true;
// step the drive mechanism if we have a valid drive
if( drive_selected<NUM_TDRIVES && drive_mounted_disk[drive_selected]>0 )
{
if( stepOut && drive_current_track[drive_selected]>0 )
drive_current_track[drive_selected]--;
else if( !stepOut && drive_current_track[drive_selected]<DRIVE_NUM_TRACKS-1 )
drive_current_track[drive_selected]++;
}
// update track register if requested
if( cmd & 0x10 )
{
if( stepOut && drive_track>0 )
drive_track--;
else if( !stepOut && drive_track<DRIVE_NUM_TRACKS-1 )
drive_track++;
}
// update status and track register if at track 0
if( drive_selected<NUM_TDRIVES && drive_mounted_disk[drive_selected]>0 )
{
if( drive_current_track[drive_selected]==0 )
{
drive_track = 0;
drive_status = DRIVE_STATUS_T1_TRACK0;
}
}
else if( (cmd&0x04)!=0 )
drive_status = DRIVE_STATUS_T1_SEEKERR;
}
else if( (cmd&0xE0)==0x80 )
{
// read sector (type 2)
if( drive_selected<NUM_TDRIVES && drive_mounted_disk[drive_selected]>0 )
{
drive_current_sector = drive_sector;
drive_read_sector(drive_selected);
drive_data_idx = 0;
drive_data_count = DRIVE_SECTOR_LENGTH;
drive_command = cmd;
drive_data_request = true;
drive_status = DRIVE_STATUS_T2_DRQ;
}
else
drive_status = DRIVE_STATUS_T2_RECNOTFOUND;
}
else if( (cmd&0xE0)==0xA0 )
{
// write sector (type 2)
if( drive_selected<NUM_TDRIVES && drive_mounted_disk[drive_selected]>0 )
{
drive_current_sector = drive_sector;
drive_data_idx = 0;
drive_command = cmd;
drive_data_request = true;
drive_status = DRIVE_STATUS_T2_DRQ;
}
else
drive_status = DRIVE_STATUS_T2_RECNOTFOUND;
}
else if( cmd == 0xC4 )
{
// read address (type 3)
if( drive_selected<NUM_TDRIVES && drive_mounted_disk[drive_selected]>0 )
{
drive_current_sector++;
if( drive_current_sector>DRIVE_NUM_SECTORS ) drive_current_sector = 1;
drive_data_idx = 0;
drive_data_count = 6;
drive_command = cmd;
drive_data_request = true;
drive_data_buffer[0] = drive_current_track[drive_selected];
drive_data_buffer[1] = 6;
drive_data_buffer[2] = drive_current_sector;
drive_data_buffer[3] = DRIVE_SECTOR_LENGTH;
drive_data_buffer[4] = 0;
drive_data_buffer[5] = 0;
}
else
drive_status = DRIVE_STATUS_NOTREADY;
}
else if( cmd == 0xF4 )
{
// write track (type 3)
if( drive_selected<NUM_TDRIVES && drive_mounted_disk[drive_selected]>0 )
{
drive_current_sector = 0;
drive_data_idx = 0;
drive_aux = 0;
drive_command = cmd;
drive_data_request = true;
drive_status = DRIVE_STATUS_T2_DRQ;
}
else
drive_status = DRIVE_STATUS_NOTREADY;
}
else if( (cmd&0xFE) == 0xE4 )
{
// read track (type 3)
if( drive_selected<NUM_TDRIVES && drive_mounted_disk[drive_selected]>0 )
{
// NOT IMPLEMENTED => do not produce data request and
// set "RECORD NOT FOUND" status flag to signal error condition
drive_data_request = false;
drive_status = DRIVE_STATUS_T2_RECNOTFOUND;
}
else
drive_status = DRIVE_STATUS_NOTREADY;
}
}
static byte tdrive_in_data()
{
if( drive_data_idx < drive_data_count )
{
drive_data = drive_data_buffer[drive_data_idx++];
// check whether all available data has been read
if( drive_data_idx == drive_data_count )
{
if( (drive_command & 0xF0) == 0x90 && drive_sector<DRIVE_NUM_SECTORS )
{
// multi-sector read => read next sector
drive_sector++;
drive_read_sector(drive_selected);
drive_data_idx=0;
}
else
{
drive_data_request = false;
drive_status = 0;
drive_command = 0;
}
}
}
return drive_data;
}
byte tdrive_in(byte addr)
{
byte data = 0xff;
switch( addr )
{
case 0xf8: // read status register
data = drive_status;
if( drive_command!=0 ) data |= DRIVE_STATUS_BUSY;
break;
case 0xf9: // read track register
data = drive_track;
break;
case 0xfa: // read sector register
data = drive_sector;
break;
case 0xfb: // read data register
data = tdrive_in_data();
break;
case 0xfc: // wait for interrupt
data = drive_data_request ? 0x80 : 0x00;
break;
}
#if DEBUGLVL>=2
printf("%04x: tdrive_in(%02x) = %02x\n", regPC-1, addr, data);
#endif
return data;
}
void tdrive_out(byte addr, byte data)
{
#if DEBUGLVL>=2
printf("%04x: tdrive_out(%02x, %02x)\n", regPC-1, addr, data);
#endif
switch( addr )
{
case 0xf8: // write command register (execute command)
tdrive_out_command(data);
break;
case 0xf9: // write track register
drive_track = data;
break;
case 0xfa: // write sector register
drive_sector = data;
break;
case 0xfb: // write data register
tdrive_out_data(data);
break;
case 0xfc: // write extended command register
{
// if bits 0-2 equal 010 then
// update current drive (bits 4-5)
if( (data & 0x07) == 0x02 )
drive_selected = (~data & 0x30) >> 4;
break;
}
}
}
void tdrive_register_ports()
{
bool drive_used = false;
for(byte i=0; i<NUM_TDRIVES; i++)
drive_used |= drive_mounted_disk[i]!=0;
for(byte i=0xf8; i<=0xfd; i++)
{
io_register_port_inp(i, drive_used ? tdrive_in : NULL);
io_register_port_out(i, drive_used ? tdrive_out : NULL);
}
}
void tdrive_setup()
{
for(byte i=0; i<NUM_TDRIVES; i++)
drive_mounted_disk[i] = 0;
tdrive_register_ports();
tdrive_reset();
}
#endif