rmg-001/src/emulator.rs

139 lines
4.5 KiB
Rust
Raw Normal View History

2021-10-14 01:50:48 +00:00
use std::{thread, time};
2021-11-01 02:02:09 +00:00
use winit_input_helper::WinitInputHelper;
use winit::event::{VirtualKeyCode};
2021-10-14 01:50:48 +00:00
2021-11-01 02:02:09 +00:00
use crate::cpu::{CPU, Cycles, Interrupt};
use crate::ppu::PPU;
use crate::bus::Bus;
2021-10-30 14:13:31 +00:00
use crate::timer::Timer;
2021-11-01 02:02:09 +00:00
use crate::joypad::{Joypad, Button, JOYPAD_ADDRESS};
2021-10-26 22:34:59 +00:00
pub struct Emulator {
cpu: CPU,
ppu: PPU,
bus: Bus,
timer: Timer,
2021-11-01 02:02:09 +00:00
joypad: Joypad,
}
2021-10-26 22:34:59 +00:00
impl Emulator {
pub fn new() -> Self {
2021-11-01 02:02:09 +00:00
let mut joypad: Joypad = Joypad::new();
Self {
cpu: CPU::new(),
ppu: PPU::new(),
bus: Bus::new(),
timer: Timer::new(),
2021-11-01 02:02:09 +00:00
joypad: Joypad::new(),
}
}
pub fn handle_input(&mut self, input: &WinitInputHelper) {
let mut change = false;
if input.key_pressed(VirtualKeyCode::K) {
change = true;
self.joypad.press(Button::A);
}
if input.key_pressed(VirtualKeyCode::J) {
change = true;
self.joypad.press(Button::B);
}
if input.key_pressed(VirtualKeyCode::W) {
change = true;
self.joypad.press(Button::Up);
}
if input.key_pressed(VirtualKeyCode::S) {
change = true;
self.joypad.press(Button::Down);
}
if input.key_pressed(VirtualKeyCode::A) {
change = true;
self.joypad.press(Button::Left);
}
if input.key_pressed(VirtualKeyCode::D) {
change = true;
self.joypad.press(Button::Right);
}
if input.key_pressed(VirtualKeyCode::N) {
change = true;
self.joypad.press(Button::Start);
}
if input.key_pressed(VirtualKeyCode::B) {
change = true;
self.joypad.press(Button::Select);
}
if input.key_released(VirtualKeyCode::K) {
change = true;
self.joypad.release(Button::A);
}
if input.key_released(VirtualKeyCode::J) {
change = true;
self.joypad.release(Button::B);
}
if input.key_released(VirtualKeyCode::W) {
change = true;
self.joypad.release(Button::Up);
}
if input.key_released(VirtualKeyCode::S) {
change = true;
self.joypad.release(Button::Down);
}
if input.key_released(VirtualKeyCode::A) {
change = true;
self.joypad.release(Button::Left);
}
if input.key_released(VirtualKeyCode::D) {
change = true;
self.joypad.release(Button::Right);
}
if input.key_released(VirtualKeyCode::N) {
change = true;
self.joypad.release(Button::Start);
}
if input.key_released(VirtualKeyCode::B) {
change = true;
self.joypad.release(Button::Select);
}
if change {
self.bus.force_write(JOYPAD_ADDRESS, self.joypad.get(&self.bus));
self.bus.set_interrupt_flag(Interrupt::Joypad, true);
}
}
2021-10-26 22:34:59 +00:00
pub fn draw(&mut self, frame: &mut [u8]) {
2021-10-29 00:38:30 +00:00
let ppu_frame = self.ppu.get_rgba_frame();
2021-10-26 22:34:59 +00:00
for (i, pixel) in frame.chunks_exact_mut(4).enumerate() {
pixel.copy_from_slice(&ppu_frame[i]);
}
}
pub fn run(&mut self, cpu_cycles: Cycles) {
self.cpu.reset_cycles();
while self.cpu.get_cycles().0 <= cpu_cycles.0 {
self.cpu.run(&mut self.bus);
2021-10-29 20:40:47 +00:00
self.ppu.do_cycles(&mut self.bus, self.cpu.get_last_op_cycles());
self.timer.do_cycles(&mut self.bus, self.cpu.get_last_op_cycles());
2021-10-26 22:34:59 +00:00
}
}
pub fn cpu_loop(&mut self) {
2021-10-14 01:50:48 +00:00
let mut exit = false;
while !exit {
self.cpu.run(&mut self.bus);
2021-10-20 02:55:35 +00:00
// exit = self.cpu.get_exec_calls_count() >= 1258895; // log 1
// exit = self.cpu.get_exec_calls_count() >= 1068422; // log 3
2021-10-20 03:36:10 +00:00
// exit = self.cpu.get_exec_calls_count() >= 1262766; // log 4
// exit = self.cpu.get_exec_calls_count() >= 1763388; // log 5
// exit = self.cpu.get_exec_calls_count() >= 1763388; // log 5
// exit = self.cpu.get_exec_calls_count() >= 243272; // log 6
2021-10-20 17:29:55 +00:00
// exit = self.cpu.get_exec_calls_count() >= 287416; // log 7
2021-10-20 18:39:39 +00:00
// exit = self.cpu.get_exec_calls_count() >= 223892; // log 8
2021-10-20 18:47:59 +00:00
// exit = self.cpu.get_exec_calls_count() >= 4420382; // log 9
2021-10-20 19:33:11 +00:00
// exit = self.cpu.get_exec_calls_count() >= 6714723; // log 10
// exit = self.cpu.get_exec_calls_count() >= 7429762; // log 11
2021-10-14 01:50:48 +00:00
}
}
}