diff --git a/assembler/src/command_metadata.rs b/assembler/src/command_metadata.rs index bc75d68..a52dc4b 100644 --- a/assembler/src/command_metadata.rs +++ b/assembler/src/command_metadata.rs @@ -97,7 +97,7 @@ impl SourceCommandMetadata { opcode: Opcode::HALT, argument_type: Argument::parse_none, }), - _ => return Err(ParsingError::UnknownCommand(opcode.to_owned())), + _ => Err(ParsingError::UnknownCommand(opcode.to_owned())), } } } diff --git a/assembler/src/compiler.rs b/assembler/src/compiler.rs index 1108e67..daff744 100644 --- a/assembler/src/compiler.rs +++ b/assembler/src/compiler.rs @@ -28,7 +28,7 @@ struct AddressIterator { } impl<'a> AddressIterator> { - fn new(items: &'a Vec) -> Self { + fn new(items: &'a [SourceCodeItem]) -> Self { Self { items: items.iter(), next_address: 0, @@ -72,7 +72,7 @@ impl<'a, T: Iterator> Iterator for AddressIterator } impl ParsedProgram { - fn addresses(items: &Vec) -> AddressIterator> { + fn addresses(items: &[SourceCodeItem]) -> AddressIterator> { AddressIterator::new(items) } @@ -114,8 +114,8 @@ impl ParsedProgram { continue; } CompilerDirective::Data(data) => data - .into_iter() - .map(|&byte| MemoryItem::Data(byte as u32)) + .iter() + .map(|&machine_word| MemoryItem::Data(machine_word)) .for_each(|item| current_section.items.push(item)), CompilerDirective::Pointer(label) => { current_section.items.push(MemoryItem::Data( diff --git a/assembler/src/parser.rs b/assembler/src/parser.rs index 2b4d40b..78b1d83 100644 --- a/assembler/src/parser.rs +++ b/assembler/src/parser.rs @@ -39,7 +39,7 @@ impl ParsedProgram { } self.labels.insert(label, self.items.len()); - return Ok(()); + Ok(()) } } @@ -106,5 +106,5 @@ fn process_line(line: &str, program: &mut ParsedProgram) -> Result<(), ParsingEr let command = SourceCodeCommand::from_token_stream(&mut tokens)?; program.items.push(SourceCodeItem::Command(command)); - return Ok(()); + Ok(()) } diff --git a/assembler/src/parser/address.rs b/assembler/src/parser/address.rs index 1978b29..8864e29 100644 --- a/assembler/src/parser/address.rs +++ b/assembler/src/parser/address.rs @@ -25,7 +25,7 @@ impl AddressWithMode { return Ok(AddressingMode::Indirect); } - return Ok(AddressingMode::Relative); + Ok(AddressingMode::Relative) } fn parse_address(stream: &mut TokenStream) -> Result { diff --git a/assembler/src/parser/compiler_directive.rs b/assembler/src/parser/compiler_directive.rs index 67da283..0bf071c 100644 --- a/assembler/src/parser/compiler_directive.rs +++ b/assembler/src/parser/compiler_directive.rs @@ -16,7 +16,7 @@ impl CompilerDirective { } while let Ok(number) = stream.next_long_number() { - data.push(number as u32); + data.push(number); } // ensure that no unparsed input left diff --git a/assembler/src/parser/token.rs b/assembler/src/parser/token.rs index 5b9ccb8..45eb31f 100644 --- a/assembler/src/parser/token.rs +++ b/assembler/src/parser/token.rs @@ -76,7 +76,7 @@ impl FromStr for TokenStream { .tokens .push(Token::Word(found.as_str().to_owned())); - assert!(found.len() > 0, "match should be NON-empty"); + assert!(!found.is_empty(), "match should be NON-empty"); index += found.len(); continue; } @@ -173,15 +173,14 @@ impl TokenStream { } pub fn next_end_of_input(&mut self) -> Result<(), TokenStreamError> { - let output = match self.peek(1) { - Ok(Token::EndOfInput) => (), - Ok(token) => return Err(TokenStreamError::UnexpectedToken(token.clone())), - Err(err) => return Err(err), - }; - - self.advance_cursor(); - - Ok(output) + match self.peek(1) { + Ok(Token::EndOfInput) => { + self.advance_cursor(); + Ok(()) + }, + Ok(token) => Err(TokenStreamError::UnexpectedToken(token.clone())), + Err(err) => Err(err), + } } } @@ -195,7 +194,7 @@ fn parse_number(input: &str) -> Option<(T, usize)> { let prefix = parsed.name("prefix").map_or("", |matched| matched.as_str()); let value = &parsed["number"]; - let value = value.replace("_", ""); + let value = value.replace('_', ""); let radix = match prefix { "0x" => 16, "0b" => 2, diff --git a/cli_utils/src/lib.rs b/cli_utils/src/lib.rs index eac539c..8ba551a 100644 --- a/cli_utils/src/lib.rs +++ b/cli_utils/src/lib.rs @@ -1,8 +1,8 @@ use std::{error::Error, fmt::Display, path::PathBuf}; -pub fn check_empty_arguments(args: &Vec) -> Result<(), ConfigurationError> { +pub fn check_empty_arguments(args: &[String]) -> Result<(), ConfigurationError> { for (index, arg) in args.iter().enumerate() { - if arg.len() < 1 { + if arg.is_empty() { return Err(ConfigurationError::EmptyArgument(index)); } } diff --git a/cpu/src/cpu.rs b/cpu/src/cpu.rs index 76b79c2..615497e 100644 --- a/cpu/src/cpu.rs +++ b/cpu/src/cpu.rs @@ -18,7 +18,7 @@ type MicrocodeStorage = Vec; type MicroInstructionCounter = usize; type TRegisterValue = u32; -pub struct CPU { +pub struct Cpu { io_controller: IOController, registers: Registers, status: Status, @@ -27,7 +27,7 @@ pub struct CPU { microcode_program_counter: MicroInstructionCounter, } -impl Debug for CPU { +impl Debug for Cpu { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("CPU") .field("io_controller", &self.io_controller) @@ -38,17 +38,16 @@ impl Debug for CPU { } } -impl Display for CPU { +impl Display for Cpu { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "Registers:")?; - writeln!(f, "{}", self.registers.to_string())?; + writeln!(f, "{}", self.registers)?; writeln!(f, "Status: {}", self.status)?; writeln!(f, "MC Counter: {}", self.microcode_program_counter) } } - -impl CPU { +impl Cpu { pub fn new(memory: Memory, io_controller: IOController) -> Self { Self { io_controller, @@ -91,13 +90,11 @@ impl CPU { let left = if micro_instruction.contains(&Signal::ZERO_LEFT) { 0 + } else if micro_instruction.contains(&Signal::SELECT_PC) { + // no sign extension happens + self.registers.program_counter as u32 } else { - if micro_instruction.contains(&Signal::SELECT_PC) { - // no sign extension happens - self.registers.program_counter as u32 - } else { - self.registers.accumulator - } + self.registers.accumulator }; let right_0 = micro_instruction.contains(&Signal::SELECT_RIGHT_DATA) as u8; @@ -142,38 +139,27 @@ impl CPU { } } - if micro_instruction - .get(&Signal::WRITE_PROGRAM_COUNTER) - .is_some() - { + if micro_instruction.contains(&Signal::WRITE_PROGRAM_COUNTER) { self.registers.program_counter = alu_output.value as RawAddress; } - let invert_flags = micro_instruction - .get(&Signal::WRITE_PROGRAM_COUNTER_CLEAR) - .is_some(); + let invert_flags = micro_instruction.contains(&Signal::WRITE_PROGRAM_COUNTER_CLEAR); // Z invert write // 0 0 0 // 1 0 1 // 1 1 0 // 0 1 1 - if micro_instruction - .get(&Signal::WRITE_PROGRAM_COUNTER_Z) - .is_some() + if micro_instruction.contains(&Signal::WRITE_PROGRAM_COUNTER_Z) + && self.status.zero != invert_flags { - if self.status.zero != invert_flags { - self.registers.program_counter = alu_output.value as RawAddress; - } + self.registers.program_counter = alu_output.value as RawAddress; } - if micro_instruction - .get(&Signal::WRITE_PROGRAM_COUNTER_C) - .is_some() + if micro_instruction.contains(&Signal::WRITE_PROGRAM_COUNTER_C) + && self.status.carry != invert_flags { - if self.status.carry != invert_flags { - self.registers.program_counter = alu_output.value as RawAddress; - } + self.registers.program_counter = alu_output.value as RawAddress; } if micro_instruction.contains(&Signal::WRITE_COMMAND) { @@ -218,7 +204,11 @@ impl CPU { micro_instructions_executed += 1; } - log::info!("Instructions: {}; MC: {}", instructions_executed, micro_instructions_executed); + log::info!( + "Instructions: {}; MC: {}", + instructions_executed, + micro_instructions_executed + ); } fn opcode_to_mc(opcode: Opcode) -> MicroInstructionCounter { diff --git a/cpu/src/cpu/control_unit.rs b/cpu/src/cpu/control_unit.rs index 65a9f49..26f36e6 100644 --- a/cpu/src/cpu/control_unit.rs +++ b/cpu/src/cpu/control_unit.rs @@ -2,7 +2,9 @@ use std::collections::HashSet; use super::MicrocodeStorage; -#[allow(non_camel_case_types)] +// this is how I want to call signals. I strongly believe that +// signal names should be uppercase as they are constants to me +#[allow(non_camel_case_types, clippy::upper_case_acronyms)] #[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)] pub enum Signal { // enables io diff --git a/cpu/src/io_controller.rs b/cpu/src/io_controller.rs index f66c186..89575f0 100644 --- a/cpu/src/io_controller.rs +++ b/cpu/src/io_controller.rs @@ -50,7 +50,7 @@ impl Device for SimpleInputOutput { } fn write_to_device(&mut self, payload: u8) { - std::io::stdout().write(&[payload]).unwrap(); + std::io::stdout().write_all(&[payload]).unwrap(); } } @@ -79,8 +79,7 @@ impl IOController { } pub fn write(&mut self, device_address: RawPort, payload: u8) { - self.devices - .get_mut(&device_address) - .map(|device| device.write_to_device(payload)); + if let Some(device) = self.devices + .get_mut(&device_address) { device.write_to_device(payload) } } } diff --git a/cpu/src/main.rs b/cpu/src/main.rs index 3afadfb..fb8adb4 100644 --- a/cpu/src/main.rs +++ b/cpu/src/main.rs @@ -10,7 +10,7 @@ use io_controller::{IOController, SimpleInputOutput}; use isa::CompiledProgram; use memory::Memory; -use crate::cpu::CPU; +use crate::cpu::Cpu; mod cpu; mod io_controller; @@ -48,7 +48,7 @@ fn start() -> Result<(), Box> { let memory = Memory::burn(program); let io_controller = IOController::new().connect(0, Box::new(SimpleInputOutput::new(output))); - let cpu = CPU::new(memory, io_controller); + let cpu = Cpu::new(memory, io_controller); cpu.start(); Ok(()) diff --git a/isa/src/lib.rs b/isa/src/lib.rs index 24c7ab9..ddc68ad 100644 --- a/isa/src/lib.rs +++ b/isa/src/lib.rs @@ -105,7 +105,7 @@ impl Display for MemoryItem { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { MemoryItem::Data(data) => write!(f, "Data({})", data), - MemoryItem::Command(command) => write!(f, "Command: {}", command.to_string()), + MemoryItem::Command(command) => write!(f, "Command: {}", command), } } }