-
Notifications
You must be signed in to change notification settings - Fork 8
/
io.c
767 lines (676 loc) · 28.3 KB
/
io.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
#include "arm.h"
#include "dma.h"
#include "io.h"
#include "sound.h"
#include "timer.h"
uint8_t io_read(uint32_t address) {
io_open_bus = false;
switch (address) {
case 0x04000000: return disp_cnt.b.b0 & 0xff;
case 0x04000001: return disp_cnt.b.b1 & 0xff;
case 0x04000002: return green_inv.b.b0 & 0x01;
case 0x04000003: return green_inv.b.b1 & 0x00;
case 0x04000004: return disp_stat.b.b0 & 0xff;
case 0x04000005: return disp_stat.b.b1 & 0xff;
case 0x04000006: return v_count.b.b0 & 0xff;
case 0x04000007: return v_count.b.b1 & 0x00;
case 0x04000008: return bg[0].ctrl.b.b0 & 0xff;
case 0x04000009: return bg[0].ctrl.b.b1 & 0xdf;
case 0x0400000a: return bg[1].ctrl.b.b0 & 0xff;
case 0x0400000b: return bg[1].ctrl.b.b1 & 0xdf;
case 0x0400000c: return bg[2].ctrl.b.b0 & 0xff;
case 0x0400000d: return bg[2].ctrl.b.b1 & 0xff;
case 0x0400000e: return bg[3].ctrl.b.b0 & 0xff;
case 0x0400000f: return bg[3].ctrl.b.b1 & 0xff;
case 0x04000048: return win_in.b.b0 & 0x3f;
case 0x04000049: return win_in.b.b1 & 0x3f;
case 0x0400004a: return win_out.b.b0 & 0x3f;
case 0x0400004b: return win_out.b.b1 & 0x3f;
case 0x04000050: return bld_cnt.b.b0 & 0xff;
case 0x04000051: return bld_cnt.b.b1 & 0x3f;
case 0x04000052: return bld_alpha.b.b0 & 0x1f;
case 0x04000053: return bld_alpha.b.b1 & 0x1f;
case 0x04000060: return sqr_ch[0].sweep.b.b0 & 0x7f;
case 0x04000061: return sqr_ch[0].sweep.b.b1 & 0x00;
case 0x04000062: return sqr_ch[0].tone.b.b0 & 0xc0;
case 0x04000063: return sqr_ch[0].tone.b.b1 & 0xff;
case 0x04000064: return sqr_ch[0].ctrl.b.b0 & 0x00;
case 0x04000065: return sqr_ch[0].ctrl.b.b1 & 0x40;
case 0x04000066: return sqr_ch[0].ctrl.b.b2 & 0x00;
case 0x04000067: return sqr_ch[0].ctrl.b.b3 & 0x00;
case 0x04000068: return sqr_ch[1].tone.b.b0 & 0xc0;
case 0x04000069: return sqr_ch[1].tone.b.b1 & 0xff;
case 0x0400006c: return sqr_ch[1].ctrl.b.b0 & 0x00;
case 0x0400006d: return sqr_ch[1].ctrl.b.b1 & 0x40;
case 0x0400006e: return sqr_ch[1].ctrl.b.b2 & 0x00;
case 0x0400006f: return sqr_ch[1].ctrl.b.b3 & 0x00;
case 0x04000070: return wave_ch.wave.b.b0 & 0xe0;
case 0x04000071: return wave_ch.wave.b.b1 & 0x00;
case 0x04000072: return wave_ch.volume.b.b0 & 0x00;
case 0x04000073: return wave_ch.volume.b.b1 & 0xe0;
case 0x04000074: return wave_ch.ctrl.b.b0 & 0x00;
case 0x04000075: return wave_ch.ctrl.b.b1 & 0x40;
case 0x04000076: return wave_ch.ctrl.b.b2 & 0x00;
case 0x04000077: return wave_ch.ctrl.b.b3 & 0x00;
case 0x04000078: return noise_ch.env.b.b0 & 0x00;
case 0x04000079: return noise_ch.env.b.b1 & 0xff;
case 0x0400007a: return noise_ch.env.b.b2 & 0x00;
case 0x0400007b: return noise_ch.env.b.b3 & 0x00;
case 0x0400007c: return noise_ch.ctrl.b.b0 & 0xff;
case 0x0400007d: return noise_ch.ctrl.b.b1 & 0x40;
case 0x0400007e: return noise_ch.ctrl.b.b2 & 0x00;
case 0x0400007f: return noise_ch.ctrl.b.b3 & 0x00;
case 0x04000080: return snd_psg_vol.b.b0 & 0x77;
case 0x04000081: return snd_psg_vol.b.b1 & 0xff;
case 0x04000082: return snd_pcm_vol.b.b0 & 0x0f;
case 0x04000083: return snd_pcm_vol.b.b1 & 0x77;
case 0x04000084: return snd_psg_enb.b.b0 & 0x8f;
case 0x04000085: return snd_psg_enb.b.b1 & 0x00;
case 0x04000086: return snd_psg_enb.b.b2 & 0x00;
case 0x04000087: return snd_psg_enb.b.b3 & 0x00;
case 0x04000088: return snd_bias.b.b0 & 0xff;
case 0x04000089: return snd_bias.b.b1 & 0xc3;
case 0x0400008a: return snd_bias.b.b2 & 0x00;
case 0x0400008b: return snd_bias.b.b3 & 0x00;
case 0x04000090:
case 0x04000091:
case 0x04000092:
case 0x04000093:
case 0x04000094:
case 0x04000095:
case 0x04000096:
case 0x04000097:
case 0x04000098:
case 0x04000099:
case 0x0400009a:
case 0x0400009b:
case 0x0400009c:
case 0x0400009d:
case 0x0400009e:
case 0x0400009f: {
uint8_t wave_bank = (wave_ch.wave.w >> 2) & 0x10;
uint8_t wave_idx = (wave_bank ^ 0x10) | (address & 0xf);
return wave_ram[wave_idx];
}
case 0x040000b8: return dma_ch[0].count.b.b0 & 0x00;
case 0x040000b9: return dma_ch[0].count.b.b1 & 0x00;
case 0x040000ba: return dma_ch[0].ctrl.b.b0 & 0xe0;
case 0x040000bb: return dma_ch[0].ctrl.b.b1 & 0xf7;
case 0x040000c4: return dma_ch[1].count.b.b0 & 0x00;
case 0x040000c5: return dma_ch[1].count.b.b1 & 0x00;
case 0x040000c6: return dma_ch[1].ctrl.b.b0 & 0xe0;
case 0x040000c7: return dma_ch[1].ctrl.b.b1 & 0xf7;
case 0x040000d0: return dma_ch[2].count.b.b0 & 0x00;
case 0x040000d1: return dma_ch[2].count.b.b1 & 0x00;
case 0x040000d2: return dma_ch[2].ctrl.b.b0 & 0xe0;
case 0x040000d3: return dma_ch[2].ctrl.b.b1 & 0xf7;
case 0x040000dc: return dma_ch[3].count.b.b0 & 0x00;
case 0x040000dd: return dma_ch[3].count.b.b1 & 0x00;
case 0x040000de: return dma_ch[3].ctrl.b.b0 & 0xe0;
case 0x040000df: return dma_ch[3].ctrl.b.b1 & 0xff;
case 0x04000100: return tmr[0].count.b.b0 & 0xff;
case 0x04000101: return tmr[0].count.b.b1 & 0xff;
case 0x04000102: return tmr[0].ctrl.b.b0 & 0xc7;
case 0x04000103: return tmr[0].ctrl.b.b1 & 0x00;
case 0x04000104: return tmr[1].count.b.b0 & 0xff;
case 0x04000105: return tmr[1].count.b.b1 & 0xff;
case 0x04000106: return tmr[1].ctrl.b.b0 & 0xc7;
case 0x04000107: return tmr[1].ctrl.b.b1 & 0x00;
case 0x04000108: return tmr[2].count.b.b0 & 0xff;
case 0x04000109: return tmr[2].count.b.b1 & 0xff;
case 0x0400010a: return tmr[2].ctrl.b.b0 & 0xc7;
case 0x0400010b: return tmr[2].ctrl.b.b1 & 0x00;
case 0x0400010c: return tmr[3].count.b.b0 & 0xff;
case 0x0400010d: return tmr[3].count.b.b1 & 0xff;
case 0x0400010e: return tmr[3].ctrl.b.b0 & 0xc7;
case 0x0400010f: return tmr[3].ctrl.b.b1 & 0x00;
case 0x04000120: return sio_data32.b.b0 & 0xff;
case 0x04000121: return sio_data32.b.b1 & 0xff;
case 0x04000122: return sio_data32.b.b2 & 0xff;
case 0x04000123: return sio_data32.b.b3 & 0xff;
case 0x04000128: return sio_cnt.b.b0 & 0xff;
case 0x04000129: return sio_cnt.b.b1 & 0xff;
case 0x0400012a: return sio_data8.b.b0 & 0xff;
case 0x04000134: return r_cnt.b.b0 & 0xff;
case 0x04000135: return r_cnt.b.b1 & 0xff;
case 0x04000130: return key_input.b.b0 & 0xff;
case 0x04000131: return key_input.b.b1 & 0x3f;
case 0x04000200: return int_enb.b.b0 & 0xff;
case 0x04000201: return int_enb.b.b1 & 0x3f;
case 0x04000202: return int_ack.b.b0 & 0xff;
case 0x04000203: return int_ack.b.b1 & 0x3f;
case 0x04000204: return wait_cnt.b.b0 & 0xff;
case 0x04000205: return wait_cnt.b.b1 & 0xdf;
case 0x04000206: return wait_cnt.b.b2 & 0x00;
case 0x04000207: return wait_cnt.b.b3 & 0x00;
case 0x04000208: return int_enb_m.b.b0 & 0x01;
case 0x04000209: return int_enb_m.b.b1 & 0x00;
case 0x0400020a: return int_enb_m.b.b2 & 0x00;
case 0x0400020b: return int_enb_m.b.b3 & 0x00;
case 0x04000300: return post_boot & 0x01;
case 0x04000301: return int_halt & 0x00;
}
io_open_bus = true;
return 0;
}
static void dma_load(uint8_t ch, uint8_t value) {
uint8_t old = dma_ch[ch].ctrl.b.b1;
dma_ch[ch].ctrl.b.b1 = value;
if ((old ^ value) & value & 0x80) {
dma_dst_addr[ch] = dma_ch[ch].dst.w;
dma_src_addr[ch] = dma_ch[ch].src.w;
if (dma_ch[ch].ctrl.w & DMA_32) {
dma_dst_addr[ch] &= ~3;
dma_src_addr[ch] &= ~3;
} else {
dma_dst_addr[ch] &= ~1;
dma_src_addr[ch] &= ~1;
}
dma_count[ch] = dma_ch[ch].count.w;
dma_transfer(IMMEDIATELY);
}
}
static void tmr_load(uint8_t idx, uint8_t value) {
uint8_t old = tmr[idx].ctrl.b.b0;
tmr[idx].ctrl.b.b0 = value;
if (value & TMR_ENB)
tmr_enb |= (1 << idx);
else
tmr_enb &= ~(1 << idx);
if ((old ^ value) & value & TMR_ENB) {
tmr[idx].count.w = tmr[idx].reload.w;
tmr_icnt[idx] = 0;
}
}
static void snd_reset_state(uint8_t ch, bool enb) {
if (enb) {
snd_ch_state[ch].phase = false;
snd_ch_state[ch].samples = 0;
snd_ch_state[ch].length_time = 0;
snd_ch_state[ch].sweep_time = 0;
snd_ch_state[ch].env_time = 0;
if (ch == 2) wave_reset();
if (ch == 3) {
if (noise_ch.ctrl.w & NOISE_7)
snd_ch_state[ch].lfsr = 0x007f;
else
snd_ch_state[ch].lfsr = 0x7fff;
}
snd_psg_enb.w |= (1 << ch);
}
}
void io_write(uint32_t address, uint8_t value) {
switch (address) {
case 0x04000000:
if (arm_r.r[15] >= 0x4000) {
//The CGB mode enable bit 3 can only be set by the bios
value &= 0xf7;
}
disp_cnt.b.b0 = value;
break;
case 0x04000001: disp_cnt.b.b1 = value; break;
case 0x04000002: green_inv.b.b0 = value; break;
case 0x04000003: green_inv.b.b1 = value; break;
case 0x04000004:
disp_stat.b.b0 &= 0x47;
disp_stat.b.b0 |= value & ~0x47;
break;
case 0x04000005: disp_stat.b.b1 = value; break;
case 0x04000008: bg[0].ctrl.b.b0 = value; break;
case 0x04000009: bg[0].ctrl.b.b1 = value; break;
case 0x0400000a: bg[1].ctrl.b.b0 = value; break;
case 0x0400000b: bg[1].ctrl.b.b1 = value; break;
case 0x0400000c: bg[2].ctrl.b.b0 = value; break;
case 0x0400000d: bg[2].ctrl.b.b1 = value; break;
case 0x0400000e: bg[3].ctrl.b.b0 = value; break;
case 0x0400000f: bg[3].ctrl.b.b1 = value; break;
case 0x04000010: bg[0].xofs.b.b0 = value; break;
case 0x04000011: bg[0].xofs.b.b1 = value; break;
case 0x04000012: bg[0].yofs.b.b0 = value; break;
case 0x04000013: bg[0].yofs.b.b1 = value; break;
case 0x04000014: bg[1].xofs.b.b0 = value; break;
case 0x04000015: bg[1].xofs.b.b1 = value; break;
case 0x04000016: bg[1].yofs.b.b0 = value; break;
case 0x04000017: bg[1].yofs.b.b1 = value; break;
case 0x04000018: bg[2].xofs.b.b0 = value; break;
case 0x04000019: bg[2].xofs.b.b1 = value; break;
case 0x0400001a: bg[2].yofs.b.b0 = value; break;
case 0x0400001b: bg[2].yofs.b.b1 = value; break;
case 0x0400001c: bg[3].xofs.b.b0 = value; break;
case 0x0400001d: bg[3].xofs.b.b1 = value; break;
case 0x0400001e: bg[3].yofs.b.b0 = value; break;
case 0x0400001f: bg[3].yofs.b.b1 = value; break;
case 0x04000020: bg_pa[2].b.b0 = value; break;
case 0x04000021: bg_pa[2].b.b1 = value; break;
case 0x04000022: bg_pb[2].b.b0 = value; break;
case 0x04000023: bg_pb[2].b.b1 = value; break;
case 0x04000024: bg_pc[2].b.b0 = value; break;
case 0x04000025: bg_pc[2].b.b1 = value; break;
case 0x04000026: bg_pd[2].b.b0 = value; break;
case 0x04000027: bg_pd[2].b.b1 = value; break;
case 0x04000028:
bg_refxe[2].b.b0 =
bg_refxi[2].b.b0 = value;
break;
case 0x04000029:
bg_refxe[2].b.b1 =
bg_refxi[2].b.b1 = value;
break;
case 0x0400002a:
bg_refxe[2].b.b2 =
bg_refxi[2].b.b2 = value;
break;
case 0x0400002b:
bg_refxe[2].b.b3 =
bg_refxi[2].b.b3 = value;
break;
case 0x0400002c:
bg_refye[2].b.b0 =
bg_refyi[2].b.b0 = value;
break;
case 0x0400002d:
bg_refye[2].b.b1 =
bg_refyi[2].b.b1 = value;
break;
case 0x0400002e:
bg_refye[2].b.b2 =
bg_refyi[2].b.b2 = value;
break;
case 0x0400002f:
bg_refye[2].b.b3 =
bg_refyi[2].b.b3 = value;
break;
case 0x04000030: bg_pa[3].b.b0 = value; break;
case 0x04000031: bg_pa[3].b.b1 = value; break;
case 0x04000032: bg_pb[3].b.b0 = value; break;
case 0x04000033: bg_pb[3].b.b1 = value; break;
case 0x04000034: bg_pc[3].b.b0 = value; break;
case 0x04000035: bg_pc[3].b.b1 = value; break;
case 0x04000036: bg_pd[3].b.b0 = value; break;
case 0x04000037: bg_pd[3].b.b1 = value; break;
case 0x04000038:
bg_refxe[3].b.b0 =
bg_refxi[3].b.b0 = value;
break;
case 0x04000039:
bg_refxe[3].b.b1 =
bg_refxi[3].b.b1 = value;
break;
case 0x0400003a:
bg_refxe[3].b.b2 =
bg_refxi[3].b.b2 = value;
break;
case 0x0400003b:
bg_refxe[3].b.b3 =
bg_refxi[3].b.b3 = value;
break;
case 0x0400003c:
bg_refye[3].b.b0 =
bg_refyi[3].b.b0 = value;
break;
case 0x0400003d:
bg_refye[3].b.b1 =
bg_refyi[3].b.b1 = value;
break;
case 0x0400003e:
bg_refye[3].b.b2 =
bg_refyi[3].b.b2 = value;
break;
case 0x0400003f:
bg_refye[3].b.b3 =
bg_refyi[3].b.b3 = value;
break;
case 0x04000048: win_in.b.b0 = value; break;
case 0x04000049: win_in.b.b1 = value; break;
case 0x0400004a: win_out.b.b0 = value; break;
case 0x0400004b: win_out.b.b1 = value; break;
case 0x04000050: bld_cnt.b.b0 = value; break;
case 0x04000051: bld_cnt.b.b1 = value; break;
case 0x04000052: bld_alpha.b.b0 = value; break;
case 0x04000053: bld_alpha.b.b1 = value; break;
case 0x04000054: bld_bright.b.b0 = value; break;
case 0x04000055: bld_bright.b.b1 = value; break;
case 0x04000056: bld_bright.b.b2 = value; break;
case 0x04000057: bld_bright.b.b3 = value; break;
case 0x04000060:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[0].sweep.b.b0 = value;
break;
case 0x04000061:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[0].sweep.b.b1 = value;
break;
case 0x04000062:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[0].tone.b.b0 = value;
break;
case 0x04000063:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[0].tone.b.b1 = value;
break;
case 0x04000064:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[0].ctrl.b.b0 = value;
break;
case 0x04000065:
if (snd_psg_enb.w & PSG_ENB) {
sqr_ch[0].ctrl.b.b1 = value;
snd_reset_state(0, value & 0x80);
}
break;
case 0x04000066:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[0].ctrl.b.b2 = value;
break;
case 0x04000067:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[0].ctrl.b.b3 = value;
break;
case 0x04000068:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[1].tone.b.b0 = value;
break;
case 0x04000069:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[1].tone.b.b1 = value;
break;
case 0x0400006c:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[1].ctrl.b.b0 = value;
break;
case 0x0400006d:
if (snd_psg_enb.w & PSG_ENB) {
sqr_ch[1].ctrl.b.b1 = value;
snd_reset_state(1, value & 0x80);
}
break;
case 0x0400006e:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[1].ctrl.b.b2 = value;
break;
case 0x0400006f:
if (snd_psg_enb.w & PSG_ENB)
sqr_ch[1].ctrl.b.b3 = value;
break;
case 0x04000070:
if (snd_psg_enb.w & PSG_ENB)
wave_ch.wave.b.b0 = value;
break;
case 0x04000071:
if (snd_psg_enb.w & PSG_ENB)
wave_ch.wave.b.b1 = value;
break;
case 0x04000072:
if (snd_psg_enb.w & PSG_ENB)
wave_ch.volume.b.b0 = value;
break;
case 0x04000073:
if (snd_psg_enb.w & PSG_ENB)
wave_ch.volume.b.b1 = value;
break;
case 0x04000074:
if (snd_psg_enb.w & PSG_ENB)
wave_ch.ctrl.b.b0 = value;
break;
case 0x04000075:
if (snd_psg_enb.w & PSG_ENB) {
wave_ch.ctrl.b.b1 = value;
snd_reset_state(2, value & 0x80);
}
break;
case 0x04000076:
if (snd_psg_enb.w & PSG_ENB)
wave_ch.ctrl.b.b2 = value;
break;
case 0x04000077:
if (snd_psg_enb.w & PSG_ENB)
wave_ch.ctrl.b.b3 = value;
break;
case 0x04000078:
if (snd_psg_enb.w & PSG_ENB)
noise_ch.env.b.b0 = value;
break;
case 0x04000079:
if (snd_psg_enb.w & PSG_ENB)
noise_ch.env.b.b1 = value;
break;
case 0x0400007a:
if (snd_psg_enb.w & PSG_ENB)
noise_ch.env.b.b2 = value;
break;
case 0x0400007b:
if (snd_psg_enb.w & PSG_ENB)
noise_ch.env.b.b3 = value;
break;
case 0x0400007c:
if (snd_psg_enb.w & PSG_ENB)
noise_ch.ctrl.b.b0 = value;
break;
case 0x0400007d:
if (snd_psg_enb.w & PSG_ENB) {
noise_ch.ctrl.b.b1 = value;
snd_reset_state(3, value & 0x80);
}
break;
case 0x0400007e:
if (snd_psg_enb.w & PSG_ENB)
noise_ch.ctrl.b.b2 = value;
break;
case 0x0400007f:
if (snd_psg_enb.w & PSG_ENB)
noise_ch.ctrl.b.b3 = value;
break;
case 0x04000080:
if (snd_psg_enb.w & PSG_ENB)
snd_psg_vol.b.b0 = value;
break;
case 0x04000081:
if (snd_psg_enb.w & PSG_ENB)
snd_psg_vol.b.b1 = value;
break;
case 0x04000082:
//PCM is not affected by the PSG Enable flag
snd_pcm_vol.b.b0 = value;
break;
case 0x04000083:
snd_pcm_vol.b.b1 = value;
if (value & 0x08) fifo_a_len = 0;
if (value & 0x80) fifo_b_len = 0;
break;
case 0x04000084:
snd_psg_enb.b.b0 &= 0xf;
snd_psg_enb.b.b0 |= value & ~0xf;
if (!(value & PSG_ENB)) {
sqr_ch[0].sweep.w = 0;
sqr_ch[0].tone.w = 0;
sqr_ch[0].ctrl.w = 0;
sqr_ch[1].tone.w = 0;
sqr_ch[1].ctrl.w = 0;
wave_ch.wave.w = 0;
wave_ch.volume.w = 0;
wave_ch.ctrl.w = 0;
noise_ch.env.w = 0;
noise_ch.ctrl.w = 0;
snd_psg_vol.w = 0;
snd_psg_enb.w = 0;
}
break;
case 0x04000085: snd_psg_enb.b.b1 = value; break;
case 0x04000086: snd_psg_enb.b.b2 = value; break;
case 0x04000087: snd_psg_enb.b.b3 = value; break;
case 0x04000088: snd_bias.b.b0 = value; break;
case 0x04000089: snd_bias.b.b1 = value; break;
case 0x0400008a: snd_bias.b.b2 = value; break;
case 0x0400008b: snd_bias.b.b3 = value; break;
case 0x04000090:
case 0x04000091:
case 0x04000092:
case 0x04000093:
case 0x04000094:
case 0x04000095:
case 0x04000096:
case 0x04000097:
case 0x04000098:
case 0x04000099:
case 0x0400009a:
case 0x0400009b:
case 0x0400009c:
case 0x0400009d:
case 0x0400009e:
case 0x0400009f: {
uint8_t wave_bank = (wave_ch.wave.w >> 2) & 0x10;
uint8_t wave_idx = (wave_bank ^ 0x10) | (address & 0xf);
wave_ram[wave_idx] = value;
}
break;
case 0x040000a0: snd_fifo_a_0 = value; break;
case 0x040000a1: snd_fifo_a_1 = value; break;
case 0x040000a2: snd_fifo_a_2 = value; break;
case 0x040000a3: snd_fifo_a_3 = value; break;
case 0x040000a4: snd_fifo_b_0 = value; break;
case 0x040000a5: snd_fifo_b_1 = value; break;
case 0x040000a6: snd_fifo_b_2 = value; break;
case 0x040000a7: snd_fifo_b_3 = value; break;
case 0x040000b0: dma_ch[0].src.b.b0 = value; break;
case 0x040000b1: dma_ch[0].src.b.b1 = value; break;
case 0x040000b2: dma_ch[0].src.b.b2 = value; break;
case 0x040000b3: dma_ch[0].src.b.b3 = value; break;
case 0x040000b4: dma_ch[0].dst.b.b0 = value; break;
case 0x040000b5: dma_ch[0].dst.b.b1 = value; break;
case 0x040000b6: dma_ch[0].dst.b.b2 = value; break;
case 0x040000b7: dma_ch[0].dst.b.b3 = value; break;
case 0x040000b8: dma_ch[0].count.b.b0 = value; break;
case 0x040000b9: dma_ch[0].count.b.b1 = value; break;
case 0x040000ba: dma_ch[0].ctrl.b.b0 = value; break;
case 0x040000bb: dma_load(0, value); break;
case 0x040000bc: dma_ch[1].src.b.b0 = value; break;
case 0x040000bd: dma_ch[1].src.b.b1 = value; break;
case 0x040000be: dma_ch[1].src.b.b2 = value; break;
case 0x040000bf: dma_ch[1].src.b.b3 = value; break;
case 0x040000c0: dma_ch[1].dst.b.b0 = value; break;
case 0x040000c1: dma_ch[1].dst.b.b1 = value; break;
case 0x040000c2: dma_ch[1].dst.b.b2 = value; break;
case 0x040000c3: dma_ch[1].dst.b.b3 = value; break;
case 0x040000c4: dma_ch[1].count.b.b0 = value; break;
case 0x040000c5: dma_ch[1].count.b.b1 = value; break;
case 0x040000c6: dma_ch[1].ctrl.b.b0 = value; break;
case 0x040000c7: dma_load(1, value); break;
case 0x040000c8: dma_ch[2].src.b.b0 = value; break;
case 0x040000c9: dma_ch[2].src.b.b1 = value; break;
case 0x040000ca: dma_ch[2].src.b.b2 = value; break;
case 0x040000cb: dma_ch[2].src.b.b3 = value; break;
case 0x040000cc: dma_ch[2].dst.b.b0 = value; break;
case 0x040000cd: dma_ch[2].dst.b.b1 = value; break;
case 0x040000ce: dma_ch[2].dst.b.b2 = value; break;
case 0x040000cf: dma_ch[2].dst.b.b3 = value; break;
case 0x040000d0: dma_ch[2].count.b.b0 = value; break;
case 0x040000d1: dma_ch[2].count.b.b1 = value; break;
case 0x040000d2: dma_ch[2].ctrl.b.b0 = value; break;
case 0x040000d3: dma_load(2, value); break;
case 0x040000d4: dma_ch[3].src.b.b0 = value; break;
case 0x040000d5: dma_ch[3].src.b.b1 = value; break;
case 0x040000d6: dma_ch[3].src.b.b2 = value; break;
case 0x040000d7: dma_ch[3].src.b.b3 = value; break;
case 0x040000d8: dma_ch[3].dst.b.b0 = value; break;
case 0x040000d9: dma_ch[3].dst.b.b1 = value; break;
case 0x040000da: dma_ch[3].dst.b.b2 = value; break;
case 0x040000db: dma_ch[3].dst.b.b3 = value; break;
case 0x040000dc: dma_ch[3].count.b.b0 = value; break;
case 0x040000dd: dma_ch[3].count.b.b1 = value; break;
case 0x040000de: dma_ch[3].ctrl.b.b0 = value; break;
case 0x040000df: dma_load(3, value); break;
case 0x04000100: tmr[0].reload.b.b0 = value; break;
case 0x04000101: tmr[0].reload.b.b1 = value; break;
case 0x04000102: tmr_load(0, value); break;
case 0x04000103: tmr[0].ctrl.b.b1 = value; break;
case 0x04000104: tmr[1].reload.b.b0 = value; break;
case 0x04000105: tmr[1].reload.b.b1 = value; break;
case 0x04000106: tmr_load(1, value); break;
case 0x04000107: tmr[1].ctrl.b.b1 = value; break;
case 0x04000108: tmr[2].reload.b.b0 = value; break;
case 0x04000109: tmr[2].reload.b.b1 = value; break;
case 0x0400010a: tmr_load(2, value); break;
case 0x0400010b: tmr[2].ctrl.b.b1 = value; break;
case 0x0400010c: tmr[3].reload.b.b0 = value; break;
case 0x0400010d: tmr[3].reload.b.b1 = value; break;
case 0x0400010e: tmr_load(3, value); break;
case 0x0400010f: tmr[3].ctrl.b.b1 = value; break;
case 0x04000120: sio_data32.b.b0 = value; break;
case 0x04000121: sio_data32.b.b1 = value; break;
case 0x04000122: sio_data32.b.b2 = value; break;
case 0x04000123: sio_data32.b.b3 = value; break;
case 0x04000128: sio_cnt.b.b0 = value; break;
case 0x04000129: sio_cnt.b.b1 = value; break;
case 0x0400012a: sio_data8.b.b0 = value; break;
case 0x04000134: r_cnt.b.b0 = value; break;
case 0x04000135: r_cnt.b.b1 = value; break;
case 0x04000200:
int_enb.b.b0 = value;
arm_check_irq();
break;
case 0x04000201:
int_enb.b.b1 = value;
arm_check_irq();
break;
case 0x04000202: int_ack.b.b0 &= ~value; break;
case 0x04000203: int_ack.b.b1 &= ~value; break;
case 0x04000204:
wait_cnt.b.b0 = value;
update_ws();
break;
case 0x04000205:
wait_cnt.b.b1 = value;
update_ws();
break;
case 0x04000206:
wait_cnt.b.b2 = value;
update_ws();
break;
case 0x04000207:
wait_cnt.b.b3 = value;
update_ws();
break;
case 0x04000208:
int_enb_m.b.b0 = value;
arm_check_irq();
break;
case 0x04000209:
int_enb_m.b.b1 = value;
arm_check_irq();
break;
case 0x0400020a:
int_enb_m.b.b2 = value;
arm_check_irq();
break;
case 0x0400020b:
int_enb_m.b.b3 = value;
arm_check_irq();
break;
case 0x04000300: post_boot = value; break;
case 0x04000301: int_halt = true; break;
}
}
void trigger_irq(uint16_t flag) {
int_ack.w |= flag;
int_halt = false;
arm_check_irq();
}
static const uint8_t ws0_s_lut[2] = { 2, 1 };
static const uint8_t ws1_s_lut[2] = { 4, 1 };
static const uint8_t ws2_s_lut[2] = { 8, 1 };
static const uint8_t ws_n_lut[4] = { 4, 3, 2, 8 };
void update_ws() {
ws_n[0] = ws_n_lut[(wait_cnt.w >> 2) & 3];
ws_n[1] = ws_n_lut[(wait_cnt.w >> 5) & 3];
ws_n[2] = ws_n_lut[(wait_cnt.w >> 8) & 3];
ws_s[0] = ws0_s_lut[(wait_cnt.w >> 4) & 1];
ws_s[1] = ws1_s_lut[(wait_cnt.w >> 7) & 1];
ws_s[2] = ws2_s_lut[(wait_cnt.w >> 10) & 1];
int8_t i;
for (i = 0; i < 3; i++) {
ws_n_t16[i] = ws_n[i] + 1;
ws_s_t16[i] = ws_s[i] + 1;
ws_n_arm[i] = ws_n_t16[i] + ws_s_t16[i];
ws_s_arm[i] = ws_s_t16[i] << 1;
}
}