-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
162 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cart | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/maxfierke/gogo-gb/cart/mbc" | ||
"github.com/maxfierke/gogo-gb/mem" | ||
) | ||
|
||
var ( | ||
ErrUnsupportedMbc = errors.New("unsupported or unknown MBC type") | ||
) | ||
|
||
type Cartridge struct { | ||
Header Header | ||
mbc mem.MemHandler | ||
} | ||
|
||
func NewCartridge(r *Reader) (*Cartridge, error) { | ||
cartridge := new(Cartridge) | ||
cartridge.Header = r.Header | ||
|
||
rom := make([]byte, r.Header.RomSizeBytes()) | ||
copy(rom, r.headerBuf[:]) | ||
|
||
switch r.Header.CartType { | ||
case CART_TYPE_MBC0: | ||
cartridge.mbc = mbc.NewMBC0(rom) | ||
default: | ||
return nil, fmt.Errorf("unsupported or unknown MBC type: %s", r.Header.CartTypeName()) | ||
} | ||
|
||
return cartridge, nil | ||
} | ||
|
||
func (c *Cartridge) DebugPrint() { | ||
c.Header.DebugPrint() | ||
} | ||
|
||
func (c *Cartridge) OnRead(mmu *mem.MMU, addr uint16) mem.MemRead { | ||
return c.mbc.OnRead(mmu, addr) | ||
} | ||
|
||
func (c *Cartridge) OnWrite(mmu *mem.MMU, addr uint16, value byte) mem.MemWrite { | ||
return c.mbc.OnWrite(mmu, addr, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package mbc | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/maxfierke/gogo-gb/mem" | ||
) | ||
|
||
const ( | ||
mbc0_rom_bank_start = 0x0000 | ||
mbc0_rom_bank_end = 0x7FFF | ||
mbc0_ram_bank_start = 0xA000 | ||
mbc0_ram_bank_end = 0xBFFF | ||
) | ||
|
||
type MBC0 struct { | ||
rom []byte | ||
} | ||
|
||
func NewMBC0(rom []byte) *MBC0 { | ||
return &MBC0{rom: rom} | ||
} | ||
|
||
func (m *MBC0) OnRead(mmu *mem.MMU, addr uint16) mem.MemRead { | ||
if addr <= mbc0_rom_bank_end { | ||
return mem.ReadReplace(m.rom[addr]) | ||
} | ||
|
||
return mem.ReadPassthrough() | ||
} | ||
|
||
func (m *MBC0) OnWrite(mmu *mem.MMU, addr uint16, value byte) mem.MemWrite { | ||
if addr <= mbc0_rom_bank_end { | ||
// Put the Read-Only in ROM | ||
return mem.WriteBlock() | ||
} | ||
|
||
if addr >= mbc0_ram_bank_start && addr <= mbc0_ram_bank_end { | ||
// RAM is RAM and this is a fake cartridge, so... | ||
return mem.WritePassthrough() | ||
} | ||
|
||
panic(fmt.Sprintf("Attempting to write 0x%x @ 0x%x, which is out-of-bounds for MBC0", value, addr)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters