-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.c
2218 lines (2005 loc) · 59.7 KB
/
custom.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
/*
* UAE - The Un*x Amiga Emulator
*
* Custom chip emulation
*
* (c) 1995 Bernd Schmidt, Alessandro Bissacco
*/
#include <ctype.h>
#ifdef __unix
#include <sys/time.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "config.h"
#include "amiga.h"
#include "options.h"
#include "events.h"
#include "memory.h"
#include "custom.h"
#include "cia.h"
#include "disk.h"
#include "blit.h"
#include "xwin.h"
#include "os.h"
#define DMA_AUD0 0x0001
#define DMA_AUD1 0x0002
#define DMA_AUD2 0x0004
#define DMA_AUD3 0x0008
#define DMA_DISK 0x0010
#define DMA_SPRITE 0x0020
#define DMA_BLITTER 0x0040
#define DMA_COPPER 0x0080
#define DMA_BITPLANE 0x0100
#define DMA_BLITPRI 0x0400
/* These are default values for mouse emulation.
* The first two are default values for mstepx and mstepy.
* The second line set the orizontal and vertical offset for amiga and X
* pointer matching
*/
#define defstepx (1<<16)
#define defstepy (1<<16)
#ifndef __mac__
#define defxoffs 0
#define defyoffs 0
#else
#define defxoffs 210
#define defyoffs 85
#endif
/* Values below define mouse auto calibration process.
* They are not critical, change them if you want.
* The most important is calweight, which sets mouse adjustement rate */
static const int docal = 60, xcaloff = 40, ycaloff = 20;
static const int calweight = 3;
static int lastsampledmx, lastsampledmy;
/*
* Events
*/
unsigned long int cycles, nextevent, nextev_count, specialflags;
int vpos;
UWORD lof;
struct ev eventtab[ev_max];
bool copper_active;
static const int maxhpos = 227,maxvpos = 312;
static const int dskdelay = 2; /* FIXME: ??? */
static const int minfirstline = 29;
/*
* hardware register values that are visible/can be written to by someone
*/
static UWORD cregs[256];
static UWORD dmacon,intena,intreq,adkcon;
static ULONG cop1lc,cop2lc,copcon;
static CPTR bpl1pt,bpl2pt,bpl3pt,bpl4pt,bpl5pt,bpl6pt;
static ULONG bpl1dat,bpl2dat,bpl3dat,bpl4dat,bpl5dat,bpl6dat;
static UWORD bplcon0,bplcon1,bplcon2,bplcon3;
static UWORD diwstrt,diwstop,ddfstrt,ddfstop;
static WORD bpl1mod,bpl2mod;
static UWORD sprdata[8], sprdatb[8], sprctl[8], sprpos[8];
static ULONG sprpt[8];
static bool sprarmed[8], sprptset[8];
/* Kludge. FIXME: How does sprite restart after vsync work? */
static int spron[8];
static UWORD bltadat,bltbdat,bltcdat,bltddat,bltafwm,bltalwm,bltsize;
static WORD bltamod,bltbmod,bltcmod,bltdmod;
static UWORD bltcon0,bltcon1;
static ULONG bltapt,bltbpt,bltcpt,bltdpt,bltcnxlpt,bltdnxlpt;
static ULONG dskpt;
static UWORD dsklen,dsksync;
static int joy0x, joy1x, joy0y, joy1y;
static int lastspr0x,lastspr0y,lastdiffx,lastdiffy,spr0pos,spr0ctl;
static int mstepx,mstepy,xoffs=defxoffs,yoffs=defyoffs;
static int sprvbfl;
static enum { normal_mouse, dont_care_mouse, follow_mouse} mousestate;
xcolnr acolors[64];
UWORD color_regs[32];
/*
* "hidden" hardware registers
*/
static int dblpf_ind1[64], dblpf_ind2[64];
static ULONG coplc;
static UWORD copi1,copi2;
static enum {
COP_stop, COP_read, COP_wait, COP_move, COP_skip
} copstate;
static UWORD vblitsize,hblitsize,blitpreva,blitprevb;
static UWORD blitlpos,blitashift,blitbshift,blinea,blineb;
static bool blitline,blitfc,blitzero,blitfill,blitife,blitdesc,blitsing;
static bool blitonedot,blitsign;
static long int bltwait;
static int dsklength;
static enum {
BLT_done, BLT_init, BLT_read, BLT_work, BLT_write, BLT_next
} bltstate;
static int plffirstline,plflastline,plfstrt,plfstop,plflinelen;
static int diwfirstword,diwlastword;
static int plf1pri, plf2pri;
int dskdmaen; /* used in cia.c */
static UWORD dskmfm,dskbyte,dsktime;
static bool dsksynced;
static int bpldelay1;
static int bpldelay2;
static bool bplhires;
static int bplplanecnt;
static bool pfield_fullline,pfield_linedone;
static bool pfield_linedmaon;
static int pfield_lastpart_hpos;
unsigned char apixels[1000]; /* includes a lot of safety padding */
char spixels[1000]; /* for sprites */
char spixstate[1000]; /* more sprites */
/*
* Statistics
*/
static unsigned long int msecs = 0, frametime = 0, timeframes = 0;
static unsigned long int seconds_base;
bool bogusframe;
/*
* helper functions
*/
static void pfield_doline_slow(int);
static void pfield_doline(void);
static void pfield_hsync(int);
bool inhibit_frame;
static int framecnt = 0;
static __inline__ void count_frame(void)
{
if (inhibit_frame)
framecnt = 1;
else {
framecnt++;
if (framecnt == framerate)
framecnt = 0;
}
}
static __inline__ void setclr(UWORD *p, UWORD val)
{
if (val & 0x8000) {
*p |= val & 0x7FFF;
} else {
*p &= ~val;
}
}
bool dmaen(UWORD dmamask)
{
return (dmamask & dmacon) && (dmacon & 0x200);
}
static __inline__ int current_hpos(void)
{
return cycles - eventtab[ev_hsync].oldcycles;
}
static void calcdiw(void)
{
diwfirstword = (diwstrt & 0xFF) * 2 - 0x60;
diwlastword = (diwstop & 0xFF) * 2 + 0x200 - 0x60;
if (diwfirstword < 0) diwfirstword = 0;
plffirstline = diwstrt >> 8;
plflastline = diwstop >> 8;
#if 0
/* This happens far too often. */
if (plffirstline < minfirstline) {
fprintf(stderr, "Warning: Playfield begins before line %d!\n", minfirstline);
plffirstline = minfirstline;
}
#endif
if ((plflastline & 0x80) == 0) plflastline |= 0x100;
#if 0 /* Turrican does this */
if (plflastline > 313) {
fprintf(stderr, "Warning: Playfield out of range!\n");
plflastline = 313;
}
#endif
plfstrt = ddfstrt;
plfstop = ddfstop;
if (plfstrt < 0x18) plfstrt = 0x18;
if (plfstop > 0xD8) plfstop = 0xD8;
if (plfstrt > plfstop) plfstrt = plfstop;
/*
* Prize question: What are the next lines supposed to be?
* I can't seem to get it right.
*/
#if 0
/* Pretty good guess, but wrong. */
plflinelen = (plfstop-plfstrt+15) & ~7;
plfstrt &= ~(bplhires ? 3 : 7);
plfstop &= ~(bplhires ? 3 : 7);
#endif
plfstrt &= ~(bplhires ? 3 : 3);
plfstop &= ~(bplhires ? 3 : 3);
plflinelen = (plfstop-plfstrt+15) & ~7;
}
static void pfield_may_need_update(bool colreg)
{
int i;
/* Ignore, if this happened before or after the DDF window */
if (framecnt != 0 || !pfield_linedmaon || current_hpos() <= plfstrt
|| vpos < plffirstline || vpos >= plflastline)
{
return;
}
/*
* If a color reg was modified, it is only important if we are within
* the DIW.
*/
if (current_hpos() <= (diwfirstword+0x60)/4 && colreg)
return;
/*
* If we are past the DDF window, me might as well draw the complete
* line now.
*/
if (current_hpos() > plfstrt + plflinelen && pfield_fullline) {
if (!pfield_linedone)
pfield_doline();
pfield_linedone = true;
return;
}
if (pfield_fullline) {
pfield_lastpart_hpos = 0;
memset(apixels, 0, sizeof(apixels));
pfield_fullline = false;
} else {
assert(pfield_lastpart_hpos <= current_hpos());
}
for (i = pfield_lastpart_hpos; i < current_hpos(); i++) {
pfield_doline_slow(i);
}
pfield_lastpart_hpos = current_hpos();
}
/* Apparently, the DMA bit is tested by the hardware at some point,
* presumably at the ddfstart position, to determine whether it
* ought to draw the line.
* This is probably not completely correct, but should not matter
* very much.
*/
static void pfield_calclinedma(void)
{
if (current_hpos() >= plfstrt)
return;
pfield_linedmaon = dmaen(DMA_BITPLANE);
}
/*
* register functions
*/
static UWORD DMACONR(void)
{
return (dmacon | (bltstate==BLT_done ? 0 : 0x4000)
| (blitzero ? 0x2000 : 0));
}
static UWORD INTENAR(void) { return intena; }
static UWORD INTREQR(void) { return intreq; }
static UWORD ADKCONR(void) { return adkcon; }
static UWORD VPOSR(void) { return (vpos >> 8) | lof; }
static void VPOSW(UWORD v) { lof = v & 0x8000; }
static UWORD VHPOSR(void) { return (vpos << 8) | current_hpos(); }
static void COP1LCH(UWORD v) { cop1lc= (cop1lc & 0xffff) | ((ULONG)v << 16); }
static void COP1LCL(UWORD v) { cop1lc= (cop1lc & ~0xffff) | v; }
static void COP2LCH(UWORD v) { cop2lc= (cop2lc & 0xffff) | ((ULONG)v << 16); }
static void COP2LCL(UWORD v) { cop2lc= (cop2lc & ~0xffff) | v; }
static void COPJMP1(UWORD a)
{
coplc = cop1lc; copstate = COP_read;
eventtab[ev_copper].active = 1; eventtab[ev_copper].oldcycles = cycles;
eventtab[ev_copper].evtime = 4; events_schedule();
copper_active = true;
}
static void COPJMP2(UWORD a)
{
coplc = cop2lc; copstate = COP_read;
eventtab[ev_copper].active = 1; eventtab[ev_copper].oldcycles = cycles;
eventtab[ev_copper].evtime = 4; events_schedule();
copper_active = true;
}
static void DMACON(UWORD v)
{
UWORD oldcon = dmacon;
setclr(&dmacon,v); dmacon &= 0x1FFF;
pfield_calclinedma();
if ((dmacon & DMA_COPPER) > (oldcon & DMA_COPPER)) {
COPJMP1(0); /* @@@ not sure how this works */
}
if (copper_active && !eventtab[ev_copper].active) {
eventtab[ev_copper].active = true;
eventtab[ev_copper].oldcycles = cycles;
eventtab[ev_copper].evtime = 1;
events_schedule();
}
}
static void INTENA(UWORD v) { setclr(&intena,v); specialflags |= SPCFLAG_INT; }
static void INTREQ(UWORD v) { setclr(&intreq,v); specialflags |= SPCFLAG_INT; }
static void ADKCON(UWORD v) { setclr(&adkcon,v); }
static void BPL1PTH(UWORD v) { bpl1pt= (bpl1pt & 0xffff) | ((ULONG)v << 16); }
static void BPL1PTL(UWORD v) { bpl1pt= (bpl1pt & ~0xffff) | v; }
static void BPL2PTH(UWORD v) { bpl2pt= (bpl2pt & 0xffff) | ((ULONG)v << 16); }
static void BPL2PTL(UWORD v) { bpl2pt= (bpl2pt & ~0xffff) | v; }
static void BPL3PTH(UWORD v) { bpl3pt= (bpl3pt & 0xffff) | ((ULONG)v << 16); }
static void BPL3PTL(UWORD v) { bpl3pt= (bpl3pt & ~0xffff) | v; }
static void BPL4PTH(UWORD v) { bpl4pt= (bpl4pt & 0xffff) | ((ULONG)v << 16); }
static void BPL4PTL(UWORD v) { bpl4pt= (bpl4pt & ~0xffff) | v; }
static void BPL5PTH(UWORD v) { bpl5pt= (bpl5pt & 0xffff) | ((ULONG)v << 16); }
static void BPL5PTL(UWORD v) { bpl5pt= (bpl5pt & ~0xffff) | v; }
static void BPL6PTH(UWORD v) { bpl6pt= (bpl6pt & 0xffff) | ((ULONG)v << 16); }
static void BPL6PTL(UWORD v) { bpl6pt= (bpl6pt & ~0xffff) | v; }
/*
* I've seen the listing of an example program that changes
* from lo- to hires while a line is being drawn. That's
* awful, but we want to emulate it.
*/
static void BPLCON0(UWORD v)
{
pfield_may_need_update(false);
bplcon0 = v;
bplhires = (bplcon0 & 0x8000) == 0x8000;
bplplanecnt = (bplcon0 & 0x7000) >> 12;
calcdiw(); /* This should go away. */
}
static void BPLCON1(UWORD v)
{
pfield_may_need_update(false);
bplcon1 = v;
bpldelay1 = bplcon1 & 0xF;
bpldelay2 = (bplcon1 >> 4) & 0xF;
}
static void BPLCON2(UWORD v)
{
pfield_may_need_update(false);
bplcon2 = v;
plf1pri = 1 << 2*(v & 7);
plf2pri = 1 << 2*((v>>3) & 7);
}
static void BPLCON3(UWORD v) { pfield_may_need_update(false); bplcon3 = v; }
static void BPL1MOD(UWORD v) { pfield_may_need_update(false); bpl1mod = v; }
static void BPL2MOD(UWORD v) { pfield_may_need_update(false); bpl2mod = v; }
static void BPL1DAT(UWORD v) { bpl1dat = v; }
static void BPL2DAT(UWORD v) { bpl2dat = v; }
static void BPL3DAT(UWORD v) { bpl3dat = v; }
static void BPL4DAT(UWORD v) { bpl4dat = v; }
static void BPL5DAT(UWORD v) { bpl5dat = v; }
static void BPL6DAT(UWORD v) { bpl6dat = v; }
/* We call pfield_may_need_update() from here. Actually,
* I have no idea what happens if someone changes ddf or
* diw mid-line, and I don't really want to know. I doubt
* that this sort of thing was ever used to create a
* useful effect.
*/
static void DIWSTRT(UWORD v) { pfield_may_need_update(false); diwstrt = v; calcdiw(); }
static void DIWSTOP(UWORD v) { pfield_may_need_update(false); diwstop = v; calcdiw(); }
static void DDFSTRT(UWORD v) { pfield_may_need_update(false); ddfstrt = v; calcdiw(); }
static void DDFSTOP(UWORD v) { pfield_may_need_update(false); ddfstop = v; calcdiw(); }
static void BLTADAT(UWORD v) { bltadat = v; }
static void BLTBDAT(UWORD v) { bltbdat = v; }
static void BLTCDAT(UWORD v) { bltcdat = v; }
static void BLTAMOD(UWORD v) { bltamod = v; }
static void BLTBMOD(UWORD v) { bltbmod = v; }
static void BLTCMOD(UWORD v) { bltcmod = v; }
static void BLTDMOD(UWORD v) { bltdmod = v; }
static void BLTCON0(UWORD v) { bltcon0 = v; }
static void BLTCON1(UWORD v) { bltcon1 = v; }
static void BLTAFWM(UWORD v) { bltafwm = v; }
static void BLTALWM(UWORD v) { bltalwm = v; }
static void BLTAPTH(UWORD v) { bltapt= (bltapt & 0xffff) | ((ULONG)v << 16); }
static void BLTAPTL(UWORD v) { bltapt= (bltapt & ~0xffff) | (v); }
static void BLTBPTH(UWORD v) { bltbpt= (bltbpt & 0xffff) | ((ULONG)v << 16); }
static void BLTBPTL(UWORD v) { bltbpt= (bltbpt & ~0xffff) | (v); }
static void BLTCPTH(UWORD v) { bltcpt= (bltcpt & 0xffff) | ((ULONG)v << 16); }
static void BLTCPTL(UWORD v) { bltcpt= (bltcpt & ~0xffff) | (v); }
static void BLTDPTH(UWORD v) { bltdpt= (bltdpt & 0xffff) | ((ULONG)v << 16); }
static void BLTDPTL(UWORD v) { bltdpt= (bltdpt & ~0xffff) | (v); }
static void BLTSIZE(UWORD v) { bltsize = v; bltstate = BLT_init; specialflags |= SPCFLAG_BLIT; }
static void SPRxCTL(UWORD v, int num) { pfield_may_need_update(false); sprctl[num] = v; sprarmed[num] = false; spron[num] |= 2; }
static void SPRxPOS(UWORD v, int num) { pfield_may_need_update(false); sprpos[num] = v; }
static void SPRxDATA(UWORD v, int num) { pfield_may_need_update(false); sprdata[num] = v; sprarmed[num] = true; }
static void SPRxDATB(UWORD v, int num) { pfield_may_need_update(false); sprdatb[num] = v; }
static void SPRxPTH(UWORD v, int num) { sprpt[num] &= 0xffff; sprpt[num] |= (ULONG)v << 16; spron[num] |= 1; }
static void SPRxPTL(UWORD v, int num) { sprpt[num] &= ~0xffff; sprpt[num] |= v; spron[num] |= 1; }
static void COLOR(UWORD v, int num)
{
pfield_may_need_update(true);
color_regs[num] = v;
acolors[num] = xcolors[v];
acolors[num+32] = xcolors[(v >> 1) & 0x777];
}
static void DSKSYNC(UWORD v) { dsksync = v; }
static void DSKDAT(UWORD v) { dskmfm = v; }
static void DSKPTH(UWORD v) { dskpt = (dskpt & 0xffff) | ((ULONG)v << 16); }
static void DSKPTL(UWORD v) { dskpt = (dskpt & ~0xffff) | (v); }
static void DSKLEN(UWORD v)
{
if (v & 0x8000) { dskdmaen++; } else { dskdmaen = 0; }
dsktime = dskdelay; dsksynced = false;
dsklen = dsklength = v; dsklength &= 0x3fff;
if (dskdmaen == 2 && dsksync != 0x4489 && (adkcon & 0x400)) {
printf("Non-standard sync: %04x len: %x\n", dsksync, dsklength);
}
if (dsklen & 0x4000) DISK_InitWrite();
if (dskdmaen) specialflags |= SPCFLAG_DISK;
}
static UWORD DSKBYTR(void)
{
UWORD v = (dsklen >> 1) & 0x6000;
v |= dskbyte;
dskbyte &= ~0x8000;
if (dsksync == dskmfm) v |= 0x1000;
return v;
}
static UWORD DSKDATR(void) { return dskmfm; }
static UWORD POTGOR(void)
{
if (!buttonstate[2]) {
return 0xffff;
} else {
return 0xbbff;
}
}
static UWORD JOY0DAT(void) { return joy0x + (joy0y << 8); }
static UWORD JOY1DAT(void)
{
#ifdef HAVE_JOYSTICK
UWORD dir;
bool button;
read_joystick(&dir, &button);
buttonstate[1] = button;
return dir;
#else
return joy1x + (joy1y << 8);
#endif
}
static void JOYTEST(UWORD v)
{
joy0x = joy1x = v & 0xFC;
joy0y = joy1y = (v >> 8) & 0xFC;
}
static void AUD0LCH(UWORD v) { audlc[0] = (audlc[0] & 0xffff) | ((ULONG)v << 16); }
static void AUD0LCL(UWORD v) { audlc[0] = (audlc[0] & ~0xffff) | v; }
static void AUD1LCH(UWORD v) { audlc[1] = (audlc[1] & 0xffff) | ((ULONG)v << 16); }
static void AUD1LCL(UWORD v) { audlc[1] = (audlc[1] & ~0xffff) | v; }
static void AUD2LCH(UWORD v) { audlc[2] = (audlc[2] & 0xffff) | ((ULONG)v << 16); }
static void AUD2LCL(UWORD v) { audlc[2] = (audlc[2] & ~0xffff) | v; }
static void AUD3LCH(UWORD v) { audlc[3] = (audlc[3] & 0xffff) | ((ULONG)v << 16); }
static void AUD3LCL(UWORD v) { audlc[3] = (audlc[3] & ~0xffff) | v; }
static void AUD0PER(UWORD v) { audper[0] = v; }
static void AUD1PER(UWORD v) { audper[1] = v; }
static void AUD2PER(UWORD v) { audper[2] = v; }
static void AUD3PER(UWORD v) { audper[3] = v; }
static void AUD0VOL(UWORD v) { audvol[0] = v; }
static void AUD1VOL(UWORD v) { audvol[1] = v; }
static void AUD2VOL(UWORD v) { audvol[2] = v; }
static void AUD3VOL(UWORD v) { audvol[3] = v; }
static void AUD0LEN(UWORD v) { audlen[0] = v; }
static void AUD1LEN(UWORD v) { audlen[1] = v; }
static void AUD2LEN(UWORD v) { audlen[2] = v; }
static void AUD3LEN(UWORD v) { audlen[3] = v; }
/* Custom chip memory bank */
static ULONG custom_lget(CPTR);
static UWORD custom_wget(CPTR);
static UBYTE custom_bget(CPTR);
static void custom_lput(CPTR, ULONG);
static void custom_wput(CPTR, UWORD);
static void custom_bput(CPTR, UBYTE);
addrbank custom_bank = {
custom_lget, custom_wget, custom_bget,
custom_lput, custom_wput, custom_bput,
default_xlate, default_check
};
UWORD custom_wget(CPTR addr)
{
#ifdef DUALCPU
customacc = true;
#endif
switch(addr & 0x1FE) {
case 0x002: return DMACONR();
case 0x004: return VPOSR();
case 0x006: return VHPOSR();
case 0x008: return DSKDATR();
case 0x016: return POTGOR();
case 0x01A: return DSKBYTR();
case 0x01C: return INTENAR();
case 0x01E: return INTREQR();
case 0x010: return ADKCONR();
case 0x00A: return JOY0DAT();
case 0x00C: return JOY1DAT();
default:
custom_wput(addr,0);
return 0xffff;
}
}
UBYTE custom_bget(CPTR addr)
{
return custom_wget(addr & 0xfffe) >> (addr & 1 ? 0 : 8);
}
ULONG custom_lget(CPTR addr)
{
return ((ULONG)custom_wget(addr & 0xfffe) << 16) | custom_wget((addr+2) & 0xfffe);
}
void custom_wput(CPTR addr, UWORD value)
{
#ifdef DUALCPU
customacc = true;
#endif
addr &= 0x1FE;
cregs[addr>>1] = value;
switch(addr) {
case 0x020: DSKPTH(value); break;
case 0x022: DSKPTL(value); break;
case 0x024: DSKLEN(value); break;
case 0x026: DSKDAT(value); break;
case 0x02A: VPOSW(value); break;
case 0x040: BLTCON0(value); break;
case 0x042: BLTCON1(value); break;
case 0x044: BLTAFWM(value); break;
case 0x046: BLTALWM(value); break;
case 0x050: BLTAPTH(value); break;
case 0x052: BLTAPTL(value); break;
case 0x04C: BLTBPTH(value); break;
case 0x04E: BLTBPTL(value); break;
case 0x048: BLTCPTH(value); break;
case 0x04A: BLTCPTL(value); break;
case 0x054: BLTDPTH(value); break;
case 0x056: BLTDPTL(value); break;
case 0x058: BLTSIZE(value); break;
case 0x064: BLTAMOD(value); break;
case 0x062: BLTBMOD(value); break;
case 0x060: BLTCMOD(value); break;
case 0x066: BLTDMOD(value); break;
case 0x070: BLTCDAT(value); break;
case 0x072: BLTBDAT(value); break;
case 0x074: BLTADAT(value); break;
case 0x07E: DSKSYNC(value); break;
case 0x080: COP1LCH(value); break;
case 0x082: COP1LCL(value); break;
case 0x084: COP2LCH(value); break;
case 0x086: COP2LCL(value); break;
case 0x088: COPJMP1(value); break;
case 0x08A: COPJMP2(value); break;
case 0x08E: DIWSTRT(value); break;
case 0x090: DIWSTOP(value); break;
case 0x092: DDFSTRT(value); break;
case 0x094: DDFSTOP(value); break;
case 0x096: DMACON(value); break;
case 0x09A: INTENA(value); break;
case 0x09C: INTREQ(value); break;
case 0x09E: ADKCON(value); break;
case 0x0A0: AUD0LCH(value); break;
case 0x0A2: AUD0LCL(value); break;
case 0x0A4: AUD0LEN(value); break;
case 0x0A6: AUD0PER(value); break;
case 0x0A8: AUD0VOL(value); break;
case 0x0B0: AUD1LCH(value); break;
case 0x0B2: AUD1LCL(value); break;
case 0x0B4: AUD1LEN(value); break;
case 0x0B6: AUD1PER(value); break;
case 0x0B8: AUD1VOL(value); break;
case 0x0C0: AUD2LCH(value); break;
case 0x0C2: AUD2LCL(value); break;
case 0x0C4: AUD2LEN(value); break;
case 0x0C6: AUD2PER(value); break;
case 0x0C8: AUD2VOL(value); break;
case 0x0D0: AUD3LCH(value); break;
case 0x0D2: AUD3LCL(value); break;
case 0x0D4: AUD3LEN(value); break;
case 0x0D6: AUD3PER(value); break;
case 0x0D8: AUD3VOL(value); break;
case 0x0E0: BPL1PTH(value); break;
case 0x0E2: BPL1PTL(value); break;
case 0x0E4: BPL2PTH(value); break;
case 0x0E6: BPL2PTL(value); break;
case 0x0E8: BPL3PTH(value); break;
case 0x0EA: BPL3PTL(value); break;
case 0x0EC: BPL4PTH(value); break;
case 0x0EE: BPL4PTL(value); break;
case 0x0F0: BPL5PTH(value); break;
case 0x0F2: BPL5PTL(value); break;
case 0x0F4: BPL6PTH(value); break;
case 0x0F6: BPL6PTL(value); break;
case 0x100: BPLCON0(value); break;
case 0x102: BPLCON1(value); break;
case 0x104: BPLCON2(value); break;
case 0x106: BPLCON3(value); break;
case 0x108: BPL1MOD(value); break;
case 0x10A: BPL2MOD(value); break;
case 0x110: BPL1DAT(value); break;
case 0x112: BPL2DAT(value); break;
case 0x114: BPL3DAT(value); break;
case 0x116: BPL4DAT(value); break;
case 0x118: BPL5DAT(value); break;
case 0x11A: BPL6DAT(value); break;
case 0x180: case 0x182: case 0x184: case 0x186: case 0x188: case 0x18A:
case 0x18C: case 0x18E: case 0x190: case 0x192: case 0x194: case 0x196:
case 0x198: case 0x19A: case 0x19C: case 0x19E: case 0x1A0: case 0x1A2:
case 0x1A4: case 0x1A6: case 0x1A8: case 0x1AA: case 0x1AC: case 0x1AE:
case 0x1B0: case 0x1B2: case 0x1B4: case 0x1B6: case 0x1B8: case 0x1BA:
case 0x1BC: case 0x1BE:
COLOR(value & 0xFFF, (addr & 0x3E) / 2);
break;
case 0x120: case 0x124: case 0x128: case 0x12C:
case 0x130: case 0x134: case 0x138: case 0x13C:
SPRxPTH(value, (addr - 0x120) / 4);
break;
case 0x122: case 0x126: case 0x12A: case 0x12E:
case 0x132: case 0x136: case 0x13A: case 0x13E:
SPRxPTL(value, (addr - 0x122) / 4);
break;
case 0x140: case 0x148: case 0x150: case 0x158:
case 0x160: case 0x168: case 0x170: case 0x178:
SPRxPOS(value, (addr - 0x140) / 8);
break;
case 0x142: case 0x14A: case 0x152: case 0x15A:
case 0x162: case 0x16A: case 0x172: case 0x17A:
SPRxCTL(value, (addr - 0x142) / 8);
break;
case 0x144: case 0x14C: case 0x154: case 0x15C:
case 0x164: case 0x16C: case 0x174: case 0x17C:
SPRxDATA(value, (addr - 0x144) / 8);
break;
case 0x146: case 0x14E: case 0x156: case 0x15E:
case 0x166: case 0x16E: case 0x176: case 0x17E:
SPRxDATB(value, (addr - 0x146) / 8);
break;
case 0x36: JOYTEST(value); break;
}
}
void custom_bput(CPTR addr, UBYTE value)
{
/* Yes, there are programs that do this. The programmers should be shot.
* This might actually work sometimes. */
UWORD rval = value;
CPTR raddr = addr & 0x1FE;
if (addr & 1) {
rval |= cregs[raddr >> 1] & 0xFF00;
} else {
rval <<= 8;
rval |= cregs[raddr >> 1] & 0xFF;
}
custom_wput(raddr, rval);
}
void custom_lput(CPTR addr, ULONG value)
{
custom_wput(addr & 0xfffe, value >> 16);
custom_wput((addr+2) & 0xfffe, (UWORD)value);
}
static bool copcomp(void)
{
UWORD vp = vpos & (((copi2 >> 8) & 0x7F) | 0x80);
UWORD hp = current_hpos() & (copi2 & 0xFE);
UWORD vcmp = copi1 >> 8;
UWORD hcmp = copi1 & 0xFE;
return (vp > vcmp || (vp == vcmp && hp >= hcmp)) && ((copi2 & 0x8000) || !(DMACONR() & 0x4000));
}
/*
* Calculate the minimum number of cycles after which the
* copper comparison becomes true. This is quite tricky. I hope it works.
*/
static int calc_copcomp_true(int currvpos, int currhpos)
{
UWORD vp = currvpos & (((copi2 >> 8) & 0x7F) | 0x80);
UWORD hp = currhpos & (copi2 & 0xFE);
UWORD vcmp = copi1 >> 8;
UWORD hcmp = copi1 & 0xFE;
int cycleadd = maxhpos - currhpos;
int coptime = 0;
if ((vp > vcmp || (vp == vcmp && hp >= hcmp)) && ((copi2 & 0x8000) || !(DMACONR() & 0x4000)))
return 0;
try_again:
while (vp < vcmp) {
currvpos++;
if (currvpos > maxvpos + 1)
return -1;
currhpos = 0;
coptime += cycleadd;
cycleadd = maxhpos;
vp = currvpos & (((copi2 >> 8) & 0x7F) | 0x80);
}
hp = currhpos & (copi2 & 0xFE);
if (!(vp > vcmp)) {
while (hp < hcmp) {
currhpos++;
if (currhpos > maxhpos) {
/* Now, what? There might be a good position on the
* next line. But it can also be the FFFF FFFE
* case.
*/
currhpos = 0;
currvpos++;
vp = currvpos & (((copi2 >> 8) & 0x7F) | 0x80);
goto try_again;
}
coptime++;
hp = currhpos & (copi2 & 0xFE);
}
}
if (coptime == 0) /* waiting for the blitter */
return 1;
return coptime;
}
static void copper_read(void)
{
if (dmaen(DMA_COPPER)){
copi1 = chipmem_bank.wget(coplc);
copi2 = chipmem_bank.wget(coplc+2);
coplc += 4;
eventtab[ev_copper].oldcycles = cycles;
eventtab[ev_copper].evtime = (copi1 & 1) ? (copi2 & 1) ? 4 : 6 : 4;
copstate = (copi1 & 1) ? (copi2 & 1) ? COP_skip : COP_wait : COP_move;
} else {
copstate = COP_read;
eventtab[ev_copper].active = false;
}
}
static void do_copper(void)
{
switch(copstate){
case COP_read:
copper_read();
break;
case COP_move:
if (copi1 >= (copcon & 2 ? 0x40 : 0x80))
custom_bank.wput(copi1,copi2);
copper_read();
break;
case COP_skip:
if (copcomp())
coplc += 4;
copper_read();
break;
case COP_wait: {
int coptime = calc_copcomp_true(vpos, current_hpos());
if (coptime < 0) {
copstate = COP_stop;
eventtab[ev_copper].active = false;
copper_active = false;
} else {
if (!coptime)
copper_read();
else {
eventtab[ev_copper].evtime = coptime;
eventtab[ev_copper].oldcycles = cycles;
}
}
break;
}
case COP_stop:
eventtab[ev_copper].active = false;
copper_active = false;
break;
}
}
static __inline__ bool blitter_read(void)
{
if (bltcon0 & 0xe00){
if (!dmaen(DMA_BLITTER)) return true; /* blitter stopped */
if (!blitline){
if (bltcon0 & 0x800) bltadat = chipmem_bank.wget(bltapt);
if (bltcon0 & 0x400) bltbdat = chipmem_bank.wget(bltbpt);
}
if (bltcon0 & 0x200) bltcdat = chipmem_bank.wget(bltcpt);
}
bltstate = BLT_work;
return (bltcon0 & 0xE00) != 0;
}
static __inline__ bool blitter_write(void)
{
if (bltddat) blitzero = false;
if ((bltcon0 & 0x100) || blitline){
if (!dmaen(DMA_BLITTER)) return true;
chipmem_bank.wput(bltdpt,bltddat);
}
bltstate = BLT_next;
return (bltcon0 & 0x100) != 0;
}
static void blitter_blit(void)
{
UWORD blitahold,blitbhold,blitchold;
UWORD bltaold;
if (blitdesc) {
UWORD bltamask = 0xffff;
if (!blitlpos) { bltamask &= bltafwm; }
if (blitlpos == (hblitsize - 1)) { bltamask &= bltalwm; }
bltaold = bltadat & bltamask;
blitahold = (((ULONG)bltaold << 16) | blitpreva) >> (16-blitashift);
blitbhold = (((ULONG)bltbdat << 16) | blitprevb) >> (16-blitbshift);
blitchold = bltcdat;
} else {
UWORD bltamask = 0xffff;
if (!blitlpos) { bltamask &= bltafwm; }
if (blitlpos == (hblitsize - 1)) { bltamask &= bltalwm; }
bltaold = bltadat & bltamask;
blitahold = (((ULONG)blitpreva << 16) | bltaold) >> blitashift;
blitbhold = (((ULONG)blitprevb << 16) | bltbdat) >> blitbshift;
blitchold = bltcdat;
}
bltddat = 0;
bltddat = blit_func(blitahold, blitbhold, blitchold, bltcon0 & 0xFF);
if (blitfill){
UWORD fillmask;
for (fillmask = 1; fillmask; fillmask <<= 1){
UWORD tmp = bltddat;
if (blitfc) {
if (blitife)
bltddat |= fillmask;
else
bltddat ^= fillmask;
}
if (tmp & fillmask) blitfc = !blitfc;
}
}
bltstate = BLT_write;
blitpreva = bltaold; blitprevb = bltbdat;
}
static void blitter_nxblit(void)
{
bltstate = BLT_read;
if (blitdesc){
if (++blitlpos == hblitsize) {
if (--vblitsize == 0) {
bltstate = BLT_done;
#ifdef NO_FAST_BLITTER
custom_bank.wput(0xDFF09C,0x8040);
#endif
}
blitfc = bltcon1 & 0x4;
blitlpos = 0;
if (bltcon0 & 0x800) bltapt -= 2+bltamod;
if (bltcon0 & 0x400) bltbpt -= 2+bltbmod;
if (bltcon0 & 0x200) bltcpt -= 2+bltcmod;
if (bltcon0 & 0x100) bltdpt -= 2+bltdmod;
} else {
if (bltcon0 & 0x800) bltapt -= 2;
if (bltcon0 & 0x400) bltbpt -= 2;
if (bltcon0 & 0x200) bltcpt -= 2;
if (bltcon0 & 0x100) bltdpt -= 2;
}
} else {
if (++blitlpos == hblitsize) {
if (--vblitsize == 0) {
bltstate = BLT_done;
#ifdef NO_FAST_BLITTER
custom_bank.wput(0xDFF09C,0x8040);
#endif
}
blitlpos = 0;
if (bltcon0 & 0x800) bltapt += 2+bltamod;
if (bltcon0 & 0x400) bltbpt += 2+bltbmod;
if (bltcon0 & 0x200) bltcpt += 2+bltcmod;
if (bltcon0 & 0x100) bltdpt += 2+bltdmod;
} else {
if (bltcon0 & 0x800) bltapt += 2;
if (bltcon0 & 0x400) bltbpt += 2;
if (bltcon0 & 0x200) bltcpt += 2;
if (bltcon0 & 0x100) bltdpt += 2;
}
}
}