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 itertools::Itertools;
use nullable_result::{GeneralIterExt, NullableResult};
use crate::{
@ -11,6 +14,20 @@ pub struct Backplane {
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 {
pub fn new(cards: Vec<card::Config<'_>>) -> anyhow::Result<Self> {
if cards.len() > 255 {
@ -24,13 +41,17 @@ impl Backplane {
})
}
#[allow(dead_code)]
pub fn cards(&self) -> &[Box<dyn Card>] {
self.cards.as_ref()
pub fn reset(&mut self) {
for card in &mut self.cards {
card.reset();
}
}
pub fn cards_mut(&mut self) -> &mut Vec<Box<dyn Card>> {
&mut self.cards
pub fn card_cmd(&mut self, card_num: u8, cmd: &[&str]) -> anyhow::Result<()> {
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> {

View File

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