Skip to content

Commit

Permalink
Merge pull request #84 from perlindgren/is_active
Browse files Browse the repository at this point in the history
Is active
  • Loading branch information
onsdagens authored Sep 9, 2024
2 parents 7df8d51 + 637e169 commit c7cc820
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 18 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

Tracking changes per date:

## 240909

- Added un-clock history for set of active components

## 240908

- Added functionality to determine set of active components

## 230801

- Simulator run/halt is implemented in `vizia` using a simple eventing mechanism. Later we might want to spawn a simulation thread for faster execution (right now its tied to frame rate).
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typetag = "0.2.10"

[dependencies.vizia]
git = "https://github.com/vizia/vizia.git"
rev = "7093bfd518c4bee5544a75c2ffc92dfe4f817bc0"
#path = "../vizia"
optional = true

Expand Down
111 changes: 111 additions & 0 deletions examples/mux2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::path::PathBuf;
#[cfg(feature = "gui-egui")]
use syncrim::gui_egui::editor::Library;
use syncrim::{
common::{ComponentStore, Input},
components::*,
fern::fern_setup,
};
fn main() {
fern_setup();
let cs = ComponentStore {
store: vec![
Mux::rc_new(
"mux",
(200.0, 200.0),
Input::new("ctrl", "out"),
vec![
Input::new("c1", "out"),
Input::new("c2", "out"),
Input::new("c3", "out"),
Input::new("c4", "out"),
],
),
Mux::rc_new(
"mux2",
(300.0, 300.0),
Input::new("mux", "out"),
vec![
Input::new("c1_2", "out"),
Input::new("c2_2", "out"),
Input::new("c3_2", "out"),
Input::new("c4_2", "out"),
],
),
ProbeEdit::rc_new("ctrl", (190.0, 100.0)),
Wire::rc_new(
"w0",
vec![(190.0, 110.0), (190.0, 150.0)],
Input::new("ctrl", "out"),
),
Constant::rc_new("c1", (140.0, 170.0), 0),
Constant::rc_new("c2", (140.0, 190.0), 1),
Constant::rc_new("c3", (140.0, 210.0), 2),
Constant::rc_new("c4", (140.0, 230.0), 3),
Constant::rc_new("c1_2", (140.0, 270.0), 20),
Constant::rc_new("c2_2", (140.0, 290.0), 21),
Constant::rc_new("c3_2", (140.0, 310.0), 22),
Constant::rc_new("c4_2", (140.0, 330.0), 23),
Wire::rc_new(
"w1",
vec![(150.0, 170.0), (180.0, 170.0)],
Input::new("c1", "out"),
),
Wire::rc_new(
"w2",
vec![(150.0, 190.0), (180.0, 190.0)],
Input::new("c2", "out"),
),
Wire::rc_new(
"w3",
vec![(150.0, 210.0), (180.0, 210.0)],
Input::new("c3", "out"),
),
Wire::rc_new(
"w4",
vec![(150.0, 230.0), (180.0, 230.0)],
Input::new("c4", "out"),
),
Wire::rc_new(
"w1_2",
vec![(150.0, 270.0), (280.0, 270.0)],
Input::new("c1_2", "out"),
),
Wire::rc_new(
"w2_2",
vec![(150.0, 290.0), (280.0, 290.0)],
Input::new("c2_2", "out"),
),
Wire::rc_new(
"w3_2",
vec![(150.0, 310.0), (280.0, 310.0)],
Input::new("c3_2", "out"),
),
Wire::rc_new(
"w4_2",
vec![(150.0, 330.0), (280.0, 330.0)],
Input::new("c4_2", "out"),
),
Wire::rc_new(
"w5",
vec![(210.0, 200.0), (290.0, 200.0), (290.0, 250.0)],
Input::new("mux", "out"),
),
Wire::rc_new(
"w6",
vec![(310.0, 300.0), (350.0, 300.0)],
Input::new("mux", "out"),
),
Probe::rc_new("p_mux", (360.0, 300.0), Input::new("mux2", "out")),
],
};

let path = PathBuf::from("mux2.json");
cs.save_file(&path);

#[cfg(feature = "gui-egui")]
syncrim::gui_egui::gui(cs, &path, Library::default()).ok();

#[cfg(feature = "gui-vizia")]
syncrim::gui_vizia::gui(cs, &path);
}
121 changes: 121 additions & 0 deletions examples/mux3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use std::path::PathBuf;
#[cfg(feature = "gui-egui")]
use syncrim::gui_egui::editor::Library;
use syncrim::{
common::{ComponentStore, Input},
components::*,
fern::fern_setup,
};
fn main() {
fern_setup();
let cs = ComponentStore {
store: vec![
Mux::rc_new(
"mux",
(200.0, 200.0),
Input::new("ctrl", "out"),
vec![
Input::new("c1", "out"),
Input::new("c2", "out"),
Input::new("c3", "out"),
Input::new("c4", "out"),
],
),
Mux::rc_new(
"mux2",
(300.0, 300.0),
Input::new("ctrl2", "out"),
vec![
Input::new("mux", "out"),
Input::new("c2_2", "out"),
Input::new("c3_2", "out"),
Input::new("c4_2", "out"),
],
),
ProbeEdit::rc_new("ctrl", (190.0, 100.0)),
ProbeEdit::rc_new("ctrl2", (290.0, 200.0)),
Wire::rc_new(
"w0",
vec![(190.0, 110.0), (190.0, 150.0)],
Input::new("ctrl", "out"),
),
Wire::rc_new(
"w0_2",
vec![(290.0, 210.0), (290.0, 250.0)],
Input::new("ctrl2", "out"),
),
Constant::rc_new("c1", (140.0, 170.0), 0),
Constant::rc_new("c2", (140.0, 190.0), 1),
Constant::rc_new("c3", (140.0, 210.0), 2),
Constant::rc_new("c4", (140.0, 230.0), 3),
Constant::rc_new("c2_2", (140.0, 290.0), 21),
Constant::rc_new("c3_2", (140.0, 310.0), 22),
Constant::rc_new("c4_2", (140.0, 330.0), 23),
Wire::rc_new(
"w1",
vec![(150.0, 170.0), (180.0, 170.0)],
Input::new("c1", "out"),
),
Wire::rc_new(
"w2",
vec![(150.0, 190.0), (180.0, 190.0)],
Input::new("c2", "out"),
),
Wire::rc_new(
"w3",
vec![(150.0, 210.0), (180.0, 210.0)],
Input::new("c3", "out"),
),
Wire::rc_new(
"w4",
vec![(150.0, 230.0), (180.0, 230.0)],
Input::new("c4", "out"),
),
// Wire::rc_new(
// "w1_2",
// vec![(150.0, 270.0), (280.0, 270.0)],
// Input::new("c1_2", "out"),
// ),
Wire::rc_new(
"w2_2",
vec![(150.0, 290.0), (280.0, 290.0)],
Input::new("c2_2", "out"),
),
Wire::rc_new(
"w3_2",
vec![(150.0, 310.0), (280.0, 310.0)],
Input::new("c3_2", "out"),
),
Wire::rc_new(
"w4_2",
vec![(150.0, 330.0), (280.0, 330.0)],
Input::new("c4_2", "out"),
),
Wire::rc_new(
"w5",
vec![
(210.0, 200.0),
(230.0, 200.0),
(230.0, 270.0),
(280.0, 270.0),
],
Input::new("mux", "out"),
),
Wire::rc_new(
"w6",
vec![(310.0, 300.0), (350.0, 300.0)],
Input::new("mux2", "out"),
),
Probe::rc_new("probe_mux", (360.0, 300.0), Input::new("mux2", "out")),
],
};

let path = PathBuf::from("mux2.json");
cs.save_file(&path);

#[cfg(feature = "gui-egui")]
syncrim::gui_egui::gui(cs, &path, Library::default()).ok();

#[cfg(feature = "gui-vizia")]
syncrim::gui_vizia::gui(cs, &path);
}
21 changes: 18 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use petgraph::Graph;
use serde::{Deserialize, Serialize};
use std::any::Any;
use std::{collections::HashMap, rc::Rc};
use std::{
collections::{HashMap, HashSet},
rc::Rc,
};

#[cfg(feature = "gui-egui")]
use crate::gui_egui::editor::{EditorMode, EditorRenderReturn, GridOptions, SnapPriority};
Expand Down Expand Up @@ -34,11 +37,16 @@ pub struct Simulator {
pub sim_state: Vec<Signal>,
pub id_nr_outputs: IdNrOutputs,
pub id_field_index: IdFieldIndex,
pub history: Vec<Vec<Signal>>,
pub history: Vec<(Vec<Signal>, HashSet<Id>)>,
pub component_ids: Vec<Id>,
pub graph: Graph<Id, ()>,
// Running state, (do we need it accessible from other crates?)
pub(crate) running: bool,

// Used to determine active components
pub sinks: Vec<Id>,
pub inputs_read: HashMap<Id, HashSet<Id>>,
pub active: HashSet<Id>,
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -82,6 +90,13 @@ pub trait Component {
fn un_clock(&self) {}
/// reset component internal state to initial value
fn reset(&self) {}

/// consider component to be a sink
/// either output to environment (e.g., for visualization)
/// or stateful (e.g., register)
fn is_sink(&self) -> bool {
false
}
/// any
fn as_any(&self) -> &dyn Any;
}
Expand Down Expand Up @@ -177,7 +192,7 @@ impl Ports {
}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug, Hash, Eq, PartialEq)]
pub struct Input {
pub id: Id,
pub field: Id,
Expand Down
4 changes: 2 additions & 2 deletions src/components/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ impl Component for Add {
// propagate addition to output
fn clock(&self, simulator: &mut Simulator) -> Result<(), Condition> {
// get input values
let a_in = u32::try_from(simulator.get_input_value(&self.a_in));
let b_in = u32::try_from(simulator.get_input_value(&self.b_in));
let a_in = u32::try_from(simulator.get_input_value_mut(self.id.clone(), &self.a_in));
let b_in = u32::try_from(simulator.get_input_value_mut(self.id.clone(), &self.b_in));

let (value, overflow, res) = match (&a_in, &b_in) {
(Ok(a), Ok(b)) => {
Expand Down
7 changes: 5 additions & 2 deletions src/components/mux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ impl Component for Mux {
// propagate selected input value to output
fn clock(&self, simulator: &mut Simulator) -> Result<(), Condition> {
// get input value
let select: SignalValue = simulator.get_input_value(&self.select);
let select: SignalValue = simulator.get_input_value_mut(self.id.clone(), &self.select);
trace!("-----------{}------------", self.id);
let (value, res) = if let Ok(select) = TryInto::<SignalUnsigned>::try_into(select) {
let select = select as usize;
trace!("select {}", select);
if select < self.m_in.len() {
(simulator.get_input_value(&self.m_in[select]), Ok(()))
(
simulator.get_input_value_mut(self.id.clone(), &self.m_in[select]),
Ok(()),
)
} else {
(
SignalValue::Unknown,
Expand Down
13 changes: 12 additions & 1 deletion src/components/probe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "gui-egui")]
use crate::common::EguiComponent;
use crate::common::{Component, Id, Input, InputPort, OutputType, Ports};
use crate::common::{Component, Condition, Id, Input, InputPort, OutputType, Ports, Simulator};
use log::*;
use serde::{Deserialize, Serialize};
use std::any::Any;
Expand Down Expand Up @@ -47,12 +47,23 @@ impl Component for Probe {
)
}

fn clock(&self, simulator: &mut Simulator) -> Result<(), Condition> {
// get input value
let value = simulator.get_input_value_mut(self.id.clone(), &self.input);
trace!("probe: register id {} in {:?}", self.id, value);
Ok(())
}

fn set_id_port(&mut self, target_port_id: Id, new_input: Input) {
if target_port_id.as_str() == PROBE_IN_ID {
self.input = new_input
}
}

fn is_sink(&self) -> bool {
true
}

fn as_any(&self) -> &dyn Any {
self
}
Expand Down
6 changes: 5 additions & 1 deletion src/components/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Component for Register {
// propagate input value to output
fn clock(&self, simulator: &mut Simulator) -> Result<(), Condition> {
// get input value
let value = simulator.get_input_value(&self.r_in);
let value = simulator.get_input_value_mut(self.id.clone(), &self.r_in);
// set output
simulator.set_out_value(&self.id, "out", value);
trace!("eval: register id {} in {:?}", self.id, value);
Expand All @@ -62,6 +62,10 @@ impl Component for Register {
}
}

fn is_sink(&self) -> bool {
true
}

fn as_any(&self) -> &dyn Any {
self
}
Expand Down
Loading

0 comments on commit c7cc820

Please sign in to comment.