clippy clean :)

This commit is contained in:
Alex Janka 2023-02-12 09:46:47 +11:00
parent cee17032bd
commit 18a0ceb46e
12 changed files with 64 additions and 77 deletions

View file

@ -6,8 +6,8 @@ mod util;
use clap::{ArgGroup, Parser}; use clap::{ArgGroup, Parser};
use minifb::{Window, WindowOptions}; use minifb::{Window, WindowOptions};
use processor::{ use processor::{
memory::{rom::ROM, Memory}, memory::{rom::Rom, Memory},
CPU, Cpu,
}; };
use std::{ use std::{
fs, fs,
@ -83,8 +83,8 @@ fn main() {
*v = args.verbose; *v = args.verbose;
} }
let rom: ROM = match fs::read(args.rom) { let rom: Rom = match fs::read(args.rom) {
Ok(data) => ROM::load(data), Ok(data) => Rom::load(data),
Err(e) => { Err(e) => {
println!("Error reading ROM: {e}"); println!("Error reading ROM: {e}");
return; return;
@ -112,7 +112,7 @@ fn main() {
window.topmost(true); window.topmost(true);
let mut cpu = CPU::new( let mut cpu = Cpu::new(
Memory::init(bootrom, args.run_bootrom, rom), Memory::init(bootrom, args.run_bootrom, rom),
window, window,
args.tile_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 will_pause = unsafe { PAUSE_QUEUED };
let pause_enabled = unsafe { PAUSE_ENABLED }; let pause_enabled = unsafe { PAUSE_ENABLED };
cpu.exec_next(); cpu.exec_next();

View file

@ -1,11 +1,11 @@
use self::{ use self::{
tile_window::TileWindow, tile_window::TileWindow,
types::{ types::{
Colour, DrawMode, ObjSize, Object, ObjectFlags, Palette, TiledataArea, TilemapArea, LCDC, Colour, DrawMode, Lcdc, ObjSize, Object, ObjectFlags, Palette, TiledataArea, TilemapArea,
}, },
}; };
use crate::{ use crate::{
processor::{SplitRegister, CPU}, processor::{Cpu, SplitRegister},
util::{clear_bit, get_bit, set_bit, set_or_clear_bit}, util::{clear_bit, get_bit, set_bit, set_or_clear_bit},
FACTOR, HEIGHT, WIDTH, 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_WIDTH_SCALED: usize = TILE_WINDOW_WIDTH * FACTOR;
const TILE_WINDOW_HEIGHT_SCALED: usize = TILE_WINDOW_HEIGHT * FACTOR; const TILE_WINDOW_HEIGHT_SCALED: usize = TILE_WINDOW_HEIGHT * FACTOR;
pub struct GPU { pub struct Gpu {
pub buffer: Vec<u32>, pub buffer: Vec<u32>,
scaled_buffer: Vec<u32>, scaled_buffer: Vec<u32>,
mode: DrawMode, mode: DrawMode,
@ -30,7 +30,7 @@ pub struct GPU {
has_window_been_enabled: bool, has_window_been_enabled: bool,
} }
impl GPU { impl Gpu {
pub(super) fn new(enable_tile_window: bool) -> Self { pub(super) fn new(enable_tile_window: bool) -> Self {
let tile_window = if enable_tile_window { let tile_window = if enable_tile_window {
let mut window = Window::new( let mut window = Window::new(
@ -63,7 +63,7 @@ impl GPU {
} }
} }
impl CPU { impl Cpu {
pub fn advance_gpu_clock(&mut self, steps: usize) { pub fn advance_gpu_clock(&mut self, steps: usize) {
let lcdc = self.get_lcdc(); let lcdc = self.get_lcdc();
if lcdc.enable { if lcdc.enable {
@ -115,9 +115,9 @@ impl CPU {
self.set_lcd_status(); self.set_lcd_status();
} }
fn get_lcdc(&self) -> LCDC { fn get_lcdc(&self) -> Lcdc {
let reg = self.memory.get(0xFF40); let reg = self.memory.get(0xFF40);
LCDC { Lcdc {
enable: get_bit(reg, 7), enable: get_bit(reg, 7),
window_tilemap: if get_bit(reg, 6) { window_tilemap: if get_bit(reg, 6) {
TilemapArea::T9C00 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.gpu.mode = DrawMode::HBlank;
self.render_scanline(self.gpu.scanline, lcdc); self.render_scanline(self.gpu.scanline, lcdc);
} }
@ -208,7 +208,7 @@ impl CPU {
self.memory.set(0xFF44, self.gpu.scanline); 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 { for x in 0..WIDTH {
self.gpu.buffer[(scanline as usize * WIDTH) + x] = Colour::from_u8_rgb(255, 0, 255); 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_y = 0_u8.wrapping_sub(self.memory.get(0xFF42));
let scroll_x = 0_u8.wrapping_sub(self.memory.get(0xFF43)); let scroll_x = 0_u8.wrapping_sub(self.memory.get(0xFF43));
self.render_tiles( 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); let pos_y = self.memory.get(0xFF4A);
// subtracting 7 to get the Real Number... // subtracting 7 to get the Real Number...
let pos_x = self.memory.get(0xFF4B); 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); let objs = self.parse_oam(scanline, &lcdc.obj_size);
for object in objs { for object in objs {
self.render_object(scanline, object, &lcdc.obj_size, bg_palette); self.render_object(scanline, object, &lcdc.obj_size, bg_palette);
@ -357,6 +357,7 @@ impl CPU {
} }
} }
#[allow(clippy::too_many_arguments)]
fn render_tiles( fn render_tiles(
&mut self, &mut self,
scanline: u8, scanline: u8,

View file

@ -54,7 +54,7 @@ impl ObjSize {
} }
#[derive(Debug, PartialEq, Clone, Copy)] #[derive(Debug, PartialEq, Clone, Copy)]
pub(super) struct LCDC { pub(super) struct Lcdc {
pub(super) enable: bool, pub(super) enable: bool,
pub(super) window_tilemap: TilemapArea, pub(super) window_tilemap: TilemapArea,
pub(super) window_enable: bool, pub(super) window_enable: bool,

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
processor::{get_bit, Direction, Flags, Reg8, SplitRegister, CPU}, processor::{get_bit, Cpu, Direction, Flags, Reg8, SplitRegister},
util::{clear_bit, set_bit}, util::{clear_bit, set_bit},
}; };
impl CPU { impl Cpu {
pub(crate) fn and(&mut self, first: u8, second: u8) -> u8 { pub(crate) fn and(&mut self, first: u8, second: u8) -> u8 {
let result = first & second; let result = first & second;
self.set_or_clear_flag(Flags::Zero, result == 0x0); self.set_or_clear_flag(Flags::Zero, result == 0x0);

View file

@ -1,2 +1,3 @@
#[allow(clippy::module_inception)]
pub mod instructions; pub mod instructions;
pub mod primitives; pub mod primitives;

View file

@ -1,10 +1,10 @@
use crate::{ use crate::{
processor::{get_bit, Direction, Flags, SplitRegister, CPU}, processor::{get_bit, Cpu, Direction, Flags, SplitRegister},
util::{get_rotation_carry, rotate}, util::{get_rotation_carry, rotate},
}; };
use std::ops::{BitAnd, BitOr}; use std::ops::{BitAnd, BitOr};
impl CPU { impl Cpu {
pub(crate) fn pop_word(&mut self) -> u16 { pub(crate) fn pop_word(&mut self) -> u16 {
let address = self.reg.sp; let address = self.reg.sp;
self.reg.sp = self.reg.sp.wrapping_add(0x2); self.reg.sp = self.reg.sp.wrapping_add(0x2);

View file

@ -1,4 +1,4 @@
use self::rom::ROM; use self::rom::Rom;
use crate::{ use crate::{
processor::{clear_bit, get_bit, SplitRegister}, processor::{clear_bit, get_bit, SplitRegister},
verbose_println, verbose_println,
@ -88,7 +88,7 @@ impl Default for Joypad {
pub struct Memory { pub struct Memory {
bootrom: Vec<u8>, bootrom: Vec<u8>,
bootrom_enabled: bool, bootrom_enabled: bool,
rom: ROM, rom: Rom,
vram: [u8; 8192], vram: [u8; 8192],
ram: [u8; 8192], ram: [u8; 8192],
switchable_ram: [u8; 8192], switchable_ram: [u8; 8192],
@ -103,7 +103,7 @@ pub struct Memory {
} }
impl 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 { Self {
bootrom, bootrom,
bootrom_enabled, bootrom_enabled,
@ -133,36 +133,22 @@ impl Memory {
self.rom.get(address) self.rom.get(address)
} }
} }
0x8000..0xA000 => { 0x8000..0xA000 => self.vram[(address - 0x8000) as usize],
self.vram[(address - 0x8000) as usize]
}
0xA000..0xC000 => { 0xA000..0xC000 => {
// cart ram // cart ram
self.rom.get_ram(address) self.rom.get_ram(address)
} }
0xC000..0xE000 => { 0xC000..0xE000 => self.ram[(address - 0xC000) as usize],
self.ram[(address - 0xC000) as usize] 0xE000..0xFE00 => self.ram[(address - 0xE000) as usize],
} 0xFE00..0xFEA0 => self.oam[(address - 0xFE00) as usize],
0xE000..0xFE00 => { 0xFEA0..0xFF00 => 0xFF,
self.ram[(address - 0xE000) as usize]
}
0xFE00..0xFEA0 => {
self.oam[(address - 0xFE00) as usize]
}
0xFEA0..0xFF00 => {
0xFF
}
0xFF00..0xFF4C => self.get_io(address), 0xFF00..0xFF4C => self.get_io(address),
0xFF4C..0xFF80 => { 0xFF4C..0xFF80 => {
// println!("empty space 2 read"); // println!("empty space 2 read");
0xFF 0xFF
} }
0xFF80..0xFFFF => { 0xFF80..0xFFFF => self.cpu_ram[(address - 0xFF80) as usize],
self.cpu_ram[(address - 0xFF80) as usize] 0xFFFF => self.interrupts,
}
0xFFFF => {
self.interrupts
}
} }
} }

View file

@ -1,21 +1,21 @@
use crate::processor::memory::Address; use crate::processor::memory::Address;
use std::str::from_utf8_unchecked; use std::str::from_utf8_unchecked;
use self::mbcs::{MBC, MBC1, NONE}; use self::mbcs::{Mbc, Mbc1, None};
mod mbcs; mod mbcs;
pub struct ROM { pub struct Rom {
title: String, title: String,
mbc: Box<dyn MBC>, mbc: Box<dyn Mbc>,
} }
impl ROM { impl Rom {
pub fn load(data: Vec<u8>) -> Self { pub fn load(data: Vec<u8>) -> Self {
let mut title_length = 0x143; let mut title_length = 0x143;
for i in 0x134..0x143 { for (i, val) in data.iter().enumerate().take(0x143).skip(0x134) {
title_length = i; title_length = i;
if data[i] == 0x0 { if *val == 0x0 {
break; break;
} }
} }
@ -24,13 +24,13 @@ impl ROM {
let _sgb_flag = data[0x146]; let _sgb_flag = data[0x146];
let rom_size = data[0x148]; let rom_size = data[0x148];
let ram_size = data[0x149]; let ram_size = data[0x149];
let mbc: Box<dyn MBC> = match data[0x147] { let mbc: Box<dyn Mbc> = match data[0x147] {
0x00 => Box::new(NONE::init(data)), 0x00 => Box::new(None::init(data)),
0x01 => Box::new(MBC1::init(data, rom_size, 0, None)), 0x01 => Box::new(Mbc1::init(data, rom_size, 0, None)),
0x02 => Box::new(MBC1::init(data, rom_size, ram_size, None)), 0x02 => Box::new(Mbc1::init(data, rom_size, ram_size, None)),
0x03 => { 0x03 => {
println!("MBC1 w/battery - battery not implemented!"); 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]), _ => panic!("unimplemented mbc: {:#X}", data[0x147]),
}; };

View file

@ -1,6 +1,6 @@
use crate::processor::memory::Address; use crate::processor::memory::Address;
pub(super) trait MBC { pub(super) trait Mbc {
fn get(&self, address: Address) -> u8; fn get(&self, address: Address) -> u8;
fn get_ram(&self, address: Address) -> u8; fn get_ram(&self, address: Address) -> u8;
fn set(&mut self, address: Address, data: u8); fn set(&mut self, address: Address, data: u8);
@ -8,17 +8,17 @@ pub(super) trait MBC {
fn mbc_type(&self) -> &str; fn mbc_type(&self) -> &str;
} }
pub(super) struct NONE { pub(super) struct None {
pub(super) data: Vec<u8>, pub(super) data: Vec<u8>,
} }
impl NONE { impl None {
pub(super) fn init(data: Vec<u8>) -> Self { pub(super) fn init(data: Vec<u8>) -> Self {
Self { data } Self { data }
} }
} }
impl MBC for NONE { impl Mbc for None {
fn get(&self, address: Address) -> u8 { fn get(&self, address: Address) -> u8 {
self.data[address as usize] self.data[address as usize]
} }
@ -29,8 +29,7 @@ impl MBC for NONE {
fn set_ram(&mut self, _address: Address, _data: u8) {} 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 { fn mbc_type(&self) -> &str {
"None" "None"
@ -43,7 +42,7 @@ enum BankingMode {
Advanced, Advanced,
} }
pub(super) struct MBC1 { pub(super) struct Mbc1 {
pub(super) data: Vec<u8>, pub(super) data: Vec<u8>,
rom_len: usize, rom_len: usize,
rom_bank: u8, rom_bank: u8,
@ -58,7 +57,7 @@ const KB: usize = 1024;
const ROM_BANK_SIZE: usize = 16 * KB; const ROM_BANK_SIZE: usize = 16 * KB;
const RAM_BANK_SIZE: usize = 8 * KB; const RAM_BANK_SIZE: usize = 8 * KB;
impl MBC1 { impl Mbc1 {
pub(super) fn init( pub(super) fn init(
data: Vec<u8>, data: Vec<u8>,
rom_size: u8, rom_size: u8,
@ -103,7 +102,7 @@ impl MBC1 {
} }
} }
impl MBC for MBC1 { impl Mbc for Mbc1 {
fn get(&self, address: Address) -> u8 { fn get(&self, address: Address) -> u8 {
self.data[self.get_rom_addr(address)] 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 { fn get_rom_addr(&self, address: Address) -> usize {
(match address { (match address {
0x0..0x4000 => match self.bank_mode { 0x0..0x4000 => match self.bank_mode {

View file

@ -1,4 +1,4 @@
use self::{gpu::GPU, memory::Memory, timer::Timers}; use self::{gpu::Gpu, memory::Memory, timer::Timers};
use crate::{ use crate::{
util::{clear_bit, get_bit}, util::{clear_bit, get_bit},
verbose_println, verbose_println,
@ -24,13 +24,13 @@ pub(crate) enum Direction {
Right, Right,
} }
pub struct CPU { pub struct Cpu {
pub memory: Memory, pub memory: Memory,
pub reg: Registers, pub reg: Registers,
pub last_instruction: u8, pub last_instruction: u8,
last_instruction_addr: u16, last_instruction_addr: u16,
window: Window, window: Window,
gpu: GPU, gpu: Gpu,
halted: bool, halted: bool,
timers: Timers, timers: Timers,
} }
@ -40,7 +40,7 @@ const CLOCK_SPEED: usize = 4194304;
const SPEEDUP: f64 = 1.; const SPEEDUP: f64 = 1.;
// const FF04_SPEED: f64 = 16384.; // const FF04_SPEED: f64 = 16384.;
impl CPU { impl Cpu {
pub fn new( pub fn new(
mut memory: Memory, mut memory: Memory,
window: Window, window: Window,
@ -56,7 +56,7 @@ impl CPU {
last_instruction: 0x0, last_instruction: 0x0,
last_instruction_addr: 0x0, last_instruction_addr: 0x0,
window, window,
gpu: GPU::new(enable_tile_window), gpu: Gpu::new(enable_tile_window),
halted: false, halted: false,
timers: Timers::init(), timers: Timers::init(),
} }

View file

@ -1,12 +1,12 @@
use crate::{ use crate::{
processor::{ processor::{
instructions::instructions::{res, set}, instructions::instructions::{res, set},
Flags, Reg8, SplitRegister, CPU, Cpu, Flags, Reg8, SplitRegister,
}, },
util::as_signed, util::as_signed,
}; };
impl CPU { impl Cpu {
pub fn run_opcode(&mut self, opcode: u8) -> u8 { pub fn run_opcode(&mut self, opcode: u8) -> u8 {
match opcode { match opcode {
0x00 => { 0x00 => {

View file

@ -1,7 +1,7 @@
use std::time::Duration; use std::time::Duration;
use crate::{ use crate::{
processor::{CLOCK_SPEED, CPU, SPEEDUP}, processor::{Cpu, CLOCK_SPEED, SPEEDUP},
util::{get_bit, set_bit}, 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) { pub(super) fn increment_timers(&mut self, machine_cycles: u8) {
let clock_cycles = (machine_cycles as usize) * 4; let clock_cycles = (machine_cycles as usize) * 4;