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.
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 edge | Sample event | Count before | Count after | Counts every cycle |
|---|---|---|---|---|
| E1 | 1 | 0 | 1 | 1 |
| E2 | 0 | 1 | 1 | 2 |
| E3 | 1 | 1 | 2 | 3 |
| E4 | 0 | 2 | 2 | 4 |
| E5 | 1 | 2 | 3 | 4 |
| E6 | 1 | 3 | 4 | 4 |
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 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.
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.
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.