Class: GBRb::MMU

Inherits:
Object
  • Object
show all
Defined in:
lib/gbrb/mmu.rb

Constant Summary collapse

SIZE =
0x10000
WORKING_RAM_ADDRESS =
0xc000
WORKING_RAM_LENGTH =
0x2000
WORKING_RAM_SHADOW_ADDRESS =
0xe000
WORKING_RAM_SHADOW_LENGTH =
0x1e00
ROM_BANK_0_ADDRESS =
0x0000
ROM_BANK_1_ADDRESS =
0x4000
GPU_START =
0x8000
GPU_END =
0x9fff

Instance Method Summary collapse

Constructor Details

#initialize(bios = [], cartridge = :none, gpu = nil) ⇒ MMU

Returns a new instance of MMU.



15
16
17
18
19
20
# File 'lib/gbrb/mmu.rb', line 15

def initialize bios=[], cartridge=:none, gpu=nil
  @storage = Array.new SIZE, 0
  load_cartridge cartridge unless cartridge == :none
  load_bios bios
  @gpu = gpu
end

Instance Method Details

#debug_ascii(start, stop) ⇒ Object



67
68
69
70
71
# File 'lib/gbrb/mmu.rb', line 67

def debug_ascii start, stop
  @storage[start..stop].map{|el| (0x20..0x7e).cover?(el) ? el.chr : "."}
                       .each_slice(0x10).map{|el| el.join}
                       .map{|el| "|" + el + "|"}
end

#debug_hex(start, stop) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/gbrb/mmu.rb', line 59

def debug_hex start, stop
  @storage[start..stop].map do |cell|
    cell.to_s(16)
        .rjust(2, "0")
  end.each_slice(0x08).map{|el| el.join(" ")}
     .each_slice(0x02).map{|el| el.join("  ")}
end

#debug_indicies(start, stop) ⇒ Object



53
54
55
56
57
# File 'lib/gbrb/mmu.rb', line 53

def debug_indicies start, stop
  (start...stop).step(0x10)
                .map{|el| el.to_s 16 }
                .map{|el| el.rjust(4, "0")}
end

#dump(start = 0, stop = SIZE) ⇒ Object



47
48
49
50
51
# File 'lib/gbrb/mmu.rb', line 47

def dump start=0, stop=SIZE
  debug_indicies(start, stop).zip(debug_hex(start, stop), debug_ascii(start, stop))
                .map{|row| row.join("  ")}
                .join("\n")
end

#read_byte(addr) ⇒ Object



22
23
24
25
# File 'lib/gbrb/mmu.rb', line 22

def read_byte addr
  raise unless (0..SIZE).cover? addr
  @storage[addr]
end

#read_word(addr) ⇒ Object



27
28
29
# File 'lib/gbrb/mmu.rb', line 27

def read_word addr
  (read_byte(addr+1) << 8) + read_byte(addr)
end

#resetObject



73
74
# File 'lib/gbrb/mmu.rb', line 73

def reset
end

#unload_biosObject



76
77
78
79
80
81
82
# File 'lib/gbrb/mmu.rb', line 76

def unload_bios
  @storage[0..(ROM_BANK_0_ADDRESS + MEMORY_BANK_SIZE)] = if @cartridge
    @cartridge.bank
  else
    Array.new(ROM_BANK_0_ADDRESS + MEMORY_BANK_SIZE){0}
  end
end

#write_byte(addr, value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/gbrb/mmu.rb', line 31

def write_byte addr, value
  @storage[addr] = value & 0xff
  if (WORKING_RAM_ADDRESS..WORKING_RAM_ADDRESS + WORKING_RAM_SHADOW_LENGTH).cover? addr
    copy_address = addr + WORKING_RAM_SHADOW_ADDRESS - WORKING_RAM_ADDRESS
    @storage[copy_address] = value & 0xff
  end
  if (GPU_START..GPU_END).cover? addr and @gpu
    @gpu.push [:mmu, addr]
  end
end

#write_word(addr, value) ⇒ Object



42
43
44
45
# File 'lib/gbrb/mmu.rb', line 42

def write_word addr, value
  write_byte addr, value
  write_byte addr+1, value >> 8
end