Verilog_Assignment_4
Verilog_Assignment_4
-------------------------------------------------
A. Counters
1. Write a Verilog code for 4-bit down_counter with enable and load signals, on
assertion of load count value must be 3. Write a code for the testbench
and give appropriate inputs to test all possible combinations.
endmodule
testbench:
`include "exp1.v"
module down_counter_tb();
reg enable,clk,reset,load;
wire [3:0]count;
down_counter
dut(.clk(clk),.reset(reset),.enable(enable),.load(load),.count(count));
initial begin
forever #5 clk=~clk;
end
initial begin
reset =1;
clk=0;
load=0;
enable =0;
end
initial begin
enable =1;
#20;
enable =0;
enable =1;
#60;
enable=0;
#10;
$finish;
end
initial begin
$monitor("time: %0t | reset = %b | enable = %b | load = %b | count = %b",
$time,reset,enable,load,count);
end
endmodule
-----------------------------------------------------------------------------------
-------------------------------------
2. Write a Verilog code for 4-bit up_counter with enable and load signals, on
assertion of load count value must be 9. Write a code for the testbench and
give appropriate inputs to test all possible combinations.
endmodule
testbench:
`timescale 1ns/1ps
`include "exp2.v"
module up_counter_tb();
reg enable,clk,reset,load;
wire [3:0]count;
up_counter
dut(.clk(clk),.reset(reset),.enable(enable),.load(load),.count(count));
initial begin
forever #5 clk=~clk;
end
initial begin
reset =1;
clk=0;
load=0;
enable =0;
end
initial begin
enable =1;
#20;
enable =0;
enable =1;
#60;
enable=0;
#10;
$finish;
end
initial begin
$monitor("time: %0t | reset = %b | enable = %b | load = %b | count = %b",
$time,reset,enable,load,count);
end
endmodule
-----------------------------------------------------------------------------------
-------------------------------------
3.Write a Verilog code for 4 bit up-down counter with enable signal and load
signal, when the enable signal is asserted it counts from 0 to 15 in up
counting mode and counts from 15 to 0 in down counting mode, and upon
assertion of reset in up mode the count must go to 0 and in down mode the
count must go to 15. On assertion of load in up counting mode the count
must go to 9 and in down counting mode the count must go to 3. Write
associated testbench and give appropriate inputs to test different
combinations.
module up_down_counter (
input clk,
input reset,
input up_down,load,enable,
output reg [3:0] count
);
else if (enable)begin
if (up_down==1)
testbench:
module up_down_counter_tb;
up_down_counter uut(
.clk(clk),
.reset(reset),.enable(enable),
.up_down(up_down),
.count(count),.load(load));
initial begin
clk = 0;
forever #5 clk = ~clk;
end
initial
load =0;
initial begin
$finish;
end
initial begin
$monitor("Time = %t | reset = %b ,enable = %b | up_down = %b |, load = %b |
count = %b", $time, reset,enable, up_down,load, count);
end
endmodule
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------
4.Write a single Verilog code to implement 8bit, 16bit and 32 bit synchronous
up_down counter. With active low reset, clk and load as inputs. Whenever
the load is asserted, the counter should load a user defined value. Write a
testbench to verify the same.
else if (enable)begin
if (up_down==1)
testbench:
module up_down_counter_tb;
up_down_counter #(.LOAD_VALUE(4'b0110),.size(8))uut(
.clk(clk),
.reset(reset),.enable(enable),
.up_down(up_down),
.count(count),.load(load));
initial begin
clk = 0;
forever #5 clk = ~clk;
end
initial
load =0;
initial begin
$finish;
end
initial begin
$monitor("Time = %t | reset = %b ,enable = %b | up_down = %b |, load = %b |
count = %d", $time, reset,enable, up_down,load, count);
end
endmodule
-----------------------------------------------------------------------------------
-------------------------------------------------
B. Registers
1. Write a Verilog code for a Serial in serial out shift register with
asynchronous active low reset and write the testbench to verify the same.
testbench:
`include "exp5.v"
module siso_tb();
reg clk;
reg reset;
reg din;
wire dout;
initial begin
clk=0;
forever #5 clk=~clk;
end
initial begin
reset = 0;
din=0;
#10 reset=1;
din=1;#10;
din=0;#10;
din=0;#10;
din=1;#50;
reset=0;
$finish;
end
initial
$monitor("time = %t |, din = %b |, clk = %b | reset = %b | dout = %b",
$time,din,clk,reset,dout);
endmodule
-----------------------------------------------------------------------------------
-------------------------------------------------
2. Write a Verilog code for a Serial in parallel out shift register with
synchronous active low reset and write the testbench to verify the same
end
endmodule
testbench:
`include "exp6.v"
module sipo_tb();
reg clk;
reg reset;
reg din;
wire dout;
Sipo dut(clk,reset,din,dout);
initial begin
clk=0;
forever #5 clk=~clk;
end
initial begin
reset = 0;
din=0;
#10 reset=1;
din=1;#10;
din=0;#10;
din=0;#10;
din=1;#50;
reset=0;
$finish;
end
initial
$monitor("time = %t |, din = %b |, clk = %b | reset = %b | dout = %b",
$time,din,clk,reset,dout);
endmodule
-----------------------------------------------------------------------------------
------------------------------------------------
3. Write a Verilog code for a Parallel in parallel out shift register with
synchronous active high reset and write the testbench to verify the same.
testbench:
`include "exp7.v"
module pipo_tb();
reg clk;
reg reset;
reg din;
wire dout;
pipo dut(clk,reset,din,dout);
initial begin
clk=0;
forever #5 clk=~clk;
end
initial begin
reset = 0;
din=0;
#10 reset=1;
din=1;#10;
din=0;#10;
din=0;#10;
din=1;#50;
reset=0;
$finish;
end
initial
$monitor("time = %t |, din = %b |, clk = %b | reset = %b | dout = %b",
$time,din,clk,reset,dout);
endmodule
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------
4.Write a Verilog code for a Shift register with asynchronous active low reset,
with clock, reset, shift, [7:0] data_in as inputs and [7:0] data_out as output.
Whenever the “shift” is low, it should perform the left shift operation and
whenever the “shift” is high, it should perform the right shift operation. Write
the testbench to verify the same.
testbench:
`include "exp8.v"
module tb_shift_register;
reg clk;
reg reset_n;
reg shift;
reg [7:0] data_in;
wire [7:0] data_out;
shift_register
uut(.clk(clk),.reset_n(reset_n),.shift(shift),.data_in(data_in),.data_out(data_out)
);
initial begin
clk = 0;
forever #5 clk = ~clk;
end
initial begin
reset_n = 1;
shift = 0;
data_in = 8'b00000000;
#10 reset_n = 0;
#10 reset_n = 1;
shift = 0;
data_in = 8'b00000001;
#10
#10
#10
#10
shift = 1;
data_in = 8'b10000000;
#10
#10
#10
#10
#10 $finish;
end
initial
$monitor(" time = %0t | reset = %b | clk = %b | data_in = %b | data_out = %b",
$time,reset_n,clk,data_in,data_out);
endmodule
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------
C. SERIES GENERATOR
testbench:
`include "fibonacci.v"
module tb_fibonacci_generator;
reg clk;
reg reset_n;
wire [7:0] fib_out;
fibonacci_generator uut(.clk(clk),.reset_n(reset_n),.fib_out(fib_out));
initial begin
clk = 0;
forever #5 clk = ~clk;
end
initial begin
reset_n = 0;
#100 reset_n = 1;
#80 $finish;
end
initial begin
$monitor("Time: %0t | Reset_n: %b | Fibonacci Out: %d", $time, reset_n, fib_out);
end
endmodule
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
2.
assign C =~(A|B);
assign D= ~B;
assign Q = ~(C&D);
endmodule
testbench:
`include "exp10.v"
module bool_tb();
reg A,B,C,D;
wire Q;
boolean dut(Q,A,B);
initial begin
A=0;B=0;
#10;
A=0;B=1;
#10;
A=1;B=0;
#10;
A=1;B=1;
#20;$finish;
end
initial
$monitor(" A = %b | B = %b | OUT = %b",A,B,Q);
endmodule
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------