got rom to load

This commit is contained in:
Andrew Glaze 2024-07-21 21:43:37 -04:00
parent 915bff96a1
commit af72573d13
5 changed files with 34 additions and 30 deletions

2
.gitignore vendored
View File

@ -6,3 +6,5 @@ DerivedData/
.swiftpm/configuration/registries.json .swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc .netrc
*.nes

View File

@ -1,9 +1,17 @@
class Bus { class Bus {
var cpuVram: [UInt8] = .init(repeating: 0, count: 2048) var cpuVram: [UInt8] = .init(repeating: 0, count: 2048)
var rom: Rom
fileprivate let RAM : UInt16 = 0x0000 fileprivate let RAM : UInt16 = 0x0000
fileprivate let RAM_MIRRORS_END: UInt16 = 0x1FFF fileprivate let RAM_MIRRORS_END: UInt16 = 0x1FFF
fileprivate let PPU_REGISTERS: UInt16 = 0x2000 fileprivate let PPU_REGISTERS: UInt16 = 0x2000
fileprivate let PPU_REGISTERS_MIRRORS_END: UInt16 = 0x3FFF 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: case PPU_REGISTERS...PPU_REGISTERS_MIRRORS_END:
let mirrorDownAddr = addr & 0b00100000_00000111; let mirrorDownAddr = addr & 0b00100000_00000111;
fatalError("PPU not implemented yet") fatalError("PPU not implemented yet")
case ROM_ADDRESS_START...ROM_ADDRESS_END:
return readProgramRom(addr)
default: default:
print("Ignoring mem access at \(addr)") print("Ignoring mem access at \(addr)")
return 0 return 0
@ -34,4 +44,13 @@ extension Bus: Memory {
print("Ignorming mem-write at \(addr)") 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)]
}
} }

View File

@ -37,7 +37,7 @@ class CPU {
var stackPointer: UInt8 = STACK_RESET var stackPointer: UInt8 = STACK_RESET
var status: CPUFlags = [.interruptDisable, .break2] var status: CPUFlags = [.interruptDisable, .break2]
var programCounter: UInt16 = 0 var programCounter: UInt16 = 0
var bus = Bus() var bus: Bus?
func getOpperandAddress(_ mode: AddressingMode) -> UInt16 { func getOpperandAddress(_ mode: AddressingMode) -> UInt16 {
@ -99,6 +99,7 @@ class CPU {
func load(_ program: [UInt8]) { func load(_ program: [UInt8]) {
//memory[0x0600 ..< (0x0600 + program.count)] = program[0..<program.count] //memory[0x0600 ..< (0x0600 + program.count)] = program[0..<program.count]
bus = Bus(try! Rom(program))
memWriteU16(0xFFFC, data: 0x0600) memWriteU16(0xFFFC, data: 0x0600)
} }
@ -390,19 +391,19 @@ class CPU {
extension CPU: Memory { extension CPU: Memory {
func memRead(_ addr: UInt16) -> UInt8 { func memRead(_ addr: UInt16) -> UInt8 {
return bus.memRead(addr) return bus!.memRead(addr)
} }
func memWrite(_ addr: UInt16, data: UInt8) { func memWrite(_ addr: UInt16, data: UInt8) {
bus.memWrite(addr, data: data) bus!.memWrite(addr, data: data)
} }
func memReadU16(_ addr: UInt16) -> UInt16 { func memReadU16(_ addr: UInt16) -> UInt16 {
return bus.memReadU16(addr) return bus!.memReadU16(addr)
} }
func memWriteU16(_ addr: UInt16, data: UInt16) { func memWriteU16(_ addr: UInt16, data: UInt16) {
bus.memWriteU16(addr, data: data) bus!.memWriteU16(addr, data: data)
} }
} }

View File

@ -8,7 +8,7 @@ struct Rom {
var screenMirror: Mirroring var screenMirror: Mirroring
init(_ raw: [UInt8]) throws { 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) mapper = (raw[7] & 0b1111_0000) | (raw[6] >> 4)
let inesVer = (raw[7] >> 2) & 0b11 let inesVer = (raw[7] >> 2) & 0b11
@ -33,8 +33,8 @@ struct Rom {
let programStart = 16 + (skipTrainer ? 512 : 0) let programStart = 16 + (skipTrainer ? 512 : 0)
let characterStart = programStart + programSize let characterStart = programStart + programSize
program = Array(raw[programStart...(programStart + programSize)]) program = Array(raw[programStart..<(programStart + programSize)])
character = Array(raw[characterStart...(characterStart + characterSize)]) character = Array(raw[characterStart..<(characterStart + characterSize)])
} }
} }

View File

@ -94,28 +94,10 @@ func readScreenState(_ cpu: CPU, frame: inout [UInt8]) -> Bool {
return update return update
} }
let gameCode: [UInt8] = [ guard let rom = NSData(contentsOfFile: "snake.nes") else { fatalError("Rom not found") }
0x20, 0x06, 0x06, 0x20, 0x38, 0x06, 0x20, 0x0d, 0x06, 0x20, 0x2a, 0x06, 0x60, 0xa9, 0x02, 0x85, var gameCode = [UInt8](repeating: 0, count: rom.length)
0x02, 0xa9, 0x04, 0x85, 0x03, 0xa9, 0x11, 0x85, 0x10, 0xa9, 0x10, 0x85, 0x12, 0xa9, 0x0f, 0x85, rom.getBytes(&gameCode, length: rom.length)
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
]
var cpu = CPU() var cpu = CPU()
cpu.load(gameCode) cpu.load(gameCode)