izapple2/core6502/registers.go

92 lines
2.0 KiB
Go
Raw Normal View History

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
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
)
type registers struct {
2019-01-26 15:59:36 +00:00
data [8]uint8
}
func (r *registers) getRegister(i int) uint8 { return r.data[i] }
2019-01-26 15:59:36 +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
func (r *registers) setRegister(i int, v uint8) {
2019-01-26 15:59:36 +00:00
r.data[i] = v
}
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
func (r *registers) getPC() uint16 {
2019-01-26 15:59:36 +00:00
return uint16(r.data[regPC])*256 + uint16(r.data[regPC+1])
}
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
func (r *registers) getFlagBit(i uint8) uint8 {
if r.getFlag(i) {
return 1
}
2019-01-28 22:10:23 +00:00
return 0
}
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
}
func (r *registers) setFlag(i uint8) {
2019-01-27 22:03:08 +00:00
r.data[regP] |= i
}
func (r *registers) clearFlag(i uint8) {
2019-01-27 22:03:08 +00:00
r.data[regP] &^= i
}
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)
}
}
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
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())
}