Remove Backplane::{cards,cards_mut}

This commit is contained in:
pjht 2022-11-14 10:54:10 -06:00
parent 88d7d79bb3
commit 7656d04cd8
2 changed files with 36 additions and 33 deletions

View File

@ -1,4 +1,7 @@
use std::fmt::Display;
use anyhow::anyhow; use anyhow::anyhow;
use itertools::Itertools;
use nullable_result::{GeneralIterExt, NullableResult}; use nullable_result::{GeneralIterExt, NullableResult};
use crate::{ use crate::{
@ -11,6 +14,20 @@ pub struct Backplane {
cards: Vec<Box<dyn Card>>, cards: Vec<Box<dyn Card>>,
} }
impl Display for Backplane {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"{}",
self.cards
.iter()
.enumerate()
.format_with("\n", |(i, card), g| {
g(&format_args!("Card {i}: {card}"))
})
))
}
}
impl Backplane { impl Backplane {
pub fn new(cards: Vec<card::Config<'_>>) -> anyhow::Result<Self> { pub fn new(cards: Vec<card::Config<'_>>) -> anyhow::Result<Self> {
if cards.len() > 255 { if cards.len() > 255 {
@ -24,13 +41,17 @@ impl Backplane {
}) })
} }
#[allow(dead_code)] pub fn reset(&mut self) {
pub fn cards(&self) -> &[Box<dyn Card>] { for card in &mut self.cards {
self.cards.as_ref() card.reset();
}
} }
pub fn cards_mut(&mut self) -> &mut Vec<Box<dyn Card>> { pub fn card_cmd(&mut self, card_num: u8, cmd: &[&str]) -> anyhow::Result<()> {
&mut self.cards self.cards
.get_mut(card_num as usize)
.ok_or_else(|| anyhow!("Card {} does not exist", card_num))?
.cmd(cmd)
} }
pub fn read_word(&mut self, address: u32) -> Result<u16, BusError> { pub fn read_word(&mut self, address: u32) -> Result<u16, BusError> {

View File

@ -85,36 +85,20 @@ fn main() -> Result<(), anyhow::Error> {
.about("Send a command to a card"), .about("Send a command to a card"),
|args, state| { |args, state| {
let num = args.get_one::<String>("num").unwrap().parse::<u8>()?; let num = args.get_one::<String>("num").unwrap().parse::<u8>()?;
state state.cpu.bus_mut().card_cmd(
.cpu num,
.bus_mut() &args
.cards_mut() .get_many::<String>("cmd")
.get_mut(num as usize) .unwrap()
.ok_or_else(|| anyhow!("Card {} does not exist", num))? .map(String::as_str)
.cmd( .collect_vec(),
&args )?;
.get_many::<String>("cmd")
.unwrap()
.map(String::as_str)
.collect_vec(),
)?;
Ok(None) Ok(None)
}, },
) )
.with_command( .with_command(
Command::new("ls").about("List the cards in the system"), Command::new("ls").about("List the cards in the system"),
|_, state| { |_, state| Ok(Some(state.cpu.bus_mut().to_string())),
Ok(Some(
state
.cpu
.bus_mut()
.cards()
.iter()
.enumerate()
.map(|(i, card)| format!("Card {i}: {card}"))
.join("\n"),
))
},
) )
.with_command( .with_command(
Command::new("regs").about("Show CPU registers"), Command::new("regs").about("Show CPU registers"),
@ -209,9 +193,7 @@ fn main() -> Result<(), anyhow::Error> {
.with_command( .with_command(
Command::new("reset").about("Reset the cards and CPU, in that order"), Command::new("reset").about("Reset the cards and CPU, in that order"),
|_, state| { |_, state| {
for card in state.cpu.bus_mut().cards_mut() { state.cpu.bus_mut().reset();
card.reset();
}
state.cpu.reset(); state.cpu.reset();
Ok(None) Ok(None)
}, },