clippy clean :)
This commit is contained in:
parent
cee17032bd
commit
18a0ceb46e
12
src/main.rs
12
src/main.rs
|
@ -6,8 +6,8 @@ mod util;
|
|||
use clap::{ArgGroup, Parser};
|
||||
use minifb::{Window, WindowOptions};
|
||||
use processor::{
|
||||
memory::{rom::ROM, Memory},
|
||||
CPU,
|
||||
memory::{rom::Rom, Memory},
|
||||
Cpu,
|
||||
};
|
||||
use std::{
|
||||
fs,
|
||||
|
@ -83,8 +83,8 @@ fn main() {
|
|||
*v = args.verbose;
|
||||
}
|
||||
|
||||
let rom: ROM = match fs::read(args.rom) {
|
||||
Ok(data) => ROM::load(data),
|
||||
let rom: Rom = match fs::read(args.rom) {
|
||||
Ok(data) => Rom::load(data),
|
||||
Err(e) => {
|
||||
println!("Error reading ROM: {e}");
|
||||
return;
|
||||
|
@ -112,7 +112,7 @@ fn main() {
|
|||
|
||||
window.topmost(true);
|
||||
|
||||
let mut cpu = CPU::new(
|
||||
let mut cpu = Cpu::new(
|
||||
Memory::init(bootrom, args.run_bootrom, rom),
|
||||
window,
|
||||
args.tile_window,
|
||||
|
@ -145,7 +145,7 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
fn run_cycle(cpu: &mut CPU) {
|
||||
fn run_cycle(cpu: &mut Cpu) {
|
||||
let will_pause = unsafe { PAUSE_QUEUED };
|
||||
let pause_enabled = unsafe { PAUSE_ENABLED };
|
||||
cpu.exec_next();
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use self::{
|
||||
tile_window::TileWindow,
|
||||
types::{
|
||||
Colour, DrawMode, ObjSize, Object, ObjectFlags, Palette, TiledataArea, TilemapArea, LCDC,
|
||||
Colour, DrawMode, Lcdc, ObjSize, Object, ObjectFlags, Palette, TiledataArea, TilemapArea,
|
||||
},
|
||||
};
|
||||
use crate::{
|
||||
processor::{SplitRegister, CPU},
|
||||
processor::{Cpu, SplitRegister},
|
||||
util::{clear_bit, get_bit, set_bit, set_or_clear_bit},
|
||||
FACTOR, HEIGHT, WIDTH,
|
||||
};
|
||||
|
@ -19,7 +19,7 @@ const TILE_WINDOW_HEIGHT: usize = 24 * 8;
|
|||
const TILE_WINDOW_WIDTH_SCALED: usize = TILE_WINDOW_WIDTH * FACTOR;
|
||||
const TILE_WINDOW_HEIGHT_SCALED: usize = TILE_WINDOW_HEIGHT * FACTOR;
|
||||
|
||||
pub struct GPU {
|
||||
pub struct Gpu {
|
||||
pub buffer: Vec<u32>,
|
||||
scaled_buffer: Vec<u32>,
|
||||
mode: DrawMode,
|
||||
|
@ -30,7 +30,7 @@ pub struct GPU {
|
|||
has_window_been_enabled: bool,
|
||||
}
|
||||
|
||||
impl GPU {
|
||||
impl Gpu {
|
||||
pub(super) fn new(enable_tile_window: bool) -> Self {
|
||||
let tile_window = if enable_tile_window {
|
||||
let mut window = Window::new(
|
||||
|
@ -63,7 +63,7 @@ impl GPU {
|
|||
}
|
||||
}
|
||||
|
||||
impl CPU {
|
||||
impl Cpu {
|
||||
pub fn advance_gpu_clock(&mut self, steps: usize) {
|
||||
let lcdc = self.get_lcdc();
|
||||
if lcdc.enable {
|
||||
|
@ -115,9 +115,9 @@ impl CPU {
|
|||
self.set_lcd_status();
|
||||
}
|
||||
|
||||
fn get_lcdc(&self) -> LCDC {
|
||||
fn get_lcdc(&self) -> Lcdc {
|
||||
let reg = self.memory.get(0xFF40);
|
||||
LCDC {
|
||||
Lcdc {
|
||||
enable: get_bit(reg, 7),
|
||||
window_tilemap: if get_bit(reg, 6) {
|
||||
TilemapArea::T9C00
|
||||
|
@ -145,7 +145,7 @@ impl CPU {
|
|||
}
|
||||
}
|
||||
|
||||
fn enter_hblank(&mut self, lcdc: &LCDC) {
|
||||
fn enter_hblank(&mut self, lcdc: &Lcdc) {
|
||||
self.gpu.mode = DrawMode::HBlank;
|
||||
self.render_scanline(self.gpu.scanline, lcdc);
|
||||
}
|
||||
|
@ -208,7 +208,7 @@ impl CPU {
|
|||
self.memory.set(0xFF44, self.gpu.scanline);
|
||||
}
|
||||
|
||||
fn render_scanline(&mut self, scanline: u8, lcdc: &LCDC) {
|
||||
fn render_scanline(&mut self, scanline: u8, lcdc: &Lcdc) {
|
||||
for x in 0..WIDTH {
|
||||
self.gpu.buffer[(scanline as usize * WIDTH) + x] = Colour::from_u8_rgb(255, 0, 255);
|
||||
}
|
||||
|
@ -233,7 +233,7 @@ impl CPU {
|
|||
}
|
||||
}
|
||||
|
||||
fn render_scanline_bg(&mut self, scanline: u8, lcdc: &LCDC, palette: Palette) {
|
||||
fn render_scanline_bg(&mut self, scanline: u8, lcdc: &Lcdc, palette: Palette) {
|
||||
let scroll_y = 0_u8.wrapping_sub(self.memory.get(0xFF42));
|
||||
let scroll_x = 0_u8.wrapping_sub(self.memory.get(0xFF43));
|
||||
self.render_tiles(
|
||||
|
@ -248,7 +248,7 @@ impl CPU {
|
|||
);
|
||||
}
|
||||
|
||||
fn render_scanline_window(&mut self, scanline: u8, lcdc: &LCDC, palette: Palette) {
|
||||
fn render_scanline_window(&mut self, scanline: u8, lcdc: &Lcdc, palette: Palette) {
|
||||
let pos_y = self.memory.get(0xFF4A);
|
||||
// subtracting 7 to get the Real Number...
|
||||
let pos_x = self.memory.get(0xFF4B);
|
||||
|
@ -271,7 +271,7 @@ impl CPU {
|
|||
}
|
||||
}
|
||||
|
||||
fn render_scanline_obj(&mut self, scanline: u8, lcdc: &LCDC, bg_palette: Palette) {
|
||||
fn render_scanline_obj(&mut self, scanline: u8, lcdc: &Lcdc, bg_palette: Palette) {
|
||||
let objs = self.parse_oam(scanline, &lcdc.obj_size);
|
||||
for object in objs {
|
||||
self.render_object(scanline, object, &lcdc.obj_size, bg_palette);
|
||||
|
@ -357,6 +357,7 @@ impl CPU {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_tiles(
|
||||
&mut self,
|
||||
scanline: u8,
|
||||
|
|
|
@ -54,7 +54,7 @@ impl ObjSize {
|
|||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub(super) struct LCDC {
|
||||
pub(super) struct Lcdc {
|
||||
pub(super) enable: bool,
|
||||
pub(super) window_tilemap: TilemapArea,
|
||||
pub(super) window_enable: bool,
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use crate::{
|
||||
processor::{get_bit, Direction, Flags, Reg8, SplitRegister, CPU},
|
||||
processor::{get_bit, Cpu, Direction, Flags, Reg8, SplitRegister},
|
||||
util::{clear_bit, set_bit},
|
||||
};
|
||||
|
||||
impl CPU {
|
||||
impl Cpu {
|
||||
pub(crate) fn and(&mut self, first: u8, second: u8) -> u8 {
|
||||
let result = first & second;
|
||||
self.set_or_clear_flag(Flags::Zero, result == 0x0);
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
#[allow(clippy::module_inception)]
|
||||
pub mod instructions;
|
||||
pub mod primitives;
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use crate::{
|
||||
processor::{get_bit, Direction, Flags, SplitRegister, CPU},
|
||||
processor::{get_bit, Cpu, Direction, Flags, SplitRegister},
|
||||
util::{get_rotation_carry, rotate},
|
||||
};
|
||||
use std::ops::{BitAnd, BitOr};
|
||||
|
||||
impl CPU {
|
||||
impl Cpu {
|
||||
pub(crate) fn pop_word(&mut self) -> u16 {
|
||||
let address = self.reg.sp;
|
||||
self.reg.sp = self.reg.sp.wrapping_add(0x2);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use self::rom::ROM;
|
||||
use self::rom::Rom;
|
||||
use crate::{
|
||||
processor::{clear_bit, get_bit, SplitRegister},
|
||||
verbose_println,
|
||||
|
@ -88,7 +88,7 @@ impl Default for Joypad {
|
|||
pub struct Memory {
|
||||
bootrom: Vec<u8>,
|
||||
bootrom_enabled: bool,
|
||||
rom: ROM,
|
||||
rom: Rom,
|
||||
vram: [u8; 8192],
|
||||
ram: [u8; 8192],
|
||||
switchable_ram: [u8; 8192],
|
||||
|
@ -103,7 +103,7 @@ pub struct Memory {
|
|||
}
|
||||
|
||||
impl Memory {
|
||||
pub fn init(bootrom: Vec<u8>, bootrom_enabled: bool, rom: ROM) -> Self {
|
||||
pub fn init(bootrom: Vec<u8>, bootrom_enabled: bool, rom: Rom) -> Self {
|
||||
Self {
|
||||
bootrom,
|
||||
bootrom_enabled,
|
||||
|
@ -133,36 +133,22 @@ impl Memory {
|
|||
self.rom.get(address)
|
||||
}
|
||||
}
|
||||
0x8000..0xA000 => {
|
||||
self.vram[(address - 0x8000) as usize]
|
||||
}
|
||||
0x8000..0xA000 => self.vram[(address - 0x8000) as usize],
|
||||
0xA000..0xC000 => {
|
||||
// cart ram
|
||||
self.rom.get_ram(address)
|
||||
}
|
||||
0xC000..0xE000 => {
|
||||
self.ram[(address - 0xC000) as usize]
|
||||
}
|
||||
0xE000..0xFE00 => {
|
||||
self.ram[(address - 0xE000) as usize]
|
||||
}
|
||||
0xFE00..0xFEA0 => {
|
||||
self.oam[(address - 0xFE00) as usize]
|
||||
}
|
||||
0xFEA0..0xFF00 => {
|
||||
0xFF
|
||||
}
|
||||
0xC000..0xE000 => self.ram[(address - 0xC000) as usize],
|
||||
0xE000..0xFE00 => self.ram[(address - 0xE000) as usize],
|
||||
0xFE00..0xFEA0 => self.oam[(address - 0xFE00) as usize],
|
||||
0xFEA0..0xFF00 => 0xFF,
|
||||
0xFF00..0xFF4C => self.get_io(address),
|
||||
0xFF4C..0xFF80 => {
|
||||
// println!("empty space 2 read");
|
||||
0xFF
|
||||
}
|
||||
0xFF80..0xFFFF => {
|
||||
self.cpu_ram[(address - 0xFF80) as usize]
|
||||
}
|
||||
0xFFFF => {
|
||||
self.interrupts
|
||||
}
|
||||
0xFF80..0xFFFF => self.cpu_ram[(address - 0xFF80) as usize],
|
||||
0xFFFF => self.interrupts,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
use crate::processor::memory::Address;
|
||||
use std::str::from_utf8_unchecked;
|
||||
|
||||
use self::mbcs::{MBC, MBC1, NONE};
|
||||
use self::mbcs::{Mbc, Mbc1, None};
|
||||
|
||||
mod mbcs;
|
||||
|
||||
pub struct ROM {
|
||||
pub struct Rom {
|
||||
title: String,
|
||||
mbc: Box<dyn MBC>,
|
||||
mbc: Box<dyn Mbc>,
|
||||
}
|
||||
|
||||
impl ROM {
|
||||
impl Rom {
|
||||
pub fn load(data: Vec<u8>) -> Self {
|
||||
let mut title_length = 0x143;
|
||||
for i in 0x134..0x143 {
|
||||
for (i, val) in data.iter().enumerate().take(0x143).skip(0x134) {
|
||||
title_length = i;
|
||||
if data[i] == 0x0 {
|
||||
if *val == 0x0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -24,13 +24,13 @@ impl ROM {
|
|||
let _sgb_flag = data[0x146];
|
||||
let rom_size = data[0x148];
|
||||
let ram_size = data[0x149];
|
||||
let mbc: Box<dyn MBC> = match data[0x147] {
|
||||
0x00 => Box::new(NONE::init(data)),
|
||||
0x01 => Box::new(MBC1::init(data, rom_size, 0, None)),
|
||||
0x02 => Box::new(MBC1::init(data, rom_size, ram_size, None)),
|
||||
let mbc: Box<dyn Mbc> = match data[0x147] {
|
||||
0x00 => Box::new(None::init(data)),
|
||||
0x01 => Box::new(Mbc1::init(data, rom_size, 0, None)),
|
||||
0x02 => Box::new(Mbc1::init(data, rom_size, ram_size, None)),
|
||||
0x03 => {
|
||||
println!("MBC1 w/battery - battery not implemented!");
|
||||
Box::new(MBC1::init(data, rom_size, ram_size, None))
|
||||
Box::new(Mbc1::init(data, rom_size, ram_size, None))
|
||||
}
|
||||
_ => panic!("unimplemented mbc: {:#X}", data[0x147]),
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::processor::memory::Address;
|
||||
|
||||
pub(super) trait MBC {
|
||||
pub(super) trait Mbc {
|
||||
fn get(&self, address: Address) -> u8;
|
||||
fn get_ram(&self, address: Address) -> u8;
|
||||
fn set(&mut self, address: Address, data: u8);
|
||||
|
@ -8,17 +8,17 @@ pub(super) trait MBC {
|
|||
fn mbc_type(&self) -> &str;
|
||||
}
|
||||
|
||||
pub(super) struct NONE {
|
||||
pub(super) struct None {
|
||||
pub(super) data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl NONE {
|
||||
impl None {
|
||||
pub(super) fn init(data: Vec<u8>) -> Self {
|
||||
Self { data }
|
||||
}
|
||||
}
|
||||
|
||||
impl MBC for NONE {
|
||||
impl Mbc for None {
|
||||
fn get(&self, address: Address) -> u8 {
|
||||
self.data[address as usize]
|
||||
}
|
||||
|
@ -29,8 +29,7 @@ impl MBC for NONE {
|
|||
|
||||
fn set_ram(&mut self, _address: Address, _data: u8) {}
|
||||
|
||||
fn set(&mut self, _address: Address, _data: u8) {
|
||||
}
|
||||
fn set(&mut self, _address: Address, _data: u8) {}
|
||||
|
||||
fn mbc_type(&self) -> &str {
|
||||
"None"
|
||||
|
@ -43,7 +42,7 @@ enum BankingMode {
|
|||
Advanced,
|
||||
}
|
||||
|
||||
pub(super) struct MBC1 {
|
||||
pub(super) struct Mbc1 {
|
||||
pub(super) data: Vec<u8>,
|
||||
rom_len: usize,
|
||||
rom_bank: u8,
|
||||
|
@ -58,7 +57,7 @@ const KB: usize = 1024;
|
|||
const ROM_BANK_SIZE: usize = 16 * KB;
|
||||
const RAM_BANK_SIZE: usize = 8 * KB;
|
||||
|
||||
impl MBC1 {
|
||||
impl Mbc1 {
|
||||
pub(super) fn init(
|
||||
data: Vec<u8>,
|
||||
rom_size: u8,
|
||||
|
@ -103,7 +102,7 @@ impl MBC1 {
|
|||
}
|
||||
}
|
||||
|
||||
impl MBC for MBC1 {
|
||||
impl Mbc for Mbc1 {
|
||||
fn get(&self, address: Address) -> u8 {
|
||||
self.data[self.get_rom_addr(address)]
|
||||
}
|
||||
|
@ -161,7 +160,7 @@ impl MBC for MBC1 {
|
|||
}
|
||||
}
|
||||
|
||||
impl MBC1 {
|
||||
impl Mbc1 {
|
||||
fn get_rom_addr(&self, address: Address) -> usize {
|
||||
(match address {
|
||||
0x0..0x4000 => match self.bank_mode {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use self::{gpu::GPU, memory::Memory, timer::Timers};
|
||||
use self::{gpu::Gpu, memory::Memory, timer::Timers};
|
||||
use crate::{
|
||||
util::{clear_bit, get_bit},
|
||||
verbose_println,
|
||||
|
@ -24,13 +24,13 @@ pub(crate) enum Direction {
|
|||
Right,
|
||||
}
|
||||
|
||||
pub struct CPU {
|
||||
pub struct Cpu {
|
||||
pub memory: Memory,
|
||||
pub reg: Registers,
|
||||
pub last_instruction: u8,
|
||||
last_instruction_addr: u16,
|
||||
window: Window,
|
||||
gpu: GPU,
|
||||
gpu: Gpu,
|
||||
halted: bool,
|
||||
timers: Timers,
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ const CLOCK_SPEED: usize = 4194304;
|
|||
const SPEEDUP: f64 = 1.;
|
||||
// const FF04_SPEED: f64 = 16384.;
|
||||
|
||||
impl CPU {
|
||||
impl Cpu {
|
||||
pub fn new(
|
||||
mut memory: Memory,
|
||||
window: Window,
|
||||
|
@ -56,7 +56,7 @@ impl CPU {
|
|||
last_instruction: 0x0,
|
||||
last_instruction_addr: 0x0,
|
||||
window,
|
||||
gpu: GPU::new(enable_tile_window),
|
||||
gpu: Gpu::new(enable_tile_window),
|
||||
halted: false,
|
||||
timers: Timers::init(),
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use crate::{
|
||||
processor::{
|
||||
instructions::instructions::{res, set},
|
||||
Flags, Reg8, SplitRegister, CPU,
|
||||
Cpu, Flags, Reg8, SplitRegister,
|
||||
},
|
||||
util::as_signed,
|
||||
};
|
||||
|
||||
impl CPU {
|
||||
impl Cpu {
|
||||
pub fn run_opcode(&mut self, opcode: u8) -> u8 {
|
||||
match opcode {
|
||||
0x00 => {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use crate::{
|
||||
processor::{CLOCK_SPEED, CPU, SPEEDUP},
|
||||
processor::{Cpu, CLOCK_SPEED, SPEEDUP},
|
||||
util::{get_bit, set_bit},
|
||||
};
|
||||
|
||||
|
@ -19,7 +19,7 @@ impl Timers {
|
|||
}
|
||||
}
|
||||
|
||||
impl CPU {
|
||||
impl Cpu {
|
||||
pub(super) fn increment_timers(&mut self, machine_cycles: u8) {
|
||||
let clock_cycles = (machine_cycles as usize) * 4;
|
||||
|
||||
|
|
Loading…
Reference in a new issue