Verilog Laboratory Manual
Verilog Laboratory Manual
G.V.K.Sharma Associate Professor, Department of ECE, GITAM Institute of Technology, GITAM University
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Lab- I
a. Suggested Reading i) Section 1.8 in CMOS Digital Integrated Circuits by Kang and Leblicini TMH Publications b. Suggested Reading ii) Seciton 3.6 and 3.7 in Fundamentals of Digital Logic with Verilog Design by Stephen Brown and Vranesic TMH Publications 2. Demostration of VLSI Design Flow and Usage of Xilinx Project Navigator/Modelsim 3. Synthesis and Simulation of Sample Verilog Models (1-bit Half Adder, 2X4 Decoder etc.) Verilog Model illustrating various Basic Gates
module gates(a, b, y1, y2, y3, y4 ,y5); input a, b; output y1, y2, y3, y4, y5; assign y1 = a&b; // AND Gate assign y21 = a|b; // OR Gate assign y3 = a^b; // XOR Gate assign y4 = ~(a&b); // NAND Gate assign y5 = ~(a|b; // NOR Gate endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Lab -II Modeling Combinational Logic in Verilog 4 X 1 Multiplexer Verilog Model (Dataflow)
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
module mux41(i0, i1, i2, i3, s1, s0, op); input i0, i1, i2, i3, s1, s0; output op; assign op = (~s1 & ~s0 & i0) | (~s1 & s0 & i1) | (s1 & ~s0 & i2) |(s1 & s0 & i3); end module
1 X 4 Demultiplexer
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
endmodule
4 X 2 Encoder
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
op op op op op
= = = = =
4 X 2 Priority Encoder
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Student Exercises 1. 2 X 4 Decoder (Dataflow & Behavioral Models) 2. BCD to 7 Segment Display Code Converter 3. 4-Bit Binary to Gray Code Converter 4. 4-Bit Gray to Binary Code Converter 5. BCD to Excess 3 Code Converter 6. 8 X 3 Encoder & Decoder (using Dataflow and Behavioral Models) 7. 8-Bit Parity Generator
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
y y y y y
= = = = =
module sevenseg(input [3:0] data, output reg [6:0] segments); parameter BLANK = 7b000_0000; parameter ZERO = 7b111_1110; parameter ONE = 7b011_0000; parameter TWO = 7b110_1101; parameter THREE = 7b111_1001; parameter FOUR = 7b011_0011; parameter FIVE = 7b101_1011; parameter SIX = 7b101_1111; parameter SEVEN = 7b111_0000; parameter EIGHT = 7b111_1111; parameter NINE = 7b111_1011; always @(*) case(data) 0: segments = ZEROS; 1: segments = ONE; 2: segments = TWO; 3: segments = THREE; 4: segments = FOUR; 5: segments = FIVE; 6: segments = SIX; 7: segments = SEVEN; 8: segments = EIGHT; 9: segments = NINE; default: segments = BLANK; endcase endmodule
Lab-III Modeling Combinational Logic in Verilog (Structural Modeling) Model a 4 X 1 Multiplexer using 2 X 1 Multiplexers
module mux21(i0, i1, s, op); input i0,i1,s; output op; assign op = s ? i1 : i0; endmodule module mux41(ip, sel, op); input [0:3] ip; input [1:0] sel; output op; wire i1, i2; mux21 m1(ip[0], ip[1],sel[0], i1); mux21 m2(ip[2], ip[3],sel[0], i2); mux21 m3(i1, i2,sel[1], op); endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Model a 4-bit Full Adder/Substractor using 1-bit Full Adders Verilog Model of a 1-bit Full Adder
module fadd1(a,b,cin,sum,cout); input a,b,cin; output sum,cout; assign sum = a^b^cin; assign cout = (a&b)|(b&cin)|(cin&a); endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
m[0:3], en); y[0:3], m[0]); y[4:7], m[1]); m[8:11], m[2]); m[12:15], m[3]);
module comp5(a,b,agt,bgt); input [4:0] a,b; output agt,bgt; wire [4:1] acarry, bcarry; comp1 c1(a[4], comp1 c2(a[3], comp1 c3(a[2], comp1 c4(a[1], comp1 c5(a[0], endmodule b[4], b[3], b[2], b[1], b[0],
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
1b1, 1b1, acarry[4], bcarry[4]); acarry[4], bcarry[4], acarry[3], bcarry[3]); acarry[3], bcarry[3], acarry[2], bcarry[2]); acarry[2], bcarry[2], acarry[1], bcarry[1]); acarry[1], bcarry[1], agt, bgt);
Student Exercise 1. Realize a 4-bit X 4-bit Array multiplier with 1-bit HAs and 1-bitFAs using Structural Modeling
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
always@(posedge clk or negedge rst) begin if(!rst) q=0; else q = d; end endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Verilog Model of a 4-Bit Ripple Counter (Structural Model) using JK Flip Flop
module jkff(j, k, rst, clk, q); input j, k, rst, clk; output q; reg state; assign q = state; always @(negedge rst or posedge clk) begin if(!rst) state = 1b0; else if(j=1b1 & k=1b0) state = 1b1; else if(j=1b0& k=1b1) state = 1b0; else if(j=1b1 & k=1b1) state = ~state; end endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
module ripcount(rst, clk, q); input rst, clk; inout [3:0] q; jkff f1(1b1, jkff f2(1b1, jkff f3(1b1, jkff f4(1b1, endmodule 1b1, 1b1, 1b1, 1b1, rst, rst, rst, rst, clk, q[0]); q[0], q[1]); q[1], q[2]); q[2], q[3]);
Verilog Model of a 4-bit Synchronous Counter with synchronous reset (Behavioral Model)
module syncount(rst, cen, clk, q); input rst, cen, clk; output reg [3:0] q; always @(posedge clk) begin if(rst) q =4b000; else if(cen) q = q+4d1; end endmodule
Student Exercises 1. Develop the Verilog Model of a MOD-12 Synchronous Counter using D Flip Flops and appropriate dataflow statements 2. Develop the Verilog Model of a MOD-12 Synchronous Counter using JK Flip Flops and appropriate dataflow statements 3. Develop the Behavioral Verilog Model of a MOD-12 Synchronous Counter using always statements 4. Model a Ring Counter and Johnson Counter using Behvavioral & Structural Modeling (using DFFs) 5. Model a 4-Bit Up counter with parallel load
Prepared By G.V.K.Sharma, Department of ECE, GITAM Institute of Technology, GITAM University
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
module univsh(d, clk, sel, rst, serin, serout); input [3:0] d; input [1:0] sel; input clk, serin, ; output serout; output reg [3:0] q; always @(posedge clk or negedge rst) begin if(!rst) q = 5b00000; else if(sel == 2b00) q = d; else if(sel == 2b01) q = {serin, q[3:1]}; else if(sel == 2b10) q = {q[2:0], serin}; else q = q; end assign serout = q[0]; endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
module ram(clk, addr, wrb, din, dout); input clk; input [5:0] addr; input wrb; input [16:0] din; output [15:0] dout; reg [15:0] men[63:0]; always @(posedge clk) if(~wrb) mem[addr] = din; assign din = mem[addr]; endmodule
Finite State Machines Every sequential logic circuit consists of combinational logic circuit along with flip flops. The set of all the flip flops can be thought of as a register and the various set of values taken by all the flipflops taken together (register) can be named as states. We see that a sequential logic circuit essentially has two parts. First, a combinational logic circuit that takes inputs and current state and generates the output and nexstate. Second, a state register that accepts the nextstate and produces it at the output (as the current state) after the positive edge of clock pulse. The behavior of the combinational logic circuit can be modeled using an always block along with case statement (wherein the outputs for various input combinations are mentioned). The behavior of the state register can be modeling as an n-bit register (if number of states <=2N) using always block MOORE State Machine: The output of a moore state machine depends only on the state and not on the inputs.
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Model a Three 1s Detector using a Moore State Machine and code it in Verilog
module moore(ip, clk, op, rst); input ip, clk, rst; output op; reg op; parameter S0=0, S1=1, S2=2, S3=3; reg [0:1] cs, ns; always @(posedge clk or posedge rst) // State Register begin if(rst) cs = S0; else cs = ns; end
always @(cs) //Combinational Logic that implements state logic case(cs) S0: begin op =0; if(ip) ns = S1; else ns = S0; end S1: begin op =0; if(ip) ns = S2; end S2: begin op =0; if(ip) ns = S3; end S3: begin op =1; if(ip) ns = S3; end default: op = 0; ns = S0; endcase end endmodule else ns = S0; else ns = S0; else ns = S0;
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
always @(posedge clk or negedge rst) // State register that hold cs begin if(!rst) cs = 4b1000; else cs = ns; end always @(cs) // Combinational logic that generates ns from cs begin case(cs) 4b1000: ns = 4b0100; 4b0100: ns = 4b0010; 4b0010: ns = 4b0001; 4b0001: ns = 4b1000; default: ns = 4b1000; endcase end assign q = cs; endmodule
Student Exercise: 1. Model a 3-bit Johnson Counter as a Moore State Machine 2. Derive the sequence detector for the sequence 0101 and model it as a Moore State Machine MEALY State Machine
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
module control(clk,st,ld,sh,m,done ); input clk, m, st; output ld, sh, done; reg [3:0] cs,ns; reg ld, sh, done; always@(posedge clk) cs = ns; always@(cs or st or m) begin case(cs) 4'd0 : if(st) begin else begin 4'd1 : if(m) begin else begin
ns = 4'd1; ld = 1'b1; sh = 1'b0; done =1'b0; end ns = 4'd0; ld = 1'b0; sh = 1'b0; done =1'b0;end ns = 4'd2; ld = 1'b1; sh = 1'b0; done =1'b0;end ns = 4'd3; ld = 1'b0; sh = 1'b1; done =1'b0;end
4'd2 : begin ns = 4'd3; ld = 1'b0; sh = 1'b1; done =1'b0;end 4'd3 : if(m) begin ns = 4'd4; ld = 1'b1; sh = 1'b0; done =1'b0;end else begin ns = 4'd5; ld = 1'b0; sh = 1'b1; done =1'b0;end 4'd4 : begin ns = 4'd5; ld = 1'b0; sh = 1'b1; done =1'b0;end 4'd5 : if(m) begin ns = 4'd6; ld = 1'b1; sh = 1'b0; done =1'b0;end else begin ns = 4'd7; ld = 1'b0; sh = 1'b1; done =1'b0;end 4'd6 : begin ns = 4'd7; ld = 1'b0; sh = 1'b1; done =1'b0;end 4'd7 : if(m) begin ns = 4'd8; ld = 1'b1; sh = 1'b0; done =1'b0;end else begin ns = 4'd9; ld = 1'b0; sh = 1'b1; done =1'b0;end 4'd8 : begin ns = 4'd9; ld = 1'b0; sh = 1'b1; done =1'b0;end 4'd9 : begin ns = 4'd0; ld = 1'b0; sh = 1'b0; done =1'b1;end default: begin ns = 4'd0; ld = 1'b0; sh = 1'b0; end endcase end endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
module fa1(a,b,cin,sum,cout); input a; input b; input cin; output sum; output cout; assign sum = a ^ b ^ cin; assign cout = (a&b) | (b&cin) | (cin&a); endmodule
1'b0, sum[0], c1); c1, sum[1], c2); c2, sum[2], c3); c3, sum[3], cout);
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
module mul(x,y,prod,st,clk,done); input [3:0] x; input [3:0] y; output [8:0] prod; output done; input st; input clk; wire [8:0] tempd; wire [8:0] q; wire [3:0] sum; wire cout; wire ld, sh, m; assign m = q[0]; assign tempd = st ? {5'b00000,y} : {cout,sum, q[3:0]}; shireg s1 (tempd, q,ld,sh,clk); fa4 f1 (x,q[7:4] ,sum, cout); control c1 (clk,st,ld,sh,m,done); assign prod = q; endmodule
Design Project II: Greatest Common Divisor (GCD) calculator Verilog Model for Comparator
module comp4(a,b,altb); input [3:0] a; input [3:0] b; output altb; assign altb = a < b; endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
module f1(a,b,cin,sum,cout); input a; input b; input cin; output sum; output cout; assign sum = a ^ b ^ cin; assign cout = (a&b) | (b&cin) | (cin&a); endmodule
module sub4(a,b,sum); input [3:0] a; input [3:0] b; output [3:0] sum; wire c1, c2, c3,cout; f1 fulladd1(a[0], ~b[0], f1 fulladd2(a[1], ~b[1], f1 fulladd3(a[2], ~b[2], f1 fulladd4(a[3], ~b[3], endmodule
1'b1, sum[0], c1); c1, sum[1], c2); c2, sum[2], c3); c3, sum[3], cout);
module reg4(d,clk,ld,q); input [3:0] d; input clk; input ld; output [3:0] q; reg [3:0] q; always @(posedge clk) begin if(ld) q = d; end endmodule
module neq4(a,b,aneqb); input [3:0] a; input [3:0] b; output aneqb; assign aneqb = a != b; endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
module control(clk,go,xneqy,xlty,xsel,ysel,xld,yld,dld); input clk, go, xneqy, xlty; output xsel, ysel, xld, yld, dld; reg xsel, ysel, xld, yld, dld; reg [2:0] cs, ns; always @(posedge clk) cs = ns; always @(cs or go or xneqy or xlty) begin case (cs) 3'b000: begin if(go) ns = 3'b001; else ns = 3'b000; xsel = 1'b0; ysel = 1'b0; xld = 1'b0; yld = 1'b0; dld=1'b0; end 3'b001: begin ns = 3'b010; xsel = 1'b0; ysel = 1'b0; xld = 1'b1; yld = 1'b1; dld=1'b0; end 3'b010: begin if(xneqy) if(xlty) ns = 3'b011; else ns = 3'b100; else ns = 3'b101; xsel = 1'b0; ysel = 1'b0; end 3'b011: begin ns = 3'b010; xsel = 1'b0; ysel = 1'b1; end 3'b100: begin ns = 3'b010; xsel = 1'b1; ysel = 1'b0; end 3'b101: begin ns = 3'b000; xsel = 1'b0; ysel = 1'b0; end default: begin ns = 3'b000; xsel = 1'b0; ysel = 1'b0; end endcase end endmodule
module mux421(a,b,sel,zout); input [3:0] a; input [3:0] b; input sel; output [3:0] zout; assign zout = sel ? b : a; endmodule
Department of Electronics and Communication Engineering GITAM Institute of Technology, GITAM University, Visakhapatnam
xsuby, ysubx; zout1, zout2; regx, regy; xsel, ysel, xld, yld, dld, xneqy; (clk,go,xneqy,xlty,xsel,ysel,xld,yld,dld); (x, xsuby, xsel,zout1); (y, ysubx, ysel,zout2); (zout1, clk, xld, regx); (zout2, clk, yld, regy); (regx, regy, xsuby); (regy, regx, ysubx); (regx, regy, xlty); (regx, regy, xneqy); (regx, clk, dld, zout);
control cc mux421 m1 mux421 m2 reg4 xreg reg4 yreg sub4 s1 sub4 s2 comp4 c1 neq4 n1 reg4 d endmodule