2019-02-16 19:15:41 +00:00
|
|
|
package core6502
|
2019-01-26 15:59:36 +00:00
|
|
|
|
2019-01-28 22:10:23 +00:00
|
|
|
import "fmt"
|
|
|
|
|
2019-01-26 15:59:36 +00:00
|
|
|
const (
|
2019-01-28 22:40:18 +00:00
|
|
|
regA = 0
|
|
|
|
regX = 1
|
|
|
|
regY = 2
|
|
|
|
regP = 4
|
|
|
|
regSP = 5
|
|
|
|
regPC = 6 // 2 bytes
|
|
|
|
regNone = -1
|
2019-01-26 15:59:36 +00:00
|
|
|
)
|
|
|
|
|
2019-01-27 22:03:08 +00:00
|
|
|
const (
|
|
|
|
flagN uint8 = 1 << 7
|
|
|
|
flagV uint8 = 1 << 6
|
2019-02-03 00:38:36 +00:00
|
|
|
flag5 uint8 = 1 << 5
|
2019-01-27 22:03:08 +00:00
|
|
|
flagB uint8 = 1 << 4
|
|
|
|
flagD uint8 = 1 << 3
|
|
|
|
flagI uint8 = 1 << 2
|
|
|
|
flagZ uint8 = 1 << 1
|
|
|
|
flagC uint8 = 1 << 0
|
|
|
|
)
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
type Registers struct {
|
2019-01-26 15:59:36 +00:00
|
|
|
data [8]uint8
|
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) getRegister(i int) uint8 { return r.data[i] }
|
2019-01-26 15:59:36 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) getA() uint8 { return r.data[regA] }
|
|
|
|
func (r *Registers) getX() uint8 { return r.data[regX] }
|
|
|
|
func (r *Registers) getY() uint8 { return r.data[regY] }
|
|
|
|
func (r *Registers) getP() uint8 { return r.data[regP] }
|
|
|
|
func (r *Registers) getSP() uint8 { return r.data[regSP] }
|
2019-01-26 17:57:03 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) setRegister(i int, v uint8) {
|
2019-01-26 15:59:36 +00:00
|
|
|
r.data[i] = v
|
|
|
|
}
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) setA(v uint8) { r.setRegister(regA, v) }
|
|
|
|
func (r *Registers) setX(v uint8) { r.setRegister(regX, v) }
|
|
|
|
func (r *Registers) setY(v uint8) { r.setRegister(regY, v) }
|
|
|
|
func (r *Registers) setP(v uint8) { r.setRegister(regP, v) }
|
|
|
|
func (r *Registers) setSP(v uint8) { r.setRegister(regSP, v) }
|
2019-01-26 15:59:36 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) getPC() uint16 {
|
2019-01-26 15:59:36 +00:00
|
|
|
return uint16(r.data[regPC])*256 + uint16(r.data[regPC+1])
|
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) setPC(v uint16) {
|
2019-01-26 15:59:36 +00:00
|
|
|
r.data[regPC] = uint8(v >> 8)
|
|
|
|
r.data[regPC+1] = uint8(v)
|
|
|
|
}
|
2019-01-27 22:03:08 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) getFlagBit(i uint8) uint8 {
|
2019-01-27 22:49:16 +00:00
|
|
|
if r.getFlag(i) {
|
|
|
|
return 1
|
|
|
|
}
|
2019-01-28 22:10:23 +00:00
|
|
|
return 0
|
2019-01-27 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) getFlag(i uint8) bool {
|
2019-01-28 22:10:23 +00:00
|
|
|
return (r.data[regP] & i) != 0
|
2019-01-27 22:03:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) setFlag(i uint8) {
|
2019-01-27 22:03:08 +00:00
|
|
|
r.data[regP] |= i
|
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) clearFlag(i uint8) {
|
2019-01-27 22:03:08 +00:00
|
|
|
r.data[regP] &^= i
|
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) updateFlag(i uint8, v bool) {
|
2019-01-27 22:03:08 +00:00
|
|
|
if v {
|
|
|
|
r.setFlag(i)
|
|
|
|
} else {
|
|
|
|
r.clearFlag(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r *Registers) updateFlagZN(t uint8) {
|
2019-01-27 22:03:08 +00:00
|
|
|
r.updateFlag(flagZ, t == 0)
|
|
|
|
r.updateFlag(flagN, t >= (1<<7))
|
|
|
|
}
|
2019-01-28 22:10:23 +00:00
|
|
|
|
2019-02-16 19:15:41 +00:00
|
|
|
func (r Registers) String() string {
|
2019-01-28 22:10:23 +00:00
|
|
|
return fmt.Sprintf("A: %#02x, X: %#02x, Y: %#02x, SP: %#02x, PC: %#04x, P: %#02x, (NV-BDIZC): %08b",
|
|
|
|
r.getA(), r.getX(), r.getY(), r.getSP(), r.getPC(), r.getP(), r.getP())
|
|
|
|
}
|