mirror of
https://github.com/ivanizag/izapple2.git
synced 2025-01-04 18:30:39 +00:00
Avoid memory allocations, reuse an array from the state
This commit is contained in:
parent
6387e93f06
commit
c7cc5c15b4
@ -11,13 +11,20 @@ import (
|
|||||||
// https://www.csh.rit.edu/~moffitt/docs/6502.html#FLAGS
|
// https://www.csh.rit.edu/~moffitt/docs/6502.html#FLAGS
|
||||||
// https://ia800509.us.archive.org/18/items/Programming_the_6502/Programming_the_6502.pdf
|
// https://ia800509.us.archive.org/18/items/Programming_the_6502/Programming_the_6502.pdf
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxInstructionSize = 3
|
||||||
|
)
|
||||||
|
|
||||||
// State represents the state of the simulated device
|
// State represents the state of the simulated device
|
||||||
type State struct {
|
type State struct {
|
||||||
reg registers
|
reg registers
|
||||||
mem Memory
|
mem Memory
|
||||||
cycles uint64
|
cycles uint64
|
||||||
opcodes *[256]opcode
|
opcodes *[256]opcode
|
||||||
trace bool
|
trace bool
|
||||||
|
lineCache []uint8
|
||||||
|
// We cache the allocation of a line to avoid a malloc per instruction. To be used only
|
||||||
|
// by ExecuteInstruction(). 2x speedup on the emulation!!
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -54,21 +61,23 @@ func (s *State) ExecuteInstruction() {
|
|||||||
panic(fmt.Sprintf("Unknown opcode 0x%02x\n", opcodeID))
|
panic(fmt.Sprintf("Unknown opcode 0x%02x\n", opcodeID))
|
||||||
}
|
}
|
||||||
|
|
||||||
line := make([]uint8, opcode.bytes)
|
if s.lineCache == nil {
|
||||||
|
s.lineCache = make([]uint8, maxInstructionSize)
|
||||||
|
}
|
||||||
for i := uint16(0); i < opcode.bytes; i++ {
|
for i := uint16(0); i < opcode.bytes; i++ {
|
||||||
line[i] = s.mem.Peek(pc)
|
s.lineCache[i] = s.mem.Peek(pc)
|
||||||
pc++
|
pc++
|
||||||
}
|
}
|
||||||
s.reg.setPC(pc)
|
s.reg.setPC(pc)
|
||||||
|
|
||||||
if s.trace {
|
if s.trace {
|
||||||
//fmt.Printf("%#04x %#02x\n", pc-opcode.bytes, opcodeID)
|
//fmt.Printf("%#04x %#02x\n", pc-opcode.bytes, opcodeID)
|
||||||
fmt.Printf("%#04x %-13s: ", pc-opcode.bytes, lineString(line, opcode))
|
fmt.Printf("%#04x %-13s: ", pc-opcode.bytes, lineString(s.lineCache, opcode))
|
||||||
}
|
}
|
||||||
opcode.action(s, line, opcode)
|
opcode.action(s, s.lineCache, opcode)
|
||||||
s.cycles += uint64(opcode.cycles)
|
s.cycles += uint64(opcode.cycles)
|
||||||
if s.trace {
|
if s.trace {
|
||||||
fmt.Printf("%v, [%02x]\n", s.reg, line)
|
fmt.Printf("%v, [%02x]\n", s.reg, s.lineCache)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user