RTL Foundations / F09

Counters: How Do Four Clock Cycles Differ from Four Completed Operations?

Trace a collector through active and idle cycles, locate the fourth sample edge, and check count widths against a real waveform.

Lesson film

Film chapters

TranscriptCaptionsDownload film

The sensor panel waits for four valid readings before it can report a complete set. Some cycles bring in a reading. Others pass while the panel waits. If it declares the job complete on the fourth rising edge after the start, how many readings will it actually have?

F08 used the clock to track when data moved between registers. Here we use the same clock to count completed samples. The sample values stay in the data path.

Advance on a sample event

The upstream logic prepares sample_take_i before each rising edge. With synchronous reset released, a value of one marks a completed sample at that edge. A zero marks an idle cycle. In this lesson that synchronous event arrives as an input. F11 derives the event from valid and ready signals.

Six rising edges occur, with sample events at edges 1, 3, 5, and 6. These four events advance the count.
The clock keeps running through the idle cycles. At those edges the count holds its previous value.

Clear the counter at E0, then drive the sample event sequence 1, 0, 1, 0, 1, 1. From that sequence, fill in the completion count after each edge. Compare it with a second version that advances every cycle on the same clock. Both versions saturate at four.

Rising edgeSample eventCount beforeCount afterCounts every cycle
E11011
E20112
E31123
E40224
E51234
E61344

The cycle counter reaches four at E4. Only the readings at E1 and E3 have arrived by then. At E6 both counters show four, so a test that checks only the final value misses the mismatch from E2 through E5.

How many bits does a count of four need?

A four-slot buffer uses indices zero through three. Two bits are enough to say where to write the next sample. A completion count has to represent zero before any sample arrives and four after the final sample. Those five values require three bits. In three-bit binary, four is 100.

The slot index ranges from zero to three and takes two bits. The completion count ranges from zero to four. Four is 100 in binary and takes three bits.
The fourth write uses index three. After that write, the completion count becomes four.

The design uses $clog2(TARGET + 1) to size the counter for a positive target. The extra value is the initial zero. A target of one uses one bit. A target of three uses two bits for the values zero through three. The design also defines what happens at the limit. For this counter, an extra sample at E7 leaves the count saturated at four. A wraparound design would need to specify when it wraps and update the expected count table accordingly.

Which edge completes the fourth sample?

The E5 update leaves count_o at three. If the next edge has a sample event, that event completes the set. If the edge is idle, the count stays at three. So sample_last_o checks for a sample event, a current count of target minus one, and an inactive reset.

Before E6 the count is three and the sample event is one, so sample_last_o is one. E6 updates the completion count to four.
Another register on the same clock can sample sample_last_o at E6. That lets it update the current phase on the very edge that updates the count.
assign sample_last_o = rst_ni && sample_take_i && (count_o == BEFORE_LIMIT);

always_ff @(posedge clk_i) begin
    if (!rst_ni)
        count_o <= '0;
    else if (sample_take_i && (count_o != LIMIT))
        count_o <= count_o + 1'b1;
end

Follow the sampling order from F08. At E6 a clocked process reads the old value three. The NBA update writes four later at the same simulation time. A controller that tests count_o == 4 at a rising edge misses the event at E6 and sees the completed count at E7. Testing just count_o == 3 can mistake an idle edge for the final work event. sample_last_o identifies the edge that completes the set. The comparison count_o == 4 describes how much work has already finished.

The incorrect count reaches four at E4 while the correct count is two. The last-sample condition is high before E6, and the correct count reaches four after E6.
The waveform comes from the Icarus VCD. The driver prepares inputs on falling edges. Dashed lines mark the rising edges the testbench samples.

Use a separate count for a timeout

If the sensor stops sending readings, the completion count holds. Suppose the job has a deadline of six cycles after it starts. A separate counter tracks elapsed cycles, including idle ones. It needs its own starting point and expiration rule.

Suppose E0 accepts the start. E1 is the first counted edge and E6 is the sixth. E0 initializes the timer. If the fourth sample arrives at E6, both the completion and expiration conditions hold. With completion given priority, the job succeeds. If three readings have arrived and E6 is idle, it times out at E6. It also times out if E6 brings only the third reading. The downloadable design implements the main work counter. The timer is an extension to its contract.

Replay the work log

Download the lab package. Extract it on Linux or WSL and run bash run.sh. The driver prepares inputs at the falling edge. At the next rising edge, the monitor captures sample_last_o and waits 1 ns to observe the updated count. It checks nine edges, including saturation at E7 and synchronous reset priority at E8. The negative control treats every non-reset edge as a work event and finishes two cycles early.

Icarus Verilog 12.0 passed the tests for all three targets. In the recorded generic synthesis flow, Yosys 0.33 confirmed count widths of one, two, and three bits for targets one, three, and four. The run summary, raw waveform, and evidence and SHA-256 hashes are available separately.

Transfer question: what changes for one or three samples?

Keep the original six events. Set the target to one, then to three. How many bits does each version need? On which edge is its last-sample signal sampled high? When does the count hold at the limit?

Answer

Target one needs one bit. Before E1 the count is zero. The first sample at E1 sets it to one. Target three needs two bits. Before E5 the count is two. The third sample at E5 sets it to three. Once each reaches its limit it holds there until the synchronous reset at E8.

In the next lesson, the FSM uses the last-sample event. Once the counter tells the controller this sample completes the set, the controller moves from collection to waiting for delivery.

Keep the clock running and change the work

Each box is a rising edge. A check means a sample arrived. Remove the fourth sample and compare when each counter reports completion.

edgesample_takeSamplesClock-only

Download RTL, testbench and results

Check your understanding

1. The sample events are 1, 0, 1, 0, 1, 1. How many samples are complete after E4?
2. How many bits do a four-slot buffer index and a completion count that saturates at four each need?
3. The count is three and the target is four. The next edge is idle and the one after it has a sample. Which edge samples sample_last_o high?
4. The timeout extension counts E1 first and E6 sixth. Completion has priority. What happens when the fourth sample arrives at E6?
5. Set the target to three and replay 1, 0, 1, 0, 1, 1. What are the count width, last-sample edge, and count after E6?