Skip to content

Commit ec96e90

Browse files
committed
Change do_tick to take elapsed time since start instead of since the previous tick
1 parent 315ba90 commit ec96e90

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

src/bin/mlogv32.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ enum VMEvent {
143143
fn tui(stdout: TextContent, debug: TextContent, tx: Sender<VMCommand>, rx: Receiver<VMEvent>) {
144144
let mut siv = cursive::crossterm().into_runner();
145145

146-
siv.set_fps(10);
146+
siv.set_fps(20);
147147
siv.set_theme(Theme::terminal_default());
148148

149149
siv.add_fullscreen_layer(
@@ -219,7 +219,7 @@ fn tui(stdout: TextContent, debug: TextContent, tx: Sender<VMCommand>, rx: Recei
219219
}
220220
}
221221
}
222-
if last_state_time.elapsed().as_secs_f64() > (1. / 4.) {
222+
if last_state_time.elapsed().as_secs_f64() > (1. / 8.) {
223223
tx.send(VMCommand::GetState).unwrap();
224224
last_state_time = Instant::now();
225225
}
@@ -382,8 +382,10 @@ fn main() -> Result<(), Box<dyn Error>> {
382382

383383
println!("Initializing processors...");
384384

385+
let mut time = Duration::ZERO;
385386
for _ in 0..500 {
386-
vm.do_tick(Duration::from_secs_f64(1. / 60.));
387+
vm.do_tick(time);
388+
time += Duration::from_secs_f64(1. / 60.);
387389
}
388390

389391
// switch to TUI
@@ -433,16 +435,12 @@ fn main() -> Result<(), Box<dyn Error>> {
433435
prev_single_step = !*single_step;
434436
}
435437

436-
tui_println!(debug, "Starting.");
437-
438438
let mut ticks = 0;
439-
let mut now = Instant::now() - Duration::from_secs_f64(1. / 60.);
440439
let mut start = Instant::now();
441440

442441
loop {
443-
vm.do_tick(now.elapsed());
442+
vm.do_tick(start.elapsed());
444443
ticks += 1;
445-
now = Instant::now();
446444

447445
if let BuildingData::Switch(power) = &mut *power_switch.data.borrow_mut()
448446
&& let BuildingData::Switch(pause) = &mut *pause_switch.data.borrow_mut()
@@ -490,7 +488,9 @@ fn main() -> Result<(), Box<dyn Error>> {
490488
if *power != prev_power {
491489
tx_event.send(VMEvent::Power(*power))?;
492490
prev_power = *power;
493-
if !*power {
491+
if *power {
492+
tui_println!(debug, "Starting.");
493+
} else {
494494
let elapsed = start.elapsed();
495495
tui_println!(debug, "Processor halted.");
496496
if !error_output.is_empty() {

src/logic/vm/mod.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ impl LogicVM {
6161
/// Run the simulation until all processors halt, or until a number of ticks are finished.
6262
/// Returns true if all processors halted, or false if the tick limit was reached.
6363
pub fn run(&self, max_ticks: Option<usize>) -> bool {
64-
let mut now = Instant::now();
64+
let start = Instant::now();
6565
let mut tick = 0;
6666

6767
loop {
68-
self.do_tick(now.elapsed());
69-
now = Instant::now();
68+
self.do_tick(start.elapsed());
7069

7170
if self.running_processors.get() == 0 {
7271
// all processors finished, return true
@@ -85,10 +84,10 @@ impl LogicVM {
8584

8685
/// Execute one tick of the simulation.
8786
///
88-
/// Note: negative delta values will be ignored.
89-
pub fn do_tick(&self, delta: Duration) {
87+
/// Note: `time` is the time elapsed since the *start* of the simulation.
88+
pub fn do_tick(&self, time: Duration) {
9089
// never move time backwards
91-
let time = self.time.get() + duration_millis_f64(delta).max(0.);
90+
let time = duration_millis_f64(time);
9291
self.time.set(time);
9392

9493
for processor in self.iter_processors() {
@@ -1839,25 +1838,29 @@ mod tests {
18391838
",
18401839
);
18411840

1842-
vm.do_tick(Duration::ZERO);
1841+
let mut time = Duration::ZERO;
1842+
vm.do_tick(time);
18431843

18441844
with_processor(&mut vm, (0, 0), |p| {
18451845
assert_eq!(p.state.decode_printbuffer(), "123");
18461846
});
18471847

1848-
vm.do_tick(Duration::from_secs_f64(1. / 60.));
1848+
time += Duration::from_secs_f64(1. / 60.);
1849+
vm.do_tick(time);
18491850

18501851
with_processor(&mut vm, (0, 0), |p| {
18511852
assert_eq!(p.state.decode_printbuffer(), "1234");
18521853
});
18531854

1854-
vm.do_tick(Duration::from_millis(500));
1855+
time += Duration::from_millis(500);
1856+
vm.do_tick(time);
18551857

18561858
with_processor(&mut vm, (0, 0), |p| {
18571859
assert_eq!(p.state.decode_printbuffer(), "1234");
18581860
});
18591861

1860-
vm.do_tick(Duration::from_millis(500));
1862+
time += Duration::from_millis(500);
1863+
vm.do_tick(time);
18611864

18621865
with_processor(&mut vm, (0, 0), |p| {
18631866
assert_eq!(p.state.decode_printbuffer(), "12345");
@@ -2584,17 +2587,22 @@ mod tests {
25842587
",
25852588
);
25862589

2587-
vm.do_tick(Duration::ZERO);
2588-
vm.do_tick(Duration::ZERO);
2590+
let mut time = Duration::ZERO;
25892591

2590-
vm.do_tick(Duration::from_secs(1));
2591-
vm.do_tick(Duration::ZERO);
2592+
vm.do_tick(time);
2593+
vm.do_tick(time);
25922594

2593-
vm.do_tick(Duration::from_millis(1));
2594-
vm.do_tick(Duration::ZERO);
2595+
time += Duration::from_secs(1);
2596+
vm.do_tick(time);
2597+
vm.do_tick(time);
25952598

2596-
vm.do_tick(Duration::from_secs(60));
2597-
vm.do_tick(Duration::ZERO);
2599+
time += Duration::from_millis(1);
2600+
vm.do_tick(time);
2601+
vm.do_tick(time);
2602+
2603+
time += Duration::from_secs(60);
2604+
vm.do_tick(time);
2605+
vm.do_tick(time);
25982606

25992607
let processor = take_processor(&mut vm, (0, 0));
26002608
assert_variables_epsilon(

0 commit comments

Comments
 (0)