Skip to content
62 changes: 55 additions & 7 deletions crates/celox-design/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,19 @@ pub struct RuntimeSchema<A> {

/// Width of [`SchedulerSlots::limit`], [`SchedulerSlots::now`],
/// [`SchedulerSlots::exit_arg`], [`SchedulerSlots::count`],
/// [`SchedulerSlots::current`], [`SchedulerSlots::levels`] and every [`SchedulerSlots::wake`] object.
/// [`SchedulerSlots::current`], [`SchedulerSlots::levels`],
/// [`SchedulerSlots::ready`], [`SchedulerSlots::clock_value`],
/// [`SchedulerSlots::clock_armed`] and every
/// [`SchedulerSlots::wake`] object, and of each element of
/// [`SchedulerSlots::clock_next`] and [`SchedulerSlots::clock_period`].
pub const SCHEDULER_TIME_WIDTH: usize = 64;
/// The most host periodic clocks a compiled scheduler runs: one element of
/// [`SchedulerSlots::clock_next`] and [`SchedulerSlots::clock_period`], and
/// one bit of the clock masks, per entry of [`SchedulerSchema::clocks`].
pub const SCHEDULER_CLOCKS: usize = 64;
/// Width of [`SchedulerSlots::exit`], [`SchedulerSlots::finish`],
/// [`SchedulerSlots::cont`] and every [`SchedulerSlots::waiting`] object.
/// [`SchedulerSlots::cont`], [`SchedulerSlots::dirty`] and every [`SchedulerSlots::waiting`] and
/// [`SchedulerSlots::baseline`] object.
pub const SCHEDULER_FLAG_WIDTH: usize = 8;

/// Hidden state objects through which a compiled scheduler and the runtime
Expand All @@ -904,18 +913,35 @@ pub struct SchedulerSlots<A> {
pub finish: A,
/// Where a shared combinational settle continues.
pub cont: A,
/// Set when state combinational logic reads may have changed since its
/// last settle.
pub dirty: A,
/// The number of time slots the run ran.
pub count: A,
/// The kernel running when the run returned, `u64::MAX` for none.
pub current: A,
/// The levels of the event signals at the start of the last slot, one
/// bit per event in event order.
pub levels: A,
/// The kernels ready to run in the current slot, one bit per kernel in
/// kernel order.
pub ready: A,
/// The time each process kernel resumes at, in kernel order, valid
/// where [`Self::waiting`] is set.
pub wake: Vec<A>,
/// Whether each process kernel waits for its wake time.
/// Whether each process kernel waits for its wake time or event.
pub waiting: Vec<A>,
/// The level a kernel waiting for an event last saw its signal at.
pub baseline: Vec<A>,
/// The time of each host clock's next edge, one element per entry of
/// [`SchedulerSchema::clocks`], valid where [`Self::clock_armed`] is set.
pub clock_next: A,
/// The period of each host clock, one element per clock.
pub clock_period: A,
/// The level each host clock's next edge sets, one bit per clock.
pub clock_value: A,
/// Whether each host clock has a next edge, one bit per clock.
pub clock_armed: A,
}

impl<A> SchedulerSlots<A> {
Expand All @@ -927,11 +953,18 @@ impl<A> SchedulerSlots<A> {
exit_arg: map(self.exit_arg),
finish: map(self.finish),
cont: map(self.cont),
dirty: map(self.dirty),
count: map(self.count),
current: map(self.current),
levels: map(self.levels),
ready: map(self.ready),
wake: self.wake.into_iter().map(&mut map).collect(),
waiting: self.waiting.into_iter().map(map).collect(),
waiting: self.waiting.into_iter().map(&mut map).collect(),
baseline: self.baseline.into_iter().map(&mut map).collect(),
clock_next: map(self.clock_next),
clock_period: map(self.clock_period),
clock_value: map(self.clock_value),
clock_armed: map(self.clock_armed),
}
}

Expand All @@ -944,13 +977,20 @@ impl<A> SchedulerSlots<A> {
&self.exit_arg,
&self.finish,
&self.cont,
&self.dirty,
&self.count,
&self.current,
&self.levels,
&self.ready,
&self.clock_next,
&self.clock_period,
&self.clock_value,
&self.clock_armed,
]
.into_iter()
.chain(&self.wake)
.chain(&self.waiting)
.chain(&self.baseline)
}
}

Expand All @@ -967,6 +1007,9 @@ pub enum SchedulerExit {
DelayOverflow = 2,
/// A process suspended in a way the scheduler does not compile.
Internal = 3,
/// The next edge of host clock [`SchedulerSlots::exit_arg`] overflows
/// the simulation time.
ClockOverflow = 4,
}

impl SchedulerExit {
Expand All @@ -976,6 +1019,7 @@ impl SchedulerExit {
1 => Some(Self::Finish),
2 => Some(Self::DelayOverflow),
3 => Some(Self::Internal),
4 => Some(Self::ClockOverflow),
_ => None,
}
}
Expand All @@ -992,9 +1036,12 @@ pub struct SchedulerSchema<A> {
/// Per process kernel, why the scheduler does not run it, or `None` when
/// it does.
pub fallback: Vec<Option<String>>,
/// The event signals the scheduler's kernels write, in the order of
/// the bits of [`SchedulerSlots::levels`].
/// The event signals the scheduler's kernels write or its host clocks
/// may drive, in the order of the bits of [`SchedulerSlots::levels`].
pub events: Vec<A>,
/// The event signals a host periodic clock may drive inside the
/// scheduler's slots, in the order of its clock slots.
pub clocks: Vec<A>,
}

impl<A> SchedulerSchema<A> {
Expand All @@ -1004,7 +1051,8 @@ impl<A> SchedulerSchema<A> {
slots: self.slots.map(&mut map),
unit: self.unit,
fallback: self.fallback,
events: self.events.into_iter().map(map).collect(),
events: self.events.into_iter().map(&mut map).collect(),
clocks: self.clocks.into_iter().map(map).collect(),
}
}
}
Expand Down
22 changes: 16 additions & 6 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,7 @@ pub fn schedule_symbolic_rtl_reporting_loops(
unit: None,
fallback: Vec::new(),
events: Vec::new(),
clocks: Vec::new(),
});
// The runtime reads counters, the signals waits watch and staged
// nonblocking values directly; no unit loads a wait's signal.
Expand Down Expand Up @@ -2218,16 +2219,14 @@ pub fn schedule_symbolic_rtl_reporting_loops(
}

/// Declare the hidden objects a compiled scheduler exchanges with the
/// runtime (`RuntimeSchema::scheduler`) as variables of the top module,
/// when the design has `process_count` process kernels.
/// runtime (`RuntimeSchema::scheduler`) as variables of the top module, for
/// a design with `process_count` process kernels. A design without kernels
/// has them too: host clocks may run in a compiled scheduler.
fn declare_scheduler_slots(
modules: &mut HashMap<ModuleId, SimModule>,
root: ModuleId,
process_count: usize,
) -> Option<celox_design::SchedulerSlots<SourceVarId>> {
if process_count == 0 {
return None;
}
let variables = &mut modules.get_mut(&root)?.variables;
let mut next = variables.keys().map(|id| id.0 + 1).max().unwrap_or(0);
let mut declare = |name: String, width: usize| {
Expand Down Expand Up @@ -2255,23 +2254,34 @@ fn declare_scheduler_slots(
);
id
};
use celox_design::{SCHEDULER_FLAG_WIDTH as FLAG, SCHEDULER_TIME_WIDTH as TIME};
use celox_design::{
SCHEDULER_CLOCKS as CLOCKS, SCHEDULER_FLAG_WIDTH as FLAG, SCHEDULER_TIME_WIDTH as TIME,
};
Some(celox_design::SchedulerSlots {
limit: declare("limit".into(), TIME),
now: declare("now".into(), TIME),
exit: declare("exit".into(), FLAG),
exit_arg: declare("exit_arg".into(), TIME),
finish: declare("finish".into(), FLAG),
cont: declare("cont".into(), FLAG),
dirty: declare("dirty".into(), FLAG),
count: declare("count".into(), TIME),
current: declare("current".into(), TIME),
levels: declare("levels".into(), TIME),
ready: declare("ready".into(), TIME),
wake: (0..process_count)
.map(|index| declare(format!("wake[{index}]"), TIME))
.collect(),
waiting: (0..process_count)
.map(|index| declare(format!("waiting[{index}]"), FLAG))
.collect(),
baseline: (0..process_count)
.map(|index| declare(format!("baseline[{index}]"), FLAG))
.collect(),
clock_next: declare("clock_next".into(), TIME * CLOCKS),
clock_period: declare("clock_period".into(), TIME * CLOCKS),
clock_value: declare("clock_value".into(), TIME),
clock_armed: declare("clock_armed".into(), TIME),
})
}

Expand Down
100 changes: 89 additions & 11 deletions crates/celox-runtime/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,52 @@ pub(crate) struct SchedulerRefs {
pub(crate) limit: SignalRef,
pub(crate) now: SignalRef,
pub(crate) exit: SignalRef,
pub(crate) exit_arg: SignalRef,
pub(crate) finish: SignalRef,
pub(crate) count: SignalRef,
pub(crate) current: SignalRef,
pub(crate) levels: SignalRef,
/// The wake and waiting slots of each kernel the scheduler runs, by
/// process index.
pub(crate) ready: SignalRef,
/// The wake and waiting slots of each kernel the scheduler runs that
/// only delays, by process index.
pub(crate) wake: Vec<Option<(SignalRef, SignalRef)>>,
/// The waiting and baseline slots of each kernel the scheduler runs that
/// waits for an edge, by process index.
pub(crate) waits: Vec<Option<(SignalRef, SignalRef)>>,
/// The number of such kernels.
pub(crate) wait_kernels: usize,
/// The bit of each kernel the scheduler runs in its ready mask: its
/// place among them in kernel order.
pub(crate) ordinals: Vec<Option<u32>>,
/// The event ids of the bits of `levels`.
pub(crate) events: Vec<usize>,
/// The event id and signal of each host clock the scheduler may run,
/// in the order of its clock slots.
pub(crate) clocks: Vec<(usize, SignalRef)>,
pub(crate) clock_next: SignalRef,
pub(crate) clock_period: SignalRef,
pub(crate) clock_value: SignalRef,
pub(crate) clock_armed: SignalRef,
}

impl SchedulerRefs {
/// Element `bit` of the per-clock slot `slots`.
pub(crate) fn clock_element(slots: SignalRef, bit: usize) -> SignalRef {
let width = celox_design::SCHEDULER_TIME_WIDTH;
SignalRef {
offset: slots.offset + bit * width / 8,
width,
is_4state: false,
array_layout: None,
}
}

/// The clock slot that runs a scheduled `event` of `signal`, if any.
pub(crate) fn clock_of(&self, event: usize, signal: SignalRef) -> Option<usize> {
self.clocks
.iter()
.position(|&(id, clock)| id == event && clock == signal)
}
}

impl<B: SimBackend> ResolvedSchedule<B> {
Expand Down Expand Up @@ -279,26 +316,67 @@ impl<B: SimBackend> ResolvedSchedule<B> {
limit: resolve(&slots.limit),
now: resolve(&slots.now),
exit: resolve(&slots.exit),
exit_arg: resolve(&slots.exit_arg),
finish: resolve(&slots.finish),
count: resolve(&slots.count),
current: resolve(&slots.current),
levels: resolve(&slots.levels),
wake: slots
.wake
.iter()
.zip(&slots.waiting)
.zip(&scheduler.fallback)
.map(|((wake, waiting), fallback)| {
fallback
.is_none()
.then(|| (resolve(wake), resolve(waiting)))
ready: resolve(&slots.ready),
wake: (0..scheduler.fallback.len())
.map(|index| {
(scheduler.fallback[index].is_none()
&& model.processes[index].waits.is_empty())
.then(|| (resolve(&slots.wake[index]), resolve(&slots.waiting[index])))
})
.collect(),
waits: (0..scheduler.fallback.len())
.map(|index| {
(scheduler.fallback[index].is_none()
&& !model.processes[index].waits.is_empty())
.then(|| {
(
resolve(&slots.waiting[index]),
resolve(&slots.baseline[index]),
)
})
})
.collect(),
ordinals: {
let mut next = 0;
scheduler
.fallback
.iter()
.map(|fallback| {
fallback.is_none().then(|| {
next += 1;
next - 1
})
})
.collect()
},
wait_kernels: (0..scheduler.fallback.len())
.filter(|&index| {
scheduler.fallback[index].is_none()
&& !model.processes[index].waits.is_empty()
})
.count(),
events: scheduler
.events
.iter()
.map(|event| backend.resolve_event_opt(event).map(|event| event.id()))
.collect::<Option<_>>()?,
clocks: scheduler
.clocks
.iter()
.map(|clock| {
let event = backend.resolve_event_opt(clock)?;
Some((event.id(), resolve(clock)))
})
.collect::<Option<_>>()?,
clock_next: resolve(&slots.clock_next),
clock_period: resolve(&slots.clock_period),
clock_value: resolve(&slots.clock_value),
clock_armed: resolve(&slots.clock_armed),
})
}),
kernel_fallback: model
Expand Down
Loading
Loading