-
Notifications
You must be signed in to change notification settings - Fork 10
/
exor.c
1284 lines (1203 loc) · 56.2 KB
/
exor.c
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* EXORcister simulator
* Copyright
* (C) 2011 Joseph H. Allen
*
* This 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 1, or (at your option) any later version.
*
* It 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 software; see the file COPYING. If not, write to the Free Software Foundation,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Exorciser / swtpc emulator */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include "sim6800.h"
#include "exor.h"
#include "exorterm.h"
#include "utils.h"
char *lpt_name; /* Name of line printer file */
FILE *lpt_file; /* Line printer file */
/* Options */
int swtpc = 0;
const char *exbug_name; /* = "exbug.bin"; */
int trace_disk = 0; /* Enable disk trace */
int lower = 0; /* Allow lower case */
int protect_roms = 1; /* Protect "ROM"s from writing if set */
/* Diskettes */
struct drive_info {
const char *name;
FILE *f;
int bytes; /* Bytes per sector */
int tracks; /* Tracks per disk */
int sects; /* Sectors per track */
} drive[4] =
{
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 }
};
/* Memory */
unsigned char mem[65536];
int pending_read_ahead = 1;
unsigned char read_ahead_c;
int count = 10;
int polling = 1; /* Allow ACIA polling */
static int saved;
const char *local_prefix;
/* Copy a file by name */
int copyfile(const char *src, const char *dest)
{
FILE *f, *g;
printf("Copying %s to %s...\n", src, dest);
f = fopen(src, "r");
if (f)
{
g = fopen(dest, "w");
if (g)
{
char buf[1024];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), f)))
fwrite(buf, 1, len, g);
fclose(g);
fclose(f);
return 0;
}
else
{
fclose(f);
return -1;
}
}
else
{
return -1;
}
}
/* Find configuration or state file */
/* If 'copy' set, make a writable copy of the file in the user's home directory */
const char *choose_config_file(const char *name, int copy)
{
FILE *f;
/* Create path $HOME/.exorsim */
if (!local_prefix)
{
char *home = getenv("HOME");
char *tmp = malloc(strlen(home) + strlen("/.exorsim") + 1);
sprintf(tmp, "%s/.exorsim", home);
local_prefix = tmp;
mkdir(local_prefix, 0700); /* Create directory in case it doesn't exist */
}
/* First try current directory */
f = fopen(name, "r");
if (f)
{
fclose(f);
return name;
}
else
{
/* Next, try ~/.exotsim */
char *local = malloc(strlen(local_prefix) + 1 + strlen(name) + 1);
sprintf(local, "%s/%s", local_prefix, name);
f = fopen(local, "r");
if (f)
{
fclose(f);
return local;
}
else
{
/* Try /usr/local/share/exorsim */
char *sys = malloc(strlen(DATADIR) + strlen(name) + 1);
sprintf(sys, "%s%s", DATADIR, name);
if (copy)
{
/* Make local copy */
copyfile(sys, local);
return local;
}
else
{
/* Otherwise it had better be there */
return sys;
}
}
}
}
void getsect(int n, int addr, int sect, int len)
{
if (trace_disk) printf("Read sector %d into %x, size=%d\n", sect, addr, len);
if (drive[n].f) {
fseek(drive[n].f, sect * drive[n].bytes, SEEK_SET);
fread(mem + addr, len, 1, drive[n].f);
} else {
printf("Tried to read from non-existent disk %d\n", n);
stop = 1;
}
}
void getsect1(int n, unsigned char *addr, int ofst, int len)
{
if (trace_disk) printf("Read sector %d\n", ofst / 256);
fseek(drive[n].f, ofst, SEEK_SET);
fread(addr, len, 1, drive[n].f);
}
void putsect(int n, int addr, int sect, int len)
{
if (trace_disk) printf("Write sector %d into %x, size=%d\n", sect, addr, len);
if (drive[n].f) {
fseek(drive[n].f, sect * drive[n].bytes, SEEK_SET);
fwrite(mem + addr, len, 1, drive[n].f);
fflush(drive[n].f);
} else {
printf("Tried to write to non-existent disk %d\n", n);
stop = 1;
}
}
void putsect1(int n, unsigned char *addr, int ofst, int len)
{
if (trace_disk) printf("Write sector %d\n", ofst / 256);
fseek(drive[n].f, ofst, SEEK_SET);
fwrite(addr, len, 1, drive[n].f);
fflush(drive[n].f);
}
/* FD1771 emulation.. */
int cur_drive = 0;
int cur_sect = 1;
int cur_track = 0;
unsigned char cur_status = 0;
unsigned char cur_buf[256];
int cur_count;
unsigned char cur_data;
int cur_state; /* 0 = IDLE, 1 = read single, 2 = read multiple, 3 = write single */
int cur_dir = 1;
int count1;
/* All memory reads go through this function */
unsigned char mread(unsigned short addr)
{
unsigned char c;
int rtn;
if (swtpc) {
switch (addr) {
#if 0
case 0x8004: {
if (count--)
return 0x00;
else {
count = 10;
return 0x03;
}
#endif
case 0x8004: { /* Check serial port status */
if (polling) {
int flags;
if (pending_read_ahead)
return 0x03;
flags = fcntl(fileno(stdin), F_GETFL);
if (flags == -1) {
printf("fcntl error\n");
exit(-1);
}
fcntl(fileno(stdin), F_SETFL, flags | O_NONBLOCK);
rtn = read(fileno(stdin), &read_ahead_c, 1);
fcntl(fileno(stdin), F_SETFL, flags);
if (rtn == 1) {
count = 0;
pending_read_ahead = 1;
return 0x03;
} else {
if (count == 1000)
poll(NULL, 0, 1); /* Don't hog CPU time */
else
++count;
return 0x02;
}
} else {
/* No polling: return false then true */
if (count--)
return 0x00;
else {
count = 10;
return 0x03;
}
}
} case 0x8005: { /* Read from serial port */
if (polling) {
c = read_ahead_c;
pending_read_ahead = 0;
} else {
int rtn = 0;
int flags = fcntl(fileno(stdin), F_GETFL);
c = '?';
if (flags == -1) {
printf("fcntl error\n");
exit(-1);
}
while (rtn < 1 && !stop) {
fcntl(fileno(stdin), F_SETFL, flags | O_NONBLOCK);
rtn = read(fileno(stdin), &c, 1);
fcntl(fileno(stdin), F_SETFL, flags);
if (rtn < 1 && !stop) {
poll(NULL, 0, 8); /* Don't hog CPU time */
}
}
}
if (!lower && c >= 'a' && c <= 'z')
c += 'A' - 'a';
if (swtpc) {
if (c == 127)
c = 8;
} else {
if (c == 8)
c = 127;
}
saved = c;
return c;
} case 0x8007: { /* Alias, used to test if this is really an ACIA */
return saved;
} case 0x8018: { /* status */
if (cur_state == 1) {
if (count1) {
if (!--count1) {
/* Not readying? Give lost data. */
/* The code sometimes reads without taking data to verify a previous
write (by checking for CRC error). */
cur_state = 0;
cur_status = 0x04;
}
}
}
return cur_status;
} case 0x8019: { /* track */
return cur_track;
} case 0x801a: { /* sector */
return cur_sect;
} case 0x801b: { /* data */
/* printf("Read %d\n", cur_state); */
if (cur_state == 1) {
count1 = 100;
c = cur_buf[cur_count++];
if (cur_count == drive[cur_drive].bytes) {
/* printf("Sector %d done\n", cur_sect); */
cur_state = 0;
cur_status = 0;
}
return c;
} else if (cur_state == 2) {
count1 = 100;
c = cur_buf[cur_count++];
if (cur_count == drive[cur_drive].bytes) {
if (cur_sect == drive[cur_drive].sects) {
/* printf("Sector %d done, track done.\n", cur_sect); */
cur_status = 0; /* All done! */
cur_state = 0;
} else {
/* printf("Sector %d done\n", cur_sect); */
++cur_sect;
cur_count = 0;
getsect1(cur_drive, cur_buf, (cur_track * drive[cur_drive].sects + (cur_sect - 1)) * drive[cur_drive].bytes, drive[cur_drive].bytes);
}
}
return c;
} else {
return 0;
}
} default: {
return mem[addr];
}
}
} else {
switch (addr) {
case 0xFCF9: {
return 0x80;
} case 0xFCF8: {
return 0xF;
} case 0xFCF4: { /* Check serial port status */
if (quick_term_poll())
return 0x03;
else
return 0x02;
} case 0xFCF5: { /* Read from serial port */
if (quick_term_poll())
return term_in();
else
return 0;
} default: {
return mem[addr];
}
}
}
}
/* All memory writes go through this function */
void mwrite(unsigned short addr, unsigned char data)
{
if (swtpc) {
if (protect_roms)
{
/* Do not write to ROM */
if (addr >= 0xe000 && addr < 0xe400)
return;
}
mem[addr] = data;
switch (addr) {
case 0x8018: { /* Command */
switch (data & 0xF0) {
case 0x00: { /* Restore */
/* printf("FD1771 restore!\n"); */
cur_track = 0;
cur_sect = 1;
cur_dir = 1;
if (drive[cur_drive].f)
cur_status = 0x00;
else
cur_status = 0xD0;
break;
} case 0x10: { /* Seek */
/* printf("FD1771 seek to track %d\n", cur_data); */
cur_track = cur_data;
if (drive[cur_drive].f)
cur_status = 0x00;
else
cur_status = 0xD0;
break;
} case 0x30: { /* Step */
printf("FD1771 tried to step\n");
if ((int)cur_track + cur_dir != -1)
cur_track += cur_dir;
cur_state = 0x00;
if (drive[cur_drive].f)
cur_status = 0x00;
else
cur_status = 0xD0;
break;
} case 0x50: { /* Step in */
printf("FD1771 tried to step in\n");
++cur_track;
cur_dir = 1;
if (drive[cur_drive].f)
cur_status = 0x00;
else
cur_status = 0xD0;
break;
} case 0x70: { /* Step out */
printf("FD1771 tried to step out\n");
if (cur_track)
--cur_track;
cur_dir = -1;
if (drive[cur_drive].f)
cur_status = 0x00;
else
cur_status = 0xD0;
break;
} case 0x80: { /* Read single */
/* printf("FD1771 read single\n"); */
if (drive[cur_drive].f) {
getsect1(cur_drive, cur_buf, (cur_track * drive[cur_drive].sects + (cur_sect - 1)) * drive[cur_drive].bytes, drive[cur_drive].bytes);
cur_state = 1;
cur_count = 0;
cur_status = 0x03; /* DRQ + BUSY */
count1 = 100;
} else {
cur_status = 0x90;
}
break;
} case 0x90: { /* Read multiple */
/* printf("FD1771 read multiple\n"); */
if (drive[cur_drive].f) {
getsect1(cur_drive, cur_buf, (cur_track * drive[cur_drive].sects + (cur_sect - 1)) * drive[cur_drive].bytes, drive[cur_drive].bytes);
cur_state = 2;
cur_count = 0;
cur_status = 0x03; /* DRQ + BUSY */
} else {
cur_status = 0x90;
}
break;
} case 0xA0: { /* Write single */
if (drive[cur_drive].f) {
cur_state = 3;
cur_count = 0;
cur_status = 0x03; /* DQA + BUSY */
} else {
cur_status = 0x90;
}
/* printf("FD1771 write single\n"); */
break;
} case 0xB0: { /* Write multiple */
printf("FD1771 tried to write multiple\n");
break;
} case 0xC0: { /* Read track */
printf("FD1771 tried to read track\n");
break;
} case 0xF0: { /* Write track */
printf("FD1771 tried to write track\n");
break;
} case 0xD0: { /* Force interrupt */
break;
} default: {
printf("Unknown FD1771 command %x\n", data);
exit(-1);
}
}
break;
} case 0x8014: { /* Set drive */
cur_drive = (data & 3);
/* printf("Set drive to %x\n", data); */
break;
} case 0x8019: { /* Track */
/* printf("Set track = %d\n", data); */
cur_track = data;
break;
} case 0x801a: { /* Sector */
/* printf("Set sector = %d\n", data); */
if (!data)
data = 1;
cur_sect = data;
break;
} case 0x801b: { /* Data */
cur_data = data;
if (cur_state == 3) {
/* printf("Write data %d\n", cur_count); */
cur_buf[cur_count++] = data;
if (cur_count == drive[cur_drive].bytes) {
printf("Write done.\n");
cur_state = 0;
cur_status = 0;
putsect1(cur_drive, cur_buf, (cur_track * drive[cur_drive].sects + (cur_sect - 1)) * drive[cur_drive].bytes, drive[cur_drive].bytes);
}
}
break;
} case 0x8005: { /* Write to serial port */
putchar(data); fflush(stdout);
break;
}
}
} else {
/* Do not write to ROM */
if (protect_roms)
{
if ((addr >= 0xE800 && addr < 0xEC00) ||
(addr >= 0xF000 && addr < 0xFC00) ||
(addr >= 0xFCFC && addr < 0xFD00))
return;
}
mem[addr] = data;
switch (addr) {
case 0xFCF5: { /* Write to serial port */
term_out(data);
/* putchar(data); fflush(stdout); */
}
}
}
}
unsigned short pull2();
/* Addresses of floppy parameters */
#define CURDRV 0 /* Current drive: 0 -3 */
#define STRSCT 1 /* Starting sector (2 bytes) */
#define NUMSCT 3 /* Number of sectors (2 bytes) */
#define LSCTLN 5 /* Length of last sector (1 byte) */
#define CURADR 6 /* Transfer address (2 bytes) */
#define FDSTAT 8 /* Error status: 0x30 means no error */
#define SCTCNT 0x0B /* Sector count (2 byts): (STRSCT + NUMSCT - SCTCNT - 1) is bad sector number */
#define SIDES 0x0D /* bit 7 = 1 means single-sided, bit 7 = 0 means double-sided */
/* MDOS disk error codes for FDSTAT */
#define ER_NON '0' /* No error */
#define ER_CRC '1' /* Data CRC error */
#define ER_WRT '2' /* Write protected disk */
#define ER_RDY '3' /* Disk not ready */
#define ER_MRK '4' /* Deleted data mark encountered */
#define ER_TIM '5' /* Timeout */
#define ER_DAD '6' /* Invalid disk address */
#define ER_SEK '7' /* Seek error */
#define ER_DMA '8' /* Data address mark error */
#define ER_ACR '9' /* Address mark CRC error */
/* Check drive number */
int check_drive(int n)
{
if (n >= 4) {
// printf("\r\nFloppy error: attempt to access drive number %d >= 4\n", n);
mem[FDSTAT] = ER_RDY;
c_flag = 1;
return -1;
}
if (!drive[n].f) {
// printf("\r\nFloppy error: attempt to access non-existent disk %d\n", n);
mem[FDSTAT] = ER_RDY;
c_flag = 1;
return -1;
}
return 0;
}
/* Check sector number */
int check_sect(int n, int sect)
{
if (sect >= drive[n].sects * drive[n].tracks) {
printf("\r\nFloppy error: attempt to access past end of disk %d, sector %d\n", n, sect);
mem[FDSTAT] = ER_DAD;
c_flag = 1;
return -1;
}
return 0;
}
/* Send character to printer */
void lpt_out(unsigned char c)
{
if (c)
{
if (lpt_file)
{
fputc(c, lpt_file);
if (c == '\n')
{
fflush(lpt_file);
}
}
else
{
term_out(c);
}
}
}
/* All jumps go through this function */
int intercept_inch = -1;
int echo_flag_addr;
int exbug_detected = 0;
void jump(unsigned short addr)
{
if (swtpc) {
pc = addr;
return;
} else {
if (addr == intercept_inch) {
/* Intercept INCH function */
/* Note that we don't intercept output functions, we just emulate the ACIA hardware, see mwrite() */
acca = term_in();
if (!mem[echo_flag_addr]) { /* Echo flag */
term_out(acca);
/* putchar(c);
fflush(stdout); */
} else {
mem[echo_flag_addr] = 0;
}
c_flag = 0; /* No error */
}
else
{
/* Intercept EXORdisk-II calls */
/* EXORdisk-II PROM is 0xE800 - 0xEBFF */
/* Note that Line Printer driver is included in the EXORdisk-II PROM */
switch (addr) {
case 0xE800: /* OSLOAD (no modified parms) */ {
printf("\nOSLOAD...\n");
getsect(0, 0x0020, 23, 128);
getsect(0, 0x0020 + 0x0080, 24, 128);
pc = 0x0020;
sp = 0x00FF;
return;
} case 0xE822: /* FDINIT (no modified parms) */ {
c_flag = 0;
break;
}
#if 0
case 0xF853: /* CHKERR */ {
break;
} case 0xE85A: /* PRNTER */ {
break;
}
#endif
case 0xE869: /* READSC (read full sectors) */ {
mem[LSCTLN] = 128;
} case 0xE86D: /* READPS (read partial sectors) (FDSTAT, carry, sides) */ {
int x;
int n = mem[CURDRV];
int first = (mem[STRSCT] << 8) + mem[STRSCT + 1];
int num = (mem[NUMSCT] << 8) + mem[NUMSCT + 1];
int addr = (mem[CURADR] << 8) + mem[CURADR + 1];
int last = mem[LSCTLN];
if (trace_disk) printf("Read sectors: drive=%d, first=%d, number=%d, addr=%x, size of last=%d\n", n, first, num,
addr, last);
if (check_drive(n))
break;
for (x = 0; x != num; ++x) {
if (check_sect(n, first + x))
goto oops;
getsect(n, addr + 128 * x, first + x, ((x + 1 == num) ? mem[LSCTLN] : 128));
}
mem[FDSTAT] = ER_NON;
if (drive[n].tracks == 77)
mem[SIDES] = 0x80;
else
mem[SIDES] = 0;
c_flag = 0;
oops: break;
} case 0xE86F: /* RDCRC */ {
if (trace_disk) printf("RDCRC\n");
int x;
int n = mem[CURDRV];
int first = (mem[STRSCT] << 8) + mem[STRSCT + 1];
int num = (mem[NUMSCT] << 8) + mem[NUMSCT + 1];
int addr = (mem[CURADR] << 8) + mem[CURADR + 1];
int last = mem[LSCTLN];
if (trace_disk) printf("RDCRC: drive=%d, first=%d, number=%d, addr=%x, size of last=%d\n", n, first, num,
addr, last);
if (check_drive(n))
break;
for (x = 0; x != num; ++x) {
if (check_sect(n, first + x))
goto oops;
}
mem[FDSTAT] = ER_NON;
if (drive[n].tracks == 77)
mem[SIDES] = 0x80;
else
mem[SIDES] = 0;
c_flag = 0;
break;
} case 0xE875: /* RESTOR */ {
int n = mem[CURDRV];
if (trace_disk) printf("RESTOR\n");
if (check_drive(n))
break;
mem[FDSTAT] = ER_NON;
if (drive[n].tracks == 77)
mem[SIDES] = 0x80;
else
mem[SIDES] = 0;
c_flag = 0;
break;
} case 0xE878: /* SEEK */ {
int n = mem[CURDRV];
int first = (mem[STRSCT] << 8) + mem[STRSCT + 1];
if (trace_disk) printf("SEEK\n");
if (check_drive(n))
break;
if (check_sect(n, first))
break;
if (drive[n].tracks == 77)
mem[SIDES] = 0x80;
else
mem[SIDES] = 0;
c_flag = 0;
break;
} case 0xE872: /* RWTEST */ {
if (trace_disk) printf("RWTEST\n");
} case 0xE87B: /* WRTEST */ {
unsigned char buf[128];
int x;
int n = mem[CURDRV];
int first = (mem[STRSCT] << 8) + mem[STRSCT + 1];
int num = (mem[NUMSCT] << 8) + mem[NUMSCT + 1];
int addr = (mem[CURADR] << 8) + mem[CURADR + 1];
if (trace_disk) printf("WRTEST\n");
if (check_drive(n))
break;
for (x = 0; x != 128; x += 2) {
buf[x] = mem[addr];
buf[x + 1] = mem[addr + 1];
}
for(x=0; x != num; ++x) {
if (check_sect(n, first + x))
goto oops;
if (trace_disk) printf("Wrtest sector %d drive %d\n", first + x, n);
fseek(drive[n].f, (first + x) * 128, SEEK_SET);
fwrite(buf, 128, 1, drive[n].f);
fflush(drive[n].f);
}
c_flag = 0;
if (drive[n].tracks == 77)
mem[SIDES] = 0x80;
else
mem[SIDES] = 0;
mem[FDSTAT] = ER_NON;
break;
} case 0xE87E: /* WRDDAM */ {
int n = mem[CURDRV];
printf("\r\nFloppy error: we do not support WRDDAM\n");
c_flag = 1;
if (drive[n].tracks == 77)
mem[SIDES] = 0x80;
else
mem[SIDES] = 0;
mem[FDSTAT] = ER_WRT;
break;
} case 0xE884: /* WRITSC */ {
if (trace_disk) printf("WRITSC\n");
} case 0xE881: /* WRVERF */ {
int x;
int n = mem[CURDRV];
int first = (mem[STRSCT] << 8) + mem[STRSCT + 1];
int num = (mem[NUMSCT] << 8) + mem[NUMSCT + 1];
int addr = (mem[CURADR] << 8) + mem[CURADR + 1];
int last = mem[LSCTLN];
if (trace_disk) printf("WRVERF: drive=%d, first=%d, number=%d, addr=%x, size of last=%d\n", n, first, num,
addr, last);
if (check_drive(n))
break;
for(x=0; x != num; ++x) {
if (check_sect(n, first + x))
goto oops;
putsect(n, addr + 128 * x, first + x, 128);
}
if (drive[n].tracks == 77)
mem[SIDES] = 0x80;
else
mem[SIDES] = 0;
mem[FDSTAT] = ER_NON;
c_flag = 0;
break;
} case 0xE887: /* CLOCK */ {
printf("Floppy: Someone called CLOCK?\n");
c_flag = 0;
break;
} case 0xEBC0: /* LPINIT */ {
if (trace_disk) printf("LPINIT\n");
c_flag = 0;
break;
} case 0xEBCC: /* LIST */ {
if (trace_disk) printf("LIST\n");
lpt_out(acca);
c_flag = 0;
break;
} case 0xEBE4: /* LDATA */ {
if (trace_disk)printf("LDATA\n");
while (mem[ix] != 4) {
lpt_out(mem[ix]);
++ix;
}
lpt_out('\r');
lpt_out('\n');
c_flag = 0;
break;
} case 0xEBF2: /* LDATA1 */ {
if (trace_disk) printf("LDATA1\n");
while (mem[ix] != 4) {
lpt_out(mem[ix]);
++ix;
}
c_flag = 0;
break;
} default: {
pc = addr;
return;
}
}
}
/* Notice that call was intercepted */
simulated(addr);
/* Return from subroutine now */
addr = pull2();
jump(addr);
}
}
int load_exbug()
{
FILE *f = fopen(exbug_name, "r");
if (!f) {
fprintf(stderr, "Couldn't load '%s'\n", exbug_name);
return -1;
}
if (1 != fread(mem, 64 * 1024, 1, f)) {
fprintf(stderr, "Couldn't read '%s'\n", exbug_name);
return -1;
}
printf("'%s' loaded.\n", exbug_name);
fclose(f);
if (!memcmp(&mem[0xFA8B], "\xb6\xfc\xf4\x47", 4))
{
printf(" EXBUG-1.1 detected\n");
intercept_inch = 0xFA8B;
echo_flag_addr = 0xFF53;
exbug_detected = 1;
}
else if (!memcmp(&mem[0xFA6B], "\xb6\xfc\xf4\x47", 4))
{
printf(" EXBUG-1.2 detected\n");
intercept_inch = 0xFA6B;
echo_flag_addr = 0xFF53;
exbug_detected = 1;
}
#ifdef M6809
else if (!memcmp(&mem[0xF0D2], "\xb6\xfc\xf4\x47", 4))
{
printf(" EXBUG09-2.1 detected\n");
intercept_inch = 0xF0D2;
echo_flag_addr = 0xFF58;
exbug_detected = 1;
}
#endif
return 0;
}
void close_drive(int n)
{
if (drive[n].f) {
fclose(drive[n].f);
}
drive[n].f = 0;
}
void show_drive(int n)
{
if (drive[n].f) {
printf("%s mounted as drive %d\n", drive[n].name, n);
} else {
printf("No disk in drive %d\n", n);
}
}
void set_drive(int n, const char *name)
{
drive[n].name = name;
}
int load_drive(int n)
{
FILE *f;
long t;
f = fopen(drive[n].name, "r+");
if (!f) {
fprintf(stderr, "Couldn't open '%s'\n", drive[n].name);
return -1;
}
fseek(f, 0, SEEK_END);
t = ftell(f);
if (swtpc) {
if (t == 256 * 10 * 35) {
drive[n].bytes = 256;
drive[n].sects = 10;
drive[n].tracks = 35;
drive[n].f = f;
printf("'%s' opened for drive %d (tracks=%d sectors=%d)\n", drive[n].name, n, drive[cur_drive].tracks, drive[cur_drive].sects);
} else if (t == 256 * 10 * 40) {
drive[n].bytes = 256;
drive[n].sects = 10;
drive[n].tracks = 40;
drive[n].f = f;
printf("'%s' opened for drive %d (tracks=%d sectors=%d)\n", drive[n].name, n, drive[n].tracks, drive[n].sects);
} else if (t == 256 * 20 * 35) {
drive[n].bytes = 256;
drive[n].sects = 20;
drive[n].tracks = 35;
drive[n].f = f;
printf("'%s' opened for drive %d (tracks=%d sectors=%d)\n", drive[n].name, n, drive[n].tracks, drive[n].sects);
} else if (t == 256 * 20 * 40) {
drive[n].bytes = 256;
drive[n].sects = 20;
drive[n].tracks = 40;
drive[n].f = f;
printf("'%s' opened for drive %d (tracks=%d sectors=%d)\n", drive[n].name, n, drive[n].tracks, drive[n].sects);
} else if (t == 256 * 18 * 80) {
drive[n].bytes = 256;
drive[n].sects = 18;
drive[n].tracks = 80;
drive[n].f = f;
printf("'%s' opened for drive %d (tracks=%d sectors=%d)\n", drive[n].name, n, drive[n].tracks, drive[n].sects);
} else if (t == 256 * 20 * 80) {
drive[n].bytes = 256;
drive[n].sects = 20;
drive[n].tracks = 80;
drive[n].f = f;
printf("'%s' opened for drive %d (tracks=%d sectors=%d)\n", drive[n].name, n, drive[n].tracks, drive[n].sects);
} else if (t == 256 * 36 * 80) {
drive[n].bytes = 256;
drive[n].sects = 36;
drive[n].tracks = 80;
drive[n].f = f;
printf("'%s' opened for drive %d (tracks=%d sectors=%d)\n", drive[n].name, n, drive[n].tracks, drive[n].sects);
} else if (t == 256 * 72 * 80) {
drive[n].bytes = 256;
drive[n].sects = 72;
drive[n].tracks = 80;
drive[n].f = f;
printf("'%s' opened for drive %d (tracks=%d sectors=%d)\n", drive[n].name, n, drive[n].tracks, drive[n].sects);
} else {
fclose(f);
printf("'%s' is not a valid disk\n", drive[n].name);
return -1;
}
} else {
if (t == 128 * 26 * 77) {
printf("'%s' opened for drive %d (single sided)\n", drive[n].name, n);
drive[n].f = f;
drive[n].bytes = 128;
drive[n].tracks = 77;
drive[n].sects = 26;
} else if (t == 128 * 26 * 77 * 2) {
printf("'%s' opened for drive %d (double sided)\n", drive[n].name, n);
drive[n].f = f;
drive[n].bytes = 128;
drive[n].tracks = 77 * 2;
drive[n].sects = 26;
} else if (t == 128 * 40 * 16) {
printf("'%s' opened for drive %d (single sided minifloppy)\n", drive[n].name, n);
drive[n].f = f;