Avoid memory allocations, reuse an array from the state

This commit is contained in:
Ivan Izaguirre 2020-01-11 17:17:39 +01:00
parent 6387e93f06
commit c7cc5c15b4
1 changed files with 19 additions and 10 deletions

View File

@ -11,13 +11,20 @@ import (
// https://www.csh.rit.edu/~moffitt/docs/6502.html#FLAGS
// 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
type State struct {
reg registers
mem Memory
cycles uint64
opcodes *[256]opcode
trace bool
reg registers
mem Memory
cycles uint64
opcodes *[256]opcode
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 (
@ -54,21 +61,23 @@ func (s *State) ExecuteInstruction() {
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++ {
line[i] = s.mem.Peek(pc)
s.lineCache[i] = s.mem.Peek(pc)
pc++
}
s.reg.setPC(pc)
if s.trace {
//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)
if s.trace {
fmt.Printf("%v, [%02x]\n", s.reg, line)
fmt.Printf("%v, [%02x]\n", s.reg, s.lineCache)
}
}