Verilog Code
Verilog Code
Verilog module
aruNaCHala
College oF
eNgiNeeriNg For
WomeN
VaisHNaVi s m
eCe
1
Verilog allows circuit design
Verilog using three different modeling
Verilog is a Hardware styles:
Description Language (HDL) Structural Modeling (Gate-Level)
used to design and model digital
circuits. Unlike regular Dataflow Modeling (Equation-
programming languages, Verilog Based)
describes how hardware (like Behavioral Modeling (High-
logic gates, multiplexers, Level Code)
registers, and processors) should
Structural Modeling = Like
function. It allows engineers to: physically wiring gates in
hardware.
Dataflow Modeling = Like
Design digital circuits (e.g.,
writing equations for circuits.
adders, multiplexers, processors) Behavioral Modeling = Like
Simulate circuits before coding circuit behavior with if-
manufacturing them else and loops.
Implement hardware on FPGAs
or ASICs
2
aNd gate struCtural
module and_gate_structural
BeHaVior (input A, input B, output Y);
module or_gate (
input A, B, and (Y, A, B);
output reg Y
); endmodule
always @(A or B) begin
Y = A&&B;
end
endmodule
3
or gate struCtural
BeHaViour module OR_Gate_Structural
(input A, input B, output Y);
or (Y, A, B);
module or_gate (
endmodule
input A, B,
output reg Y
);
always @(A or B) begin
Y = A | B;
end
endmodule
4
Not gate struCtural
BeHaViour module NOT_GATE_Structural
(input A, output Y);
module NOT_gate (
not (Y, A);
input A,
endmodule
output reg Y
);
always @(A ) begin
Y =~A;
end
endmodule
5
NaNd gate struCtural
BeHaViour module
NAND_GATE_Structural (input
module NOT_gate ( A,inputB output Y);
input A,B, nand (Y, A, B);
output reg Y endmodule
);
always @(A or B) begin
Y =~(A & B);
end
endmodule
6
Nor gate struCtural
BeHaViour module NOR_GATE_Structural
(input A,input B output Y);
module NOR_gate (
nor (Y, A, B);
input A,B,
endmodule
output reg Y
);
always @(A or B) begin
Y =~(A|B);
end
endmodule
7
Xor gate struCtural
BeHaViour module XOR_GATE_Structural
(input A,input B output Y);
module XOR_gate (
xor (Y, A, B);
input A,B,
endmodule
output reg Y
);
always @(A or B) begin
Y =(A^B);
end
endmodule
8
XNor gate struCtural
BeHaViour module
XNOR_GATE_Structural (input
module XNOR_gate ( A,input B output Y);
input A,B, xnor (Y, A, B);
output reg Y endmodule
);
always @(A or B) begin
Y =^(A~B);
end
endmodule
9
HalF adder struCtural
BeHaViour module half_adder_structural (
input wire a, b,
module half_adder_behavioral (
output wire sum, carry
input A, B,
);
output reg sum, carry
xor(sum, a, b);
);
and(carry, a, b);
always @(A or B) begin
endmodule
sum = A ^ B;
carry = A & B;
end
endmodule
data FloW
module half_adder_dataflow (
input wire a, b,
output wire sum, carry
);
assign sum = a ^ b;
assign carry = a & b;
endmodule
10
Full adder struCtural
BeHaViour module full_adder_structural (
input A, B, Cin,
module full_adder_behavioral (
output Sum, Cout
input A, B, Cin,
);
output reg Sum, Cout
xor (Sum, A, B, Cin);
);
or (Cout, (A & B), (B & Cin),
always @(A or B or Cin) begin
(A & Cin));
Sum = A ^ B ^ Cin;
endmodule
Cout = (A & B) | (B & Cin) |
(A & Cin);
end
endmodule
data FloW
module full_adder_dataflow (
input A, B, Cin,
output wire Sum, Cout
);
assign Sum = A ^ B ^ Cin;
assign Cout = (A & B) | (B &
Cin) | (A & Cin);
endmodule
11
suBtraCtor less = 0;
Comparator
module comparator(a, b, greater,
equal, less);
input [3:0] a, b;
output reg greater, equal, less;
12
paritY eNCoder
module encoder_4to2(input [3:0] t FlipFlop
data, output reg [1:0] encoded); module T_flipflop ( input T, clk,
output reg Q
always @(data) begin );
case (data) always @(posedge clk) begin
4'b1000: encoded = 2'b11; if (T == 1)
4'b0100: encoded = Q <= ~Q;
2'b10; else
4'b0010: encoded = Q <= Q;
2'b01;
end
4'b0001: encoded =
2'b00; endmodule
); );
assign P = A * B;
endmodule always @(posedge clk or
posedge reset) begin
13
if (reset) begin always @(posedge clk or
q <= 0; posedge reset) begin
2'b10: q <= 1;
2'b11: q <= ~q; endmodule
endcase up CouNter
qbar <= ~q; module up_counter (
end input clk,
end input reset,
endmodule output reg [3:0] q
);
sHiFt registers
module shift_register ( always @(posedge clk or
posedge reset) begin
input clk,
if (reset)
input reset,
q <= 4'b0000;
input d,
else
output reg [3:0] q
q <= q + 1;
);
end
14
endmodule input d,
15
d FlipFlop output q, qbar
);
BeHaViour model
module d_flipflop (
wire dbar, r1, r2, r3, r4, s1, s2;
input clk,
not (dbar, d);
input reset,
nand (r1, d, clk);
input d,
nand (r2, dbar, clk);
output reg q,
nand (s1, r1, s2);
output qbar
nand (s2, r2, s1);
);
16
assign product = a * b;
assign qbar = ~q;
endmodule
always @(posedge clk or 1:2 demultipleXer
posedge reset) begin
module demux_1to2 (
if (reset)
input din,
q <= 0;
input sel,
else begin
output reg d0,
case ({s, r})
output reg d1
2'b00: q <= q;
);
2'b01: q <= 0;
2'b10: q <= 1;
always @(din, sel) begin
2'b11: q <= 1'bx;
if (sel == 0) begin
endcase
d0 = din;
end
d1 = 0;
end
end else begin
endmodule
d0 = 0;
multiplier d1 = din;
module multiplier ( end
input [3:0] a, end
input [3:0] b,
output [7:0] product endmodule
);
demultipleXer
17
module demux_1to4 ( input [1:0] sel,
input din, output reg dout
input [1:0] sel, );
output reg [3:0] dout
); always @(din or sel) begin
case (sel)
always @(din or sel) begin 2'b00: dout = din[0];
dout = 4'b0000; 2'b01: dout = din[1];
case (sel) 2'b10: dout = din[2];
2'b00: dout[0] = din; 2'b11: dout = din[3];
2'b01: dout[1] = din; endcase
2'b10: dout[2] = din; end
2'b11: dout[3] = din; endmodule
endcase BeHaVioral model
end (usiNg iF-else
statemeNt)
endmodule
module mux_4to1_if_else (
4:1 multipleXer input [3:0] din,
BeHaVioral model input [1:0] sel,
statemeNt) );
module mux_4to1_behavioral (
always @(din or sel) begin
input [3:0] din,
if (sel == 2'b00)
18
dout = din[0]; endmodule
else if (sel == 2'b01) struCtural modeliNg
dout = din[1]; module mux_4to1_structural (
else if (sel == 2'b10) input [3:0] din,
dout = din[2]; input [1:0] sel,
else output dout
dout = din[3]; );
end
19
BeHaVioral model always @(d0 or d1 or sel) begin
20
3'b111: dout = din[7];
not (not_sel, sel); endcase
and (y0, d0, not_sel); end
and (y1, d1, sel); endmodule
or (dout, y0, y1); BeHaVioral model
endmodule (usiNg iF-else)
multipleXer module mux_8to1_if_else (
21
else dout = din[7]; CopyEdit struCtural
end model
endmodule
module mux_8to1_structural (
dataFloW model input [7:0] din,
module mux_8to1_dataflow ( input [2:0] sel,
input [7:0] din, output dout
input [2:0] sel, );
output dout wire s0, s1, s2;
); wire y0, y1, y2, y3, y4, y5, y6,
assign dout = (sel == 3'b000 & y7;
din[0]) |
(sel == 3'b001 & din[1]) not (s0, sel[0]);
| not (s1, sel[1]);
(sel == 3'b010 & din[2])
not (s2, sel[2]);
|
(sel == 3'b011 & din[3])
| and (y0, din[0], s2, s1, s0);
(sel == 3'b100 & din[4]) and (y1, din[1], s2, s1, sel[0]);
| and (y2, din[2], s2, sel[1], s0);
(sel == 3'b101 & din[5]) and (y3, din[3], s2, sel[1], sel[0]);
|
and (y4, din[4], sel[2], s1, s0);
(sel == 3'b110 & din[6])
and (y5, din[5], sel[2], s1, sel[0]);
|
and (y6, din[6], sel[2], sel[1], s0);
(sel == 3'b111 & din[7]);
and (y7, din[7], sel[2], sel[1],
Endmodule
sel[0]);
verilog
22
output reg [3:0] bin
or (dout, y0, y1, y2, y3, y4, y5, );
y6, y7);
endmodule always @(gray) begin
BiNarY to graY Code bin[3] = gray[3];
CoNVerter bin[2] = bin[3] ^ gray[2];
(BeHaVioral model) bin[1] = bin[2] ^ gray[1];
module binary_to_gray ( bin[0] = bin[1] ^ gray[0];
input [3:0] bin, end
output reg [3:0] gray endmodule
); ripple CarrY adder
BeHaVioral model
always @(bin) begin
(usiNg alWaYs
gray[3] = bin[3];
BloCK)
gray[2] = bin[3] ^ bin[2];
module
gray[1] = bin[2] ^ bin[1]; ripple_carry_adder_behavioral (
gray[0] = bin[1] ^ bin[0]; input [3:0] a, b,
end input cin,
endmodule output reg [3:0] sum,
graY to BiNarY Code output reg cout
CoNVerter );
(BeHaVioral model)
module gray_to_binary ( always @(a or b or cin) begin
input [3:0] gray, {cout, sum} = a + b + cin;
23
end module
endmodule ripple_carry_adder_structural (
input [3:0] a, b,
dataFloW model
input cin,
(usiNg assigN)
output [3:0] sum,
module
output cout
ripple_carry_adder_dataflow (
);
input [3:0] a, b,
wire c1, c2, c3;
input cin,
full_adder FA0 (a[0], b[0], cin,
output [3:0] sum,
sum[0], c1);
output cout
full_adder FA1 (a[1], b[1], c1,
); sum[1], c2);
assign {cout, sum} = a + b + cin; full_adder FA2 (a[2], b[2], c2,
endmodule sum[2], c3);
full_adder FA3 (a[3], b[3], c3,
struCtural model
sum[3], cout);
(usiNg Full adders) endmodule
module full_adder (
input a, b, cin,
BootH arraY
output sum, cout
multiplier
);
module booth_multiplier (
assign sum = a ^ b ^ cin;
input [3:0] multiplicand,
assign cout = (a & b) | (b & cin) |
(a & cin); input [3:0] multiplier,
endmodule output [7:0] product
);
24
reg [7:0] p;
integer i;
reg prev_bit;
always @(multiplicand,
multiplier) begin
p = 0;
prev_bit = 0;
for (i = 0; i < 4; i = i + 1) begin
case ({multiplier[i],
prev_bit})
2'b10: p = p -
(multiplicand << i);
2'b01: p = p +
(multiplicand << i);
2'b00: p = p;
2'b11: p = p;
endcase
prev_bit = multiplier[i];
end
end
assign product = p;
endmodule
25