Stage 3: The SystemVerilog Implementation
This stage has two halves, and they are in two different files. The module
(subc_divide.sv) is the state machine that does the arithmetic. The
testbench (tb_subc_divide.sv) is what hands it work and collects the
answers. You write a piece of each, because the protocol between them only makes
sense if you have implemented both ends of it.
Handshaking protocol
Since the operation takes a variable number of clock cycles, the transfer of inputs and outputs uses a handshaking protocol. For the inputs:
output inready: Indicates that the module is ready for the next set of inputs,a,bandnbitsinput invalid: Set to indicate that there are valid data values fora,b, andnbits. The module will start processing the data wheninready=invalid=1.
For the outputs:
output outvalid: Indicates that the module processing is complete and a valid outputzis available.input outready: Indicates to the module that the data is ready to be accepted. The module will consider the data as transferred whenoutready=outvalid=1.
The pattern — a valid from the sender, a ready from the receiver, transfer on
the cycle both are high — is worth recognising. It is the same one AXI-Stream and
most other on-chip interfaces use, and you will meet it again.
State machine
We can manage the handshaking protocol in the module by using three states:
IDLE: Waiting for inputs.- In this state, the module outputs
inready=1andoutvalid=0. - The module waits until the signal
invalid=1. - When
invalid=1, the inputsa,b, andnbitsshould be registered to the internal registers and the module should move to the stateRUN.
- In this state, the module outputs
RUN: The module is performing the division over multiple iterations.- You can use the variable
countfor the iteration number and stop after the correct number of iterations. - While running,
inready=0andoutvalid=0 - After completing the
nbitsiterations, the module should move toDONE.
- You can use the variable
DONE: The results are ready.- The module asserts
outvalid=1andinready=0to indicate the results are ready, and that it is not yet ready for new inputs. - When the signal
outready=1the module assumes the outputs have been read, and moves to theIDLEstate in the next iteration.
- The module asserts
Writing the module
Complete the section marked TODO in subc_divide.sv. You write two blocks:
- an
always_combthat computes the next value of every register from the current ones, and - an
always_ff @(posedge clk)that commits them.
Splitting it that way is the standard shape for a state machine — the decisions in one place, the clocking in another.
The combinational block must assign every _next signal on every path, or you
infer a latch. The usual way is to start with defaults that hold the current
value, then override them per state:
a_next = a_reg; b_next = b_reg; z_next = z_reg;
count_next = count; state_next = state;
The RUN state is one iteration of exactly the algorithm you already wrote in
Python. Two things to watch:
- Compare the shifted value against
b_reg, nota_reg. The comparison is on the remainder after the shift, which is the whole point of “bring down the next bit”. - Drive the outputs from the current state, not the next one.
inready,outvalidandzdescribe what the module is doing now:
inready = (state == IDLE);
outvalid = (state == DONE);
z = z_reg;
Writing the testbench
The reading of the test vectors, the writing of the results and the counting of
clock cycles are all given. What you write is the handshake itself — the TODO in
tb_subc_divide.sv marks it. Four things have to happen, in order:
- Wait until the module is ready (
inready), then holdinvalidhigh across one posedge and drop it again. - Wait for
outvalid, counting posedges intocycle_count. - Sample
zgot = zwhileoutvalidis still high — that is the only time the module promiseszmeans anything. - Hold
outreadyhigh across one posedge and drop it, which is how the module learns the answer has been taken.
Bound the wait loop. Write step 2 as
while (!outvalid && cycle_count < MAX_WAIT), not aswhile (!outvalid).A module that never asserts
outvalid— which is exactly what a half-finished state machine is — would otherwise hang the simulation, and a simulator that never returns looks precisely like one that is working hard. A failed case is far more useful than a run that never ends. If the loop times out, incrementtimeoutsand leavezgotandcycle_countalone; the build scores an unanswered case as the failure it is.
There is a watchdog in the testbench as a backstop, outside the part you write. If the whole simulation is still going after 500 µs it prints a diagnosis and stops, so a stuck design costs you seconds rather than a coffee break. If you see
TIMEOUT: still running after 500 us of simulated time.
then the module stopped handshaking: check that the state machine leaves RUN
after nbits iterations, asserts outvalid in DONE, and returns to IDLE once
outready is seen.
Running the stage
python subc_build.py --through svsim
This compiles both files, runs the simulation, and compares the result against your Python — you do not invoke the simulator yourself. If the compile fails, that is reported as a score of zero with the tool output attached, rather than as a traceback that leaves you with no submission.
SystemVerilog divider: 10/10
✓ (6/6) SystemVerilog matches your Python model
✓ (4/4) Each case finishes within nbits + 2 cycles
Both are scored proportionally, and the feedback names the first case that failed
with its a, b and nbits, what Python said and what the simulation said.
The latency check is only credited on cases that were answered correctly. Fast
and wrong is not a design, and timing a case whose answer is wrong measures
nothing. A correct machine spends one cycle in IDLE, nbits in RUN and one in
DONE, so nbits + 2 is a comfortable allowance rather than a tight one.
The comparison is against your Python model, not against ours. That means two unimplemented halves would agree with each other perfectly — so if your Python returns the same
zfor every case, this stage scores zero regardless of what the simulation did, and says so. Fixsubc_divide.pyfirst.
Running on the NYU server
If you are on the NYU server, follow the
python instructions to log in, clone the
repository, install uv, and create and activate the virtual environment. Then
run the build through uv:
uv run python subc_build.py --through svsim
Using the Vivado GUI
The flow above is command line only. If you prefer, you can follow the instructions for using the Vivado GUI, and you can edit your SystemVerilog in the Vivado editor. Run the build afterwards to be scored.
Go to grading and submission