-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameboy.c
2958 lines (2681 loc) · 82.2 KB
/
gameboy.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
/*
gameboy.c
This file emulates the hardware (all around the Gameboy-CPU).
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef UNIX
#include <unistd.h>
#endif
#include <ctype.h>
#include <string.h>
#include "globals.h"
#include "gameboy.h"
#include "sgb.h"
#include "z80.h"
#define Z80OPC_GAMEBOYC_SKIP /* only M_RST needed from here */
#include "z80opc.h"
#include "arplay.h"
#include "sys.h"
#ifdef SOUND
/*#define DEBUG_SOUND*/
#include "sound.h"
#endif
rombank bank[GB_MAXROMBANKCOUNT]; /* rom */
rambank sram[GB_MAXRAMBANKCOUNT]; /* sram */
uchar *vram[2]; /* vram */
uchar *iram[8]; /* internal ram */
/* don't forget echoing ! */
uchar *oam; /* oam */
uchar *io; /* io */
char *lcdbuffer; /* gameboy screen buffer */
unsigned int GB_BGPAL[8][4]=
{{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000}};
unsigned int GB_OBJPAL[8][4]=
{{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000},
{0x7FFF,0x56B5,0x294A,0x0000}};
ulong BGPAL[8][4],OBJPAL[8][4];
uint rombanknr,workbanknr;
uint romselectmask;
uchar rambanknr;
int rambankenabled;
uchar sramselectmask;
int mbctype;
int mbcmode;
uchar newjoypadstate,joypadstate,lcdphase;
int timerfreq,timerclks,dividerclks,lcdclks,joypadclks,
vblankdelay,sramsize,srambanksize,lastjoysel,serialioclks,
soundcounter,ABORT_EMULATION,vblankoccured,
hdmastarted,hdmasrc,hdmadst,soundclks,snd_updateclks,colormode,
siosendout;
int serialclks[2][2]={{4096,2048},{128,64}};
/* [SC bit 1][cpuspeed] */
char sramfile[256];
void (*scanline)(void); /* standard gameboy video output */
void gb_scanline8(void);
void gb_scanline16(void);
void gb_scanline32(void);
void cgb_scanline16(void); /* gameboy color video output */
void cgb_scanline32(void);
void cgb_scanline8(void);
void switch2color(void);
int GB_SRAMBANKSIZE[]=
{
0x2000,0x0800,0x2000,0x2000
};
uchar GB_SRAMSELECTMASK[]=
{
1,1,1,3
};
uchar GB_SRAMBANKCOUNT[]=
{
1,1,1,4
};
uint GB_VCLKS[]=
{
80,172,208,560
};
/*uint GB_VCLKS[]=
{
80,172,208,460 * cycles 2,3,0,sigma *
};*/
/*uint GB_VCLKS[]=
{
80,172,204,456 * cycles 2,3,0,sigma (old, I made the refresh slower) *
};*/
uint GB_TIMERFRQ[]=
{
1024,16,64,256
};
uchar Area_0xFF00_init[]=
{
0xCF,0x00,0x7E,0xFF,0xAD,0x00,0x00,0xF8,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE1,
0x80,0xBF,0xF3,0xFF,0x3F,0xFF,0x3F,0x00,
0xFF,0x3F,0x7F,0xFF,0x9F,0xFF,0x3F,0xFF,
0xFF,0x00,0x00,0x3F,0x77,0xF3,0xF1,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x91,0x80,0x00,0x00,0x05,0x00,0x00,0xFC,
0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00
};
uchar Nintendo_Logo[]=
{
0xCE,0xED,0x66,0x66,0xCC,0x0D,0x00,0x0B,
0x03,0x73,0x00,0x83,0x00,0x0C,0x00,0x0D,
0x00,0x08,0x11,0x1F,0x88,0x89,0x00,0x0E,
0xDC,0xCC,0x6E,0xE6,0xDD,0xDD,0xD9,0x99,
0xBB,0xBB,0x67,0x63,0x6E,0x0E,0xEC,0xCC,
0xDD,0xDC,0x99,0x9F,0xBB,0xB9,0x33,0x3E
};
uchar H_Flip[]=
{
0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,
0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,
0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
0x04,0x84,0x44,0xC4,0x24,0xA4,0x64,0xE4,
0x14,0x94,0x54,0xD4,0x34,0xB4,0x74,0xF4,
0x0C,0x8C,0x4C,0xCC,0x2C,0xAC,0x6C,0xEC,
0x1C,0x9C,0x5C,0xDC,0x3C,0xBC,0x7C,0xFC,
0x02,0x82,0x42,0xC2,0x22,0xA2,0x62,0xE2,
0x12,0x92,0x52,0xD2,0x32,0xB2,0x72,0xF2,
0x0A,0x8A,0x4A,0xCA,0x2A,0xAA,0x6A,0xEA,
0x1A,0x9A,0x5A,0xDA,0x3A,0xBA,0x7A,0xFA,
0x06,0x86,0x46,0xC6,0x26,0xA6,0x66,0xE6,
0x16,0x96,0x56,0xD6,0x36,0xB6,0x76,0xF6,
0x0E,0x8E,0x4E,0xCE,0x2E,0xAE,0x6E,0xEE,
0x1E,0x9E,0x5E,0xDE,0x3E,0xBE,0x7E,0xFE,
0x01,0x81,0x41,0xC1,0x21,0xA1,0x61,0xE1,
0x11,0x91,0x51,0xD1,0x31,0xB1,0x71,0xF1,
0x09,0x89,0x49,0xC9,0x29,0xA9,0x69,0xE9,
0x19,0x99,0x59,0xD9,0x39,0xB9,0x79,0xF9,
0x05,0x85,0x45,0xC5,0x25,0xA5,0x65,0xE5,
0x15,0x95,0x55,0xD5,0x35,0xB5,0x75,0xF5,
0x0D,0x8D,0x4D,0xCD,0x2D,0xAD,0x6D,0xED,
0x1D,0x9D,0x5D,0xDD,0x3D,0xBD,0x7D,0xFD,
0x03,0x83,0x43,0xC3,0x23,0xA3,0x63,0xE3,
0x13,0x93,0x53,0xD3,0x33,0xB3,0x73,0xF3,
0x0B,0x8B,0x4B,0xCB,0x2B,0xAB,0x6B,0xEB,
0x1B,0x9B,0x5B,0xDB,0x3B,0xBB,0x7B,0xFB,
0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,
0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7,
0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,
0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF,
};
uchar IOIn(uint);
void IOOut(uint,uchar);
uchar IOIn(uint addr)
{
switch (addr) {
case 0x44:
return LY<153 ? LY : 0;
default:
return io[addr];
}
}
uchar GetByteAt(uint addr)
{
#ifdef HARDDEBUG
if (addr>=0xFF10&&addr<=0xFF27) {
fprintf(OUTSTREAM,"IO read access: 0x%04X: %04X=>",Z80_REG.W.PC,addr);
if (addr>=0x4000) {
if (addr>=0x8000) {
if (addr>=0xA000) {
if (addr>=0xC000) {
if (addr>=0xD000) {
if (addr>=0xE000) {
if (addr>=0xF000) {
if (addr>=0xFE00) {
if (addr>=0xFF00) {
fprintf(OUTSTREAM,"%02X",IOIn(addr&0x00FF));
} else fprintf(OUTSTREAM,"%02X",oam[addr&0x00FF]);
} else fprintf(OUTSTREAM,"%02X",
iram[workbanknr][addr&0x0FFF]);
} else fprintf(OUTSTREAM,"%02X",iram[0][addr&0x0FFF]);
} else fprintf(OUTSTREAM,"%02X",
iram[workbanknr][addr&0x0FFF]);
} else fprintf(OUTSTREAM,"%02X",iram[0][addr&0x0FFF]);
} else fprintf(OUTSTREAM,"%02X",sram[rambanknr][addr&0x1FFF]);
} else fprintf(OUTSTREAM,"%02X",vram[VBK&1][addr&0x1FFF]);
} else fprintf(OUTSTREAM,"%02X",bank[rombanknr][addr&0x3FFF]);
} else fprintf(OUTSTREAM,"%02X",bank[0][addr]);
fprintf(OUTSTREAM,"\n");
}
#endif
if (addr<0x4000) return bank[0][addr];
if (addr<0x8000) return bank[rombanknr][addr & 0x3FFF];
if (addr<0xA000) return vram[VBK&1][addr & 0x1FFF];
if (addr<0xC000)
return /*(addr&0x1FFF)>=srambanksize ? 0 :*/
sram[rambanknr][addr & 0x1FFF];
if (addr<0xD000) return iram[0][addr&0x0FFF];
if (addr<0xE000) return iram[workbanknr][addr&0x0FFF];
if (addr<0xF000) return iram[0][addr&0x0FFF];
if (addr<0xFE00) return iram[workbanknr][addr&0x0FFF];
if (addr<0xFF00) return oam[addr & 0x00FF];
return IOIn(addr & 0x00FF);
/* switch (addr >> 13) {
}*/
}
uint GetWordAt(uint addr)
{
return ((uint)GetByteAt(addr+1)<<8)|GetByteAt(addr);
}
void IOOut(uint addr,uchar value)
{
int i;
uint u;
value=ar_checkwrite(addr+0xFF00,value);
if (addr<0x4C) {
switch (addr) {
case 0x00: /* joypad info */
if (sgb_packagetransfer && !colormode) {
switch ((value >> 4)& 0x03) {
case 0:
#ifdef TDEBUG
fprintf(OUTSTREAM,"init\n");
sgb_dumppackagebuffer();
#endif
sgb_packagetransfer=1;
sgb_byte&=0xFFF0;
/* sgb_buffer[0]=0;*/
sgb_bitnr=0;
sgb_bit=-1;
break;
case 1:sgb_bit=1;break;
case 2:sgb_bit=0;break;
case 3:
if (sgb_bit<0) break;
if ((sgb_pkgdone==0)&&(sgb_byte<SGB_BUFFERSIZE)) {
sgb_buffer[sgb_byte]|=(sgb_bit<<sgb_bitnr);
sgb_bitnr++;
if (sgb_bitnr>7) {
sgb_bitnr=0;
sgb_byte++;
sgb_buffer[sgb_byte]=0;
/* printf("(%d,%d)\n",sgb_byte,sgb_bitnr);*/
if (!(sgb_byte&0x0F)) {
sgb_packagetransfer=0;
sgb_pkgdone=1;
sgb_processcmd();
}
}
} else {
/* package transfer successfully closed */
sgb_closecmd();
}
break;
}
}
switch ((value >> 4)& 0x03) {
case 0:
#ifdef TDEBUG
fprintf(OUTSTREAM,"init\n");
sgb_dumppackagebuffer();
#endif
sgb_packagetransfer=1;
sgb_pkgdone=0;
sgb_byte&=0xFFF0;
sgb_bitnr=0;
/* sgb_buffer[0]=0;*/
sgb_bit=-1;
break;
case 1:lastjoysel=0;break;
case 2:lastjoysel=1;break;
case 3:lastjoysel=2;sgb_packagetransfer=0;break;
}
switch (lastjoysel) {
case 0:
P1=(joypadstate&0x0F)|0xD0;
break;
case 1:
P1=(joypadstate>>4)|0xE0;
break;
case 2:
/*if (!force_stdgameboy) P1=0x02;*/
break;
}
/*printf("jps: (%02X,%02X,%d)->%02X\n",value,joypadstate,lastjoysel,P1);*/
break;
case 0x01: /* serial read/write byte */
siosendout=value;
break;
case 0x02: /* serial control */
if (colormode)
serialioclks=serialclks[(SC&2)>>1][DBLSPEED];
else serialioclks=serialclks[0][0];
#ifdef DIALOGLINK
if ((value&0x81)==0x81) {
dlglink_sndbyte(siosendout);
#ifdef HARDDEBUG
printf("Master sio at PC:%04X (CTRL:%02X) =>%02X.\n",
Z80_REG.W.PC,value,siosendout);
#endif
}
#ifdef HARDDEBUG
if ((value&0x81)==0x80) {
printf("Slave sio at PC:%04X (CTRL:%02X) =>%02X.\n",
Z80_REG.W.PC,value,siosendout);
}
#endif
#endif
SC=value;
#ifndef DIALOGLINK
if (SC&0x01) IF|=0x08;
SC&=0x7F;SB=0xFF;
#endif
break;
case 0x04: /* reset divider reg */
DIV=0;dividerclks=GB_DIVLOAD;
break;
case 0x07:
TAC=value|0xF8;
timerclks=timerfreq=GB_TIMERFRQ[value&0x03];
break;
case 0x0F: /* interrupt flag */
IF=value|0xE0;
break;
case 0x40: /* lcd control */
if ((value & 0x80)==0) {
LY=0;
lcdphase=0;
STAT=(STAT&0xFC)|2;
lcdclks=GB_VCLKS[0];
}
LCDC=value;
break;
case 0x41:
STAT=(STAT&0x07)|0x80|(value&0x78);
break;
case 0x44:
/* should I reset ? */
break;
case 0x46:
for (i=0;i<0xA0;i++)
/* if ((i&3)==3)
oam[i]=(oam[i]&0x0F)|(GetByteAt(((uint)value << 8)+i)&0xF0);
else */
oam[i]=GetByteAt(((uint)value << 8)+i);
break;
/* unsupported regs (unchanged) */
case 0x03:
/* sound control regs */
case 0x15:
io[addr]=value;
break;
case 0x24: /* handled in sound module */
case 0x25:
case 0x26:
if (addr==0xFF26 && (value&0x80)>0) {
soundcounter=4000000;
io[0x26]=0x8F;
}
#ifdef DEBUG_SOUND
printf("NR5%d<=%02X\n",(addr-4)&0x7,value);
#endif
io[addr]=value;
break;
case 0x10:
NR10=value;
#ifdef SOUND
#ifdef DEBUG_SOUND
if (value&7) printf("NR10<=%02X\n",value);
#endif
snd1_st=snd1_str=((NR10>>4)&7)<<1;
#endif
break;
case 0x11:
NR11=value;
#ifdef DEBUG_SOUND
if ((value&0xC0)!=0x80) printf("NR11<=%02X\n",value);
#endif
break;
case 0x12:
NR12=value;
#ifdef DEBUG_SOUND
printf("NR12<=%02X\n",value);
#endif
#ifdef SOUND
value=(value&7)<<2;
if (/*(NR12&8)==0 &&*/ value<snd_length1) snd_length1=value;
#endif
break;
case 0x13:
NR13=value;
#ifdef SOUND
/* if (value==0xFF) snd_length1=0;*/
snd_reload1=gbfreq((((int)(NR14&7)<<8))|NR13);
#endif
#ifdef DEBUG_SOUND
printf("snd_reload1(low: %05i;%02X-%02X)\n",snd_reload1,NR13,NR12);
#endif
break;
case 0x14:
NR14=value;
#ifdef SOUND
snd_reload1=gbfreq((((int)(NR14&7)<<8))|NR13);
if (value&0x80) {
if ((snd_init1=NR12&0xF7 ? 1 : 0))
snd_length1=value & 0x40 ? 65-(NR11&0x3F) : 0xFFFF;
#ifdef DEBUG_SOUND
printf("%05i %09i (%02X%02X)\n",
((int)(NR14&7)<<8)|NR13,snd_reload1,NR13,NR14);
#endif
}
#endif
break;
case 0x16:
#ifdef DEBUG_SOUND
if ((value&0xC0)!=0x80) printf("NR21<=%02X\n",value);
#endif
NR21=value;
break;
case 0x17:
NR22=value;
#ifdef SOUND
value=(value&7)<<2;
if (/*(NR22&8)==0 &&*/ value<snd_length2) snd_length2=value;
#endif
break;
case 0x18:
NR23=value;
#ifdef SOUND
if (value==0xFF) snd_length2=0;
snd_reload2=gbfreq((((int)(NR24&7)<<8))|NR23);
#endif
break;
case 0x19:
NR24=value;
#ifdef SOUND
snd_reload2=gbfreq((((int)(NR24&7)<<8))|NR23);
if (value&0x80) {
if ((snd_init2=NR22&0xF7 ? 1 : 0))
snd_length2=value & 0x40 ? 65-(NR21&0x3F) : 0xFFFF;
}
#endif
break;
case 0x1A:
NR30=value;
break;
case 0x1B:
NR31=value;
break;
case 0x1C:
NR32=value;
#ifdef SOUND
switch ((value>>5)&3) {
case 0:snd_shr=4;break;
case 1:snd_shr=0;break;
case 2:snd_shr=1;break;
case 3:snd_shr=2;break;
}
#endif
break;
case 0x1D:
NR33=value;
#ifdef SOUND
if (value==0xFF) snd_length3=0;
snd_reload3=gbfreq((((int)(NR34&7)<<8))|NR33)>>1;
#endif
break;
case 0x1E:
NR34=value;
#ifdef SOUND
snd_reload3=gbfreq((((int)(NR34&7)<<8))|NR33)>>1;
if (value&0x80) {
if ((snd_init3=(NR30>>7)))
snd_length3=value & 0x40 ? 257-NR31 : 0xFFFF;
}
#endif
break;
case 0x20:
#ifdef DEBUG_SOUND
printf("NR41<=%02X\n",value);
#endif
NR41=value;
break;
case 0x21:
#ifdef DEBUG_SOUND
printf("NR42<=%02X\n",value);
#endif
NR42=value;
#ifdef SOUND
value=(value&7)<<2;
if (/*(NR42&8)==0 &&*/ value<snd_length4) snd_length4=value;
/* if ((NR42&0xF7)==0) snd_init4=snd_length4=0;*/
#endif
break;
case 0x22:
#ifdef DEBUG_SOUND
printf("NR43<=%02X\n",value);
#endif
NR43=value;
break;
case 0x23:
#ifdef DEBUG_SOUND
printf("NR44<=%02X\n",value);
#endif
NR44=value;
/*
#ifdef SOUND
if (value&0x80) {
snd_reload4=1;
if ((snd_init4=NR42&0xF8 ? 1 : 0)) {
snd_length4=value & 0x40 ? 64-(NR41&0x3F) :
(NR42 & 0x07)<<4;
printf("snd_len4 %d\n",snd_length4);
}
}
#endif
*/
#ifdef SOUND
if (value&0x80) {
snd_reload4=1;
if ((snd_init4=NR42&0xF7 ? NR44>>7 : 0))
snd_length4=value & 0x40 ? 65-(NR41&0x3F) :
(NR42 & 0x07)<<4;
}
#endif
soundcounter=4000000;
break;
/* unchanged writes */
case 0x05: /* timer counter */
case 0x06: /* timer modulo */
case 0x30: /* waveform sound data */
case 0x31: /**/
case 0x32:
case 0x33:
case 0x34: /**/
case 0x35:
case 0x36:
case 0x37: /**/
case 0x38:
case 0x39:
case 0x3A:
case 0x3B:
case 0x3C: /**/
case 0x3D:
case 0x3E:
case 0x3F: /* --- waveform end */
case 0x42: /* scroll y */
case 0x43: /* scroll x */
case 0x45: /* lcd y compare */
case 0x47: /* BG palette */
case 0x48: /* sprite palette 0 */
case 0x49: /* sprite palette 1 */
case 0x4A: /* window y */
case 0x4B: /* window x */
io[addr]=value;
break;
default:
return;
}
} else {
/* also 0xFFFF (=interrupt enable) *** unchanged *** */
if (addr>0x7F) io[addr]=value;
/* CGB io handling */
switch (addr) {
case 0x4D: /* cpu speed switch */
if (!colormode) switch2color();
KEY1=(KEY1&0x80)|(value&1);
break;
case 0x4F: /* video bank selection */
if (!colormode) switch2color();
VBK=value&1;
break;
case 0x51: /* cgb DMA source hi */
if (!colormode) switch2color();
HDMA1=value;
hdmasrc=((unsigned int)HDMA1<<8)|(HDMA2&0xF0);
break;
case 0x52: /* cgb DMA source low */
if (!colormode) switch2color();
HDMA2=value&0xF0;
hdmasrc=((unsigned int)HDMA1<<8)|(HDMA2&0xF0);
break;
case 0x53: /* cgb DMA destination hi */
if (!colormode) switch2color();
HDMA3=value&0x1F;
hdmadst=((unsigned int)(HDMA3&0x1F)<<8)|(HDMA4&0xF0);
break;
case 0x54: /* cgb DMA destination low */
if (!colormode) switch2color();
HDMA4=value&0xF0;
hdmadst=((unsigned int)(HDMA3&0x1F)<<8)|(HDMA4&0xF0);
break;
case 0x55: /* cgb DMA start and counter */
if (!colormode) switch2color();
HDMA5=value;
hdmasrc=((unsigned int)HDMA1<<8)|(HDMA2&0xF0);
hdmadst=((unsigned int)(HDMA3&0x1F)<<8)|(HDMA4&0xF0);
switch (hdmastarted) { /* general=>1; horizontal=>2 stoping=>-1*/
case 2:
hdmastarted=value&0x80 ? 2 : -1;
HDMA5&=0x7F;
break;
default:
hdmastarted=((HDMA5&0x80)>>7)+1;
if (hdmastarted==2) {
HDMA5&=0x7F;
break;
}
for (i=((HDMA5&0x7F)+1)<<4;i>0;i--,hdmadst++,hdmasrc++)
vram[VBK&1][hdmadst&0x1FFF]=GetByteAt(hdmasrc);
HDMA5=0x80;
HDMA1=hdmasrc>>8;
HDMA2=hdmasrc&0xF0;
HDMA3=(hdmadst>>8)&0x1F;
HDMA4=hdmadst&0xF0;
hdmastarted=0;
break;
}
#ifdef DEBUG
printf("HDMA5<= %02X (%i) (%04X=>%04X) at 0x%04X\n",value,
hdmastarted,hdmasrc,hdmadst+0x8000,Z80_REG.W.PC);
#endif
/* for (i=((HDMA5&0x7F)+1)<<4;i>0;i--,dma_d++,dma_s++)
vram[VBK&1][dma_d&0x1FFF]=GetByteAt(dma_s);*/
break;
case 0x56: /* infrared IO */
if (!colormode) switch2color();
#ifdef DEBUG
printf("Fixme: infrared access (%02X at %04X).\n",value,Z80_REG.W.PC);
#endif
break;
case 0x68: /* BG pal write specs */
if (!colormode) switch2color();
BCPS=value&0xBF;
u=GB_BGPAL[(value>>3)&7][(value>>1)&3];
BCPD=value&1 ? (u>>8)&0x7F : u&0xFF;
break;
case 0x69: /* BG pal write data */
if (!colormode) switch2color();
i=BCPS;
if (i&1) { /* high word */
u=GB_BGPAL[(i>>3)&7][(i>>1)&3]=(GB_BGPAL[(i>>3)&7][(i>>1)&3]&0xFF)|
(value<<8);
} else { /* low word */
u=GB_BGPAL[(i>>3)&7][(i>>1)&3]=(GB_BGPAL[(i>>3)&7][(i>>1)&3]&0xFF00)|
value;
}
BGPAL[(i>>3)&7][(i>>1)&3]=color_translate(u);
i=BCPS=i&0x80 ? (i&0x80)|((i+1)&0x3F) : i;
u=GB_BGPAL[(i>>3)&7][(i>>1)&3];
BCPD=i&1 ? u>>8 : u&0xFF;
break;
case 0x6A: /* OBJ pal write specs */
if (!colormode) switch2color();
OCPS=value&0xBF;
u=GB_OBJPAL[(value>>3)&7][(value>>1)&3];
OCPD=value&1 ? (u>>8)&0x7F : u&0xFF;
break;
case 0x6B: /* OBJ pal write data */
if (!colormode) switch2color();
i=OCPS;
if (i&1) { /* high word */
u=GB_OBJPAL[(i>>3)&7][(i>>1)&3]=(GB_OBJPAL[(i>>3)&7][(i>>1)&3]&0xFF)|
(value<<8);
} else { /* low word */
u=GB_OBJPAL[(i>>3)&7][(i>>1)&3]=(GB_OBJPAL[(i>>3)&7][(i>>1)&3]&0xFF00)|
value;
}
OBJPAL[(i>>3)&7][(i>>1)&3]=color_translate(u);
i=OCPS=i&0x80 ? (i&0x80)|((i+1)&0x3F) : i;
u=GB_OBJPAL[(i>>3)&7][(i>>1)&3];
OCPD=i&1 ? u>>8 : u&0xFF;
break;
case 0x70: /* work bank selection */
if (!colormode) switch2color();
SVBK=value&7;
workbanknr=value&7;
if (workbanknr==0) workbanknr++;
#ifdef HARDDEBUG
printf("WRAM bank %i selected.\n",workbanknr);
#endif
break;
}
}
}
void SetByteAt(uint addr, uchar value)
{
#ifdef DEBUG
if (addr==0xff59)
fprintf(OUTSTREAM,"%04X=>%02X accessed at PC: %04X\n",
addr,value,Z80_REG.W.PC);
/* fprintf(OUTSTREAM,"%d%d ",(value>>5)&1,(value>>4)&1);*/
#endif
switch (addr >> 8) {
case 0xFF:IOOut(addr & 0xFF,value);return;
default:
switch (addr >> 13) {
case 0: /* RAM bank enable */
if (mbctype==0) {
fprintf(OUTSTREAM,"Fixme: No MBC and using SRAM ?\n");
fprintf(OUTSTREAM," (written %02X to %04X)\n",value,addr);
return;
}
if ((mbctype==2)&&((addr & 0x0100)>0)) {
fprintf(OUTSTREAM,"Fixme: Using MBC2 and writing 0x%02X to 0x%04X.\n",
value,addr);
return;
}
value&=0x0F;
#ifdef HARDDEBUG
if ((value!=10)&&(value!=0)) {
fprintf(OUTSTREAM,"SRAM disable ? (0x%02X=>0x%04X)\n",value,addr);
}
#endif
rambankenabled= value == 10 ? 1 : 0;
return;
case 1: /* ROM bank select */
switch (mbctype) {
case 0:
if (value!=1) {
fprintf(OUTSTREAM,"Fixme: No MBC and using ROM switching ?\n");
fprintf(OUTSTREAM," (written %02X to %04X)\n",value,addr);
return;
}
break;
case 1:
if (mbcmode)
rombanknr=value; else
rombanknr=(rombanknr&0x60)|(value & 0x1F);
if (rombanknr>=romselectmask) {
fprintf(OUTSTREAM,"Fixme: ROM bank %d selected.\n",rombanknr);
return;
}
break;
case 2:
if ((addr & 0x0100)==0) {
fprintf(OUTSTREAM,
"Fixme: Using MBC2 and writing 0x%02X to 0x%04X.\n",
value,addr);
return;
}
if (mbcmode)
rombanknr=value; else
rombanknr=(rombanknr&0x60)|(value & 0x1F);
if (rombanknr>=romselectmask) {
fprintf(OUTSTREAM,"Fixme: ROM bank %d selected.\n",value);
return;
}
break;
case 5:
if (addr>=0x3000) break;
if (value>=romselectmask) {
fprintf(OUTSTREAM,"Fixme: ROM bank %d selected (PC:%04X).\n",
value,Z80_REG.W.PC);
value&=romselectmask-1;
}
rombanknr=value&0x7F;
break;
}
if (rombanknr==0) rombanknr++;
#ifdef HARDDEBUG
fprintf(OUTSTREAM,"ROM select %d (PC:%04X).\n",rombanknr,Z80_REG.W.PC);
#endif
return;
case 2: /* RAM/ROMHI bank select */
if ((value&0xF0)>0) return;
if (mbctype==2) {
fprintf(OUTSTREAM,"Fixme: MBC2 and selecting RAM banks ?\n");
fprintf(OUTSTREAM," (written %02X to %04X)\n",value,addr);
return;
}
if ((mbcmode==0)&&(mbctype==1)) { /* ROM hi-addr */
if (value>3) {
fprintf(OUTSTREAM,"Fixme: 16/8 mode, illegal hi address.\n");
fprintf(OUTSTREAM," (written %02X to %04X)\n",value,addr);
return;
}
rombanknr=(rombanknr & 0x1F)|((value&3)<<5);
if (rombanknr>=romselectmask) {
fprintf(OUTSTREAM,"Fixme: ROM bank %d selected.\n",value);
return;
}
#ifdef DEBUG
fprintf(OUTSTREAM,"ROM bank %d selected (hi addr) PC:%04X.\n",
rombanknr,Z80_REG.W.PC);
#endif
return;
}
rambanknr=value&3;
if (rambanknr>=sramselectmask) {
fprintf(OUTSTREAM,"Fixme: RAM bank %d selected.\n",rambanknr);
fprintf(OUTSTREAM," (written %02X to %04X)\n",value,addr);
return;
}
#ifdef DEBUG
fprintf(OUTSTREAM,"RAM bank %d selected.\n",rambanknr);
#endif
return;
case 3: /* MBC mode select */
if ((value&0xF0)>0) return;
if (mbctype==2) {
fprintf(OUTSTREAM,"Fixme: MBC2 and selecting modes ?\n");
fprintf(OUTSTREAM," (at PC: %04X written %02X to %04X)\n",
Z80_REG.W.PC,value,addr);
return;
}
if (mbctype==5) { /* clock counter latch */
#ifdef DEBUG
fprintf(OUTSTREAM,"Fixme: MBC5 clock funcs not implemented (%04X).\n",
Z80_REG.W.PC);
#endif
return;
}
mbcmode=value & 1;
#ifdef DEBUG
fprintf(OUTSTREAM,"MBC mode %s selected.\n",mbcmode == 0 ? "16/8" : "4/32");
#endif
return;
case 4: /* VRAM access */
/* ************************************** */
/* *** NOT CHECKING IF ACCESSIBLE !!! *** */
/* ************************************** */
/* fprintf(OUTSTREAM,"VRAM access: %04X at PC %04X\n",addr,Z80_REG.W.PC); */
vram[VBK&1][addr & 0x1FFF]=value;
return;
case 5: /* SRAM access */
value=ar_checkwrite(addr,value);
if (rambankenabled!=0)
sram[rambanknr][addr & 0x1FFF]=value;
return;
case 6: /* internal RAM and OAM */
case 7: /* w/o IO (0xFF00-) */
if (addr<0xFE00) {
/* internal RAM with echoing ! */
value=ar_checkwrite((addr&0x1FFF)+0xC000,value);
value=ar_checkwrite((addr&0x1FFF)+0xE000,value);
switch (addr>>12) {
case 0xC:
case 0xE:
iram[0][addr & 0x0FFF]=value;
break;
case 0xD:
case 0xF:
iram[workbanknr][addr & 0x0FFF]=value;
break;
}
} else {
/* OAM access */
/* ************************************** */
/* *** NOT CHECKING IF ACCESSIBLE !!! *** */
/* *** THIS COULD BE FATAL !!! *** */
/* ************************************** */
oam[addr & 0xFF]=value;
}
return;
}
}
}
void switch2color(void)
{
int k,l;
if (force_stdgameboy) return;
switch (bitmapbits) {
case 8:
scanline=cgb_scanline8;
break;
case 16:
scanline=cgb_scanline16;
break;
case 32:
scanline=cgb_scanline32;
break;
default:
printf("Error: Cannot handle resolution %i, yet.\n",bitmapbits);
exit(1);
}
for (k=0;k<8;k++)
for (l=0;l<4;l++) {
BGPAL[k][l]= color_translate(GB_BGPAL[k][l] );
OBJPAL[k][l]=color_translate(GB_OBJPAL[k][l]);
}
#ifndef DOS
fprintf(OUTSTREAM,"Gameboy COLOR rom detected.\n");
#endif
colormode=1;
}
/* for games with battery: saving routine */
void savestate(void)
{
FILE *fp;
int j,zeros,b;
fprintf(OUTSTREAM,"Saving state to %s ... ",sramfile);
fp=fopen(sramfile,"wb");
if (fp) {
zeros=0;j=0;
while (j<sramsize) {
b=sram[j / srambanksize][j % srambanksize];
if (b==0) zeros++;
else {
while (zeros) { fputc(0,fp);zeros--;}
fputc(b,fp);
}
j++;
}
fclose(fp);
if (zeros==sramsize) {
fprintf(OUTSTREAM,"(empty) ");
if (unlink(sramfile)) fprintf(OUTSTREAM,"erase FAILED, ");
}
fprintf(OUTSTREAM,"done.\n");
} else {
fprintf(OUTSTREAM,"ERROR\n");
}
}
void tidyup(void)
{
unsigned int i;
donesys();
fprintf(OUTSTREAM,"Freeing resources ... ");
for (i=0;i<GB_WORKBANKCOUNT;i++) {