Professional Documents
Culture Documents
for Common
Digital Functions
module fa_tb ;
reg a, b, ci ;
reg [3:0] i ;
wire sum, co ;
fa
initial begin
$monitor($time, " a is %b, b is %b, ci is %b, sum is %b, co is %b\n",
a, b, ci, sum , co) ;
for (i=0; i < 8; i=i+1) #10 {a, b, ci} = i ;
end
endmodule
endmodule
g0(sum[0],
g1(sum[1],
g2(sum[2],
g3(sum[3],
2-1 Multiplexer
// 2-1 MUX
// This is a behavioral description.
module mux2to1(out, sel, in) ;
input sel ;
output out ;
input [1:0] in ;
reg out ;
always @(sel or in) begin
if (sel == 1) out = in[1] ;
else out = in[0] ;
end
endmodule
//
//
//
//
3-8 Decoder
3-8 decoder with an enable input
This is a behavioral description.
[7:0] out ;
[2:0] a ;
en ;
reg
[7:0] out ;
[2:0] a ;
en ;
[3:0] i ;
[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
endmodule
D-Latch
/*
Verilog description of a negative-level senstive D latch
with preset (active low)
*/
module dlatch(q, clock, preset, d) ;
output q ;
input clock, d, preset ;
reg
q;
always @(clock or d or preset) begin
if (!preset) q = 1 ;
else if (!clock) q = d ;
end
endmodule
D Flip Flop
// Negative edge-triggered D FF with sync clear
module D_FF(q, d, clr, clk) ;
output q ;
input d, clr, clk ;
reg
q;
always @ (negedge clk) begin
if (clr)
q <= 1'b0 ;
else
q <= d ;
end
endmodule
q ;
d, clr, clk ;
D_FF
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
endmodule
2-Bit Counter
// 2-bit counter
module cnt2bit(cnt, tout, encnt, clr, clk) ;
output [1:0] cnt ;
output tout ;
input encnt ;
input clr ;
input clk ;
wire ttemp ;
cnt1bit u0(cnt[0], ttemp, encnt, clr, clk) ;
cnt1bit u1(cnt[1], tout, ttemp, clr, clk) ;
endmodule
74161 Operation
clk ;
nclr ;
nload ;
ent ;
enp ;
rco ;
[3:0] count ;
[3:0] parallel_in ;
reg
reg
[3:0] count ;
rco ;
T Flip-flops
// T type flip-flop built from D flip-flop and inverter
module t_ff(q, clk, reset) ;
output
input
wire
d_ff
not
q;
clk, reset ;
d;
endmodule
Ripple Counter
/*
*/
tff0(q[0],
tff1(q[1],
tff2(q[2],
tff3(q[3],
endmodule
clk, reset) ;
q[0], reset) ;
q[1], reset) ;
q[2], reset) ;
Up/Down Counter
// 4-bit up/down counter
module up_dwn(cnt, up, clk, nclr) ;
output [3:0] cnt ;
input up, clk, nclr ;
reg
[3:0] cnt ;
Shift Register
// 4 bit shift register
`define WID 3
module serial_shift_reg(sout, pout, sin, clk) ;
output sout ;
output [`WID:0] pout ;
input sin, clk ;
reg
[`WID:0] pout ;
always @(posedge clk) begin
pout <= {sin, pout[`WID:1]} ;
end
Mealy FSM
Inputs
Outputs
COMBINATIONAL
LOGIC
Current State
Registers
Q
CLK
Next state
DRAM Controller
// Example of a Mealy machine for a DRAM controller
module mealy(clk, cs, refresh, ras, cas, ready) ;
input
clk, cs, refresh ;
output
ras, cas, ready ;
parameter
s0 = 0, s1 = 1, s2 = 2, s3 = 3, s4 = 4 ;
s3 ;
s1 ;
s0 ;
s0 ;
s2 ;
Modeling Memory
// memory model, bidir data bus
module memory(data, addr, ce, rw) ;
inout [7:0] data ;
input ce, rw ;
input [5:0] addr ;
reg
reg
tri
wire
[7:0] mem[0:63] ;
[7:0] data_out ;
[7:0] data ;
tri_en ;
Binary to BCD
//
// We need to convert a 5-bit binary number
// to 2 BCD digits
//
module bin2bcd(bcd1, bcd0, bin) ;
output
output
input
[3:0] bcd1 ;
[3:0] bcd0 ;
[4:0] bin ;
reg
reg
reg
integer
[3:0] bcd1 ;
[3:0] bcd0 ;
[7:0] bcd ;
i ;