Lab 07
Lab 07
Name:
Kevin Bradshaw
Introduction
The design in this lab will demonstrate the ways in which Verilog encoding makes hardware
design more efficient. It is possible to design a 32-bit ALU from 1-bit ALUs. (i.e., you
could program a 1-bit ALU that implements a full adder, chain four of these together to
make a 4-bit ALU, and chain 8 of those together to make a 32-bit ALU.) However, it is
easier (both in time and lines of code) to code it succinctly in Verilog. It is even easier,
however, to design a module to be inefficient or incorrect.
Pre-requisite
For this lab you are expected to know Verilog programming and understand how to use
VCS based upon the last lab and understand the ALU from Chapter C.5 of the textbook.
Verilog Review
The following Verilog modules have errors. Explain the errors in each module and provide
a fix for each.
1. Logical gates
module g a t e s ( input
[3:0] a, b,
output reg [ 3 : 0 ] y1 , y2 , y3 , y4 , y5 ) ;
always @( b )
begin
y1 = a & b ;
y2 = a | b ;
y3 = a b ;
y4 = ( a & b ) ;
y5 = ( a | b ) ;
end
endmodule
2.
4. Priority Encoder
module p r i o r i t y ( input
[3:0] a,
output reg [ 3 : 0 ] y ) ;
always @( )
if
( a [ 3 ] ) y = 4 b1000 ;
e l s e i f ( a [ 2 ] ) y = 4 b0100 ;
e l s e i f ( a [ 1 ] ) y = 4 b0010 ;
e l s e i f ( a [ 0 ] ) y = 4 b0001 ;
endmodule
A 4:2 priority encoder should have 4 bits as input and 2 bits as an output. Also, each case should
be defined properly for the if statements after the change to the output is made. To make the code
more efficient, case statements could be added instead of using if and else statements. Lastly,
there should be a default case added as a final else statement.
5.
a, b, c ,
6. Register
module f l o p r s e n ( input
input
reset ,
input
set ,
input
[3:0] d,
output reg [ 3 : 0 ] q ) ;
clk ,
The always statements should be combined because q is dependent on three different inputs.
always @(posedge clk, posedge set, posedge reset)
if (set)
q <=1;
else if (reset)
q <=0;
else
q <=d;
endmodule
Lab overview
In this Lab, you will use the Verilog hardware description language to implement and verify
a 32-bit sign extender module, a register file and the ALU module.
Note: the modules that you implement in this lab and several labs following will be
used as building blocks for a complete single cycle processor. It is highly recommended
that you implement and test your models properly.
A 32-bit extender takes as input a 16-bit immediate value, and based on the control bit,
the value is either zero extended or sign extended to a 32-bit value. If the control bit is 1,
then the value is zero extended; otherwise, it is sign extended. Type the following Verilog
code into a new file and name it SignExtender.v.
module S i g n E x t e n d e r (BusImm , Imm16 , C t r l ) ;
output [ 3 1 : 0 ] BusImm ;
input [ 1 5 : 0 ] Imm16 ;
input C t r l ;
wire e x t B i t ;
assign #1 e x t B i t = ( C t r l ? 1 b0 : Imm16 [ 0 ] ) ;
assign BusImm = {{16{ e x t B i t } } , Imm16 } ;
endmodule
Implement a testbench for the module, SignExtender, and simulate the testbench with
VCS. Your testbench should be self-checking, i.e. it should not only set inputs but it should
also check for expected outputs and print a failure message in the event of unexpected
output. You may use the testbenches provided in Lab06 as an example of an exhaustive,
self-checking testbench.
The module above contains an error, and you are required to find it using you testbench
module and correct it. Run the simulation again to ensure the SignExtender module is
working properly. At this point, demonstrate your progress to the TA.
5.1
Deliverables
Completion of this part of the lab requires the following deliverables to be turned in to
eCampus along with your completed lab form:
Verilog code for your corrected 32-bit sign extender.
Verilog code for your testbench which tests all corner cases of the sign extender and
catches the error in the provided code.
A screen shot of the sign extenders input and output waveforms from the VCS DVE
with the testbench as stimulus.
Register File
Implement a 32x32 register file. You may use behavioral style Verilog programming, but
your code must be synthesizable (use only synthesizable constructs). The register file
contains 32, 32-bit registers. The file MiniRegisterFile.v contains a small register file
which is missing several features, but can provide a starting point.
Below is a specification of the register file;
Buses A, B, and W are 32 bit wide.
When RegWr is set to 1, then the data on Bus W is stored in the register specified
by Rw, at negative clock edge.
Register 0 must always read zero.
Data from registers (as specified by Ra and Rb) is sent on Bus A and Bus B respectively, after a delay of 2.
Writes to the register file must have a delay of 3.
The Register File module should have the following interface:
module R e g i s t e r F i l e ( BusA , BusB , BusW, RA, RB, RW, RegWr ,
Clk ) ;
Bus A
Ra
Rb
32x32
Register File
Rw
Bus B
Bus W
Clk
RegWr
Value
0x00000000
0x00000001
0x00000002
0x00000003
0x00000004
0x00000005
0x00000006
0x00000007
Register
8
9
10
11
12
13
14
15
Value
0x00000008
0x00000009
0x000000A
0x000000B
0x000000C
0x000000D
0x000000E
0x000000F
All the other registers should have a value of zero. Specify the values on BusA and BusB
along with the registers (if any) that will be modified. Put N/A if none are modified:
Ra
Rb
Rw
RegWr
Bus W
0
2
4
6
8
A
C
E
1
3
5
7
9
B
D
F
0
1
0
A
B
C
D
E
0
0
1
1
1
0
1
0
0x00000000
0x00001000
0x00001000
0x00001010
0x00103000
0x00000000
0x0000ABCD
0x09080009
6.1
Bus A
Bus B
Modified
0x0
0x1
N/A
0x2
0x3
N/A
0x4
0x5
$0
0x6
0x7
$10
0x8
0x9
$11
0x1010
0x103000
N/A
0x12
0x13
$13
0x14
0x15
N/A
Deliverables
Completion of this part of the lab requires the following deliverables to be turned in to
eCampus along with your completed lab form:
Verilog code for your new register file.
Verilog code for your testbench which tests the register file according to the specification above.
A screen shot of the register files input and output waveforms from the VCS DVE
with the testbench as stimulus.
ALU Block
Design an ALU that provides support for implementing the following instructions: and,
sub, or, xor, slt, addi, ori, sll, slt (ignoring the overflow). All operations must have
a delay of 20 before the output is available. Use non-blocking assignments as shown in the
provided template.
Your ALU should implement the following operations (which are needed to support the
instructions in the pre-lab).
Opperation
AND
OR
ADD
SLL
SRL
SUB
SLT
ADDU
SUBU
XOR
SLTU
NOR
SRA
LUI
Note* Several instructions have subtle requirements. The lui instruction supposed to
take the least significant 16 bits of BusB, and place them in the most significant bits of
BusW, with the lower 16 bits of BusW set to 0. slt is a signed instruction, but in performing
comparisons, verilog assumes unsigned numbers. One way to work around this is to invert
the most significant bit before comparisons. For example, if A and B were both 6 bit signed
numbers, one can perform the comparison in the following manner:
i f ( { A [ 5 ] , A [ 4 : 0 ] } < { B [ 5 ] , B [ 4 : 0 ] } )
For sra, the >>> operation performs an arithmetic shift, but only if the the value is
signed. Again, verilog assumes values are unsigned, so it needs to be told otherwise. The
following example does an arithmetic shift on A by 5:
$ s i g n e d (A) >>> 5 ;
You should use the provided ALU code template ALU.v. You will need to extend it for
your own purposes. The ALU module has the following interface:
module ALU(BusW, Zero , BusA , BusB , ALUCtrl ) ;
10
from eCampus. Make sure to add the test vectors from the prelab to the test bench.
Note: You will implement the ALU control block in a future lab.
7.1
Deliverables
Completion of this part of the lab requires the following deliverables to be turned in to
eCampus along with your completed lab form:
Your Verilog code for the ALU and control block. Ensure your program is clearly
commented.
A file with waveform trace from DVE (print to file) when running the ALUTest.v
testbench. Be sure to set it to require enough pages that the full signal values are
readable in the wavefore trace. Postscript format is fine.