This is an old revision of the document!
In this lab you will implement a two's complement binary adder and demonstrate the ability to perform binary addition, inversion, and subtraction.
In this lab exercise you will be performing two's complement addition and subtraction. You will also be performing sign-extension on 8-bit numbers to generate 9-bit numbers. Answer the following questions to demonstrate your understanding of two's complement representation, sign extension, addition, and subtraction. You may want to refer to Chapter 9 of the textbook as you complete this laboratory.
Determine the decimal value for the following eight-bit two's complement binary numbers:
Extend each of the following numbers to nine bits using sign-extension: (Note that sign extension is described in section 3.3.1 of the textbook)
Negate each of the two's complement numbers from the previous question. (Note that negating two's complement numbers is described in section 3.3.2 of the textbook)
Perform addition with the following two's complement numbers: (Note that addition of two's complement numbers is described in section 3.3.3 of the textbook)
1) 110000100 + 110011001 2) 001100100 + 000011001
Perform subtraction of the following two's complement numbers. To do this, negate the second number and perform two's complement addition as described in section 3.3.4 of the textbook.
1) 110000100 - 110011001 2) 001100100 - 000011001
In this first exercise, you will create a 9-bit two's complement full adder for implementing arithmetic. You will do this with two SystemVerilog files:
This is a classical hierarchical design: you will create a simple 1-bit full adder module and you will then create a new module and instance 9 of your 1-bit adders inside it.
Start by creating a new Vivado project for this lab. Make sure you follow the steps in red to properly configure the error messages in your project. Next, create an empty SystemVerilog file named “FullAdd.sv” and add the following ports to this module as described in the table below:
Module Name: FullAdd | |||
---|---|---|---|
Port Name | Direction | Width | Function |
a | Input | 1 | 'A' operand input |
b | Input | 1 | 'B' operand input |
cin | Input | 1 | Carry in |
s | Output | 1 | Sum output |
co | Output | 1 | Carry out output |
Once you have created the empty module with the ports outlined above, create the single-bit full adder cell as shown in Figure 9.2 1) of the text. Like the diagram, your circuit should include a 3-input XOR gate, three 2-input AND gates, and a 3-input OR gate. Note that you must use structural SystemVerilog to create this full adder cell. Although there are easier ways to create adder circuits using dataflow SystemVerilog, you are required to use structural SystemVerilog for this lab.
And, remember, the coding standard says you must either use meaningful signal names or add comments to describe what the local signals you declare with the 'logic' keyword are. For structural designs like this, coming up with meaningful signal names can be klunky (some students use names like aANDb, aANDcin, bANDcin, …). In later labs this can be come totally unwieldy. You might just consider using comments like this:
// The outputs of the 3 AND gates in the full adder logic a1, a2, a3;
Once you have created your circuit, simulate the full adder by testing all possible input conditions. Create a TCL file to provide the stimulus for this simulation. The TCL code below simulates two of the eight conditions.
# Simulate a=0, b=0, cin=0 for 10 ns add_force a 0 add_force b 0 add_force cin 0 run 10 ns # Simulate a=1, b=1, cin=1 for 10 ns add_force a 1 add_force b 1 add_force cin 1 run 10 ns
Attach a PDF of your full adder SystemVerilog code
Include a copy of your full adder TCL simulation script in your lab report.
As mentioned above, this lab will involve hierarchy. This is covered in-depth in Chapter 11, but will be briefly explained here.
A circuit with hierarchy is a circuit that contains modules within modules. In many complex digital logic circuits there are many levels of hierarchy. In this circuit you will have three levels of hierarchy - your FullAdd, your Add9, and a top level module. For this exercise you will create the Add9 which will instance 9 FullAdd modules. Program 11.1.1 in your textbook gives an example of instancing modules. In this case, three “mux21” modules are instanced to build a “mux41” module.
The next step in this exercise is to create a 9 bit ripple-carry adder. Create a new SystemVerilog file named Add9.sv and add the following ports as shown in the table below:
Module Name: Add9 | |||
---|---|---|---|
Port Name | Direction | Width | Function |
a | Input | 9 | 'A' operand input |
b | Input | 9 | 'B' operand input |
cin | Input | 1 | Carry in |
s | Output | 9 | Sum output |
co | Output | 1 | Carry out of last stage |
After creating the empty module, create the needed local wires (you will need some, figure out where), and insert 9 instances of your FullAdd module into your Add9 module (as described in Section 9.1 of the textbook and as shown in the figure below). You should use the .port(wire) way of mapping ports on instances to your wires (see Program 11.1.2 in the textbook). Note that the co output of your Add9 is the carry-out of the last FullAdd instance.
Simulate the behavior of your 9-bit adder by creating a TCL script of your own. Provide at least two input conditions to test your adder. Rather than assigning each bit of an input with a single TCL command, you can assign all 9 bits of an input with a single “add_force” command as shown in the following example:
add_force a 11000101
Read the following tutorial. This tutorial contains additional examples and instruction for creating TCL files.
Include a copy of your 9-bit adder TCL file in your lab report.
Exercise 1 Pass-off: Show a TA your simulation for your Add9 module and explain how you know your circuit is correct. Also, show that your tcl script for the FullAdd module tested all possible combinations.
For this exercise you will create a top-level design that instances your 9-bit adder and connects your adder to the switches and LEDs.
Both operands will be interpreted as two's complement numbers. Begin your design by creating a top-level module with the following name and ports.
Module Name: arithmetic_top | |||
---|---|---|---|
Port Name | Direction | Width | Function |
sw | Input | 16 | Switches (sw[15:8] = A input, sw[7:0] = B) |
led | Output | 9 | LED signals (result) |
NOTE: the name of the module and of the ports are crucial - use what is in the table above. The reason is that the testbench you will use below to prove your circuit works will rely on it being called 'arithmetic_top' and on the ports having the names above. And, just for uniformity sakes, call your file 'arithmetic_top.sv' - the convention is typically to have them be the same to minimize confusion.
A block diagram of the overall design is shown below.
This is different from how we did things in the last lab, why? If you recall, in the last lab you took a copy of the master .xdc file for the board, uncommented out the switches and lights you wanted to use and then changed the names of the signals mapped to those switches and lights so it would match your design (A, B, C, O1, O2, …). This week's lab represents an alternate approach. Here, your top level SystemVerilog module will have names that match what is in the .xdc file and so all you have to do is uncomment the appropriate lines in the .xdc file.
This has the advantage that your SystemVerilog code uses names like 'sw' and 'led' for the signal names, making it clear during the design and simulation stage just what signal is connected where to the board.
Importantly, it also has the benefit that you can put additional logic into this top level module. In this case you will put logic into the top level module to do the sign extension of your two inputs. How should you do that? There are two ways: (1) you could figure out how to sign extend as an expression that you pass to the Add9 instance you create or (2) you could declare two new signals that are 9 bits wide, use 'assign' statements to sign extend your two inputs to those signals, and then wire those new signals up to your Add9 module. Either way, you do not create a 'Sign extend' module - it is just some logic in your top module.
Also, the carry out of your adder is not used, but you do need to connect something to it. So, declare a wire in your top-level module and attach it to the carry out of your adder. But, do not connect it to anything else - the synthesis tool will recognize that it is unused and throw it away.
Similarly, the carry in of your adder needs to be tied to a '0' value. You have two ways of doing this. The first is to simply tie a 1'b0 as the carry in to your Add9 module when you instance it in 'arithmetic_top'. The second is to declare a wire for this purpose and use an 'assign' statement to assign it to a constant '0' and then wire that signal into 'arithmetic_top'. It is your choice of how to do this.
Simulating your logic is very important and there are a variety of techniques you can use to simulate your modules. TCL files are a good way to find errors by providing simple scripts with a few test cases and can be used to find obvious problems early in the simulation phase. However, it is difficult to fully test complex digital circuits with TCL files.
Digital circuits are often tested with special SystemVerilog modules called testbenches. Testbenches are SystemVerilog files that are used to test your circuit and are not used for designing new logic circuts. Testbenches are written differently than synthesizable SystemVerilog and are used to provide more thorough testing of digital circuits than is possible with TCL files. In this lab, and all future labs, you will be given a SystemVerilog testbench file that will test your circuit.
When you have completed your top-level design and removed all syntax errors, simulate your design manually to convince yourself that your circuit is working properly. Once you believe your circuit is working properly, download the following tb_arithmetic.sv file and simulate your module with this testbench. Read through the testbench tutorial to learn how to add and use a testbench for your verification. The testbench will run automatically, but you might need additional run time for it to finish. To do this, simply type a “run all” command into the TCL command line. The simulation will stop when the testbench ends.
The testbench will simulate your circuit's operation to make sure that the output of your circuit is correct for each case. The testbench will continue until it prints out a Simulation done message indicating the number of errors that were found. Make sure you have 0 errors before proceeding to the next exercise.
What to do if it doesn't work? How do I debug it?
When you simulate a top-level module, all you see are the top level ports and top level local signals. What if you want to see signals inside a sub-module (like your 9-bit adder)?
Copy the testbench output from the TCL console to your lab report to show that there were no errors.
Exercise 2 Pass-off: Show a TA your top level code and explain how you connected the inputs to your Add9 module. Also, show that the testbench did not report any errors.
For this final exercise, you can proceed with the implementation and downloading of your design. Begin this process by creating and adding an XDC constraints file. Your file should have entries for all 16 switches and 9 led outputs. The easiest way to create this file is to start with the master .xdc file and modify it with the signals you will use by uncommenting the ports you use and making sure the port names and your top-level signal names match (they should if you followed the instructions above).
Once you have added the XDC file to your project, synthesize your project.
Provide a summary of your synthesis warnings.
Perform the Running the Implementation Design Step step.
Summarize the size of your circuit (LUTs and I/Os).
Perform the Running the bitgen Design Step step. Download your circuit and verify that it works as expected.
Exercise #3 Pass-Off: No specific pass-off, just answer the questions above.
Attach the link to a video (preferred) at some site like Youtube, Dropbox, etc. of your circuit working on the Nexys4 board for final passoff. Alternatively, attach the video itself if it will fit.
There are now some additions to the coding standards regarding how to attach a video. Please follow them.
Show that it works for a variety of values. Show that all the bit positions work (don't just choose small numbers to add). For the 4 cases below, do the addition with multiple pairs of numbers for each case:
In the end, if the TA is convinced that you have demonstrated that all parts of your adder work, you will get full credit.
Attach PDF for your Add9 module to Learning Suite. (Make sure your SystemVerilog conforms to the lab SystemVerilog coding standards).
Attach PDF for your arithmetic_top module to Learning Suite.
How many hours did you work on the lab?
Provide any suggestions for improving this lab in the future.