Verilog Modules For Common Digital Functions
Verilog Modules For Common Digital Functions
Verilog Modules For Common Digital Functions
for Common
Digital Functions
Full Adder (Dataflow Description)
//
// Here is a data flow description of a full_adder
//
assign sum = a ^ b ^ ci ;
assign co = (a & b) | ((a | b) & ci) ;
endmodule
Full Adder (Behvioral Description)
endmodule
Full Adder Testbench
// testbench for fa cell
module fa_tb ;
reg a, b, ci ;
reg [3:0] i ;
wire sum, co ;
initial begin
end
endmodule
Adder (4-bit) – Structural Description
endmodule
2-1 Multiplexer
// 2-1 MUX
// This is a dataflow description
// This is a behavioral description.
module mux2to1(out, sel, in) ;
module mux2to1(out, sel, in) ;
input sel ;
input sel ;
output out ;
output out ;
input [1:0] in ;
input [1:0] in ;
reg out ;
assign out = sel ? in[1] : in[0] ;
always @(sel or in) begin
endmodule
if (sel == 1) out = in[1] ;
else out = in[0] ;
end
endmodule
2-1 Multiplexer (Case Statement)
// 2-1 MUX
// This is a behavioral description.
endmodule
3-8 Decoder
//
// 3-8 decoder with an enable input
// This is a behavioral description.
//
module decoder_tb ;
reg [2:0] a ;
reg en ;
reg [3:0] i ;
wire [7:0] out ;
// Exhaustively test it
initial begin
$monitor($time, " en is %b, a is %b, out is %b\n",en, a, out) ;
#10 begin
en = 0 ;
for (i=0; i < 8; i=i+1) #10 a = i ;
end
#10 begin
en = 1 ;
for (i=0; i < 8; i=i+1) begin #10 a = i ;
end
end
endmodule
Digital Magnitude Comparator
endmodule
D-Latch
/*
Verilog description of a negative-level senstive D latch
with preset (active low)
*/
endmodule
D Flip Flop
module test_dff ;
wire q ;
reg d, clr, clk ;
initial begin
clk = 1'b1 ;
forever #5 clk = ~clk ;
end
initial fork
#0 begin
clr = 1'b1 ;
d = 1'b0 ;
end
#20 begin
d = 1'b0 ;
clr=1'b0 ;
end
#30 d = 1'b1 ;
#50 d = 1'b0 ;
join
initial begin
$monitor($time, "clr=%b, d=%b,q=%b", clr, d, q) ;
end
endmodule
4-bit D Register with Parallel Load
//
// 4-bit D register with parallel load
//
module dreg_pld(Dout, Din, ld, clk) ;
input [3:0] Din ;
input ld, clk ;
output [3:0] Dout ;
reg [3:0] Dout ;
endmodule
1-Bit Counter Cell
// 1 bit counter module
endmodule
2-Bit Counter
// 2-bit counter
endmodule
74161 Operation
4-bit Synchronous Counter (74LS161)
// 4-bit counter
input clk ;
input nclr ;
input nload ;
input ent ;
input enp ;
output rco ;
output [3:0] count ;
input [3:0] parallel_in ;
output q;
input clk, reset ;
wire d;
endmodule
Ripple Counter
/*
This is an asynchronout ripple carry counter.
It is 4-bits wide.
*/
output [3:0] q ;
input clk, reset ;
endmodule
Up/Down Counter
endmodule
Shift Register
endmodule
Universal Shift Register
endmodule
Mealy FSM
Inputs Outputs
COMBINATIONAL
LOGIC
Current State
Next state
Registers
Q D
CLK
DRAM Controller
// Example of a Mealy machine for a DRAM controller
parameter s0 = 0, s1 = 1, s2 = 2, s3 = 3, s4 = 4 ;
s0 : begin
if (refresh)
begin
next_state = s3 ;
ras = 1'b1 ;
cas = 1'b0 ;
ready = 1'b0 ;
end
else if (cs)
begin
next_state = s1 ;
ras = 1'b0 ;
cas = 1'b1 ;
ready = 1'b0 ;
end
else
begin
next_state = s0 ;
ras = 1'b1 ;
cas = 1'b1 ;
ready = 1'b1 ;
end
end
DRAM Controller (part 3)
s1 : begin
next_state = s2 ;
ras = 1'b0 ;
cas = 1'b0 ;
ready = 1'b0 ;
end
s2 : begin
if (~cs)
begin
next_state = s0 ;
ras = 1'b1 ;
cas = 1'b1 ;
ready = 1'b1 ;
end
else
begin
next_state = s2 ;
ras = 1'b0 ;
cas = 1'b0 ;
ready = 1'b0 ;
end
end
s3 : begin
next_state = s4 ;
ras = 1'b1 ;
cas = 1'b0 ;
ready = 1'b0 ;
end
s4 : begin
next_state = s0 ;
ras = 1'b0 ;
cas = 1'b0 ;
ready = 1'b0 ;
end
endcase
end
endmodule
1-bit ALU Module
// 1-bit alu like the one in Mano text (ECE580)
reg aci ;
endmodule
Modeling Memory
// memory model, bidir data bus
wire tri_en ;
endmodule
Binary to BCD
//
// We need to convert a 5-bit binary number
// to 2 BCD digits
//
endmodule