This lab implements an Arithmetic Logic Unit (ALU).
ALUs are the computational heart of most processors.
As shown in Table 5.1, our ALU supports the operations add, sub, and, and or.
We can choose the ALU’s desired functionality through a two-bit control signal.
Table 5.1 Operations supported by our ALU depending on a two-bit control signal.
Control Signal
Operation
2’b00
add
2’b01
sub
2’b10
and
2’b11
or
In Section 5.1 we first design the ALU pieces required for the operations in Table 5.1.
Once the supporting pieces are in place and tested, we implement a basic ALU.
Section 5.2 tasks will bring our design to the Urbana board and allow us to control the ALU through the board’s slide switches.
We’ll then extend our initial ALU by adding condition flags in Section 5.3.
As discussed further in Section 5.4, the condition flags allow us to interpret the results of our operations.
Lastly, Section 5.5 deploys the extended ALU to the Urbana board.
Hint
Schematics of the basic and extended 64-bit ALUs are provided throughout this lab.
These are sufficient to solve the tasks.
In addition, a similar interactive implementation of the 4-bit parts is available on CircuitVerse.
Fig. 5.1.1 Illustration of our ALU’s basic part for 64-bit inputs i_a and i_b, and the 2-bit control signal i_alu_ctrl.
This section designs the initial part of our ALU which is shown in Fig. 5.1.1.
The two N-bit values i_a and i_b, and the control signal i_alu_ctrl represent the inputs to our basic ALU.
Outputs are given through the N-bit value o_result and the carry-out o_carry_out.
The signal o_carry_out is the adder’s carry-out, even when the control signal selects AND or OR.
Implement the module adder in the file adder.sv.
Test your implementation in the testbench adder_tb in the file adder_tb.sv.
Check at least three test cases!
Implement the module mux_2 in the file mux_2.sv.
Test your implementation in the testbench mux_2_tb in the file mux_2_tb.sv.
Test both values of i_s, using different values for i_in0 and i_in1.
Implement the module mux_4 in the file mux_4.sv.
Use the previously implemented module mux_2 in your implementation of mux_4!
Test your implementation in the testbench mux_4_tb in the file mux_4_tb.sv.
Test all four values of i_s, using different values for i_in0, i_in1, i_in2, and i_in3.
We have all building blocks at hand to implement the basic version of our ALU.
We’ll use the following SystemVerilog declaration (see also Fig. 5.1.1):
Before going into the details of the design, let’s derive some example inputs and outputs.
These also build the minimum example values for our ALU’s testbench.
Table 5.1.1 Example inputs and outputs for our basic ALU.
Implement the module alu in the file alu.sv.
Test your implementation in the testbench alu_tb in the file alu_tb.sv.
Instantiate alu with N=8.
Add respective tests for all examples in Table 5.1.1 to alu_tb.
Generate a waveform plot illustrating the application of your ALU w.r.t. Table 5.1.1’s inputs.
Limit your plot to these inputs and change the input every 10 time units, i.e., visualize 50 time units.
The plot shall show all inputs, i.e., i_a, i_b and i_alu_ctrl, and all outputs, i.e., o_result, o_carry_out.
Fig. 5.2.1 Picture of the deployed arithmetic logic unit.
Shown is the configuration for inputs i_a[3:0]=4'b0011, i_b[3:0]=4'b0010 and i_alu_ctrl[1:0]=2'b00.
Our design is finished and the simulations look promising: Let’s put the ALU on the Urbana board!
For this, we write a top-level module alu_top for the board.
The module instantiates a 4-bit version of our module alu and maps the board’s switches to the inputs i_a[3:0], i_b[3:0] and i_alu_ctrl[1:0].
Specifically, we wire SW[3:0] to i_a[3:0], SW[7:4] to i_b[3:0], and SW[14:13] to i_alu_ctrl[1:0].
As in the tiny calculator in Section 4.3, switch SW[15] selects what displays 4 and 3 show:
SW15 down (0): display 4 shows i_b[3:0] and display 3 shows i_a[3:0].
SW15 up (1): display 3 shows the result o_result[3:0] and display 4 is off.
The LEDs show the control and carry-out signals.
We connect i_alu_ctrl[0] to LED13, i_alu_ctrl[1] to LED14, and o_carry_out to LED7.
Each LED lights up when its signal is 1.
Table 5.2.1 Example inputs and outputs for our 4-bit ALU when deployed on an Urbana board.
1/** 2 * Top-level module of the basic alu. 3 * The alu operates on the 4-bit binary numbers in SW[3:0] and SW[7:4]. 4 * Switch SW15 selects what displays 4 and 3 show: 5 * SW15 = 0: display 4 shows input B (SW[7:4]), display 3 shows input A (SW[3:0]). 6 * SW15 = 1: display 4 is off, display 3 shows the result. 7 * The control signal is shown using LED13 and LED14. 8 * The carry out is shown via LED7. 9 *10 * @param SW_I input bits of input switches.11 * @param SW_CTRL input bits of two switches for ALU control.12 * @param SW_VIEW input bit of switch SW15 which selects between inputs and result.13 * @param LEDR_* output bits corresponding to the board's leds.14 * @param D0_SEG output bits which drive the seven-segment displays 4-715 * @param D1_SEG output bits which drive the seven-segment displays 0-316 * @param D0_AN anode signal for seven-segment displays 4-717 * @param D1_AN anode signal for seven-segment displays 0-318 **/19modulealu_top(inputlogic[7:0]SW_I,20inputlogic[14:13]SW_CTRL,21inputlogic[15:15]SW_VIEW,22outputlogic[14:13]LEDR_CTRL,23outputlogic[7:7]LEDR_C,24outputlogic[7:0]D0_SEG,25outputlogic[7:0]D1_SEG,26outputlogic[3:0]D0_AN,27outputlogic[3:0]D1_AN);2829// TODO: finish the implementation30endmodule
Tasks
Implement the top-level module alu_top.
Use the template in Listing 5.2.1.
Create the corresponding alu_top.xdc constraints file.
Compile your finished ALU using the openXC7 toolchain and program the FPGA of an Urbana board.
Make sure that the board shows the correct results for the inputs in Table 5.2.1.
Provide a picture of the board for each input.
Now, let’s extend our ALU by a set of condition flags which provide information about our ALU’s results.
Specifically, we’ll introduce the NZCV flags whose meaning is given in Table 5.3.1.
These flags are commonly used in practice and a large variety of instructions rely on them.
For example, the Arm ISA uses NZCV flags extensively for conditional branching.
We implement the NZCV flags by extending our original module shown in Fig. 5.1.1.
The SystemVerilog declaration of the extended design is given in Listing 5.3.1.
Compared to the declaration of our initial ALU given in Listing 5.1.1, the module alu_nzcv does not output the carry-out signal o_carry_out directly anymore.
Instead we output four bits for the NZCV flags in o_nzcv.
We extend the ALU shown in Fig. 5.1.1 by introducing the flags one-by-one:
Fig. 5.3.1 Illustration of our partially extended ALU after adding support for the Zero flag.
Fig. 5.3.2 Illustration of our partially extended ALU after adding support for the Zero and Negative flags.
Fig. 5.3.3 Illustration of our partially extended ALU after adding support for the Zero, Negative and Carry flags.
Fig. 5.3.4 Illustration of our extended ALU with full support for the NZCV flags.
Once again, we identify building blocks before tackling the entire module.
Of course the biggest building block, required already in Fig. 5.3.1, is given through Section 5.1’s basic ALU which we already designed in the module alu.
We only introduce one additional standalone module before tackling the NZCV-extended ALU.
This module is given by the three-input XOR which we require to compute the overflow flag in Fig. 5.3.4.
Our XOR should have the following declaration:
We now implement the ALU with support for the NZCV flags.
It is the ❤️ of our processor core, so we test it thoroughly in a testbench.
For the tests in Table 5.3.2, we use a 32-bit instance of our parameterized ALU.
Table 5.3.2 Example inputs and outputs for our extended ALU.
i_a
i_b
i_alu_ctrl
o_result
o_nzcv
32’h0000_0000
32’h0000_0000
2’b00
32’h0000_0000
4’b0100
32’h0000_0000
32’hffff_ffff
2’b00
32’hffff_ffff
4’b1000
32’h0000_0001
32’hffff_ffff
2’b00
4’b0110
32’h0000_ffff
32’h0000_0001
2’b00
4’b0000
32’h0000_0000
32’h0000_0000
2’b01
4’b0110
32’h0001_0000
32’h0000_0001
2’b01
4’b0010
32’hffff_ffff
32’hffff_ffff
2’b10
4’b1000
32’hffff_ffff
32’h7743_3477
2’b10
4’b0000
32’h0000_0000
32’hffff_ffff
2’b10
4’b0100
32’h0000_0000
32’hffff_ffff
2’b11
4’b1000
Tasks
Implement the module xor_3 in the file xor_3.sv.
Test your implementation in the testbench xor_3_tb in the file xor_3_tb.sv.
Test all eight input combinations.
Implement the module alu_nzcv in the file alu_nzcv.sv.
Test your implementation in the testbench alu_nzcv_tb in the file alu_nzcv_tb.sv.
Instantiate alu_nzcv with N=32.
Add tests for the examples in Table 5.3.2 to alu_nzcv_tb.
Generate a waveform plot illustrating the application of your extended ALU to Table 5.3.2’s inputs.
Apply each input for 10 time units and limit the plot to a total time of 100 time units.
Visualize all inputs (i_a, i_b and i_alu_ctrl) and all outputs (o_result, o_nzcv).
In this part we think about a few examples and the respective meaning of the obtained flags.
Table 5.4.1 makes the beginning.
We recapitulate that the meaning of raw bits depends on the interpretation.
Specifically, we consider the inputs to our ALU to be either signed or unsigned integers.
Next, we examine the meaning of the condition flags in a series of examples.
The examples are given in Table 5.4.2.
Compared to Table 5.3.2, we derive the flags on paper first and explain their meaning with respect to the given inputs and expected outputs.
Table 5.4.1 Different integer interpretations of four bits.
The second column assumes an unsigned interpretation.
The third column assumes a two’s complement representation.
Raw (bits)
Unsigned
Signed (two’s complement)
\(0000_2\)
\(0_{10}\)
\(0_{10}\)
\(0001_2\)
\(1_{10}\)
\(1_{10}\)
\(0010_2\)
\(2_{10}\)
\(2_{10}\)
\(0011_2\)
\(3_{10}\)
\(3_{10}\)
\(0100_2\)
\(0101_2\)
\(0110_2\)
\(1000_2\)
\(8_{10}\)
\(-(2^3 - 0) = -8_{10}\)
\(1001_2\)
\(9_{10}\)
\(-(2^3 - 1) = -7_{10}\)
\(1010_2\)
\(1101_2\)
Table 5.4.2 Example inputs and outputs for our extended 4-bit ALU.
i_a
i_b
i_alu_ctrl
o_result
o_nzcv
4’b0100
4’b0100
2’b00
4’b1101
4’b0011
2’b00
4’b0100
4’b1010
2’b01
4’b0110
4’b1001
2’b10
4’b0110
4’b0101
2’b11
Tasks
Complete Table 5.4.1.
Provide all numbers in base 10.
Complete Table 5.4.2.
Explain briefly what the respective condition flags indicate when assuming the following two cases:
i_a and i_b represent unsigned integers
i_a and i_b represent signed integers
ANDS (immediate) is a flag-setting base instruction of the A64 Instruction Set Architecture.
In assembly code one would use the syntax ANDS<Xd>,<Xn>,#<imm> when working with the 64-bit view of the general purpose registers.
Locate and name a flag-setting A64 base instruction which performs an addition and one which performs a subtraction.
Look up the assembly syntax for the 32-bit and 64-bit variants.
Hint
A short description of the NZCV condition flags is given in the documentation of AArch64’s system registers.
Fig. 5.5.1 Picture of the deployed arithmetic logic unit.
Shown is the configuration for inputs i_a[3:0]=4'b0100, i_b[3:0]=4'b1010 and i_alu_ctrl[1:0]=2'b00.
Let’s also use our NZCV-extended ALU alu_nzcv to program the FPGA of an Urbana board.
Once again we require a top-level module.
This time we use the name alu_nzcv_top and the template given in Listing 5.5.1.
Pretty much everything works analogously to the corresponding top-level module of Section 5.2.
From a high level perspective the main difference between the modules alu and alu_nzcv lies in the outputs:
alu sets the carry-out signal o_carry_out while alu_nzcv sets the condition flags o_nzcv[3:0].
Thus, in the module alu_nzcv_top we wire the NZCV flags to LED3 (Negative), LED2 (Zero), LED1 (Carry) and LED0 (Overflow).
Listing 5.5.1 Template for the module alu_nzcv_top.
1/** 2 * Top-level module of the NZCV-extended alu. 3 * The alu operates on the 4-bit binary numbers in SW[3:0] and SW[7:4]. 4 * Switch SW15 selects what displays 4 and 3 show: 5 * SW15 = 0: display 4 shows input B (SW[7:4]), display 3 shows input A (SW[3:0]). 6 * SW15 = 1: display 4 is off, display 3 shows the result. 7 * The control signal is shown using LED13 and LED14. 8 * The NZCV flags are shown via LED3 (N), LED2 (Z), LED1 (C) and LED0 (V). 9 *10 * @param SW_I input bits of input switches.11 * @param SW_CTRL input bits of two switches for ALU control.12 * @param SW_VIEW input bit of switch SW15 which selects between inputs and result.13 * @param LEDR_* output bits corresponding to the board's leds.14 * @param D0_SEG output bits which drive the seven-segment displays 4-715 * @param D1_SEG output bits which drive the seven-segment displays 0-316 * @param D0_AN anode signal for seven-segment displays 4-717 * @param D1_AN anode signal for seven-segment displays 0-318 **/19modulealu_nzcv_top(inputlogic[7:0]SW_I,20inputlogic[14:13]SW_CTRL,21inputlogic[15:15]SW_VIEW,22outputlogic[3:0]LEDR_FLAGS,23outputlogic[14:13]LEDR_CTRL,24outputlogic[7:0]D0_SEG,25outputlogic[7:0]D1_SEG,26outputlogic[3:0]D0_AN,27outputlogic[3:0]D1_AN);2829// TODO: finish the implementation30endmodule
Tasks
Implement the top-level module alu_nzcv_top.
Use the template in Listing 5.5.1.
Create the corresponding alu_nzcv_top.xdc constraints file.
Compile your finished ALU using the openXC7 toolchain and program the FPGA of an Urbana board.
Make sure that the board shows the correct results for the inputs in Table 5.4.2.
Provide a picture of the board for each input.