Class: Rubyboy::Cartridge::Mbc1

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyboy/cartridge/mbc1.rb

Instance Method Summary collapse

Constructor Details

#initialize(rom, ram) ⇒ Mbc1

Returns a new instance of Mbc1.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubyboy/cartridge/mbc1.rb', line 6

def initialize(rom, ram)
  @rom = rom
  @ram = ram
  @rom_bank = 1
  @ram_bank = 0
  @ram_enable = false
  @ram_banking_mode = false

  @read_methods = Array.new(0xc000)
  @write_methods = Array.new(0xc000)

  set_methods
end

Instance Method Details

#read_byte(addr) ⇒ Object



66
67
68
# File 'lib/rubyboy/cartridge/mbc1.rb', line 66

def read_byte(addr)
  @read_methods[addr].call
end

#set_methodsObject



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
# File 'lib/rubyboy/cartridge/mbc1.rb', line 20

def set_methods
  0xc000.times do |addr|
    @read_methods[addr] =
      case addr
      when 0x0000..0x3fff then -> { @rom.data[addr] }
      when 0x4000..0x7fff then -> { @rom.data[addr + (@rom_bank - 1) * 0x4000] }
      when 0xa000..0xbfff
        lambda do
          if @ram_enable
            if @ram_banking_mode
              @ram.eram[addr - 0xa000 + @ram_bank * 0x800]
            else
              @ram.eram[addr - 0xa000]
            end
          else
            0xff
          end
        end
      end
  end

  0xc000.times do |addr|
    @write_methods[addr] =
      case addr
      when 0x0000..0x1fff then ->(value) { @ram_enable = value & 0x0f == 0x0a }
      when 0x2000..0x3fff
        lambda do |value|
          @rom_bank = value & 0x1f
          @rom_bank = 1 if @rom_bank == 0
        end
      when 0x4000..0x5fff then ->(value) { @ram_bank = value & 0x03 }
      when 0x6000..0x7fff then ->(value) { @ram_banking_mode = value & 0x01 == 0x01 }
      when 0xa000..0xbfff
        lambda do |value|
          if @ram_enable
            if @ram_banking_mode
              @ram.eram[addr - 0xa000 + @ram_bank * 0x800] = value
            else
              @ram.eram[addr - 0xa000] = value
            end
          end
        end
      end
  end
end

#write_byte(addr, value) ⇒ Object



70
71
72
# File 'lib/rubyboy/cartridge/mbc1.rb', line 70

def write_byte(addr, value)
  @write_methods[addr].call(value)
end