Refactored memory space to remove big method (#3)

Co-authored-by: Saf <saf@irkenkitties.com>
This commit is contained in:
Saf 2020-08-30 16:40:20 -07:00 committed by GitHub
parent c952f33d50
commit 90f3621081
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 11 deletions

View File

@ -92,21 +92,29 @@ module N65
def normalize_address(address)
case @type
when :prog
return (address - 0x8000) if address_inside_prog_rom1?(address)
return (address - 0xC000) if address_inside_prog_rom2?(address)
raise(AccessOutsideProgRom, format('Address $%.4X is outside PROG ROM', address))
normalize_prog_rom_address(address)
when :char
unless address_inside_char_rom?(address)
raise(AccessOutsideCharRom, format('Address $%.4X is outside CHAR ROM', address))
end
address
normalize_char_rom_address(address)
else
address
end
end
def normalize_prog_rom_address(address)
return (address - 0x8000) if address_inside_prog_rom1?(address)
return (address - 0xC000) if address_inside_prog_rom2?(address)
message = 'Address $%.4X is outside PROG ROM'
raise(AccessOutsideProgRom, format(message, address))
end
def normalize_char_rom_address(address)
return address if address_inside_char_rom?(address)
message = 'Address $%.4X is outside CHAR ROM'
raise(AccessOutsideCharRom, format(message, address))
end
def address_inside_prog_rom1?(address)
address >= 0x8000 && address < 0xC000
end

View File

@ -47,7 +47,7 @@ RSpec.describe(N65::MemorySpace) do
end
end
describe '#read' do
describe '#read prog rom' do
let(:address) { 0xC100 }
let(:mirroed_address) { 0x8100 }
let(:bank) { described_class.create_prog_rom }
@ -76,7 +76,27 @@ RSpec.describe(N65::MemorySpace) do
end
end
describe '#write' do
describe '#read char rom' do
let(:address) { 0x100 }
let(:bank) { described_class.create_char_rom }
let(:data) { 'hi there'.bytes }
before { bank.write(address, data) }
context 'when reading from the bank' do
it 'can read from the bank' do
expect(bank.read(address, data.size)).to eq(data)
end
end
context 'when attempting to read out of bounds' do
it 'throws an error' do
expect { bank.read(0x2000, 0x10) }.to raise_error(described_class::AccessOutsideCharRom)
end
end
end
describe '#write prog rom' do
let(:address) { 0xC100 }
let(:mirroed_address) { 0x8100 }
let(:bank) { described_class.create_prog_rom }
@ -104,4 +124,24 @@ RSpec.describe(N65::MemorySpace) do
end
end
end
describe '#write char rom' do
let(:address) { 0x100 }
let(:bank) { described_class.create_char_rom }
let(:data) { 'hi there'.bytes }
before { bank.write(address, data) }
context 'when reading from the bank' do
it 'can read from the bank' do
expect(bank.read(address, data.size)).to eq(data)
end
end
context 'when attempting to write out of bounds' do
it 'throws an error' do
expect { bank.write(0x2000, data) }.to raise_error(described_class::AccessOutsideCharRom)
end
end
end
end