Verilog Code FAQ
Verilog Code FAQ
Verilog Code FAQ
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 clock enable.
Following is Verilog code for a 4-bit register with a positive-edge clock, asynchronous
set and clock enable.
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.
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.
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.
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.
Following is the Verilog code for a 4-bit signed up counter with an asynchronous reset
and a modulo maximum.
module counter (clk, clr, q);
parameter MAX_SQRT = 4, MAX = (MAX_SQRT*MAX_SQRT);
input clk, clr;
output [MAX_SQRT-1:0] q;
reg [MAX_SQRT-1:0] cnt;
always @ (posedge clk or posedge clr)
begin
if (clr)
cnt <= 0;
else
cnt <= (cnt + 1) %MAX;
end
assign q = cnt;
endmodule
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,
serial in and 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.
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.
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 a 4-to-1 1-bit MUX using an If statement.
Following is the Verilog Code for a 4-to-1 1-bit MUX using a Case statement.
Following is the Verilog code for a 3-to-1 1-bit MUX with a 1-bit latch.
Following is the Verilog code for an unsigned 8-bit adder with carry in.
endmodule
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.
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.
module mult(clk, a, b, mult);
input clk;
input [17:0] a;
input [17:0] b;
output [35:0] mult;
reg [35:0] mult;
reg [17:0] a_in, b_in;
wire [35:0] mult_res;
reg [35:0] pipe_1, pipe_2, pipe_3;
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.
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.
do <= RAM[addr];
end
end
endmodule
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.
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.
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).
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
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;
4’b1001: data = 4’b0010;
4’b1010: data = 4’b1110;
4’b1011: data = 4’b0010;
4’b1100: data = 4’b0100;
4’b1101: data = 4’b1010;
4’b1110: data = 4’b1100;
4’b1111: data = 4’b0000;
default: data = 4’bXXXX;
endcase
end
endmodule
Following is the Verilog code for an FSM with a single process.