Verilog Assignment
Verilog Assignment
Question 1.
Write a verilog module to implement an adder - it should have the module interfac
Solution
input [7:0] a, b;
endmodule // adder
This basically refers to the "long form" multiplication as we learn in school. For each digit of
the multiplier, we shift the multiplicand to the appropriate place value, and then add all the
partial products. An example of the partial products in binary for a simple 4-bit multiplication
are shown below. As expected, multiplying two 4-bit numbers can result in an output that is
up to 8 bits in length.
Number Representation
The numbers themselves are represented in 2's complement notation. Therefore, if the
multiplicand is negative, the PP values should be "sign-extended" to get the correct result. An
example for negative multiplicand is shown below - negative multiplier requires similar
careful handling, and is left as an exercise.
1010 # Decimal -6
x 0011 # Decimal 3
--------
11111010 # Partial product 0 (PP0) - sign extended
1111010 # PP1
000000 # PP2
00000 # PP3
--------
(1)11101110 # Decimal -18 (discard the overflow 1)
--------
Hardware Implementation
The above multiplication process can be directly implemented as combinational logic, where
each partial product vector is created using a set of AND gates, and the results are put
through a chain of adders.
The other alternative is to have a reduced hardware with a single register to hold the final
product. This accumulates the final product by iterating over several clock cycles (how
many?). A diagram indicating the architecture is shown here.
Solution Design: - Expected behaviour:
In the present template, we assume that there is also a "start" signal that indicates when the inputs
are valid, and that any time the start signal is 1, the multiplier should initialize internal variables to a
known state. You can assume that the start signal is only high for 1 cycle, and that the multiplication
itself should start only when the start signal is removed (made 0). Finally, after performing the
multiplication, the module should make the "done" signal high and keep it that way till the next start
signal is received.
`define width 8
`define ctrwidth 4
module seq_mult (
// Outputs
p, done,
// Inputs
clk, start, a, b
);
if (start) begin
done <= 0;
p <= 0;
ctr <= 0;
multiplicand <=
begin
end
end
endmodule // seqmult
Q2 – Testbech code – for verification of your code in vivado
`timescale 1ns/1ns
`define width 8
`define TIMEOUT 100
module seq_mult_tb () ;
reg signed [`width-1:0] a, b;
reg clk, start;
integer tot, err;
integer timer;
reg timedout;
task start_and_crank_dut;
begin
tot += 1;
timer = 0;
// start the DUT for one clock cycle
start = 1;
@(posedge clk);
// Remove start
#1 start = 0;
initial begin
// Initialize the clock
clk = 1;
tot = 0;
err = 0;
timedout = 0;
a = 10;
b = 2;
start_and_crank_dut;
// One input 0
a = 0;
b = 10;
start_and_crank_dut;
// Other input 0
a = 10;
b = 0;
start_and_crank_dut;
// Large values
a = 127;
b = 127;
start_and_crank_dut;
$finish;
end
endmodule // seq_mult_tb
&&&&&