-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzx81.c
2495 lines (2155 loc) · 60 KB
/
zx81.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
#include "zx81.h"
#include "sdl.h"
#include "sdl_loadsave.h"
#include "sdl_sound.h"
#include "config.h"
#include "zx81config.h"
#include "common.h"
#include "sound.h"
#include "z80/z80.h"
#include "w5100.h"
#include "types.h"
#include "dissz80.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#define LASTINSTNONE 0
#define LASTINSTINFE 1
#define LASTINSTOUTFE 2
#define LASTINSTOUTFD 3
#define LASTINSTOUTFF 4
#ifdef ZXMORE
#define WAITMOD
#endif
#ifdef ZXNU
#ifndef VDRIVE
#define VDRIVE
#endif
#endif
// #define VRCNTR
ZX81 zx81;
BYTE *memory;
extern BYTE *sz81mem;
extern int rwsz81mem;
/* odd place to have this, but the display does work in an odd way :-) */
unsigned char scrnbmp_new[ZX_VID_FULLWIDTH*ZX_VID_FULLHEIGHT/8]; /* written */
unsigned char scrnbmp[ZX_VID_FULLWIDTH*ZX_VID_FULLHEIGHT/8]; /* displayed */
unsigned char scrnbmp_old[ZX_VID_FULLWIDTH*ZX_VID_FULLHEIGHT/8]; /* checked against for diffs */
/* chroma */
unsigned char scrnbmpc_new[ZX_VID_FULLWIDTH*ZX_VID_FULLHEIGHT]; /* written */
unsigned char scrnbmpc[ZX_VID_FULLWIDTH*ZX_VID_FULLHEIGHT]; /* displayed */
static int RasterX = 0;
static int RasterY = 0;
static int TVP;
static int dest;
/* TV specifications */
#define HTOLMIN 414-30
#define HTOLMAX 414+30
#define VTOLMIN 310-100
#define VTOLMAX 310+100
#define HMIN 8
#define HMAX 32
#define VMIN 170
static int HSYNC_TOLERANCEMIN = HTOLMIN;
static int HSYNC_TOLERANCEMAX = HTOLMAX;
static int VSYNC_TOLERANCEMIN = VTOLMIN;
static int VSYNC_TOLERANCEMAX = VTOLMAX;
static int HSYNC_MINLEN = HMIN;
static int HSYNC_MAXLEN = HMAX;
static int VSYNC_MINLEN = VMIN;
int int_pending, nmi_pending, hsync_pending;
long noise;
int border, ink, paper;
int SelectAYReg;
BYTE font[512];
BYTE memhrg[1024];
int borrow=0;
unsigned long tstates=0;
unsigned long tsmax=0;
unsigned long frames=0;
/* I/O port 1 allows reading of the zx81 structure */
int configbyte=0;
int NMI_generator;
int VSYNC_state, HSYNC_state, SYNC_signal;
int psync, sync_len;
int setborder=0;
int LastInstruction;
int MemotechMode=0;
int shift_register, shift_reg_inv;
int rowcounter=0;
int zx81_stop=0;
int hsync_counter=0;
int ispeedup;
int ffetch;
/* ZXmore stuff */
int zxmspeed=1;
int zxmoff=0;
int zxmnmicounter=0;
int zxmnmi=0;
int zxmfetching=0;
int zxmroml=0x3f, zxmraml=0xf3;
int zxmvideo=0;
int zxmrowcounter=0;
unsigned int zxmoffset=0;
int zxmflash=0, zxmmod=0;
#ifdef ZXNU
int zxnu_latch=0;
#endif
extern void loadrombank(int offset);
#ifdef SNDPRC
extern int sp_on;
extern void sp_start(int idx);
#endif
#ifdef ZXPAND
extern void ZXpand_Update(int millis);
extern void ZXpand_IO_Write(int addr, int data);
extern int ZXpand_IO_ReadStatus();
extern int ZXpand_IO_Read(int addr);
#endif
#ifdef APU
void am_write_io8(uint8_t addr, uint8_t data);
uint8_t am_read_io8(uint8_t addr);
#endif
#ifdef ZXMORE
int zxmAddress(int Address, int rd)
{
int romsize, swap, offset;
romsize = 0x1000 + ((zxmraml & 0x0f) << 12);
swap = zxmraml & 0x10;
if ((swap && Address>=romsize) || (!swap && Address<romsize)) {
/* ram access */
// if (zxmvideo) Address &= 0x7fff; /* more or less what happened with early CPLDs */
if ((!zxmnmi || !rd) && (zxmroml&0x40)) Address |= 0x8000;
#ifdef ZXMSHMEM
if ((zxmnmi && rd) || (Address>=0x2000 && Address<0x4000)) offset = 0x70000;
#else
if (zxmnmi && rd) offset = 0x70000;
#endif
else offset = (zxmraml & 0xe0) << 11;
offset += 0x80000;
} else {
/* rom access */
if (zxmnmi) offset = 0x70000;
else offset = (zxmroml & 0x07) << 16;
}
return Address + offset;
}
void zxm_writebyte(int Address, int Data)
{
int iAddress, ib, ie;
iAddress = zxmAddress(Address,0);
if (iAddress>=0x80000) {
memory[iAddress] = Data;
return;
}
if (zxmroml&0x08) return;
/* modifications are saved in exitmem() in common.c */
switch (zxmflash) {
case 0: if (Address==0x5555 && Data==0xaa) zxmflash++;
break;
case 1: if (Address==0x2aaa && Data==0x55) zxmflash++; else zxmflash=0;
break;
case 2: if (Address==0x5555) zxmflash=Data; else zxmflash=0;
break;
case 0x80: if (Address==0x5555 && Data==0xaa) zxmflash++; else zxmflash=0;
break;
case 0x81: if (Address==0x2aaa && Data==0x55) zxmflash++; else zxmflash=0;
break;
case 0x82: ib = iAddress & 0xff000;
ie = ib | 0x00fff;
while (ib<=ie) memory[ib++] = 0xff;
// fprintf(stdout,"SERS %04x: %05x %02x %02x\n", z80.pc.w, iAddress, Data, zxmflash);
zxmflash = 0;
zxmmod = 1;
break;
case 0xa0: memory[iAddress] = Data;
// fprintf(stdout,"BPRG %04x: %05x %02x %02x\n", z80.pc.w, iAddress, Data, zxmflash);
zxmflash = 0;
zxmmod = 1;
break;
default: zxmflash = 0;
}
}
BYTE zxm_readbyte(int Address)
{
int iAddress;
int romsize;
romsize = 0x1000 + ((zxmraml & 0x0f) << 12);
if (!zxmfetching && zxmnmi && Address>=romsize && (zxmraml&0xe0)!=0xe0) {
zxmnmi = 0;
}
iAddress = zxmAddress(Address,1);
return memory[iAddress];
}
#else
void zxm_writebyte(int Address, int Data)
{
memory[Address] = Data;
}
BYTE zxm_readbyte(int Address)
{
return memory[Address];
}
#endif
/* in common.c */
void aszmic4hacks();
void aszmic7hacks();
void kcomm(int a);
unsigned char lcomm(int a1, int a2);
void disassemble(const unsigned int dAddr, const BYTE opcode)
{
DISZ80 *d; /* Pointer to the Disassembly structure */
int err;
/* Allocate the dZ80 structure */
d = (DISZ80*) malloc(sizeof(DISZ80));
if (d == NULL)
{
printf("dz80: cannot allocate %ld bytes\n", sizeof(DISZ80));
exit(1);
}
/* Set up dZ80's structure - it's not too fussy */
memset(d, 0, sizeof(DISZ80));
/* Set the default radix and strings (comments and "db") */
dZ80_SetDefaultOptions(d);
/* Set the CPU type */
d->cpuType = DCPU_Z80;
/* Set the start of the Z80's memory space - not used */
d->mem0Start = NULL;
/* Indicate we're disassembling a single instruction */
d->flags |= DISFLAG_SINGLE;
/* Set the disassembly address */
d->start = d->end = dAddr;
/* :-) */
d->op = opcode;
d->availop = 1;
err = dZ80_Disassemble(d);
if (err != DERR_NONE)
{
printf("**** dZ80 error: %s\n", dZ80_GetErrorText(err));
}
/* Display the disassembled line, using the hex dump and disassembly buffers in the DISZ80 structure */
#ifdef ZXMORE
printf(" %6ld %02X %02X %d %d %04X %04X %04X %04X %04X %04X %04X %04X%10s: %s\n",
tstates, zxmroml, zxmraml, zxmnmi, zxmvideo, dAddr,
z80.af.w, z80.bc.w, z80.de.w, z80.hl.w, z80.sp.w, z80.ix.w, z80.iy.w,
d->hexDisBuf, d->disBuf);
#else
printf(" %6ld %04X %04X %04X %04X %04X %04X %10s: %s\n",
tstates, dAddr,
z80.af.w, z80.bc.w, z80.de.w, z80.hl.w, z80.sp.w,
d->hexDisBuf, d->disBuf);
#endif
free(d);
}
/* EightyOne - A Windows ZX80/81/clone emulator.
* Copyright (C) 2003-2006 Michael D Wynne
*
* 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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* zx81.c
*
*/
/* Used to be in AccDraw */
void Plot(int c)
{
int k, kh, kl;
unsigned char b, m;
k = TVP + dest + RasterX;
RasterX++;
if (RasterX >= ZX_VID_FULLWIDTH) return;
if (k >= ZX_VID_FULLWIDTH*ZX_VID_FULLHEIGHT) return;
kh = k >> 3;
kl = k & 7;
m = 0x80 >> kl;
b = scrnbmp_new[kh];
if (c&0x01) b |= m; else b &= ~m;
if (zx81.colour!=COLOURDISABLED) scrnbmpc_new[k] = c >> 4;
scrnbmp_new[kh] = b;
}
int myrandom( int x )
{
return rand() % ( x + 1 );
}
BYTE zx81_readbyte(int Address);
void zx81_writebyte(int Address, int Data);
void h4th_store(int n)
{
FILE *f;
char filnam[10];
int blkloc, i, j;
unsigned char data;
sprintf(filnam,"%05d.4th", n);
blkloc = (zx81_readbyte(0xfc77) << 8) | zx81_readbyte(0xfc76);
fprintf(stderr,"STOREing %s...\n",filnam);
f = fopen(filnam,"w");
for (j=0; j<16; j++) {
for (i=0; i<32; i++) {
data = zx81_readbyte(blkloc++);
data = (data&0x80) + 32 + (data&0x7f);
fprintf(f,"%c", data);
}
blkloc++;
fprintf(f,"\n");
}
fclose(f);
}
int h4th_load(int n)
{
FILE *f;
char filnam[10];
int blkloc, blkend, i, inv;
unsigned char data;
sprintf(filnam,"%05d.4th", n);
blkloc = (zx81_readbyte(0xfc77) << 8) | zx81_readbyte(0xfc76);
blkend = blkloc + 16*32;
fprintf(stderr,"LOADing %s...\n",filnam);
while (blkloc<blkend) zx81_writebyte(blkloc++,0);
blkloc = blkend - 16*32;
f = fopen(filnam,"r");
if (!f) return 0;
i = 0;
while (blkloc<blkend) {
data = fgetc(f);
if (feof(f)) break;
inv = data&0x80;
data &= 0x7f;
if (data < 0x20) {
while (i&0x1f) { blkloc++; i++; }
i = 0;
} else {
if (data>=0x60) data -= 0x40; else data -= 0x20;
if (inv) data |= 0x80;
zx81_writebyte(blkloc++,data);
i++;
}
}
fclose(f);
return 1;
}
/* zx97 code has been removed */
/* In sz81, the extended instructions were called within edops.c;
to keep the current Philip Kendall's z80_ed.c clean, the instructions are
tested here in line with EightyOne */
/* TODO: zxmoffset ;-) */
int PatchTest(int pc)
{
int byte, edbyte, offset=0;
#ifdef ZXMORE
offset = (zxmroml & 0x07) << 16;
if (offset==0x70000) return(pc);
#endif
byte = memory[pc+offset];
if (byte==0xed) {
edbyte = memory[pc+offset+1];
#ifdef ZXMORE
offset += 0x80000;
if (zxmroml&0x40) offset += 0x8000;
#endif
switch (edbyte) {
case 0xfa: if (pc==0x0692 || pc==0x0693) {
kcomm(z80.hl.w);
return(pc+2);
}
if (pc==0x157b) {
h4th_store(z80.hl.w);
return(pc+2);
}
break;
case 0xfb: if (pc==0x06c5 || pc==0x06c6) {
z80.af.b.h=lcomm(z80.af.b.h,z80.hl.w);
return(pc+2);
}
if (pc==0x1611) {
if (h4th_load(z80.hl.w)) return(pc+0x7b);
return(pc+2);
}
break;
case 0xfc: if (pc==0x348 && zx81.machine==MACHINEZX81) {
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset += 0x8000;
#endif
if (z80.hl.w < 0x8000)
sdl_load_file(z80.hl.w+offset,LOAD_FILE_METHOD_NAMEDLOAD);
else
sdl_load_file(z80.hl.w+offset,LOAD_FILE_METHOD_SELECTLOAD);
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset -= 0x8000;
#endif
return(z80.pc.w);
}
if (zx81.machine==MACHINEZX80 && pc==0x206) {
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset += 0x8000;
#endif
sdl_load_file(z80.hl.w+offset,LOAD_FILE_METHOD_SELECTLOAD);
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset -= 0x8000;
#endif
return(pc+2);
}
if (pc==0x19f0 && zx81.machine==MACHINELAMBDA) {
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset += 0x8000;
#endif
if (z80.hl.w < 0x8000)
sdl_load_file(z80.hl.w+offset,LOAD_FILE_METHOD_NAMEDLOAD);
else
sdl_load_file(z80.hl.w+offset,LOAD_FILE_METHOD_SELECTLOAD);
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset -= 0x8000;
#endif
return(z80.pc.w);
}
break;
case 0xfd: if (zx81.machine==MACHINEZX81 && pc==0x02fc) {
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset += 0x8000;
#endif
sdl_save_file(z80.hl.w+offset,SAVE_FILE_METHOD_NAMEDSAVE);
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset -= 0x8000;
#endif
return(pc+2);
}
if (zx81.machine==MACHINEZX80 && pc==0x01b6) {
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset += 0x8000;
#endif
sdl_save_file(z80.hl.w+offset,SAVE_FILE_METHOD_UNNAMEDSAVE);
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset -= 0x8000;
#endif
return(pc+2);
}
if (zx81.machine==MACHINELAMBDA && pc==0x0d0d) {
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset += 0x8000;
#endif
sdl_save_file(z80.hl.w+offset,SAVE_FILE_METHOD_UNNAMEDSAVE);
#ifdef ZXMORE
if (zxmroml&0x40) zxmoffset -= 0x8000;
#endif
return(pc+2);
}
break;
default : ;
}
}
if (zx81.single_step) {
printf("PC: %04X OP: %02X SP: %04X\n", pc, byte, z80.sp.w);
printf("AF: %02X %02X\n", z80.af.b.h, z80.af.b.l);
printf("BC: %04X DE: %04X HL: %04X\n", z80.bc.w, z80.de.w, z80.hl.w);
printf("\n");
}
return(pc);
}
int zx81_contend(int Address, int states, int time)
{
return(time);
}
void zx81_writebyte(int Address, int Data)
{
#ifdef VISUALS
if (Address>=0x8000 && !(Data&64) && !NMI_generator) {
shift_register |= Data;
}
#endif
noise = (noise<<8) | Data;
if (zx81.aytype == AY_TYPE_QUICKSILVA)
{
if (Address == 0x7fff) SelectAYReg=Data&15;
if (Address == 0x7ffe) sound_ay_write(SelectAYReg,Data,0);
}
// The lambda colour board has 1k of RAM mapped between 8k-16k (8 shadows)
// with a further 8 shadows between 49152 and 57344.
if (zx81.colour==COLOURLAMBDA && ((Address>=8192 && Address<16384)
|| (Address>=49152 && Address<57344)))
{
if ((Address&0x3000) == 0x3000) {
printf("Write Lambda colour RAM (0x%04X:0x%02X)\n", Address, Data);
return;
}
Address = (Address&1023)+8192;
zxm_writebyte(Address,Data);
return;
}
if (zx81.chrgen==CHRGENQS && Address>=0x8400 && Address<=0x87ff)
{
font[Address-0x8400]=Data;
zx81.enableqschrgen=1;
}
if (Address>zx81.RAMTOP) {
if (Address==0x8003) {
if (Data==0x80 && sdl_aszmicrom.state) {
memcpy(mem,sdl_aszmicrom.data,4096);
printf("Going to ASZMIC version %d...\n",mem[0x16]);
if (mem[0x16]==0x07) aszmic7hacks(); else aszmic4hacks();
memcpy(mem+4096,mem,4096);
} else if (Data==0x0f) {
printf("Going to BASIC...\n");
memcpy(mem,sdl_zx81rom.data,8192);
}
}
if (zx81.RAMTOP==0xbfff) Address &= 0x7fff;
else Address = (Address&(zx81.RAMTOP));
}
if (Address<=zx81.ROMTOP && zx81.protectROM)
{
if ((zx81.truehires==HIRESMEMOTECH) && (Address<1024))
memhrg[Address]=Data;
#ifdef ZXMORE
// possibly flash programming
zxm_writebyte(Address,Data);
#endif
return;
}
#ifdef FNMI
/* 50 notes music interface" by Gaetano Marano, implementation by Stefano Bodrato */
if (Address>8191 && Address<16384) sound_fnmi(Data);
#endif
if (Address>8191 && Address<16384 && zx81.shadowROM && zx81.protectROM) return;
if (Address<10240 && zx81.truehires==HIRESMEMOTECH) return;
if (Address>=10240 && Address<12288 && zx81.truehires==HIRESG007) return;
if (zx81.wsz81mem && Address>=0x4000 && Address<0x8000) sz81mem[Address-0x4000]=Data;
zxm_writebyte(Address,Data);
}
BYTE zx81_readbyte(int Address)
{
int data;
// The lambda colour board has 1k of RAM mapped between 8k-16k (8 shadows)
// with a further 8 shadows between 49152 and 57344.
if (zx81.colour==COLOURLAMBDA && ((Address>=8192 && Address<16384)
|| (Address>=49152 && Address<57344)))
{
if ((Address&0x3000) == 0x3000) {
data=zxm_readbyte(Address);
printf("Read Lambda colour RAM (0x%04X:0x%02X)\n", Address, data);
} else {
Address = (Address&1023)+8192;
data=zxm_readbyte(Address);
}
return(data);
}
if (zx81.rsz81mem && Address>=0xc000) return sz81mem[Address-0xc000];
if (Address<=zx81.RAMTOP) data=zxm_readbyte(Address);
// else data=memory[(Address&(zx81.RAMTOP-16384))+16384];
else if (zx81.RAMTOP==0xbfff) data=zxm_readbyte(Address&0x7fff);
else data=zxm_readbyte(Address&zx81.RAMTOP);
if ((Address<1024 && (zx81.truehires==HIRESMEMOTECH)) && (z80.i&1))
data=memhrg[Address];
if ((Address>=0x0c00 && Address<=0x0cff) && (zx81.truehires==HIRESG007))
data=zxm_readbyte(Address+8192);
if ((Address<256 || (Address>=512 && Address<768))
&& (z80.i&1) && (zx81.truehires==HIRESG007))
data=zxm_readbyte(Address+8192);
#ifdef VISUALS
if (Address>=0x8000 && !(data&64) && !NMI_generator) {
shift_register |= data;
}
#endif
noise = (noise<<8) | data;
return(data);
}
// BYTE opcode_fetch(int Address)
//
// Given an address, opcode fetch return the byte at that memory address,
// modified depending on certain circumstances.
// It also loads the video shift register and generates video noise.
//
// If Address is less than M1NOT, all code is executed,
// the shift register is cleared and video noise is set to what is on
// the data bus.
//
// If Address >= M1NOT, and bit 6 of the fetched opcode is not set
// a NOP is returned and we load the shift register accordingly,
// depending on which video system is in use (WRX/Memotech/etc.)
//
// The ZX81 has effectively two busses. The ROM is on the first bus
// while (usually) RAM is on the second. In video generation, the ROM
// bus is used to get character bitmap data while the second bus
// is used to get the display file. This is important because depending
// on which bus RAM is placed, it can either be used for extended
// Fonts OR WRX style hi-res graphics, but never both.
BYTE zx81_opcode_fetch_org(int Address)
{
#ifdef ZXPAND
static int calls = 0;
#endif
int inv;
int opcode, bit6, update=0;
BYTE data;
#ifndef ZXMORE
BYTE data2;
#endif
#ifdef ZXPAND
// very rough timing here;
// assuming a 1mhz call rate it will be 1ms every 1000 calls.
// it may be too rough, with "in" and "jp" instructions;
// what is the unit of the delay in zxpand/zxpandcom.h"?
// ms is too much; for now microseconds are assumed.
// 10 calls may be 30 us.
++calls;
if (calls == 10)
{
calls = 0;
ZXpand_Update(1);
}
#endif
#ifdef ZXMORE
if (!zxmvideo || Address<zx81.m1not)
#else
if (Address<zx81.m1not)
#endif
{
// This is not video related, so just return the opcode
// and make some noise onscreen.
zxmfetching = 1;
data = zxm_readbyte(Address);
zxmfetching = 0;
#ifdef ZXMORE
if (data==0xc9) zxmnmi = 0;
#endif
noise |= data;
return(data);
}
// We can only execute code below M1NOT. If an opcode fetch occurs
// above M1NOT, we actually fetch (address&32767). This is important
// because it makes it impossible to place the display file in the
// 48-64k region if a 64k RAM Pack is used. How does the real
// Hardware work?
#ifdef ZXMORE
data = zxm_readbyte((Address>=zx81.m1not)?Address&32767:Address);
#else
data = zxm_readbyte((Address>=49152)?Address&32767:Address);
#endif
opcode=data;
bit6=opcode&64;
// Since we got here, we're generating video (ouch!)
// Bit six of the opcode is important. If set, the opcode
// gets executed and nothing appears onscreen. If unset
// the Z80 executes a NOP and the code is used to somehow
// generate the TV picture (exactly how depends on which
// display method is used)
if (!bit6) opcode=0;
inv = data&128;
// First check for WRX graphics. This is easy, we just create a
// 16 bit Address from the IR Register pair and fetch that byte
// loading it into the video shift register.
if (z80.i>=zx81.maxireg && zx81.truehires==HIRESWRX && !bit6)
{
if (zx81.colour==COLOURCHROMA)
{
#ifdef ZXMORE
ink = 0;
paper = 15;
#else
int c;
// If the Chroma 81 interface is enabled, we had better fetch
// the ink and paper colour from memory too.
c = zxm_readbyte(Address);
ink = c&15;
paper = (c>>4) & 15;
#endif
}
data=zxm_readbyte((z80.i<<8) | (z80.r7 & 128) | ((z80.r-1) & 127));
update=1;
}
else if ((z80.i&1) && MemotechMode)
{
// Next Check Memotech Hi-res. Memotech is only enabled
// When the I register is odd.
// TODO: RasterX<66 check
if ((opcode!=118 || RasterX<(13+58-2)*2) && RasterY>=56 && RasterY<=(56+192))
{
opcode=0;
inv=(MemotechMode==3);
update=1;
}
}
else if ((z80.i&1) && (zx81.truehires==HIRESG007))
{
// Like Memotech, G007 is enabled when I is odd.
// However, it is much simpler, in that it disables
// the bit 6 detection entirely and relies on the R
// register to generate an interupt at the right time.
opcode=0;
inv=0;
update=1;
}
else if (!bit6)
{
// If we get here, we're generating normal Characters
// (or pseudo Hi-Res), but we still need to figure out
// where to get the bitmap for the character from
if (zx81.colour==COLOURCHROMA)
{
#ifdef ZXMORE
ink = 0;
paper = 15;
#else
int c;
// If the Chroma 81 interface is enabled, we had better fetch
// the ink and paper colour from memory too.
if (zx81.chromamode&0x10) { // Attribute file
c=zxm_readbyte(Address);
} else { // Character code
data2 = ((data&0x80)>>1) | (data&0x3f);
c=zxm_readbyte(0xc000 + (data2<<3) + rowcounter);
}
ink = c&15;
paper = (c>>4) & 15;
#endif
}
// First try to figure out which character set we're going
// to use if CHR$x16 is in use. Else, standard ZX81
// character sets are only 64 characters in size.
if ((zx81.chrgen==CHRGENCHR16 && (z80.i&1))
|| (zx81.chrgen==CHRGENQS && zx81.enableqschrgen))
data = ((data&128)>>1)|(data&63);
#if ZXMORE
else if (z80.i&1) data = ((data&128)>>1)|(data&63);
#endif
else data = data&63;
// If I points to ROM, OR I points to the 8-16k region for
// CHR$x16, we'll fetch the bitmap from there.
// Lambda and the QS Character board have external memory
// where the character set is stored, so if one of those
// is enabled we better fetch it from the dedicated
// external memory.
// Otherwise, we can't get a bitmap from anywhere, so
// display 11111111 (??What does a real ZX81 do?).
if (z80.i<64 || (z80.i>=128 && z80.i<192 && zx81.chrgen==CHRGENCHR16))
{
if ((zx81.extfont && z80.i<32) || (zx81.chrgen==CHRGENQS && zx81.enableqschrgen))
data=font[(data<<3) + rowcounter];
else data=zxm_readbyte((((z80.i&254)<<8) + (data<<3)) + rowcounter);
}
else data=255;
update=1;
}
if (update)
{
// Update gets set to true if we managed to fetch a bitmap from
// somewhere. The only time this doesn't happen is if we encountered
// an opcode with bit 6 set above M1NOT.
if (zx81.colour==COLOURLAMBDA)
{
int c;
// If Lambda colour is enabled, we had better fetch
// the ink and paper colour from memory too.
c = zxm_readbyte(8192+(Address&1023)+1);
ink = c&15;
paper = (c>>4) & 15;
if (setborder) // TODO
{
border=0;
setborder=0;
}
}
else if (zx81.colour==COLOURCHROMA)
{
if (setborder)
{
border=zx81.chromamode & 15;
setborder=0;
}
}
// Finally load the bitmap we retrieved into the video shift
// register, remembering to make some video noise too.
shift_register |= data;
shift_reg_inv |= inv? 255:0;
if (zx81.machine==MACHINELAMBDA) noise |= (Address>>8);
else noise |= z80.i;
return(0);
}
else
{
// This is the fallthrough for when we found an opcode with
// bit 6 set in the display file. We actually execute these
// opcodes, and generate the noise.
if (zx81.colour==COLOURLAMBDA)
{
ink = paper = 0;
}
if (zx81.colour==COLOURCHROMA)
{
ink = paper = zx81.chromamode & 15;
}
noise |= data;
return(opcode);
}
}
BYTE zx81_opcode_fetch(int Address)
{
BYTE opcode;
opcode = zx81_opcode_fetch_org(Address);
#if 0
if ((opcode==0xcd || (opcode&0xc7) == 0xc4) && Address>0x08cf ) // one of the nine calls
disassemble(Address, opcode);
#endif
if (Address>=sdl_emulator.bdis && Address<=sdl_emulator.edis && ffetch)
disassemble(Address, opcode);
ffetch = 0;
return opcode;
}
#ifdef ZXMORE
BYTE zxmusbb[0x10000];
int zxmusbr=0, zxmusbrl=0;
int zxmusbw=0, zxmusbwf=0, zxmusbwl=0;
int zxmusbc=0, zxmusbs=0;
FILE *zxmusbf;
#define ZXMUSBCS 1018
void load_usb(int reset)
{
static int stage, total, parts;
int n, n1, n2, n3, n4, i;
struct stat buf;
zxmusbr = zxmusbrl = 0;
#ifdef DEBUGUSB
fprintf(stdout,"load_usb %d %d\n", reset, stage);
#endif
if (reset) {
stage = 1;
return;
}
i = 0;
switch (stage) {
case 1: zxmusbb[18]=0;
printf("Opening file %s...\n", zxmusbb+6);
zxmusbf = fopen((const char*)(zxmusbb+6),"rb");
if (zxmusbf) {
fstat(fileno(zxmusbf),&buf);
total = buf.st_size;
} else {
total = -1;
}
n1 = (total & 0x000000ff);
n2 = (total & 0x0000ff00) >> 8;