From cee17032bd1a67604bee2064eeb81e42ab77b2bc Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Sun, 12 Feb 2023 09:41:34 +1100 Subject: [PATCH] clippy suggestions --- src/main.rs | 10 ++++----- src/processor/gpu.rs | 10 ++++----- src/processor/gpu/tile_window.rs | 2 +- src/processor/gpu/types.rs | 2 +- src/processor/instructions/primitives.rs | 14 ++++++------ src/processor/memory.rs | 28 ++++++++++++------------ src/processor/memory/rom/mbcs.rs | 4 +--- src/processor/mod.rs | 2 +- src/processor/opcodes.rs | 2 +- src/processor/timer.rs | 4 ++-- src/util.rs | 8 +++---- 11 files changed, 41 insertions(+), 45 deletions(-) diff --git a/src/main.rs b/src/main.rs index e17966e..afda88c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ use std::{ #[macro_export] macro_rules! verbose_println { ($($tts:tt)*) => { - if crate::is_verbose() { + if $crate::is_verbose() { println!($($tts)*); } }; @@ -27,7 +27,7 @@ macro_rules! verbose_println { #[macro_export] macro_rules! verbose_print { ($($tts:tt)*) => { - if crate::is_verbose() { + if $crate::is_verbose() { print!($($tts)*); } }; @@ -149,10 +149,8 @@ fn run_cycle(cpu: &mut CPU) { let will_pause = unsafe { PAUSE_QUEUED }; let pause_enabled = unsafe { PAUSE_ENABLED }; cpu.exec_next(); - if !pause_enabled { - if cpu.reg.pc >= 0x100 { - unsafe { PAUSE_ENABLED = true }; - } + if !pause_enabled && cpu.reg.pc >= 0x100 { + unsafe { PAUSE_ENABLED = true }; } if will_pause { pause_then_step(); diff --git a/src/processor/gpu.rs b/src/processor/gpu.rs index f89a5d3..d06fc15 100644 --- a/src/processor/gpu.rs +++ b/src/processor/gpu.rs @@ -349,9 +349,9 @@ impl CPU { if x_coord < WIDTH { let buffer_index = (scanline as usize * WIDTH) + x_coord; if !object.flags.behind_bg_and_window - || self.gpu.buffer[buffer_index] == bg_palette.zero.to_rgb() + || self.gpu.buffer[buffer_index] == bg_palette.zero.as_rgb() { - self.gpu.buffer[buffer_index] = colour.to_rgb(); + self.gpu.buffer[buffer_index] = colour.as_rgb(); } } } @@ -384,7 +384,7 @@ impl CPU { let tilemap_column = (tile_line_x / 8) as u16; - let tile_px_x = (tile_line_x % 8) as u8; + let tile_px_x = tile_line_x % 8; let tile_addr = tiledata.get_addr( self.memory .get(tilemap.get_addr(row_addr + (tilemap_column))), @@ -396,7 +396,7 @@ impl CPU { let msb = get_bit(msbs, 7 - tile_px_x); let colour = bits_to_mapped_colour(lsb, msb, palette); - self.gpu.buffer[(scanline as usize * WIDTH) + x] = colour.to_rgb(); + self.gpu.buffer[(scanline as usize * WIDTH) + x] = colour.as_rgb(); } } @@ -408,7 +408,7 @@ impl CPU { } } -fn scale_buffer(buffer: &Vec, width: usize, height: usize, factor: usize) -> Vec { +fn scale_buffer(buffer: &[u32], width: usize, height: usize, factor: usize) -> Vec { let mut v = vec![]; for y in 0..height { for _ in 0..factor { diff --git a/src/processor/gpu/tile_window.rs b/src/processor/gpu/tile_window.rs index f02ad7e..f370728 100644 --- a/src/processor/gpu/tile_window.rs +++ b/src/processor/gpu/tile_window.rs @@ -86,7 +86,7 @@ impl TileWindow { let colour = bits_to_mapped_colour(lsb, msb, palette); self.sprite_buffer[real_px_x + (real_px_y * TILE_WINDOW_WIDTH)] = - colour.to_rgb(); + colour.as_rgb(); } } } diff --git a/src/processor/gpu/types.rs b/src/processor/gpu/types.rs index b22dc79..bb9cc58 100644 --- a/src/processor/gpu/types.rs +++ b/src/processor/gpu/types.rs @@ -74,7 +74,7 @@ pub(super) enum Colour { } impl Colour { - pub(super) fn to_rgb(&self) -> u32 { + pub(super) fn as_rgb(&self) -> u32 { match self { Colour::White => Self::from_u8_rgb(0xFF, 0xFF, 0xFF), Colour::LightGray => Self::from_u8_rgb(0xAA, 0xAA, 0xAA), diff --git a/src/processor/instructions/primitives.rs b/src/processor/instructions/primitives.rs index dfc2bb9..99e9685 100644 --- a/src/processor/instructions/primitives.rs +++ b/src/processor/instructions/primitives.rs @@ -41,7 +41,7 @@ impl CPU { self.set_or_clear_flag(Flags::Zero, rotated == 0x0); self.clear_flag(Flags::HalfCarry); self.clear_flag(Flags::NSubtract); - return rotated; + rotated } pub(crate) fn rotate(&mut self, byte: u8, direction: Direction) -> u8 { @@ -56,7 +56,7 @@ impl CPU { self.set_or_clear_flag(Flags::Zero, rotated == 0x0); self.clear_flag(Flags::HalfCarry); self.clear_flag(Flags::NSubtract); - return rotated; + rotated } pub(crate) fn shift(&mut self, byte: u8, direction: Direction) -> u8 { @@ -67,7 +67,7 @@ impl CPU { self.set_or_clear_flag(Flags::Zero, rotated == 0x0); self.clear_flag(Flags::HalfCarry); self.clear_flag(Flags::NSubtract); - return rotated; + rotated } pub(crate) fn get_flag(&mut self, flag: Flags) -> u8 { @@ -113,7 +113,7 @@ impl CPU { & 0x1000) == 0x1000, ); - return result; + result } pub(crate) fn add_u8s(&mut self, first: u8, second: u8) -> u8 { @@ -125,7 +125,7 @@ impl CPU { Flags::HalfCarry, (((first & 0xF).wrapping_add(second & 0xF)) & 0x10) == 0x10, ); - return result; + result } pub(crate) fn add_u16s(&mut self, first: u16, second: u16) -> u16 { @@ -137,7 +137,7 @@ impl CPU { Flags::HalfCarry, (((first & 0xFFF).wrapping_add(second & 0xFFF)) & 0x1000) == 0x1000, ); - return result; + result } pub(crate) fn sub_u8s(&mut self, first: u8, second: u8) -> u8 { @@ -149,7 +149,7 @@ impl CPU { Flags::HalfCarry, (((first & 0xF).wrapping_sub(second & 0xF)) & 0x10) == 0x10, ); - return result; + result } pub(crate) fn dec_raw(&mut self, val: u8) -> u8 { diff --git a/src/processor/memory.rs b/src/processor/memory.rs index b23870d..01832fe 100644 --- a/src/processor/memory.rs +++ b/src/processor/memory.rs @@ -128,40 +128,40 @@ impl Memory { // rom access // todo - switchable rom banks if self.bootrom_enabled && ((address as usize) < self.bootrom.len()) { - return self.bootrom[address as usize]; + self.bootrom[address as usize] } else { - return self.rom.get(address); + self.rom.get(address) } } 0x8000..0xA000 => { - return self.vram[(address - 0x8000) as usize]; + self.vram[(address - 0x8000) as usize] } 0xA000..0xC000 => { // cart ram self.rom.get_ram(address) } 0xC000..0xE000 => { - return self.ram[(address - 0xC000) as usize]; + self.ram[(address - 0xC000) as usize] } 0xE000..0xFE00 => { - return self.ram[(address - 0xE000) as usize]; + self.ram[(address - 0xE000) as usize] } 0xFE00..0xFEA0 => { - return self.oam[(address - 0xFE00) as usize]; + self.oam[(address - 0xFE00) as usize] } 0xFEA0..0xFF00 => { - return 0xFF; + 0xFF } 0xFF00..0xFF4C => self.get_io(address), 0xFF4C..0xFF80 => { // println!("empty space 2 read"); - return 0xFF; + 0xFF } 0xFF80..0xFFFF => { - return self.cpu_ram[(address - 0xFF80) as usize]; + self.cpu_ram[(address - 0xFF80) as usize] } 0xFFFF => { - return self.interrupts; + self.interrupts } } } @@ -213,7 +213,7 @@ impl Memory { if address == 0xFF00 { return self.joypad.as_register(); } - return self.io[(address - 0xFF00) as usize]; + self.io[(address - 0xFF00) as usize] } fn set_io(&mut self, address: Address, data: u8) { @@ -252,11 +252,11 @@ impl Memory { } 0xFF44 | 0xFF76 | 0xFF77 => { // read-only addresses - println!("BANNED write: {:#X} to {:#X}", data, address); + println!("BANNED write: {data:#X} to {address:#X}"); } 0xFF46 => { if data > 0xDF { - panic!("dma transfer out of bounds: {:#X}", data); + panic!("dma transfer out of bounds: {data:#X}"); } self.io[addr_l] = data; let mut addr: u16 = 0x0; @@ -274,7 +274,7 @@ impl Memory { } pub fn update_pressed_keys(&mut self, keys: Vec) -> bool { - let old = self.joypad.clone(); + let old = self.joypad; self.joypad.down = keys.contains(&Key::Down) || keys.contains(&Key::S); self.joypad.up = keys.contains(&Key::Up) || keys.contains(&Key::W); self.joypad.left = keys.contains(&Key::Left) || keys.contains(&Key::A); diff --git a/src/processor/memory/rom/mbcs.rs b/src/processor/memory/rom/mbcs.rs index e399011..f59bb09 100644 --- a/src/processor/memory/rom/mbcs.rs +++ b/src/processor/memory/rom/mbcs.rs @@ -30,7 +30,6 @@ impl MBC for NONE { fn set_ram(&mut self, _address: Address, _data: u8) {} fn set(&mut self, _address: Address, _data: u8) { - return; } fn mbc_type(&self) -> &str { @@ -120,7 +119,7 @@ impl MBC for MBC1 { fn set_ram(&mut self, address: Address, data: u8) { let mut addr = self.get_ram_addr(address); if self.ram_enabled && let Some(ram) = &mut self.ram { - addr = addr % ram.len(); + addr %= ram.len(); ram[addr] = data; } } @@ -155,7 +154,6 @@ impl MBC for MBC1 { } _ => {} } - return; } fn mbc_type(&self) -> &str { diff --git a/src/processor/mod.rs b/src/processor/mod.rs index 7fd95b0..40a610e 100644 --- a/src/processor/mod.rs +++ b/src/processor/mod.rs @@ -96,7 +96,7 @@ impl CPU { fn next_opcode(&mut self) -> u8 { let opcode = self.memory.get(self.reg.pc); self.reg.pc = self.reg.pc.wrapping_add(0x1); - return opcode; + opcode } fn handle_interrupts(&mut self) -> u8 { diff --git a/src/processor/opcodes.rs b/src/processor/opcodes.rs index 2afbe0e..623ffd4 100644 --- a/src/processor/opcodes.rs +++ b/src/processor/opcodes.rs @@ -2442,5 +2442,5 @@ impl CPU { } fn undefined(opcode: u8) -> u8 { - panic!("Undefined behaviour: opcode {:#X}", opcode); + panic!("Undefined behaviour: opcode {opcode:#X}"); } diff --git a/src/processor/timer.rs b/src/processor/timer.rs index 6eaa30f..be2cca6 100644 --- a/src/processor/timer.rs +++ b/src/processor/timer.rs @@ -27,7 +27,7 @@ impl CPU { self.timers.div_counter += clock_cycles; let div_diff = (self.timers.div_counter / 256) as u8; - self.timers.div_counter = self.timers.div_counter % 256; + self.timers.div_counter %= 256; self.memory .set(0xFF04, self.memory.get(0xFF04).wrapping_add(div_diff)); @@ -35,7 +35,7 @@ impl CPU { if timer_enabled { self.timers.tima_counter += clock_cycles; let tima_diff = (self.timers.tima_counter / timer_rate) as u8; - self.timers.tima_counter = self.timers.tima_counter % timer_rate; + self.timers.tima_counter %= timer_rate; let (val, wrap) = self.memory.get(0xFF05).overflowing_add(tima_diff); if wrap { self.memory.set(0xFF05, self.memory.get(0xFF06)); diff --git a/src/util.rs b/src/util.rs index 7d555ba..98e8a25 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,14 +3,14 @@ use std::mem::transmute; pub(crate) fn as_signed(unsigned: u8) -> i8 { unsafe { - return transmute(unsigned); + transmute(unsigned) } } pub(crate) fn get_bit(byte: u8, flag: u8) -> bool { let mask = 1 << flag; let got = byte & mask; - return got > 0x0; + got > 0x0 } pub(crate) fn set_or_clear_bit(byte: u8, flag: u8, condition: bool) -> u8 { @@ -34,12 +34,12 @@ pub(crate) fn rotate(byte: u8, direction: &Direction) -> (u8, bool) { Direction::Left => { let carry = get_bit(byte, 7); let r = byte << 1; - return (r, carry); + (r, carry) } Direction::Right => { let carry = get_bit(byte, 0); let r = byte >> 1; - return (r, carry); + (r, carry) } } }