SwiftNES/Sources/CPU/CPU.swift

596 lines
17 KiB
Swift
Raw Normal View History

2024-05-17 13:15:10 -05:00
import Foundation
import SDL
2024-05-10 22:06:45 -05:00
enum AddressingMode {
case Immediate
case ZeroPage
case ZeroPage_X
case ZeroPage_Y
case Absolute
case Absolute_X
case Absolute_Y
case Indirect_X
case Indirect_Y
case NoneAddressing
}
2024-08-19 21:05:08 -05:00
enum Interrupt {
case NMI
var vectorAddr: UInt16 {
switch self {
case .NMI:
0xfffa
}
}
var bFlagMask: UInt8 {
switch self {
case .NMI:
0b00100000
}
}
var cpuCycles: UInt8 {
switch self {
case .NMI:
2
}
}
}
2024-05-11 22:30:17 -05:00
struct CPUFlags: OptionSet {
var rawValue: UInt8
static let carry = CPUFlags(rawValue: 0b00000001)
static let zero = CPUFlags(rawValue: 0b00000010)
static let interruptDisable = CPUFlags(rawValue: 0b00000100)
static let decimalMode = CPUFlags(rawValue: 0b00001000)
static let break1 = CPUFlags(rawValue: 0b00010000)
static let break2 = CPUFlags(rawValue: 0b00100000)
static let overflow = CPUFlags(rawValue: 0b01000000)
static let negative = CPUFlags(rawValue: 0b10000000)
}
let STACK: UInt16 = 0x0100
let STACK_RESET: UInt8 = 0xfd
2024-05-10 22:06:45 -05:00
class CPU {
var register_a: UInt8 = 0
var register_x: UInt8 = 0
var register_y: UInt8 = 0
2024-05-11 22:30:17 -05:00
var stackPointer: UInt8 = STACK_RESET
var status: CPUFlags = [.interruptDisable, .break2]
2024-05-10 22:06:45 -05:00
var programCounter: UInt16 = 0
2024-08-18 14:55:39 -05:00
var bus: Bus
init(bus: Bus) {
self.bus = bus
}
2024-05-10 22:06:45 -05:00
2024-08-19 21:05:08 -05:00
func getOpperandAddress(_ mode: AddressingMode) -> (UInt16, Bool) {
2024-05-10 22:06:45 -05:00
switch mode {
case .Immediate:
2024-08-19 21:05:08 -05:00
return (programCounter, false)
2024-08-18 14:55:39 -05:00
default:
return getAbsoluteAddress(mode, addr: programCounter)
}
}
2024-08-19 21:05:08 -05:00
func getAbsoluteAddress(_ mode: AddressingMode, addr: UInt16) -> (UInt16, Bool) {
2024-08-18 14:55:39 -05:00
switch mode {
2024-05-10 22:06:45 -05:00
case .ZeroPage:
2024-08-19 21:05:08 -05:00
return (UInt16(memRead(addr)), false)
2024-05-10 22:06:45 -05:00
case .Absolute:
2024-08-19 21:05:08 -05:00
return (memReadU16(addr), false)
2024-05-10 22:06:45 -05:00
case .ZeroPage_X:
2024-08-18 14:55:39 -05:00
let pos = memRead(addr)
2024-05-10 22:06:45 -05:00
let addr = pos &+ register_x
2024-08-19 21:05:08 -05:00
return (UInt16(addr), false)
2024-05-10 22:06:45 -05:00
case .ZeroPage_Y:
2024-08-18 14:55:39 -05:00
let pos = memRead(addr)
2024-05-10 22:06:45 -05:00
let addr = pos &+ register_y
2024-08-19 21:05:08 -05:00
return (UInt16(addr), false)
2024-05-10 22:06:45 -05:00
case .Absolute_X:
2024-08-18 14:55:39 -05:00
let base = memReadU16(addr)
2024-08-19 21:05:08 -05:00
let addr = base &+ UInt16(register_x)
return (addr, isPageCross(base, addr))
2024-05-10 22:06:45 -05:00
case .Absolute_Y:
2024-08-18 14:55:39 -05:00
let base = memReadU16(addr)
2024-08-19 21:05:08 -05:00
let addr = base &+ UInt16(register_y)
return (addr, isPageCross(base, addr))
2024-05-10 22:06:45 -05:00
case .Indirect_X:
2024-08-18 14:55:39 -05:00
let base = memRead(addr)
2024-05-10 22:06:45 -05:00
let ptr = UInt8(base) &+ register_x
let lo = memRead(UInt16(ptr))
let hi = memRead(UInt16(ptr &+ 1))
2024-08-19 21:05:08 -05:00
return (UInt16(hi) << 8 | UInt16(lo), false)
2024-05-10 22:06:45 -05:00
case .Indirect_Y:
2024-08-18 14:55:39 -05:00
let base = memRead(addr)
2024-05-10 22:06:45 -05:00
let lo = memRead(UInt16(base))
let hi = memRead(UInt16(base &+ 1))
let deref_base = UInt16(hi) << 8 | UInt16(lo)
let deref = deref_base &+ UInt16(register_y)
2024-08-19 21:05:08 -05:00
return (deref, isPageCross(deref, deref_base))
2024-08-18 14:55:39 -05:00
default:
2024-05-10 22:06:45 -05:00
fatalError("mode \(mode) is not implemented")
}
}
2024-08-19 21:05:08 -05:00
func isPageCross(_ lhs: UInt16, _ rhs: UInt16) -> Bool {
lhs & 0xff00 != rhs & 0xff00
}
2024-05-10 22:06:45 -05:00
func reset() {
register_a = 0
register_x = 0
register_y = 0
2024-05-11 22:30:17 -05:00
stackPointer = STACK_RESET
status = [.interruptDisable, .break2]
2024-05-10 22:06:45 -05:00
programCounter = self.memReadU16(0xFFFC)
}
2024-08-18 14:55:39 -05:00
//func loadAndRun(_ program: [UInt8]) {
//load(program)
//reset()
//run()
//}
//func load(_ program: [UInt8]) {
//bus = Bus(try! Rom(program))
//for i in 0..<program.count {
//memWrite(0x8000 + UInt16(i), data: program[i])
//}
2024-07-19 21:52:46 -05:00
//memory[0x0600 ..< (0x0600 + program.count)] = program[0..<program.count]
2024-08-18 14:55:39 -05:00
//memWriteU16(0xFFFC, data: 0x0600)
//}
2024-05-10 22:06:45 -05:00
func run() {
2024-08-21 09:13:15 -05:00
run(onCycle: {}, onComplete: {})
2024-05-11 22:30:17 -05:00
}
2024-05-17 21:51:00 -05:00
func run(onCycle: @escaping () -> (), onComplete: @escaping () -> ()) {
2024-05-10 22:06:45 -05:00
let opcodes = OPCODES_MAP
2024-08-18 15:33:25 -05:00
while true {
2024-08-20 22:36:04 -05:00
if bus.pollNMI() != nil {
2024-08-19 21:05:08 -05:00
interrupt(.NMI)
}
2024-08-18 15:33:25 -05:00
processOpcodes(onCycle: onCycle, opcodes: opcodes) {
2024-05-17 21:51:00 -05:00
onComplete()
}
2024-05-17 13:15:10 -05:00
}
2024-08-19 21:05:08 -05:00
}
func interrupt(_ interrupt: Interrupt) {
stackPushU16(programCounter)
var flag = status
if interrupt.bFlagMask & 0b010000 == 1 {
flag.insert(.break1)
} else {
flag.remove(.break1)
}
if interrupt.bFlagMask & 0b100000 == 1 {
flag.insert(.break2)
} else {
flag.remove(.break2)
}
stackPush(flag.rawValue)
status.insert(.interruptDisable)
bus.tick(interrupt.cpuCycles)
programCounter = memReadU16(interrupt.vectorAddr)
2024-05-17 13:15:10 -05:00
}
2024-05-10 22:06:45 -05:00
2024-08-18 15:33:25 -05:00
func processOpcodes(onCycle: () -> (), opcodes: [UInt8: OpCode], onComplete: () -> ()) {
2024-05-17 21:51:00 -05:00
onCycle()
2024-05-17 13:15:10 -05:00
let code = memRead(programCounter)
programCounter += 1
let programCounterState = programCounter
guard let opcode = opcodes[code] else {fatalError("OpCode \(code) not recgonized!")}
switch code {
/// LDA
case 0xa9, 0xa5, 0xb5, 0xad, 0xbd, 0xb9, 0xa1, 0xb1:
lda(opcode.mode)
/// STA
case 0x85, 0x95, 0x8d, 0x9d, 0x99, 0x81, 0x91:
sta(opcode.mode)
case 0xd8:
status.remove(.decimalMode)
case 0x58:
status.remove(.interruptDisable)
case 0xb8:
status.remove(.overflow)
case 0x18:
clearCarryFlag()
case 0x38:
setCarryFlag()
case 0x78:
status.insert(.interruptDisable)
case 0xf8:
status.insert(.decimalMode)
case 0x48:
stackPush(register_a)
case 0x68:
pla()
case 0x08:
php()
case 0x28:
plp()
case 0x69, 0x65, 0x75, 0x6d, 0x7d, 0x79, 0x61, 0x71:
adc(opcode.mode)
case 0xe9, 0xe5, 0xf5, 0xed, 0xfd, 0xf9, 0xe1, 0xf1:
sbc(opcode.mode)
case 0x29, 0x25, 0x35, 0x2d, 0x3d, 0x39, 0x21, 0x31:
and(opcode.mode)
case 0x49, 0x45, 0x55, 0x4d, 0x5d, 0x59, 0x41, 0x51:
eor(opcode.mode)
case 0x09, 0x05, 0x15, 0x0d, 0x1d, 0x19, 0x01, 0x11:
ora(opcode.mode)
case 0x4a:
lsrAccumulator()
case 0x46, 0x56, 0x4e, 0x5e:
_ = lsr(opcode.mode)
case 0x0a:
aslAccumulator()
case 0x06, 0x16, 0x0e, 0x1e:
_ = asl(opcode.mode)
case 0x2a:
rolAccumulator()
case 0x26, 0x36, 0x2e, 0x3e:
_ = rol(opcode.mode)
case 0x6a:
rorAccumulator()
case 0x66, 0x76, 0x6e, 0x7e:
_ = ror(opcode.mode)
case 0xe6, 0xf6, 0xee, 0xfe:
_ = inc(opcode.mode)
case 0xc8:
iny()
case 0xc6, 0xd6, 0xce, 0xde:
_ = dec(opcode.mode)
case 0xca:
dex()
case 0x88:
dey()
case 0xc9, 0xc5, 0xd5, 0xcd, 0xdd, 0xd9, 0xc1, 0xd1:
compare(mode: opcode.mode, compare_with: register_a)
case 0xc0, 0xc4, 0xcc:
compare(mode: opcode.mode, compare_with: register_y)
case 0xe0, 0xe4, 0xec:
compare(mode: opcode.mode, compare_with: register_x)
case 0x4c:
let memAddr = memReadU16(programCounter)
programCounter = memAddr
case 0x6c:
let memAddr = memReadU16(programCounter)
//6502 bug mode with with page boundary:
// if address $3000 contains $40, $30FF contains $80, and $3100 contains $50,
// the result of JMP ($30FF) will be a transfer of control to $4080 rather than $5080 as you intended
// i.e. the 6502 took the low byte of the address from $30FF and the high byte from $3000
let indirectRef: UInt16
if memAddr & 0x00ff == 0x00ff {
let lo = memRead(memAddr)
2024-08-18 14:55:39 -05:00
let hi = memRead(memAddr & 0xff00)
2024-05-17 13:15:10 -05:00
indirectRef = UInt16(hi) << 8 | UInt16(lo)
} else {
indirectRef = memReadU16(memAddr)
2024-05-10 22:06:45 -05:00
}
2024-05-17 13:15:10 -05:00
programCounter = indirectRef
case 0x20:
stackPushU16(programCounter + 2 - 1)
let targetAddr = memReadU16(programCounter)
programCounter = targetAddr
case 0x60:
programCounter = stackPopU16() + 1
case 0x40:
status.rawValue = stackPop()
status.remove(.break1)
2024-08-18 14:55:39 -05:00
status.insert(.break2)
2024-05-17 13:15:10 -05:00
programCounter = stackPopU16()
case 0xd0:
branch(!status.contains(.zero))
case 0x70:
branch(status.contains(.overflow))
case 0x50:
branch(!status.contains(.overflow))
case 0x10:
branch(!status.contains(.negative))
case 0x30:
branch(status.contains(.negative))
case 0xf0:
branch(status.contains(.zero))
case 0xb0:
branch(status.contains(.carry))
case 0x90:
branch(!status.contains(.carry))
case 0x24, 0x2c:
bit(opcode.mode)
case 0x86, 0x96, 0x8e:
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(opcode.mode)
2024-05-17 13:15:10 -05:00
memWrite(addr, data: register_x)
case 0x84, 0x94, 0x8c:
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(opcode.mode)
2024-05-17 13:15:10 -05:00
memWrite(addr, data: register_y)
case 0xa2, 0xa6, 0xb6, 0xae, 0xbe:
ldx(opcode.mode)
case 0xa0, 0xa4, 0xb4, 0xac, 0xbc:
ldy(opcode.mode)
case 0xea:
return
case 0xa8:
2024-08-18 14:55:39 -05:00
register_y = register_a
2024-05-17 13:15:10 -05:00
updateZeroAndNegativeFlags(register_y)
case 0xba:
register_x = stackPointer
updateZeroAndNegativeFlags(register_x)
case 0x8a:
register_a = register_x
updateZeroAndNegativeFlags(register_a)
case 0x9a:
stackPointer = register_x
case 0x98:
register_a = register_y
updateZeroAndNegativeFlags(register_a)
/// TAX
case 0xaa:
tax()
/// INX
case 0xe8:
inx()
/// BRK
case 0x00:
2024-08-18 15:33:25 -05:00
//timer.invalidate()
2024-05-17 21:51:00 -05:00
onComplete()
2024-08-18 14:55:39 -05:00
/// NOP Read
case 0x04, 0x44, 0x64, 0x14, 0x34, 0x54, 0x74, 0xd4, 0xf4, 0x0c, 0x1c, 0x3c, 0x5c, 0x7c, 0xdc, 0xfc:
2024-08-19 21:05:08 -05:00
let (addr, pageCross) = getOpperandAddress(opcode.mode)
2024-08-18 14:55:39 -05:00
let _ = self.memRead(addr)
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-08-18 14:55:39 -05:00
// Do nothing
/// NOP
case 0x1a, 0x3a, 0x5a, 0x7a, 0xda, 0xfa:
{ /* Do Nothing */}()
/// RRA
case 0x67, 0x77, 0x6f, 0x7f, 0x7b, 0x63, 0x73:
let data = ror(opcode.mode)
addToRegisterA(data)
/// ISB
case 0xe7, 0xf7, 0xef, 0xff, 0xfb, 0xe3, 0xf3:
let data = inc(opcode.mode)
subFromRegisterA(data)
/// SKB
case 0x80, 0x82, 0x89, 0xc2, 0xe2:
{ /* 2 byte NOP immediate, do nothing */ }()
/// LAX
case 0xa7, 0xb7, 0xaf, 0xbf, 0xa3, 0xb3:
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(opcode.mode)
2024-08-18 14:55:39 -05:00
let data = memRead(addr)
setRegisterA(data)
register_x = register_a
/// SAX
case 0x87, 0x97, 0x8f, 0x83:
let data = register_a & register_x
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(opcode.mode)
2024-08-18 14:55:39 -05:00
memWrite(addr, data: data)
// Unoffical SBC
case 0xeb:
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(opcode.mode)
2024-08-18 14:55:39 -05:00
let data = self.memRead(addr)
subFromRegisterA(data)
/// DCP
case 0xc7, 0xd7, 0xCF, 0xdF, 0xdb, 0xd3, 0xc3:
2024-08-19 21:05:08 -05:00
let (addr, _) = getOpperandAddress(opcode.mode)
2024-08-18 14:55:39 -05:00
var data = memRead(addr)
data = data &- 1
memWrite(addr, data: data)
if data <= register_a {
status.insert(.carry)
}
let tmp = register_a &- data
updateZeroAndNegativeFlags(tmp)
/// SLO
case 0x07, 0x17, 0x0F, 0x1f, 0x1b, 0x03, 0x13:
let data = asl(opcode.mode)
orWithRegisterA(data)
/// RLA
case 0x27, 0x37, 0x2F, 0x3F, 0x3b, 0x33, 0x23:
let data = rol(opcode.mode)
andWithRegisterA(data)
/// SRE
case 0x47, 0x57, 0x4F, 0x5f, 0x5b, 0x43, 0x53:
let data = lsr(opcode.mode)
xorWithRegisterA(data)
2024-05-17 13:15:10 -05:00
default: fatalError("TODO!")
}
2024-08-19 21:05:08 -05:00
bus.tick(opcode.cycles)
2024-05-17 13:15:10 -05:00
if programCounterState == programCounter {
programCounter += UInt16(opcode.len - 1)
2024-05-10 22:06:45 -05:00
}
}
func updateZeroAndNegativeFlags(_ result: UInt8) {
if result == 0 {
2024-05-11 22:30:17 -05:00
status.insert(.zero)
2024-05-10 22:06:45 -05:00
} else {
2024-05-11 22:30:17 -05:00
status.remove(.zero)
2024-05-10 22:06:45 -05:00
}
if result & 0b1000_0000 != 0 {
2024-05-11 22:30:17 -05:00
status.insert(.negative)
2024-05-10 22:06:45 -05:00
} else {
2024-05-11 22:30:17 -05:00
status.remove(.negative)
2024-05-10 22:06:45 -05:00
}
}
2024-05-11 22:30:17 -05:00
func setRegisterA(_ value: UInt8) {
2024-05-10 22:06:45 -05:00
register_a = value
updateZeroAndNegativeFlags(register_a)
}
2024-05-11 22:30:17 -05:00
func setCarryFlag() {
status.insert(.carry)
2024-05-10 22:06:45 -05:00
}
2024-05-11 22:30:17 -05:00
func clearCarryFlag() {
status.remove(.carry)
2024-05-10 22:06:45 -05:00
}
2024-05-11 22:30:17 -05:00
/// note: ignoring decimal mode
/// http://www.righto.com/2012/12/the-6502-overflow-flag-explained.html
func addToRegisterA(_ data: UInt8) {
let shouldCarry = status.contains(.carry) ? 1 : 0
let sum = UInt16(register_a) + UInt16(data) + UInt16(shouldCarry)
let carry = sum > 0xff
if carry {
status.insert(.carry)
} else {
status.remove(.carry)
}
2024-05-17 13:15:10 -05:00
let result = UInt8(truncatingIfNeeded: sum)
2024-05-11 22:30:17 -05:00
if (data ^ result) & (result ^ register_a) & 0x80 != 0 {
status.insert(.overflow)
} else {
status.remove(.overflow)
}
setRegisterA(result)
}
2024-08-18 14:55:39 -05:00
func subFromRegisterA(_ data: UInt8) {
addToRegisterA(UInt8(bitPattern: (Int8(bitPattern: data) &* -1) &- 1))
}
func orWithRegisterA(_ data: UInt8) {
setRegisterA(data | self.register_a)
}
func xorWithRegisterA(_ data: UInt8) {
setRegisterA(data ^ self.register_a)
}
func andWithRegisterA(_ data: UInt8) {
setRegisterA(data & register_a)
}
2024-05-11 22:30:17 -05:00
func stackPop() -> UInt8 {
stackPointer = stackPointer &+ 1
return memRead(STACK + UInt16(stackPointer))
}
func stackPush(_ data: UInt8) {
memWrite(STACK + UInt16(stackPointer), data: data)
stackPointer = stackPointer &- 1
}
func stackPopU16() -> UInt16 {
let lo = UInt16(stackPop())
let hi = UInt16(stackPop())
return hi << 8 | lo
}
func stackPushU16(_ data: UInt16) {
let hi = UInt8(data >> 8)
let lo = UInt8(data & 0xff)
stackPush(hi)
stackPush(lo)
}
func compare(mode: AddressingMode, compare_with: UInt8) {
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)
if data <= compare_with {
status.insert(.carry)
} else {
status.remove(.carry)
}
updateZeroAndNegativeFlags(compare_with &- data)
2024-08-19 21:05:08 -05:00
if pageCross {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
}
func branch(_ condition: Bool) {
if condition {
2024-08-19 21:05:08 -05:00
bus.tick(1)
2024-05-11 22:30:17 -05:00
let addr = memRead(programCounter)
let jump: Int8 = Int8(bitPattern: addr)
let jump_addr = programCounter &+ 1 &+ UInt16(bitPattern: Int16(jump))
2024-08-19 21:05:08 -05:00
if (programCounter &+ 1) & 0xff00 != jump_addr & 0xff00 {
bus.tick(1)
}
2024-05-11 22:30:17 -05:00
programCounter = jump_addr
}
}
}
extension CPU: Memory {
func memRead(_ addr: UInt16) -> UInt8 {
2024-08-18 14:55:39 -05:00
return bus.memRead(addr)
2024-05-10 22:06:45 -05:00
}
2024-05-11 22:30:17 -05:00
func memWrite(_ addr: UInt16, data: UInt8) {
2024-08-18 14:55:39 -05:00
bus.memWrite(addr, data: data)
2024-07-19 21:52:46 -05:00
}
func memReadU16(_ addr: UInt16) -> UInt16 {
2024-08-18 14:55:39 -05:00
return bus.memReadU16(addr)
2024-07-19 21:52:46 -05:00
}
func memWriteU16(_ addr: UInt16, data: UInt16) {
2024-08-18 14:55:39 -05:00
bus.memWriteU16(addr, data: data)
2024-05-11 22:30:17 -05:00
}
}
2024-05-10 22:06:45 -05:00
2024-05-11 22:30:17 -05:00
protocol Memory {
func memRead(_ addr: UInt16) -> UInt8
func memWrite(_ addr: UInt16, data: UInt8)
func memReadU16(_ addr: UInt16) -> UInt16
func memWriteU16(_ addr: UInt16, data: UInt16)
}
extension Memory {
func memReadU16(_ addr: UInt16) -> UInt16 {
let lo = UInt16(memRead(addr))
let hi = UInt16(memRead(addr + 1))
return (hi << 8) | lo
}
func memWriteU16(_ addr: UInt16, data: UInt16) {
let hi = UInt8(data >> 8)
let lo = UInt8(data & 0xff)
self.memWrite(addr, data: lo)
self.memWrite(addr + 1, data: hi)
}
2024-05-10 22:06:45 -05:00
}