Skip to content

Commit

Permalink
add option to change the simulation speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Spydr06 committed Mar 19, 2023
1 parent 2a6321c commit fc8e087
Show file tree
Hide file tree
Showing 5 changed files with 2,215 additions and 14 deletions.
30 changes: 30 additions & 0 deletions content/circuit-panel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,36 @@
<attribute name="action">app.save-as</attribute>
</item>
</section>
<section>
<submenu>
<attribute name="label">S_imulation Speed</attribute>
<item>
<attribute name="label" translatable="yes">Halt</attribute>
<attribute name="action">app.change-tick-speed</attribute>
<attribute name="target" type="i">0</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Slow</attribute>
<attribute name="action">app.change-tick-speed</attribute>
<attribute name="target" type="i">1</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Default</attribute>
<attribute name="action">app.change-tick-speed</attribute>
<attribute name="target" type="i">10</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Fast</attribute>
<attribute name="action">app.change-tick-speed</attribute>
<attribute name="target" type="i">100</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Turbo</attribute>
<attribute name="action">app.change-tick-speed</attribute>
<attribute name="target" type="i">1000</attribute>
</item>
</submenu>
</section>
<section>
<submenu>
<attribute name="label">_Theme</attribute>
Expand Down
2,149 changes: 2,148 additions & 1 deletion examples/4-bit-adder.lrsproj

Large diffs are not rendered by default.

24 changes: 14 additions & 10 deletions src/application/gactions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::{fatal::*, project::Project};
use crate::{fatal::*, project::Project, simulator::Simulator};

const SYSTEM_PREFERENCE_THEME: u8 = 0;
const DARK_THEME: u8 = 1;
Expand Down Expand Up @@ -51,7 +51,7 @@ impl<'a> From<&GAction<'a>> for gio::SimpleAction {
}

lazy_static! {
pub(super) static ref ACTIONS: [GAction<'static>; 18] = [
pub(super) static ref ACTIONS: [GAction<'static>; 19] = [
GAction::new("quit", &["<primary>Q", "<primary>W"], None, None, Application::gaction_quit),
GAction::new("about", &["<primary>comma"], None, None, Application::gaction_about),
GAction::new("save", &["<primary>S"], None, None, Application::gaction_save),
Expand All @@ -70,6 +70,7 @@ lazy_static! {
GAction::new("edit-module", &[], Some(glib::VariantTy::STRING), None, Application::gaction_edit_module),
GAction::new("search-module", &["<primary>F"], None, None, Application::gaction_search_module),
GAction::new("change-theme", &[], None, Some((glib::VariantTy::BYTE, SYSTEM_PREFERENCE_THEME.to_variant())), Application::gaction_change_theme),
GAction::new("change-tick-speed", &[], None, Some((glib::VariantTy::INT32, Simulator::DEFAULT_TICKS_PER_SECOND.to_variant())), Application::gaction_change_tps)
];
}

Expand Down Expand Up @@ -185,15 +186,18 @@ impl Application {
action.set_state(&new.to_variant());
}

/*fn gaction_light_mode(self, _: Option<&glib::Variant>) {
adw::StyleManager::default()
.set_color_scheme(adw::ColorScheme::ForceLight)
}
fn gaction_change_tps(self, action: &gio::SimpleAction, parameter: Option<&glib::Variant>) {
let new = parameter
.expect("could not get theme parameter")
.get::<i32>()
.expect("the parameter needs to be of type `u8`");

fn gaction_system_preference_mode(self, _: Option<&glib::Variant>) {
adw::StyleManager::default()
.set_color_scheme(adw::ColorScheme::Default)
}*/
self.imp()
.project()
.lock().unwrap().set_tps(new);

action.set_state(&new.to_variant());
}

pub(super) fn close_current_file<F>(&self, after: F)
where
Expand Down
12 changes: 11 additions & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub type ProjectRef = Arc<Mutex<Project>>;
#[derive(Deserialize)]
pub struct Project {
modules: HashMap<String, Module>,
main_plot: Plot
main_plot: Plot,
tps: i32
}

impl Default for Project {
Expand Down Expand Up @@ -43,6 +44,7 @@ impl Project {
Self {
modules: modules.iter().map(|module| (module.name().to_owned(), module.clone())).collect(),
main_plot: Plot::new(),
tps: Simulator::DEFAULT_TICKS_PER_SECOND
}
}

Expand Down Expand Up @@ -138,4 +140,12 @@ impl Project {
.filter_map(|(_, module)| module.plot_mut())
.chain(std::iter::once(&mut self.main_plot))
}

pub fn tps(&self) -> i32 {
self.tps
}

pub fn set_tps(&mut self, tps: i32) {
self.tps = tps
}
}
14 changes: 12 additions & 2 deletions src/simulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct Simulator {
}

impl Simulator {
const TICKS_PER_SECOND: f64 = 10.0;
pub const DEFAULT_TICKS_PER_SECOND: i32 = 10;

pub fn new(project: ProjectRef, window: RefCell<Option<MainWindow>>) -> Self {
info!("starting simulation...");
Expand Down Expand Up @@ -100,11 +100,21 @@ impl Simulator {
}

fn schedule(running: Arc<AtomicBool>, project: ProjectRef, tx: Sender<UICallback>) {
let wait_time = Duration::from_secs_f64(1.0 / Self::TICKS_PER_SECOND);
while running.load(Ordering::Relaxed) {
let start = Instant::now();

let mut project = project.lock().unwrap();
let tps = project.tps();

// if we halt the simulation, check again in 0.5 seconds
if tps == 0 {
drop(project);
thread::sleep(Duration::from_millis(500));
continue;
}

let wait_time = Duration::from_secs_f64(1.0 / tps as f64);

Self::simulate(&mut project, &tx);
drop(project);

Expand Down

0 comments on commit fc8e087

Please sign in to comment.