emulator: applied some clippy lints

This commit is contained in:
2025-06-15 04:03:48 +01:00
parent a16f57c737
commit a240346a84
13 changed files with 357 additions and 288 deletions
+6 -5
View File
@@ -14,7 +14,8 @@ pub struct ControlPanel {
}
impl ControlPanel {
pub fn new(sender: Sender<Command>) -> Self {
#[must_use]
pub const fn new(sender: Sender<Command>) -> Self {
Self {
visible: false,
sender,
@@ -48,11 +49,11 @@ impl Component for ControlPanel {
{
if state.running == Running::Running {
self.sender.send(Command::Stop).unwrap_or_else(|_| {
state.error = Some("Failed to send command".to_string())
state.error = Some("Failed to send command".to_string());
});
} else {
self.sender.send(Command::Start).unwrap_or_else(|_| {
state.error = Some("Failed to send command".to_string())
state.error = Some("Failed to send command".to_string());
});
}
}
@@ -88,7 +89,7 @@ impl Component for ControlPanel {
}
}
fn render_register_table(state: &mut State, ui: &mut egui::Ui, _ctx: &egui::Context) {
fn render_register_table(state: &State, ui: &mut egui::Ui, _ctx: &egui::Context) {
// Left column - Registers
ui.vertical(|ui| {
ui.heading("Registers");
@@ -114,7 +115,7 @@ fn render_register_table(state: &mut State, ui: &mut egui::Ui, _ctx: &egui::Cont
// iterate over state.reg_file.iter() in chunks of 4 registers
for chunk in state.reg_file.all().chunks(4) {
for reg in chunk {
ui.label(format!("{}", reg.0));
ui.label(reg.0.to_string());
ui.label(format!("0x{:08X} ({})", reg.1, reg.1,));
}
ui.end_row();
+18 -23
View File
@@ -1,7 +1,4 @@
use crate::{
common::instructions::{Address, Interrupt},
emulator::system::model::{Command, Running, State},
};
use crate::emulator::system::model::{Command, Running, State};
use std::sync::mpsc::{Receiver, Sender};
pub trait Component {
@@ -11,7 +8,7 @@ pub trait Component {
fn category(&self) -> Category;
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Category {
Control,
Memory,
@@ -20,22 +17,19 @@ pub enum Category {
}
impl Category {
pub fn as_str(&self) -> &'static str {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Category::Control => "Control Systems",
Category::Memory => "Memory Systems",
Category::Io => "I/O Systems",
Category::Programming => "Programming",
Self::Control => "Control Systems",
Self::Memory => "Memory Systems",
Self::Io => "I/O Systems",
Self::Programming => "Programming",
}
}
pub fn list() -> Vec<Category> {
vec![
Category::Control,
Category::Memory,
Category::Io,
Category::Programming,
]
#[must_use]
pub fn list() -> Vec<Self> {
vec![Self::Control, Self::Memory, Self::Io, Self::Programming]
}
}
@@ -47,6 +41,7 @@ pub struct EmulatorUI {
}
impl EmulatorUI {
#[must_use]
pub fn new(sender: Sender<Command>, receiver: Receiver<State>) -> Self {
Self {
sender,
@@ -68,10 +63,10 @@ impl EmulatorUI {
}
impl eframe::App for EmulatorUI {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.update_state();
if let Running::Running = self.state.running {
if self.state.running == Running::Running {
ctx.request_repaint();
}
@@ -88,7 +83,7 @@ impl eframe::App for EmulatorUI {
});
egui::CentralPanel::default().show(ctx, |ui| {
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |_ui| {
egui::Window::new("Main Menu")
.resizable(false)
.default_width(300.0)
@@ -96,9 +91,9 @@ impl eframe::App for EmulatorUI {
super::menu::render_menu(self, ui, ctx);
});
for c in self.components.iter_mut() {
for c in &mut self.components {
let mut visible = *c.visible();
if visible == true {
if visible {
egui::Window::new(c.name())
.open(&mut visible)
.show(ctx, |ui| {
@@ -110,7 +105,7 @@ impl eframe::App for EmulatorUI {
});
});
egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| {
egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |_ui| {
// interface::bottompanel::render_bottom_panel(self, ui, ctx);
});
}
+25 -20
View File
@@ -14,7 +14,8 @@ pub struct MemoryInspector {
}
impl MemoryInspector {
pub fn new(sender: Sender<Command>) -> Self {
#[must_use]
pub const fn new(sender: Sender<Command>) -> Self {
Self {
view_size: 256,
view_addr: 0,
@@ -66,9 +67,12 @@ impl Component for MemoryInspector {
if search_clicked || enter_pressed {
if let Ok(new) = parse_address(&self.addr_input) {
self.view_addr = new;
self.sender
.send(Command::Read(new, self.view_size))
.unwrap();
if let Err(why) = self.sender.send(Command::Read(new, self.view_size)) {
panic!(
"Error sending message across threads -- cannot be recovered: {why}"
)
}
} else {
state.error = Some("Invalid address".to_string());
}
@@ -79,7 +83,7 @@ impl Component for MemoryInspector {
// Show input error if any
if let Some(error) = &state.error {
ui.colored_label(egui::Color32::RED, format!("Error: {}", error));
ui.colored_label(egui::Color32::RED, format!("Error: {error}"));
}
ui.add_space(10.0);
@@ -98,7 +102,7 @@ impl Component for MemoryInspector {
ui.strong("Address");
for i in 0..4 {
ui.strong(format!("{:X}", i));
ui.strong(format!("{i:X}"));
}
ui.strong("Decimal");
@@ -107,11 +111,11 @@ impl Component for MemoryInspector {
ui.end_row();
// Memory data (8 bytes per row)
for (row, chunk) in state.memory_view.chunks(4).enumerate() {
let row_address = self.view_addr + (row * 4) as u32;
ui.monospace(format!("0x{:08X} ({})", row_address, row_address));
for (row, chunk) in (0u32..).zip(state.memory_view.chunks(4)) {
let row_address = self.view_addr + (row * 4);
ui.monospace(format!("0x{row_address:08X} ({row_address})"));
for &byte in chunk {
ui.monospace(format!("{:02X}", byte));
ui.monospace(format!("{byte:02X}"));
}
// Fill remaining columns if last row is incomplete
@@ -120,10 +124,11 @@ impl Component for MemoryInspector {
}
// combine all 4 bytes in the chunk into a u32
let combined =
chunk.iter().fold(0u32, |acc, &byte| acc << 8 | byte as u32);
let combined = chunk
.iter()
.fold(0u32, |acc, &byte| acc << 8 | u32::from(byte));
ui.monospace(format!("{}", combined));
ui.monospace(format!("{combined}"));
// ui.monospace(format!("{:?}", Instruction::decode(combined)));
ui.monospace("TODO! instruction");
@@ -136,17 +141,17 @@ impl Component for MemoryInspector {
}
fn parse_address(address: &str) -> Result<u32, ParseIntError> {
if address.starts_with("0x") {
return u32::from_str_radix(&address[2..], 16);
if let Some(hex_part) = address.strip_prefix("0x") {
return u32::from_str_radix(hex_part, 16);
}
if address.starts_with("0b") {
return u32::from_str_radix(&address[2..], 2);
if let Some(bin_part) = address.strip_prefix("0b") {
return u32::from_str_radix(bin_part, 2);
}
if address.starts_with("0o") {
return u32::from_str_radix(&address[1..], 8);
if let Some(oct_part) = address.strip_prefix("0o") {
return u32::from_str_radix(oct_part, 8);
}
u32::from_str_radix(address, 10)
address.parse::<u32>()
}
+1 -1
View File
@@ -13,7 +13,7 @@ pub fn render_menu(state: &mut EmulatorUI, ui: &mut egui::Ui, _ctx: &egui::Conte
ui.heading(cat.as_str());
ui.add_space(10.0);
for comp in state.components.iter_mut() {
for comp in &mut state.components {
let name = comp.name();
if comp.category() == cat {
ui.toggle_value(comp.visible(), name);
+13 -9
View File
@@ -1,17 +1,21 @@
use crate::{
common::instructions::Register,
emulator::{
system::model::State,
ui::interface::{Component, EmulatorUI},
},
emulator::{system::model::State, ui::interface::Component},
};
pub struct StackInspector {
visible: bool,
}
impl Default for StackInspector {
fn default() -> Self {
Self::new()
}
}
impl StackInspector {
pub fn new() -> Self {
#[must_use]
pub const fn new() -> Self {
Self { visible: false }
}
}
@@ -29,7 +33,7 @@ impl Component for StackInspector {
super::interface::Category::Memory
}
fn render(&mut self, state: &mut State, ui: &mut egui::Ui, ctx: &egui::Context) {
fn render(&mut self, state: &mut State, ui: &mut egui::Ui, _ctx: &egui::Context) {
ui.vertical(|ui| {
ui.heading("Stack Inspector");
egui::ScrollArea::vertical()
@@ -44,13 +48,13 @@ impl Component for StackInspector {
ui.label("Value");
ui.end_row();
for (i, value) in state.stack_view.iter().take(32).enumerate() {
for (i, value) in (0u32..).zip(state.stack_view.iter().take(32)) {
ui.label(format!(
"{} [{}]",
i,
state.reg_file.get(Register::Spr) - i as u32 * 4
state.reg_file.get(Register::Spr) - i * 4
));
ui.label(format!("0x{:08X} ({})", value, value));
ui.label(format!("0x{value:08X} ({value})"));
ui.end_row();
}