Professional Documents
Culture Documents
Uday Intern Project Original 2
Uday Intern Project Original 2
Affiliation (Institution/Company):RGUKT,Basar
Email:iiit.pendem@gmail.com Phone:8106113419
Title of Internship Program Undertaken:Design and Verification using Verilog Internship Program
Important Instructions:
1) All assignment results should be computer screenshot or computer typed. Handwritten and
scanned copies shall not be considered for evaluation
2) Due date for all assignment submission is 1 Week from the last date of internship
3) All assignment questions should be captured along with solutions/answers.
4) Code snippets, simulation results should be captured properly
5) Use only the JPEG image format for capturing the simulation results and name/label the results
appropriately.
6) The description of answers should be short and crisp. Provide only the required information,
answered copied or cut and pasted from google shall not be considered.
=(7)+(11*16)+(13*256)+(2*4096)
…………………………
………………………….
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
-39+92 = ( 00110101)2 = 53
Q: 3). Represent the Boolean expression f (A,B,C) = A’BC’+A’BC+A’B’C’+ABC in SOP and POS forms?
The above SOP and POS forms can be written in simplified form by using K-map simplification as :
Since 2^k>=n+k+1
Therefore k=4 satisfies the above equation.
Answer is “4”.
5. Convert Binary code 0101 to gray code and convert gray code 1010 to binary?
Solution:
module casex_example();
always @ (opcode a or b or c)
casex(opcode)
%b",$time,opcode); end
%b",$time,opcode); end
%b",$time,opcode); end
default : begin
endcase
always #2 a = $random;
always #2 b = $random;
always #2 c = $random;
initial begin
opcode = 0;
#2 opcode = 3'b101x;
#2 opcode = 4'b0101;
#2 opcode = 0010;
#2 opcode = 4'b0000;
#2 $finish;
end
endmodule
Solution:
module casex_example(out,a,b,c,opcode);
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
input [30] opcode;
input [10] a,b,c;
output reg [10]out;
always@(opcode)
begin
casex(opcode)
4'b1zzx begin
Don't care 20 bits
out = a;
$display("@%0dns 4'b1zzx is selected, opcode %b",$time,opcode); end
4'b01 begin
bit 10 is don't care
out = b;
$display("@%0dns 4'b01 is selected, opcode %b",$time,opcode); end
4'b001 begin
bit 0 is don't care
out = c;
$display("@%0dns 4'b001 is selected, opcode %b",$time,opcode); end
default begin
$display("@%0dns default is selected, opcode %b,$time,opcode); end
endcase
end
endmodule
//Testbench code goes here
module casex_tb;
reg [30]opcode;
reg [10]a,b,c;
wire [10]out;
casex_example c1(.out(out),.a(a),.b(b),.c(c),.opcode(opcode));
always #2 a = $random;
always #2 b = $random;
always #2 c = $random;
initial begin
opcode = 0;
#2 opcode = 3'b101x;
#2 opcode = 4'b0101;
#2 opcode = 0010;
#2 opcode = 4'b0000;
#2 $finish; end
endmodule
//result
//result
task conversion;
input [7:0]c;
output reg [7:0]f;
begin
f={c_to_f(c)};
end
endtask
always @ (c)
begin
conversion(c,f);
$display("@%0dns centigrade c=%0d to fahrenheit f=%0d",$time,c,f);
end
endmodule
//tb
module temp_conv_tb;
// Inputs
reg [7:0] c;
// Outputs
wire [7:0] f;
initial begin
// Initialize Inputs
c = 0;
#10;
c = 10;
endmodule
//Result
@0ns centigrade c=0 to fahrenheit f=32
@10ns centigrade c=10 to fahrenheit f=50
@20ns centigrade c=20 to fahrenheit f=68
3. Generate a 200 MHz clock and simulate and attach the output waveform?
Solution:
module clock_gen();
reg clk;
endmodule
module test;
reg clk;
realtime delay=2.5;
// Outputs
initial clk=1'b1;
// Initialize Inputs
always #(delay) clk=~clk;
// Wait 100 ns for global reset to finish
initial begin
#100;
$finish;
end
// Add stimulus here
endmodule
Solution:
module casexz(next,present);
input [3:0]present;
output reg [3:0]next;
always@(present)
begin
casex(present)
4'bzzx1:begin next=1;
$display("next value is %d",next);
end
4'bxz1x:
begin
next=2;
$display("next value is %d",next);
end
endcase
casez(present)
4'bx1zz:begin
next=3;
$display("next value is %d",next);
end
4'b1xzz:begin
next=4;
$display("next value is %d",next);
end
endcase
end
endmodule
//tb
module tb;
reg [3:0]present;
wire [3:0]next;
casexz x1(next,present);
initial begin
present=4'b01z1;
#10
present=4'bxx10;
#10
present=4'bx100;
#10;
present=4'b1x00;
end
endmodule
//result
next value is 1
next value is 2
next value is 3
next value is 4
//result
mux2_1 m0(w1,s1,a,b);
mux2_1 m1(w2,s1,c,d);
mux2_1 m2(y,s0,w1,w2);
endmodule
//test_bench
`define S $stop;
module tick_mux4_1_tb;
// Inputs
reg s1;
reg s0;
reg a;
reg b;
reg c;
reg d;
parameter delay=10;
// Outputs
wire y;
initial begin
// Initialize Inputs
s0 = 0;
s1 = 0;
a = 1;
b = 0;
c = 0;
d = 0;
#(delay);
s0=0;s1=1;a=0;b=1;c=0;d=0;
#(delay);
s0=1;s1=0;a=0;b=0;c=1;d=0;
#(delay);
s0=1;s1=1;a=0;b=0;c=0;d=1;
#(delay);
`S
end
// Wait 100 ns for global reset to finish
initial begin
#100;
$finish;
end
initial
$monitor($time,"a=%b,b=%b,c=%b,d=%b,s0=%b,s1=%b,y=%b",a,b,c,d,s0,s1,y);
endmodule
//output
Finished circuit initialization process.
0a=1,b=0,c=0,d=0,s0=0,s1=0,y=1
10a=0,b=1,c=0,d=0,s0=0,s1=1,y=1
20a=0,b=0,c=1,d=0,s0=1,s1=0,y=1
30a=0,b=0,c=0,d=1,s0=1,s1=1,y=1
Stopped at time : 40 ns
//"mux2_1.v"
module mux2_1(y,s,a,b);
input a,b,s;
output y;
assign y=s?b:a;
endmodule
//result
//tb
module tb;
reg [3:0]i;
reg [1:0]s;
wire [3:0]y1,y2,y3,y4;
demux d1(y1,y2,y3,y4,i,s);
initial
begin
i=15;
s=2'b01;
$strobe("value of y2=%d when selection line is %b",y2,s);
#10
s=2'b11;
$strobe("value of y4=%d when selection line is %b",y4,s);
#10
s=2'b00;
$strobe("value of y1=%d when selection line is %b",y1,s);
end
endmodule
//result
value of y2=15
value of y4=15
value of y1=15
2. Design and Verify 8 to 3 Priority Encoder?
Solution:
module encoder(y,i,en);
input [0:7]i;
input en;
output reg [2:0]y;
always@(*)
begin
//testbench
module tb;
reg [0:7]i;reg en;
wire [2:0]y;
encoder e1(y,i,en);
initial
begin
en=1;
i=8'b00000001;
#10
i=8'b00000101;
#10
i=8'b10100000;
#10
i=8'b00100001;
#10 en=0;
i=8'b00000011;
end
initial
$monitor($time," output values are y=%d when input is %b",y,i);
endmodule
//result
0 output values are y=0 when input is 00000001
10 output values are y=2 when input is 00000101
20 output values are y=7 when input is 10100000
30 output values are y=5 when input is 00100001
40 make enable as HIGH
//testbench
module tb;
reg [3:0]a,b;
reg cin;
wire [3:0]sout;
wire cout;
rca r1(cout,sout,a,b,cin);
initial begin
cin=0;a=4'b0000;b=4'b0011;
#10 a=4'b0011; b=4'b0010;
#10 a=4'b0001; b=4'b0011;
#10 a=4'b1001; b=4'b1011;end
initial
$monitor($time," when inputs are a=%b b=%b ,output cout=%b sum=%b ",a,b,cout,sout);
endmodule
//result
0 when inputs are a=0000 b=0011 ,output cout=0 sum=0011
10 when inputs are a=0011 b=0010 ,output cout=0 sum=0101
20 when inputs are a=0001 b=0011 ,output cout=0 sum=0100
30 when inputs are a=1001 b=1011 ,output cout=1 sum=0100
5. Design BCD to 7 segment display and verify?
Solution:
module bcdconversion(a,b,c,d,e,f,g,in);
input [3:0]in;
output reg a,b,c,d,e,f,g;
always@(*)
begin
if(in>=0 && in<10)
begin
case(in)
4'b0000:begin
a=1;b=1;c=1;d=1;e=1;f=1;g=0;
$display($time," Displaying 0 when input is %b",in);end
4'b0001:begin
a=1;b=1;c=0;d=0;e=0;f=0;g=0;
$display($time," Displaying 1 when input is %b",in);end
4'b0010:begin
a=1;b=1;c=0;d=1;e=1;f=0;g=1;
$display($time," Displaying 2 when input is %b",in);end
4'b0011:begin
a=1;b=1;c=1;d=1;e=0;f=0;g=1;
$display($time," Displaying 3 when input is %b",in);end
4'b0100:begin
a=0;b=1;c=1;d=0;e=0;f=1;g=1;
$display($time," Displaying 4 when input is %b",in);end
4'b0101:begin
a=1;b=0;c=1;d=1;e=0;f=1;g=0;
$display($time," Displaying 5 when input is %b",in);end
4'b0110:begin
a=1;b=0;c=1;d=1;e=1;f=1;g=1;
$display($time," Displaying 6 when input is %b",in);end
4'b0111:begin
a=1;b=1;c=1;d=1;e=0;f=0;g=0;
$display($time," Displaying 7 when input is %b",in);end
endcase
end
else
//testbench
module tb;
reg [3:0]in;
wire a,b,c,d,e,f,g;
bcdconversion b2(a,b,c,d,e,f,g,in);
initial
begin
in=4'b0010;
#10 in=4'b0001;
#10 in=4'b0011;
#10 in=4'b0111;
#10 in=4'b0000;
#10 in=4'b0100;
#10 in=4'b0110;
#10 in=4'b0101;
#10 in=4'b1111;
end
endmodule
//result
0 Displaying 2 when input is 0010
10 Displaying 1 when input is 0001
20 Displaying 3 when input is 0011
30 Displaying 7 when input is 0111
40 Displaying 0 when input is 0000
50 Displaying 4 when input is 0100
60 Displaying 6 when input is 0110
70 Displaying 5 when input is 0101
enter the correct input between 0 and 9
Session 07: Design and Verification of Sequential circuits
1.Write Verilog codes for latch and a D flip flop and verify?
Solution:
//SR Latch
module srlatch(s,r,en,out);
input s,r,en;
output reg out;
reg temp=0;
always@(*)
begin
if(en==1)begin
casex({s,r})
2'b00:out<=temp;
2'b01:begin
out<=0;
temp<=out;end
2'b10:begin
out<=1;
temp<=out;end
2'b11:
$display("\t it is not valid");
endcase
end
end
endmodule
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
//tb for sr latch
module tb;
reg s,r,en;
wire out;
srlatch s1(s,r,en,out);
initial
begin
en=1;
s=0;r=0;
#10 s=0;r=1;
#10 s=1;r=0;
//#10 s=1;r=1;
#10 s=1;r=0;
#10 s=0;r=0;
end
initial
$monitor($time, " when s=%b r=%b then output values are %b",s,r,out);
endmodule
//result
//D Latch
module dlatch(d,en,out);
input d,en;
output reg out;
always@(*)
begin
if(en==1)begin
casex(d)
1'b0:
out<=0;
1'b1:
out<=1;
endcase
end
end
endmodule
//tb
module tb;
reg d,en;
wire out;
dlatch d1(d,en,out);
initial
begin
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
$dumpfile("dump.vcd");
$dumpvars;
en=1;
d=0;
#10 d=1;
#10 d=0;
#10 d=1;
#10 d=0;
end
initial
$monitor($time, " when d=%b then output values are %b",d,out);
endmodule
//result
0 when d=0 then output values are 0
10 when d=1 then output values are 1
20 when d=0 then output values are 0
30 when d=1 then output values are 1
40 when d=0 then output values are 0
//result
0 when j=0 k=0 then output values are 0
10 when j=0 k=1 then output values are 0
20 when j=1 k=0 then output values are 1
40 when j=1 k=1 then output values are 0
50 when j=1 k=0 then output values are 1
60 when j=0 k=0 then output values are 0
70 when j=0 k=1 then output values are 0
80 when j=0 k=0 then output values are 0
// T Flipflop
module tff(t,clk,out);
input t,clk;
output reg out;
reg temp=0;
always@(posedge clk or t)
begin
case(t)
1'b0:out<=temp;
1'b1:out<=~temp;
endcase
end
endmodule
//tb
module tb;
reg t,clk;
wire out;
tff t1(t,clk,out);
initial
begin
$dumpfile("dump.vcd");
$dumpvars;
clk=0;
t=0;
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
#20 t=1;
#10 t=0;
#20 t=1;
#10 t=0;
end
always
#10 clk=~clk;
initial
$monitor($time, " when t=%b then output values are %b",t,out);
initial
#100 $finish;
endmodule
//result
0 when t=0 then output values are 0
20 when t=1 then output values are 1
30 when t=0 then output values are 0
50 when t=1 then output values are 1
60 when t=0 then output values are 0
3. Develop Verilog code for 4 bit synchronous shift register? use clock frequency of 100 Mhz
in test bench?
Solution:module syn_4bit_shiftreg(clk,reset,d,q,ctrl);
input clk,reset,d;
input [1:0]ctrl;
output reg[3:0]q;
always @(posedge clk, posedge reset)
begin
if(reset)
q<=4'b0000;
else
begin
if(ctrl==2'b00)
begin
q[3]<=d;
q[2]<=q[3];
q[1]<=q[2];
q[0]<=q[1];
end
else if(ctrl==2'b01)
begin
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
q[0]<=d;
q[1]<=q[0];
q[2]<=q[1];
q[3]<=q[2];
end
else if(ctrl==2'b10)
begin
q[2]<=q[3];
q[1]<=q[2];
q[0]<=q[1];
q[3]<=q[0];
end
else
begin
q[0]<=q[3];
q[1]<=q[0];
q[2]<=q[1];
q[3]<=q[2];
end
end
end
endmodule
//tb
module syn_4bitshiftreg_tb;
// Inputs
reg clk;
reg reset;
reg d;
reg [1:0] ctrl;
// Outputs
wire [3:0] q;
initial
// Initialize Inputs
clk = 0;
always #5 clk=~clk; //clock_period=10ns =>clock_frequency=100MHz.
initial begin
reset = 1;
d = 0;
ctrl = 0;
#10;
ctrl=2'b00;
reset=0;
d=1;#10 d=0;#10 d=1;#10 d=0;
#10;
ctrl=2'b01;
//output
@0ns reset=1 ctrl=00 d=0 output=0000
@10ns reset=0 ctrl=00 d=1 output=0000
@15ns reset=0 ctrl=00 d=1 output=1000
@20ns reset=0 ctrl=00 d=0 output=1000
@25ns reset=0 ctrl=00 d=0 output=0100
@30ns reset=0 ctrl=00 d=1 output=0100
@35ns reset=0 ctrl=00 d=1 output=1010
@40ns reset=0 ctrl=00 d=0 output=1010
@45ns reset=0 ctrl=00 d=0 output=0101
@50ns reset=0 ctrl=01 d=0 output=0101
@55ns reset=0 ctrl=01 d=0 output=1010
@60ns reset=0 ctrl=01 d=1 output=1010
@65ns reset=0 ctrl=01 d=1 output=0101
@70ns reset=0 ctrl=10 d=1 output=0101
@75ns reset=0 ctrl=10 d=1 output=1010
@80ns reset=0 ctrl=11 d=1 output=1010
@85ns reset=0 ctrl=11 d=1 output=0101
@95ns reset=0 ctrl=11 d=1 output=1010
Stopped at time : 100 ns
parameter n = 24;
always @(posedge clk or rst)
begin
if(rst)
out <= 'd0;
else if(out < (n-1))
out <= out + 1;
else
out <= 'd0;
end
endmodule
//testbench
module count_n_tb;
reg clk,rst;
wire[4:0] out;
count_n coun_n_inst(clk,rst,out);
initial
begin
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
$dumpfile("dump.vcd");
$dumpvars;
clk = 1'b0;
rst = 1'b1;
#6 rst = 1'b0;
end
always #5 clk <= ~ clk;
initial
$monitor("output values are %d",out);
initial
#1000 $finish;
endmodule
//result
VCD info: dumpfile dump.vcd opened for output.
output values are 0
output values are 1
output values are 2
output values are 3
output values are 4
output values are 5
output values are 6
output values are 7
output values are 8
output values are 9
output values are 10
output values are 11
output values are 12
output values are 13
output values are 14
output values are 15
output values are 16
output values are 17
output values are 18
output values are 19
output values are 20
output values are 21
output values are 22
output values are 23
output values are 0
output values are 1
output values are 2
output values are 3
output values are 4
output values are 5
output values are 6
output values are 7
output values are 8
output values are 9
output values are 10
output values are 11
output values are 12
output values are 13
output values are 14
output values are 15
output values are 16
output values are 17
output values are 18
output values are 19
( input clk,
input rstn,
output reg out1,out2);
//tb
module tb;
parameter N = 4;
reg clk;
reg rstn;
wire out1,out2;
modN_ctr u0 ( .clk(clk),
.rstn(rstn),
.out1(out1),.out2(out2));
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
always #10 clk = ~clk;
initial begin
$dumpfile("dump.vcd");
$dumpvars;
end
initial begin
{clk, rstn} <= 0;
//result
VCD info: dumpfile dump.vcd opened for output.
T=0 rstn=0 out1=x out2=x
T=10 rstn=0 out1=0 out2=0
T=30 rstn=1 out1=0 out2=0
T=50 rstn=1 out1=0 out2=1
T=70 rstn=1 out1=1 out2=0
T=90 rstn=1 out1=1 out2=1
T=110 rstn=1 out1=0 out2=0
T=130 rstn=1 out1=0 out2=1
T=150 rstn=1 out1=1 out2=0
T=170 rstn=1 out1=1 out2=1
T=190 rstn=1 out1=0 out2=0
T=210 rstn=1 out1=0 out2=1
T=230 rstn=1 out1=1 out2=0
T=250 rstn=1 out1=1 out2=1
T=270 rstn=1 out1=0 out2=0
T=290 rstn=1 out1=0 out2=1
T=310 rstn=1 out1=1 out2=0
T=330 rstn=1 out1=1 out2=1
T=350 rstn=1 out1=0 out2=0
T=370 rstn=1 out1=0 out2=1
T=390 rstn=1 out1=1 out2=0
T=410 rstn=1 out1=1 out2=1
T=430 rstn=1 out1=0 out2=0
1. Calculate the max frequency of the circuit with 2 flip flops connected back to back with a
combinational circuit between flip flops with following timings given.
a. flip flop 1 - delay - 2 ns,
b. combinational circuit delay - 4 ns
c. setup time of flip flop 1 - 2 ns
d. setup time of flip flop 2 - 2 ns
e. flip flop 2 delay - 2 ns
Solution:
//testbench
`timescale 1ns/1ps
module double_sync_tb;
integer count = 0;
reg clk, D1, resetn;
wire q2;
double_sync ds_inst (.clock(clk), .resetn(resetn), .D1(D1), .Q2(Q2));
always #10 clk = ~clk ;
initial begin
$dumpfile("dump.vcd");
$dumpvars;
clk=0;
resetn = 0;
#1;
resetn = 1;
D1 = 0;
for (count = 0 ; count < 10; count = count+1) begin
D1 = count;
#15;
end
$finish;
end
endmodule
Projects :
1. State machine design- Mandatory
2. Dual Port Memory design- Mandatory
3. Serial Adder design- Mandatory
4. FIFO Design and Verification - Mandatory
5. APB Memory design - Optional
//result
VCD info: dumpfile dump.vcd opened for output.
0 out is x when state is xx for input is x
5 out is 0 when state is 00 for input is x
25 out is 0 when state is 01 for input is 1
35 out is 0 when state is 10 for input is 0
sequence is detected
45 out is 0 when state is 11 for input is 1
55 out is 1 when state is 01 for input is 1
65 out is 0 when state is 10 for input is 0
sequence is detected
75 out is 0 when state is 11 for input is 1
85 out is 1 when state is 01 for input is 1
95 out is 0 when state is 10 for input is 0
sequence is detected
105 out is 0 when state is 11 for input is 1
115 out is 1 when state is 01 for input is 1
125 out is 0 when state is 10 for input is 0
sequence is detected
135 out is 0 when state is 11 for input is 1
145 out is 1 when state is 01 for input is 1
155 out is 0 when state is 10 for input is 0
sequence is detected
165 out is 0 when state is 11 for input is 1
175 out is 1 when state is 01 for input is 1
185 out is 0 when state is 00 for input is 1
195 out is 0 when state is 01 for input is 1
Solution:
module dualport(q_a,q_b,data_a,data_b,addr_a,addr_b,we_a,we_b,re_a,re_b,clk_a,clk_b);
input [7:0]data_a,data_b;
input [7:0]addr_a,addr_b;
input we_a,we_b,re_a,re_b,clk_a,clk_b;
output reg [7:0]q_a,q_b;
//memory
reg [7:0]ram[255:0];
always@(posedge clk_a)
begin
if(we_a)//writing into memory
ram[addr_a]<=data_a;
else if(re_a)//reading from memory
q_a<=ram[addr_a];
else
q_a<=0;
end
always@(posedge clk_b)
begin
if(we_b)//writing into memory
ram[addr_b]<=data_b;
else if(re_b)//reading from memory
q_b<=ram[addr_b];
else
q_b<=0;
end
endmodule
//tb
module tb;
reg [7:0]data_a,data_b;
reg [7:0]addr_a,addr_b;
reg we_a,we_b,re_a,re_b,clk_a,clk_b;
wire [7:0]q_a,q_b;
dualport df(q_a,q_b,data_a,data_b,addr_a,addr_b,we_a,we_b,re_a,re_b,clk_a,clk_b);
initial
begin
data_a=15;
data_b=11;
clk_a=0;clk_b=0;
addr_a=8'b11010101; addr_b=8'b00101011;
#10 we_a=1;we_b=1;
#10 we_a=0;we_b=0;
#10 re_a=1;re_b=1;
#60 re_a=0;re_b=0;end
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
always #10 clk_a=~clk_a;
always #10 clk_b=~clk_b;
initial
$monitor($time," output qa=%b qb=%b",q_a,q_b);
initial
#150 $finish;
initial begin
$dumpfile("dump.vcd");
$dumpvars; end
endmodule
//result
0 output qa=xxxxxxxx qb=xxxxxxxx
30 output qa=00001111 qb=00001011
90 output qa=00000000 qb=00000000
Solution:
module serial_adder_4b(ser_in,shft_cntrl,clk,rst,s);
input ser_in,shft_cntrl,clk,rst;
output wire s;
wire clk_ff,j,k;
reg [3:0]x,y;
reg c;
assign clk_ff=(clk)&(shft_cntrl);
assign s=(x[0])^(y[0])^c;
always@(posedge(clk) or rst)
begin
if(rst)
x<=4'b0000;
else begin
x[3]<=s;
x[2]<=x[3];
x[1]<=x[2];
x[0]<=x[1];
$display("x=%b",x[0]);
end
end
always@(posedge(clk) or rst)
begin
if(rst)
y<=4'b0000;
else begin
y[3]<=ser_in;
y[2]<=y[3];
y[1]<=y[2];
y[0]<=y[1];
$display("y=%b",y[0]);
end
end
always @(posedge(clk_ff) or rst)
begin
if(rst)begin
c<=1'b0;
end
else
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
begin
case({x[0]&&y[0],(~(x[0]||y[0]))})
2'b00:c<=c;
2'b01:c<=0;
2'b10:c<=1;
2'b11:c<=~c;
default:$display("invalid jk_flipflop input");
endcase
end
end
endmodule
`timescale 1ns/1ps
module serial_adder_4b_tb;
reg ser_in,shft_cntrl,clk,rst;
wire s;
integer i;
serial_adder_4b dut(ser_in,shft_cntrl,clk,rst,s);
always #5 clk=~clk;
initial begin
clk=0;rst=1;shft_cntrl=0;
#15;
shft_cntrl=1;
rst=0;
for(i=0;i<10;i=i+1)
begin
ser_in=$random;
#10;
$display("ser_in=%b,ser_out=%b",ser_in,s);
end
#20 $finish;
end
endmodule
//Result
y=0
x=0
ser_in=0,ser_out=0
x=0
y=0
ser_in=1,ser_out=0
y=0
x=0
ser_in=1,ser_out=0
x=0
y=0
ser_in=1,ser_out=0
y=0
x=0
ser_in=1,ser_out=1
x=0
y=1
ser_in=1,ser_out=1
y=1
x=0
ser_in=1,ser_out=1
x=0
y=1
ser_in=0,ser_out=1
y=1
Solution:
module aFIFO(
input wclk,rclk,reset,
input w_enable,r_enable,
input [7:0]wdata,
output reg [7:0]rdata,
output full_flag,empty_flag);
parameter FIFO_DEPTH=16,POINTER=4;
reg [7:0]fifo[0:FIFO_DEPTH-1];
reg [POINTER:0]wpointer,rpointer,wpointer_sync,rpointer_sync,Qr,Qw;
assign full_flag=(({~wpointer[POINTER],wpointer[POINTER-
1:0]}==rpointer_sync[POINTER:0]))?1:0;
assign empty_flag=(wpointer_sync==rpointer)?1:0;
//write
always@(posedge wclk,posedge reset) begin
if(reset==1)
begin
wpointer<=0;
end
else begin
if(w_enable==1 && full_flag!=1)begin
fifo[wpointer[POINTER-1:0]]<=wdata;
wpointer<=wpointer+1;
end
end
end
//read
always@(posedge rclk,posedge reset) begin
if(reset==1)
begin
rpointer<=0;
end
PRIVATE & CONFIDENTIAL
Entuple Technologies and/or Excel VLSI Technologies
else begin
if(r_enable==1 && empty_flag!=1)begin
rdata<=fifo[rpointer[POINTER-1:0]];
rpointer<=rpointer+1;
end
end
end
//w_ptr sync for read side
always@(posedge rclk,posedge reset)
begin
if(reset==1) begin
Qw<=0;
wpointer_sync<=0;
end
else begin
Qw<=wpointer; wpointer_sync<=Qw;
end
end
//r_ptr sync for write side
always@(posedge wclk,posedge reset)
begin
if(reset==1) begin
Qr<=0;
rpointer_sync<=0;
end
else begin
Qr<=rpointer; rpointer_sync<=Qr;
end
end
endmodule
//testbench
module aFIFO_tb;
parameter FIFO_DEPTH=16,POINTER=4;
// Inputs
reg wclk;
reg rclk;
reg reset;
reg w_enable;
reg r_enable;
reg [7:0] wdata,data_in;
// Outputs
wire [7:0]rdata;
wire full_flag;
wire empty_flag;
integer count;
// Instantiate the Unit Under Test (UUT)
aFIFO #(FIFO_DEPTH,POINTER) uut(
.wclk(wclk),
.rclk(rclk),
.reset(reset),
.w_enable(w_enable),
.r_enable(r_enable),
.wdata(wdata),
.rdata(rdata),
.full_flag(full_flag),
.empty_flag(empty_flag)
);
task write_task;