Skip to content

Commit

Permalink
Use overflow addition/subtraction operators where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
bstien committed May 1, 2024
1 parent 825247a commit 64408f7
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Sources/Swift6502/CPU+Instructions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ private extension CPU {
setFlag(.carry, value >= memory)

// Set flags based on difference between value and memory.
let diff = value.subtractingReportingOverflow(memory).partialValue
let diff = value &- memory
setZeroFlag(using: diff)
setNegativeFlag(using: diff)
}
Expand All @@ -689,9 +689,9 @@ private extension CPU {
let result: UInt8
switch op {
case .inc:
result = value.addingReportingOverflow(1).partialValue
result = value &+ 1
case .dec:
result = value.subtractingReportingOverflow(1).partialValue
result = value &- 1
}

setZeroFlag(using: result)
Expand All @@ -706,7 +706,7 @@ private extension CPU {
private extension CPU {
func pushToStack(byte: UInt8) {
writeByte(0x0100 + stackPointer.asWord, data: byte)
stackPointer = stackPointer.subtractingReportingOverflow(1).partialValue
stackPointer = stackPointer &- 1
}

func pushToStack(word: UInt16) {
Expand All @@ -715,7 +715,7 @@ private extension CPU {
}

func pullByteFromStack() -> UInt8 {
stackPointer = stackPointer.addingReportingOverflow(1).partialValue
stackPointer = stackPointer &+ 1
return readByte(0x0100 + stackPointer.asWord)
}

Expand Down

0 comments on commit 64408f7

Please sign in to comment.