@@ -11,74 +11,93 @@ def initialize(ppu, rom, ram, mbc, timer, interrupt, joypad, apu)
1111 @apu = apu
1212 @interrupt = interrupt
1313 @timer = timer
14+ end
15+
16+ def read_byte ( addr )
17+ case addr >> 12
18+ when 0x0 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 , 0xa , 0xb
19+ return @mbc . read_byte ( addr )
20+ when 0x8 , 0x9
21+ return @ppu . read_byte ( addr )
22+ when 0xc
23+ return @ram . wram1 [ addr - 0xc000 ]
24+ when 0xd
25+ return @ram . wram2 [ addr - 0xd000 ]
26+ when 0xf
27+ case addr >> 8
28+ when 0xfe
29+ return @ppu . read_byte ( addr ) if addr <= 0xfe9f
30+ when 0xff
31+ last_byte = addr & 0xFF
1432
15- @read_methods = Array . new ( 0x10000 )
16- @write_methods = Array . new ( 0x10000 )
33+ case last_byte
34+ when 0x00
35+ return @joypad . read_byte ( addr )
36+ when 0x04 , 0x05 , 0x06 , 0x07
37+ return @timer . read_byte ( addr )
38+ when 0x0f
39+ return @interrupt . read_byte ( addr )
40+ when 0x46
41+ return @ppu . read_byte ( addr )
42+ when 0xff
43+ return @interrupt . read_byte ( addr )
44+ end
1745
18- set_methods
19- end
46+ return @apu . read_byte ( addr ) if last_byte <= 0x26 && last_byte >= 0x10
2047
21- def set_methods
22- 0x10000 . times do |addr |
23- case addr
24- when 0x0000 ..0x7fff
25- @read_methods [ addr ] = -> { @mbc . read_byte ( addr ) }
26- @write_methods [ addr ] = -> ( value ) { @mbc . write_byte ( addr , value ) }
27- when 0x8000 ..0x9fff
28- @read_methods [ addr ] = -> { @ppu . read_byte ( addr ) }
29- @write_methods [ addr ] = -> ( value ) { @ppu . write_byte ( addr , value ) }
30- when 0xa000 ..0xbfff
31- @read_methods [ addr ] = -> { @mbc . read_byte ( addr ) }
32- @write_methods [ addr ] = -> ( value ) { @mbc . write_byte ( addr , value ) }
33- when 0xc000 ..0xcfff
34- @read_methods [ addr ] = -> { @ram . wram1 [ addr - 0xc000 ] }
35- @write_methods [ addr ] = -> ( value ) { @ram . wram1 [ addr - 0xc000 ] = value }
36- when 0xd000 ..0xdfff
37- @read_methods [ addr ] = -> { @ram . wram2 [ addr - 0xd000 ] }
38- @write_methods [ addr ] = -> ( value ) { @ram . wram2 [ addr - 0xd000 ] = value }
39- when 0xfe00 ..0xfe9f
40- @read_methods [ addr ] = -> { @ppu . read_byte ( addr ) }
41- @write_methods [ addr ] = -> ( value ) { @ppu . write_byte ( addr , value ) }
42- when 0xff00
43- @read_methods [ addr ] = -> { @joypad . read_byte ( addr ) }
44- @write_methods [ addr ] = -> ( value ) { @joypad . write_byte ( addr , value ) }
45- when 0xff04 ..0xff07
46- @read_methods [ addr ] = -> { @timer . read_byte ( addr ) }
47- @write_methods [ addr ] = -> ( value ) { @timer . write_byte ( addr , value ) }
48- when 0xff0f
49- @read_methods [ addr ] = -> { @interrupt . read_byte ( addr ) }
50- @write_methods [ addr ] = -> ( value ) { @interrupt . write_byte ( addr , value ) }
51- when 0xff10 ..0xff26
52- @read_methods [ addr ] = -> { @apu . read_byte ( addr ) }
53- @write_methods [ addr ] = -> ( value ) { @apu . write_byte ( addr , value ) }
54- when 0xff30 ..0xff3f
55- @read_methods [ addr ] = -> { @apu . read_byte ( addr ) }
56- @write_methods [ addr ] = -> ( value ) { @apu . write_byte ( addr , value ) }
57- when 0xff46
58- @read_methods [ addr ] = -> { @ppu . read_byte ( addr ) }
59- @write_methods [ addr ] = -> ( value ) { 0xa0 . times { |i | write_byte ( 0xfe00 + i , read_byte ( ( value << 8 ) + i ) ) } }
60- when 0xff40 ..0xff4b
61- @read_methods [ addr ] = -> { @ppu . read_byte ( addr ) }
62- @write_methods [ addr ] = -> ( value ) { @ppu . write_byte ( addr , value ) }
63- when 0xff80 ..0xfffe
64- @read_methods [ addr ] = -> { @ram . hram [ addr - 0xff80 ] }
65- @write_methods [ addr ] = -> ( value ) { @ram . hram [ addr - 0xff80 ] = value }
66- when 0xffff
67- @read_methods [ addr ] = -> { @interrupt . read_byte ( addr ) }
68- @write_methods [ addr ] = -> ( value ) { @interrupt . write_byte ( addr , value ) }
69- else
70- @read_methods [ addr ] = -> { 0xff }
71- @write_methods [ addr ] = -> ( _value ) { }
48+ return @apu . read_byte ( addr ) if last_byte <= 0x3f && last_byte >= 0x30
49+
50+ return @ppu . read_byte ( addr ) if last_byte <= 0x4b && last_byte >= 0x40
51+
52+ return @ram . hram [ addr - 0xff80 ] if last_byte <= 0xfe && last_byte >= 0x80
7253 end
7354 end
74- end
7555
76- def read_byte ( addr )
77- @read_methods [ addr ] . call
56+ 0xff
7857 end
7958
8059 def write_byte ( addr , value )
81- @write_methods [ addr ] . call ( value )
60+ case addr >> 12
61+ when 0x0 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 , 0xa , 0xb
62+ return @mbc . write_byte ( addr , value )
63+ when 0x8 , 0x9
64+ return @ppu . write_byte ( addr , value )
65+ when 0xc
66+ return @ram . wram1 [ addr - 0xc000 ] = value
67+ when 0xd
68+ return @ram . wram2 [ addr - 0xd000 ] = value
69+ when 0xf
70+ case addr >> 8
71+ when 0xfe
72+ return @ppu . write_byte ( addr , value ) if addr <= 0xfe9f
73+ when 0xff
74+ last_byte = addr & 0xFF
75+
76+ case last_byte
77+ when 0x00
78+ return @joypad . write_byte ( addr , value )
79+ when 0x04 , 0x05 , 0x06 , 0x07
80+ return @timer . write_byte ( addr , value )
81+ when 0x0f
82+ return @interrupt . write_byte ( addr , value )
83+ when 0x46
84+ 0xa0 . times { |i | write_byte ( 0xfe00 + i , read_byte ( ( value << 8 ) + i ) ) }
85+ return
86+ when 0xff
87+ return @interrupt . write_byte ( addr , value )
88+ end
89+
90+ return @apu . write_byte ( addr , value ) if last_byte <= 0x26 && last_byte >= 0x10
91+
92+ return @apu . write_byte ( addr , value ) if last_byte <= 0x3f && last_byte >= 0x30
93+
94+ return @ppu . write_byte ( addr , value ) if last_byte <= 0x4b && last_byte >= 0x40
95+
96+ return @ram . hram [ addr - 0xff80 ] = value if last_byte <= 0xfe && last_byte >= 0x80
97+ end
98+ end
99+
100+ nil
82101 end
83102
84103 def read_word ( addr )
0 commit comments