Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
onsdagens committed Aug 7, 2023
1 parent 25bdf2b commit d02fbef
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 43 deletions.
2 changes: 1 addition & 1 deletion riscv/examples/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() {
//init data memory with 0's
let range = Range {
start: 0x5000_0000u32,
end: 0x5000_1000u32,
end: 0x5000_0500u32,
};
for address in range.clone() {
data_mem.insert(address as usize, 0);
Expand Down
5 changes: 3 additions & 2 deletions riscv/src/components/instr_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ impl Component for InstrMem {
| (*self.bytes.get(&((pc + 3) as usize)).unwrap() as u32);
//the asm_riscv crate incorrectly panics when trying from instead of
//returning Err, catch it and handle instead
let instruction_fmt = panic::catch_unwind(|| format!("{:?}", asm_riscv::I::from(instr)))
.unwrap_or_else(|_| format!("Unknown instruction"));
let instruction_fmt =
panic::catch_unwind(|| format!("{:?}", asm_riscv::I::try_from(instr)))
.unwrap_or_else(|_| format!("Unknown instruction"));
trace!("instruction: {}", instruction_fmt);
trace!("pc:0x{:08x}", pc);
// set output
Expand Down
9 changes: 9 additions & 0 deletions src/components/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use log::*;
use num_enum::IntoPrimitive;
use num_enum::TryFromPrimitive;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use std::ops::Range;
use std::{cell::RefCell, collections::BTreeMap, convert::TryFrom, rc::Rc};

Expand Down Expand Up @@ -357,6 +358,14 @@ impl Component for Mem {
}
}

impl Deref for Memory {
type Target = RefCell<BTreeMap<usize, u8>>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
130 changes: 91 additions & 39 deletions src/gui_vizia/components/mem.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
common::Simulator,
components::{Mem, Memory},
gui_vizia::{ViziaComponent, V},
gui_vizia::{GuiData, ViziaComponent, V},
};
use log::*;
use vizia::prelude::*;
Expand All @@ -26,54 +27,65 @@ impl ViziaComponent for Mem {

fn left_view(&self, cx: &mut Context) {
trace!("---- Create Left Mem View");
//We initialize data_slice with the initial state of the memory.
//from now on, data_slice only gets updated over
//the relevant (visible) data interval, and when needed (so only on clock)
//so as to not trigger unneccessary redraws.
let data_slice = {
let mut data_slice = vec![];
let mem = self.memory.clone();
for idx in self.range.start as usize..self.range.end as usize {
data_slice.push(format!(
"0x{:08x}: {:02x}{:02x}{:02x}{:02x}",
idx,
mem.0.borrow().get(&idx).copied().unwrap_or_else(|| 0u8),
mem.0
.borrow()
.get(&(idx + 1))
.copied()
.unwrap_or_else(|| 0u8),
mem.0
.borrow()
.get(&(idx + 2))
.copied()
.unwrap_or_else(|| 0u8),
mem.0
.borrow()
.get(&(idx + 3))
.copied()
.unwrap_or_else(|| 0u8)
));
}
data_slice
};
View::build(
DataMemView {
data: self.memory.clone(),
start: self.range.start as usize,
data_slice: data_slice,
},
cx,
|cx| {
Label::new(cx, "Data Memory")
.left(Pixels(10.0))
.top(Pixels(10.0));

VirtualList::new(
cx,
DataMemView::data.map(|mem| {
let mut vec = vec![];
for i in mem.0.borrow().iter() {
if i.0 % 4 == 0 {
vec.push(format!(
"0x{:08x}: 0x{:02x}{:02x}{:02x}{:02x}",
i.0,
i.1,
mem.0
.borrow()
.get(&((i.0 + 1) as usize))
.copied()
.unwrap_or_else(|| 0u8),
mem.0
.borrow()
.get(&((i.0 + 2) as usize))
.copied()
.unwrap_or_else(|| 0u8),
mem.0
.borrow()
.get(&((i.0 + 3) as usize))
.copied()
.unwrap_or_else(|| 0u8)
));
}
}
vec
}),
20.0,
|cx, _, item| {
HStack::new(cx, |cx| {
Label::new(cx, item);
})
.child_left(Pixels(10.0))
},
);
VirtualList::new(cx, DataMemView::data_slice, 20.0, |cx, idx, item| {
HStack::new(cx, |cx| {
//if a value comes into view, update it with fresh data from memory
cx.emit(DataEvent::UpdateVal(idx));
Label::new(cx, item);
})
.child_left(Pixels(10.0))
.bind(
GuiData::simulator.then(Simulator::cycle),
move |mut view, _| {
trace!("Emitting idx {}", idx);
//on clock, update all values in view.
view.context().emit(DataEvent::UpdateVal(idx));
},
)
});
},
);
}
Expand All @@ -82,10 +94,50 @@ impl ViziaComponent for Mem {
#[derive(Lens, Clone)]
pub struct DataMemView {
data: Memory,
start: usize,
data_slice: Vec<String>,
}

pub enum DataEvent {
UpdateVal(usize),
}

impl View for DataMemView {
fn element(&self) -> Option<&'static str> {
Some("MemView")
}
fn event(&mut self, _cx: &mut EventContext, event: &mut Event) {
event.map(|event, _| match event {
DataEvent::UpdateVal(idx) => {
self.data_slice[*idx] = format!(
"0x{:08x}: {:02x}{:02x}{:02x}{:02x}",
idx,
self.data
.0
.borrow()
.get(&(self.start + idx * 4))
.copied()
.unwrap_or_else(|| 0u8),
self.data
.0
.borrow()
.get(&(self.start + idx * 4 + 1))
.copied()
.unwrap_or_else(|| 0u8),
self.data
.0
.borrow()
.get(&(self.start + idx * 4 + 2))
.copied()
.unwrap_or_else(|| 0u8),
self.data
.0
.borrow()
.get(&(self.start + idx * 4 + 3))
.copied()
.unwrap_or_else(|| 0u8)
);
}
})
}
}
2 changes: 1 addition & 1 deletion src/gui_vizia/components/mux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl View for MuxView {
fn draw(&self, cx: &mut DrawContext<'_>, canvas: &mut Canvas) {
let bounds = cx.bounds();
let scale = cx.scale_factor();
// trace!("Mux draw {:?}", bounds);
//println!("Mux draw {:?}", bounds);

let mut path = Path::new();
let mut paint = Paint::color(vizia::vg::Color::rgbf(0.0, 0.0, 0.0));
Expand Down

0 comments on commit d02fbef

Please sign in to comment.