-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz80.c
1473 lines (1299 loc) · 34.5 KB
/
z80.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
/* Emulation of the Z80 CPU with hooks into the other parts of z81.
* Copyright (C) 1994 Ian Collier.
* z81 changes (C) 1995-2001 Russell Marks.
* sz81 further development (C) 2023-2024 Ian Jordan
*
* 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.
*/
#include <string.h> /* for memset/memcpy */
#include <stdbool.h>
#include <stdio.h>
#include "common.h"
#include "sound.h"
#include "sdl_sound.h"
#include "z80.h"
#include "sdl.h"
#include "loadp.h"
#define RUN_ROM 2
#define LASTINSTNONE 0
#define LASTINSTINFE 1
#define LASTINSTOUTFE 2
#define LASTINSTOUTFD 3
#define LASTINSTOUTFF 4
// Horizontal line timings
#define HLENGTH 207 // TStates in horizontal scanline
// TV Emulation
#define SCAN50 310 // Number of scanline per frame at 50Hz
#define SCAN60 262 // Number of scanline per frame at 60Hz
#define HSCAN (2*HLENGTH)
#define HTOL 30 // Tolerance in detection of horizontal sync
#define VTOL 10 // Tolerance in detection of VSYNC
#define VMIN 170
#define HMIN 8
#define HMAX 32
/* ZX80 specific state */
#define SYNCNONE 0
#define SYNCTYPEH 1
#define SYNCTYPEV 2
#define parity(a) (partable[a])
/* Function declarations */
extern int printer_inout(int is_out, int val);
static void setRemainingDisplayBoundaries(void);
static inline void checkhsync(int tolchk);
static inline void checkvsync(int tolchk);
static inline void checksync(int inc);
static void anyout(void);
static void vsync_raise(void);
static void vsync_lower(void);
static inline int z80_interrupt(void);
static inline int nmi_interrupt(void);
static unsigned long z80_op(void);
static void adjustChroma(bool start);
static void setEmulatedTV(bool fiftyHz, uint16_t vtol);
static void zx80_loop(void);
static void zx81_loop(void);
#ifdef LOAD_AND_SAVE
static void loadAndSaveROM(void);
#endif
/* state variables to be preserved in a snap shot*/
/* ZX81 and display state */
unsigned long tstates = 0, frames = 0;
int framewait = 0;
int vsx = 0;
int vsy = 0;
int RasterX = 0;
int RasterY = 0;
int S_RasterX = 0;
int S_RasterY = 0;
int nmi_pending, hsync_pending;
int NMI_generator;
int VSYNC_state, HSYNC_state, SYNC_signal;
int psync, sync_len;
int rowcounter = 0;
int hsync_counter = 0;
int VSYNC_TOLERANCEMIN = SCAN50 - VTOL;
int VSYNC_TOLERANCEMAX = SCAN50 + VTOL;
int FRAME_SCAN = SCAN50;
bool rowcounter_hold = false;
bool running_rom = false;
bool frameNotSync = true;
/* Z80 state */
unsigned char a, f, b, c, d, e, h, l;
unsigned char r, a1, f1, b1, c1, d1, e1, h1, l1, i, iff1, iff2, im;
unsigned short pc;
unsigned short ix, iy, sp;
unsigned char radjust;
unsigned char ixoriy, new_ixoriy;
unsigned char intsample=0;
unsigned char op;
unsigned short m1cycles;
/* ZX80 state variables */
int scanlineCounter = 0;
int videoFlipFlop1Q = 1;
int videoFlipFlop2Q = 0;
int videoFlipFlop3Q = 0;
int videoFlipFlop3Clear = 0;
int prevVideoFlipFlop3Q = 0;
int lineClockCarryCounter = 0;
int scanline_len = 0;
int sync_type = SYNCNONE;
int nosync_lines = 0;
bool vsyncFound = false;
/* Variables to be recreated after loading state through call to setDisplayBoundaries() */
static int dest;
static int adjustStartX=0;
static int adjustStartY=0;
static int startX = 0;
static int startY = 0;
static int syncX = 0;
static int endX = 0;
static int endY = 0;
/* variables that do not need to be preserved */
/* Display area */
static unsigned char scrnbmp_new_base[((DISPLAY_F_WIDTH >> 3) + DISPLAY_PADDING) * DISPLAY_F_HEIGHT]; /* written */
static unsigned char scrnbmp_base[((DISPLAY_F_WIDTH >> 3) + DISPLAY_PADDING) * DISPLAY_F_HEIGHT]; /* displayed */
static unsigned char *const scrnbmp_new = scrnbmp_new_base + DISPLAY_PADDING;
unsigned char *const scrnbmp = scrnbmp_base + DISPLAY_PADDING;
/* chroma */
static unsigned char scrnbmpc_new_base[((DISPLAY_F_WIDTH >> 3) + DISPLAY_PADDING) * DISPLAY_F_HEIGHT];/* written */
static unsigned char scrnbmpc_base[((DISPLAY_F_WIDTH >> 3) + DISPLAY_PADDING) * DISPLAY_F_HEIGHT]; /* displayed */
static unsigned char *const scrnbmpc_new = scrnbmpc_new_base + DISPLAY_PADDING;
unsigned char *const scrnbmpc = scrnbmpc_base + DISPLAY_PADDING;
static unsigned long ts=0;
int ay_reg = 0;
int LastInstruction;
/* Constants */
static const unsigned char partable[256] = {
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
0, 4, 4, 0, 4, 0, 0, 4, 4, 0, 0, 4, 0, 4, 4, 0,
4, 0, 0, 4, 0, 4, 4, 0, 0, 4, 4, 0, 4, 0, 0, 4
};
const unsigned long tsmax = 65000;
static const int HSYNC_TOLERANCEMIN = HSCAN - HTOL;
static const int HSYNC_TOLERANCEMAX = HSCAN + HTOL;
static const int HSYNC_MINLEN = HMIN;
static const int HSYNC_MAXLEN = HMAX;
static const int VSYNC_MINLEN = VMIN;
static const int HSYNC_START = 16;
static const int HSYNC_END = 32;
static const int HLEN = HLENGTH;
static const int MAX_JMP = 8;
/* The following are defined as the RiscOS gcc compiler fails if they are static const */
#define scanlinePixelLength (HLENGTH << 1)
#define ZX80HSyncDuration 20
#define ZX80HSyncAcceptanceDuration ((3 * ZX80HSyncDuration) / 2)
#define ZX80HSyncAcceptanceDurationPixels (ZX80HSyncAcceptanceDuration * 2)
#define ZX80MaximumSupportedScanlineOverhang (ZX80HSyncDuration * 2)
#define ZX80MaximumSupportedScanlineOverhangPixels (ZX80MaximumSupportedScanlineOverhang * 2)
static const int PortActiveDuration = 3;
static const int ZX80HSyncAcceptancePixelPosition = scanlinePixelLength - ZX80HSyncAcceptanceDurationPixels;
static const int scanlineThresholdPixelLength = scanlinePixelLength + ZX80MaximumSupportedScanlineOverhangPixels;
void setEmulatedTV(bool fiftyHz, uint16_t vtol)
{
// This can look confusing as we have an emulated display, and a real
// display, both can be at either 50 or 60 Hz
if (fiftyHz)
{
VSYNC_TOLERANCEMIN = SCAN50 - vtol;
VSYNC_TOLERANCEMAX = SCAN50 + vtol;
FRAME_SCAN = SCAN50;
}
else
{
VSYNC_TOLERANCEMIN = SCAN60 - vtol;
VSYNC_TOLERANCEMAX = SCAN60 + vtol;
FRAME_SCAN = SCAN60;
}
}
void setDisplayBoundaries(void)
{
adjustStartX = (zx80 && (!fullDisplay)) ? DISPLAY_ZX80_OFF : 0;
if (centreScreen)
{
if (!(fullDisplay || fiveSevenSix))
{
adjustStartX = DISPLAY_N_PIXEL_OFF + (zx80 ? DISPLAY_ZX80_OFF : 0);
adjustStartY = (useNTSC) ? (DISPLAY_N_START_Y >> 1) : -(DISPLAY_N_START_Y >> 1);
}
}
setRemainingDisplayBoundaries();
}
static void setRemainingDisplayBoundaries(void)
{
startX = disp.start_x - adjustStartX - 6;
syncX = disp.start_x - adjustStartX;
startY = disp.start_y - adjustStartY;
endX = disp.end_x - adjustStartX;
endY = disp.end_y - adjustStartY;
}
/* Ensure that chroma and pixels are byte aligned */
static void adjustChroma(bool start)
{
if (start)
{
if (!adjustStartX)
{
if (fullDisplay)
{
adjustStartX = DISPLAY_F_PIXEL_OFF;
}
else if (fiveSevenSix)
{
adjustStartX = DISPLAY_P_PIXEL_OFF;
}
else
{
adjustStartX = DISPLAY_N_PIXEL_OFF;
}
}
setRemainingDisplayBoundaries();
}
else
{
setDisplayBoundaries();
}
}
#ifdef LOAD_AND_SAVE
static void loadAndSaveROM(void)
{
if (!running_rom)
{
if (pc == rom_patches.load.start) // load
{
int run_rom = sdl_load_file(de, (de < 0x8000) ? LOAD_FILE_METHOD_NAMEDLOAD : LOAD_FILE_METHOD_SELECTLOAD);
if ((!rom_patches.load.use_rom) || (run_rom != RUN_ROM))
{
pc = rom_patches.rstrtAddr;
op = fetchm(pc);
}
}
else if (pc == rom_patches.save.start) // save
{
int run_rom = sdl_save_file(hl, SAVE_FILE_METHOD_NAMEDSAVE);
if ((!rom_patches.save.use_rom) || (run_rom != RUN_ROM))
{
pc = rom_patches.rstrtAddr;
op = fetchm(pc);
}
}
}
else
{
if (pc == rom_patches.retAddr)
{
running_rom = false;
}
}
}
#endif
static void vsync_raise(void)
{
/* save current pos - in screen coords*/
vsx = RasterX - syncX + (ts << 1);
vsy = RasterY - startY;
// move to next valid pixel
if (vsx >= disp.width)
{
vsx = 0;
vsy++;
}
else if (vsx < 0)
{
vsx = 0;
}
if ((vsy < 0) || (vsy >= disp.height))
{
vsx = 0;
vsy = 0;
}
}
/* for vsync on -> off */
static void vsync_lower(void)
{
int nx = RasterX - syncX + (ts << 1);
int ny = RasterY - startY;
// Move to the next valid pixel
if (nx >= disp.width)
{
nx = 0;
ny++;
}
else if (nx < 0)
{
nx = 0;
}
if ((ny < 0) || (ny >= disp.height))
{
nx = 0;
ny = 0;
}
// leave if start and end are same pixel
if ((nx == vsx) && (ny == vsy)) return;
// Determine if there is a frame wrap
if((ny < vsy) || ((ny == vsy) && (nx < vsx)))
{
// wrapping around frame, so display bottom
uint8_t* start = scrnbmp_new+vsy*disp.stride_byte+(vsx>>3)-1;
*start++ = (0xff >> (vsx & 0x7));
memset(start, 0xff, disp.stride_byte*(disp.height-vsy)-(vsx>>3)-1);
// check for case where wrap ends at bottom
if ((nx == 0) && (ny == 0)) return;
// Fall through to display top half
vsx = 0;
vsy = 0;
}
uint8_t* start = scrnbmp_new+vsy*disp.stride_byte+(vsx>>3)-1;
uint8_t* end = scrnbmp_new+ny*disp.stride_byte+(nx>>3);
*start++ = (0xff >> (vsx & 0x7));
// end bits?
if (nx & 0x7)
{
*end = (0xff << (nx & 0x7));
}
// Note: End equalling start is not unusual after adjusting positions to be on screen
// especially when displaying the loading screen
if (end > start)
{
memset(start, 0xff, end-start);
}
}
void mainloop()
{
intsample = 0;
framewait = 0;
a = f = b = c = d = e = h = l = a1 = f1 = b1 = c1 = d1 = e1 = h1 = l1 = i = iff1 = iff2 = im = r = 0;
ixoriy = new_ixoriy = 0;
ix = iy = sp = pc = 0;
tstates = radjust = 0;
RasterX = 0;
RasterY = 0;
dest = disp.offset + (adjustStartY * disp.stride_bit) + adjustStartX;
psync = 1;
sync_len = 0;
/* ULA */
NMI_generator = 0;
nmi_pending = 0;
rowcounter = 0;
hsync_pending = 0;
VSYNC_state = HSYNC_state = 0;
frames = 0;
hsync_counter = 0;
setEmulatedTV(!useNTSC, vertTol);
if (sdl_emulator.autoload)
{
sdl_emulator.autoload = 0;
/* This could be an initial autoload or a later forcedload */
if (!sdl_load_file(0, LOAD_FILE_METHOD_DETECT))
/* wait for a real frame, to avoid an annoying frame `jump'. */
framewait = 1;
}
zx80 ? zx80_loop() : zx81_loop();
}
void zx81_loop(void)
{
while (1)
{
if(nmi_pending)
{
ts = nmi_interrupt();
tstates += ts;
nmi_pending = 0;
}
else if (iff1 && intsample && !((radjust - 1) & 0x40))
{
ts = z80_interrupt();
hsync_counter = -2; /* INT ACK after two tstates */
hsync_pending = 1; /* a HSYNC may be started */
tstates += ts;
}
else
{
// Get the next op, calculate the next byte to display and execute the op
op = fetchm(pc);
// After this instruction can have interrupt
intsample = 1;
if (((pc & 0x8000) && (!m1not || (pc & 0x4000)) && !(op & 0x40)))
{
if ((RasterX >= startX) &&
(RasterX < endX) &&
(RasterY >= startY) &&
(RasterY < endY))
{
unsigned char v;
int addr;
if ((i < 0x20) || ((i < 0x40) && LowRAM && (!useWRX)))
{
if (!(chr128 && (i > 0x20) && (i & 1)))
addr = ((i & 0xfe) << 8) | ((op & 0x3f) << 3) | rowcounter;
else
addr = ((i & 0xfe) << 8) | ((((op & 0x80) >> 1) | (op & 0x3f)) << 3) | rowcounter;
if (UDGEnabled && (addr >= 0x1E00) && (addr < 0x2000))
{
v = mem[addr + ((op & 0x80) ? 0x6800 : 0x6600)];
}
else
{
v = mem[addr];
}
}
else if (useWRX)
{
v = mem[(i << 8) | (r & 0x80) | (radjust & 0x7f)];
}
else
{
v = 0xff;
}
v = (op & 0x80) ? ~v : v;
if (chromamode)
{
int k = (dest + RasterX) >> 3;
scrnbmpc_new[k] = (chromamode & 0x10) ? fetch(pc) : fetch(0xc000 | ((((op & 0x80) >> 1) | (op & 0x3f)) << 3) | rowcounter);
scrnbmp_new[k] = v;
}
else
{
int k = dest + RasterX;
int kh = k >> 3;
int kl = k & 7;
if (kl)
{
scrnbmp_new[kh++] |= (v >> kl);
scrnbmp_new[kh] = (v << (8 - kl));
}
else
{
scrnbmp_new[kh] = v;
}
}
}
/* The CPU sees a nop - so skip the Z80 emulation loop */
pc++;
radjust++;
ts = 4;
tstates += ts;
}
else
{
ts = z80_op();
switch(LastInstruction)
{
case LASTINSTOUTFD:
NMI_generator = 0;
anyout();
break;
case LASTINSTOUTFE:
NMI_generator = 1;
anyout();
break;
case LASTINSTINFE:
if (!NMI_generator)
{
if (VSYNC_state == 0)
{
VSYNC_state = 1;
vsync_raise();
}
}
LastInstruction = LASTINSTNONE;
break;
case LASTINSTOUTFF:
anyout();
break;
}
}
}
// Determine changes to sync state
int states_remaining = ts;
int since_hstart = 0;
int tswait = 0;
int tstate_inc;
do
{
tstate_inc = states_remaining > MAX_JMP ? MAX_JMP: states_remaining;
states_remaining -= tstate_inc;
hsync_counter+=tstate_inc;
RasterX += (tstate_inc<<1);
if (hsync_counter >= HLEN)
{
hsync_counter -= HLEN;
hsync_pending = 1;
}
// Start of HSYNC, and NMI if enabled
if ((hsync_pending == 1) && (hsync_counter >= HSYNC_START))
{
if (NMI_generator)
{
nmi_pending = 1;
if (ts==4)
{
tswait = 14 + (3 - states_remaining - (hsync_counter - HSYNC_START));
}
else
{
tswait = 14;
}
states_remaining += tswait;
ts += tswait;
tstates += tswait;
}
HSYNC_state = 1;
since_hstart = hsync_counter - HSYNC_START + 1;
if (VSYNC_state || rowcounter_hold)
{
rowcounter = 0;
rowcounter_hold = false;
}
else
{
rowcounter++;
rowcounter &= 7;
}
hsync_pending = 2;
}
// end of HSYNC
if ((hsync_pending == 2) && (hsync_counter >= HSYNC_END))
{
HSYNC_state = 0;
hsync_pending = 0;
}
// NOR the vertical and horizontal SYNC states to create the SYNC signal
SYNC_signal = (VSYNC_state || HSYNC_state) ? 0 : 1;
checksync(since_hstart ? since_hstart : MAX_JMP);
since_hstart = 0;
}
while (states_remaining);
if (tstates >= tsmax)
{
tstates -= tsmax;
frames++;
frame_pause();
}
/* this isn't used for any sort of Z80 interrupts,
* purely for the emulator's UI.
*/
if (interrupted)
{
if (interrupted == 1)
{
do_interrupt(); /* also zeroes it */
}
/* I've added these new interrupt types to support a thorough
* emulator reset and to do a proper exit i.e. back to main */
else if (interrupted == INTERRUPT_EMULATOR_RESET ||
interrupted == INTERRUPT_EMULATOR_EXIT)
{
return;
}
else /* must be 2 */
{
/* a kludge to let us do a reset */
interrupted = 0;
a = f = b = c = d = e = h = l = a1 = f1 = b1 = c1 = d1 = e1 = h1 = l1 = i = iff1 = iff2 = im = r = 0;
ixoriy = new_ixoriy = 0;
ix = iy = sp = pc = 0;
tstates = radjust = 0;
RasterX = 0;
RasterY = 0;
dest = disp.offset + (adjustStartY * disp.stride_bit) + adjustStartX;
psync = 1;
sync_len = 0;
/* ULA */
NMI_generator = 0;
hsync_pending = 0;
VSYNC_state = HSYNC_state = 0;
}
}
}
}
void zx80_loop(void)
{
unsigned long ts; // deliberately hides static ts, so vsync at correct raster
while (1)
{
// Get the next op, calculate the next byte to display and execute the op
op = fetchm(pc);
intsample = 1;
m1cycles = 1;
if (((pc & 0x8000) && (!m1not || (pc & 0x4000)) && !(op & 0x40)))
{
if ((RasterX >= startX) &&
(RasterX < endX) &&
(RasterY >= startY) &&
(RasterY < endY))
{
unsigned char v;
int addr;
if ((i < 0x20) || ((i < 0x40) && LowRAM && (!useWRX)))
{
if (!(chr128 && (i > 0x20) && (i & 1)))
addr = ((i & 0xfe) << 8) | ((op & 0x3f) << 3) | rowcounter;
else
addr = ((i & 0xfe) << 8) | ((((op & 0x80) >> 1) | (op & 0x3f)) << 3) | rowcounter;
if (UDGEnabled && (addr >= 0x1E00) && (addr < 0x2000))
{
v = mem[addr + ((op & 0x80) ? 0x6800 : 0x6600)];
}
else
{
v = mem[addr];
}
}
else if (useWRX)
{
v = mem[(i << 8) | (r & 0x80) | (radjust & 0x7f)];
}
else
{
v = 0xff;
}
v = (op & 0x80) ? ~v : v;
if (chromamode)
{
int k = (dest + RasterX) >> 3;
scrnbmpc_new[k] = (chromamode & 0x10) ? fetch(pc) : fetch(0xc000 | ((((op & 0x80) >> 1) | (op & 0x3f)) << 3) | rowcounter);
scrnbmp_new[k] = v;
}
else
{
int k = dest + RasterX;
int kh = k >> 3;
int kl = k & 7;
if (kl)
{
scrnbmp_new[kh++] |= (v >> kl);
scrnbmp_new[kh] = (v << (8 - kl));
}
else
{
scrnbmp_new[kh] = v;
}
}
}
/* The CPU sees a nop - so skip the Z80 emulation loop */
pc++;
radjust++;
ts = 4;
tstates += ts;
// Update the flip flop
prevVideoFlipFlop3Q = videoFlipFlop3Q;
if (videoFlipFlop3Clear)
{
videoFlipFlop3Q = videoFlipFlop2Q;
}
videoFlipFlop2Q = !videoFlipFlop1Q;
}
else
{
ts = z80_op();
// Update the flip flop
prevVideoFlipFlop3Q = videoFlipFlop3Q;
for (int i = 0; i < m1cycles; ++i)
{
if (videoFlipFlop3Clear)
{
videoFlipFlop3Q = videoFlipFlop2Q;
}
videoFlipFlop2Q = !videoFlipFlop1Q;
}
}
if (!videoFlipFlop3Q)
{
videoFlipFlop1Q = 0;
if (prevVideoFlipFlop3Q)
{
rowcounter = (rowcounter + 1) & 7;
}
}
// execute an interrupt
if (iff1 && intsample && !((radjust - 1) & 0x40))
{
unsigned long tstore = z80_interrupt();
tstates += tstore;
ts += tstore;
// single m1Cycle
if (videoFlipFlop3Clear)
{
videoFlipFlop3Q = videoFlipFlop2Q;
}
videoFlipFlop2Q = !videoFlipFlop1Q;
videoFlipFlop1Q = 1;
}
RasterX += (ts << 1);
scanline_len += (ts << 1);
if (RasterX >= scanlinePixelLength)
{
RasterX -= scanlinePixelLength;
RasterY++;
}
switch (LastInstruction)
{
case LASTINSTOUTFD:
case LASTINSTOUTFE:
case LASTINSTOUTFF: // VSync end
videoFlipFlop1Q = 0;
videoFlipFlop2Q = 1;
videoFlipFlop3Clear = 1;
if (!videoFlipFlop3Q)
{
sync_len += ts;
if (sync_len > ZX80HSyncAcceptanceDuration)
{
videoFlipFlop3Q = 1;
}
}
LastInstruction = LASTINSTNONE;
break;
case LASTINSTINFE: // VSync start
if (videoFlipFlop3Q)
{
sync_len = PortActiveDuration;
}
else
{
sync_len += ts;
}
videoFlipFlop1Q = 1;
videoFlipFlop3Clear = 0;
videoFlipFlop3Q = 0;
rowcounter = 0;
LastInstruction = LASTINSTNONE;
break;
default:
if (!videoFlipFlop3Q)
{
sync_len += ts;
}
break;
}
if (prevVideoFlipFlop3Q != videoFlipFlop3Q)
{
videoFlipFlop3Q ? vsync_lower() : vsync_raise();
// ZX80 HSYNC sound - excluded if Chroma
#ifdef OSS_SOUND_SUPPORT
if ((sdl_sound.device == DEVICE_VSYNC) && frameNotSync)
{
sound_beeper(videoFlipFlop3Q);
}
#endif
}
if (videoFlipFlop3Q && (sync_len > 0))
{
// The line is now complete
if (sync_len <= ZX80HSyncAcceptanceDuration)
{
sync_type = SYNCTYPEH;
if (scanline_len >= ZX80HSyncAcceptancePixelPosition)
{
lineClockCarryCounter = ts;
scanline_len = scanlinePixelLength;
}
}
else
{
int overhangPixels = scanline_len - scanlinePixelLength;
sync_type = SYNCTYPEV;
if (overhangPixels < 0)
{
if (scanline_len >= ZX80HSyncAcceptancePixelPosition)
{
lineClockCarryCounter = 0;
}
else
{
lineClockCarryCounter = scanline_len > 1;
}
scanline_len = scanlinePixelLength;
}
else if (overhangPixels > 0)
{
lineClockCarryCounter = overhangPixels > 1;
scanline_len = scanlinePixelLength;
}
}
}
// If we are at the end of a line then process it
if (!((scanline_len < scanlineThresholdPixelLength) && (sync_type == SYNCNONE)))
{
if (sync_type == SYNCTYPEV)
{
// Frames synchonised after second vsync in range
if (vsyncFound)
{
#ifdef DEBUG_SYNC
if (!found)
{
printf("T %i\n", ++count);
found = true;
}
#endif
frameNotSync = !((RasterY >= VSYNC_TOLERANCEMIN) && (RasterY < VSYNC_TOLERANCEMAX) &&
(scanlineCounter >= VSYNC_TOLERANCEMIN) && (scanlineCounter < VSYNC_TOLERANCEMAX));
vsyncFound = !frameNotSync;
}
else
{
#ifdef DEBUG_SYNC
if (found)
{
printf("F %i\n", count);
found = false;
}
#endif
vsyncFound = (scanlineCounter >= VSYNC_TOLERANCEMIN) && (scanlineCounter < VSYNC_TOLERANCEMAX);
}
scanlineCounter = 0;
if (!vsyncFound)
{
sync_type = SYNCNONE;
sync_len = 0;
}
nosync_lines = 0;
}
else
{
if (sync_type == SYNCTYPEH)
{
scanlineCounter++;
}
if (((sync_type == SYNCNONE) && videoFlipFlop3Q) || (scanlineCounter >= VSYNC_TOLERANCEMAX))
{
frameNotSync = true;
vsyncFound = false;
}
if (sync_type == SYNCNONE)
{
int overhangPixels = scanline_len - scanlinePixelLength;
if (overhangPixels > 0)
{
lineClockCarryCounter = (overhangPixels >> 1);
scanline_len = scanlinePixelLength;
}
nosync_lines++;
}
else
{
nosync_lines = 0;
}
}
// Synchronise the TV position
S_RasterX += scanline_len;
if (S_RasterX >= scanlinePixelLength)
{
S_RasterX -= scanlinePixelLength;
S_RasterY++;
if (S_RasterY >= VSYNC_TOLERANCEMAX)
{
S_RasterX = 0;
sync_type = SYNCTYPEV;
if (sync_len < HSYNC_MINLEN)
{
sync_len = HSYNC_MINLEN;
}
}