From af72573d13e0f19ca9ba5aabcacef056c67e61f3 Mon Sep 17 00:00:00 2001 From: Andrew Glaze Date: Sun, 21 Jul 2024 21:43:37 -0400 Subject: [PATCH] got rom to load --- .gitignore | 2 ++ Sources/Bus.swift | 19 +++++++++++++++++++ Sources/CPU.swift | 11 ++++++----- Sources/Rom.swift | 6 +++--- Sources/main.swift | 26 ++++---------------------- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 0023a53..2f5d282 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ DerivedData/ .swiftpm/configuration/registries.json .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .netrc + +*.nes diff --git a/Sources/Bus.swift b/Sources/Bus.swift index 6e97d7b..14e132c 100644 --- a/Sources/Bus.swift +++ b/Sources/Bus.swift @@ -1,9 +1,17 @@ class Bus { var cpuVram: [UInt8] = .init(repeating: 0, count: 2048) + var rom: Rom + fileprivate let RAM : UInt16 = 0x0000 fileprivate let RAM_MIRRORS_END: UInt16 = 0x1FFF fileprivate let PPU_REGISTERS: UInt16 = 0x2000 fileprivate let PPU_REGISTERS_MIRRORS_END: UInt16 = 0x3FFF + fileprivate let ROM_ADDRESS_START: UInt16 = 0x8000 + fileprivate let ROM_ADDRESS_END: UInt16 = 0xFFFF + + init(_ rom: Rom) { + self.rom = rom + } } @@ -16,6 +24,8 @@ extension Bus: Memory { case PPU_REGISTERS...PPU_REGISTERS_MIRRORS_END: let mirrorDownAddr = addr & 0b00100000_00000111; fatalError("PPU not implemented yet") + case ROM_ADDRESS_START...ROM_ADDRESS_END: + return readProgramRom(addr) default: print("Ignoring mem access at \(addr)") return 0 @@ -34,4 +44,13 @@ extension Bus: Memory { print("Ignorming mem-write at \(addr)") } } + + func readProgramRom(_ addr: UInt16) -> UInt8 { + var addr = addr - 0x8000 + if rom.program.count == 0x4000 && addr >= 0x4000 { + // rom mirroring + addr = addr % 0x4000 + } + return rom.program[Int(addr)] + } } diff --git a/Sources/CPU.swift b/Sources/CPU.swift index 37fe472..b7c6e62 100644 --- a/Sources/CPU.swift +++ b/Sources/CPU.swift @@ -37,7 +37,7 @@ class CPU { var stackPointer: UInt8 = STACK_RESET var status: CPUFlags = [.interruptDisable, .break2] var programCounter: UInt16 = 0 - var bus = Bus() + var bus: Bus? func getOpperandAddress(_ mode: AddressingMode) -> UInt16 { @@ -99,6 +99,7 @@ class CPU { func load(_ program: [UInt8]) { //memory[0x0600 ..< (0x0600 + program.count)] = program[0.. UInt8 { - return bus.memRead(addr) + return bus!.memRead(addr) } func memWrite(_ addr: UInt16, data: UInt8) { - bus.memWrite(addr, data: data) + bus!.memWrite(addr, data: data) } func memReadU16(_ addr: UInt16) -> UInt16 { - return bus.memReadU16(addr) + return bus!.memReadU16(addr) } func memWriteU16(_ addr: UInt16, data: UInt16) { - bus.memWriteU16(addr, data: data) + bus!.memWriteU16(addr, data: data) } } diff --git a/Sources/Rom.swift b/Sources/Rom.swift index 93b68ce..e99dc09 100644 --- a/Sources/Rom.swift +++ b/Sources/Rom.swift @@ -8,7 +8,7 @@ struct Rom { var screenMirror: Mirroring init(_ raw: [UInt8]) throws { - guard raw[0...4] == [0x4E, 0x45, 0x53, 0x1A] else { throw HeaderParseError.notINES("File is not in iNES file format.") } + guard raw[0..<4] == [0x4E, 0x45, 0x53, 0x1A] else { throw HeaderParseError.notINES("File is not in iNES file format.") } mapper = (raw[7] & 0b1111_0000) | (raw[6] >> 4) let inesVer = (raw[7] >> 2) & 0b11 @@ -33,8 +33,8 @@ struct Rom { let programStart = 16 + (skipTrainer ? 512 : 0) let characterStart = programStart + programSize - program = Array(raw[programStart...(programStart + programSize)]) - character = Array(raw[characterStart...(characterStart + characterSize)]) + program = Array(raw[programStart..<(programStart + programSize)]) + character = Array(raw[characterStart..<(characterStart + characterSize)]) } } diff --git a/Sources/main.swift b/Sources/main.swift index a5b29f7..d899e0e 100644 --- a/Sources/main.swift +++ b/Sources/main.swift @@ -94,28 +94,10 @@ func readScreenState(_ cpu: CPU, frame: inout [UInt8]) -> Bool { return update } -let gameCode: [UInt8] = [ - 0x20, 0x06, 0x06, 0x20, 0x38, 0x06, 0x20, 0x0d, 0x06, 0x20, 0x2a, 0x06, 0x60, 0xa9, 0x02, 0x85, - 0x02, 0xa9, 0x04, 0x85, 0x03, 0xa9, 0x11, 0x85, 0x10, 0xa9, 0x10, 0x85, 0x12, 0xa9, 0x0f, 0x85, - 0x14, 0xa9, 0x04, 0x85, 0x11, 0x85, 0x13, 0x85, 0x15, 0x60, 0xa5, 0xfe, 0x85, 0x00, 0xa5, 0xfe, - 0x29, 0x03, 0x18, 0x69, 0x02, 0x85, 0x01, 0x60, 0x20, 0x4d, 0x06, 0x20, 0x8d, 0x06, 0x20, 0xc3, - 0x06, 0x20, 0x19, 0x07, 0x20, 0x20, 0x07, 0x20, 0x2d, 0x07, 0x4c, 0x38, 0x06, 0xa5, 0xff, 0xc9, - 0x77, 0xf0, 0x0d, 0xc9, 0x64, 0xf0, 0x14, 0xc9, 0x73, 0xf0, 0x1b, 0xc9, 0x61, 0xf0, 0x22, 0x60, - 0xa9, 0x04, 0x24, 0x02, 0xd0, 0x26, 0xa9, 0x01, 0x85, 0x02, 0x60, 0xa9, 0x08, 0x24, 0x02, 0xd0, - 0x1b, 0xa9, 0x02, 0x85, 0x02, 0x60, 0xa9, 0x01, 0x24, 0x02, 0xd0, 0x10, 0xa9, 0x04, 0x85, 0x02, - 0x60, 0xa9, 0x02, 0x24, 0x02, 0xd0, 0x05, 0xa9, 0x08, 0x85, 0x02, 0x60, 0x60, 0x20, 0x94, 0x06, - 0x20, 0xa8, 0x06, 0x60, 0xa5, 0x00, 0xc5, 0x10, 0xd0, 0x0d, 0xa5, 0x01, 0xc5, 0x11, 0xd0, 0x07, - 0xe6, 0x03, 0xe6, 0x03, 0x20, 0x2a, 0x06, 0x60, 0xa2, 0x02, 0xb5, 0x10, 0xc5, 0x10, 0xd0, 0x06, - 0xb5, 0x11, 0xc5, 0x11, 0xf0, 0x09, 0xe8, 0xe8, 0xe4, 0x03, 0xf0, 0x06, 0x4c, 0xaa, 0x06, 0x4c, - 0x35, 0x07, 0x60, 0xa6, 0x03, 0xca, 0x8a, 0xb5, 0x10, 0x95, 0x12, 0xca, 0x10, 0xf9, 0xa5, 0x02, - 0x4a, 0xb0, 0x09, 0x4a, 0xb0, 0x19, 0x4a, 0xb0, 0x1f, 0x4a, 0xb0, 0x2f, 0xa5, 0x10, 0x38, 0xe9, - 0x20, 0x85, 0x10, 0x90, 0x01, 0x60, 0xc6, 0x11, 0xa9, 0x01, 0xc5, 0x11, 0xf0, 0x28, 0x60, 0xe6, - 0x10, 0xa9, 0x1f, 0x24, 0x10, 0xf0, 0x1f, 0x60, 0xa5, 0x10, 0x18, 0x69, 0x20, 0x85, 0x10, 0xb0, - 0x01, 0x60, 0xe6, 0x11, 0xa9, 0x06, 0xc5, 0x11, 0xf0, 0x0c, 0x60, 0xc6, 0x10, 0xa5, 0x10, 0x29, - 0x1f, 0xc9, 0x1f, 0xf0, 0x01, 0x60, 0x4c, 0x35, 0x07, 0xa0, 0x00, 0xa5, 0xfe, 0x91, 0x00, 0x60, - 0xa6, 0x03, 0xa9, 0x00, 0x81, 0x10, 0xa2, 0x00, 0xa9, 0x01, 0x81, 0x10, 0x60, 0xa2, 0x00, 0xea, - 0xea, 0xca, 0xd0, 0xfb, 0x60 -] +guard let rom = NSData(contentsOfFile: "snake.nes") else { fatalError("Rom not found") } +var gameCode = [UInt8](repeating: 0, count: rom.length) +rom.getBytes(&gameCode, length: rom.length) + var cpu = CPU() cpu.load(gameCode)