2021-11-05 15:08:17 +00:00
|
|
|
// use std::{thread, time};
|
2021-11-15 14:15:31 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::cell::RefCell;
|
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};
|
2021-10-14 00:38:37 +00:00
|
|
|
use crate::ppu::PPU;
|
|
|
|
use crate::bus::Bus;
|
2021-10-30 14:13:31 +00:00
|
|
|
use crate::timer::Timer;
|
2021-11-15 14:15:31 +00:00
|
|
|
use crate::joypad::{Joypad, Button};
|
2021-10-14 00:38:37 +00:00
|
|
|
|
2021-10-26 22:34:59 +00:00
|
|
|
pub struct Emulator {
|
2021-10-14 00:38:37 +00:00
|
|
|
bus: Bus,
|
2021-11-15 16:25:30 +00:00
|
|
|
cpu: CPU,
|
|
|
|
ppu: Rc<RefCell<PPU>>,
|
2021-11-15 14:27:03 +00:00
|
|
|
timer: Rc<RefCell<Timer>>,
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad: Rc<RefCell<Joypad>>,
|
2021-10-14 00:38:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 22:34:59 +00:00
|
|
|
impl Emulator {
|
2021-10-14 00:38:37 +00:00
|
|
|
pub fn new() -> Self {
|
2021-11-15 14:15:31 +00:00
|
|
|
let joypad = Rc::new(RefCell::new(Joypad::new()));
|
2021-11-15 16:25:30 +00:00
|
|
|
let timer = Rc::new(RefCell::new(Timer::new()));
|
|
|
|
let ppu = Rc::new(RefCell::new(PPU::new()));
|
2021-10-14 00:38:37 +00:00
|
|
|
Self {
|
2021-11-15 16:25:30 +00:00
|
|
|
bus: Bus::new(Rc::clone(&ppu), Rc::clone(&joypad), Rc::clone(&timer)),
|
2021-10-14 00:38:37 +00:00
|
|
|
cpu: CPU::new(),
|
2021-11-15 16:25:30 +00:00
|
|
|
ppu,
|
2021-11-15 14:27:03 +00:00
|
|
|
timer,
|
|
|
|
joypad,
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_input(&mut self, input: &WinitInputHelper) {
|
|
|
|
let mut change = false;
|
2021-11-15 14:15:31 +00:00
|
|
|
let mut joypad = self.joypad.borrow_mut();
|
2021-11-01 02:02:09 +00:00
|
|
|
if input.key_pressed(VirtualKeyCode::K) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.press(Button::A);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_pressed(VirtualKeyCode::J) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.press(Button::B);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_pressed(VirtualKeyCode::W) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.press(Button::Up);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_pressed(VirtualKeyCode::S) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.press(Button::Down);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_pressed(VirtualKeyCode::A) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.press(Button::Left);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_pressed(VirtualKeyCode::D) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.press(Button::Right);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_pressed(VirtualKeyCode::N) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.press(Button::Start);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_pressed(VirtualKeyCode::B) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.press(Button::Select);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if input.key_released(VirtualKeyCode::K) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.release(Button::A);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::J) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.release(Button::B);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::W) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.release(Button::Up);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::S) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.release(Button::Down);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::A) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.release(Button::Left);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::D) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.release(Button::Right);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::N) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.release(Button::Start);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if input.key_released(VirtualKeyCode::B) {
|
|
|
|
change = true;
|
2021-11-15 14:15:31 +00:00
|
|
|
joypad.release(Button::Select);
|
2021-11-01 02:02:09 +00:00
|
|
|
}
|
|
|
|
if change {
|
|
|
|
self.bus.set_interrupt_flag(Interrupt::Joypad, true);
|
2021-10-14 00:38:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 16:59:48 +00:00
|
|
|
pub fn run(&mut self, cpu_cycles: Cycles, frame_buffer: &mut [u8]) {
|
2021-10-26 22:34:59 +00:00
|
|
|
self.cpu.reset_cycles();
|
2021-11-07 02:36:16 +00:00
|
|
|
while self.cpu.get_cycles().to_t().0 <= cpu_cycles.0 {
|
2021-10-26 22:34:59 +00:00
|
|
|
self.cpu.run(&mut self.bus);
|
2021-11-15 16:25:30 +00:00
|
|
|
let cycles = self.cpu.get_last_op_cycles().to_t();
|
2021-11-15 18:15:31 +00:00
|
|
|
self.ppu.borrow_mut().do_cycles(&mut self.bus, cycles, frame_buffer);
|
|
|
|
self.timer.borrow_mut().do_cycles(&mut self.bus, cycles);
|
2021-11-03 13:36:30 +00:00
|
|
|
|
|
|
|
// 1 CPU cycle = 238.42ns
|
|
|
|
// thread::sleep(time::Duration::from_nanos((self.cpu.get_last_op_cycles().0 * 238).try_into().unwrap()));
|
|
|
|
|
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;
|
2021-11-11 01:57:58 +00:00
|
|
|
let mut frame: [u8; 144 * 160 * 4] = [0; 144 * 160 * 4];
|
2021-10-14 01:50:48 +00:00
|
|
|
while !exit {
|
|
|
|
self.cpu.run(&mut self.bus);
|
2021-11-15 16:25:30 +00:00
|
|
|
self.ppu.borrow_mut().do_cycles(&mut self.bus, self.cpu.get_last_op_cycles().to_t(), &mut frame);
|
2021-11-15 14:27:03 +00:00
|
|
|
self.timer.borrow_mut().do_cycles(&mut self.bus, self.cpu.get_last_op_cycles().to_t());
|
2021-10-14 01:50:48 +00:00
|
|
|
|
2021-10-20 02:55:35 +00:00
|
|
|
// exit = self.cpu.get_exec_calls_count() >= 1258895; // log 1
|
2021-11-03 13:36:30 +00:00
|
|
|
exit = self.cpu.get_exec_calls_count() >= 161502; // log 2
|
2021-10-20 02:55:35 +00:00
|
|
|
// 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
|
|
|
}
|
2021-10-14 00:38:37 +00:00
|
|
|
}
|
|
|
|
}
|