Refactor ROM loading. Preparation for alt roms.

This commit is contained in:
Ivan Izaguirre 2019-06-07 20:01:20 +02:00
parent b847cd34e6
commit d079ec3d2b
3 changed files with 34 additions and 33 deletions

View File

@ -3,10 +3,11 @@ package apple2
import "github.com/ivanizag/apple2/core6502"
// NewApple2 instantiates an apple2
func NewApple2(romFile string, charRomFile string, clockMhz float64,
func NewApple2(charRomFile string, clockMhz float64,
isColor bool, fastMode bool, panicSS bool) *Apple2 {
var a Apple2
a.mmu = newMemoryManager(&a, romFile)
a.mmu = newMemoryManager(&a)
a.cpu = core6502.NewNMOS6502(a.mmu)
if charRomFile != "" {
a.cg = NewCharacterGenerator(charRomFile)
@ -35,6 +36,34 @@ func (a *Apple2) insertCard(c card, slot int) {
a.cards[slot] = c
}
const (
apple2RomSize = 12 * 1024
apple2eRomSize = 16 * 1024
)
// LoadRom loads a standard Apple2+ or 2e ROM
func (a *Apple2) LoadRom(filename string) {
data := loadResource(filename)
size := len(data)
if size != apple2RomSize && size != apple2eRomSize {
panic("Rom size not supported")
}
romStart := 0
mmu := a.mmu
if size == apple2eRomSize {
// The extra 4kb ROM is first in the rom file.
// It starts with 256 unused bytes not mapped to 0xc000.
a.isApple2e = true
extraRomSize := apple2eRomSize - apple2RomSize
mmu.physicalROMe = newMemoryRange(0xc000, data[0:extraRomSize])
romStart = extraRomSize
}
mmu.physicalROM = newMemoryRange(0xd000, data[romStart:])
mmu.resetRomPaging()
}
// AddDisk2 insterts a DiskII controller
func (a *Apple2) AddDisk2(slot int, diskRomFile string, diskImage string) {
var c cardDisk2

View File

@ -68,7 +68,8 @@ func MainApple() *Apple2 {
return nil
}
a := NewApple2(*romFile, *charRomFile, *cpuClock, !*mono, *fastDisk, *panicSS)
a := NewApple2(*charRomFile, *cpuClock, !*mono, *fastDisk, *panicSS)
a.LoadRom(*romFile)
if *languageCardSlot >= 0 {
a.AddLanguageCard(*languageCardSlot)
}

View File

@ -101,46 +101,17 @@ func (mmu *memoryManager) resetBaseRamPaging() {
mmu.setPages(0x00, 0xbf, mmu.physicalMainRAM)
}
func newMemoryManager(a *Apple2, romFile string) *memoryManager {
func newMemoryManager(a *Apple2) *memoryManager {
var mmu memoryManager
mmu.apple2 = a
ram := make([]uint8, 0xc000) // Reserve 48kb
mmu.physicalMainRAM = newMemoryRange(0, ram)
mmu.loadRom(romFile)
mmu.resetBaseRamPaging()
mmu.resetRomPaging()
return &mmu
}
const (
apple2RomSize = 12 * 1024
apple2eRomSize = 16 * 1024
)
func (mmu *memoryManager) loadRom(filename string) {
data := loadResource(filename)
size := len(data)
if size != apple2RomSize && size != apple2eRomSize {
panic("Rom size not supported")
}
a := mmu.apple2
romStart := 0
if size == apple2eRomSize {
// The extra 4kb ROM is first in the rom file.
// It starts with 256 unused bytes not mapped to 0xc000.
a.isApple2e = true
extraRomSize := apple2eRomSize - apple2RomSize
mmu.physicalROMe = newMemoryRange(0xc000, data[0:extraRomSize])
romStart = extraRomSize
}
mmu.physicalROM = newMemoryRange(0xd000, data[romStart:])
}
func (mmu *memoryManager) save(w io.Writer) {
mmu.physicalMainRAM.save(w)
}