§ 5.4.2. Counter and Queue¶
Two small clocked parts hold the state inside the devices: a counter (the timer, the LED blink) and a first-in, first-out queue (console bytes, button events).
| Component | Description | Structural | Behavioral |
|---|---|---|---|
ClockedCounter |
Counts enabled clock edges, optionally every n-th | Yes | Yes (4 and 32 bits) |
SynchronousFifo |
A queue of 1–7 words with sticky overflow | Yes | Yes (8 and 32 bits) |
5.4.2.1. ClockedCounter¶
Catalog: device.counter.W for W = 4, 8, 16, 32; parameter divisor
(positive, default 1).
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST |
Input | 1 each | Clock; reset clears the count and the divider |
ENABLE |
Input | 1 | Low holds the count and the divider |
COUNT |
Output | W | Enabled ticks since reset, wrapping |
TICK |
Output | 1 | This edge will increment the count |
Behavior¶
COUNT increments on every divisor-th enabled rising edge and wraps at
2^W. TICK is a signal, not a second clock: there is one clock in the
machine.
With divisor 1, TICK = ENABLE && !RST and can stay high between many
edges; it is an increment enable, not a narrow pulse. With a larger divisor,
the divider phase advances only on enabled edges. Pausing preserves that
phase as well as COUNT, so resuming does not restart the interval. Reset
clears both; divisor 0 is rejected at construction.
C++ reference¶
#include <cstdint>
template<unsigned W>
struct CounterRef {
static_assert(W == 4 || W == 8 || W == 16 || W == 32);
uint32_t count = 0, phase = 0;
bool tick(bool reset, bool enable, uint32_t divisor = 1) const {
return !reset && enable && phase == divisor - 1;
}
void edge(bool reset, bool enable, uint32_t divisor = 1) {
if (reset) { count = phase = 0; return; }
if (!enable) return;
if (phase == divisor - 1) {
phase = 0;
count = (count + 1) & (UINT32_MAX >> (32 - W));
} else ++phase;
}
};
This models binary edge transitions with a fixed positive divisor. It does not model reset's propagation between edges or uncertain clocks/enables.
Implementation¶
TICK_CONTROL makes the enable (with phase state, a terminal comparison and
an advance group when divisor > 1); COUNT_STATE holds the count and
INCREMENT adds one with the adders of § 2.
The compact 4- and 32-bit counters execute their own compiled gates. Other widths retain their structural assembly even when compact children are preferred. Parameterized divider logic changes the circuit and is timed for that configuration.
Timing. A count change follows an enabled rising edge after the
register's propagation; TICK can also change combinationally with ENABLE.
The following stored paths are for the default divisor 1, with reset low;
they do not characterize every positive divisor or certify an enclosing
timer's clock.
Latest time each path can still change after its input (for a setup row: how long before the edge the input must be stable), proven on the gate-level form.
| Path | unitticks | embeddedps | desktopps |
|---|---|---|---|
4-bit edge to count CLK ↑ → COUNT | 4 | 175 | 120 |
32-bit edge to count CLK ↑ → COUNT | 4 | 175 | 120 |
4-bit enable to tick ENABLE → TICK | 1 | 50 | 35 |
32-bit enable setup ENABLE before CLK ↑ | 10 | 413 | 287 |
Examples¶
All 8 recorded rows of ClockedCounterScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-logic-v1 / structural (3c048584c5332bb2). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | CLKInput · 1 bit |
ENABLEInput · 1 bit |
RSTInput · 1 bit |
COUNTExpected output · 4 bit |
TICKExpected output · 1 bit |
|---|---|---|---|---|---|---|
#0reset | 8 | 0 |
1 |
1 |
not checked | not checked |
#1ready | 18 | 0 |
1 |
0 |
not checked | not checked |
#2one | 27 | 1 |
1 |
0 |
0x1 |
1 |
#3two | 39 | 1 |
1 |
0 |
0x2 |
1 |
#4paused | 47 | 1 |
0 |
0 |
0x2 |
0 |
#5resumed | 60 | 1 |
1 |
0 |
0x3 |
1 |
#6maximum | 213 | 0 |
1 |
0 |
0xF |
not checked |
#7wraps | 223 | 1 |
1 |
0 |
0x0 |
1 |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
5.4.2.2. SynchronousFifo¶
Catalog: device.fifo.W for W = 8, 16, 32; parameter depth 1–7
(default 4).
IO pins¶
| Pin | Direction | Width | Meaning |
|---|---|---|---|
CLK, RST |
Input | 1 each | Clock; reset empties the queue and clears overflow |
PUSH, PUSH_DATA |
Input | 1, W | Append a word |
POP |
Input | 1 | Remove the oldest word |
CLEAR_OVERFLOW |
Input | 1 | Clear the lost-word flag |
DATA |
Output | W | The oldest word (0 when empty) |
VALID, FULL, PUSH_READY |
Output | 1 each | Not empty; full; a push would be kept |
COUNT |
Output | 3 | Words held |
OVERFLOW |
Output | 1 | A push was dropped since the last clear |
Behavior¶
At a rising edge, POP with VALID removes the head and PUSH with
PUSH_READY appends. A push to a full queue is dropped and sets
OVERFLOW. When full, a simultaneous pop and push pops and drops the push;
when empty, a simultaneous pop and push keeps the new word.
For a partly full queue, simultaneous pop and push preserve its count and
replace the old head with the next word in FIFO order. A new dropped push
wins over CLEAR_OVERFLOW on the same edge; otherwise that input clears
the sticky flag. Empty POP is ignored. Reset makes COUNT = 0, VALID = 0,
DATA = 0 and clears overflow; old slot contents cannot be read as a valid
word. All decisions use occupancy before the edge, which explains why a
full queue cannot accept a simultaneous replacement.
C++ reference¶
#include <cstdint>
#include <deque>
#include <stdexcept>
template<unsigned W>
struct FifoRef {
static_assert(W == 8 || W == 16 || W == 32);
unsigned depth;
std::deque<uint32_t> words;
bool overflow = false;
explicit FifoRef(unsigned capacity) : depth(capacity) {
if (depth < 1 || depth > 7) throw std::invalid_argument("depth");
}
uint32_t data() const { return words.empty() ? 0 : words.front(); }
void edge(bool reset, bool push, uint32_t value, bool pop, bool clear) {
if (reset) { words.clear(); overflow = false; return; }
const bool full = words.size() == depth, empty = words.empty();
if (pop && !empty) words.pop_front();
if (push && !full) words.push_back(value & (UINT32_MAX >> (32 - W)));
overflow = (push && full) || (overflow && !clear);
}
};
This binary reference uses a host queue only to describe accepted words;
the component uses registers, decoders and muxes. Arbitrary X/Z queue
controls are outside this integer reference.
Implementation¶
CONTROL holds the count and overflow; POINTERS the head and tail;
STORAGE the words, a write decoder and the read mux.
Pointers wrap at the configured depth, including non-powers of two. The
compact forms exist at W = 8 and 32 and compile this same circuit; W = 16
is structural. The read selection is gated by VALID, so an empty queue
returns zero rather than a stale slot.
Timing. Acceptance is decided from the pre-edge FULL/VALID; count,
head data and overflow settle after the edge. The selected stored paths are
for depth 4 and reset low. Other depths alter pointer wrap/decode paths and
must use their own derived timing rather than these default rows.
Latest time each path can still change after its input (for a setup row: how long before the edge the input must be stable), proven on the gate-level form.
| Path | unitticks | embeddedps | desktopps |
|---|---|---|---|
Byte queue edge to head CLK ↑ → DATA | 17 | 885 | 615 |
Word queue edge to head CLK ↑ → DATA | 17 | 885 | 615 |
Push data setup, byte queue PUSH_DATA before CLK ↑ | 8 | 333 | 231 |
Pop setup, word queue POP before CLK ↑ | 16 | 857 | 597 |
Examples¶
All 10 recorded rows of SynchronousFifoScenarioTest, in checkpoint order. Outputs are test expectations verified against the simulation. Time is the absolute settled checkpoint time in simulator units, not milliseconds or CPU cycles.
Default scenario profile: fidelity-logic-v1 / structural (3c048584c5332bb2). Changing fidelity or the circuit can change these times.
| Checkpointzero-based number · timeline label | Timesimulator units | CLEAR_OVERFLOWInput · 1 bit |
CLKInput · 1 bit |
POPInput · 1 bit |
PUSHInput · 1 bit |
PUSH_DATAInput · 8 bit |
RSTInput · 1 bit |
COUNTExpected output · 3 bit |
DATAExpected output · 8 bit |
FULLExpected output · 1 bit |
OVERFLOWExpected output · 1 bit |
PUSH_READYExpected output · 1 bit |
VALIDExpected output · 1 bit |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
#0reset | 13 | 0 |
0 |
0 |
0 |
0x00 |
1 |
not checked | not checked | not checked | not checked | not checked | not checked |
#1ready | 24 | 0 |
0 |
0 |
0 |
0x00 |
0 |
not checked | not checked | not checked | not checked | not checked | not checked |
#2push-a | 45 | 0 |
1 |
0 |
1 |
0x41 |
0 |
0x1 |
0x41 |
not checked | 0 |
not checked | 1 |
#3push-b | 61 | 0 |
1 |
0 |
1 |
0x42 |
0 |
0x2 |
0x41 |
not checked | not checked | not checked | not checked |
#4pop-a | 85 | 0 |
1 |
1 |
0 |
0x42 |
0 |
0x1 |
0x42 |
not checked | not checked | not checked | not checked |
#5empty | 107 | 0 |
1 |
1 |
0 |
0x42 |
0 |
0x0 |
0x00 |
not checked | not checked | not checked | 0 |
#6full | 179 | 0 |
1 |
0 |
1 |
0x04 |
0 |
0x4 |
0x01 |
1 |
0 |
0 |
not checked |
#7overflow | 188 | 0 |
1 |
0 |
1 |
0x05 |
0 |
0x4 |
0x01 |
not checked | 1 |
not checked | not checked |
#8pop-after-overflow | 215 | 0 |
1 |
1 |
0 |
0x05 |
0 |
0x3 |
0x02 |
0 |
1 |
1 |
not checked |
#9clear-overflow | 228 | 1 |
1 |
0 |
0 |
0x05 |
0 |
0x3 |
not checked | not checked | 0 |
not checked | not checked |
Scroll within the table for all rows and pins. Buses use hexadecimal; values containing X/Z use MSB-first binary. Inputs show the declared values in effect at each checkpoint, including retained inputs. Sequential tests also replay preparation changes between checkpoints, such as lowering the clock. Use the matching checkpoint label in the simulation below.
Source and tests¶
Interfaces, counter, FIFO and compact registrations, functional and queue-model checks.
| Test | Coverage |
|---|---|
ClockedCounterTest |
Increment and pause at 4/8/16/32 bits (20 edges, so wrap is exercised at 4 bits); divisor 3 at 4 bits; structural and available compact forms |
SynchronousFifoTest |
Order, count, full, overflow, simultaneous push/pop, reset; depths 1–7 at W = 8/32 against a queue model with deterministic push/pop patterns; both forms |