2024-05-10 22:06:45 -05:00
|
|
|
// The Swift Programming Language
|
|
|
|
// https://docs.swift.org/swift-book
|
|
|
|
|
2024-05-11 22:30:17 -05:00
|
|
|
import SDL
|
2024-05-17 13:15:10 -05:00
|
|
|
import Foundation
|
2024-05-11 22:30:17 -05:00
|
|
|
|
|
|
|
guard SDL_Init(SDL_INIT_VIDEO) == 0 else {
|
|
|
|
fatalError("SDL could not initialize! SDL_Error: \(String(cString: SDL_GetError()))")
|
|
|
|
}
|
|
|
|
|
|
|
|
let window = SDL_CreateWindow(
|
2024-08-20 11:07:36 -05:00
|
|
|
"SwiftNES",
|
2024-05-11 22:30:17 -05:00
|
|
|
Int32(SDL_WINDOWPOS_CENTERED_MASK), Int32(SDL_WINDOWPOS_CENTERED_MASK),
|
2024-08-20 11:07:36 -05:00
|
|
|
Int32(256.0 * 3.0), Int32(240.0 * 3.0),
|
2024-05-11 22:30:17 -05:00
|
|
|
SDL_WINDOW_SHOWN.rawValue)
|
|
|
|
|
|
|
|
let canvas = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC.rawValue)
|
|
|
|
|
2024-08-20 11:07:36 -05:00
|
|
|
SDL_RenderSetScale(canvas, 3.0, 3.0)
|
2024-05-11 22:30:17 -05:00
|
|
|
|
2024-08-20 11:07:36 -05:00
|
|
|
var texture = SDL_CreateTexture(canvas, SDL_PIXELFORMAT_RGB24.rawValue, Int32(SDL_TEXTUREACCESS_TARGET.rawValue), 256, 240)
|
2024-05-17 13:15:10 -05:00
|
|
|
|
|
|
|
var event = SDL_Event()
|
|
|
|
|
2024-08-21 17:35:32 -05:00
|
|
|
guard let bytes = NSData(contentsOfFile: "pacman.nes") else { fatalError("Rom not found") }
|
2024-08-20 11:07:36 -05:00
|
|
|
var gameCode = [UInt8](repeating: 0, count: bytes.length)
|
|
|
|
bytes.getBytes(&gameCode, length: bytes.length)
|
|
|
|
|
|
|
|
let rom = try Rom(gameCode)
|
2024-08-21 17:35:32 -05:00
|
|
|
let joypad1 = Joypad()
|
|
|
|
|
|
|
|
let keyMap = [
|
|
|
|
SDLK_DOWN : JoypadButton.DOWN,
|
|
|
|
SDLK_UP : JoypadButton.UP,
|
|
|
|
SDLK_LEFT : JoypadButton.LEFT,
|
|
|
|
SDLK_RIGHT : JoypadButton.RIGHT,
|
|
|
|
SDLK_SPACE : JoypadButton.SELECT,
|
|
|
|
SDLK_RETURN : JoypadButton.START,
|
|
|
|
SDLK_a : JoypadButton.BUTTON_A,
|
|
|
|
SDLK_s : JoypadButton.BUTTON_B
|
|
|
|
]
|
2024-08-20 11:07:36 -05:00
|
|
|
|
2024-08-20 16:21:52 -05:00
|
|
|
var frame = Frame()
|
2024-08-21 17:35:32 -05:00
|
|
|
let bus = Bus(rom: rom, joypad1: joypad1) { ppu in
|
2024-08-20 16:21:52 -05:00
|
|
|
Render.render(ppu, frame: frame)
|
|
|
|
SDL_UpdateTexture(texture, nil, frame.data, 256 * 3)
|
|
|
|
SDL_RenderCopy(canvas, texture, nil, nil)
|
|
|
|
SDL_RenderPresent(canvas)
|
2024-08-20 11:07:36 -05:00
|
|
|
|
2024-05-17 13:15:10 -05:00
|
|
|
while SDL_PollEvent(&event) > 0 {
|
|
|
|
if event.type == SDL_QUIT.rawValue {
|
2024-08-20 11:07:36 -05:00
|
|
|
SDL_DestroyWindow(window)
|
|
|
|
SDL_Quit()
|
|
|
|
exit(0)
|
|
|
|
}
|
2024-05-17 13:15:10 -05:00
|
|
|
if event.type == SDL_KEYDOWN.rawValue {
|
2024-08-21 17:35:32 -05:00
|
|
|
let keyCode = SDL_KeyCode(UInt32(event.key.keysym.sym))
|
|
|
|
switch keyCode {
|
2024-05-17 13:15:10 -05:00
|
|
|
case SDLK_ESCAPE:
|
|
|
|
SDL_DestroyWindow(window)
|
|
|
|
SDL_Quit()
|
|
|
|
exit(0)
|
|
|
|
default:
|
2024-08-21 17:35:32 -05:00
|
|
|
guard let key = keyMap[keyCode] else { continue }
|
|
|
|
joypad1.setButton(key, pressed: true)
|
2024-05-17 13:15:10 -05:00
|
|
|
}
|
|
|
|
}
|
2024-08-21 17:35:32 -05:00
|
|
|
if event.type == SDL_KEYUP.rawValue {
|
|
|
|
guard let key = keyMap[SDL_KeyCode(UInt32(event.key.keysym.sym))] else { continue }
|
|
|
|
joypad1.setButton(key, pressed: false)
|
|
|
|
}
|
2024-05-17 13:15:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 16:21:52 -05:00
|
|
|
let cpu = CPU(bus: bus)
|
|
|
|
cpu.reset()
|
|
|
|
cpu.run()
|