From f1db35782281f67f6f8789007c11f053b8187827 Mon Sep 17 00:00:00 2001 From: LiarPrincess <4982138+LiarPrincess@users.noreply.github.com> Date: Mon, 9 Nov 2020 18:55:19 +0100 Subject: [PATCH] SwiftFormat --- .swift-version | 1 + .swiftformat | 127 ++++++++++++++++++ Sources/Code generation/Output/Common.swift | 50 +++---- .../Output/PrintOpcodeEnum.swift | 4 +- Sources/Code generation/main.swift | 10 +- .../GameBoyKit/Cartridge/CartridgeMap.swift | 3 + .../Cartridge/Header/CartridgeHeader.swift | 6 +- Sources/GameBoyKit/Cpu/Cpu+Instructions.swift | 1 + Sources/GameBoyKit/Cpu/Cpu.swift | 12 +- Sources/GameBoyKit/Cpu/CpuRegisters.swift | 26 ++-- Sources/GameBoyKit/Cpu/Interrupts.swift | 10 +- .../GameBoyKit/Debug/CBPrefixedOpcode.swift | 1 - Sources/GameBoyKit/Debug/DebugState.swift | 80 +++++------ Sources/GameBoyKit/Debug/Debugger+Cpu.swift | 64 ++++----- Sources/GameBoyKit/Debug/Debugger+Lcd.swift | 43 +++--- Sources/GameBoyKit/Debug/Debugger.swift | 38 +++--- .../GameBoyKit/Debug/UnprefixedOpcode.swift | 1 - Sources/GameBoyKit/Extensions.swift | 8 +- Sources/GameBoyKit/GameBoy.swift | 17 +-- Sources/GameBoyKit/GameboyInput.swift | 14 +- Sources/GameBoyKit/IO/Joypad.swift | 18 +-- Sources/GameBoyKit/IO/Timer.swift | 4 +- .../GameBoyKit/Lcd/Entities/LcdControl.swift | 2 + .../GameBoyKit/Lcd/Entities/LcdStatus.swift | 2 + Sources/GameBoyKit/Lcd/Framebuffer.swift | 2 +- Sources/GameBoyKit/Lcd/Lcd+Draw.swift | 14 +- Sources/GameBoyKit/Lcd/Lcd.swift | 20 +-- Sources/GameBoyKit/Lcd/Tile.swift | 7 +- Sources/GameBoyKit/Memory/Memory+read.swift | 24 ++-- Sources/GameBoyKit/Memory/Memory+write.swift | 12 +- Sources/GameBoyKit/Memory/Memory.swift | 19 +-- Sources/GameBoyKit/Memory/MemoryMap.swift | 2 + Sources/GameBoyMac/GameBoyWindow.swift | 30 +++-- Sources/GameBoyMac/Metal.swift | 21 ++- Tests/GameBoyKitROMTests/BlarggTests.swift | 3 +- Tests/GameBoyKitROMTests/BootromTests.swift | 6 +- .../Dump files/DumpFileContent.swift | 2 +- .../Dump files/StreamReader.swift | 35 ++--- .../Dump files/openDumpFile.swift | 90 +++++++------ .../Dump files/runDump.swift | 3 +- Tests/GameBoyKitROMTests/TetrisTest.swift | 2 +- Tests/GameBoyKitROMTests/main.swift | 6 +- .../GameBoyKitROMTests/performanceTest.swift | 2 +- .../Cartridge/CartridgeHeaderTests.swift | 2 +- .../Cartridge/Common/CartridgeTestCase.swift | 3 +- .../GameBoyKitTests/Cartridge/MBC1Tests.swift | 3 +- .../GameBoyKitTests/Cartridge/MBC3Tests.swift | 3 +- .../Cartridge/NoMBCTests.swift | 3 +- .../Cpu/Common/CpuTestCase.swift | 3 +- Tests/GameBoyKitTests/Cpu/CpuLdTests.swift | 2 +- .../Cpu/CpuOpcodeLengthTests.swift | 38 +++--- .../Cpu/CpuOpcodeTimingTests.swift | 70 +++++----- .../Cpu/CpuOtherInstructionsTests.swift | 8 +- .../Cpu/CpuRegistersTests.swift | 2 +- .../GameBoyKitTests/Cpu/InterruptsTests.swift | 16 +-- Tests/GameBoyKitTests/IO/TimerTimaTests.swift | 6 +- .../Memory/Common/FakeLcdMemory.swift | 4 +- .../Memory/Common/FakeTimerMemory.swift | 6 +- .../Memory/Common/MemoryTestCase.swift | 5 +- Tests/GameBoyKitTests/Memory/DmaTests.swift | 2 +- .../Memory/MemoryReadTests.swift | 14 +- .../Memory/MemoryWriteTests.swift | 6 +- .../Memory/UnmapBootromTests.swift | 2 +- 63 files changed, 609 insertions(+), 431 deletions(-) create mode 100644 .swift-version create mode 100644 .swiftformat diff --git a/.swift-version b/.swift-version new file mode 100644 index 0000000..819e07a --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +5.0 diff --git a/.swiftformat b/.swiftformat new file mode 100644 index 0000000..38e15ce --- /dev/null +++ b/.swiftformat @@ -0,0 +1,127 @@ +# ============= +# == Options == +# ============= + +# Use 'swiftformat --options' to list all of the possible options +# For most of the options we use default value (except for '--self') + +--allman false +--commas always +--elseposition same-line # I hate it, but this is 'community choice' in Swift +--empty void +--header ignore # We don't have a header. Headers are ugly. +--ifdef indent +--indent 2 +--indentcase false +--linebreaks lf +--maxwidth none # 80 is preferred (but not required), don't go over 100 +--nospaceoperators ..<, ... +--operatorfunc spaced +--self insert # Yes! +--selfrequired +--semicolons inline +--stripunusedargs closure-only +--tabwidth unspecified # Don't use tabs! +--trailingclosures +--trimwhitespace always +--xcodeindentation disabled + +# Arguments are +# - group - group size (count % group == 0) +# - threshold - min digit count to apply (number.count >= threshold) +--decimalgrouping 3,1 +--binarygrouping 4,1 +--octalgrouping 4,1 +--hexgrouping 4,1 +--exponentcase lowercase +--exponentgrouping enabled +--fractiongrouping enabled +--hexliteralcase lowercase + +--wraparguments preserve +--wrapcollections preserve +--wrapparameters preserve +--closingparen balanced + +# We have our own (see: './Scipts/sort_swift_imports') +# But it is not required to follow it. +--importgrouping alphabetized + +# Our own rule: all patterns should start at the same column, +# otherwise reader has to 'zig-zag' to read which pattern is bound where. +# (in other words: if all patterns have binding -> hoist, otherwise per variable) +# +# For example, this is not allowed (or at least frowned upon): +# switch frozen { +# case .elsa +# case let .anna(x) <-- nope, both 'elsa' and 'anna' should start the same column +# } +--patternlet hoist + +# =========== +# == Rules == +# =========== + +--disable andOperator # Use '&&' ot ',' wherever you want +--enable anyObjectProtocol +--enable blankLinesAroundMark +--enable blankLinesAtEndOfScope +--disable blankLinesAtStartOfScope # Empty line after 'class' definition +--enable blankLinesBetweenScopes +--enable braces +--enable consecutiveBlankLines +--enable consecutiveSpaces +--enable duplicateImports +--enable elseOnSameLine +--enable emptyBraces +--enable fileHeader +--disable hoistPatternLet +--disable indent # This breaks a lof of already written stuff. +--disable isEmpty # False-positives +--enable leadingDelimiters +--enable linebreakAtEndOfFile +--enable linebreaks +--enable numberFormatting +# --enable ranges - deprecated +--enable redundantBackticks +--enable redundantBreak +--enable redundantExtensionACL +--enable redundantFileprivate +--enable redundantGet +--enable redundantInit +--enable redundantLet +--enable redundantLetError +--enable redundantNilInit +--enable redundantObjc +--enable redundantParens +--enable redundantPattern +--disable redundantRawValues # It is 'ok' to specify raw value (even if it is the same as value) +--disable redundantReturn # implicit = bad +--enable redundantSelf +--enable redundantVoidReturnType +--enable semicolons +--disable sortedImports +--enable spaceAroundBraces +--enable spaceAroundBrackets +--enable spaceAroundComments +--enable spaceAroundGenerics +--enable spaceAroundOperators +--enable spaceAroundParens +--enable spaceInsideBraces +--enable spaceInsideBrackets +--enable spaceInsideComments +--enable spaceInsideGenerics +--enable spaceInsideParens +--disable specifiers # Conflict with SwiftLint +--enable strongOutlets +--enable strongifiedSelf +--enable todos +--enable trailingClosures +--disable trailingCommas # +--enable trailingSpace +--enable typeSugar +--enable unusedArguments +--enable void +--disable wrap # Breaks already written code +--disable wrapArguments # See above +--disable yodaConditions # '0 < x && x < 10' is better than ' x > 0 && x < 10' diff --git a/Sources/Code generation/Output/Common.swift b/Sources/Code generation/Output/Common.swift index 091c934..c9d2334 100644 --- a/Sources/Code generation/Output/Common.swift +++ b/Sources/Code generation/Output/Common.swift @@ -12,7 +12,7 @@ func getEnumCase(_ opcode: Opcode) -> String { var result = opcode.mnemonic.lowercased() if result == "prefix" { return result } - if result == "stop" { return result } + if result == "stop" { return result } if let operand1 = opcode.operand1 { result += "_" @@ -61,24 +61,24 @@ private func getOperandValue(_ value: String) -> String { case "30H": return "30" case "38H": return "38" - case "SP": return "sp" + case "SP": return "sp" case "SP+r8": return "spR8" - case "d8": return "d8" + case "d8": return "d8" case "d16": return "d16" - case "r8": return "r8" + case "r8": return "r8" case "a16": return "a16" - case "NC": return "nc" - case "NZ": return "nz" - case "Z": return "z" - - case "(BC)": return "pBC" - case "(C)": return "pC" - case "(DE)": return "pDE" - case "(HL)": return "pHL" + case "NC": return "nc" + case "NZ": return "nz" + case "Z": return "z" + + case "(BC)": return "pBC" + case "(C)": return "pC" + case "(DE)": return "pDE" + case "(HL)": return "pHL" case "(HL+)": return "pHLI" case "(HL-)": return "pHLD" - case "(a8)": return "pA8" + case "(a8)": return "pA8" case "(a16)": return "pA16" default: return "Unknown value: " + value.lowercased() @@ -154,7 +154,7 @@ func getUnprefixedOpcodeCall(_ opcode: Opcode) -> String { let operand1 = opcode.operand1!.lowercased() let operand2 = opcode.operand2!.lowercased() - if isA(operand1) && isRegister(operand2) { return "add_a_r(.\(operand2))" } + if isA(operand1) && isRegister(operand2) { return "add_a_r(.\(operand2))" } if isHL(operand1) && isCombinedRegister(operand2) { return "add_hl_r(.\(operand2))" } } @@ -252,9 +252,9 @@ func getUnprefixedOpcodeCall(_ opcode: Opcode) -> String { } case "rlca": return "rlca()" - case "rla": return "rla()" + case "rla": return "rla()" case "rrca": return "rrca()" - case "rra": return "rra()" + case "rra": return "rra()" case "jp": switch opcode.addr { @@ -309,15 +309,15 @@ func getUnprefixedOpcodeCall(_ opcode: Opcode) -> String { let argument = "0x" + operand.dropLast() return "rst(\(argument))" - case "stop": return "stop()" - case "daa": return "daa()" - case "cpl": return "cpl()" - case "scf": return "scf()" - case "ccf": return "ccf()" - case "halt": return "halt()" + case "stop": return "stop()" + case "daa": return "daa()" + case "cpl": return "cpl()" + case "scf": return "scf()" + case "ccf": return "ccf()" + case "halt": return "halt()" case "prefix": return "executePrefixed(\(next8))" - case "di": return "di()" - case "ei": return "ei()" + case "di": return "di()" + case "ei": return "ei()" default: break } @@ -345,7 +345,7 @@ func getCBPrefixedOpcodeCall(_ opcode: Opcode) -> String { return "" } -private let singleRegisters = ["a", "b", "c", "d", "e", "h", "l"] +private let singleRegisters = ["a", "b", "c", "d", "e", "h", "l"] private let combinedRegisters = ["af", "bc", "de", "hl"] private let jumpConditions = ["nz", "z", "nc", "c"] diff --git a/Sources/Code generation/Output/PrintOpcodeEnum.swift b/Sources/Code generation/Output/PrintOpcodeEnum.swift index bd1523a..acb5fe6 100644 --- a/Sources/Code generation/Output/PrintOpcodeEnum.swift +++ b/Sources/Code generation/Output/PrintOpcodeEnum.swift @@ -58,8 +58,8 @@ private func printOpcodeEnum(_ enumName: String, _ opcodes: [Opcode]) { print("") } -private func byAddress(_ opcodes: [Opcode]) -> [String:Opcode] { - var result = [String:Opcode]() +private func byAddress(_ opcodes: [Opcode]) -> [String: Opcode] { + var result = [String: Opcode]() for op in opcodes { result[op.addr] = op } diff --git a/Sources/Code generation/main.swift b/Sources/Code generation/main.swift index d93e70c..4ea3e41 100644 --- a/Sources/Code generation/main.swift +++ b/Sources/Code generation/main.swift @@ -6,7 +6,7 @@ import Foundation let currentFile = URL(fileURLWithPath: #file) let sourcesDir = currentFile.deletingLastPathComponent().deletingLastPathComponent() -let cpuDir = sourcesDir.appendingPathComponent("GameBoyKit").appendingPathComponent("Cpu") +let cpuDir = sourcesDir.appendingPathComponent("GameBoyKit").appendingPathComponent("Cpu") let debugDir = sourcesDir.appendingPathComponent("GameBoyKit").appendingPathComponent("Debug") let opcodes = try readOpcodes() @@ -34,7 +34,7 @@ printPrefixOpcodeEnum(opcodes) // MARK: - Modify cpu instructions -//file = frameworkDir.appendingPathComponent("Cpu+Instructions.swift") -//let instructionsFileContent = try! String(contentsOf: file, encoding: .utf8) -//freopen(file.path, "w", stdout) -//modifyInstructions(opcodes, instructionsFileContent) +// file = frameworkDir.appendingPathComponent("Cpu+Instructions.swift") +// let instructionsFileContent = try! String(contentsOf: file, encoding: .utf8) +// freopen(file.path, "w", stdout) +// modifyInstructions(opcodes, instructionsFileContent) diff --git a/Sources/GameBoyKit/Cartridge/CartridgeMap.swift b/Sources/GameBoyKit/Cartridge/CartridgeMap.swift index 6f4a908..40056d4 100644 --- a/Sources/GameBoyKit/Cartridge/CartridgeMap.swift +++ b/Sources/GameBoyKit/Cartridge/CartridgeMap.swift @@ -2,6 +2,9 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +// swiftformat:disable consecutiveSpaces +// swiftformat:disable redundantSelf + internal enum CartridgeMap { internal static let interruptVectors: ClosedRange = 0x0000...0x00ff internal static let entryPoint: ClosedRange = 0x0100...0x0103 diff --git a/Sources/GameBoyKit/Cartridge/Header/CartridgeHeader.swift b/Sources/GameBoyKit/Cartridge/Header/CartridgeHeader.swift index 54d2164..e8e8f50 100644 --- a/Sources/GameBoyKit/Cartridge/Header/CartridgeHeader.swift +++ b/Sources/GameBoyKit/Cartridge/Header/CartridgeHeader.swift @@ -78,12 +78,12 @@ private func isChecksumValid(_ data: Data) -> ChecksumResult { var hl: UInt16 = CartridgeMap.headerChecksumRange.start var b: UInt8 = 0x19 // bootrom: 0x00f1 - var a: UInt8 = b // bootrom: 0x00f3 + var a: UInt8 = b // bootrom: 0x00f3 while b > 0 { a &+= data[hl] // bootrom: 0x00f4 - hl += 1 // bootrom: 0x00f5 - b -= 1 // bootrom: 0x00f6 + hl += 1 // bootrom: 0x00f5 + b -= 1 // bootrom: 0x00f6 } a &+= data[hl] // bootrom: 0x00f9 diff --git a/Sources/GameBoyKit/Cpu/Cpu+Instructions.swift b/Sources/GameBoyKit/Cpu/Cpu+Instructions.swift index bf04003..b1c21ac 100644 --- a/Sources/GameBoyKit/Cpu/Cpu+Instructions.swift +++ b/Sources/GameBoyKit/Cpu/Cpu+Instructions.swift @@ -3,6 +3,7 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. // swiftlint:disable file_length +// swiftformat:disable consecutiveSpaces // This file is massive, but we need it this way so we can easier Cmd+F. // Source: diff --git a/Sources/GameBoyKit/Cpu/Cpu.swift b/Sources/GameBoyKit/Cpu/Cpu.swift index 5c0adca..4f69ce8 100644 --- a/Sources/GameBoyKit/Cpu/Cpu.swift +++ b/Sources/GameBoyKit/Cpu/Cpu.swift @@ -102,11 +102,11 @@ public final class Cpu { private func getInterruptHandlingRoutine(_ type: Interrupts.Kind) -> UInt16 { switch type { - case .vBlank: return 0x40 + case .vBlank: return 0x40 case .lcdStat: return 0x48 - case .timer: return 0x50 - case .serial: return 0x58 - case .joypad: return 0x60 + case .timer: return 0x50 + case .serial: return 0x58 + case .joypad: return 0x60 } } @@ -119,7 +119,7 @@ public final class Cpu { /// Next 16 bits after pc internal var next16: UInt16 { - let low = UInt16(self.read(self.pc + 1)) + let low = UInt16(self.read(self.pc + 1)) let high = UInt16(self.read(self.pc + 2)) return (high << 8) | low } @@ -143,7 +143,7 @@ public final class Cpu { } internal func pop16() -> UInt16 { - let low = UInt16(self.pop8()) + let low = UInt16(self.pop8()) let high = UInt16(self.pop8()) return (high << 8) | low } diff --git a/Sources/GameBoyKit/Cpu/CpuRegisters.swift b/Sources/GameBoyKit/Cpu/CpuRegisters.swift index 7068afa..1b8609e 100644 --- a/Sources/GameBoyKit/Cpu/CpuRegisters.swift +++ b/Sources/GameBoyKit/Cpu/CpuRegisters.swift @@ -2,10 +2,12 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +// swiftformat:disable consecutiveSpaces private let zeroFlagMask: UInt8 = 1 << 7 private let subtractFlagMask: UInt8 = 1 << 6 private let halfCarryFlagMask: UInt8 = 1 << 5 private let carryFlagMask: UInt8 = 1 << 4 +// swiftformat:enable consecutiveSpaces public struct CpuRegisters { @@ -39,17 +41,17 @@ public struct CpuRegisters { public internal(set) var f: UInt8 { get { var result: UInt8 = 0 - result |= self.zeroFlag ? zeroFlagMask : 0 - result |= self.subtractFlag ? subtractFlagMask : 0 + result |= self.zeroFlag ? zeroFlagMask : 0 + result |= self.subtractFlag ? subtractFlagMask : 0 result |= self.halfCarryFlag ? halfCarryFlagMask : 0 - result |= self.carryFlag ? carryFlagMask : 0 + result |= self.carryFlag ? carryFlagMask : 0 return result } set { - self.zeroFlag = isSet(newValue, mask: zeroFlagMask) - self.subtractFlag = isSet(newValue, mask: subtractFlagMask) + self.zeroFlag = isSet(newValue, mask: zeroFlagMask) + self.subtractFlag = isSet(newValue, mask: subtractFlagMask) self.halfCarryFlag = isSet(newValue, mask: halfCarryFlagMask) - self.carryFlag = isSet(newValue, mask: carryFlagMask) + self.carryFlag = isSet(newValue, mask: carryFlagMask) } } @@ -126,10 +128,12 @@ public struct CpuRegisters { public func get(_ f: Flag) -> Bool { switch f { + // swiftformat:disable consecutiveSpaces case .zeroFlag: return self.zeroFlag case .subtractFlag: return self.subtractFlag case .halfCarryFlag: return self.halfCarryFlag case .carryFlag: return self.carryFlag + // swiftformat:enable consecutiveSpaces } } @@ -157,10 +161,12 @@ public struct CpuRegisters { internal mutating func set(_ f: Flag, to value: Bool) { switch f { - case .zeroFlag: self.zeroFlag = value - case .subtractFlag: self.subtractFlag = value + // swiftformat:disable consecutiveSpaces + case .zeroFlag: self.zeroFlag = value + case .subtractFlag: self.subtractFlag = value case .halfCarryFlag: self.halfCarryFlag = value - case .carryFlag: self.carryFlag = value + case .carryFlag: self.carryFlag = value + // swiftformat:enable consecutiveSpaces } } @@ -193,7 +199,7 @@ private func combine(high: UInt8, low: UInt8) -> UInt16 { private func split(combined: UInt16) -> (high: UInt8, low: UInt8) { let high = UInt8((combined & 0xff00) >> 8) - let low = UInt8(combined & 0xff) + let low = UInt8(combined & 0xff) return (high, low) } diff --git a/Sources/GameBoyKit/Cpu/Interrupts.swift b/Sources/GameBoyKit/Cpu/Interrupts.swift index 3d370e8..0a843b3 100644 --- a/Sources/GameBoyKit/Cpu/Interrupts.swift +++ b/Sources/GameBoyKit/Cpu/Interrupts.swift @@ -2,11 +2,13 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +// swiftformat:disable consecutiveSpaces private let vBlankMask: UInt8 = 1 << 0 private let lcdStatMask: UInt8 = 1 << 1 private let timerMask: UInt8 = 1 << 2 private let serialMask: UInt8 = 1 << 3 private let joypadMask: UInt8 = 1 << 4 +// swiftformat:enable consecutiveSpaces /// FF0F Interrupt Flag; /// FFFF Interrupt Enable @@ -21,11 +23,11 @@ public final class Interrupts { fileprivate var mask: UInt8 { switch self { - case .vBlank: return vBlankMask + case .vBlank: return vBlankMask case .lcdStat: return lcdStatMask - case .timer: return timerMask - case .serial: return serialMask - case .joypad: return joypadMask + case .timer: return timerMask + case .serial: return serialMask + case .joypad: return joypadMask } } } diff --git a/Sources/GameBoyKit/Debug/CBPrefixedOpcode.swift b/Sources/GameBoyKit/Debug/CBPrefixedOpcode.swift index f1d1740..4c7085f 100644 --- a/Sources/GameBoyKit/Debug/CBPrefixedOpcode.swift +++ b/Sources/GameBoyKit/Debug/CBPrefixedOpcode.swift @@ -271,4 +271,3 @@ internal enum CBPrefixedOpcode: UInt8, RawRepresentable { case set_7_pHL = 0xfe case set_7_a = 0xff } - diff --git a/Sources/GameBoyKit/Debug/DebugState.swift b/Sources/GameBoyKit/Debug/DebugState.swift index 669f2f6..1b02b1f 100644 --- a/Sources/GameBoyKit/Debug/DebugState.swift +++ b/Sources/GameBoyKit/Debug/DebugState.swift @@ -16,10 +16,10 @@ internal struct DebugState { internal let h: UInt8 internal let l: UInt8 - internal let zeroFlag: Bool - internal let subtractFlag: Bool + internal let zeroFlag: Bool + internal let subtractFlag: Bool internal let halfCarryFlag: Bool - internal let carryFlag: Bool + internal let carryFlag: Bool internal let pc: UInt16 internal let sp: UInt16 @@ -32,9 +32,9 @@ internal struct DebugState { // swiftlint:disable:next type_name internal struct IO { internal let joypad: UInt8 - internal let sb: UInt8 - internal let sc: UInt8 - internal let unmapBootrom: UInt8 + internal let sb: UInt8 + internal let sc: UInt8 + internal let unmapBootrom: UInt8 } internal let io: IO @@ -42,10 +42,10 @@ internal struct DebugState { // MARK: - Timer internal struct Timer { - internal let div: UInt8 + internal let div: UInt8 internal let tima: UInt8 - internal let tma: UInt8 - internal let tac: UInt8 + internal let tma: UInt8 + internal let tac: UInt8 } internal let timer: Timer @@ -53,29 +53,29 @@ internal struct DebugState { // MARK: - Audio internal struct Audio { - internal let nr10: UInt8 - internal let nr11: UInt8 - internal let nr12: UInt8 - internal let nr13: UInt8 - internal let nr14: UInt8 - internal let nr21: UInt8 - internal let nr22: UInt8 - internal let nr23: UInt8 - internal let nr24: UInt8 - internal let nr30: UInt8 - internal let nr31: UInt8 - internal let nr32: UInt8 - internal let nr33: UInt8 - internal let nr34: UInt8 - internal let nr41: UInt8 - internal let nr42: UInt8 - internal let nr43: UInt8 - internal let nr44: UInt8 - internal let nr50: UInt8 - internal let nr51: UInt8 - internal let nr52: UInt8 + internal let nr10: UInt8 + internal let nr11: UInt8 + internal let nr12: UInt8 + internal let nr13: UInt8 + internal let nr14: UInt8 + internal let nr21: UInt8 + internal let nr22: UInt8 + internal let nr23: UInt8 + internal let nr24: UInt8 + internal let nr30: UInt8 + internal let nr31: UInt8 + internal let nr32: UInt8 + internal let nr33: UInt8 + internal let nr34: UInt8 + internal let nr41: UInt8 + internal let nr42: UInt8 + internal let nr43: UInt8 + internal let nr44: UInt8 + internal let nr50: UInt8 + internal let nr51: UInt8 + internal let nr52: UInt8 internal let nr3_ram_start: UInt8 - internal let nr3_ram_end: UInt8 + internal let nr3_ram_end: UInt8 } internal let audio: Audio @@ -83,16 +83,16 @@ internal struct DebugState { // MARK: - Lcd internal struct Lcd { - internal let control: UInt8 - internal let status: UInt8 - internal let scrollY: UInt8 - internal let scrollX: UInt8 - internal let line: UInt8 + internal let control: UInt8 + internal let status: UInt8 + internal let scrollY: UInt8 + internal let scrollX: UInt8 + internal let line: UInt8 internal let lineCompare: UInt8 - internal let dma: UInt8 + internal let dma: UInt8 internal let backgroundPalette: UInt8 - internal let spritePalette0: UInt8 - internal let spritePalette1: UInt8 + internal let spritePalette0: UInt8 + internal let spritePalette1: UInt8 internal let windowY: UInt8 internal let windowX: UInt8 } @@ -103,7 +103,7 @@ internal struct DebugState { internal struct Interrupts { internal let enable: UInt8 - internal let flag: UInt8 + internal let flag: UInt8 } internal let interrupts: Interrupts diff --git a/Sources/GameBoyKit/Debug/Debugger+Cpu.swift b/Sources/GameBoyKit/Debug/Debugger+Cpu.swift index b3969d5..efed4a2 100644 --- a/Sources/GameBoyKit/Debug/Debugger+Cpu.swift +++ b/Sources/GameBoyKit/Debug/Debugger+Cpu.swift @@ -4,22 +4,22 @@ // From Blargg tests private let unprefixedLengths = [ - 1,3,1,1,1,1,2,1,3,1,1,1,1,1,2,1, // 0 - 0,3,1,1,1,1,2,1,2,1,1,1,1,1,2,1, // 1 - 2,3,1,1,1,1,2,1,2,1,1,1,1,1,2,1, // 2 - 2,3,1,1,1,1,2,1,2,1,1,1,1,1,2,1, // 3 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 4 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 5 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 6 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 7 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 8 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 9 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // A - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // B - 1,1,3,3,3,1,2,1,1,1,3,0,3,3,2,1, // C - 1,1,3,0,3,1,2,1,1,1,3,0,3,0,2,1, // D - 2,1,1,0,0,1,2,1,2,1,3,0,0,0,2,1, // E - 2,1,1,1,0,1,2,1,2,1,3,1,0,0,2,1 // F + 1, 3, 1, 1, 1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 2, 1, // 0 + 0, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, // 1 + 2, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, // 2 + 2, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, // 3 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 4 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 5 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 6 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 7 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 8 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 9 + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B + 1, 1, 3, 3, 3, 1, 2, 1, 1, 1, 3, 0, 3, 3, 2, 1, // C + 1, 1, 3, 0, 3, 1, 2, 1, 1, 1, 3, 0, 3, 0, 2, 1, // D + 2, 1, 1, 0, 0, 1, 2, 1, 2, 1, 3, 0, 0, 0, 2, 1, // E + 2, 1, 1, 1, 0, 1, 2, 1, 2, 1, 3, 1, 0, 0, 2, 1 // F ] extension Debugger { @@ -29,7 +29,7 @@ extension Debugger { } private func next16(pc: UInt16) -> UInt16 { - let low = UInt16(self.memory.read(pc + 1)) + let low = UInt16(self.memory.read(pc + 1)) let high = UInt16(self.memory.read(pc + 2)) return (high << 8) | low } @@ -49,7 +49,7 @@ extension Debugger { switch self.unprefixedOpcodeAt(pc: self.cpu.pc) { case .none: return case let .some(opcode) where opcode == .prefix: self.printPrefixedOpcode() - case let .some(opcode): printUnprefixedOpcode(opcode) + case let .some(opcode): self.printUnprefixedOpcode(opcode) } } @@ -88,7 +88,7 @@ extension Debugger { return } - let next8 = self.next8(pc: before.cpu.pc) + let next8 = self.next8(pc: before.cpu.pc) let next16 = self.next16(pc: before.cpu.pc) switch opcode { @@ -100,7 +100,7 @@ extension Debugger { case .jp_a16, .jp_pHL: print(" > jump to \(next16.hex)") - case .jp_c_a16, .jp_nc_a16, .jp_nz_a16, .jp_z_a16: + case .jp_c_a16, .jp_nc_a16, .jp_nz_a16, .jp_z_a16: let taken = after.cpu.pc == next16 ? "TAKEN" : "NOT TAKEN" print(" > conditional jump to \(next16.hex) \(taken)") @@ -172,7 +172,7 @@ extension Debugger { public func dumpCpuState() { let stackStart: UInt16 = self.cpu.sp - var stackEnd: UInt16 = 0xfffe + var stackEnd: UInt16 = 0xfffe let stackCount = stackEnd - stackStart if stackCount > 20 { @@ -188,27 +188,27 @@ Cpu sp: \(cpu.sp) (\(cpu.sp.hex)) \(stackValues.map { $0.hex }) cycle: \(cpu.cycle) auxiliary registers: - a: \(formatRegister(r.a)) - b: \(formatRegister(r.b)) | c: \(formatRegister(r.c)) | bc: \(formatRegister(r.bc)) - d: \(formatRegister(r.d)) | e: \(formatRegister(r.e)) | de: \(formatRegister(r.de)) - h: \(formatRegister(r.h)) | l: \(formatRegister(r.l)) | hl: \(formatRegister(r.hl)) + a: \(self.format(r.a)) + b: \(self.format(r.b)) | c: \(self.format(r.c)) | bc: \(self.format(r.bc)) + d: \(self.format(r.d)) | e: \(self.format(r.e)) | de: \(self.format(r.de)) + h: \(self.format(r.h)) | l: \(self.format(r.l)) | hl: \(self.format(r.hl)) flags: - z: \(formatFlag(r.zeroFlag)) - n: \(formatFlag(r.subtractFlag)) - h: \(formatFlag(r.halfCarryFlag)) - c: \(formatFlag(r.carryFlag)) + z: \(self.format(r.zeroFlag)) + n: \(self.format(r.subtractFlag)) + h: \(self.format(r.halfCarryFlag)) + c: \(self.format(r.carryFlag)) """) } - private func formatRegister(_ value: UInt8) -> String { + private func format(_ value: UInt8) -> String { return "\(value.dec) (\(value.hex))" } - private func formatRegister(_ value: UInt16) -> String { + private func format(_ value: UInt16) -> String { return "\(value.dec) (\(value.hex))" } - private func formatFlag(_ value: Bool) -> String { + private func format(_ value: Bool) -> String { return value ? "1" : "0" } } diff --git a/Sources/GameBoyKit/Debug/Debugger+Lcd.swift b/Sources/GameBoyKit/Debug/Debugger+Lcd.swift index eb17e24..a2512e2 100644 --- a/Sources/GameBoyKit/Debug/Debugger+Lcd.swift +++ b/Sources/GameBoyKit/Debug/Debugger+Lcd.swift @@ -4,8 +4,8 @@ // swiftlint:disable function_body_length -private let tileSize = 8 // pixels -private let tileRowCount = 32 +private let tileSize = 8 // pixels +private let tileRowCount = 32 private let tileColumnCount = 32 private let tilesPerRow = 32 @@ -56,7 +56,7 @@ Lcd print("Tile indices \(tileMap):") // horizontal markers - print(" | " , separator: "", terminator: "") + print(" | ", separator: "", terminator: "") for tileColumn in 0.. 0 && self.cpu.pc != pc { @@ -75,15 +77,17 @@ public final class Debugger { // MARK: - Fill state +// swiftformat:disable consecutiveSpaces + extension Debugger { private func captureState() -> DebugState { return DebugState( cpu: self.captureCpuState(), - io: self.captureIOState(), + io: self.captureIOState(), timer: self.captureTimerState(), audio: self.captureAudioState(), - lcd: self.captureLcdState(), + lcd: self.captureLcdState(), interrupts: self.captureInterruptsState() ) } @@ -97,10 +101,10 @@ extension Debugger { e: self.cpu.registers.e, h: self.cpu.registers.h, l: self.cpu.registers.l, - zeroFlag: self.cpu.registers.zeroFlag, - subtractFlag: self.cpu.registers.subtractFlag, + zeroFlag: self.cpu.registers.zeroFlag, + subtractFlag: self.cpu.registers.subtractFlag, halfCarryFlag: self.cpu.registers.halfCarryFlag, - carryFlag: self.cpu.registers.carryFlag, + carryFlag: self.cpu.registers.carryFlag, pc: self.cpu.pc, sp: self.cpu.sp ) @@ -154,11 +158,11 @@ extension Debugger { private func captureLcdState() -> DebugState.Lcd { return DebugState.Lcd( - control: self.memory.read(MemoryMap.Lcd.control), - status: self.memory.read(MemoryMap.Lcd.status), - scrollY: self.memory.read(MemoryMap.Lcd.scrollY), - scrollX: self.memory.read(MemoryMap.Lcd.scrollX), - line: self.memory.read(MemoryMap.Lcd.line), + control: self.memory.read(MemoryMap.Lcd.control), + status: self.memory.read(MemoryMap.Lcd.status), + scrollY: self.memory.read(MemoryMap.Lcd.scrollY), + scrollX: self.memory.read(MemoryMap.Lcd.scrollX), + line: self.memory.read(MemoryMap.Lcd.line), lineCompare: self.memory.read(MemoryMap.Lcd.lineCompare), dma: self.memory.read(MemoryMap.Lcd.dma), backgroundPalette: self.memory.read(MemoryMap.Lcd.backgroundPalette), diff --git a/Sources/GameBoyKit/Debug/UnprefixedOpcode.swift b/Sources/GameBoyKit/Debug/UnprefixedOpcode.swift index 19baeeb..490dced 100644 --- a/Sources/GameBoyKit/Debug/UnprefixedOpcode.swift +++ b/Sources/GameBoyKit/Debug/UnprefixedOpcode.swift @@ -271,4 +271,3 @@ internal enum UnprefixedOpcode: UInt8, RawRepresentable { case cp_d8 = 0xfe case rst_38 = 0xff } - diff --git a/Sources/GameBoyKit/Extensions.swift b/Sources/GameBoyKit/Extensions.swift index 53f4a10..bf1cc34 100644 --- a/Sources/GameBoyKit/Extensions.swift +++ b/Sources/GameBoyKit/Extensions.swift @@ -39,13 +39,13 @@ extension UInt8 { return "\(padding)\(s)" } - public var hex: String { + public var hex: String { let s = String(self, radix: 16, uppercase: false) let padding = String(repeating: "0", count: 2 - s.count) return "0x\(padding)\(s)" } - public var bin: String { + public var bin: String { let s = String(self, radix: 2, uppercase: false) let padding = String(repeating: "0", count: 8 - s.count) return "0b\(padding)\(s)" @@ -60,13 +60,13 @@ extension UInt16 { return "\(padding)\(s)" } - public var hex: String { + public var hex: String { let s = String(self, radix: 16, uppercase: false) let padding = String(repeating: "0", count: 4 - s.count) return "0x\(padding)\(s)" } - public var bin: String { + public var bin: String { let s = String(self, radix: 2, uppercase: false) let padding = String(repeating: "0", count: 16 - s.count) return "0b\(padding)\(s)" diff --git a/Sources/GameBoyKit/GameBoy.swift b/Sources/GameBoyKit/GameBoy.swift index 359ddca..eb79176 100644 --- a/Sources/GameBoyKit/GameBoy.swift +++ b/Sources/GameBoyKit/GameBoy.swift @@ -26,7 +26,8 @@ public final class GameBoy { public init(bootrom: Bootrom?, cartridge: Cartridge, - input: GameboyInputProvider) { + input: GameboyInputProvider) + { let interrupts = Interrupts() self.lcd = Lcd(interrupts: interrupts) self.audio = Audio() @@ -35,15 +36,15 @@ public final class GameBoy { self.serialPort = SerialPort() self.linkCable = LinkCable() - self.memory = Memory(bootrom: bootrom, - cartridge: cartridge, - joypad: self.joypad, - lcd: self.lcd, - audio: self.audio, - timer: self.timer, + self.memory = Memory(bootrom: bootrom, + cartridge: cartridge, + joypad: self.joypad, + lcd: self.lcd, + audio: self.audio, + timer: self.timer, interrupts: interrupts, serialPort: self.serialPort, - linkCable: self.linkCable) + linkCable: self.linkCable) self.cpu = Cpu(memory: self.memory, interrupts: interrupts) diff --git a/Sources/GameBoyKit/GameboyInput.swift b/Sources/GameBoyKit/GameboyInput.swift index 72aa85e..6be9cfb 100644 --- a/Sources/GameBoyKit/GameboyInput.swift +++ b/Sources/GameBoyKit/GameboyInput.swift @@ -4,21 +4,17 @@ public struct GameboyInput { - public var up = false - public var down = false - public var left = false + public var up = false + public var down = false + public var left = false public var right = false public var a = false public var b = false - public var start = false + public var start = false public var select = false - #if DEBUG - public var debug = false - #endif - - public init() { } + public init() {} } public protocol GameboyInputProvider: AnyObject { diff --git a/Sources/GameBoyKit/IO/Joypad.swift b/Sources/GameBoyKit/IO/Joypad.swift index 7bdc172..86db298 100644 --- a/Sources/GameBoyKit/IO/Joypad.swift +++ b/Sources/GameBoyKit/IO/Joypad.swift @@ -2,6 +2,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +// swiftformat:disable consecutiveSpaces + private let buttonsMask: UInt8 = 1 << 5 private let directionKeysMask: UInt8 = 1 << 4 @@ -15,8 +17,6 @@ private let downMask: UInt8 = 1 << 3 private let leftMask: UInt8 = 1 << 1 private let rightMask: UInt8 = 1 << 0 -internal var inputBreak = false - /// FF00 - P1/JOYP public final class Joypad: JoypadMemory { @@ -38,16 +38,12 @@ public final class Joypad: JoypadMemory { } isButtons ? self.setButtons(from: input) : self.setDirections(from: input) - - #if DEBUG - inputBreak = input.debug - #endif } } private weak var provider: GameboyInputProvider? - internal init (provider: GameboyInputProvider) { + internal init(provider: GameboyInputProvider) { self.provider = provider } @@ -55,15 +51,15 @@ public final class Joypad: JoypadMemory { self._value = 0 if !input.a { self._value |= aMask } if !input.b { self._value |= bMask } - if !input.start { self._value |= startMask } + if !input.start { self._value |= startMask } if !input.select { self._value |= selectMask } } private func setDirections(from input: GameboyInput) { self._value = 0 - if !input.up { self._value |= upMask } - if !input.down { self._value |= downMask } - if !input.left { self._value |= leftMask } + if !input.up { self._value |= upMask } + if !input.down { self._value |= downMask } + if !input.left { self._value |= leftMask } if !input.right { self._value |= rightMask } } } diff --git a/Sources/GameBoyKit/IO/Timer.swift b/Sources/GameBoyKit/IO/Timer.swift index d6cc25a..134b616 100644 --- a/Sources/GameBoyKit/IO/Timer.swift +++ b/Sources/GameBoyKit/IO/Timer.swift @@ -61,7 +61,7 @@ public final class Timer: TimerMemory { /// FF06 - TMA - Timer Modulo. /// When the TIMA overflows, this data will be loaded. - public internal(set) var tma: UInt8 = 0x00 + public internal(set) var tma: UInt8 = 0x00 /// FF07 - TAC - Timer Control. /// Bit 2 - stop timer, bits 1 and 0 - select clock @@ -114,6 +114,7 @@ public final class Timer: TimerMemory { } private func getPeriod(tac: UInt8) -> Int { + // swiftformat:disable consecutiveSpaces switch tac & 0b11 { case 0b00: return Cpu.clockSpeed / 4_096 // 1024 case 0b01: return Cpu.clockSpeed / 262_144 // 16 @@ -121,5 +122,6 @@ public final class Timer: TimerMemory { case 0b11: return Cpu.clockSpeed / 16_384 // 256 default: fatalError("Invalid timer frequency.") // ? } + // swiftformat:enable consecutiveSpaces } } diff --git a/Sources/GameBoyKit/Lcd/Entities/LcdControl.swift b/Sources/GameBoyKit/Lcd/Entities/LcdControl.swift index 804bd37..2626791 100644 --- a/Sources/GameBoyKit/Lcd/Entities/LcdControl.swift +++ b/Sources/GameBoyKit/Lcd/Entities/LcdControl.swift @@ -5,6 +5,7 @@ public struct LcdControl { internal enum Masks { + // swiftformat:disable consecutiveSpaces internal static let isLcdEnabled: UInt8 = 1 << 7 internal static let isWindowEnabled: UInt8 = 1 << 5 internal static let windowTileMap: UInt8 = 1 << 6 @@ -13,6 +14,7 @@ public struct LcdControl { internal static let spriteSize: UInt8 = 1 << 2 internal static let isSpriteEnabled: UInt8 = 1 << 1 internal static let isBackgroundVisible: UInt8 = 1 << 0 + // swiftformat:enable consecutiveSpaces } public let value: UInt8 diff --git a/Sources/GameBoyKit/Lcd/Entities/LcdStatus.swift b/Sources/GameBoyKit/Lcd/Entities/LcdStatus.swift index 59a05da..425999d 100644 --- a/Sources/GameBoyKit/Lcd/Entities/LcdStatus.swift +++ b/Sources/GameBoyKit/Lcd/Entities/LcdStatus.swift @@ -5,12 +5,14 @@ public struct LcdStatus { internal enum Masks { + // swiftformat:disable consecutiveSpaces internal static let isLineCompareInterruptEnabled: UInt8 = 1 << 6 internal static let isOamInterruptEnabled: UInt8 = 1 << 5 internal static let isVBlankInterruptEnabled: UInt8 = 1 << 4 internal static let isHBlankInterruptEnabled: UInt8 = 1 << 3 internal static let isLineCompareInterrupt: UInt8 = 1 << 2 internal static let mode: UInt8 = 0b11 + // swiftformat:enable consecutiveSpaces } public let value: UInt8 diff --git a/Sources/GameBoyKit/Lcd/Framebuffer.swift b/Sources/GameBoyKit/Lcd/Framebuffer.swift index 2f1284f..b46a38c 100644 --- a/Sources/GameBoyKit/Lcd/Framebuffer.swift +++ b/Sources/GameBoyKit/Lcd/Framebuffer.swift @@ -6,7 +6,7 @@ public struct Framebuffer { public typealias PixelBufferPointer = UnsafeMutableBufferPointer - public static let width = Lcd.Constants.width + public static let width = Lcd.Constants.width public static let height = Lcd.Constants.height /// Data that should be put on screen: diff --git a/Sources/GameBoyKit/Lcd/Lcd+Draw.swift b/Sources/GameBoyKit/Lcd/Lcd+Draw.swift index b02874a..9ecd0dc 100644 --- a/Sources/GameBoyKit/Lcd/Lcd+Draw.swift +++ b/Sources/GameBoyKit/Lcd/Lcd+Draw.swift @@ -24,9 +24,9 @@ extension Lcd { // swiftlint:disable:next function_body_length private func drawBackgroundLine() { - let line = Int(self.line) - let globalY = (Int(self.scrollY) + line) % Constants.backgroundMapHeight - let tileRow = globalY / Tile.Constants.height + let line = Int(self.line) + let globalY = (Int(self.scrollY) + line) % Constants.backgroundMapHeight + let tileRow = globalY / Tile.Constants.height let tileLine = globalY % Tile.Constants.height let tileMap = self.getTileMap(for: self.control.backgroundTileMap) @@ -48,7 +48,7 @@ extension Lcd { let pixelsToEnd = framebufferSlice.count - progress let startBit = globalX % Tile.Constants.width - let lastBit = min(Tile.Constants.width, pixelsToEnd) + let lastBit = min(Tile.Constants.width, pixelsToEnd) for bit in startBit..= 0 + let inScreenLeft = sprite.realX + Tile.Constants.width >= 0 let inScreenRight = sprite.realX < Constants.width guard inScreenLeft && inScreenRight else { continue @@ -240,7 +240,7 @@ extension Lcd { for sprite in self.sprites { let isAfterStart = line >= sprite.realY - let isBeforeEnd = line < (sprite.realY + spriteHeight) + let isBeforeEnd = line < (sprite.realY + spriteHeight) guard isAfterStart && isBeforeEnd else { continue diff --git a/Sources/GameBoyKit/Lcd/Lcd.swift b/Sources/GameBoyKit/Lcd/Lcd.swift index 8925bf7..4e5a074 100644 --- a/Sources/GameBoyKit/Lcd/Lcd.swift +++ b/Sources/GameBoyKit/Lcd/Lcd.swift @@ -29,7 +29,7 @@ public final class Lcd: LcdMemory { public internal(set) var scrollY: UInt8 = 0 public internal(set) var scrollX: UInt8 = 0 - public internal(set) var line: UInt8 = 0 + public internal(set) var line: UInt8 = 0 public internal(set) var lineCompare: UInt8 = 0 public internal(set) var windowY: UInt8 = 0 @@ -42,12 +42,12 @@ public final class Lcd: LcdMemory { public internal(set) var tileMap9800to9bff = MemoryBuffer(region: VideoRamMap.tileMap9800to9bff) public internal(set) var tileMap9c00to9fff = MemoryBuffer(region: VideoRamMap.tileMap9c00to9fff) - internal lazy var tiles = (0.. UInt8 { + internal static func getColorValue(_ data1: UInt8, + _ data2: UInt8, + bit: Int) -> UInt8 + { let shift = 7 - bit let data1Bit = (data1 >> shift) & 0x1 let data2Bit = (data2 >> shift) & 0x1 diff --git a/Sources/GameBoyKit/Memory/Memory+read.swift b/Sources/GameBoyKit/Memory/Memory+read.swift index 4a34bf8..788f788 100644 --- a/Sources/GameBoyKit/Memory/Memory+read.swift +++ b/Sources/GameBoyKit/Memory/Memory+read.swift @@ -64,15 +64,15 @@ extension Memory { private func readInternalIO(_ address: UInt16) -> UInt8 { switch address { case MemoryMap.IO.joypad: return self.joypad.value - case MemoryMap.IO.sb: return self.serialPort.sb - case MemoryMap.IO.sc: return self.serialPort.sc - case MemoryMap.IO.unmapBootrom: return defaultValue + case MemoryMap.IO.sb: return self.serialPort.sb + case MemoryMap.IO.sc: return self.serialPort.sc + case MemoryMap.IO.unmapBootrom: return defaultValue case MemoryMap.IO.interruptFlag: return self.interrupts.flag - case MemoryMap.Timer.div: return self.timer.div + case MemoryMap.Timer.div: return self.timer.div case MemoryMap.Timer.tima: return self.timer.tima - case MemoryMap.Timer.tma: return self.timer.tma - case MemoryMap.Timer.tac: return self.timer.tac + case MemoryMap.Timer.tma: return self.timer.tma + case MemoryMap.Timer.tac: return self.timer.tac case MemoryMap.Audio.nr10: return self.audio.nr10 case MemoryMap.Audio.nr11: return self.audio.nr11 @@ -96,18 +96,18 @@ extension Memory { case MemoryMap.Audio.nr51: return self.audio.nr51 case MemoryMap.Audio.nr52: return self.audio.nr52 case MemoryMap.Audio.nr3_ram_start: return self.audio.nr3_ram_start - case MemoryMap.Audio.nr3_ram_end: return self.audio.nr3_ram_end + case MemoryMap.Audio.nr3_ram_end: return self.audio.nr3_ram_end case MemoryMap.Lcd.control: return self.lcd.control.value - case MemoryMap.Lcd.status: return self.lcd.status.value + case MemoryMap.Lcd.status: return self.lcd.status.value case MemoryMap.Lcd.scrollY: return self.lcd.scrollY case MemoryMap.Lcd.scrollX: return self.lcd.scrollX - case MemoryMap.Lcd.line: return self.lcd.line + case MemoryMap.Lcd.line: return self.lcd.line case MemoryMap.Lcd.lineCompare: return self.lcd.lineCompare - case MemoryMap.Lcd.dma: return defaultValue + case MemoryMap.Lcd.dma: return defaultValue case MemoryMap.Lcd.backgroundPalette: return self.lcd.backgroundColorPalette.value - case MemoryMap.Lcd.spritePalette0: return self.lcd.spriteColorPalette0.value - case MemoryMap.Lcd.spritePalette1: return self.lcd.spriteColorPalette1.value + case MemoryMap.Lcd.spritePalette0: return self.lcd.spriteColorPalette0.value + case MemoryMap.Lcd.spritePalette1: return self.lcd.spriteColorPalette1.value case MemoryMap.Lcd.windowY: return self.lcd.windowY case MemoryMap.Lcd.windowX: return self.lcd.windowX diff --git a/Sources/GameBoyKit/Memory/Memory+write.swift b/Sources/GameBoyKit/Memory/Memory+write.swift index d514043..c30ef56 100644 --- a/Sources/GameBoyKit/Memory/Memory+write.swift +++ b/Sources/GameBoyKit/Memory/Memory+write.swift @@ -63,14 +63,14 @@ extension Memory { case MemoryMap.IO.sb: self.linkCable.write(value) self.serialPort.sb = value - case MemoryMap.IO.sc: self.serialPort.sc = value - case MemoryMap.IO.unmapBootrom: self.isRunningBootrom = false + case MemoryMap.IO.sc: self.serialPort.sc = value + case MemoryMap.IO.unmapBootrom: self.isRunningBootrom = false case MemoryMap.IO.interruptFlag: self.interrupts.flag = value - case MemoryMap.Timer.div: self.timer.div = value + case MemoryMap.Timer.div: self.timer.div = value case MemoryMap.Timer.tima: self.timer.tima = value - case MemoryMap.Timer.tma: self.timer.tma = value - case MemoryMap.Timer.tac: self.timer.tac = value + case MemoryMap.Timer.tma: self.timer.tma = value + case MemoryMap.Timer.tac: self.timer.tac = value case MemoryMap.Audio.nr10: self.audio.nr10 = value case MemoryMap.Audio.nr11: self.audio.nr11 = value @@ -94,7 +94,7 @@ extension Memory { case MemoryMap.Audio.nr51: self.audio.nr51 = value case MemoryMap.Audio.nr52: self.audio.nr52 = value case MemoryMap.Audio.nr3_ram_start: self.audio.nr3_ram_start = value - case MemoryMap.Audio.nr3_ram_end: self.audio.nr3_ram_end = value + case MemoryMap.Audio.nr3_ram_end: self.audio.nr3_ram_end = value case MemoryMap.Lcd.control: self.lcd.control = LcdControl(value: value) diff --git a/Sources/GameBoyKit/Memory/Memory.swift b/Sources/GameBoyKit/Memory/Memory.swift index c8aee54..309814e 100644 --- a/Sources/GameBoyKit/Memory/Memory.swift +++ b/Sources/GameBoyKit/Memory/Memory.swift @@ -28,17 +28,18 @@ public final class Memory: CpuMemory { internal lazy var highRam = MemoryBuffer(region: MemoryMap.highRam) /// Catch'em all for any invalid read/write - internal var unmappedMemory = [UInt16:UInt8]() - - internal init(bootrom: BootromMemory?, - cartridge: CartridgeMemory, - joypad: JoypadMemory, - lcd: LcdMemory, - audio: AudioMemory, - timer: TimerMemory, + internal var unmappedMemory = [UInt16: UInt8]() + + internal init(bootrom: BootromMemory?, + cartridge: CartridgeMemory, + joypad: JoypadMemory, + lcd: LcdMemory, + audio: AudioMemory, + timer: TimerMemory, interrupts: Interrupts, serialPort: SerialPort, - linkCable: LinkCable) { + linkCable: LinkCable) + { self.joypad = joypad self.lcd = lcd self.audio = audio diff --git a/Sources/GameBoyKit/Memory/MemoryMap.swift b/Sources/GameBoyKit/Memory/MemoryMap.swift index bbd4649..9b321fa 100644 --- a/Sources/GameBoyKit/Memory/MemoryMap.swift +++ b/Sources/GameBoyKit/Memory/MemoryMap.swift @@ -3,6 +3,8 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. // swiftlint:disable type_name +// swiftformat:disable consecutiveSpaces +// swiftformat:disable redundantSelf public enum MemoryMap { diff --git a/Sources/GameBoyMac/GameBoyWindow.swift b/Sources/GameBoyMac/GameBoyWindow.swift index 5381606..c6e5439 100644 --- a/Sources/GameBoyMac/GameBoyWindow.swift +++ b/Sources/GameBoyMac/GameBoyWindow.swift @@ -12,13 +12,13 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate { private(set) var gameBoy: GameBoy! let keyMap: KeyMap - let device: MTLDevice - let pipeline: MTLRenderPipelineState + let device: MTLDevice + let pipeline: MTLRenderPipelineState let commandQueue: MTLCommandQueue let vertexBuffer: MTLBuffer - let texture: MTLTexture + let texture: MTLTexture - override var canBecomeKey: Bool { return true } + override var canBecomeKey: Bool { return true } override var canBecomeMain: Bool { return true } // swiftlint:disable:next function_body_length @@ -26,11 +26,11 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate { self.keyMap = keyMap self.device = Metal.createDevice() - let library = Metal.makeLibrary(device: self.device) - self.pipeline = Metal.makePipeline(device: self.device, library: library) + let library = Metal.makeLibrary(device: self.device) + self.pipeline = Metal.makePipeline(device: self.device, library: library) self.commandQueue = Metal.makeCommandQueue(device: self.device) self.vertexBuffer = Metal.makeFullscreenVertexBuffer(device: self.device) - self.texture = Metal.makeFramebuffer(device: self.device) + self.texture = Metal.makeFramebuffer(device: self.device) super.init( contentRect: NSRect( @@ -76,7 +76,7 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate { private var input = GameboyInput() func getGameboyInput() -> GameboyInput { - return input + return self.input } override func keyDown(with event: NSEvent) { @@ -91,6 +91,7 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate { if event.isARepeat { return } switch event.keyCode { + // swiftformat:disable consecutiveSpaces case self.keyMap.a.value: self.input.a = isDown case self.keyMap.b.value: self.input.b = isDown case self.keyMap.start.value: self.input.start = isDown @@ -99,6 +100,7 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate { case self.keyMap.down.value: self.input.down = isDown case self.keyMap.left.value: self.input.left = isDown case self.keyMap.right.value: self.input.right = isDown + // swiftformat:enable consecutiveSpaces default: // Use this if you want to propagate event down the responder chain: @@ -109,19 +111,21 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate { // MARK: - MTKViewDelegate - func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) { } + func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {} func draw(in view: MTKView) { self.gameBoy.tickFrame() self.updateFramebuffer() guard let drawable = view.currentDrawable, - let renderPassDesc = view.currentRenderPassDescriptor else { + let renderPassDesc = view.currentRenderPassDescriptor + else { fatalError("Error when rendering Metal frame: unable to obtain drawable.") } guard let cmdBuffer = self.commandQueue.makeCommandBuffer(), - let cmdEncoder = cmdBuffer.makeRenderCommandEncoder(descriptor: renderPassDesc) else { + let cmdEncoder = cmdBuffer.makeRenderCommandEncoder(descriptor: renderPassDesc) + else { fatalError("Error when rendering Metal frame: unable to create command buffer.") } @@ -142,9 +146,9 @@ class GameBoyWindow: NSWindow, GameboyInputProvider, MTKViewDelegate { framebuffer.pixels.withUnsafeBytes { ptr in guard let baseAddress = ptr.baseAddress else { return } texture.replace( - region: region, + region: region, mipmapLevel: 0, - withBytes: baseAddress, + withBytes: baseAddress, bytesPerRow: Framebuffer.width * MemoryLayout.size ) } diff --git a/Sources/GameBoyMac/Metal.swift b/Sources/GameBoyMac/Metal.swift index 3be59a8..c2b0b21 100644 --- a/Sources/GameBoyMac/Metal.swift +++ b/Sources/GameBoyMac/Metal.swift @@ -8,7 +8,7 @@ import GameBoyKit enum Metal { static func createDevice() -> MTLDevice { - if let lowPowerDevice = MTLCopyAllDevices().first(where: { $0.isLowPower }) { + if let lowPowerDevice = MTLCopyAllDevices().first(where: { $0.isLowPower }) { return lowPowerDevice } @@ -40,7 +40,7 @@ enum Metal { static func makePipeline(device: MTLDevice, library: MTLLibrary) -> MTLRenderPipelineState { let pipelineDesc = MTLRenderPipelineDescriptor() - pipelineDesc.vertexFunction = library.makeFunction(name: "vertex_shader") + pipelineDesc.vertexFunction = library.makeFunction(name: "vertex_shader") pipelineDesc.fragmentFunction = library.makeFunction(name: "fragment_shader") pipelineDesc.colorAttachments[0].pixelFormat = .bgra8Unorm @@ -63,15 +63,12 @@ enum Metal { let data: [Float] = [ -1.0, -1.0, 1.0, -1.0, // swiftlint:disable:this collection_alignment - -1.0, 1.0, - 1.0, 1.0 // swiftlint:disable:this collection_alignment + -1.0, 1.0, + 1.0, 1.0 // swiftlint:disable:this collection_alignment ] - if let buffer = device.makeBuffer( - bytes: data, - length: data.count * MemoryLayout.size, - options: []) { - + let bufferCount = data.count * MemoryLayout.size + if let buffer = device.makeBuffer(bytes: data, length: bufferCount, options: []) { return buffer } @@ -81,9 +78,9 @@ enum Metal { static func makeFramebuffer(device: MTLDevice) -> MTLTexture { let textureDesc = MTLTextureDescriptor.texture2DDescriptor( pixelFormat: .r8Uint, - width: GameBoyKit.Framebuffer.width, - height: GameBoyKit.Framebuffer.height, - mipmapped: false + width: GameBoyKit.Framebuffer.width, + height: GameBoyKit.Framebuffer.height, + mipmapped: false ) if let texture = device.makeTexture(descriptor: textureDesc) { diff --git a/Tests/GameBoyKitROMTests/BlarggTests.swift b/Tests/GameBoyKitROMTests/BlarggTests.swift index d0f2b82..dc0592c 100644 --- a/Tests/GameBoyKitROMTests/BlarggTests.swift +++ b/Tests/GameBoyKitROMTests/BlarggTests.swift @@ -109,7 +109,8 @@ enum BlarggTests { private func test(romPath: String, dumpDirName: String, frames: Int, - compareWithDumps: Bool) { + compareWithDumps: Bool) +{ print(romPath, terminator: "") let input = DummyInputProvider() diff --git a/Tests/GameBoyKitROMTests/BootromTests.swift b/Tests/GameBoyKitROMTests/BootromTests.swift index 4342881..d825da7 100644 --- a/Tests/GameBoyKitROMTests/BootromTests.swift +++ b/Tests/GameBoyKitROMTests/BootromTests.swift @@ -29,14 +29,14 @@ enum BootromTests { debugger.run(mode: .none, untilPC: 0x0100) } - checkFinalStateBeforeUnmappingBootrom(gameBoy: gameBoy) + self.checkFinalStateBeforeUnmappingBootrom(gameBoy: gameBoy) } private static func openRom() -> Cartridge { do { let data = try Data(contentsOf: romUrl) return try CartridgeFactory.create(data: data) - } catch { + } catch { fatalError("Unable to open: '\(romUrl)'") } } @@ -112,7 +112,7 @@ enum BootromTests { 0xff49: 0xff, // obp1 0xff4a: 0x00, // wy 0xff4b: 0x00, // wx - 0xffff: 0x00 // ie + 0xffff: 0x00 // ie ] for address in memoryAddressesToTest { diff --git a/Tests/GameBoyKitROMTests/Dump files/DumpFileContent.swift b/Tests/GameBoyKitROMTests/Dump files/DumpFileContent.swift index cfcbdad..b27de4d 100644 --- a/Tests/GameBoyKitROMTests/Dump files/DumpFileContent.swift +++ b/Tests/GameBoyKitROMTests/Dump files/DumpFileContent.swift @@ -28,5 +28,5 @@ struct DumpFileContent { } var cpu = Cpu() - var memory = [UInt8](repeating: 0, count: 0x10000) + var memory = [UInt8](repeating: 0, count: 0x1_0000) } diff --git a/Tests/GameBoyKitROMTests/Dump files/StreamReader.swift b/Tests/GameBoyKitROMTests/Dump files/StreamReader.swift index 48253bf..ce54002 100644 --- a/Tests/GameBoyKitROMTests/Dump files/StreamReader.swift +++ b/Tests/GameBoyKitROMTests/Dump files/StreamReader.swift @@ -7,13 +7,13 @@ import Foundation // Source: // https://stackoverflow.com/questions/24581517/read-a-file-url-line-by-line-in-swift -class StreamReader { +class StreamReader { private var fileHandle: FileHandle? - private let encoding: String.Encoding - private let delimiter: Data + private let encoding: String.Encoding + private let delimiter: Data - private var buffer: Data + private var buffer: Data private let bufferSize: Int private var isEof = false @@ -21,17 +21,19 @@ class StreamReader { init?(url: URL, delimiter: String = "\n", encoding: String.Encoding = .utf8, - bufferSize: Int = 4_096) { + bufferSize: Int = 4_096) + { guard let fileHandle = FileHandle(forReadingAtPath: url.path), - let delimiterData = delimiter.data(using: encoding) else { + let delimiterData = delimiter.data(using: encoding) + else { return nil } - self.encoding = encoding + self.encoding = encoding self.bufferSize = bufferSize self.fileHandle = fileHandle - self.delimiter = delimiterData - self.buffer = Data(capacity: bufferSize) + self.delimiter = delimiterData + self.buffer = Data(capacity: bufferSize) } deinit { @@ -50,8 +52,9 @@ class StreamReader { while !self.isEof { if let range = self.buffer.range(of: self.delimiter) { - let line = String(data: buffer.subdata(in: 0.. AnyIterator { return AnyIterator { self.nextLine() } } diff --git a/Tests/GameBoyKitROMTests/Dump files/openDumpFile.swift b/Tests/GameBoyKitROMTests/Dump files/openDumpFile.swift index 3fdda9e..aded428 100644 --- a/Tests/GameBoyKitROMTests/Dump files/openDumpFile.swift +++ b/Tests/GameBoyKitROMTests/Dump files/openDumpFile.swift @@ -16,17 +16,17 @@ private func toData(_ s: String) -> Data { return s.data(using: .ascii)! } -private let cpu_A = toData("cpu_A") -private let cpu_B = toData("cpu_B") -private let cpu_C = toData("cpu_C") -private let cpu_D = toData("cpu_D") -private let cpu_E = toData("cpu_E") +private let cpu_A = toData("cpu_A") +private let cpu_B = toData("cpu_B") +private let cpu_C = toData("cpu_C") +private let cpu_D = toData("cpu_D") +private let cpu_E = toData("cpu_E") private let cpu_HL = toData("cpu_HL") -private let cpu_c_carry = toData("cpu_c_carry") +private let cpu_c_carry = toData("cpu_c_carry") private let cpu_h_halfcarry = toData("cpu_h_halfcarry") private let cpu_n_substract = toData("cpu_n_substract") -private let cpu_z_zero = toData("cpu_z_zero") +private let cpu_z_zero = toData("cpu_z_zero") private let cpu_SP = toData("cpu_SP") private let cpu_PC = toData("cpu_PC") @@ -34,7 +34,7 @@ private let total_ticks = toData("total_ticks") private let instruction_ticks = toData("instruction_ticks") private let cpu_interrupt_master_enable = toData("cpu_interrupt_master_enable") -private let cpu_halted = toData("cpu_halted") +private let cpu_halted = toData("cpu_halted") private let cpu_stopped = toData("cpu_stopped") private let memory = toData("memory") @@ -61,38 +61,52 @@ private func fill(state: inout DumpFileContent, from fileUrl: URL) { let property = line[..> 8) - state.cpu.l = UInt8(v & 0xff) - } - - else if property == cpu_c_carry { state.cpu.carryFlag = parseBool(value) } - else if property == cpu_h_halfcarry { state.cpu.halfCarryFlag = parseBool(value) } - else if property == cpu_n_substract { state.cpu.subtractFlag = parseBool(value) } - else if property == cpu_z_zero { state.cpu.zeroFlag = parseBool(value) } - - else if property == cpu_SP { state.cpu.sp = parseUInt16(value) } - else if property == cpu_PC { state.cpu.pc = parseUInt16(value) } - - else if property == total_ticks { } - else if property == instruction_ticks { } - - else if property == cpu_interrupt_master_enable { state.cpu.ime = parseBool(value) } - else if property == cpu_halted { state.cpu.isHalted = parseBool(value) } - else if property == cpu_stopped { state.cpu.isStopped = parseBool(value) } - - else if property == memory { + switch property { + case cpu_A: + state.cpu.a = parseUInt8(value) + case cpu_B: + state.cpu.b = parseUInt8(value) + case cpu_C: + state.cpu.c = parseUInt8(value) + case cpu_D: + state.cpu.d = parseUInt8(value) + case cpu_E: + state.cpu.e = parseUInt8(value) + case cpu_HL: + let u16 = parseUInt16(value) + state.cpu.h = UInt8((u16 & 0xff00) >> 8) + state.cpu.l = UInt8(u16 & 0xff) + + case cpu_c_carry: + state.cpu.carryFlag = parseBool(value) + case cpu_h_halfcarry: + state.cpu.halfCarryFlag = parseBool(value) + case cpu_n_substract: + state.cpu.subtractFlag = parseBool(value) + case cpu_z_zero: + state.cpu.zeroFlag = parseBool(value) + + case cpu_SP: + state.cpu.sp = parseUInt16(value) + case cpu_PC: + state.cpu.pc = parseUInt16(value) + + case total_ticks, + instruction_ticks: + break + + case cpu_interrupt_master_enable: + state.cpu.ime = parseBool(value) + case cpu_halted: + state.cpu.isHalted = parseBool(value) + case cpu_stopped: + state.cpu.isStopped = parseBool(value) + + case memory: fillMemory(state: &state, from: 0x0000, to: 0xffff, with: value) - assert(state.memory.count == 0x10000) - } + assert(state.memory.count == 0x1_0000) - else { + default: // swiftlint:disable:next force_unwrapping let propertyString = String(bytes: property, encoding: .ascii)! print("Invalid line: \(propertyString)...") diff --git a/Tests/GameBoyKitROMTests/Dump files/runDump.swift b/Tests/GameBoyKitROMTests/Dump files/runDump.swift index 31ba210..1e925b8 100644 --- a/Tests/GameBoyKitROMTests/Dump files/runDump.swift +++ b/Tests/GameBoyKitROMTests/Dump files/runDump.swift @@ -13,7 +13,8 @@ enum RunDumpErrorHandling { func runDump(gameBoy: GameBoy, dumpDirUrl: URL, mode: Debugger.Mode = .opcodes, - errorHandling: RunDumpErrorHandling = .fatal) { + errorHandling: RunDumpErrorHandling = .fatal) +{ let debugger = Debugger(gameBoy: gameBoy) let dumpFiles = openDumpDir(directory: dumpDirUrl) diff --git a/Tests/GameBoyKitROMTests/TetrisTest.swift b/Tests/GameBoyKitROMTests/TetrisTest.swift index 3092914..7b80ab3 100644 --- a/Tests/GameBoyKitROMTests/TetrisTest.swift +++ b/Tests/GameBoyKitROMTests/TetrisTest.swift @@ -31,7 +31,7 @@ enum TetrisTest { do { let data = try Data(contentsOf: romUrl) return try CartridgeFactory.create(data: data) - } catch { + } catch { fatalError("Unable to open: '\(romUrl)'") } } diff --git a/Tests/GameBoyKitROMTests/main.swift b/Tests/GameBoyKitROMTests/main.swift index df6ec55..58925a1 100644 --- a/Tests/GameBoyKitROMTests/main.swift +++ b/Tests/GameBoyKitROMTests/main.swift @@ -13,7 +13,7 @@ import GameBoyKit // check each case. let useDumps = false -//BootromTests.run(compareWithDumps: useDumps) +// BootromTests.run(compareWithDumps: useDumps) BlarggTests.cpuInstrs01(compareWithDumps: useDumps) BlarggTests.cpuInstrs02(compareWithDumps: useDumps) @@ -28,7 +28,7 @@ BlarggTests.cpuInstrs10(compareWithDumps: useDumps) BlarggTests.cpuInstrs11(compareWithDumps: useDumps) BlarggTests.instrTiming(compareWithDumps: useDumps) -//TetrisTest.run(compareWithDumps: ()) +// TetrisTest.run(compareWithDumps: ()) // Show single frame performance -//performanceTest() +// performanceTest() diff --git a/Tests/GameBoyKitROMTests/performanceTest.swift b/Tests/GameBoyKitROMTests/performanceTest.swift index 7cfe322..173a072 100644 --- a/Tests/GameBoyKitROMTests/performanceTest.swift +++ b/Tests/GameBoyKitROMTests/performanceTest.swift @@ -41,7 +41,7 @@ private func openRom() -> Cartridge { do { let data = try Data(contentsOf: romUrl) return try CartridgeFactory.create(data: data) - } catch { + } catch { fatalError("Unable to open: '\(romUrl)'") } } diff --git a/Tests/GameBoyKitTests/Cartridge/CartridgeHeaderTests.swift b/Tests/GameBoyKitTests/Cartridge/CartridgeHeaderTests.swift index 117c6b5..200274b 100644 --- a/Tests/GameBoyKitTests/Cartridge/CartridgeHeaderTests.swift +++ b/Tests/GameBoyKitTests/Cartridge/CartridgeHeaderTests.swift @@ -14,7 +14,7 @@ class CartridgeHeaderTests: CartridgeTestCase { _ = try CartridgeHeader(rom: rom, skipChecks: false) XCTFail("It should throw") - } catch { } + } catch {} } func test_tetris() throws { diff --git a/Tests/GameBoyKitTests/Cartridge/Common/CartridgeTestCase.swift b/Tests/GameBoyKitTests/Cartridge/Common/CartridgeTestCase.swift index 9c71abc..5ebfab6 100644 --- a/Tests/GameBoyKitTests/Cartridge/Common/CartridgeTestCase.swift +++ b/Tests/GameBoyKitTests/Cartridge/Common/CartridgeTestCase.swift @@ -28,7 +28,8 @@ class CartridgeTestCase: XCTestCase { destinationCode: DestinationCode, romSize: CartridgeRomSize, ramSize: CartridgeRamSize, - checksum: UInt8) -> Data { + checksum: UInt8) -> Data + { var data = Data(count: romSize.byteCount) // Taken from Tetris (means that title will be at 'CartridgeMap.newTitle') diff --git a/Tests/GameBoyKitTests/Cartridge/MBC1Tests.swift b/Tests/GameBoyKitTests/Cartridge/MBC1Tests.swift index d7a64de..098ad44 100644 --- a/Tests/GameBoyKitTests/Cartridge/MBC1Tests.swift +++ b/Tests/GameBoyKitTests/Cartridge/MBC1Tests.swift @@ -202,7 +202,8 @@ class MBC1Tests: CartridgeTestCase { private func createCartridge(romSize: CartridgeRomSize, ramSize: CartridgeRamSize, - romData: [UInt16:UInt8] = [:]) throws -> MBC1 { + romData: [UInt16: UInt8] = [:]) throws -> MBC1 + { let type: CartridgeType = ramSize.byteCount == 0 ? .mbc1 : .mbc1Ram var data = self.createRom(title: "ROM", diff --git a/Tests/GameBoyKitTests/Cartridge/MBC3Tests.swift b/Tests/GameBoyKitTests/Cartridge/MBC3Tests.swift index 7411970..b4ed12e 100644 --- a/Tests/GameBoyKitTests/Cartridge/MBC3Tests.swift +++ b/Tests/GameBoyKitTests/Cartridge/MBC3Tests.swift @@ -193,7 +193,8 @@ class MBC3Tests: CartridgeTestCase { private func createCartridge(romSize: CartridgeRomSize, ramSize: CartridgeRamSize, - romData: [UInt16:UInt8] = [:]) throws -> MBC3 { + romData: [UInt16: UInt8] = [:]) throws -> MBC3 + { let type: CartridgeType = ramSize.byteCount == 0 ? .mbc3 : .mbc3Ram var data = self.createRom(title: "ROM", diff --git a/Tests/GameBoyKitTests/Cartridge/NoMBCTests.swift b/Tests/GameBoyKitTests/Cartridge/NoMBCTests.swift index 1780489..1be04a7 100644 --- a/Tests/GameBoyKitTests/Cartridge/NoMBCTests.swift +++ b/Tests/GameBoyKitTests/Cartridge/NoMBCTests.swift @@ -92,7 +92,8 @@ class NoMBCTests: CartridgeTestCase { private func createCartridge(romSize: CartridgeRomSize, ramSize: CartridgeRamSize, - romData: [UInt16:UInt8] = [:]) throws -> NoMBC { + romData: [UInt16: UInt8] = [:]) throws -> NoMBC + { let type: CartridgeType = ramSize.byteCount == 0 ? .noMBC : .noMBCRam var data = self.createRom(title: "ROM", diff --git a/Tests/GameBoyKitTests/Cpu/Common/CpuTestCase.swift b/Tests/GameBoyKitTests/Cpu/Common/CpuTestCase.swift index 001c4a2..6e19d36 100644 --- a/Tests/GameBoyKitTests/Cpu/Common/CpuTestCase.swift +++ b/Tests/GameBoyKitTests/Cpu/Common/CpuTestCase.swift @@ -12,7 +12,8 @@ class CpuTestCase: XCTestCase { } func createCpu(memory: CpuMemory? = nil, - interrupts: Interrupts? = nil) -> Cpu { + interrupts: Interrupts? = nil) -> Cpu + { return Cpu( memory: memory ?? FakeCpuMemory(), interrupts: interrupts ?? Interrupts() diff --git a/Tests/GameBoyKitTests/Cpu/CpuLdTests.swift b/Tests/GameBoyKitTests/Cpu/CpuLdTests.swift index 2d8adcb..4c87c86 100644 --- a/Tests/GameBoyKitTests/Cpu/CpuLdTests.swift +++ b/Tests/GameBoyKitTests/Cpu/CpuLdTests.swift @@ -240,7 +240,7 @@ class CpuLdTests: CpuTestCase { let cpu = self.createCpu(memory: memory) _ = cpu.ld_rr_d16(.hl, 0x3a5b) - XCTAssertEqual(cpu.registers.hl, 0x3A5B) + XCTAssertEqual(cpu.registers.hl, 0x3a5b) } /// When HL = FFFFh diff --git a/Tests/GameBoyKitTests/Cpu/CpuOpcodeLengthTests.swift b/Tests/GameBoyKitTests/Cpu/CpuOpcodeLengthTests.swift index d18a3ec..82b442d 100644 --- a/Tests/GameBoyKitTests/Cpu/CpuOpcodeLengthTests.swift +++ b/Tests/GameBoyKitTests/Cpu/CpuOpcodeLengthTests.swift @@ -8,22 +8,22 @@ import XCTest // This table comes from Blargg tests private let unprefixedLengths: [UInt16] = [ - 1,3,1,1,1,1,2,1,3,1,1,1,1,1,2,1, - 0,3,1,1,1,1,2,1,2,1,1,1,1,1,2,1, - 2,3,1,1,1,1,2,1,2,1,1,1,1,1,2,1, - 2,3,1,1,1,1,2,1,2,1,1,1,1,1,2,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,3,3,3,1,2,1,1,1,3,0,3,3,2,1, - 1,1,3,0,3,1,2,1,1,1,3,0,3,0,2,1, - 2,1,1,0,0,1,2,1,2,1,3,0,0,0,2,1, - 2,1,1,1,0,1,2,1,2,1,3,1,0,0,2,1 + 1, 3, 1, 1, 1, 1, 2, 1, 3, 1, 1, 1, 1, 1, 2, 1, + 0, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, + 2, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, + 2, 3, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 3, 3, 3, 1, 2, 1, 1, 1, 3, 0, 3, 3, 2, 1, + 1, 1, 3, 0, 3, 1, 2, 1, 1, 1, 3, 0, 3, 0, 2, 1, + 2, 1, 1, 0, 0, 1, 2, 1, 2, 1, 3, 0, 0, 0, 2, 1, + 2, 1, 1, 1, 0, 1, 2, 1, 2, 1, 3, 1, 0, 0, 2, 1 ] class CpuOpcodeLengthTests: CpuTestCase { @@ -46,7 +46,7 @@ class CpuOpcodeLengthTests: CpuTestCase { 0x20, 0x28, 0x30, 0x38, // jr_cc: nz, z, nc, c 0xc0, 0xc8, 0xd0, 0xd8, // ret_cc: .nz, .z, .nc, .c 0xc2, 0xca, 0xd2, 0xda, // jp_cc_nn: .nz, .z, .nc, .c - 0xc4, 0xcc, 0xd4, 0xdc // call_cc_a16: .nz, .z, .nc, .c + 0xc4, 0xcc, 0xd4, 0xdc // call_cc_a16: .nz, .z, .nc, .c ]) for (i, length) in unprefixedLengths.enumerated() { @@ -71,7 +71,7 @@ class CpuOpcodeLengthTests: CpuTestCase { // jr_cc, ret_cc, jp_cc_nn, call_cc_a16 let opcodes: [UInt8] = [ 0x28, 0xc8, 0xca, 0xcc, // z - 0x38, 0xd8, 0xda, 0xdc // c + 0x38, 0xd8, 0xda, 0xdc // c ] for opcode in opcodes { @@ -93,7 +93,7 @@ class CpuOpcodeLengthTests: CpuTestCase { // jr_cc, ret_cc, jp_cc_nn, call_cc_a16 let opcodes: [UInt8] = [ 0x20, 0xc0, 0xc2, 0xc4, // nz - 0x30, 0xd0, 0xd2, 0xd4 // nc + 0x30, 0xd0, 0xd2, 0xd4 // nc ] for opcode in opcodes { diff --git a/Tests/GameBoyKitTests/Cpu/CpuOpcodeTimingTests.swift b/Tests/GameBoyKitTests/Cpu/CpuOpcodeTimingTests.swift index 0a992d8..60a513e 100644 --- a/Tests/GameBoyKitTests/Cpu/CpuOpcodeTimingTests.swift +++ b/Tests/GameBoyKitTests/Cpu/CpuOpcodeTimingTests.swift @@ -8,41 +8,41 @@ import XCTest // Those tables come from Blargg tests private let unprefixedTimings = [ - 1,3,2,2,1,1,2,1,5,2,2,2,1,1,2,1, - 0,3,2,2,1,1,2,1,3,2,2,2,1,1,2,1, - 2,3,2,2,1,1,2,1,2,2,2,2,1,1,2,1, - 2,3,2,2,3,3,3,1,2,2,2,2,1,1,2,1, - 1,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1, - 1,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1, - 1,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1, - 2,2,2,2,2,2,0,2,1,1,1,1,1,1,2,1, - 1,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1, - 1,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1, - 1,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1, - 1,1,1,1,1,1,2,1,1,1,1,1,1,1,2,1, - 2,3,3,4,3,4,2,4,2,4,3,0,3,6,2,4, - 2,3,3,0,3,4,2,4,2,4,3,0,3,0,2,4, - 3,3,2,0,0,4,2,4,4,1,4,0,0,0,2,4, - 3,3,2,1,0,4,2,4,3,2,4,1,0,0,2,4 + 1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1, + 0, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, + 2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, + 2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, + 2, 2, 2, 2, 2, 2, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, + 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, + 2, 3, 3, 4, 3, 4, 2, 4, 2, 4, 3, 0, 3, 6, 2, 4, + 2, 3, 3, 0, 3, 4, 2, 4, 2, 4, 3, 0, 3, 0, 2, 4, + 3, 3, 2, 0, 0, 4, 2, 4, 4, 1, 4, 0, 0, 0, 2, 4, + 3, 3, 2, 1, 0, 4, 2, 4, 3, 2, 4, 1, 0, 0, 2, 4 ] private let prefixedTimings = [ - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,3,2,2,2,2,2,2,2,3,2, - 2,2,2,2,2,2,3,2,2,2,2,2,2,2,3,2, - 2,2,2,2,2,2,3,2,2,2,2,2,2,2,3,2, - 2,2,2,2,2,2,3,2,2,2,2,2,2,2,3,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2, - 2,2,2,2,2,2,4,2,2,2,2,2,2,2,4,2 + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, + 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, + 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2 ] class CpuOpcodeTimingTests: CpuTestCase { @@ -59,7 +59,7 @@ class CpuOpcodeTimingTests: CpuTestCase { 0x20, 0x28, 0x30, 0x38, // jr_cc: .nz, .z, .nc, .c 0xc0, 0xc8, 0xd0, 0xd8, // ret_cc: .nz, .z, .nc, .c 0xc2, 0xca, 0xd2, 0xda, // jp_cc_nn: .nz, .z, .nc, .c - 0xc4, 0xcc, 0xd4, 0xdc // call_cc_a16: .nz, .z, .nc, .c + 0xc4, 0xcc, 0xd4, 0xdc // call_cc_a16: .nz, .z, .nc, .c ]) for i in 0.. UInt8 { return self.videoRam[address] ?? 0 diff --git a/Tests/GameBoyKitTests/Memory/Common/FakeTimerMemory.swift b/Tests/GameBoyKitTests/Memory/Common/FakeTimerMemory.swift index af7baab..dd9fb99 100644 --- a/Tests/GameBoyKitTests/Memory/Common/FakeTimerMemory.swift +++ b/Tests/GameBoyKitTests/Memory/Common/FakeTimerMemory.swift @@ -5,8 +5,8 @@ @testable import GameBoyKit class FakeTimerMemory: TimerMemory { - var div = UInt8() + var div = UInt8() var tima = UInt8() - var tma = UInt8() - var tac = UInt8() + var tma = UInt8() + var tac = UInt8() } diff --git a/Tests/GameBoyKitTests/Memory/Common/MemoryTestCase.swift b/Tests/GameBoyKitTests/Memory/Common/MemoryTestCase.swift index dd8fc1a..a1da4a7 100644 --- a/Tests/GameBoyKitTests/Memory/Common/MemoryTestCase.swift +++ b/Tests/GameBoyKitTests/Memory/Common/MemoryTestCase.swift @@ -5,6 +5,8 @@ import XCTest @testable import GameBoyKit +// swiftformat:disable consecutiveSpaces + class MemoryTestCase: XCTestCase { func createMemory(bootrom: BootromMemory? = nil, @@ -13,7 +15,8 @@ class MemoryTestCase: XCTestCase { lcd: LcdMemory? = nil, audio: AudioMemory? = nil, timer: TimerMemory? = nil, - interrupts: Interrupts? = nil) -> Memory { + interrupts: Interrupts? = nil) -> Memory + { return Memory( bootrom: bootrom ?? FakeBootromMemory(), cartridge: cartridge ?? FakeCartridgeMemory(), diff --git a/Tests/GameBoyKitTests/Memory/DmaTests.swift b/Tests/GameBoyKitTests/Memory/DmaTests.swift index 62ffd4d..2deaaae 100644 --- a/Tests/GameBoyKitTests/Memory/DmaTests.swift +++ b/Tests/GameBoyKitTests/Memory/DmaTests.swift @@ -12,7 +12,7 @@ class DmaTests: MemoryTestCase { // Fill source (somewhere in external ram) let sourceStart: UInt16 = 0xab00 - let sourceEnd: UInt16 = 0xab9f + let sourceEnd: UInt16 = 0xab9f for address in sourceStart...sourceEnd { let value = UInt8(address & 0x00ff) diff --git a/Tests/GameBoyKitTests/Memory/MemoryReadTests.swift b/Tests/GameBoyKitTests/Memory/MemoryReadTests.swift index d2bbf9f..46b6f70 100644 --- a/Tests/GameBoyKitTests/Memory/MemoryReadTests.swift +++ b/Tests/GameBoyKitTests/Memory/MemoryReadTests.swift @@ -17,7 +17,7 @@ class MemoryReadTests: MemoryTestCase { let bootrom = FakeBootromMemory() bootrom.data[0] = startValue - bootrom.data[range.count - 1] = endValue + bootrom.data[range.count - 1] = endValue let memory = self.createMemory(bootrom: bootrom) // 'memory.hasFinishedBootrom' should be false by default @@ -31,7 +31,7 @@ class MemoryReadTests: MemoryTestCase { let cartridge = FakeCartridgeMemory() cartridge.rom[0] = startValue - cartridge.rom[range.count - 1] = endValue + cartridge.rom[range.count - 1] = endValue let memory = self.createMemory(cartridge: cartridge) memory.isRunningBootrom = false @@ -61,7 +61,7 @@ class MemoryReadTests: MemoryTestCase { let range = MemoryMap.videoRam lcd.videoRam[range.start] = startValue - lcd.videoRam[range.end] = endValue + lcd.videoRam[range.end] = endValue XCTAssertEqual(memory.read(range.start), startValue) XCTAssertEqual(memory.read(range.end), endValue) @@ -73,7 +73,7 @@ class MemoryReadTests: MemoryTestCase { let range = MemoryMap.externalRam cartridge.ram[0] = startValue - cartridge.ram[range.count - 1] = endValue + cartridge.ram[range.count - 1] = endValue XCTAssertEqual(memory.read(range.start), startValue) XCTAssertEqual(memory.read(range.end), endValue) @@ -84,7 +84,7 @@ class MemoryReadTests: MemoryTestCase { let range = MemoryMap.internalRam memory.ram[0] = startValue - memory.ram[range.count - 1] = endValue + memory.ram[range.count - 1] = endValue XCTAssertEqual(memory.read(range.start), startValue) XCTAssertEqual(memory.read(range.end), endValue) @@ -107,7 +107,7 @@ class MemoryReadTests: MemoryTestCase { let range = MemoryMap.oam lcd.oam[range.start] = startValue - lcd.oam[range.end] = endValue + lcd.oam[range.end] = endValue XCTAssertEqual(memory.read(range.start), startValue) XCTAssertEqual(memory.read(range.end), endValue) @@ -207,7 +207,7 @@ class MemoryReadTests: MemoryTestCase { let range = MemoryMap.highRam memory.highRam[0] = startValue - memory.highRam[range.count - 1] = endValue + memory.highRam[range.count - 1] = endValue XCTAssertEqual(memory.read(range.start), startValue) XCTAssertEqual(memory.read(range.end), endValue) diff --git a/Tests/GameBoyKitTests/Memory/MemoryWriteTests.swift b/Tests/GameBoyKitTests/Memory/MemoryWriteTests.swift index feeaaec..a0cb8f6 100644 --- a/Tests/GameBoyKitTests/Memory/MemoryWriteTests.swift +++ b/Tests/GameBoyKitTests/Memory/MemoryWriteTests.swift @@ -96,10 +96,10 @@ class MemoryWriteTests: MemoryTestCase { let memory = self.createMemory() let range = MemoryMap.internalRamEcho - memory.write(range.start, value: startValue) - memory.write(range.end, value: endValue) + memory.write(range.start, value: startValue) + memory.write(range.end, value: endValue) - XCTAssertEqual(memory.ram[0], startValue) + XCTAssertEqual(memory.ram[0], startValue) XCTAssertEqual(memory.ram[range.count - 1], endValue) } diff --git a/Tests/GameBoyKitTests/Memory/UnmapBootromTests.swift b/Tests/GameBoyKitTests/Memory/UnmapBootromTests.swift index 06f7234..4938ef1 100644 --- a/Tests/GameBoyKitTests/Memory/UnmapBootromTests.swift +++ b/Tests/GameBoyKitTests/Memory/UnmapBootromTests.swift @@ -18,7 +18,7 @@ class UnmapBootromTests: MemoryTestCase { let cartridge = FakeCartridgeMemory() cartridge.rom[range.start] = startValue - cartridge.rom[range.end] = endValue + cartridge.rom[range.end] = endValue let memory = self.createMemory(cartridge: cartridge) memory.write(MemoryMap.IO.unmapBootrom, value: 1) // <-- this