From b95a41520cf3fbd5de98578f26b9a61f6f7c8c6c Mon Sep 17 00:00:00 2001 From: Colby Date: Tue, 9 Aug 2016 20:54:51 +1000 Subject: [PATCH] implement mbc3 functionality --- lib/waterfoul/mbc/mbc3.rb | 47 ++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/waterfoul/mbc/mbc3.rb b/lib/waterfoul/mbc/mbc3.rb index 5b845f1..b04131b 100644 --- a/lib/waterfoul/mbc/mbc3.rb +++ b/lib/waterfoul/mbc/mbc3.rb @@ -1,14 +1,51 @@ module Waterfoul module MBC class MBC3 - def self.[](i) - fail + def initialize(program) + @game_program = program + @ram_bank = 0 + @rom_bank = 0 + @ram_enable = false end - def self.[]=(i,v) - fail + def [](i) + case i + when 0x0...0x4000 # ROM Bank 0 + @game_program[i] + when 0x4000...0x8000 # ROM Bank n + addr = i - 0x4000 + offset = @rom_bank * 0x4000 + @game_program[offset + addr] + when 0xA000...0xC000 + if @ram_enabled + addr = i - 0xA000 + if @ram_bank == 0 + @ram[addr] + else + offset = @ram_bank * 0x8000 + @ram[offset + addr] + end + else + 0xFF + end + end + end + + def []=(i,v) + case i + when 0x0...0x2000 + @ram_enable = (v & 0xA == 0xA ? true : false) + when 0x2000...0x4000 + @rom_bank = (v & 0x7F) + @rom_bank += 1 if @rom_bank == 0 + when 0x4000...0x6000 + if v <= 0x3 + @ram_bank = (v & 0x3) + #elsif (0x8..0xC) === v + end + when 0x6000...0x8000 + end end end end end -