clippy suggestions

This commit is contained in:
Alex Janka 2023-02-12 09:41:34 +11:00
parent 45a1aae05f
commit cee17032bd
11 changed files with 41 additions and 45 deletions

View file

@ -18,7 +18,7 @@ use std::{
#[macro_export] #[macro_export]
macro_rules! verbose_println { macro_rules! verbose_println {
($($tts:tt)*) => { ($($tts:tt)*) => {
if crate::is_verbose() { if $crate::is_verbose() {
println!($($tts)*); println!($($tts)*);
} }
}; };
@ -27,7 +27,7 @@ macro_rules! verbose_println {
#[macro_export] #[macro_export]
macro_rules! verbose_print { macro_rules! verbose_print {
($($tts:tt)*) => { ($($tts:tt)*) => {
if crate::is_verbose() { if $crate::is_verbose() {
print!($($tts)*); print!($($tts)*);
} }
}; };
@ -149,10 +149,8 @@ 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();
if !pause_enabled { if !pause_enabled && cpu.reg.pc >= 0x100 {
if cpu.reg.pc >= 0x100 { unsafe { PAUSE_ENABLED = true };
unsafe { PAUSE_ENABLED = true };
}
} }
if will_pause { if will_pause {
pause_then_step(); pause_then_step();

View file

@ -349,9 +349,9 @@ impl CPU {
if x_coord < WIDTH { if x_coord < WIDTH {
let buffer_index = (scanline as usize * WIDTH) + x_coord; let buffer_index = (scanline as usize * WIDTH) + x_coord;
if !object.flags.behind_bg_and_window 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 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( let tile_addr = tiledata.get_addr(
self.memory self.memory
.get(tilemap.get_addr(row_addr + (tilemap_column))), .get(tilemap.get_addr(row_addr + (tilemap_column))),
@ -396,7 +396,7 @@ impl CPU {
let msb = get_bit(msbs, 7 - tile_px_x); let msb = get_bit(msbs, 7 - tile_px_x);
let colour = bits_to_mapped_colour(lsb, msb, palette); 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<u32>, width: usize, height: usize, factor: usize) -> Vec<u32> { fn scale_buffer(buffer: &[u32], width: usize, height: usize, factor: usize) -> Vec<u32> {
let mut v = vec![]; let mut v = vec![];
for y in 0..height { for y in 0..height {
for _ in 0..factor { for _ in 0..factor {

View file

@ -86,7 +86,7 @@ impl TileWindow {
let colour = bits_to_mapped_colour(lsb, msb, palette); let colour = bits_to_mapped_colour(lsb, msb, palette);
self.sprite_buffer[real_px_x + (real_px_y * TILE_WINDOW_WIDTH)] = self.sprite_buffer[real_px_x + (real_px_y * TILE_WINDOW_WIDTH)] =
colour.to_rgb(); colour.as_rgb();
} }
} }
} }

View file

@ -74,7 +74,7 @@ pub(super) enum Colour {
} }
impl Colour { impl Colour {
pub(super) fn to_rgb(&self) -> u32 { pub(super) fn as_rgb(&self) -> u32 {
match self { match self {
Colour::White => Self::from_u8_rgb(0xFF, 0xFF, 0xFF), Colour::White => Self::from_u8_rgb(0xFF, 0xFF, 0xFF),
Colour::LightGray => Self::from_u8_rgb(0xAA, 0xAA, 0xAA), Colour::LightGray => Self::from_u8_rgb(0xAA, 0xAA, 0xAA),

View file

@ -41,7 +41,7 @@ impl CPU {
self.set_or_clear_flag(Flags::Zero, rotated == 0x0); self.set_or_clear_flag(Flags::Zero, rotated == 0x0);
self.clear_flag(Flags::HalfCarry); self.clear_flag(Flags::HalfCarry);
self.clear_flag(Flags::NSubtract); self.clear_flag(Flags::NSubtract);
return rotated; rotated
} }
pub(crate) fn rotate(&mut self, byte: u8, direction: Direction) -> u8 { 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.set_or_clear_flag(Flags::Zero, rotated == 0x0);
self.clear_flag(Flags::HalfCarry); self.clear_flag(Flags::HalfCarry);
self.clear_flag(Flags::NSubtract); self.clear_flag(Flags::NSubtract);
return rotated; rotated
} }
pub(crate) fn shift(&mut self, byte: u8, direction: Direction) -> u8 { 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.set_or_clear_flag(Flags::Zero, rotated == 0x0);
self.clear_flag(Flags::HalfCarry); self.clear_flag(Flags::HalfCarry);
self.clear_flag(Flags::NSubtract); self.clear_flag(Flags::NSubtract);
return rotated; rotated
} }
pub(crate) fn get_flag(&mut self, flag: Flags) -> u8 { pub(crate) fn get_flag(&mut self, flag: Flags) -> u8 {
@ -113,7 +113,7 @@ impl CPU {
& 0x1000) & 0x1000)
== 0x1000, == 0x1000,
); );
return result; result
} }
pub(crate) fn add_u8s(&mut self, first: u8, second: u8) -> u8 { pub(crate) fn add_u8s(&mut self, first: u8, second: u8) -> u8 {
@ -125,7 +125,7 @@ impl CPU {
Flags::HalfCarry, Flags::HalfCarry,
(((first & 0xF).wrapping_add(second & 0xF)) & 0x10) == 0x10, (((first & 0xF).wrapping_add(second & 0xF)) & 0x10) == 0x10,
); );
return result; result
} }
pub(crate) fn add_u16s(&mut self, first: u16, second: u16) -> u16 { pub(crate) fn add_u16s(&mut self, first: u16, second: u16) -> u16 {
@ -137,7 +137,7 @@ impl CPU {
Flags::HalfCarry, Flags::HalfCarry,
(((first & 0xFFF).wrapping_add(second & 0xFFF)) & 0x1000) == 0x1000, (((first & 0xFFF).wrapping_add(second & 0xFFF)) & 0x1000) == 0x1000,
); );
return result; result
} }
pub(crate) fn sub_u8s(&mut self, first: u8, second: u8) -> u8 { pub(crate) fn sub_u8s(&mut self, first: u8, second: u8) -> u8 {
@ -149,7 +149,7 @@ impl CPU {
Flags::HalfCarry, Flags::HalfCarry,
(((first & 0xF).wrapping_sub(second & 0xF)) & 0x10) == 0x10, (((first & 0xF).wrapping_sub(second & 0xF)) & 0x10) == 0x10,
); );
return result; result
} }
pub(crate) fn dec_raw(&mut self, val: u8) -> u8 { pub(crate) fn dec_raw(&mut self, val: u8) -> u8 {

View file

@ -128,40 +128,40 @@ impl Memory {
// rom access // rom access
// todo - switchable rom banks // todo - switchable rom banks
if self.bootrom_enabled && ((address as usize) < self.bootrom.len()) { if self.bootrom_enabled && ((address as usize) < self.bootrom.len()) {
return self.bootrom[address as usize]; self.bootrom[address as usize]
} else { } else {
return self.rom.get(address); self.rom.get(address)
} }
} }
0x8000..0xA000 => { 0x8000..0xA000 => {
return 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 => {
return self.ram[(address - 0xC000) as usize]; self.ram[(address - 0xC000) as usize]
} }
0xE000..0xFE00 => { 0xE000..0xFE00 => {
return self.ram[(address - 0xE000) as usize]; self.ram[(address - 0xE000) as usize]
} }
0xFE00..0xFEA0 => { 0xFE00..0xFEA0 => {
return self.oam[(address - 0xFE00) as usize]; self.oam[(address - 0xFE00) as usize]
} }
0xFEA0..0xFF00 => { 0xFEA0..0xFF00 => {
return 0xFF; 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");
return 0xFF; 0xFF
} }
0xFF80..0xFFFF => { 0xFF80..0xFFFF => {
return self.cpu_ram[(address - 0xFF80) as usize]; self.cpu_ram[(address - 0xFF80) as usize]
} }
0xFFFF => { 0xFFFF => {
return self.interrupts; self.interrupts
} }
} }
} }
@ -213,7 +213,7 @@ impl Memory {
if address == 0xFF00 { if address == 0xFF00 {
return self.joypad.as_register(); 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) { fn set_io(&mut self, address: Address, data: u8) {
@ -252,11 +252,11 @@ impl Memory {
} }
0xFF44 | 0xFF76 | 0xFF77 => { 0xFF44 | 0xFF76 | 0xFF77 => {
// read-only addresses // read-only addresses
println!("BANNED write: {:#X} to {:#X}", data, address); println!("BANNED write: {data:#X} to {address:#X}");
} }
0xFF46 => { 0xFF46 => {
if data > 0xDF { if data > 0xDF {
panic!("dma transfer out of bounds: {:#X}", data); panic!("dma transfer out of bounds: {data:#X}");
} }
self.io[addr_l] = data; self.io[addr_l] = data;
let mut addr: u16 = 0x0; let mut addr: u16 = 0x0;
@ -274,7 +274,7 @@ impl Memory {
} }
pub fn update_pressed_keys(&mut self, keys: Vec<Key>) -> bool { pub fn update_pressed_keys(&mut self, keys: Vec<Key>) -> bool {
let old = self.joypad.clone(); let old = self.joypad;
self.joypad.down = keys.contains(&Key::Down) || keys.contains(&Key::S); self.joypad.down = keys.contains(&Key::Down) || keys.contains(&Key::S);
self.joypad.up = keys.contains(&Key::Up) || keys.contains(&Key::W); self.joypad.up = keys.contains(&Key::Up) || keys.contains(&Key::W);
self.joypad.left = keys.contains(&Key::Left) || keys.contains(&Key::A); self.joypad.left = keys.contains(&Key::Left) || keys.contains(&Key::A);

View file

@ -30,7 +30,6 @@ 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) {
return;
} }
fn mbc_type(&self) -> &str { fn mbc_type(&self) -> &str {
@ -120,7 +119,7 @@ impl MBC for MBC1 {
fn set_ram(&mut self, address: Address, data: u8) { fn set_ram(&mut self, address: Address, data: u8) {
let mut addr = self.get_ram_addr(address); let mut addr = self.get_ram_addr(address);
if self.ram_enabled && let Some(ram) = &mut self.ram { if self.ram_enabled && let Some(ram) = &mut self.ram {
addr = addr % ram.len(); addr %= ram.len();
ram[addr] = data; ram[addr] = data;
} }
} }
@ -155,7 +154,6 @@ impl MBC for MBC1 {
} }
_ => {} _ => {}
} }
return;
} }
fn mbc_type(&self) -> &str { fn mbc_type(&self) -> &str {

View file

@ -96,7 +96,7 @@ impl CPU {
fn next_opcode(&mut self) -> u8 { fn next_opcode(&mut self) -> u8 {
let opcode = self.memory.get(self.reg.pc); let opcode = self.memory.get(self.reg.pc);
self.reg.pc = self.reg.pc.wrapping_add(0x1); self.reg.pc = self.reg.pc.wrapping_add(0x1);
return opcode; opcode
} }
fn handle_interrupts(&mut self) -> u8 { fn handle_interrupts(&mut self) -> u8 {

View file

@ -2442,5 +2442,5 @@ impl CPU {
} }
fn undefined(opcode: u8) -> u8 { fn undefined(opcode: u8) -> u8 {
panic!("Undefined behaviour: opcode {:#X}", opcode); panic!("Undefined behaviour: opcode {opcode:#X}");
} }

View file

@ -27,7 +27,7 @@ impl CPU {
self.timers.div_counter += clock_cycles; self.timers.div_counter += clock_cycles;
let div_diff = (self.timers.div_counter / 256) as u8; 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 self.memory
.set(0xFF04, self.memory.get(0xFF04).wrapping_add(div_diff)); .set(0xFF04, self.memory.get(0xFF04).wrapping_add(div_diff));
@ -35,7 +35,7 @@ impl CPU {
if timer_enabled { if timer_enabled {
self.timers.tima_counter += clock_cycles; self.timers.tima_counter += clock_cycles;
let tima_diff = (self.timers.tima_counter / timer_rate) as u8; 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); let (val, wrap) = self.memory.get(0xFF05).overflowing_add(tima_diff);
if wrap { if wrap {
self.memory.set(0xFF05, self.memory.get(0xFF06)); self.memory.set(0xFF05, self.memory.get(0xFF06));

View file

@ -3,14 +3,14 @@ use std::mem::transmute;
pub(crate) fn as_signed(unsigned: u8) -> i8 { pub(crate) fn as_signed(unsigned: u8) -> i8 {
unsafe { unsafe {
return transmute(unsigned); transmute(unsigned)
} }
} }
pub(crate) fn get_bit(byte: u8, flag: u8) -> bool { pub(crate) fn get_bit(byte: u8, flag: u8) -> bool {
let mask = 1 << flag; let mask = 1 << flag;
let got = byte & mask; let got = byte & mask;
return got > 0x0; got > 0x0
} }
pub(crate) fn set_or_clear_bit(byte: u8, flag: u8, condition: bool) -> u8 { 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 => { Direction::Left => {
let carry = get_bit(byte, 7); let carry = get_bit(byte, 7);
let r = byte << 1; let r = byte << 1;
return (r, carry); (r, carry)
} }
Direction::Right => { Direction::Right => {
let carry = get_bit(byte, 0); let carry = get_bit(byte, 0);
let r = byte >> 1; let r = byte >> 1;
return (r, carry); (r, carry)
} }
} }
} }