Replaced MemoryMap with an PageTable array

There was no good reason to be using a map when an array lookup would have
done nicely.
This commit is contained in:
Will Angenent 2018-05-09 10:27:43 +01:00
parent 481ddf0ebf
commit 9c9f826c6a
6 changed files with 30 additions and 31 deletions

View File

@ -30,7 +30,7 @@ var disableBell *bool
func update(screen *ebiten.Image) error {
cpu.Run(&cpuState, *showInstructions, nil, *disableBell, 1024000/60)
return vid.DrawTextScreen(cpuState.MemoryMap, screen, charMap)
return vid.DrawTextScreen(cpuState.PageTable, screen, charMap)
}
func main() {
@ -43,12 +43,12 @@ func main() {
mmu.InitApple2eROM(memory)
cpuState.Memory = memory
cpuState.MemoryMap = &memory.MemoryMap
cpuState.PageTable = &memory.PageTable
cpuState.Init()
bootVector := 0xfffc
lsb := (*cpuState.MemoryMap)[uint8(bootVector>>8)][uint8(bootVector&0xff)] // TODO move readMemory to mmu
msb := (*cpuState.MemoryMap)[uint8((bootVector+1)>>8)][uint8((bootVector+1)&0xff)]
lsb := cpuState.PageTable[bootVector>>8][bootVector&0xff] // TODO move readMemory to mmu
msb := cpuState.PageTable[(bootVector+1)>>8][(bootVector+1)&0xff]
cpuState.PC = uint16(lsb) + uint16(msb)<<8
var err error

View File

@ -64,11 +64,11 @@ func main() {
RomPretendingToBeRAM[i] = bytes[0xc000+i]
}
for i := 0x0; i < 0x40; i++ {
memory.MemoryMap[0xc0+uint8(i)] = RomPretendingToBeRAM[i*0x100 : i*0x100+0x100]
memory.PageTable[0xc0+i] = RomPretendingToBeRAM[i*0x100 : i*0x100+0x100]
}
s.Memory = memory
s.MemoryMap = &memory.MemoryMap
s.PageTable = &memory.PageTable
var breakAddress *uint16
if *breakAddressString != "" {

View File

@ -25,7 +25,7 @@ var (
type State struct {
Memory *mmu.Memory
MemoryMap *mmu.MemoryMap // For easy access, this is a shortcut for Memory.MemoryMap
PageTable *mmu.PageTable // For easy access, this is a shortcut for Memory.PageTable
pendingInterrupt bool
pendingNMI bool
A uint8
@ -103,14 +103,14 @@ func (s *State) isN() bool {
}
func push8(s *State, value uint8) {
(*s.MemoryMap)[mmu.StackPage][s.SP] = value
s.PageTable[mmu.StackPage][s.SP] = value
s.SP -= 1
s.SP &= 0xff
}
func push16(s *State, value uint16) {
(*s.MemoryMap)[mmu.StackPage][s.SP] = uint8(value >> 8)
(*s.MemoryMap)[mmu.StackPage][s.SP-1] = uint8(value & 0xff)
s.PageTable[mmu.StackPage][s.SP] = uint8(value >> 8)
s.PageTable[mmu.StackPage][s.SP-1] = uint8(value & 0xff)
s.SP -= 2
s.SP &= 0xff
}
@ -118,14 +118,14 @@ func push16(s *State, value uint16) {
func pop8(s *State) uint8 {
s.SP += 1
s.SP &= 0xff
return (*s.MemoryMap)[mmu.StackPage][s.SP]
return s.PageTable[mmu.StackPage][s.SP]
}
func pop16(s *State) uint16 {
s.SP += 2
s.SP &= 0xff
msb := uint16((*s.MemoryMap)[mmu.StackPage][s.SP])
lsb := uint16((*s.MemoryMap)[mmu.StackPage][s.SP-1])
msb := uint16(s.PageTable[mmu.StackPage][s.SP])
lsb := uint16(s.PageTable[mmu.StackPage][s.SP-1])
return lsb + msb<<8
}
@ -135,7 +135,7 @@ func readMemory(s *State, address uint16) uint8 {
return 0
}
return (*s.MemoryMap)[uint8(address>>8)][uint8(address&0xff)]
return s.PageTable[address>>8][address&0xff]
}
// Handle a write to a magic test address that triggers an interrupt and/or an NMI
@ -156,7 +156,7 @@ func writeInterruptTestOpenCollector(s *State, address uint16, value uint8) {
s.pendingNMI = NMI
}
(*s.MemoryMap)[uint8(address>>8)][uint8(address&0xff)] = value
s.PageTable[address>>8][address&0xff] = value
}
func writeMemory(s *State, address uint16, value uint8) {
@ -176,7 +176,7 @@ func writeMemory(s *State, address uint16, value uint8) {
return
}
(*s.MemoryMap)[uint8(address>>8)][uint8(address&0xff)] = value
s.PageTable[uint8(address>>8)][uint8(address&0xff)] = value
if RunningFunctionalTests && address == 0x200 {
testNumber := readMemory(s, 0x200)

View File

@ -37,7 +37,7 @@ func printInstruction(s *State, instruction string) {
}
func PrintInstruction(s *State) {
opcodeValue := (*s.MemoryMap)[uint8((s.PC)>>8)][uint8((s.PC)&0xff)]
opcodeValue := s.PageTable[(s.PC)>>8][(s.PC)&0xff]
opcode := OpCodes[opcodeValue]
mnemonic := opcode.Mnemonic
size := opcode.AddressingMode.OperandSize
@ -53,7 +53,7 @@ func PrintInstruction(s *State) {
var suffix string
if opcode.AddressingMode.Mode == AmRelative {
value = uint16((*s.MemoryMap)[uint8((s.PC+1)>>8)][uint8((s.PC+1)&0xff)])
value = uint16(s.PageTable[(s.PC+1)>>8][(s.PC+1)&0xff])
var relativeAddress uint16
if (value & 0x80) == 0 {
relativeAddress = s.PC + 2 + uint16(value)
@ -64,12 +64,12 @@ func PrintInstruction(s *State) {
suffix = fmt.Sprintf(stringFormat, relativeAddress)
opcodes = fmt.Sprintf("%02x %02x ", opcodeValue, value)
} else if size == 1 {
value = uint16((*s.MemoryMap)[uint8((s.PC+1)>>8)][uint8((s.PC+1)&0xff)])
value = uint16(s.PageTable[(s.PC+1)>>8][(s.PC+1)&0xff])
suffix = fmt.Sprintf(stringFormat, value)
opcodes = fmt.Sprintf("%02x %02x ", opcodeValue, value)
} else if size == 2 {
lsb := (*s.MemoryMap)[uint8((s.PC+1)>>8)][uint8((s.PC+1)&0xff)]
msb := (*s.MemoryMap)[uint8((s.PC+2)>>8)][uint8((s.PC+2)&0xff)]
lsb := s.PageTable[(s.PC+1)>>8][(s.PC+1)&0xff]
msb := s.PageTable[(s.PC+2)>>8][(s.PC+2)&0xff]
value = uint16(lsb) + uint16(msb)*0x100
suffix = fmt.Sprintf(stringFormat, value)
opcodes = fmt.Sprintf("%02x %02x %02x ", opcodeValue, lsb, msb)
@ -90,7 +90,7 @@ func DumpMemory(s *State, offset uint16) {
}
fmt.Printf("%04x ", offset+i)
}
fmt.Printf(" %02x", (*s.MemoryMap)[uint8((offset+i)>>8)][uint8((offset+i)&0xff)])
fmt.Printf(" %02x", s.PageTable[(offset+i)>>8][(offset+i)&0xff])
}
fmt.Print("\n")
}

View File

@ -39,22 +39,22 @@ type PhysicalMemory struct {
RomC2 [0x1000]uint8
}
type MemoryMap map[uint8][]uint8
type PageTable [0x100][]uint8
type Memory struct {
MemoryMap MemoryMap
PageTable PageTable
PhysicalMemory PhysicalMemory
}
func MapFirstHalfOfIO(m *Memory) {
for i := 0x1; i < 0x10; i++ {
m.MemoryMap[uint8(i)+0xc0] = m.PhysicalMemory.RomC1[i*0x100 : i*0x100+0x100]
m.PageTable[i+0xc0] = m.PhysicalMemory.RomC1[i*0x100 : i*0x100+0x100]
}
}
func MapSecondHalfOfIO(m *Memory) {
for i := 0x1; i < 0x10; i++ {
m.MemoryMap[uint8(i)+0xc0] = m.PhysicalMemory.RomC2[i*0x100 : i*0x100+0x100]
m.PageTable[i+0xc0] = m.PhysicalMemory.RomC2[i*0x100 : i*0x100+0x100]
}
}
@ -84,17 +84,16 @@ func InitApple2eROM(m *Memory) {
// Map 0xd000-0xffff
for i := 0x0; i < 0x30; i++ {
m.MemoryMap[uint8(i)+0xd0] = m.PhysicalMemory.UpperROM[i*0x100 : i*0x100+0x100]
m.PageTable[i+0xd0] = m.PhysicalMemory.UpperROM[i*0x100 : i*0x100+0x100]
}
}
func InitRAM() (memory *Memory) {
memory = new(Memory)
memory.MemoryMap = make(MemoryMap)
// Map main RAM
for i := 0x0; i < 0xc0; i++ {
memory.MemoryMap[uint8(i)] = memory.PhysicalMemory.MainMemory[i*0x100 : i*0x100+0x100]
memory.PageTable[i] = memory.PhysicalMemory.MainMemory[i*0x100 : i*0x100+0x100]
}
return

View File

@ -19,7 +19,7 @@ var (
flashOn bool
)
func DrawTextScreen(memory *mmu.MemoryMap, screen *ebiten.Image, charMap *ebiten.Image) error {
func DrawTextScreen(pageTable *mmu.PageTable, screen *ebiten.Image, charMap *ebiten.Image) error {
flashCounter--
if flashCounter < 0 {
flashCounter = flashFrames
@ -34,7 +34,7 @@ func DrawTextScreen(memory *mmu.MemoryMap, screen *ebiten.Image, charMap *ebiten
base := 128*(y%8) + 40*(y/8)
for x := 0; x < 40; x++ {
offset := textVideoMemory + base + x
value := (*memory)[uint8(offset>>8)][uint8(offset&0xff)]
value := (*pageTable)[uint8(offset>>8)][uint8(offset&0xff)]
inverted := false
if (value & 0xc0) == 0 {