Laboratory No. 9:: Simple Calculator (Final Lab)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Laboratory No.

9:
Simple Calculator (Final Lab)

By: Derek Hildreth


Instructor: Brother Fisher
BYU-Idaho CompE 340
July 16, 2009
I. INTRODUCTION

A. Purpose

Students will combine the sub-modules that were created in the previous lab exercises to complete the
SIMPLE CALC module. They will also use a Verilog generate statement to choose between the two versions of
the COMP module. Students will develop a test bench that thoroughly verifies the structural module. They
will also modify the ROM contents to hold the specific values that you will use in the verification process.
Finally, they will implement the logic, targeting the Spartan-3 FPGA, and examine the contents of the report
files.

The schematic for the calculator is shown in Figure 4 below. Students will write the RTL description for
the top-level module SIMPLE CALC.

B. Equipment

There is a minimal amount of equipment to be used in this lab. The few requirements are listed below:
• Xilinx ISE Navigator Software (v10.0.1)
• Spartan 3E Developer Board
• Computer capable of running the software mentioned

C. Procedure

1. Start the ISE Navigator.


2. Create a new project.
3. Import the .v source files for the MEM, COMP BEH, COMP RTL, ALU and CNTRL FSM sub-modules
4. Modify the source code for the MEM module, updating the ROM contents by using the data that you
recorded earlier for the ALU verification.
5. Write Verilog code and check syntax for the SIMPLE CALC module.
6. Create generate statements that choose either the COMP BEH or COMP RTL sub-modules. Recall that
the COMP BEH module is written in a behavioral style – intended only for simulation – and that the
COMP RTL module is written in an RTL style for synthesis.
7. Perform a syntax check.
8. Create a test bench for SIMPLE CALC and run a simulation to verify functionality.
9. Implement (Place & Route) the design and examine the contents of the MAP and PAR report files.

2
II. SCHEMATIC DIAGRAMS

This section consists of block diagrams which are useful for the laboratory procedure as well as simulation
results.

FIG. 1: Overall block diagram of the calculator project showing the relationship of all of the components.

FIG. 2: RTL view for the simple calculator.

3
FIG. 3: Detailed RTL view for the simple calculator with all modules.

FIG. 4: Simulation results for the entire simple calculator.

4
III. EXPERIMENT DATA

This section will consist of the code blocks that created the counter block as seen in Figure 4.

A. Main Simple Calculator Module

This is the verilog simple calculator file called (SIMPLE CALC.v).


‘timescale 1 ns / 1 ps

‘ d e f i n e SYNTH 1

module SIMPLE CALC (


input CLK,
input RESET,
output RESULT
);

wire [ 3 : 0 ] A IN , B IN , OP CODE, EXP OUT, ALU OUT ;


wire C IN , MEM EN, COMP EN, ALU EN ;
wire [ 2 : 0 ] ADDR;
wire [ 1 6 : 0 ] DATA FRAME ;

ALU U0 ( . A( A IN ) , . B( B IN ) , . C IN ( C IN ) , . OP CODE(OP CODE) , . CLK(CLK) , . EN( ALU EN ) , . Y(ALU OUT) ) ;

CNTRL FSM U2 ( . RESET(RESET) , . CLK(CLK) , . DATA FRAME(DATA FRAME) , . A IN ( A IN ) , . B IN ( B IN ) , . C IN ( C IN ) , . OP CODE(


OP CODE) ,
. EXP(EXP OUT) , . ALU EN ( ALU EN ) , . MEM EN(MEM EN) , . COMP EN(COMP EN) , .ADDR(ADDR) ) ;

MEM U3 ( .ADDR(ADDR) , . DATA FRAME(DATA FRAME) , . CLK(CLK) , . EN(MEM EN) ) ;

generate
i f ( ‘SYNTH )
//COMP RTL U4 (COMP EN, EXPECTED, ALU OUT, CLK, RESULT) ;
COMP RTL U4 ( . COMP EN(COMP EN) , . EXPECTED(EXP OUT) , . ALU OUT(ALU OUT) , . CLK(CLK) , . RESULT(RESULT) ) ;
else
//COMP BEH U5 (COMP EN, EXPECTED, ALU OUT, CLK, RESULT) ;
COMP BEH U5 ( . COMP EN(COMP EN) , . EXPECTED(EXP OUT) , . ALU OUT(ALU OUT) , . CLK(CLK) , . RESULT(RESULT) ) ;
endgenerate

endmodule

This is the text fixture for the simple calculator called (SIMPLE CALC TB.v).
‘timescale 1 ns / 1 ps

module SIMPLE CALC TB ;

// I n p u t s
reg CLK ;
reg RESET ;

// O u t p u t s
wire RESULT ;

// I n s t a n t i a t e t h e Unit Under Test (UUT)


SIMPLE CALC u u t (
. CLK(CLK) ,
. RESET(RESET) ,
. RESULT(RESULT)
);

i n i t i a l begin
// I n i t i a l i z e Inputs
CLK = 0 ;
RESET = 0 ;

#1 RESET = 1 ;
#1 RESET = 0 ;

// Wait 1 0 0 ns for global reset to finish


#100;

end

always
#50 CLK = ˜CLK ;

endmodule

This is the pin out information for the hardware simple calculator called Full.ucf.
#PACE : Start of Constraints generated by PACE

#T h i s ucf file has all the s w i t c h e s and LEDs t h a t might be used i n CompE 2 2 4

5
#M o d i f y this file with your net names and comment o u t or delete the lines not n e e d e d .

#PACE : Start o f PACE I /O P in Assignments

#Main S w i t c h e s
#NET ”SW3” LOC = ”N17” ;
#NET ”SW2” LOC = ”H18” ;
#NET ”SW1” LOC = ” L14 ” ;
#NET ”SW0” LOC = ” L13 ” ;

#E x t r a S w i t c h e s
#NET ”SW7” LOC = ”A6” ;
#NET ”SW6” LOC = ”B6” ;
#NET ”SW5” LOC = ”E7” ;
#NET ”SW4” LOC = ”F7” ;

#Main LED’ s
NET ”RESULT” LOC = ”F9” ;
#NET ”LED6” LOC = ”E9” ;
#NET ”LED5” LOC = ”D11” ;
#NET ”LED4” LOC = ” C11 ” ;
#NET ”LED3” LOC = ” F11 ” ;
#NET ”LED2” LOC = ” E11 ” ;
#NET ”LED1” LOC = ” E12 ” ;
#NET ”LED0” LOC = ” F12 ” ;

#E x t r a LED’ s
#NET ”LED11” LOC = ”B4” ;
#NET ”LED10” LOC = ”A4” ;
#NET ”LED9” LOC = ”D5” ;
#NET ”LED8” LOC = ”C5” ;

#P u s h b u t t o n S w i t c h e s
#NET ” BTN North ” LOC = ”V4” | PULLDOWN ;
NET ”RESET” LOC = ”H13” | PULLDOWN ; # East
NET ”CLK” LOC = ”K17” | PULLDOWN ; #S o u t h
#NET ”BTN West” LOC = ”D18” | PULLDOWN ;

NET ”CLK” CLOCK DEDICATED ROUTE = FALSE ;

#D e b o u n c e r C9 50MHz
#NET ”CLK” LOC = ”C9” ;

#Used f o r c o r r e c t i o n o f e r r o r w i t h r e f r e n c e t o none d e d i c a t e d clock inputs


#Need t o input t h e n e t name i n p l a c e o f s w i t c h i n s i d e the ””
#NET ” s w i t c h ” CLOCK DEDICATED ROUTE = FALSE ;

#PACE : Start o f PACE Area Constraints

#PACE : Start o f PACE P r o h i b i t Constraints

#PACE : End of Constraints generated by PACE

This is the map report for the simple calculator called Map Report.txt.
R e l e a s e 1 0 . 1 . 0 2 Map K. 3 7 ( n t )
X i l i n x Mapping R e p o r t F i l e f o r Design ’ SIMPLE CALC ’

Design Information
−−−−−−−−−−−−−−−−−−
Command L i n e : map − i s e C : / CompE340/ Lab09 / Lab09 . i s e − i n t s t y l e i s e −p
x c 3 s 5 0 0 e −f g 3 2 0 −4 −cm a r e a −p r o f f −k 4 −c 1 0 0 −o SIMPLE CALC map . ncd
SIMPLE CALC . ngd SIMPLE CALC . p c f
Target Device : xc3s500e
Target Package : f g 3 2 0
Target Speed : −4
Mapper V e r s i o n : s p a r t a n 3 e −− $ R e v i s i o n : 1 . 4 6 . 1 2 . 2 $
Mapped Date : Wed J u l 15 2 0 : 2 0 : 5 6 2 0 0 9

D e s i g n Summary
−−−−−−−−−−−−−−
Number o f e r r o r s : 0
Number o f w a r n i n g s : 1
Logic U t i l i z a t i o n :
Number o f S l i c e F l i p F l o p s : 20 o u t o f 9 ,312 1%
Number o f 4 input LUTs : 40 o u t o f 9 ,312 1%
Logic D i s t r i b u t i o n :
Number o f o c c u p i e d S l i c e s : 23 o u t o f 4 ,656 1%
Number o f S l i c e s c o n t a i n i n g o n l y r e l a t e d l o g i c : 23 out o f 23 100%
Number o f S l i c e s c o n t a i n i n g u n r e l a t e d l o g i c : 0 out o f 23 0%
∗ S e e NOTES b e l o w f o r an e x p l a n a t i o n o f t h e e f f e c t s o f unrelated logic .
T o t a l Number o f 4 input LUTs : 40 o u t o f 9 ,312 1%
Number o f bonded IOBs : 3 out o f 232 1%
Number o f BUFGMUXs: 1 out o f 24 4%

Peak Memory Usage : 1 5 3 MB


T o t a l REAL time t o MAP c o m p l e t i o n : 6 secs
T o t a l CPU time t o MAP c o m p l e t i o n : 3 secs

!−−− RESULTS SNIPPED FOR LAB REPORT −−−!

6
B. Individual Modules

This is alu.v.
‘timescale 1 ns / 1 ps

module ALU(
input [ 3 : 0 ] A,
input [ 3 : 0 ] B,
input C IN ,
input [ 3 : 0 ] OP CODE,
input CLK,
input EN,
output reg [3:0] Y
);

always @ ( posedge CLK)

i f (EN)
begin
c a s e (OP CODE) // S p e c i f y o u t p u t s for each address .
4 ’ b0000 :
Y = A + C IN ;
4 ’ b0001 :
Y = A + B + C IN ;
4 ’ b0010 :
Y = A + ( ˜B) + C IN ;
4 ’ b0011 :
Y = A − 1 + C IN ;
4 ’ b0100 :
Y = A & B;
4 ’ b0101 :
Y = A | B;
4 ’ b0110 :
Y = A ˆ B;
4 ’ b0111 :
Y = ˜A ;
4 ’ b1000 :
Y = 0;
default :
Y = 0;
endcase
end
endmodule

This is CNTRL FSM.v.


‘timescale 1 ns / 1 ps

module CNTRL FSM(


input RESET,
input CLK,
input [ 1 6 : 0 ] DATA FRAME,

output reg [ 1 6 : 1 3 ] A IN ,
output reg [ 1 2 : 9 ] B IN ,
output reg C IN ,
output reg [ 7 : 4 ] OP CODE,
output reg [ 3 : 0 ] EXP,

output reg ALU EN ,


output reg MEM EN,
output reg COMP EN,
output reg [ 2 : 0 ] ADDR
);

localparam [ 4 : 0 ]
S0 INIT = 5 ’ b00001 ,
S1 FETCH = 5 ’ b00010 ,
S2 ALU = 5 ’ b00100 ,
S3 COMP = 5 ’ b01000 ,
S4 DONE = 5 ’ b10000 ; // Explicite make sure condition to stay in state

reg [4:0] CURR STATE ;


reg [4:0] NEXT STATE ;
reg [2:0] ADDR I ; // used to i n c r e m e n t ADDR i n t e r n a l l y .

always @ ( posedge CLK, posedge RESET) // c u r r e n t s t a t e p r o c b l o c k


begin
// $ d i s p l a y ( ” C u r r e n t S t a t e : %b @ ADDR I : %d ” , CURR STATE, ADDR I ) ;
i f (RESET == 1 ’ b1 ) // i f r e s e t a s s e r t e d
begin
CURR STATE <= S 0 I N I T ; // R e s e t t o s t a t e 0
ADDR <= 3 ’ b000 ;
end
else
begin
CURR STATE <= NEXT STATE ; // g o t o n e x t s t a t e
ADDR <= ADDR I ;
end
end

always @ (CURR STATE)


begin

A IN = DATA FRAME [ 1 6 : 1 3 ] ;
B IN = DATA FRAME [ 1 2 : 9 ] ;

7
C IN = DATA FRAME [ 8 ] ;
OP CODE = DATA FRAME [ 7 : 4 ] ;
EXP = DATA FRAME [ 3 : 0 ] ;

ADDR I = ADDR; // assignment prevents latch inference .

c a s e (CURR STATE)
S0 INIT :
begin
ALU EN = 1 ’ b0 ;
MEM EN = 1 ’ b0 ;
COMP EN = 1 ’ b0 ;

NEXT STATE = S1 FETCH ;


end
S1 FETCH :
begin
ALU EN = 1 ’ b0 ;
MEM EN = 1 ’ b1 ;
COMP EN = 1 ’ b0 ;

NEXT STATE = S2 ALU ;


end
S2 ALU :
begin
ALU EN = 1 ’ b1 ;
MEM EN = 1 ’ b0 ;
COMP EN = 1 ’ b0 ;

NEXT STATE = S3 COMP ;


end
S3 COMP :
begin
ALU EN = 1 ’ b0 ;
MEM EN = 1 ’ b0 ;
COMP EN = 1 ’ b1 ;

NEXT STATE = S4 DONE ;


end
S4 DONE :
begin
ALU EN = 1 ’ b0 ;
MEM EN = 1 ’ b0 ;
COMP EN = 1 ’ b0 ;

if ( ADDR I >= 3 ’ b101 )


begin
NEXT STATE = S4 DONE ;
end
else
begin
NEXT STATE = S1 FETCH ;
ADDR I = ADDR I + 1 ;
end
end
default :
begin
ALU EN = 1 ’ b0 ;
MEM EN = 1 ’ b0 ;
COMP EN = 1 ’ b0 ;

NEXT STATE = S 0 I N I T ;
end
endcase
end

endmodule

This is comp beh.v.


‘timescale 1 ns / 1 ps

module COMP BEH(


//INPUTS
input wire COMP EN, // 1− b i t − e n a b l e w i r e f r o m FSM
input wire [ 3 : 0 ] EXPECTED, // 4− b i t − e x p e c t e d o u t p u t , l o a d e d f r o m ROM
input wire [ 3 : 0 ] ALU OUT, // 4− b i t − a c t u a l o u t p u t f r o m ALU
input wire CLK , // 1− b i t − c l o c k i n p u t

//OUTPUTS
output reg RESULT // 1− b i t − r e s u l t of comparison
);

always @ ( posedge CLK)


begin
a s s i g n RESULT = EXPECTED == ALU OUT ;

i f ( ! RESULT)
$ d i s p l a y ( ” At t i m e %t , EXP = %b , ACT = %b , RESULT = %b ” , $ t i m e , EXPECTED, ALU OUT, RESULT) ;
end
endmodule

This is comp rtl.v.


‘timescale 1 ns / 1 ps

8
module COMP RTL(
//INPUTS
input wire COMP EN, // 1− b i t − e n a b l e w i r e f r o m FSM
input wire [ 3 : 0 ] EXPECTED, // 4− b i t − e x p e c t e d o u t p u t , l o a d e d f r o m ROM
input wire [ 3 : 0 ] ALU OUT, // 4− b i t − a c t u a l o u t p u t f r o m ALU
input wire CLK , // 1− b i t − c l o c k i n p u t

//OUTPUTS
output reg RESULT // 1− b i t − r e s u l t of comparison
);

always @ ( posedge CLK)


i f (ALU OUT == EXPECTED)
RESULT = 1 ’ b1 ;
else
begin
RESULT = 1 ’ b0 ;
// d i s p l a y d e s c r i b e s s i m u l a t i o n mismatch and s p e c i f i e s t h e s i m l u a t i o n t i m e s t e p when mismatch occured
// $ d i s p l a y ( ” At t i m e %t , EXP = %b , ACT = %b , RESULT = %b ” , $ t i m e , EXPECTED, ALU OUT, RESULT) ;
end

endmodule

This is mem.v.
‘ t i m e s c a l e 1 ns / 1 ps
‘ i n c l u d e ”MY HEADER . t x t ”

module MEM(ADDR, DATA FRAME, CLK, EN) ;


// i n p u t s
input CLK, EN ;
input [ ‘ADDR− 1 : 0 ] ADDR;

// Output
output reg [ ‘WIDTH− 1 : 0 ] DATA FRAME ;

always @ ( posedge CLK)


i f (EN)
c a s e (ADDR)
3 ’ b000 : DATA FRAME = 1 7 ’ b 0 0 1 0 0100 0 0000 0010 ;
3 ’ b001 : DATA FRAME = 1 7 ’ b 0 0 1 0 0100 1 0000 0011 ;
3 ’ b010 : DATA FRAME = 1 7 ’ b 0 0 1 0 0100 0 0001 0110 ;
3 ’ b011 : DATA FRAME = 1 7 ’ b 0 0 1 0 0100 1 0010 1110 ;
3 ’ b100 : DATA FRAME = 1 7 ’ b 0 0 1 1 0101 0 0111 1100 ;
3 ’ b101 : DATA FRAME = 1 7 ’ b 0 0 1 0 0101 0 0101 0111 ;
3 ’ b110 : DATA FRAME = 1 7 ’ b 0 0 0 0 0000 0 0000 0000 ;
3 ’ b111 : DATA FRAME = 1 7 ’ b 0 0 0 0 0000 0 0000 0000 ;
d e f a u l t : DATA FRAME = 1 7 ’ bx ;
endcase

endmodule

This is MY HEADER.txt.
‘ d e f i n e WIDTH 17
‘ d e f i n e ADDR 3

9
IV. DISCUSSION & CONCLUSION

The purpose of this lab was to bring together all of the modules from the previous labs together and create
a simple calculator. This calculator is pre-programmed with the aritmetic and logicical operations with an
expected output which the user give. The actual results are compared and if the calculator worked properly,
the output is a high 1. Students now know how to design, build, test, and synthesis a circuit by using verilog
HDL language.

Personally, I was happy to have worked on this final project with my partner, Tim Price. Together, we
were able to quickly bring all of the modules together, hook them up, simulate, and synthesis the project
to the Spartan 3E board. We had some stumbling blocks along the way, but nothing too sever. The one
module which was giving us most hassle was the CNTRL FSM module. There were all sorts of strange things
it was doing, and we had to modify it a bit from our original one that we did in the previous lab. One in
particular that I can remember was the RST pin being set to a high asserted reset. When we changed it to
be a low asserted, most everything started to work properly.

There were some questions posed in the lab handout that I would like to address here in the conclusion:
• Using the Map Report, answer the following about your design:
– Number of registers?
∗ 20
– Number of 4-input LUTs?
∗ 40
– Number of I/O blocks?
∗ 3
– Number of global clocks?
∗ 1
Other conclusions that I can make is that the overall experience writing in verilog was much nicer and
actually a lot of fun, which is something I didn’t get with Circuit Maker and Cadence OrCAD. This was just
a simple calculator and it demonstrated quite a bit of verilog capability. There’s still a lot to learn about
verilog and it will be exciting to continue learning it to build custom circuits into FPGAs.

10

You might also like