Verilog Examples
Verilog Examples
Verilog Examples
HDL
EXAMPLES
1. 2-DFF Synchronizer.
module dff_sync2(
input clk,
input rst,
input d,
output q_synced
);
reg sync_flop1;
reg sync_flop2;
// Testbench
module dff_sync2_test;
reg clk;
reg rst;
reg d;
wire q_synced;
//wire q;
//wire qb;
initial begin
clk = 0;
rst = 0;
end
always begin
#5 clk = ~clk;
end
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
// $display("Reset flop.");
//clk = 0;
//rst = 1;
// d = 1'bx;
// display;
$display("Release reset.");
#5 rst = 1;
display;
#10 d = 1;
//$display("Toggle clk.");
//clk = 1;
//display;
end
task display;
#1 $display("d:%0h, q_synced:%0h",
d, q_synced);
endtask
endmodule
In this approach, a single process is used to code the Present State, Next State, and Output Logic.
module FSM_Type_1_SINGLE_PROCESS (
input clk,
input reset_n,
input i_X1,
output reg o_Out
);
localparam S1 = 2'b00,
S2 = 2'b01,
S3 = 2'b10,
S4 = 2'b11;
//TESTBENCH
module FSM_Type_1_TB;
reg clk;
reg reset_n;
reg i_X1;
wire o_Out;
.clk (clk),
.reset_n (reset_n),
.i_X1 (i_X1),
.o_Out (o_Out)
);
#5 reset_n = 1;
#100 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars();
end
endmodule
2. Two-Process Design
This technique involves using two separate processes: one to code Present State and Next State
logic, and another to code Output Logic.
module FSM_Type_2_TWO_PROCESS (
input clk,
input reset_n,
input i_X1,
output reg o_Out
);
localparam S1 = 2'b00,
S2 = 2'b01,
S3 = 2'b10,
S4 = 2'b11;
//TESTBENCH
module FSM_Type_2_TB;
reg clk;
reg reset_n;
reg i_X1;
wire o_Out;
.clk (clk),
.reset_n (reset_n),
.i_X1 (i_x1),
.o_Out (o_Out)
);
#5 reset_n = 1;
#100 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars();
end
endmodule
3. Three-Process Design
In this approach, three separate processes are used: one each for Present State, Next State, and
Output Logic.
module FSM_Type_3_THREE_PROCESS (
input clk,
input reset_n,
input i_X1,
output reg o_Out
);
localparam S1 = 2'b00,
S2 = 2'b01,
S3 = 2'b10,
S4 = 2'b11;
//TESTBENCH
module FSM_Type3_TB;
reg clk;
reg reset_n;
reg i_X1;
wire o_Out;
.clk (clk),
.reset_n (reset_n),
.i_X1 (i_X1),
.o_Out (o_Out)
);
#5 reset_n = 1;
#100 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars();
end
endmodule
3. Generic Pipeline/Repeater Hardware Block.
module generic_pipeline #(
parameter WIDTH = 1,
parameter PIPELINE_STAGE = 1
)(
input i_clk,
input i_rst_n,
input i_en,
input [WIDTH-1:0] i_din,
output [WIDTH-1:0] o_dout
);
// Declarations
reg [(PIPELINE_STAGE+1)*WIDTH-1:0] pipeline;
endmodule // generic_pipeline
module tb_generic_pipeline;
// Parameters
parameter WIDTH = 4;
parameter PIPELINE_STAGE = 2;
parameter CLK_PERIOD = 10;
// Inputs
reg tb_clk;
reg tb_rst_n;
reg tb_en;
reg [WIDTH-1:0] tb_din;
// Outputs
wire [WIDTH-1:0] tb_dout;
// Clock generation
always begin
#((CLK_PERIOD) / 2) tb_clk = ~tb_clk; // Toggle every half period
end
// Apply reset
#10 tb_rst_n = 0;
// End simulation
#50 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars();
end
endmodule
4. Simple DFF Synchronous and Asynchronous Design.
module dff (
input clk,
input rst,
input d,
output reg q,
output qb);
assign qb = ~q;
module tb_dff_async_reset;
// Inputs
reg tb_clk;
reg tb_rst;
reg tb_d;
// Outputs
wire tb_q;
wire tb_qb;
// Clock generation
always begin
#5 tb_clk = ~tb_clk; // Toggle every 5 time units
end
// Initial block for stimulus
initial begin
// Initialize inputs
tb_clk = 0;
tb_rst = 0;
tb_d = 0;
// End simulation
#40 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars();
end
endmodule
module dff (
input clk,
input rst,
input d,
output reg q,
output qb);
assign qb = ~q;
module tb_dff_sync_reset;
// Inputs
reg tb_clk;
reg tb_rst;
reg tb_d;
// Outputs
wire tb_q;
wire tb_qb;
// Clock generation
always begin
#5 tb_clk = ~tb_clk; // Toggle every 5 time units
end
// End simulation
#40 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars();
end
endmodule
5. Mod-M Counter Design with a Tick Generated.
// Design of a Mod-M counter which generates a 1 clock cycyle pulse as well
when count reaches to M
)(
input clk,
input rst,
output complete_tick,
output [N-1:0] count
);
endmodule
module mod_M_Counter;
reg clk;
reg rst;
wire complete_tick;
wire [2:0]count;
initial begin
clk = 0;
rst = 0;
end
always begin
#5 clk = ~clk;
end
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
$display("Reset flop.");
rst = 1;
$display("Release reset.");
#5 rst = 0;
display;
end
task display;
endmodule
endmodule
endmodule
//Testbench
module clockPulsetest;
reg clk;
reg rst;
wire clkPulse;
initial begin
clk = 0;
rst = 1;
end
always begin
#5 clk = ~clk;
end
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
$display("Reset flop.");
display;
rst = 0;
end
task display;
#1 $display("clkPulse:%0h", clkPulse);
#100 $finish;
endtask
endmodule
7. Design of Fixed Priority Arbiter.
module fixed_priority_arbiter(
input clk,
input rst,
input [3:0] REQ,
output reg [3:0] GNT
);
else if(REQ[3])
GNT <= 4'b1000;
else if(REQ[2])
GNT <= 4'b0100;
else if (REQ[1])
GNT <= 4'b0010;
else if(REQ[0])
GNT <= 4'b0001;
else
GNT <= 4'b0000;
end
endmodule
// TESTBENCH
module fixed_priority_Arbiter_test;
reg clk;
reg rst;
reg [3:0] REQ;
wire [3:0] GNT;
initial begin
clk = 0;
rst = 1;
REQ = 4'b0;
// Assert the Asynchronous Reset after 1 clock period
#10 rst = 0;
//Deassert the Reset
#5 rst = 1;
#5 rst = 0;
#100 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
module round_robin_arbiter_fixed_time_slices(
input clk,
input rst,
input [3:0] REQ,
output reg [3:0] GNT
);
end
if(REQ[0])
begin
next_state = S_0;
end
else if(REQ[1])
begin
next_state = S_1;
end
else if(REQ[2])
begin
next_state = S_2;
end
else if(REQ[3])
begin
next_state = S_3;
end
else
begin
next_state = S_ideal;
end
end // S_ideal
S_0 : begin
if(REQ[1])
begin
next_state = S_1;
end
else if(REQ[2])
begin
next_state = S_2;
end
else if(REQ[3])
begin
next_state = S_3;
end
else if(REQ[0])
begin
next_state = S_0;
end
else
begin
next_state = S_ideal;
end
end // S_0
S_1 : begin
if(REQ[2])
begin
next_state = S_2;
end
else if(REQ[3])
begin
next_state = S_3;
end
else if(REQ[0])
begin
next_state = S_0;
end
else if(REQ[1])
begin
next_state = S_1;
end
else
begin
next_state = S_ideal;
end
end //S_1
S_2 : begin
if(REQ[3])
begin
next_state = S_3;
end
else if(REQ[0])
begin
next_state = S_0;
end
else if(REQ[1])
begin
next_state = S_1;
end
else if(REQ[2])
begin
next_state = S_2;
end
else
begin
next_state = S_ideal;
end
end // S_2
S_3 : begin
if(REQ[0])
begin
next_state = S_0;
end
else if(REQ[1])
begin
next_state = S_1;
end
else if(REQ[2])
begin
next_state = S_2;
end
else if(REQ[3])
begin
next_state = S_3;
end
else
begin
next_state = S_ideal;
end
end // S_3
default : begin
if(REQ[0])
begin
next_state = S_0;
end
else if(REQ[1])
begin
next_state = S_1;
end
else if(REQ[2])
begin
next_state = S_2;
end
else if(REQ[3])
begin
next_state = S_3;
end
else
begin
next_state = S_ideal;
end
end // default
endcase // case(state)
end
always @(present_state or next_state) // Output , Combinational always block
begin
case(present_state)
S_0 : begin GNT = 4'b0001; end
endcase
end
endmodule // Round Robin Arbiter with Fixed Time Slices
begin
if(!rst)
else
case(present_state)
endcase
end
//3) Output logic can be coded along with the next state , combinational
logic as well
*/
//Test Bench:
module fixed_priority_Arbiter_fixed_time_slices_test;
reg clk;
reg rst;
reg [3:0] REQ;
wire [3:0] GNT;
#5 rst = 0;
#100 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
9. Design of Round Robin Arbiter (with Variable Time Slices).
module round_robin_arbiter_variable_time_slices(
input clk,
input rst,
input [3:0] REQ,
output reg [3:0] GNT
);
S_0 : begin
if (REQ[1])
next_state = S_1;
else if (REQ[2])
next_state = S_2;
else if (REQ[3])
next_state = S_3;
else if (REQ[0])
next_state = S_0;
else
next_state = S_ideal;
end // S_0
S_1 : begin
if (REQ[2])
next_state = S_2;
else if (REQ[3])
next_state = S_3;
else if (REQ[0])
next_state = S_0;
else if (REQ[1])
next_state = S_1;
else
next_state = S_ideal;
end // S_1
S_2 : begin
if (REQ[3])
next_state = S_3;
else if (REQ[0])
next_state = S_0;
else if (REQ[1])
next_state = S_1;
else if (REQ[2])
next_state = S_2;
else
next_state = S_ideal;
end // S_2
S_3 : begin
if (REQ[0])
next_state = S_0;
else if (REQ[1])
next_state = S_1;
else if (REQ[2])
next_state = S_2;
else if (REQ[3])
next_state = S_3;
else
next_state = S_ideal;
end // S_3
default : begin
if (REQ[0])
next_state = S_0;
else if (REQ[1])
next_state = S_1;
else if (REQ[2])
next_state = S_2;
else if (REQ[3])
next_state = S_3;
else
next_state = S_ideal;
end // default
endcase // case(state)
end
`timescale 1ns/1ps
module tb_round_robin_arbiter_variable_time_slices;
reg clk;
reg rst;
reg [3:0] REQ;
wire [3:0] GNT;
initial begin
// Initialize inputs
clk = 0;
rst = 1;
REQ = 4'b0;
// End simulation
#5 $finish;
end
// Clock generation
always #5 clk = ~clk;
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
endmodule
//TestBench
module priority_encoder_test;
reg [7:0] IN;
wire [2:0] OUT;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
IN = 8'h0;
#10 IN = 8'b0001_0001;
#10 IN = 8'b1001_0001;
#10 IN = 8'b0000_1001;
#10 IN = 8'b0101_0000;
#10 IN = 8'b0000_1010;
#10 IN = 8'b0101_0101;
#10 IN = 8'b0001_0001;
#10 IN = 8'b0000_1001;
#10 IN = 8'b0001_0101;
#10 IN = 8'b0001_0011;
end
endmodule
//Priority Encoder Using case Statement
module priority_encoder(
input [7:0] IN,
output reg [2:0] OUT
);
always @*
begin
casex(IN)
8'b1xxxxxxx : OUT = 3'b111;
8'b01xxxxxx : OUT = 3'b110;
8'b001xxxxx : OUT = 3'b101;
8'b0001xxxx : OUT = 3'b100;
8'b00001xxx : OUT = 3'b011;
8'b000001xx : OUT = 3'b010;
8'b0000001x : OUT = 3'b001;
8'b00000001 :OUT = 3'b000;
//TestBench
module priority_encoder_test;
reg [7:0] IN;
wire [2:0] OUT;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
IN = 8'h0;
#10 IN = 8'b0001_0001;
#10 IN = 8'b1001_0001;
#10 IN = 8'b0000_1001;
#10 IN = 8'b0101_0000;
#10 IN = 8'b0000_1010;
#10 IN = 8'b0101_0101;
#10 IN = 8'b0001_0001;
#10 IN = 8'b0000_1001;
#10 IN = 8'b0001_0101;
#10 IN = 8'b0001_0011;
end
endmodule
11. Design of Ring Counter.
module straight_ring_counter #(parameter WIDTH = 4)
(
input clk,
input rst,
output reg [WIDTH-1 : 0] out
);
integer i;
always @(posedge clk or negedge rst)
begin
if(!rst)
out <= 4'h1;
else
begin
out[WIDTH-1] <= out[0];
//TestBench
module ring_counter_test;
reg clk;
reg rst;
wire [3:0] out;
#100 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
12. Design of Johnson Ring Counter.
module johnson_ring_counter #(parameter WIDTH = 4)
(
input clk,
input rst,
output reg [WIDTH-1 : 0] out
);
integer i;
begin
out[WIDTH-1] <= ~ out[0];
for (i =0; i < WIDTH-1; i = i+1)
begin
out[i] <= out[i+1];
end
end
end
endmodule
//Test Bench:
module johnson_counter_test;
reg clk;
reg rst;
wire [3:0] out;
#100 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
13. Design of an Event Detector.
module pos_edge_det (
input sig, // Input signal for which a positive edge has to be detected
input clk, // Input clock signal
output reg pe // Output signal that gives a pulse when a positive edge
occurs
);
reg sig_dly; // Internal signal to store the delayed version of the signal
// This always block ensures that sig_dly is exactly 1 clock behind sig
always @(posedge clk) begin
sig_dly <= sig;
end
// Combinational logic where sig is AND with delayed, inverted version of sig
// Assign statement assigns the evaluated expression in the RHS to the
internal net pe
assign pe = sig & ~sig_dly;
endmodule
//TestBench
module tb;
reg sig; // Declare internal TB signal called sig to drive the sig pin of
the design
reg clk; // Declare internal TB signal called clk to drive the clock to
the design
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
14. Design of an Event Detector.
// Module Event Detector (without input data sync in)
module event_detector_async(
input clk,
input rst,
input data_in,
output event_out
);
reg q_ff;
//TestBench
module async_event_detectotr_test;
reg clk;
reg rst;
reg data_in;
wire event_out;
initial begin
clk = 0;
rst = 1;
data_in = 1'b0;
// Assert the Asynchronous Reset after 1 clock period
#1 rst = 0;
//Deassert the Reset
#5 rst = 1;
#100 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
( input clk,
input rstn,
output reg [N-1:0] out);
reg [N-1:0] q;
//Testbench
module tb;
parameter N = 4;
reg clk;
reg rstn;
wire [N-1:0] out;
gray_ctr u0 ( .clk(clk),
.rstn(rstn),
.out(out));
initial begin
{clk, rstn} <= 0;
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
// Pointer Generation
always @(posedge clk) begin : POINTER
if (rst) begin
wr_ptr <= 0;
rd_ptr <= 0;
end
end
endmodule
//Testbench
`timescale 1ns/1ps
module fifo_tb;
// Reset generation
initial begin
rst = 1;
#10 rst = 0;
end
// Stimulus generation
initial begin
// Test Case 1: Write data to the FIFO
data_in = 8'hAA;
wr = 1;
#20;
wr = 0;
// Finish simulation
#50 $finish;
end
initial begin
// below two lines are used to show waveform
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
// Reset Sigal
input rst,
//TestBench:
module simple_ram_test;
reg rst;
reg wr_clk;
reg [4:0] wr_address;
reg [7:0] wr_data;
reg wr_enable;
reg rd_clk;
reg [4:0] rd_address;
wire [7:0] rd_data;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
rst = 0;
#5 rst = 1;
#5 rst = 0;
wr_clk = 0;
rd_clk = 0;
wr_enable = 0;
rd_address = 5'hAA;
wr_address = rd_address;
$display("Read initial data.");
toggle_rd_clk;
$display("data[%0h]: %0h",
rd_address, rd_data);
$display("Write new data.");
wr_enable = 1;
wr_data = 8'hBB;
toggle_wr_clk;
wr_enable = 0;
$display("Read new data.");
toggle_rd_clk;
$display("data[%0h]: %0h",
rd_address, rd_data);
rst = 1;
#5 rst = 0;
end
task toggle_wr_clk;
begin
#10 wr_clk = ~wr_clk;
#10 wr_clk = ~wr_clk;
end
endtask
task toggle_rd_clk;
begin
#10 rd_clk = ~rd_clk;
#10 rd_clk = ~rd_clk;
end
endtask
endmodule
// Port Declaration
input oe;
input clk;
input [7:0] inp;
output [7:0] outp;
inout [7:0] bidir;
reg [7:0] a;
reg [7:0] b;
// Always Construct
always @ (posedge clk)
begin
b <= bidir;
a <= inp;
end
endmodule
//Testbench
module tb_bidirec;
reg oe, clk;
reg [7:0] inp;
wire [7:0] outp, bidir;
// Clock generation
initial begin
clk = 0;
forever #5 clk = ~clk;
end
// Stimulus generation
initial begin
// Enable bidirectional
oe = 1;
// Test Case 2: Write different data to bidirectional and read from outp
inp = 8'h55;
#10;
// Display results
$display("Test Case 2: outp = %h, bidir = %h", outp, bidir);
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
19. Digital Design of Half Adder (Verilog HDL).
module half_adder(a_in, b_in, sum_o, carry_o);
input a_in;
input b_in;
output sum_o;
output carry_o;
assign sum_o = a_in ^ b_in;
assign carry_o = a_in & b_in;
endmodule
//TestBench
module half_adder_test;
reg a_in;
reg b_in;
wire sum_o;
wire carry_o;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
a_in = 1'b0;
b_in = 1'b0;
end
endmodule
20. Digital Design of Full Adder (Verilog HDL).
module full_adder (a_in, b_in, carry_in, sum_o, carry_o);
input a_in;
input b_in;
input carry_in;
output sum_o;
output carry_o;
assign sum_o = (a_in ^ b_in ^ carry_in);
assign carry_o = (((a_in ^ b_in)& carry_in) | (a_in & b_in));
//assign carry_o = ((a_in & b_in) | (b_in & carry_in) | (a_in & carry_in));
endmodule
//TestBench:
module full_adder_test;
reg a_in;
reg b_in;
reg carry_in;
wire sum_o;
wire carry_o;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
a_in = 1'b0;
b_in = 1'b0;
carry_in = 1'b0;
end
endmodule
21. Digital Design of Half Subtractor (Circuit + Verilog HDL).
module half_subtractor(a_in , b_in , diff_o, borrow_o);
input a_in;
input b_in;
output diff_o;
output borrow_o;
endmodule
//TestBench
module half_subtractor_test;
reg a_in;
reg b_in;
wire diff_o;
wire borrow_o;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
a_in = 1'b0;
b_in = 1'b0;
end
endmodule
22. Digital Design of Full Subtractor (Verilog HDL).
module full_subtractor (a_in, b_in, borrow_in, diff_o, borrow_o);
input a_in;
input b_in;
input borrow_in;
output diff_o;
output borrow_o;
endmodule
//TestBench
module full_subtractor_test;
reg a_in;
reg b_in;
reg borrow_in;
wire diff_o;
wire borrow_o;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
a_in = 1'b0;
b_in = 1'b0;
borrow_in = 1'b0;
end
endmodule
23. Digital Design of Ripple Carry Adder (Verilog HDL).
`include "full_adder.sv"
endmodule
//full_adder.sv
module full_adder (a_in, b_in, carry_in, sum_o, carry_o);
input a_in;
input b_in;
input carry_in;
output sum_o;
output carry_o;
endmodule
//TestBench
module ripple_carry_adder_test;
reg [3:0]a_in;
reg [3:0]b_in;
reg carry_in;
wire [3:0]sum_o;
wire carry_o;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
a_in = 4'b0000;
b_in = 4'b0000;
carry_in = 1'b0;
end
endmodule
assign c0 = carry_in;
assign c1 = (g0 | (p0 & c0));
assign c2 = (g1 | (p1 & g0) | (p1 & p0 & c0));
assign c3 = (g2 | (p2 & g1) | (p2 & p1 & g0) | (p2 & p1 & p0 & c0));
assign c4 = (g3 | (p3 & g2) | (p3 & p2 & g1) | (p3 & p2 & p1 & g0) | (p3 &
p2 & p1 & p0 & c0));
endmodule
//full_adder.sv
module full_adder (a_in, b_in, carry_in, sum_o, carry_o);
input a_in;
input b_in;
input carry_in;
output sum_o;
output carry_o;
endmodule
//TestBench
module carry_look_ahead_adder_4_bit_test;
reg [3:0]a_in;
reg [3:0]b_in;
reg carry_in;
wire [3:0]sum_o;
wire carry_o;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
a_in = 4'b0000;
b_in = 4'b0000;
carry_in = 1'b0;
end
endmodule
25. Digital Design of 16 Bit Carry Lookahead Adder (Circuit + Verilog HDL)
`timescale 1ns / 1ps
module carry_look_ahead_16bit(a,b, cin, sum,cout);
input [15:0] a,b;
input cin;
output [15:0] sum;
output cout;
wire c1,c2,c3;
endmodule
assign p=a^b;//propagate
assign g=a&b; //generate
//carry=gi + Pi.ci
assign c[0]=cin;
assign c[1]= g[0]|(p[0]&c[0]);
assign c[2]= g[1] | (p[1]&g[0]) | p[1]&p[0]&c[0];
assign c[3]= g[2] | (p[2]&g[1]) | p[2]&p[1]&g[0] | p[2]&p[1]&p[0]&c[0];
assign cout= g[3] | (p[3]&g[2]) | p[3]&p[2]&g[1] | p[3]&p[2]&p[1]&g[0] |
p[3]&p[2]&p[1]&p[0]&c[0];
assign sum=p^c;
endmodule
////Testbench
module carry_look_ahead_16bit_tb;
reg [15:0] a,b;
reg cin;
wire [15:0] sum;
wire cout;
initial begin
a=0; b=0; cin=0;
#10 a=16'd0; b=16'd0; cin=1'd1;
#10 a=16'd14; b=16'd1; cin=1'd1;
#10 a=16'd5; b=16'd0; cin=1'd0;
#10 a=16'd999; b=16'd0; cin=1'd1;
end
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
end
initial
$monitor( "A=%d, B=%d, Cin= %d, Sum=%d, Cout=%d", a,b,cin,sum,cout);
endmodule
endmodule
//full_adder.sv
module full_adder (a_in, b_in, carry_in, sum_o, carry_o);
input a_in;
input b_in;
input carry_in;
output sum_o;
output carry_o;
endmodule
//TestBench
module parallel_adder_subtractor_test;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
// Monitor signals
$monitor("Time=%0t a_in=%b b_in=%b control_in=%b sum_diff_o=%b
carry_borrow_o=%b",$time, a_in, b_in, control_in, sum_diff_o,
carry_borrow_o);
// Test cases
#10 a_in = 4'b0000; b_in = 4'b0001; control_in = 1'b1;
#10 a_in = 4'b0001; b_in = 4'b0001; control_in = 1'b0;
#10 a_in = 4'b0010; b_in = 4'b0011; control_in = 1'b1;
#10 a_in = 4'b1111; b_in = 4'b1010; control_in = 1'b0;
#10 a_in = 4'b1001; b_in = 1'b1000; control_in = 1'b1;
#10 a_in = 4'b1101; b_in = 4'b1001; control_in = 1'b0;
#10 a_in = 4'b0001; b_in = 4'b0011; control_in = 1'b1;
#10 a_in = 4'b0001; b_in = 4'b0000; control_in = 1'b0;
#10 $finish;
end
endmodule
input a_in;
input b_in;
output aeqb_o;
output agb_o;
output alb_o;
endmodule
//TestBench
module magnitude_comparator_1_bit_test;
reg a_in;
reg b_in;
wire aeqb_o;
wire agb_o;
wire alb_o;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
// Monitor signals
$monitor("Time=%0t a_in=%b b_in=%b a==b_o=%b a>b_o=%b a<b_o=%b",
$time, a_in, b_in, aeqb_o, agb_o, alb_o);
// Test cases
a_in = 1'b0; b_in = 1'b0;
#10 a_in = 1'b1; b_in = 1'b0;
#10 a_in = 1'b0; b_in = 1'b1;
#10 a_in = 1'b1; b_in = 1'b1;
#10 a_in = 1'b0; b_in = 1'b0;
#10 $finish;
end
endmodule
input a_in;
input b_in;
output aeqb_o;
output agb_o;
output alb_o;
input aeqb_in;
input agb_in;
input alb_in;
always @(*)
begin
if(aeqb_in)
begin
aeqb_w = ((~a_in & ~b_in) | (a_in & b_in));
agb_w = (a_in & ~b_in);
alb_w = (~a_in & b_in);
end
else
begin
aeqb_w = aeqb_in;
agb_w = agb_in;
alb_w = alb_in;
end
end
endmodule
//TestBench
module magnitude_comparator_1_bit_type2_test;
reg a_in;
reg b_in;
reg aeqb_in;
reg agb_in;
reg alb_in;
wire aeqb_o;
wire agb_o;
wire alb_o;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
// Monitor signals
$monitor("Time=%0t a_in=%b b_in=%b a==b_in=%b a>b_in=%b a<b_in=%b
a==b_o=%b a>b_o=%b a<b_o=%b",
$time, a_in, b_in, aeqb_in, agb_in, alb_in, aeqb_o, agb_o,
alb_o);
// Test cases
a_in = 1'b0; b_in = 1'b0; aeqb_in = 1'b0; agb_in = 1'b0; alb_in = 1'b0;
#10 a_in = 1'b1; b_in = 1'b0;
#10 a_in = 1'b0; b_in = 1'b1;
#10 a_in = 1'b1; b_in = 1'b1;
#10 a_in = 1'b0; b_in = 1'b0;
#5 aeqb_in = 1'b1;
#10 a_in = 1'b1; b_in = 1'b0;
#10 a_in = 1'b0; b_in = 1'b1;
#10 a_in = 1'b1; b_in = 1'b1;
#10 a_in = 1'b0; b_in = 1'b0;
#10 $finish;
end
endmodule
29. Digital Design of N Bit Magnitude Comparator (Verilog HDL).
`include "magnitude_comparator_1_bit_type2.sv"
generate
for (gen = 0; gen < N; gen++)
begin
magnitude_comparator_1_bit_type2 CMP(.a_in(a_in[gen]),
.b_in(b_in[gen]), .aeqb_in(aeqb_w[gen]), .agb_in(agb_w[gen]),
.alb_in(alb_w[gen]), .aeqb_o(aeqb_w[gen + 1]), .agb_o(agb_w[gen + 1]),
.alb_o(alb_w[gen + 1]));
end
endgenerate
endmodule
//magnitude_comparator_1_bit_type2
module magnitude_comparator_1_bit_type2(a_in, b_in, aeqb_o, agb_o, alb_o,
aeqb_in, agb_in, alb_in);
input a_in;
input b_in;
output aeqb_o;
output agb_o;
output alb_o;
input aeqb_in;
input agb_in;
input alb_in;
always @(*)
begin
if(aeqb_in)
begin
aeqb_w = ((~a_in & ~b_in) | (a_in & b_in));
agb_w = (a_in & ~b_in);
alb_w = (~a_in & b_in);
end
else
begin
aeqb_w = aeqb_in;
agb_w = agb_in;
alb_w = alb_in;
end
end
endmodule
//TestBench
module magnitude_comparator_n_bit_type2_test;
parameter N = 4;
initial begin
// Dump waves
$dumpfile("dump.vcd");
$dumpvars(1);
// Monitor signals
$monitor("Time=%0t a_in=%b b_in=%b aeqb_in=%b agb_in=%b alb_in=%b
aeqb_o=%b agb_o=%b alb_o=%b",
$time, a_in, b_in, aeqb_in, agb_in, alb_in, aeqb_o, agb_o,
alb_o);
// Test cases
a_in = 4'b0; b_in = 4'b0; aeqb_in = 1'b0; agb_in = 1'b0; alb_in = 1'b0;
#10 a_in = 4'b0001; b_in = 4'b0000;
#10 a_in = 4'b0100; b_in = 4'b1001;
#10 a_in = 4'b1010; b_in = 4'b1000;
#10 a_in = 4'b1110; b_in = 4'b0010;
#5 aeqb_in = 1'b1;
#10 a_in = 4'b0001; b_in = 4'b0000;
#10 a_in = 4'b0100; b_in = 4'b1001;
#10 a_in = 4'b1010; b_in = 4'b1000;
#10 a_in = 4'b1110; b_in = 4'b0010;
#10 $finish;
end
endmodule