Skip to content

Add a stateful circuit driver interface - #206

Open
hydrolarus wants to merge 1 commit into
mainfrom
lara/circuit-driver
Open

hydrolarus wants to merge 1 commit into
mainfrom
lara/circuit-driver

Conversation

@hydrolarus

@hydrolarus hydrolarus commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

This adds a new module, Protocols.Experimental.Hedgehog.Driver, which allows stateful simulation of circuits of the shape Circuit a ().

This interface is still marked as experimental.

Testing Circuits currently either involves writing a pure model
and using idWithModel or more complex ad-hoc ways.

This change adds a stateful simulation primitive that allows
to monadically step through the execution of a circuit.

This interface is very generic and thus also very limited and
unopinionated for now. Protocol-specific "stepper" functions
can be defined later.

@martijnbastiaan

Copy link
Copy Markdown
Member

Not entirely sure what you're currently looking at, but maybe this is interesting: https://github.com/clash-lang/clash-protocols/compare/martijn/revamp-test-infrastructure. It's been a year or two in the making, with Claude recently adding monitors and test benches (which I haven't looked at). The basic idea is that we define "drivers", "sinks", and "monitors" for each protocol that produce a protocol-agnostic way for a test driver to step through the simulation. The core data structure is here:

{- | Status of a testbench component, sampled every clock cycle. The runner stops
the simulation as soon as every status trace says 'StopOk', or fails the test
as soon as any status trace says 'Error'.
-}
data Status
= {- | Do not stop the simulation, for example because a driver still has data
to send or a checker still expects data.
-}
PreventStop
| {- | Stopping the simulation is fine. The component may still be checking
for errors.
-}
StopOk
| -- | Something went wrong. The runner fails the test with this message.
Error Text
deriving (Eq, Show)
instance NFDataX Status where
deepErrorX = errorX
hasUndefined s = case isX s of
Left _ -> True
Right (Error msg) -> isLeft (isX msg)
Right _ -> False
ensureSpine = id
rnfX s = case isX s of
Left _ -> ()
Right (Error msg) -> either (const ()) rnf (isX msg)
Right _ -> ()
-- | A single sample of a signal: its bits and a mask marking undefined bits.
data Sample = Sample
{ mask :: !Natural
-- ^ Bits set in this mask are undefined
, value :: !Natural
-- ^ Value of the defined bits
}
deriving (Eq, Show)
-- | The samples of a t'Trace'. There is one sample per clock cycle of its domain.
data Samples
= -- | Samples of a signal of the given bit width
SignalSamples Int (Infinite Sample)
| -- | Samples of a status signal, see 'Status'
StatusSamples (Infinite Status)
| -- | Samples of a cover point: 'True' in every cycle the cover point was hit
CoverSamples (Infinite Bool)
| -- | A clock with the given active edge. Its samples are implied by its period.
ClockSamples ActiveEdge
-- | A single named trace
data Trace = Trace
{ name :: String
-- ^ Name of the trace
, period :: Femtoseconds
-- ^ Clock period of the domain the trace was sampled in
, samples :: Samples
-- ^ Samples, one per clock cycle
}
-- | A named group of traces. See 'TraceTree'.
data TraceTreeI = TraceTreeI
{ name :: String
-- ^ Name of this group. Groups with an empty name are flattened into their parent.
, traces :: [Either Trace TraceTree]
-- ^ Traces and subgroups
}
{- | A tree of traces. The tree structure is reflected in the scopes of a waveform
file and in the names of status and cover traces, which are joined with a slash,
for example @fifo\/out\/stall@.
-}
type TraceTree = SimOnly TraceTreeI
. Also see #144.

@hydrolarus
hydrolarus force-pushed the lara/circuit-driver branch 3 times, most recently from 9744275 to 09ba5fd Compare November 9, 2026 13:09
This interface is still marked as experimental.

Testing `Circuit`s currently either involves writing a pure model
and using `idWithModel` or more complex ad-hoc ways.

This change adds a stateful simulation primitive that allows
to monadically step through the execution of a circuit.

This interface is very generic and thus also very limited and
unopinionated for now. Protocol-specific "stepper" functions
can be defined later.
@hydrolarus hydrolarus changed the title WIP stateful circuit driver Add a stateful circuit driver interface Nov 9, 2026
@hydrolarus
hydrolarus marked this pull request as ready for review November 9, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants