SwiftNES/Sources/CPU/CPU_Instructions.swift

287 lines
6.8 KiB
Swift
Raw Normal View History

2024-05-11 22:30:17 -05:00
extension CPU {
func lda(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, pageCross) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
let value = memRead(addr)
setRegisterA(value)
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
}
func ldy(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, pageCross) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
let data = memRead(addr)
register_y = data
updateZeroAndNegativeFlags(register_y)
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
}
func ldx(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, pageCross) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
let data = memRead(addr)
register_x = data
updateZeroAndNegativeFlags(register_x)
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
}
func sta(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
memWrite(addr, data: register_a)
}
func and(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, pageCross) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
let data = memRead(addr)
self.setRegisterA(data & register_a)
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
}
func eor(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, pageCross) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
let data = memRead(addr)
setRegisterA(data ^ register_a)
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
}
func ora(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, pageCross) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
let data = memRead(addr)
setRegisterA(data | register_a)
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
}
func tax() {
register_x = register_a
updateZeroAndNegativeFlags(register_x)
}
func inx() {
register_x = register_x &+ 1
updateZeroAndNegativeFlags(register_x)
}
func iny() {
register_y = register_y &+ 1
updateZeroAndNegativeFlags(register_y)
}
func sbc(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, pageCross) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
let data = memRead(addr)
2024-05-17 13:15:10 -05:00
let res = Int8(bitPattern: data) &* -1
addToRegisterA(UInt8(bitPattern: res &- 1))
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
}
func adc(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, pageCross) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
let data = memRead(addr)
addToRegisterA(data)
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
}
func aslAccumulator() {
var data = register_a
if data >> 7 == 1 {
setCarryFlag()
} else {
clearCarryFlag()
}
data = data << 1
setRegisterA(data)
}
func asl(_ mode: AddressingMode) -> UInt8 {
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
var data = memRead(addr)
if data >> 7 == 1 {
setCarryFlag()
} else {
clearCarryFlag()
}
data = data << 1
memWrite(addr, data: data)
updateZeroAndNegativeFlags(data)
return data
}
func lsrAccumulator() {
var data = register_a
if data & 1 == 1 {
setCarryFlag()
} else {
clearCarryFlag()
}
data = data >> 1
setRegisterA(data)
}
func lsr(_ mode: AddressingMode) -> UInt8 {
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
var data = memRead(addr)
if data & 1 == 1 {
setCarryFlag()
} else {
clearCarryFlag()
}
data = data >> 1
memWrite(addr, data: data)
updateZeroAndNegativeFlags(data)
return data
}
func rolAccumulator() {
var data = register_a
let oldCarry = status.contains(.carry)
if data >> 7 == 1 {
setCarryFlag()
} else {
clearCarryFlag()
}
data = data << 1
if oldCarry {
data = data | 1
}
setRegisterA(data)
}
func rol(_ mode: AddressingMode) -> UInt8 {
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
var data = memRead(addr)
let oldCarry = status.contains(.carry)
if data >> 7 == 1 {
setCarryFlag()
} else {
clearCarryFlag()
}
data = data << 1
if oldCarry {
data = data | 1
}
memWrite(addr, data: data)
updateZeroAndNegativeFlags(data)
return data
}
func rorAccumulator() {
var data = register_a
let oldCarry = status.contains(.carry)
if data & 1 == 1 {
setCarryFlag()
} else {
clearCarryFlag()
}
data = data >> 1
if oldCarry {
data = data | 0b10000000
}
setRegisterA(data)
}
func ror(_ mode: AddressingMode) -> UInt8 {
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
var data = memRead(addr)
let oldCarry = status.contains(.carry)
if data & 1 == 1 {
setCarryFlag()
} else {
clearCarryFlag()
}
data = data >> 1
if oldCarry {
data = data | 0b10000000
}
memWrite(addr, data: data)
updateZeroAndNegativeFlags(data)
return data
}
func inc(_ mode: AddressingMode) -> UInt8 {
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
var data = memRead(addr)
data = data &+ 1
memWrite(addr, data: data)
updateZeroAndNegativeFlags(data)
return data
}
func dey() {
register_y = register_y &- 1
updateZeroAndNegativeFlags(register_y)
}
func dex() {
register_x = register_x &- 1
updateZeroAndNegativeFlags(register_x)
}
func dec(_ mode: AddressingMode) -> UInt8 {
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
var data = memRead(addr)
data = data &- 1
memWrite(addr, data: data)
updateZeroAndNegativeFlags(data)
return data
}
func pla() {
let data = stackPop()
setRegisterA(data)
}
func plp() {
status.rawValue = stackPop()
status.remove(.break1)
2024-08-18 14:55:39 -05:00
status.insert(.break2)
2024-05-11 22:30:17 -05:00
}
func php() {
var flags = status
flags.insert(.break1)
flags.insert(.break2)
stackPush(flags.rawValue)
}
func bit(_ mode: AddressingMode) {
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(mode)
2024-05-11 22:30:17 -05:00
let data = memRead(addr)
let and = register_a & data
if and == 0 {
status.insert(.zero)
} else {
status.remove(.zero)
}
if data & 0b10000000 > 0 {
status.insert(.negative)
} else {
status.remove(.negative)
}
if data & 0b01000000 > 0 {
status.insert(.overflow)
} else {
status.remove(.overflow)
}
}
}