2020-10-03 21:38:26 +00:00
|
|
|
package izapple2
|
2019-03-02 19:41:25 +00:00
|
|
|
|
2021-03-14 17:46:52 +00:00
|
|
|
import (
|
2021-05-09 17:48:54 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-03-14 17:46:52 +00:00
|
|
|
"github.com/ivanizag/izapple2/storage"
|
|
|
|
)
|
2020-10-17 18:10:48 +00:00
|
|
|
|
2020-10-13 22:26:47 +00:00
|
|
|
// Card represents an Apple II card to be inserted in a slot
|
|
|
|
type Card interface {
|
2019-10-02 21:39:39 +00:00
|
|
|
loadRom(data []uint8)
|
2019-05-18 14:43:51 +00:00
|
|
|
assign(a *Apple2, slot int)
|
2020-10-27 23:43:33 +00:00
|
|
|
reset()
|
2020-10-13 22:26:47 +00:00
|
|
|
|
|
|
|
GetName() string
|
|
|
|
GetInfo() map[string]string
|
2019-05-18 14:43:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:41:25 +00:00
|
|
|
type cardBase struct {
|
2020-10-14 19:54:51 +00:00
|
|
|
a *Apple2
|
|
|
|
name string
|
2021-03-14 17:46:52 +00:00
|
|
|
romCsxx memoryHandler
|
|
|
|
romC8xx memoryHandler
|
|
|
|
romCxxx memoryHandler
|
2020-10-14 19:54:51 +00:00
|
|
|
|
2019-09-28 11:37:42 +00:00
|
|
|
slot int
|
2019-10-20 22:00:42 +00:00
|
|
|
_ssr [16]softSwitchR
|
|
|
|
_ssw [16]softSwitchW
|
|
|
|
_ssrName [16]string
|
|
|
|
_sswName [16]string
|
2019-03-02 19:41:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 22:26:47 +00:00
|
|
|
func (c *cardBase) GetName() string {
|
|
|
|
return c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cardBase) GetInfo() map[string]string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 23:43:33 +00:00
|
|
|
func (c *cardBase) reset() {
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
|
2020-10-14 19:54:51 +00:00
|
|
|
func (c *cardBase) loadRomFromResource(resource string) {
|
2020-11-03 16:51:12 +00:00
|
|
|
data, _, err := storage.LoadResource(resource)
|
2020-10-14 19:54:51 +00:00
|
|
|
if err != nil {
|
|
|
|
// The resource should be internal and never fail
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
c.loadRom(data)
|
|
|
|
}
|
|
|
|
|
2019-10-02 21:39:39 +00:00
|
|
|
func (c *cardBase) loadRom(data []uint8) {
|
2019-05-18 14:43:51 +00:00
|
|
|
if c.a != nil {
|
2019-10-05 23:26:00 +00:00
|
|
|
panic("Assert failed. Rom must be loaded before inserting the card in the slot")
|
2019-05-18 14:43:51 +00:00
|
|
|
}
|
2020-03-08 22:39:25 +00:00
|
|
|
if len(data) == 0x100 {
|
|
|
|
// Just 256 bytes in Cs00
|
2020-09-23 16:19:15 +00:00
|
|
|
c.romCsxx = newMemoryRangeROM(0, data, "Slot ROM")
|
2021-03-14 17:46:52 +00:00
|
|
|
} else if len(data) == 0x400 {
|
|
|
|
// The file has C800 to CBFF for ROM
|
|
|
|
// The 256 bytes in Cx00 are copied from the last page in C800-CBFF
|
|
|
|
// Used on the Videx 80 columns card
|
|
|
|
c.romCsxx = newMemoryRangeROM(0, data[0x300:], "Slot ROM")
|
|
|
|
c.romC8xx = newMemoryRangeROM(0xc800, data, "Slot C8 ROM")
|
2020-03-08 22:39:25 +00:00
|
|
|
} else if len(data) == 0x800 {
|
2021-03-14 17:46:52 +00:00
|
|
|
// The file has C800 to CFFF
|
2020-03-08 22:39:25 +00:00
|
|
|
// The 256 bytes in Cx00 are copied from the first page in C800
|
2020-10-13 22:26:47 +00:00
|
|
|
c.romCsxx = newMemoryRangeROM(0, data, "Slot ROM")
|
2020-09-23 16:19:15 +00:00
|
|
|
c.romC8xx = newMemoryRangeROM(0xc800, data, "Slot C8 ROM")
|
2020-03-08 22:39:25 +00:00
|
|
|
} else if len(data) == 0x1000 {
|
|
|
|
// The file covers the full Cxxx range. Only showing the page
|
|
|
|
// corresponding to the slot used.
|
2020-09-23 16:19:15 +00:00
|
|
|
c.romCxxx = newMemoryRangeROM(0xc000, data, "Slot ROM")
|
2020-03-08 22:39:25 +00:00
|
|
|
} else {
|
|
|
|
panic("Invalid ROM size")
|
2019-09-28 11:37:42 +00:00
|
|
|
}
|
2019-05-18 14:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cardBase) assign(a *Apple2, slot int) {
|
2019-05-10 16:07:36 +00:00
|
|
|
c.a = a
|
2019-03-02 19:41:25 +00:00
|
|
|
c.slot = slot
|
2020-03-08 22:39:25 +00:00
|
|
|
if slot != 0 {
|
|
|
|
if c.romCsxx != nil {
|
|
|
|
// Relocate to the assigned slot
|
2020-08-14 15:19:24 +00:00
|
|
|
c.romCsxx.setBase(uint16(0xc000 + slot*0x100))
|
2020-03-08 22:39:25 +00:00
|
|
|
a.mmu.setCardROM(slot, c.romCsxx)
|
|
|
|
}
|
|
|
|
if c.romC8xx != nil {
|
|
|
|
a.mmu.setCardROMExtra(slot, c.romC8xx)
|
|
|
|
}
|
|
|
|
if c.romCxxx != nil {
|
|
|
|
a.mmu.setCardROM(slot, c.romCxxx)
|
|
|
|
a.mmu.setCardROMExtra(slot, c.romCxxx)
|
|
|
|
}
|
2019-09-28 11:37:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:41:25 +00:00
|
|
|
for i := 0; i < 0x10; i++ {
|
2020-08-30 19:11:43 +00:00
|
|
|
if c._ssr[i] != nil {
|
|
|
|
a.io.addSoftSwitchR(uint8(0xC80+slot*0x10+i), c._ssr[i], c._ssrName[i])
|
|
|
|
}
|
|
|
|
if c._ssw[i] != nil {
|
|
|
|
a.io.addSoftSwitchW(uint8(0xC80+slot*0x10+i), c._ssw[i], c._sswName[i])
|
|
|
|
}
|
2019-03-02 19:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-18 14:43:51 +00:00
|
|
|
|
2019-10-20 22:00:42 +00:00
|
|
|
func (c *cardBase) addCardSoftSwitchR(address uint8, ss softSwitchR, name string) {
|
|
|
|
c._ssr[address] = ss
|
|
|
|
c._ssrName[address] = name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cardBase) addCardSoftSwitchW(address uint8, ss softSwitchW, name string) {
|
|
|
|
c._ssw[address] = ss
|
|
|
|
c._sswName[address] = name
|
|
|
|
}
|
2021-05-09 17:48:54 +00:00
|
|
|
|
|
|
|
type softSwitches func(io *ioC0Page, address uint8, data uint8, write bool) uint8
|
|
|
|
|
|
|
|
func (c *cardBase) addCardSoftSwitches(sss softSwitches, name string) {
|
|
|
|
|
|
|
|
for i := uint8(0x0); i <= 0xf; i++ {
|
|
|
|
address := i
|
|
|
|
c.addCardSoftSwitchR(address, func(io *ioC0Page) uint8 {
|
|
|
|
return sss(io, address, 0, false)
|
2021-05-10 20:13:55 +00:00
|
|
|
}, fmt.Sprintf("%v%XR", name, address))
|
2021-05-09 17:48:54 +00:00
|
|
|
c.addCardSoftSwitchW(address, func(io *ioC0Page, value uint8) {
|
|
|
|
sss(io, address, value, true)
|
2021-05-10 20:13:55 +00:00
|
|
|
}, fmt.Sprintf("%v%XW", name, address))
|
2021-05-09 17:48:54 +00:00
|
|
|
}
|
|
|
|
}
|