Skip to content

Commit

Permalink
Add echo RAM and umapped RAM region handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierke committed Jan 7, 2024
1 parent 0c05705 commit 3477c07
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
11 changes: 8 additions & 3 deletions hardware/dmg.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,22 @@ func NewDMGDebug(debugger debug.Debugger) (*DMG, error) {

ram := make([]byte, DMG_RAM_SIZE)
mmu := mem.NewMMU(ram)
echo := mem.NewEchoRegion()
unmapped := mem.NewUnmappedRegion()

mmu.AddHandler(mem.MemRegion{Start: 0x0000, End: 0xFFFF}, debugger)

mmu.AddHandler(mem.MemRegion{Start: 0x0000, End: 0x7FFF}, cartridge) // MBCs ROM Banks
mmu.AddHandler(mem.MemRegion{Start: 0xA000, End: 0xBFFF}, cartridge) // MBCs RAM Banks

mmu.AddHandler(mem.MemRegion{Start: 0xFF40, End: 0xFF4B}, lcd) // LCD control registers
mmu.AddHandler(mem.MemRegion{Start: 0xE000, End: 0xFDFF}, echo) // Echo RAM (mirrors WRAM)
mmu.AddHandler(mem.MemRegion{Start: 0xFEA0, End: 0xFEFF}, unmapped) // Nop writes, zero reads

mmu.AddHandler(mem.MemRegion{Start: 0xFF01, End: 0xFF02}, serial) // Serial Port (Control & Data)
mmu.AddHandler(mem.MemRegion{Start: 0xFF40, End: 0xFF4B}, lcd) // LCD control registers

mmu.AddHandler(mem.MemRegion{Start: 0xFFFF, End: 0xFFFF}, ic)
mmu.AddHandler(mem.MemRegion{Start: 0xFF0F, End: 0xFF0F}, ic)
mmu.AddHandler(mem.MemRegion{Start: 0xFF01, End: 0xFF02}, serial)
mmu.AddHandler(mem.MemRegion{Start: 0xFFFF, End: 0xFFFF}, ic)

return &DMG{
cpu: cpu,
Expand Down
29 changes: 29 additions & 0 deletions mem/mmu.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ type MemBus interface {
Write16(addr uint16, value uint16)
}

type EchoRegion struct{}

func NewEchoRegion() *EchoRegion {
return &EchoRegion{}
}

func (umr *EchoRegion) OnRead(mmu *MMU, addr uint16) MemRead {
// Echo mirrors 0xC000
return ReadReplace(mmu.ram[addr-0x2000])
}

func (umr *EchoRegion) OnWrite(mmu *MMU, addr uint16, value byte) MemWrite {
return WriteBlock()
}

type UnmappedRegion struct{}

func NewUnmappedRegion() *UnmappedRegion {
return &UnmappedRegion{}
}

func (umr *UnmappedRegion) OnRead(mmu *MMU, addr uint16) MemRead {
return ReadReplace(0x00)
}

func (umr *UnmappedRegion) OnWrite(mmu *MMU, addr uint16, value byte) MemWrite {
return WriteBlock()
}

func NewMMU(ram []byte) *MMU {
return &MMU{
ram: ram,
Expand Down

0 comments on commit 3477c07

Please sign in to comment.