Skip to content
Closed
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
84 changes: 84 additions & 0 deletions crates/celox-frontend-core/src/symbolic/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,8 @@ fn unify_clock_domains(
modules: &HashMap<ModuleId, SimModule>,
) -> HashMap<AbsoluteAddr, AbsoluteAddr> {
let mut drive_graph: HashMap<AbsoluteAddr, Vec<AbsoluteAddr>> = HashMap::default();
// The edge each clock is sensed at: `true` rising, `false` falling.
let mut polarities: HashMap<AbsoluteAddr, bool> = HashMap::default();

for (path, id) in expanded {
let module_id = &instance_modules[id];
Expand All @@ -1820,6 +1822,20 @@ fn unify_clock_domains(
.get(&var)
.map(|variable| variable.metadata.width)
};
let mut record_polarity = |module: &SimModule, instance: InstanceId, var: SourceVarId| {
let polarity =
module
.variables
.get(&var)
.and_then(|variable| match variable.metadata.kind {
DomainKind::ClockPosedge | DomainKind::ResetAsyncHigh => Some(true),
DomainKind::ClockNegedge | DomainKind::ResetAsyncLow => Some(false),
_ => None,
});
if let Some(polarity) = polarity {
polarities.insert(bindings.locate(instance, var), polarity);
}
};

// Internal aliases (e.g. `assign clk_internal = clk_port;`)
for logic_path in &sim_module.comb_blocks {
Expand All @@ -1832,6 +1848,8 @@ fn unify_clock_domains(
|target| width(sim_module, target),
|source| width(sim_module, source),
) {
record_polarity(sim_module, *id, target);
record_polarity(sim_module, *id, source);
drive_graph
.entry(bindings.locate(*id, source))
.or_default()
Expand All @@ -1854,6 +1872,8 @@ fn unify_clock_domains(
if let Some((GlueAddr::Child(child_var), GlueAddr::Parent(parent_var))) =
whole_variable_copy(&glue_block.arena, logic_path, glue_width, glue_width)
{
record_polarity(child_module, child_id, child_var);
record_polarity(sim_module, *id, parent_var);
drive_graph
.entry(bindings.locate(*id, parent_var))
.or_default()
Expand All @@ -1872,6 +1892,8 @@ fn unify_clock_domains(
|_| parent_width,
glue_width,
) {
record_polarity(child_module, child_id, child_var);
record_polarity(sim_module, *id, *parent_var);
drive_graph
.entry(bindings.locate(child_id, child_var))
.or_default()
Expand All @@ -1882,6 +1904,68 @@ fn unify_clock_domains(
}
}

// A copy sensed at the opposite edge of its source is a clock of its own:
// `always_ff @(negedge d)` with `assign c = clk; assign d = c;` runs when
// `clk` falls, not in `clk`'s rising domain. A copy sensed at no edge
// takes the edge of its drivers, and a root sensed at no edge the edge of
// its first copy sensed at one. Copies that join edges of both kinds are
// cut apart.
let drivers = {
let mut drivers: HashMap<AbsoluteAddr, Vec<AbsoluteAddr>> = HashMap::default();
for (src, sinks) in &drive_graph {
for sink in sinks {
drivers.entry(*sink).or_default().push(*src);
}
}
drivers
};
let root_polarity = |root: AbsoluteAddr| {
polarities.get(&root).copied().or_else(|| {
let mut stack = vec![root];
let mut visited = HashSet::default();
while let Some(addr) = stack.pop() {
if !visited.insert(addr) {
continue;
}
if let Some(&polarity) = polarities.get(&addr) {
return Some(polarity);
}
if let Some(sinks) = drive_graph.get(&addr) {
stack.extend(sinks.iter().rev());
}
}
None
})
};
let effective_polarity = |addr: AbsoluteAddr| {
let mut current = addr;
let mut visited = HashSet::default();
loop {
if let Some(&polarity) = polarities.get(&current) {
return Some(polarity);
}
match drivers.get(&current).and_then(|sources| sources.first()) {
Some(&next) if visited.insert(next) => current = next,
_ => return root_polarity(current),
}
}
};
let drive_graph: HashMap<AbsoluteAddr, Vec<AbsoluteAddr>> = drive_graph
.iter()
.map(|(src, sinks)| {
let src_polarity = effective_polarity(*src);
let sinks = sinks
.iter()
.copied()
.filter(|sink| match (src_polarity, effective_polarity(*sink)) {
(Some(a), Some(b)) => a == b,
_ => true,
})
.collect();
(*src, sinks)
})
.collect();

// Resolve Canonical Clock Domains: Find the root driver for each connected component
let mut clock_domains: HashMap<AbsoluteAddr, AbsoluteAddr> = HashMap::default();

Expand Down
49 changes: 49 additions & 0 deletions crates/celox-frontend-veryl/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,53 @@ pub fn schedule_symbolic_rtl(
trace,
)?;
let lookup = &output.scheduled.frontend_lookup;
let mut constant_strings = crate::HashMap::default();
for (&instance, module_id) in &lookup.instance_module {
let Some(module) = module_ir.get(module_id) else {
continue;
};
let Some(variables) = source_id_maps.get(module_id) else {
continue;
};
for declaration in &module.declarations {
let Declaration::Comb(comb) = declaration else {
continue;
};
for statement in &comb.statements {
let veryl_analyzer::ir::Statement::Assign(assign) = statement else {
continue;
};
if !assign.expr.comptime().r#type.is_string()
|| assign.hier_dst.is_some()
|| assign.dst.len() != 1
{
continue;
}
let dst = &assign.dst[0];
let Some(variable) = module.variables.get(&dst.id) else {
continue;
};
if !matches!(variable.kind, veryl_analyzer::ir::VarKind::Let) {
continue;
}
if !assign.expr.comptime().is_const
|| assign.expr.comptime().get_value().is_err()
|| variable.r#type.is_array()
{
return Err(ParserError::illegal_context(
"string let initializer",
"Celox currently supports only constant scalar string let initializers",
Some(&variable.token),
));
}
if let Some(id) = variables.get(&dst.id)
&& let Some((address, _)) = lookup.instance_variable(instance, *id)
{
constant_strings.insert(address, assign.expr.clone());
}
}
}
}
let mut child_sources = Vec::new();
let mut components = Vec::new();
let mut component_bindings = Vec::new();
Expand All @@ -97,6 +144,7 @@ pub fn schedule_symbolic_rtl(
.collect();
if blocks.iter().any(|block| !block.is_empty()) {
child_sources.push(VerylTestbenchSource {
constant_strings: constant_strings.clone(),
id_map: VerylIdMap {
module_variables: source_id_maps
.get(module_id)
Expand Down Expand Up @@ -132,6 +180,7 @@ pub fn schedule_symbolic_rtl(
component_bindings.append(&mut instance_bindings);
}
let testbench_source = VerylTestbenchSource {
constant_strings,
id_map: VerylIdMap {
module_variables: source_id_maps,
},
Expand Down
2 changes: 2 additions & 0 deletions crates/celox-frontend-veryl/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl VerylIdMap {
#[derive(Clone, Default)]
pub struct VerylTestbenchSource {
pub id_map: VerylIdMap,
/// Constant scalar string lets are continuous assignments, outside initial blocks.
pub constant_strings: HashMap<celox_design::StateAddr, Expression>,
pub initial_statements: Option<Vec<Statement>>,
/// Length of each initial block in `initial_statements`; empty means one block.
pub initial_block_lengths: Vec<usize>,
Expand Down
Loading
Loading