fix some lints

This commit is contained in:
Matthias 2024-04-22 16:16:19 +02:00
parent bdad29af7d
commit 7579cd5d24
5 changed files with 47 additions and 12 deletions

View File

@ -49,6 +49,7 @@ where
}
impl<M: Bus, V: Variant> CPU<M, V> {
#[allow(clippy::needless_pass_by_value)]
pub fn new(memory: M, _variant: V) -> CPU<M, V> {
CPU {
registers: Registers::new(),
@ -58,10 +59,23 @@ impl<M: Bus, V: Variant> CPU<M, V> {
}
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<DecodedInstr> {
// Helper function to read a 16-bit address from memory
fn read_address<M: Bus>(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<M: Bus, V: Variant> CPU<M, V> {
let memory = &mut self.memory;
fn read_address<M: Bus>(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<M: Bus, V: Variant> CPU<M, V> {
}
}
#[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<M: Bus, V: Variant> CPU<M, V> {
}
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 {

View File

@ -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,

View File

@ -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")]

View File

@ -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],
}

View File

@ -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])
}