2019-02-16 19:15:41 +00:00
|
|
|
package core6502
|
2019-01-26 16:05:51 +00:00
|
|
|
|
2019-05-17 21:28:20 +00:00
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
2019-02-09 23:15:14 +00:00
|
|
|
|
2019-02-28 22:54:38 +00:00
|
|
|
// https://www.masswerk.at/6502/6502_instruction_set.html
|
|
|
|
// http://www.emulator101.com/reference/6502-reference.html
|
|
|
|
// https://www.csh.rit.edu/~moffitt/docs/6502.html#FLAGS
|
|
|
|
// https://ia800509.us.archive.org/18/items/Programming_the_6502/Programming_the_6502.pdf
|
|
|
|
|
2020-01-11 16:17:39 +00:00
|
|
|
const (
|
|
|
|
maxInstructionSize = 3
|
|
|
|
)
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
// State represents the state of the simulated device
|
|
|
|
type State struct {
|
2020-01-11 16:17:39 +00:00
|
|
|
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!!
|
2019-01-26 16:05:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:00:53 +00:00
|
|
|
const (
|
2019-02-22 21:19:08 +00:00
|
|
|
vectorNMI uint16 = 0xfffa
|
2019-02-22 17:00:53 +00:00
|
|
|
vectorReset uint16 = 0xfffc
|
|
|
|
vectorBreak uint16 = 0xfffe
|
|
|
|
)
|
|
|
|
|
2019-01-28 23:06:15 +00:00
|
|
|
type opcode struct {
|
2019-02-10 13:01:57 +00:00
|
|
|
name string
|
2019-06-09 15:36:29 +00:00
|
|
|
bytes uint16
|
2019-02-10 13:01:57 +00:00
|
|
|
cycles int
|
|
|
|
addressMode int
|
|
|
|
action opFunc
|
2019-01-28 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
type opFunc func(s *State, line []uint8, opcode opcode)
|
2019-01-28 23:06:15 +00:00
|
|
|
|
2019-02-28 22:54:38 +00:00
|
|
|
func (s *State) executeLine(line []uint8) {
|
|
|
|
opcode := s.opcodes[line[0]]
|
2019-02-22 21:19:08 +00:00
|
|
|
if opcode.cycles == 0 {
|
|
|
|
panic(fmt.Sprintf("Unknown opcode 0x%02x\n", line[0]))
|
|
|
|
}
|
2019-01-26 17:57:03 +00:00
|
|
|
opcode.action(s, line, opcode)
|
|
|
|
}
|
2019-02-09 23:15:14 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
// ExecuteInstruction transforms the state given after a single instruction is executed.
|
2019-09-24 21:32:03 +00:00
|
|
|
func (s *State) ExecuteInstruction() {
|
2019-02-28 22:54:38 +00:00
|
|
|
pc := s.reg.getPC()
|
2020-08-14 15:19:24 +00:00
|
|
|
opcodeID := s.mem.PeekCode(pc)
|
2019-02-28 22:54:38 +00:00
|
|
|
opcode := s.opcodes[opcodeID]
|
2019-02-22 21:19:08 +00:00
|
|
|
|
|
|
|
if opcode.cycles == 0 {
|
|
|
|
panic(fmt.Sprintf("Unknown opcode 0x%02x\n", opcodeID))
|
|
|
|
}
|
2019-02-12 23:03:43 +00:00
|
|
|
|
2020-01-11 16:17:39 +00:00
|
|
|
if s.lineCache == nil {
|
|
|
|
s.lineCache = make([]uint8, maxInstructionSize)
|
|
|
|
}
|
2019-06-09 15:36:29 +00:00
|
|
|
for i := uint16(0); i < opcode.bytes; i++ {
|
2020-08-14 15:19:24 +00:00
|
|
|
s.lineCache[i] = s.mem.PeekCode(pc)
|
2019-02-12 23:03:43 +00:00
|
|
|
pc++
|
|
|
|
}
|
2019-02-28 22:54:38 +00:00
|
|
|
s.reg.setPC(pc)
|
2019-02-12 23:03:43 +00:00
|
|
|
|
2019-09-24 21:32:03 +00:00
|
|
|
if s.trace {
|
|
|
|
//fmt.Printf("%#04x %#02x\n", pc-opcode.bytes, opcodeID)
|
2020-01-11 16:17:39 +00:00
|
|
|
fmt.Printf("%#04x %-13s: ", pc-opcode.bytes, lineString(s.lineCache, opcode))
|
2019-02-10 16:49:11 +00:00
|
|
|
}
|
2020-01-11 16:17:39 +00:00
|
|
|
opcode.action(s, s.lineCache, opcode)
|
2019-05-09 22:09:15 +00:00
|
|
|
s.cycles += uint64(opcode.cycles)
|
2019-09-24 21:32:03 +00:00
|
|
|
if s.trace {
|
2020-04-02 21:52:23 +00:00
|
|
|
fmt.Printf("%v, [%02x]\n", s.reg, s.lineCache[0:opcode.bytes])
|
2019-02-10 16:49:11 +00:00
|
|
|
}
|
2019-02-10 15:25:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:29:56 +00:00
|
|
|
// Reset resets the processor. Moves the program counter to the vector in 0cfffc.
|
2019-02-28 22:54:38 +00:00
|
|
|
func (s *State) Reset() {
|
|
|
|
startAddress := getWord(s.mem, vectorReset)
|
2019-11-23 17:29:56 +00:00
|
|
|
s.cycles += 6
|
2019-02-28 22:54:38 +00:00
|
|
|
s.reg.setPC(startAddress)
|
2019-02-16 19:15:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 17:49:11 +00:00
|
|
|
// GetCycles returns the count of CPU cycles since last reset.
|
2019-05-09 22:09:15 +00:00
|
|
|
func (s *State) GetCycles() uint64 {
|
2019-05-04 17:49:11 +00:00
|
|
|
return s.cycles
|
|
|
|
}
|
|
|
|
|
2019-10-10 22:06:42 +00:00
|
|
|
// SetTrace activates tracing of the cpu execution
|
|
|
|
func (s *State) SetTrace(trace bool) {
|
|
|
|
s.trace = trace
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTrace gets trhe tracing state of the cpu execution
|
|
|
|
func (s *State) GetTrace() bool {
|
|
|
|
return s.trace
|
|
|
|
}
|
|
|
|
|
2020-06-07 16:23:39 +00:00
|
|
|
// GetPCAndSP returns the current program counter and stack pointer. Used to trace MLI calls
|
|
|
|
func (s *State) GetPCAndSP() (uint16, uint8) {
|
|
|
|
return s.reg.getPC(), s.reg.getSP()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCarryAndAcc returns the value of te carry flag and the accumulator. Used to trace MLI calls
|
|
|
|
func (s *State) GetCarryAndAcc() (bool, uint8) {
|
|
|
|
return s.reg.getFlag(flagC), s.reg.getA()
|
|
|
|
}
|
|
|
|
|
2019-05-17 21:28:20 +00:00
|
|
|
// Save saves the CPU state (registers and cycle counter)
|
2019-10-05 23:26:00 +00:00
|
|
|
func (s *State) Save(w io.Writer) error {
|
|
|
|
err := binary.Write(w, binary.BigEndian, s.cycles)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-17 21:28:20 +00:00
|
|
|
binary.Write(w, binary.BigEndian, s.reg.data)
|
2019-10-05 23:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-17 21:28:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads the CPU state (registers and cycle counter)
|
2019-10-05 23:26:00 +00:00
|
|
|
func (s *State) Load(r io.Reader) error {
|
|
|
|
err := binary.Read(r, binary.BigEndian, &s.cycles)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = binary.Read(r, binary.BigEndian, &s.reg.data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-17 21:28:20 +00:00
|
|
|
}
|