From 89aaa3e2cb718f34f05f7d11ee7326b9ff47d06b Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 5 Mar 2017 18:27:50 +0100 Subject: [PATCH] Let Cartridge.new return directly the memory bank controller * Cartridge does not provide any extra behavior * Avoids one indirection for every access --- lib/waterfoul/cartridge.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/waterfoul/cartridge.rb b/lib/waterfoul/cartridge.rb index 9ceac25..29c96d7 100644 --- a/lib/waterfoul/cartridge.rb +++ b/lib/waterfoul/cartridge.rb @@ -16,29 +16,26 @@ module Waterfoul # ninetndo logo, game title, manufacturer, rom size, ram size and more. # class Cartridge - extend Forwardable # location byte in memory (physically located on cartrdige) that declares the type of cartrdige CARTRIDGE_TYPE_MEM_LOC = 0x147 - # delegate any reads/writes to the memory bank controller - def_delegators :@mbc, :[], :[]= - def initialize(rom) + def self.new(rom) # get cartridge type byte from game program cartridge_type = rom[CARTRIDGE_TYPE_MEM_LOC] - # assign memory bank controller to cartridge - @mbc = cartrdige_controller cartridge_type, rom + # return memory bank controller to cartridge + cartrdige_controller cartridge_type, rom end private # initialize the memory bank controller given the game program and the controller type - def cartrdige_controller type, rom + def self.cartrdige_controller type, rom controller_const(type).new rom end # return the class constant that implements the behavior of the memory bank controller # declared by the game cartridge - def controller_const(type_byte) + def self.controller_const(type_byte) case type_byte when 0x00, 0x8, 0x9 MBC::ROM