Skip to content

Timing & Clock Domains ​

Inspired by Filament, every hardware logic value in Neo carries both a cycle timing and a clock domain. This lets Neo statically catch pipeline misalignment (e.g., combining cycle t with cycle t + 1) and accidental clock-domain crossings (CDC) at compile time.

The Timing Lattice ​

Neo's timings form a partial order (lattice):

text
Constant  <=  SingleCycle(trigger, offset, clock)  <=  Dynamic(clock)

1. Constant (Bottom) ​

  • Written by omitting the timing annotation (e.g., logic mask: Bits(4) = "1010";), or produced by literals (42#32, "0101", 0, 1, enum variants, payload-less union variants, b.toBits()).
  • Compatible with every timing and crosses every clock domain safely because constant values never change.
  • A Constant-timed target can only be assigned from a Constant source.

2. SingleCycle(trigger, offset, clock) ​

  • Represents a value valid at trigger + offset cycles relative to clock.
  • Introduced by:
    • A t: Timing module/interface parameter (clock domain Any, polymorphic over the caller's clock domain).
    • Timing() — creates a fresh single-cycle trigger in the __defaultClock domain.
    • Timing(clk) — creates a fresh single-cycle trigger bound to an explicit Clock-typed signal clk.
    • Arithmetic t + n where t: Timing and n: UInt.
  • A SingleCycle(t1, o1, c1) target accepts:
    • Constant, or
    • SingleCycle(t2, o2, c2) iff t1 == t2, c1 and c2 are compatible clock domains, and Z3 proves o1 == o2.
  • SingleCycle <- Dynamic is forbidden: you cannot assign an untimed Dynamic value into a cycle-timed SingleCycle signal.

3. Dynamic(clock) (Top within a clock domain) ​

  • Written (*) for domain-polymorphic dynamic timing (ClockDomain::Any), or (Dynamic(clk)) to bind a dynamic signal to a specific Clock-typed signal clk.
  • Useful for asynchronous/stalled handshakes (such as Decoupled(T)'s ready and req), state machines, and control signals not tied to a fixed pipeline cycle.
  • Accepts Constant, SingleCycle (in a compatible clock domain), or Dynamic (in a compatible clock domain).

Timing Joins (Least Upper Bound) ​

When combining signals in binary operators (a + b, a & b, a == b), concatenation (a ## b), struct literals (.{ a, b }), tuple literals ((a, b)), Array.select(idx), or if/case/match branches, Neo computes the least upper bound (LUB) of their timings:

  • join(Constant, x) = x
  • join(Dynamic(c1), x(c2)) = Dynamic(merge(c1, c2)) (if clocks are compatible)
  • join(SingleCycle(t1, o1, c1), SingleCycle(t2, o2, c2)) = SingleCycle(t1, o1, merge(c1, c2)) iff t1 == t2, clocks are compatible, and Z3 proves o1 == o2 (otherwise timing_mismatch).
  • Combining non-constant signals from incompatible clock domains produces clock_domain_mismatch.

Cycle Advancement (seq and Reg) ​

  • Inside a seq or seq clk block, assigning a source expression to a non-local register target advances the source timing by +1 cycle (SingleCycle(t, off, clk) -> SingleCycle(t, off + 1, clk); Constant and Dynamic remain Constant and Dynamic).
  • A local logic signal declared inside a seq block is a combinational temporary and does not advance cycles.
  • Reg(expr) registers expr on __defaultClock and returns a value with timing advanced by +1 cycle.
neo
@top
module Pipeline(t: Timing) {
  input x(t): Int(32),
  output y(t + 2): Int(32),
} {
  logic s1(t + 1): Int(32);

  seq {
    logic doubled(t): Int(32) = x + x; // seq-local combinational temp
    s1 = doubled;                      // t -> t + 1
  }

  assign y = Reg(s1);                  // t + 1 -> t + 2
}

Clock Domains & CDC Verification ​

Every non-constant timing carries a ClockDomain:

  • ClockDomain::Default: The module's implicit __defaultClock (created by Timing(), seq { ... }, and Reg(...)).
  • ClockDomain::Signal(clk): A specific Clock-typed port or wire (created by Timing(clk), Dynamic(clk), and seq clk { ... }).
  • ClockDomain::Any: Domain-polymorphic timing ((*) or symbolic t: Timing parameters), compatible with any single clock domain and refined when merged with a concrete domain.

Two clock domains are compatible iff either is Any or both refer to the exact same clock.

Static CDC Error Detection ​

Neo catches clock-domain crossings automatically:

  1. Cross-domain reads (clock_domain_mismatch):
    • Reading a signal from clock domain clk_a inside seq clk_b { ... } (or inside default-clocked seq { ... } / Reg(...)).
    • Combining two signals from different clock domains in an expression (e.g., a_fast + b_slow) or assigning a signal from clk_a to a target in clk_b.
  2. Cross-domain writes (seq_block_clock_mismatch):
    • Assigning to a register target whose timing belongs to clk_a inside a seq clk_b { ... } block.
neo
@top
module DualClock {
  input clk_a: Clock,
  input clk_b: Clock,
  input in_a(Dynamic(clk_a)): Bits(8),
  output out_a(Dynamic(clk_a)): Bits(8),
  input in_b(Dynamic(clk_b)): Bits(8),
  output out_b(Dynamic(clk_b)): Bits(8),
} {
  // No __defaultClock is emitted because only explicit custom clocks are used!
  seq clk_a {
    out_a = in_a; // OK: clk_a -> clk_a
    // out_a = in_b; // ERROR (clock_domain_mismatch): reading clk_b inside seq clk_a
    // out_b = in_a; // ERROR (seq_block_clock_mismatch): writing clk_b inside seq clk_a
  }

  seq clk_b {
    out_b = in_b; // OK: clk_b -> clk_b
  }
}

Because DualClock uses only explicit clk_a and clk_b (no implicit seq { ... } or Reg(...)), Neo emits no __defaultClock port on the generated SystemVerilog or FIRRTL module.