Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions crates/celox-runtime/src/simulation/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,24 @@ impl<B: SimBackend> SimulationState<B> {
for (term, baseline) in terms.iter().zip(&mut waiter.baselines) {
match term.counter {
Some(counter) => {
*baseline = Baseline::Counter {
seen: backend.get_as(counter),
value: read(backend, term.signal),
let seen = backend.get_as(counter);
match planes(backend, term.signal) {
Some(image) => {
if let Baseline::RawCounter { seen: last, raw } = baseline {
*last = seen;
copy_planes(raw, image, term.signal.width);
} else {
let mut raw = Vec::with_capacity(image.len());
copy_planes(&mut raw, image, term.signal.width);
*baseline = Baseline::RawCounter { seen, raw };
}
}
None => {
*baseline = Baseline::Counter {
seen,
value: read(backend, term.signal),
}
}
}
}
None => match planes(backend, term.signal) {
Expand Down Expand Up @@ -128,20 +143,40 @@ impl<B: SimBackend> SimulationState<B> {
continue;
}
let baseline = &mut waiter.baselines[index];
// The common term: a plain signal no counter counts.
if let Baseline::Raw(last) = baseline {
let now = state.planes(term.signal).expect("a plain signal");
wake |= Self::advance(term.kind, last, now, term.signal.width);
continue;
}
let counted = |seen: u32| {
term.counter
.is_some_and(|counter| backend.get_as::<u32>(counter) != seen)
};
wake |= match baseline {
Baseline::Counter { seen, value } => {
let counted = term
.counter
.is_some_and(|counter| backend.get_as::<u32>(counter) != *seen);
let now = read(backend, term.signal);
let changed = occurs(term.kind, value, &now);
*value = now;
counted || changed
}
Baseline::Raw(last) => {
Baseline::RawCounter { seen, raw } => {
let now = state.planes(term.signal).expect("a plain signal");
Self::advance(term.kind, last, now, term.signal.width)
let changed = Self::advance(term.kind, raw, now, term.signal.width);
counted(*seen) || changed
}
Baseline::Counter { seen, value } => match state.planes(term.signal) {
// A baseline loaded from a state file.
Some(now) => {
let seen = *seen;
let mut raw = encode_planes(value, term.signal.width, now.len());
let changed =
Self::advance(term.kind, &mut raw, now, term.signal.width);
*baseline = Baseline::RawCounter { seen, raw };
counted(seen) || changed
}
None => {
let now = read(backend, term.signal);
let changed = occurs(term.kind, value, &now);
*value = now;
counted(*seen) || changed
}
},
Baseline::Raw(_) => unreachable!("tested above"),
Baseline::Value(last) => match state.planes(term.signal) {
// A baseline loaded from a state file.
Some(now) => {
Expand Down Expand Up @@ -191,7 +226,7 @@ impl<B: SimBackend> SimulationState<B> {
}
match baseline {
// Counters are bumped by the write itself.
Baseline::Counter { .. } => {}
Baseline::Counter { .. } | Baseline::RawCounter { .. } => {}
Baseline::Raw(last) => {
let now = encode_planes(value, signal.width, last.len());
wake |= Self::advance(term.kind, last, &now, signal.width);
Expand Down Expand Up @@ -224,6 +259,9 @@ pub enum Baseline {
/// (see [`planes`]) with the bits above its width cleared. Checks
/// compare it with the state in place.
Raw(Vec<u8>),
/// Like `Counter`, with the signal's state at the last check kept as
/// in [`Baseline::Raw`].
RawCounter { seen: u32, raw: Vec<u8> },
}

/// A process suspended at a wait.
Expand Down
26 changes: 19 additions & 7 deletions crates/celox/src/simulator/state_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,13 @@ impl<B: SimBackend> Simulator<B> {
.collect()
})
.unwrap_or_default();
let split = |term: usize, image: &[u8]| {
let bytes = widths
.get(term)
.map_or(image.len(), |width| width.div_ceil(8).max(1))
.min(image.len());
(image[..bytes].to_vec(), image[bytes..].to_vec())
};
WaiterRecord {
process: process(index),
site: waiter.site as u64,
Expand All @@ -554,13 +561,18 @@ impl<B: SimBackend> Simulator<B> {
mask: mask.to_bytes_le(),
},
Baseline::Raw(image) => {
let bytes = widths
.get(term)
.map_or(image.len(), |width| width.div_ceil(8).max(1))
.min(image.len());
BaselineRecord::Value {
value: image[..bytes].to_vec(),
mask: image[bytes..].to_vec(),
let (value, mask) = split(term, image);
BaselineRecord::Value { value, mask }
}
Baseline::RawCounter { seen, raw } => {
// The bytes a `Counter` baseline's value saves as.
let (value, mask) = split(term, raw);
let number =
|bytes: &[u8]| BigUint::from_bytes_le(bytes).to_bytes_le();
BaselineRecord::Counter {
seen: *seen,
value: number(&value),
mask: number(&mask),
}
}
})
Expand Down
85 changes: 85 additions & 0 deletions crates/celox/tests/frontends/systemverilog/timing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,88 @@ fn runtime_error_before_a_commit_leaves_no_held_writes() {
}
}
}

/// The baselines of waits on a counted variable survive a state file: a
/// simulation loaded between two pulses of `y` wakes the waiters as the
/// uninterrupted one does, and saves the same baselines again.
#[test]
fn counted_wait_baselines_survive_a_state_file() {
let source = r#"
module Top(output int n, r);
timeunit 1ns / 1ns;
bit a, b, c;
logic y;
always_comb begin y = 0; if (a) y = 1; c = b; end
initial #1 forever begin @(y); n++; end
initial #1 forever begin @(posedge y); r++; end
initial begin
#2 a = 1;
#1 b = 1;
#1 b = 0;
#1 b = 1;
#1 a = 0;
end
endmodule
"#;
let build = |four_state: bool| {
celox::Simulation::from_sv_sources(
vec![(source, std::path::Path::new("counted_state.sv"))],
"Top",
)
.four_state(four_state)
.build()
.unwrap()
};
let through_bytes = |file: &celox::StateFile| {
let mut bytes = Vec::new();
file.write_to(&mut bytes).unwrap();
celox::StateFile::read_from(bytes.as_slice()).unwrap()
};
for four_state in [false, true] {
let counts = |sim: &mut celox::Simulation| {
["n", "r"].map(|name| {
let signal = sim.signal(name);
sim.get(signal)
})
};
let mut whole = build(four_state);
whole.run_until(10).unwrap();
let expected = counts(&mut whole);
assert_eq!(
expected,
[5u32.into(), 4u32.into()],
"four_state={four_state}"
);

let mut first = build(four_state);
first.run_until(3).unwrap();
let file = through_bytes(&first.save_state().unwrap());
assert!(
file.schedule
.as_ref()
.unwrap()
.processes
.waiters
.iter()
.flat_map(|waiter| &waiter.baselines)
.any(|baseline| matches!(baseline, celox::BaselineRecord::Counter { .. })),
"four_state={four_state}: `y` is counted"
);
let mut loaded = build(four_state);
loaded.load_state(&file).unwrap();
assert_eq!(
through_bytes(&loaded.save_state().unwrap()),
file,
"four_state={four_state}"
);
loaded.run_until(10).unwrap();
assert_eq!(counts(&mut loaded), expected, "four_state={four_state}");
// A baseline taken at a check in this run saves as the loaded one.
first.run_until(4).unwrap();
loaded = build(four_state);
let file = through_bytes(&first.save_state().unwrap());
loaded.load_state(&file).unwrap();
loaded.run_until(10).unwrap();
assert_eq!(counts(&mut loaded), expected, "four_state={four_state}");
}
}
Loading