Bottle
Bottle
5)
endmodule
`timescale 1 ns/10 ps
module reg_file_testbench;
localparam T=10,
D=4*T;
reg clk, en;
reg [1:0] wr_adress, re_adress;
reg [7:0] wr_data;
wire [7:0] re_data;
endmodule
`timescale 1 ns/10 ps
module free_run_shift_reg_testbench;
localparam T=10,
D=4*T;
initial
begin
clock = 1'b1;
res = 1'b1;
#(T/2);
res = 1'b0;
end
always #(T/2)
clock = ~clock;
initial
begin
$dumpfile("free_run_shift_reg_testbench.vcd");
$dumpvars;
test_in = 8'b1;
#D;
test_in = 8'b0;
#D;
$finish;
end
endmodule
Ex:Create a Verilog module for 8-bit universal shift register. Write a testbench
code with the input 00110001. Apply different control signals and observe the
outputs.
Code:
control = 2'b11;
#D; $finish; end
endmodule
Module:
`timescale 1 ns/10 ps module univ_shift_reg
#(parameter N=8)
(
input wire clk, reset, input wire [1:0] ctrl, input wire [N-1:0] d, output
wire [N-1:0] q
) ;
always @(posedge clk, posedge reset) if (reset) r_reg <= 0; else r_reg <= r_next;
always @*
case (ctrl)
2'b00: r_next = r_reg;
2'b01: r_next = {r_reg[N-2:0], d[0]}; 2'b10: r_next = {d[N-1], r_reg[N-1:1]};
default: r_next = d; endcase
assign q = r_reg; endmodule
lab 5:
d flip flop.
TESTBENCH
`timescale 1ns/10 ps module d_ff_testbench; localparam T=10, D=4*T;
reg clk, d; wire q; d_ff uut(clk, d, q); initial begin
clk=1'b1; #(T/2); end always #(T/2) clk=~clk;
initial begin
$dumpfile ("d_ff.vcd"); $dumpvars; d=1'b1;
#D; d=1'b0;
#D; d=1'b1;
#D; d=1'b1;
#D; d=1'b0;
#D; d=1'b1;
#D; d=1'b1;
#D; d=1'b0;
#D; $finish; end endmodule
MODULE:
`timescale 1ns/10 ps module d_ff
(
input wire clk, input wire d, output reg q
);
always @(posedge clk) q<=d;
endmodule
d ff reset: code
`timescale 1ns/10 ps module d_ff_reset_testbench; localparam T=10, D=4*T;
reg clk,reset, d; wire q; d_ff_reset uut(clk,reset, d, q); initial
begin clk=1'b1; reset=1'b1; #(T/2); reset=1'b0;
end always #(T/2) clk=~clk;
initial begin
$dumpfile ("d_ff.vcd"); $dumpvars; d=1'b1;
#D; d=1'b0;
#D; d=1'b1;
#D; d=1'b1;
#D; d=1'b0;
#D; d=1'b1;
#D; d=1'b1; #D; d=1'b0;
#D;
$finish;
end
end module
MODULE:
timescale 1ns/10 ps
module d_ff_reset
( input wire clk,reset,input wire d,output reg q);
always @(posedge clk,posedge reset)
if(reset)
q<=1'b0;
else
q<=d;
endmodule
1 SEGMENT
MODULE
`timescale 1ns/10 ps
module d_ff_en_1seg
(
input wire clk, reset,
input wire en,
input wire d,
output reg q
);
endmodule
`timescale 1ns/10 ps
module d_ff_en_1seg_testbench;
localparam T=10,
D=4*T;
reg clk,reset,en, d;
wire q;
d_ff_en_1seg uut(clk,reset,en, d, q);
initial
begin
clk=1'b1;
reset=1'b1;
#(T/2);
reset=1'b0;
end
always #(T/2) clk=~clk;
initial
begin
$dumpfile ("d_ff.vcd"); $dumpvars;
en=1'b0; d=1'b1;
#D; en=1'b0; d=1'b0;
#D; en=1'b1; d=1'b1;
#D; en=1'b1; d=1'b1;
#D; en=1'b1; d=1'b0;
#D; en=1'b1; d=1'b1;
#D; en=1'b0; d=1'b1;
#D; en=1'b1; d=1'b0;
#D;
$finish;
end
endmodule
2 SEGEMENT
`timescale 1ns/10 ps
module d_ff_en_2seg
(
input wire clk, reset,
input wire en,
input wire d,
output reg q
);
reg r_reg, r_next;
always@
if(en)
r_next=d;
else
r_next=r_reg;
always@
q=r_reg;
endmodule
`timescale 1ns/10 ps
module d_ff_en_2seg_testbench;
localparam T=10,
D=4*T;
reg clk,reset,en, d;
wire r_reg, r_next, q;
d_ff_en_2seg uut(clk,reset,en, d, q);
initial
begin
clk=1'b1;
reset=1'b1;
#(T/2);
reset=1'b0;
end
always #(T/2) clk=~clk;
initial
begin
$dumpfile ("d_ff.vcd"); $dumpvars;
en=1'b0; d=1'b1;
#D; en=1'b0; d=1'b0;
#D; en=1'b1; d=1'b1;
#D; en=1'b1; d=1'b1;
#D; en=1'b1; d=1'b0;
#D; en=1'b1; d=1'b1;
#D; en=1'b0; d=1'b1;
#D; en=1'b1; d=1'b0;
#D;
$finish;
end
endmodule
Exercise:
Amend duty-cycle of the clock (25% or 75%) in the sample testbench code. Run the
listing 4.3 with the amended testbench code and observe the outputs.
LAB 4:
ADDR CARRY HARD
`timescale 1ns/10 ps
module adder_carry_hard_lit
(
input wire [3:0] a,b, output wire [3:0] sum,
output wire cout
);
wire [4:0] sum_ext;
`timescale 1ns/ 10 ps
module adder_carry_hard_lit_testbench; reg [3:0] a, b; wire [3:0] sum; wire
cout;
adder_carry_hard_lit uff(a, b, sum, cout);
initial begin
$dumpfile("adder_carry_hard_lit_testbench.vcd"); $dumpvars;
a = 4'b0001; b =4'b1001;
#20;
$display("the sum of a and b = %b", sum);
$display("the carry = %b", cout);
$finish; end
endmodule
LAB 3 : MUX 2x1 MUX: Two single-bit inputs with one selectivity switch.
`timescale 1ns/ 10ps module m21
(
input wire d0, d1, s, output reg y
);
always @*
if (s==0) y = d0; else if (s==1)
y = d1;
endmodule
m21 uut
( .d0(test_d0), .d1(test_d1), .s(test_s), .y(test_y));
$finish; end
endmodule
2. 4-bit wide 4x1 MUX: Four inputs (4-bits wide each) with two selectivity
switches.
always @*
if (s0==0 && s1==0)
y = d0;
else if (s0==0 && s1==1) y = d1;
else if (s0==1 && s1==0) y = d2;
else if (s0==1 && s1==1) y = d3 ;
endmodule
$finish; end
endmodule
To explain the working of the four shift operators in Verilog HDL (Hardware
Description Language), let's create a simple Verilog design that demonstrates the
behavior of these operators using two bits for shifting. We will use the four shift
operators: left shift (<<), right shift (>>), logical right shift (>>>), and
arithmetic right shift (>>>) to manipulate a 4-bit binary number.
module shift_operations_tb;
$dumpfile("dump.vcd");
$dumpvars;
endmodule
endmodule
module logical_right_shift( input [3:0] data_in, output reg [3:0] result
);
endmodule
. Make a 2-bits max comparator HDL design using �?� operator. You may take three 2-
bit inputs (a, b, c), and a 2-bit output should be a max number of all the three
inputs.
Note: You also need to write a testbench code for the above exercises. Please
provide your explanation with supporting waveforms obtained from your design.
module max_comparator_tb;
// Outputs
wire [1:0] max_result;
$finish;
end
endmodule
module max_comparator(
input [1:0] a, input [1:0] b, input [1:0] c,
output [1:0] max_result
);
endmodule
LAB 1 FULL ADDER:
TEST BENCH CODE:
module tb_full_adder;
full_adder dut (
.a(a),
.b(b),
.c_in(c_in),
.sum(sum),
.c_out(c_out)
);
initial begin
$dumpfile("dump.vcd");
$dumpvars(0, tb_full_adder);
$finish; end
endmodule
MODULE DESIGN CODE:
module full_adder ( input a, input b, input c_in, output sum,
output c_out
);
assign sum = a ^ b ^ c_in; assign c_out = (a & b) | (b & c_in) | (a &
c_in); endmodule