From 7579cd5d249181699e66e9cac0751953b50c22cf Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 22 Apr 2024 16:16:19 +0200 Subject: [PATCH] fix some lints --- src/cpu.rs | 25 +++++++++++++++++-------- src/instruction.rs | 3 ++- src/lib.rs | 5 +++++ src/memory.rs | 21 ++++++++++++++++++++- src/registers.rs | 5 +++-- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index 5480bc6..f78bf7d 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -49,6 +49,7 @@ where } impl CPU { + #[allow(clippy::needless_pass_by_value)] pub fn new(memory: M, _variant: V) -> CPU { CPU { registers: Registers::new(), @@ -58,10 +59,23 @@ impl CPU { } pub fn reset(&mut self) { - //TODO: // should read some bytes from the stack and also get the PC from the reset vector + //TODO: should read some bytes from the stack and also get the PC from the reset vector } + /// Get the next byte from memory and decode it into an instruction and addressing mode. + /// + /// # Panics + /// + /// This function will panic if the instruction is not recognized + /// (i.e. the opcode is invalid or has not been implemented). pub fn fetch_next_and_decode(&mut self) -> Option { + // Helper function to read a 16-bit address from memory + fn read_address(mem: &mut M, addr: u16) -> [u8; 2] { + let lo = mem.get_byte(addr); + let hi = mem.get_byte(addr.wrapping_add(1)); + [lo, hi] + } + let x: u8 = self.memory.get_byte(self.registers.program_counter); match V::decode(x) { @@ -89,12 +103,6 @@ impl CPU { let memory = &mut self.memory; - fn read_address(mem: &mut M, addr: u16) -> [u8; 2] { - let lo = mem.get_byte(addr); - let hi = mem.get_byte(addr.wrapping_add(1)); - [lo, hi] - } - let am_out = match am { AddressingMode::Accumulator | AddressingMode::Implied => { // Always the same -- no input @@ -185,6 +193,7 @@ impl CPU { } } + #[allow(clippy::too_many_lines)] pub fn execute_instruction(&mut self, decoded_instr: DecodedInstr) { match decoded_instr { (Instruction::ADC, OpInput::UseImmediate(val)) => { @@ -720,7 +729,7 @@ impl CPU { } fn add_with_carry(&mut self, value: u8) { - fn decimal_adjust(result: u8) -> u8 { + const fn decimal_adjust(result: u8) -> u8 { let bcd1: u8 = if (result & 0x0f) > 0x09 { 0x06 } else { 0x00 }; let bcd2: u8 = if (result.wrapping_add(bcd1) & 0xf0) > 0x90 { diff --git a/src/instruction.rs b/src/instruction.rs index f92edb0..6bbddf4 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -136,7 +136,7 @@ pub enum AddressingMode { impl AddressingMode { #[must_use] - pub fn extra_bytes(self) -> u16 { + pub const fn extra_bytes(self) -> u16 { match self { AddressingMode::Accumulator => 0, AddressingMode::Implied => 0, @@ -460,6 +460,7 @@ pub struct RevisionA; impl crate::Variant for RevisionA { fn decode(opcode: u8) -> Option<(Instruction, AddressingMode)> { + #[allow(clippy::match_same_arms)] match opcode { 0x66 => None, 0x6a => None, diff --git a/src/lib.rs b/src/lib.rs index 9f67933..e58110c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,11 @@ )] #![deny(anonymous_parameters, macro_use_extern_crate, pointer_structural_match)] #![allow(clippy::module_name_repetitions)] +// Registers and ops follow the 6502 naming convention and have similar names at +// times +#![allow(clippy::similar_names)] +#![allow(clippy::match_same_arms)] +#![allow(clippy::too_many_lines)] #![no_std] #[doc = include_str!("../README.md")] diff --git a/src/memory.rs b/src/memory.rs index 6f09814..db83cc3 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -58,10 +58,29 @@ impl Default for Memory { } } +/// Trait for a bus that can read and write bytes. +/// +/// This is used to abstract the memory and I/O operations of the CPU. +/// +/// # Examples +/// +/// ``` +/// use mos6502::memory::{Bus, Memory}; +/// +/// let mut memory = Memory::new(); +/// memory.set_byte(0x0000, 0x12); +/// assert_eq!(memory.get_byte(0x0000), 0x12); +/// ``` pub trait Bus { + /// Returns the byte at the given address. fn get_byte(&mut self, address: u16) -> u8; + + /// Sets the byte at the given address to the given value. fn set_byte(&mut self, address: u16, value: u8); + /// Sets the bytes starting at the given address to the given values. + /// + /// This is a default implementation that calls `set_byte` for each byte. fn set_bytes(&mut self, start: u16, values: &[u8]) { for i in 0..values.len() as u16 { self.set_byte(start + i, values[i as usize]); @@ -71,7 +90,7 @@ pub trait Bus { impl Memory { #[must_use] - pub fn new() -> Memory { + pub const fn new() -> Memory { Memory { bytes: [0; MEMORY_SIZE], } diff --git a/src/registers.rs b/src/registers.rs index 89f7864..827d0ae 100644 --- a/src/registers.rs +++ b/src/registers.rs @@ -29,6 +29,7 @@ use bitflags::bitflags; // Useful for constructing Status instances #[derive(Copy, Clone, Debug)] +#[allow(clippy::struct_excessive_bools)] pub struct StatusArgs { pub negative: bool, pub overflow: bool, @@ -42,7 +43,7 @@ pub struct StatusArgs { impl StatusArgs { #[must_use] - pub fn none() -> StatusArgs { + pub const fn none() -> StatusArgs { StatusArgs { negative: false, overflow: false, @@ -151,7 +152,7 @@ pub struct StackPointer(pub u8); impl StackPointer { #[must_use] - pub fn to_u16(self) -> u16 { + pub const fn to_u16(self) -> u16 { let StackPointer(val) = self; u16::from_le_bytes([val, 0x01]) }