HDL Verilog Examples
HDL Verilog Examples
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 2
VERILOG CODE:SOME BASIC EXAMPLES.
Following is Verilog code for a flip-flop with a negative-edge clock and asynchronous clear.
Following is Verilog code for the flip-flop with a positive-edge clock and synchronous set.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 3
VERILOG CODE:SOME BASIC EXAMPLES.
Following is Verilog code for the flip-flop with a positive-edge clock and clock enable.
Following is Verilog code for a 4-bit register with a positive-edge clock, asynchronous set and clock
enable.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 4
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for a latch with a positive gate and an asynchronous clear.
Following is Verilog code for a 4-bit latch with an inverted gate and an asynchronous preset.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 5
VERILOG CODE:SOME BASIC EXAMPLES.
Following is Verilog code for a tristate element using a combinatorial process and always block.
Following is the Verilog code for a tristate element using a concurrent assignment.
Following is the Verilog code for a 4-bit unsigned up counter with asynchronous clear.
Following is the Verilog code for a 4-bit unsigned down counter with synchronous set.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 6
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for a 4-bit unsigned up counter with an asynchronous load from the
primary input.
Following is the Verilog code for a 4-bit unsigned up counter with a synchronous load with a constant.
Following is the Verilog code for a 4-bit unsigned up counter with an asynchronous clear and a clock
enable.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 7
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for a 4-bit unsigned up/down counter with an asynchronous clear.
Following is the Verilog code for a 4-bit signed up counter with an asynchronous reset.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 8
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for a 4-bit signed up counter with an asynchronous reset and a modulo
maximum.
Following is the Verilog code for a 4-bit unsigned up accumulator with an asynchronous clear.
Following is the Verilog code for an 8-bit shift-left register with a positive-edge clock,
cloc k, serial in and serial
out.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 9
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for an 8-bit shift-left register with a negative-edge clock, a clock enable, a
serial in and a serial out.
Following is the Verilog code for an 8-bit shift-left register with a positive-edge clock, asynchronous clear,
serial in and serial out.
Following is the Verilog code for an 8-bit shift-left register with a positive-edge clock, a synchronous set,
a serial in and a serial out.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 10
VERILOG CODE:SOME BASIC EXAMPLES.
output so;
reg [7:0] tmp;
always @(posedge clk)
begin
if (s)
tmp <= 8’b11111111;
else
tmp <= {tmp[6:0], si};
end
assign so = tmp[7];
endmodule
Following is the Verilog code for an 8-bit shift-left register with a positive-edge clock,
cloc k, a serial in and a
parallel out.
Following is the Verilog code for an 8-bit shift-left register with a positive-edge clock, an asynchronous
parallel load, a serial in and a serial out.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 11
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for an 8-bit shift-left register with a positive-edge clock, a synchronous
parallel load, a serial in and a serial out.
Following is the Verilog code for an 8-bit shift-left/shift-right register with a positive-edge clock, a serial
in and a serial out.
Following is the Verilog code for a 4-to-1 1-bit MUX using an If statement.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 12
VERILOG CODE:SOME BASIC EXAMPLES.
else if (s == 2’b10)
o = c;
else
o = d;
end
endmodule
Following is the Verilog code for a 3-to-1 1-bit MUX with a 1-bit latch.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 13
VERILOG CODE:SOME BASIC EXAMPLES.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 14
VERILOG CODE:SOME BASIC EXAMPLES.
else if (sel[1])
code = 3’b001;
else if (sel[2])
code = 3’b010;
else if (sel[3])
code = 3’b011;
else if (sel[4])
code = 3’b100;
else if (sel[5])
code = 3’b101;
else if (sel[6])
code = 3’b110;
else if (sel[7])
code = 3’b111;
else
code = 3’bxxx;
end
endmodule
Following is the Verilog code for an unsigned 8-bit adder with carry in.
endmodule
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 15
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for an unsigned 8-bit adder with carry out.
assign tmp = a + b;
assign sum = tmp [7:0];
assign co = tmp [8];
endmodule
Following is the Verilog code for an unsigned 8-bit adder with carry in and carry out.
endmodule
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 16
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for an unsigned 8-bit greater or equal comparator.
endmodule
assign res = a * b;
endmodule
Following Verilog template shows the multiplication operation placed outside the always block and the
pipeline stages represented as single registers.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 17
VERILOG CODE:SOME BASIC EXAMPLES.
endmodule
Following Verilog template shows the multiplication operation placed inside the always block and the
pipeline stages are represented as single registers.
Following Verilog template shows the multiplication operation placed outside the always block and the
pipeline stages represented as single registers.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 18
VERILOG CODE:SOME BASIC EXAMPLES.
end
endmodule
Following Verilog template shows the multiplication operation placed inside the always block and the
pipeline stages are represented as single registers.
Following Verilog template shows the multiplication operation placed outside the always block and the
pipeline stages represented as shift registers.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 19
VERILOG CODE:SOME BASIC EXAMPLES.
endmodule
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 20
VERILOG CODE:SOME BASIC EXAMPLES.
do <= RAM[addr];
end
end
endmodule
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 21
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for a single-port RAM with asynchronous read.
Following is the Verilog code for a single-port RAM with "false" synchronous read.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 22
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for a single-port RAM with synchronous read (read through).
Following is the Verilog code for a single-port block RAM with enable.
Following is the Verilog code for a dual-port RAM with asynchronous read.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 23
VERILOG CODE:SOME BASIC EXAMPLES.
input clk;
input we;
input [4:0] a;
input [4:0] dpra;
input [3:0] di;
output [3:0] spo;
output [3:0] dpo;
reg [3:0] ram [31:0];
always @(posedge clk)
begin
if (we)
ram[a] <= di;
end
assign spo = ram[a];
assign dpo = ram[dpra];
endmodule
Following is the Verilog code for a dual-port RAM with false synchronous read.
spo = ram[a];
dpo = ram[dpra];
end
endmodule
Following is the Verilog code for a dual-port RAM with synchronous read (read through).
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 24
VERILOG CODE:SOME BASIC EXAMPLES.
Following is the Verilog code for a dual-port RAM with enable on each port.
module raminfr (clk, ena, enb, wea, addra, addrb, dia, doa, dob);
input clk, ena, enb, wea;
input [4:0] addra, addrb;
input [3:0] dia;
output [3:0] doa, dob;
reg [3:0] ram [31:0];
reg [4:0] read_addra, read_addrb;
always @(posedge clk)
begin
if (ena) begin
if (wea) begin
ram[addra] <= dia;
end
end
end
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 25
VERILOG CODE:SOME BASIC EXAMPLES.
always @(raddr)
begin
if (en)
case(raddr)
4’b0000: data = 4’b0010;
4’b0001: data = 4’b0010;
4’b0010: data = 4’b1110;
4’b0011: data = 4’b0010;
4’b0100: data = 4’b0100;
4’b0101: data = 4’b1010;
4’b0110: data = 4’b1100;
4’b0111: data = 4’b0000;
4’b1000: data = 4’b1010;
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 26
VERILOG CODE:SOME BASIC EXAMPLES.
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 27
VERILOG CODE:SOME BASIC EXAMPLES.
endmodule
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 28
VERILOG CODE:SOME BASIC EXAMPLES.
if (reset)
state <= s1;
else
state <= next_state;
end
VERILOG EXAMPLES------V.L.K
EXAMPLES------V.L.K Page 29