System Verilog Implementation
Handshaking protocol
We now move to implementing the module in SystemVerilog. A skeleton for the conditional subtraction divider module is in the file subc_divide.sv. Since the operation can take a variable number of clock cycles, it is useful to implement the transfer of inputs and outputs with a handshaking protocol with the following signals: For the inputs to the module, we use the two signals:
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 the processing of the data wheninready=invalid=1.
For the outputs from the module, we use the two signals:
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.
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 resutls are ready, andinready=0to indicate that it is not ready yet 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
Completing and testing the code
Use the states to complete the section marked TODO in subc_divide.sv. Then, when you are complete, I have created a testbench in tb_subc_divide.sv. The testbench:
- Reads the
test_vectors/tv_python.csvfiles with the test inputsa,b,nbitsand outputszfrom the Golden python model - Runs each input to the SV module and gets the output
z - Compares the
zfrom the python golden model with the implementation - Writes the results to a file
test_vectors/tv_sv.csv
The testbench can be run by using the xilinxutils function (if you are running the code on the NYU server, go to the next section):
- Open a terminal in Unix or command window in Windows (On Windows, you cannot use Powershell)
- Activate the virtual environment with the
xilinxutilspackage - Follow the instructions to set the path for Vivado tools
- Navigate to the
hwdesign/labs/subcdirectory - Run
sv_sim --source subc_divide.sv --tb tb_subc_divide.sv
This will run the three steps in synthesizing and simulating the SV mdule. The outputs will be stored in a CSV file, test_outputs/tv_sv.csv.
You should see how many tests passed, and you can keep modifying the SV code until all test passed. I suggest that you modify the testbench to get more visibility until you pass the tests.
Running the Simulation on the NYU Server
Note that if you are on the NYU server, you should follow the specialized python instructions. In particular, follow those instructions to:
- log into the server
- clone the repository
hwdesignto your home directory so it is at~/hwdesign - install the
uvutility - create and activate a virtual environment
- install the python package with
uvin that environment.
Once you have done these steps, you can run the script with
(hwdesign) uv run sv_sim --source subc_divide.sv --tb tb_subc_divide.sv
Running the Simulation with the Vivado GUI
The above flow uses command line only. If you prefer, you can follow the instructions for using the Vivado GUI. Running the GUI will create the same files. You can also edit your SystemVerilog files in the Vivado editor.
Go to submission