-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_boy.py
More file actions
2745 lines (2399 loc) · 84.4 KB
/
game_boy.py
File metadata and controls
2745 lines (2399 loc) · 84.4 KB
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
"""
game_boy.py -- Nintendo Game Boy (DMG) system definition.
Complete Game Boy using the proto framework with ALL code transpiled
from Python. Features:
- Sharp SM83 CPU with all 500 opcodes transpiled via @cpu.opcode()
- Full 64KB memory map with MBC1/3/5 banking
- PPU scanline renderer (BG + window + sprites) -- transpiled
- APU 4-channel audio (2 square, 1 wave, 1 noise) -- transpiled
- Timer with DIV/TIMA/TMA/TAC -- transpiled
- Joypad input -- transpiled
- External hooks: render_frame(), audio_push(), poll_input()
- RegisterBlock handlers -- transpiled via @block.on_read/@block.on_write
- MBC handlers -- transpiled via @mbc.bank_resolver/@mbc.on_write
- Step preamble (interrupt dispatch) -- transpiled
- Tick handlers -- transpiled
"""
import os
import sys as _sys
from proto import (
MemoryRegion, MemoryBank, MemoryBus, MemoryController,
MemoryAccessLevel, Handler, HandlerType,
Clock, Chip, Board, RegisterBlock, CPUDefinition,
BoardCodeGenerator,
)
# ===================================================================
# Clock
# ===================================================================
master_clock = Clock("master", 4_194_304)
# ===================================================================
# PPU Chip
# ===================================================================
ppu_chip = Chip("ppu", clock=master_clock, comment="Pixel Processing Unit")
# PPU state
ppu_chip.add_state("dot_counter", "uint32_t", "0", "Dot counter within line")
ppu_chip.add_state("mode", "uint8_t", "2", "PPU mode (0-3)")
ppu_chip.add_state("frame_ready", "bool", "false", "Frame complete flag")
ppu_chip.add_state("line_dots", "uint32_t", "0", "Accumulated dots this line")
ppu_chip.add_state("show_bg", "bool", "true", "Debug: show background layer")
ppu_chip.add_state("show_sprites", "bool", "true", "Debug: show sprite layer")
# PPU internal memory
ppu_vram = MemoryRegion("vram", 8192, comment="Video RAM")
ppu_oam = MemoryRegion("oam", 160, comment="Object Attribute Memory")
ppu_chip.add_internal_memory(ppu_vram)
ppu_chip.add_internal_memory(ppu_oam)
# Framebuffer: 160x144 pixels, 1 byte per pixel (palette index) = 23040 bytes
ppu_fb = MemoryRegion("framebuffer", 23040, comment="160x144 pixel framebuffer")
ppu_chip.add_internal_memory(ppu_fb)
# PPU I/O registers (FF40-FF4B)
ppu_io = RegisterBlock("ppu_io", base_addr=0xFF40, size=12)
ppu_io.bind(0, "lcdc", default="0x91", comment="LCD Control")
ppu_io.bind(1, "stat", default="0", comment="LCD Status")
ppu_io.bind(2, "scy", comment="Scroll Y")
ppu_io.bind(3, "scx", comment="Scroll X")
ppu_io.bind(4, "ly", comment="LCD Y coordinate", read_only=True)
ppu_io.bind(5, "lyc", comment="LY Compare")
ppu_io.bind(6, "dma", comment="OAM DMA transfer", write_only=True)
ppu_io.bind(7, "bgp", default="0xFC", comment="BG Palette")
ppu_io.bind(8, "obp0", comment="Object Palette 0")
ppu_io.bind(9, "obp1", comment="Object Palette 1")
ppu_io.bind(10, "wy", comment="Window Y")
ppu_io.bind(11, "wx", comment="Window X")
# LY read handler (transpiled)
@ppu_io.on_read(4)
def ppu_ly_read(chip):
return chip.ly
# DMA write handler (transpiled)
@ppu_io.on_write(6)
def ppu_dma_write(chip, val):
chip.dma = val
src: uint16 = uint16(val << 8)
for i in range(160):
chip.oam[i] = mem_read(uint16(src + i))
ppu_chip.add_register_block(ppu_io)
# ===================================================================
# APU Chip
# ===================================================================
apu_chip = Chip("apu", clock=master_clock, comment="Audio Processing Unit")
# Channel 1 (square + sweep)
apu_chip.add_state("ch1_enabled", "bool", "false")
apu_chip.add_state("ch1_dac", "bool", "false")
apu_chip.add_state("ch1_length", "uint16_t", "0")
apu_chip.add_state("ch1_volume", "uint8_t", "0")
apu_chip.add_state("ch1_env_timer", "uint8_t", "0")
apu_chip.add_state("ch1_env_dir", "uint8_t", "0")
apu_chip.add_state("ch1_env_pace", "uint8_t", "0")
apu_chip.add_state("ch1_freq", "uint16_t", "0")
apu_chip.add_state("ch1_freq_timer", "int32_t", "0")
apu_chip.add_state("ch1_duty", "uint8_t", "0")
apu_chip.add_state("ch1_duty_pos", "uint8_t", "0")
apu_chip.add_state("ch1_length_en", "bool", "false")
apu_chip.add_state("ch1_sweep_pace", "uint8_t", "0")
apu_chip.add_state("ch1_sweep_dir", "uint8_t", "0")
apu_chip.add_state("ch1_sweep_step", "uint8_t", "0")
apu_chip.add_state("ch1_sweep_timer", "uint8_t", "0")
apu_chip.add_state("ch1_sweep_en", "bool", "false")
apu_chip.add_state("ch1_sweep_freq", "uint16_t", "0")
apu_chip.add_state("ch1_sweep_shadow", "uint16_t", "0")
apu_chip.add_state("ch1_sweep_enabled", "bool", "false")
# Channel 2 (square)
apu_chip.add_state("ch2_enabled", "bool", "false")
apu_chip.add_state("ch2_dac", "bool", "false")
apu_chip.add_state("ch2_length", "uint16_t", "0")
apu_chip.add_state("ch2_volume", "uint8_t", "0")
apu_chip.add_state("ch2_env_timer", "uint8_t", "0")
apu_chip.add_state("ch2_env_dir", "uint8_t", "0")
apu_chip.add_state("ch2_env_pace", "uint8_t", "0")
apu_chip.add_state("ch2_freq", "uint16_t", "0")
apu_chip.add_state("ch2_freq_timer", "int32_t", "0")
apu_chip.add_state("ch2_duty", "uint8_t", "0")
apu_chip.add_state("ch2_duty_pos", "uint8_t", "0")
apu_chip.add_state("ch2_length_en", "bool", "false")
# Channel 3 (wave)
apu_chip.add_state("ch3_enabled", "bool", "false")
apu_chip.add_state("ch3_dac", "bool", "false")
apu_chip.add_state("ch3_length", "uint16_t", "0")
apu_chip.add_state("ch3_volume_code", "uint8_t", "0")
apu_chip.add_state("ch3_freq", "uint16_t", "0")
apu_chip.add_state("ch3_freq_timer", "int32_t", "0")
apu_chip.add_state("ch3_sample_pos", "uint8_t", "0")
apu_chip.add_state("ch3_length_en", "bool", "false")
# Channel 4 (noise)
apu_chip.add_state("ch4_enabled", "bool", "false")
apu_chip.add_state("ch4_dac", "bool", "false")
apu_chip.add_state("ch4_length", "uint16_t", "0")
apu_chip.add_state("ch4_volume", "uint8_t", "0")
apu_chip.add_state("ch4_env_timer", "uint8_t", "0")
apu_chip.add_state("ch4_env_dir", "uint8_t", "0")
apu_chip.add_state("ch4_env_pace", "uint8_t", "0")
apu_chip.add_state("ch4_freq_timer", "int32_t", "0")
apu_chip.add_state("ch4_lfsr", "uint16_t", "0x7FFF")
apu_chip.add_state("ch4_width", "uint8_t", "0")
apu_chip.add_state("ch4_clock_shift", "uint8_t", "0")
apu_chip.add_state("ch4_divisor_code", "uint8_t", "0")
apu_chip.add_state("ch4_length_en", "bool", "false")
# Frame sequencer
apu_chip.add_state("frame_seq_counter", "int32_t", "0")
apu_chip.add_state("frame_seq_step", "uint8_t", "0")
apu_chip.add_state("apu_enabled", "bool", "true")
# Sample buffer and downsampling
apu_chip.add_state("sample_counter", "int32_t", "0")
apu_chip.add_state("sample_count", "uint32_t", "0")
apu_chip.add_state("sample_buffer", "int16_t[4096]", "0", "Stereo sample buffer (2048 pairs)")
# Debug mute flags
apu_chip.add_state("debug_ch1_mute", "bool", "false")
apu_chip.add_state("debug_ch2_mute", "bool", "false")
apu_chip.add_state("debug_ch3_mute", "bool", "false")
apu_chip.add_state("debug_ch4_mute", "bool", "false")
# Wave RAM (internal memory on APU chip)
wave_ram = MemoryRegion("wave_ram", 16, comment="Wave pattern RAM")
apu_chip.add_internal_memory(wave_ram)
# APU I/O registers (FF10-FF3F)
apu_io = RegisterBlock("apu_io", base_addr=0xFF10, size=48)
# NR10-NR14 (Channel 1)
apu_io.bind(0, "nr10", comment="Ch1 Sweep")
apu_io.bind(1, "nr11", comment="Ch1 Length/Duty")
apu_io.bind(2, "nr12", comment="Ch1 Volume/Envelope")
apu_io.bind(3, "nr13", comment="Ch1 Freq Low")
apu_io.bind(4, "nr14", comment="Ch1 Freq Hi/Control")
# NR21-NR24 (Channel 2)
apu_io.bind(6, "nr21", comment="Ch2 Length/Duty")
apu_io.bind(7, "nr22", comment="Ch2 Volume/Envelope")
apu_io.bind(8, "nr23", comment="Ch2 Freq Low")
apu_io.bind(9, "nr24", comment="Ch2 Freq Hi/Control")
# NR30-NR34 (Channel 3)
apu_io.bind(10, "nr30", comment="Ch3 DAC Enable")
apu_io.bind(11, "nr31", comment="Ch3 Length")
apu_io.bind(12, "nr32", comment="Ch3 Volume")
apu_io.bind(13, "nr33", comment="Ch3 Freq Low")
apu_io.bind(14, "nr34", comment="Ch3 Freq Hi/Control")
# NR41-NR44 (Channel 4)
apu_io.bind(16, "nr41", comment="Ch4 Length")
apu_io.bind(17, "nr42", comment="Ch4 Volume/Envelope")
apu_io.bind(18, "nr43", comment="Ch4 Polynomial Counter")
apu_io.bind(19, "nr44", comment="Ch4 Control")
# NR50-NR52
apu_io.bind(20, "nr50", comment="Master Volume")
apu_io.bind(21, "nr51", comment="Sound Panning")
apu_io.bind(22, "nr52", comment="Sound On/Off")
# NR52 read handler (transpiled)
@apu_io.on_read(22)
def apu_nr52_read(chip):
r: uint8 = chip.nr52 & 128
if chip.ch1_enabled:
r = r | 1
if chip.ch2_enabled:
r = r | 2
if chip.ch3_enabled:
r = r | 4
if chip.ch4_enabled:
r = r | 8
return uint8(r | 112)
# NR10 write (transpiled)
@apu_io.on_write(0)
def apu_nr10_write(chip, val):
chip.nr10 = val
chip.ch1_sweep_pace = (val >> 4) & 7
chip.ch1_sweep_dir = (val >> 3) & 1
chip.ch1_sweep_step = val & 7
# NR11 write
@apu_io.on_write(1)
def apu_nr11_write(chip, val):
chip.nr11 = val
chip.ch1_duty = (val >> 6) & 3
chip.ch1_length = 64 - (val & 63)
# NR12 write
@apu_io.on_write(2)
def apu_nr12_write(chip, val):
chip.nr12 = val
chip.ch1_dac = 1 if (val & 248) else 0
if not chip.ch1_dac:
chip.ch1_enabled = 0
# NR13 write
@apu_io.on_write(3)
def apu_nr13_write(chip, val):
chip.nr13 = val
chip.ch1_freq = uint16((chip.ch1_freq & 1792) | val)
# NR14 write: trigger channel 1
@apu_io.on_write(4)
def apu_nr14_write(chip, val):
chip.nr14 = val
chip.ch1_freq = uint16((chip.ch1_freq & 255) | (uint16(val & 7) << 8))
chip.ch1_length_en = 1 if (val & 64) else 0
if val & 128:
chip.ch1_enabled = chip.ch1_dac
if chip.ch1_length == 0:
chip.ch1_length = 64
chip.ch1_freq_timer = (2048 - chip.ch1_freq) * 4
chip.ch1_volume = (chip.nr12 >> 4) & 15
chip.ch1_env_dir = (chip.nr12 >> 3) & 1
chip.ch1_env_pace = chip.nr12 & 7
chip.ch1_env_timer = chip.ch1_env_pace
chip.ch1_sweep_freq = chip.ch1_freq
chip.ch1_sweep_timer = chip.ch1_sweep_pace if chip.ch1_sweep_pace else 8
chip.ch1_sweep_en = 1 if (chip.ch1_sweep_pace > 0) or (chip.ch1_sweep_step > 0) else 0
# NR21 write
@apu_io.on_write(6)
def apu_nr21_write(chip, val):
chip.nr21 = val
chip.ch2_duty = (val >> 6) & 3
chip.ch2_length = 64 - (val & 63)
# NR22 write
@apu_io.on_write(7)
def apu_nr22_write(chip, val):
chip.nr22 = val
chip.ch2_dac = 1 if (val & 248) else 0
if not chip.ch2_dac:
chip.ch2_enabled = 0
# NR23 write
@apu_io.on_write(8)
def apu_nr23_write(chip, val):
chip.nr23 = val
chip.ch2_freq = uint16((chip.ch2_freq & 1792) | val)
# NR24 write: trigger channel 2
@apu_io.on_write(9)
def apu_nr24_write(chip, val):
chip.nr24 = val
chip.ch2_freq = uint16((chip.ch2_freq & 255) | (uint16(val & 7) << 8))
chip.ch2_length_en = 1 if (val & 64) else 0
if val & 128:
chip.ch2_enabled = chip.ch2_dac
if chip.ch2_length == 0:
chip.ch2_length = 64
chip.ch2_freq_timer = (2048 - chip.ch2_freq) * 4
chip.ch2_volume = (chip.nr22 >> 4) & 15
chip.ch2_env_dir = (chip.nr22 >> 3) & 1
chip.ch2_env_pace = chip.nr22 & 7
chip.ch2_env_timer = chip.ch2_env_pace
# NR30 write
@apu_io.on_write(10)
def apu_nr30_write(chip, val):
chip.nr30 = val
chip.ch3_dac = 1 if (val & 128) else 0
if not chip.ch3_dac:
chip.ch3_enabled = 0
# NR31 write
@apu_io.on_write(11)
def apu_nr31_write(chip, val):
chip.nr31 = val
chip.ch3_length = 256 - val
# NR32 write
@apu_io.on_write(12)
def apu_nr32_write(chip, val):
chip.nr32 = val
chip.ch3_volume_code = (val >> 5) & 3
# NR33 write
@apu_io.on_write(13)
def apu_nr33_write(chip, val):
chip.nr33 = val
chip.ch3_freq = uint16((chip.ch3_freq & 1792) | val)
# NR34 write: trigger channel 3
@apu_io.on_write(14)
def apu_nr34_write(chip, val):
chip.nr34 = val
chip.ch3_freq = uint16((chip.ch3_freq & 255) | (uint16(val & 7) << 8))
chip.ch3_length_en = 1 if (val & 64) else 0
if val & 128:
chip.ch3_enabled = chip.ch3_dac
if chip.ch3_length == 0:
chip.ch3_length = 256
chip.ch3_freq_timer = (2048 - chip.ch3_freq) * 2
chip.ch3_sample_pos = 0
# NR41 write
@apu_io.on_write(16)
def apu_nr41_write(chip, val):
chip.nr41 = val
chip.ch4_length = 64 - (val & 63)
# NR42 write
@apu_io.on_write(17)
def apu_nr42_write(chip, val):
chip.nr42 = val
chip.ch4_dac = 1 if (val & 248) else 0
if not chip.ch4_dac:
chip.ch4_enabled = 0
# NR43 write
@apu_io.on_write(18)
def apu_nr43_write(chip, val):
chip.nr43 = val
chip.ch4_clock_shift = (val >> 4) & 15
chip.ch4_width = (val >> 3) & 1
chip.ch4_divisor_code = val & 7
# NR44 write: trigger channel 4
@apu_io.on_write(19)
def apu_nr44_write(chip, val):
chip.nr44 = val
chip.ch4_length_en = 1 if (val & 64) else 0
if val & 128:
chip.ch4_enabled = chip.ch4_dac
if chip.ch4_length == 0:
chip.ch4_length = 64
chip.ch4_lfsr = 32767
chip.ch4_volume = (chip.nr42 >> 4) & 15
chip.ch4_env_dir = (chip.nr42 >> 3) & 1
chip.ch4_env_pace = chip.nr42 & 7
chip.ch4_env_timer = chip.ch4_env_pace
chip.ch4_clock_shift = (chip.nr43 >> 4) & 15
chip.ch4_width = (chip.nr43 >> 3) & 1
chip.ch4_divisor_code = chip.nr43 & 7
apu_chip.add_register_block(apu_io)
# ===================================================================
# Timer Chip
# ===================================================================
timer_chip = Chip("timer", clock=master_clock, comment="Timer")
timer_chip.add_state("div_counter", "uint16_t", "0", "DIV internal counter")
timer_chip.add_state("tima_counter", "int32_t", "0", "TIMA cycle counter")
timer_io = RegisterBlock("timer_io", base_addr=0xFF04, size=4)
timer_io.bind(0, "div", comment="Divider register")
timer_io.bind(1, "tima", comment="Timer counter")
timer_io.bind(2, "tma", comment="Timer modulo")
timer_io.bind(3, "tac", comment="Timer control")
# DIV read returns upper byte of internal counter (transpiled)
@timer_io.on_read(0)
def timer_div_read(chip):
return uint8(chip.div_counter >> 8)
# DIV write resets counter (transpiled)
@timer_io.on_write(0)
def timer_div_write(chip, val):
chip.div_counter = 0
chip.div = 0
timer_chip.add_register_block(timer_io)
# ===================================================================
# Joypad Chip
# ===================================================================
joypad_chip = Chip("joypad", clock=master_clock, comment="Joypad")
joypad_chip.add_state("button_state", "uint8_t", "0xFF", "Button state (active low)")
joypad_chip.add_state("direction_state", "uint8_t", "0xFF", "Direction state (active low)")
joypad_io = RegisterBlock("joypad_io", base_addr=0xFF00, size=1)
joypad_io.bind(0, "p1", comment="Joypad register")
# Joypad read handler (transpiled)
@joypad_io.on_read(0)
def joypad_read(chip):
sel: uint8 = chip.p1 & 48
r: uint8 = sel | 192
if not (sel & 16):
r = r | (chip.direction_state & 15)
elif not (sel & 32):
r = r | (chip.button_state & 15)
else:
r = r | 15
return r
joypad_chip.add_register_block(joypad_io)
# ===================================================================
# CPU Chip
# ===================================================================
cpu_chip = Chip("cpu", clock=master_clock, comment="Sharp SM83 CPU")
# CPU-managed state (interrupt, halt, MBC)
cpu_chip.add_state("ime", "bool", "false", "Interrupt master enable")
cpu_chip.add_state("ime_delay", "uint8_t", "0", "EI delay counter")
cpu_chip.add_state("interrupt_enable", "uint8_t", "0", "IE register")
cpu_chip.add_state("interrupt_flags", "uint8_t", "0", "IF register")
cpu_chip.add_state("mbc_type", "uint8_t", "0", "MBC type (0=ROM, 1=MBC1, 3=MBC3, 5=MBC5)")
cpu_chip.add_state("rom_bank", "uint16_t", "1", "Current ROM bank")
cpu_chip.add_state("ram_bank", "uint8_t", "0", "Current RAM bank")
cpu_chip.add_state("ram_enabled", "bool", "false", "Cart RAM enabled")
cpu_chip.add_state("mbc_mode", "uint8_t", "0", "MBC1 banking mode")
# ===================================================================
# Memory Regions
# ===================================================================
rom_region = MemoryRegion("rom", 0, access=MemoryAccessLevel.ReadOnly,
comment="Cartridge ROM (dynamic)")
cart_ram_region = MemoryRegion("cart_ram", 0,
comment="Cartridge RAM (dynamic)")
wram = MemoryRegion("wram", 8192, comment="Work RAM")
hram = MemoryRegion("hram", 127, comment="High RAM")
cpu_chip.add_internal_memory(rom_region)
cpu_chip.add_internal_memory(cart_ram_region)
cpu_chip.add_internal_memory(wram)
cpu_chip.add_internal_memory(hram)
# ===================================================================
# ROM Banking (MBC) -- all transpiled
# ===================================================================
rom_fixed_bank = MemoryBank("rom_fixed", region=rom_region,
bank_size=16384, max_banks=1, default_bank=0)
rom_banked_bank = MemoryBank("rom_banked", region=rom_region,
bank_size=16384, max_banks=512, default_bank=1)
cart_ram_bank = MemoryBank("cart_ram_bank", region=cart_ram_region,
bank_size=8192, max_banks=16, default_bank=0)
mbc = MemoryController("mbc", controls=[rom_fixed_bank, rom_banked_bank, cart_ram_bank])
# Bank resolvers (transpiled)
@mbc.bank_resolver(rom_fixed_bank)
def resolve_rom_fixed(ctrl, addr):
return 0
@mbc.bank_resolver(rom_banked_bank)
def resolve_rom_banked(ctrl, addr):
return ctrl.rom_bank
@mbc.bank_resolver(cart_ram_bank)
def resolve_cart_ram(ctrl, addr):
return ctrl.ram_bank
# Access guards for cart RAM (transpiled)
@mbc.read_guard(cart_ram_bank)
def guard_read_cart_ram(ctrl):
return ctrl.ram_enabled
@mbc.write_guard(cart_ram_bank)
def guard_write_cart_ram(ctrl):
return ctrl.ram_enabled
# MBC write handlers (transpiled)
@mbc.on_write(0x0000, 0x1FFF)
def mbc_ram_enable(ctrl, val, addr):
ctrl.ram_enabled = 1 if (val & 15) == 10 else 0
@mbc.on_write(0x2000, 0x3FFF)
def mbc_rom_bank_select(ctrl, val, addr):
if ctrl.mbc_type == 1:
b: uint8 = val & 31
if b == 0:
b = 1
ctrl.rom_bank = uint16((ctrl.rom_bank & 96) | b)
elif ctrl.mbc_type == 3:
b2: uint8 = val & 127
if b2 == 0:
b2 = 1
ctrl.rom_bank = b2
elif ctrl.mbc_type == 5:
ctrl.rom_bank = uint16((ctrl.rom_bank & 256) | val)
if ctrl.rom_bank == 0:
ctrl.rom_bank = 1
@mbc.on_write(0x4000, 0x5FFF)
def mbc_ram_bank_select(ctrl, val, addr):
if ctrl.mbc_type == 1:
if ctrl.mbc_mode == 0:
ctrl.rom_bank = uint16((ctrl.rom_bank & 31) | ((val & 3) << 5))
else:
ctrl.ram_bank = val & 3
elif ctrl.mbc_type == 3 or ctrl.mbc_type == 5:
ctrl.ram_bank = val & 15
@mbc.on_write(0x6000, 0x7FFF)
def mbc_mode_select(ctrl, val, addr):
if ctrl.mbc_type == 1:
ctrl.mbc_mode = val & 1
cpu_chip.add_memory_controller(mbc)
# ===================================================================
# Memory Bus (64KB address space)
# ===================================================================
bus = MemoryBus("bus", address_bits=16)
# 0000-3FFF: ROM bank 0 (fixed)
bus.map(0x0000, 0x3FFF, bank=rom_fixed_bank, controller=mbc, comment="ROM bank 0")
# 4000-7FFF: ROM bank N (switchable)
bus.map(0x4000, 0x7FFF, bank=rom_banked_bank, controller=mbc, comment="ROM bank N")
# 8000-9FFF: VRAM
bus.map(0x8000, 0x9FFF, region=ppu_vram, comment="Video RAM")
# A000-BFFF: Cart RAM (banked)
bus.map(0xA000, 0xBFFF, bank=cart_ram_bank, controller=mbc, comment="Cart RAM")
# C000-DFFF: WRAM
bus.map(0xC000, 0xDFFF, region=wram, comment="Work RAM")
# E000-FDFF: Echo RAM (mirrors C000-DDFF)
bus.map(0xE000, 0xFDFF, region=wram, comment="Echo RAM")
# FE00-FE9F: OAM
bus.map(0xFE00, 0xFE9F, region=ppu_oam, comment="OAM")
# FF00: Joypad
bus.map(0xFF00, 0xFF00, handler=joypad_io, comment="Joypad")
# FF04-FF07: Timer
bus.map(0xFF04, 0xFF07, handler=timer_io, comment="Timer")
# FF10-FF3F: APU
bus.map(0xFF10, 0xFF3F, handler=apu_io, comment="APU registers")
# FF40-FF4B: PPU
bus.map(0xFF40, 0xFF4B, handler=ppu_io, comment="PPU registers")
# FF80-FFFE: HRAM
bus.map(0xFF80, 0xFFFE, region=hram, comment="High RAM")
# Write-only MBC intercepts
bus.map_writes(0x0000, 0x7FFF, controller=mbc, comment="MBC writes")
# Wave RAM mapping
bus.map(0xFF30, 0xFF3F, region=wave_ram, comment="Wave RAM")
# Fallback
bus.set_fallback(read=0xFF, write=None)
# Set bus on CPU chip
cpu_chip.set_bus(bus)
# ===================================================================
# CPU Definition
# ===================================================================
cpu = CPUDefinition("sm83", data_width=8, address_width=16)
# Registers
cpu.add_register("A", 8, default="0x01")
cpu.add_register("F", 8, default="0xB0")
cpu.add_register("B", 8)
cpu.add_register("C", 8, default="0x13")
cpu.add_register("D", 8)
cpu.add_register("E", 8, default="0xD8")
cpu.add_register("H", 8)
cpu.add_register("L", 8, default="0x4D")
# Register pairs
cpu.add_register_pair("AF", "A", "F")
cpu.add_register_pair("BC", "B", "C")
cpu.add_register_pair("DE", "D", "E")
cpu.add_register_pair("HL", "H", "L")
# Flags (in F register) -- Z=bit7, N=bit6, H=bit5, C=bit4
cpu.set_flags("F", {"Z": 7, "N": 6, "H": 5, "C": 4})
cpu.registers = [r for r in cpu.registers if r.name != "F"]
cpu.add_prefix_table(0xCB)
# ===================================================================
# IF / IE register blocks (transpiled)
# ===================================================================
if_io = RegisterBlock("if_io", base_addr=0xFF0F, size=1)
if_io.bind(0, "interrupt_flags")
@if_io.on_read(0)
def if_read(chip):
return uint8(chip.interrupt_flags | 224)
@if_io.on_write(0)
def if_write(chip, val):
chip.interrupt_flags = val & 31
cpu_chip.add_register_block(if_io)
ie_io = RegisterBlock("ie_io", base_addr=0xFFFF, size=1)
ie_io.bind(0, "interrupt_enable")
@ie_io.on_read(0)
def ie_read(chip):
return chip.interrupt_enable
@ie_io.on_write(0)
def ie_write(chip, val):
chip.interrupt_enable = val
cpu_chip.add_register_block(ie_io)
bus.map(0xFF0F, 0xFF0F, handler=if_io, comment="IF register")
bus.map(0xFFFF, 0xFFFF, handler=ie_io, comment="IE register")
# ===================================================================
# Serial stub (FF01-FF02)
# ===================================================================
serial_io = RegisterBlock("serial_io", base_addr=0xFF01, size=2)
serial_io.bind(0, "sb", comment="Serial data")
serial_io.bind(1, "sc", comment="Serial control")
cpu_chip.add_register_block(serial_io)
bus.map(0xFF01, 0xFF02, handler=serial_io, comment="Serial")
# ===================================================================
# Chip helpers -- ALL transpiled from Python
# ===================================================================
# PPU update helper (transpiled)
@ppu_chip.helper("update_ppu", returns="void", params=[("cycles", "uint32_t")])
def update_ppu(ppu, cycles):
ppu.line_dots = ppu.line_dots + cycles
while ppu.line_dots >= 456:
ppu.line_dots = ppu.line_dots - 456
ppu.ly = uint8(ppu.ly + 1)
if ppu.ly == 144:
ppu.mode = 1
cpu.interrupt_flags = cpu.interrupt_flags | 1
ppu.frame_ready = 1
render_frame(ppu.framebuffer, 160, 144)
elif ppu.ly >= 154:
ppu.ly = 0
ppu.mode = 2
if ppu.ly < 144:
lcdc: uint8 = ppu.lcdc
if not (lcdc & 128):
continue
ly: uint8 = ppu.ly
bgp: uint8 = ppu.bgp
pal0: uint8 = bgp & 3
pal1: uint8 = (bgp >> 2) & 3
pal2: uint8 = (bgp >> 4) & 3
pal3: uint8 = (bgp >> 6) & 3
fb_off: uint32 = uint32(ly) * 160
if lcdc & 1:
tile_map: uint16 = 7168 if (lcdc & 8) else 6144
tile_data: uint16 = 0 if (lcdc & 16) else 2048
scrolly: uint8 = uint8(ppu.scy + ly)
ty: uint8 = scrolly >> 3
tyl: uint8 = scrolly & 7
for px in range(160):
scrollx: uint8 = uint8(ppu.scx + px)
tx: uint8 = scrollx >> 3
txl: uint8 = scrollx & 7
tile_idx: uint8 = ppu.vram[tile_map + uint16(ty) * 32 + tx]
addr: uint16 = 0
if lcdc & 16:
addr = uint16(tile_idx) * 16 + uint16(tyl) * 2
else:
addr = uint16(tile_data + uint16(int8(tile_idx) + 128) * 16 + uint16(tyl) * 2)
lo: uint8 = ppu.vram[addr]
hi: uint8 = ppu.vram[uint16(addr + 1)]
bit: uint8 = 7 - txl
color: uint8 = ((lo >> bit) & 1) | (((hi >> bit) & 1) << 1)
if color == 0:
ppu.framebuffer[fb_off + px] = pal0
elif color == 1:
ppu.framebuffer[fb_off + px] = pal1
elif color == 2:
ppu.framebuffer[fb_off + px] = pal2
else:
ppu.framebuffer[fb_off + px] = pal3
else:
for px in range(160):
ppu.framebuffer[fb_off + px] = 0
# Window layer
if (lcdc & 32) and ly >= ppu.wy:
win_map: uint16 = 7168 if (lcdc & 64) else 6144
w_tile_data: uint16 = 0 if (lcdc & 16) else 2048
wx: int32 = int(ppu.wx) - 7
wly: uint8 = uint8(ly - ppu.wy)
wty: uint8 = wly >> 3
wtyl: uint8 = wly & 7
for wpx in range(160):
if wpx < wx:
continue
wtxl: uint8 = uint8(wpx - wx) & 7
wtx: uint8 = uint8(wpx - wx) >> 3
w_tile_idx: uint8 = ppu.vram[win_map + uint16(wty) * 32 + wtx]
waddr: uint16 = 0
if lcdc & 16:
waddr = uint16(w_tile_idx) * 16 + uint16(wtyl) * 2
else:
waddr = uint16(w_tile_data + uint16(int8(w_tile_idx) + 128) * 16 + uint16(wtyl) * 2)
wlo: uint8 = ppu.vram[waddr]
whi: uint8 = ppu.vram[uint16(waddr + 1)]
wbit: uint8 = 7 - wtxl
wcolor: uint8 = ((wlo >> wbit) & 1) | (((whi >> wbit) & 1) << 1)
if wcolor == 0:
ppu.framebuffer[fb_off + wpx] = pal0
elif wcolor == 1:
ppu.framebuffer[fb_off + wpx] = pal1
elif wcolor == 2:
ppu.framebuffer[fb_off + wpx] = pal2
else:
ppu.framebuffer[fb_off + wpx] = pal3
# Sprites
if lcdc & 2:
sprite_h: uint8 = 16 if (lcdc & 4) else 8
count: int32 = 0
for i in range(40):
if count >= 10:
break
sy: uint8 = uint8(ppu.oam[i * 4] - 16)
sx: uint8 = uint8(ppu.oam[i * 4 + 1] - 8)
tile: uint8 = ppu.oam[i * 4 + 2]
flags: uint8 = ppu.oam[i * 4 + 3]
if ly < sy:
continue
if ly >= uint8(sy + sprite_h):
continue
count = count + 1
row: uint8 = uint8(ly - sy)
if flags & 64:
row = uint8(sprite_h - 1 - row)
if sprite_h == 16:
tile = tile & 254
saddr: uint16 = uint16(tile) * 16 + uint16(row) * 2
slo: uint8 = ppu.vram[saddr]
shi: uint8 = ppu.vram[uint16(saddr + 1)]
sp_pal: uint8 = ppu.obp1 if (flags & 16) else ppu.obp0
sp0: uint8 = sp_pal & 3
sp1: uint8 = (sp_pal >> 2) & 3
sp2: uint8 = (sp_pal >> 4) & 3
sp3: uint8 = (sp_pal >> 6) & 3
for b in range(8):
xpos: int32 = int(sx) + (7 - b if (flags & 32) else b)
if xpos < 0:
continue
if xpos >= 160:
continue
sbit: uint8 = uint8(7 - b)
scolor: uint8 = ((slo >> sbit) & 1) | (((shi >> sbit) & 1) << 1)
if scolor == 0:
continue
if (flags & 128) and ppu.framebuffer[fb_off + xpos] != pal0:
continue
if scolor == 1:
ppu.framebuffer[fb_off + xpos] = sp1
elif scolor == 2:
ppu.framebuffer[fb_off + xpos] = sp2
else:
ppu.framebuffer[fb_off + xpos] = sp3
# LYC compare
if ppu.ly == ppu.lyc:
ppu.stat = ppu.stat | 4
if ppu.stat & 64:
cpu.interrupt_flags = cpu.interrupt_flags | 2
else:
ppu.stat = ppu.stat & 251
# Timer update helper (transpiled)
@timer_chip.helper("update_timer", returns="void", params=[("cycles", "uint32_t")])
def update_timer(timer, cycles):
timer.div_counter = uint16(timer.div_counter + cycles)
tac: uint8 = timer.tac
if tac & 4:
timer.tima_counter = timer.tima_counter + cycles
# TAC rate selection: 0=1024, 1=16, 2=64, 3=256
rate: uint16 = 1024
tac_sel: uint8 = tac & 3
if tac_sel == 1:
rate = 16
elif tac_sel == 2:
rate = 64
elif tac_sel == 3:
rate = 256
while timer.tima_counter >= rate:
timer.tima_counter = timer.tima_counter - rate
timer.tima = uint8(timer.tima + 1)
if timer.tima == 0:
timer.tima = timer.tma
cpu.interrupt_flags = cpu.interrupt_flags | 4
# APU update helper (transpiled from Python -- full synthesis)
@apu_chip.helper("update_apu", returns="void", params=[("dots", "uint32_t")])
def update_apu(apu, dots):
if not (apu.nr52 & 128):
return
# Duty table flattened: 4 patterns x 8 steps = 32 entries
duty_table: array[uint8, 32]
# Pattern 0: 12.5% 00000001
duty_table[0] = 0
duty_table[1] = 0
duty_table[2] = 0
duty_table[3] = 0
duty_table[4] = 0
duty_table[5] = 0
duty_table[6] = 0
duty_table[7] = 1
# Pattern 1: 25% 10000001
duty_table[8] = 1
duty_table[9] = 0
duty_table[10] = 0
duty_table[11] = 0
duty_table[12] = 0
duty_table[13] = 0
duty_table[14] = 0
duty_table[15] = 1
# Pattern 2: 50% 10000111
duty_table[16] = 1
duty_table[17] = 0
duty_table[18] = 0
duty_table[19] = 0
duty_table[20] = 0
duty_table[21] = 1
duty_table[22] = 1
duty_table[23] = 1
# Pattern 3: 75% 01111110
duty_table[24] = 0
duty_table[25] = 1
duty_table[26] = 1
duty_table[27] = 1
duty_table[28] = 1
duty_table[29] = 1
duty_table[30] = 1
duty_table[31] = 0
# Channel 1 frequency timer
if apu.ch1_enabled:
apu.ch1_freq_timer = apu.ch1_freq_timer - int32(dots)
while apu.ch1_freq_timer <= 0:
freq: uint16 = uint16(apu.nr13) | ((uint16(apu.nr14) & 7) << 8)
apu.ch1_freq_timer = apu.ch1_freq_timer + int32(2048 - freq) * 4
apu.ch1_duty_pos = (apu.ch1_duty_pos + 1) & 7
# Channel 2 frequency timer
if apu.ch2_enabled:
apu.ch2_freq_timer = apu.ch2_freq_timer - int32(dots)
while apu.ch2_freq_timer <= 0:
freq: uint16 = uint16(apu.nr23) | ((uint16(apu.nr24) & 7) << 8)
apu.ch2_freq_timer = apu.ch2_freq_timer + int32(2048 - freq) * 4
apu.ch2_duty_pos = (apu.ch2_duty_pos + 1) & 7
# Channel 3 frequency timer
if apu.ch3_enabled:
apu.ch3_freq_timer = apu.ch3_freq_timer - int32(dots)
while apu.ch3_freq_timer <= 0:
freq: uint16 = uint16(apu.nr33) | ((uint16(apu.nr34) & 7) << 8)
apu.ch3_freq_timer = apu.ch3_freq_timer + int32(2048 - freq) * 2
apu.ch3_sample_pos = (apu.ch3_sample_pos + 1) & 31
# Channel 4 frequency timer (noise LFSR)
if apu.ch4_enabled:
apu.ch4_freq_timer = apu.ch4_freq_timer - int32(dots)
while apu.ch4_freq_timer <= 0:
div_code: uint8 = apu.nr43 & 7
shift: uint8 = (apu.nr43 >> 4) & 15
divisor: int32 = int32(div_code) * 16 if div_code else 8
apu.ch4_freq_timer = apu.ch4_freq_timer + (divisor << shift)
xor_bit: uint16 = (apu.ch4_lfsr & 1) ^ ((apu.ch4_lfsr >> 1) & 1)
apu.ch4_lfsr = (apu.ch4_lfsr >> 1) | (xor_bit << 14)
if apu.nr43 & 8:
apu.ch4_lfsr = apu.ch4_lfsr & ~(1 << 6)
apu.ch4_lfsr = apu.ch4_lfsr | (xor_bit << 6)
# Frame sequencer (length, sweep, envelope)
apu.frame_seq_counter = apu.frame_seq_counter + dots
while apu.frame_seq_counter >= 8192:
apu.frame_seq_counter = apu.frame_seq_counter - 8192
step: uint8 = apu.frame_seq_step
# Length counter (steps 0, 2, 4, 6)
if (step & 1) == 0:
if (apu.nr14 & 64) and apu.ch1_length > 0:
apu.ch1_length = apu.ch1_length - 1
if apu.ch1_length == 0:
apu.ch1_enabled = 0
if (apu.nr24 & 64) and apu.ch2_length > 0:
apu.ch2_length = apu.ch2_length - 1
if apu.ch2_length == 0:
apu.ch2_enabled = 0
if (apu.nr34 & 64) and apu.ch3_length > 0:
apu.ch3_length = apu.ch3_length - 1
if apu.ch3_length == 0:
apu.ch3_enabled = 0
if (apu.nr44 & 64) and apu.ch4_length > 0:
apu.ch4_length = apu.ch4_length - 1
if apu.ch4_length == 0:
apu.ch4_enabled = 0
# Sweep (steps 2, 6)
if step == 2 or step == 6:
if apu.ch1_sweep_enabled:
apu.ch1_sweep_timer = apu.ch1_sweep_timer - 1
if apu.ch1_sweep_timer == 0:
period: uint8 = (apu.nr10 >> 4) & 7
apu.ch1_sweep_timer = period if period else 8
if period:
sw_shift: uint8 = apu.nr10 & 7
delta: uint16 = apu.ch1_sweep_shadow >> sw_shift
new_freq: uint16 = 0
if apu.nr10 & 8:
new_freq = apu.ch1_sweep_shadow - delta
else:
new_freq = apu.ch1_sweep_shadow + delta
if new_freq > 2047:
apu.ch1_enabled = 0
elif sw_shift:
apu.ch1_sweep_shadow = new_freq
apu.nr13 = new_freq & 255
apu.nr14 = (apu.nr14 & 248) | ((new_freq >> 8) & 7)
delta2: uint16 = new_freq >> sw_shift
check2: uint16 = (new_freq - delta2) if (apu.nr10 & 8) else (new_freq + delta2)
if check2 > 2047:
apu.ch1_enabled = 0
# Envelope (step 7)
if step == 7:
if (apu.nr12 & 7) and apu.ch1_enabled:
apu.ch1_env_timer = apu.ch1_env_timer - 1
if apu.ch1_env_timer == 0:
apu.ch1_env_timer = apu.nr12 & 7
if apu.nr12 & 8:
if apu.ch1_volume < 15:
apu.ch1_volume = apu.ch1_volume + 1
else:
if apu.ch1_volume > 0:
apu.ch1_volume = apu.ch1_volume - 1
if (apu.nr22 & 7) and apu.ch2_enabled:
apu.ch2_env_timer = apu.ch2_env_timer - 1
if apu.ch2_env_timer == 0:
apu.ch2_env_timer = apu.nr22 & 7
if apu.nr22 & 8:
if apu.ch2_volume < 15:
apu.ch2_volume = apu.ch2_volume + 1
else:
if apu.ch2_volume > 0:
apu.ch2_volume = apu.ch2_volume - 1
if (apu.nr42 & 7) and apu.ch4_enabled:
apu.ch4_env_timer = apu.ch4_env_timer - 1
if apu.ch4_env_timer == 0:
apu.ch4_env_timer = apu.nr42 & 7
if apu.nr42 & 8:
if apu.ch4_volume < 15:
apu.ch4_volume = apu.ch4_volume + 1
else:
if apu.ch4_volume > 0:
apu.ch4_volume = apu.ch4_volume - 1
apu.frame_seq_step = (step + 1) & 7
# Sample generation (downsample from ~4.19MHz to ~48kHz, every ~87 cycles)
apu.sample_counter = apu.sample_counter + dots
while apu.sample_counter >= 87:
apu.sample_counter = apu.sample_counter - 87
if apu.sample_count >= 1614:
continue
left: int16 = 0
right: int16 = 0
# Channel 1 (square + sweep)
if apu.ch1_enabled and not apu.debug_ch1_mute:
duty: uint8 = (apu.nr11 >> 6) & 3
sample: int16 = int16(apu.ch1_volume) if duty_table[int(duty) * 8 + int(apu.ch1_duty_pos)] else 0
if apu.nr51 & 16:
left = left + sample