王金明: 《Verilog HDL 程序设计教程 》
【 19~ 3.1 】 4 位全加器
m。dule adder4 (cout , sum,ina ,inb , c in ) ;
。utput [3 : 0]
sum;
。utput cout ;
input [3 : 0] ina ,inb ;
input c in;
assign {cout , sum} =ina+i nb+c i n ;
endm。dule
【 例 3. 2】 4 位计数器
m。dule count 4 (out ,reset, c l k ) ;
。utput [3 : 0 ]。ut ;
input rese t , cl k;
reg [ 3 : 0] out ;
always @( p。sedge elk)
begin
if (rese t )
out<=O;
//同步复位
out <=out+l ;
//计数
else
end
endm。dule
【 19~ 3 . 3 】 4 位全加器的仿真程序
、 timescale l ns/ lns
、 include ” adder4 . v ”
da
dq
1
e
e
J
mx
r
u
a1J
‘α
.,
:
3
b
nu
fL
., P
’
。
e
reg c in;
wire [3 : 0 ] sum;
wire cou t ;
integer i, j ;
//测试模块的名字
//测试输入信号定义为 reg 型
//测试输出信号定义为 wi re 型
adder 4 adder (sum, cout , a , b , c i n ); //调用测试对象
always #5 cin= ~c i n ;
//设定 c in 的取值
init ial
begin
a =O; b=O; c i n=O;
f。r (i= l;i < 1 6 ; i = i+l )
#10
a =i;
end
//设定 a 的取值
- 1 -
程序文本
initial
i
qz
n
(
=
--
o
LA
h
U
S
E
-
e
。
A
甘
1
4
UF
<-1J
咱
i
rb
·
「J
--
·
「I
d
l +
气
」
//设定 b 的取值
//定义结果显示格式
end
initial
begin
♀mon itor ($time ,,,” 毛d +毛d + 毛b={ 告b, 毛d } ”, a, b , cin , cout , sum) ;
#160 $fini sh ;
end
endm。dule
【 19~ 3.4 ] 4 位计数器的仿真程序
、 timescale lns/lns
、 include ” count 4 . v ”
l
mx
u dq
c
., p
t
ns ue or
;
4
一
e
t
e
l
c
’ b
h
。
e
wire [3 : 0] out ;
parameter DELY=l OO ;
//测试输入信号定义为 reg 型
//测试输出信号定义为 wire 型
count4 mycount(out ,reset , c l k );
//调用测试对象
always #(DELY/2) elk = ~e l k ;
//产生时钟波形
initial
begin
elk =0 ; reset=O;
//激励信号定义
#DELY
#DELY
reset =l;
reset=O;
#(DELY 女 20) $fi nish ;
end
//定义结果显示格式
initial $monitor($ time , ,,” elk= 毛d re set = 屯d out =毛d”, e l k ,
reset , out) ;
endm。dule
【 侣。 3 . 5 】 “与-或-非”门电路
m。dule AOI(A,B , C, D, F) ;
input A,B,C,D;
。utput F;
- 2 -
//模块名为 AOI (端 口歹lj表 A, B, C, D, F)
//模块的输入端 口为 A, B, C, D
//模块的输出端口为 F
王金明: 《Verilog HDL 程序设计教程 》
wire A, B,C,D,F;
//定义信号的数据类型
assign F= ~( (A&B) I (C&D));
//逻辑功能描述
endm。dule
【 19~ 5.1 】用 case 语旬描述的 4 选 1 数据选择器
m。dule mux4_l (out,in0,in l , i n2,in3,sel);
。utput out ;
input in0,in l ,in2,in3 ;
input [l : O] sel;
reg out;
always @( inO 。z i nl 。z in2 。z in3 。z sel)
//敏感信号列表
case (sel )
2 ’ bOO:
out=in O;
2 ’ bO l :
out=inl;
2 ’ blO:
out = i口2;
2 ’ bll:
default : out =2 ’ bx ;
out=in3 ;
endcase
endm。dule
【 19~ 5. 2】同步置数、同步清零的计数器
m。dule count(out,data,load,reset,clk );
。utput [ 7 : 0] out ;
input [7 : 0] data;
input load,cl k , r e set ;
reg [7 : OJ out;
always @( p。sedge elk)
begin
//e lk 上升沿触发
if (!reset )
out = 8 ’ h OO ;
// 同步清 0,低电平有效
else if (load)
else
end
endm。dule
out = data ;
out = out + 1;
// 同步预置
//计数
【 19~ 5. 3 】用 always 过程语旬描述的简单算术逻辑单元
、 define add 3 ’ dO
、 define minus 3 ’ dl
、 define band 3 ’ d2
、 define bor 3 ’ d3
、 define bnot 3 ’ d4
- 3 -
程序文本
m。dule alu(out , opcode , a , b) ;
。utput [ 7 : 0 ] out ;
reg [7 : 0] out ;
input [2 : 0 ] opcode;
input [7 : 0 ] a , b ;
//操作码
//操作数
always @(opcode 。x a 。z b)
// 电平敏感的 always 块
begin
case (opcode)
、 add : out = a+b;
、 m inus : out = a-b ;
、 band : out = a&b ;
、 bor : out = a l b ;
、 b n o t : out = 呻a ;
//加操作
//减操作
//求与
//求或
//求反
default: out=8 ’ h x ;
//未收到指令时,输出任意态
endcase
end
endm。dule
【 19~ 5.4 】用 initial 过程语旬对测试变量 A、 B、 C 赋值
、 timescale lns/ ln s
m。dule t e st ;
reg A, B, C;
initial
begin
A = O;
B = l; C = O;
A = l; B = O;
c = l;
A = O;
B = l;
B = O;
C = O;
$ fini sh
#50
#50
#50
#50
#50
end
endmodule
【 例 5. 5 】用 be伊1-end 串行块产生信号波形
、 timescale lOns/ l ns
m。dule wave l ;
c y c
、
4
-- e
-
-
牛
nu
z
reg wave ;
at
an pi
ea
r e &
m
--
-l
』
咽
·
-
begin
-4-
王金明: 《Verilog HDL 程序设计教程 》
wave=O;
#(cycle/2) wave=l;
#(cycle/2) wave=O;
#(cycle/2) wave=l;
#(cycle/2) wave=O;
#(cycle/2) wave=l;
#(cycle/2) $f ini sh
end
initial $monitor {♀ t ime ,,,” wave= 告b ”, wave) ;
endm。dule 自
【 19~ 5. 6 】用 fork才oin 并行块产生信号波形
、 timescale l Ons/ l ns
m。dule wave2 ;
reg wave ;
an pi
ea
z e +
m
4 C V4 c
1
J = e
., R
r
at
·-
-L
』
唱
·
-
f。rk
wave=O;
#(cycle)
wave=l;
#(2*cycl e) wave=O;
#(3*cycl e) wave=l ;
#(4*cycl e) wave=O;
#(S*cycl e) wave=l;
#(6*cycl e) $fini sh ;
3。in
initial $monitor($ t ime ,,, ” wave= 每b ”, wave) ;
endm。dule
【 例 5.7 】 持续赋值方式定义的 2 选 l 多路选择器
m。dule MUX21_1(out , a,b , sel);
input a , b , sel ;
。utput out ;
assign out =( sel==O)?a:b ;
//持续赋值,如果 sel 为 0, 贝I] out=a : 否则 out =b
endmo dule
【 19~ 5. 8 】 阻塞赋值方式定义的 2 选 l 多路选择器
m。dule MUX21_2(out , a , b , sel) ;
input a , b , sel ;
- 5 -
//阻塞赋值
程序文本
。utput out ;
reg out;
always @(a 。z b 。z sel)
begin
if (sel ==O) out=a ;
else
out =b ;
end
endmodule
【 侣。 5.9 】 非阻塞赋值
m。dule non_ b lock(c , b , a , c l k) ;
。utput c , b ;
input c l k , a ;
reg c , b ;
always @( p。sedge elk)
begin
b<=a ;
c<=b ;
end
endm。dule
【 19~ 5. 10】 阻塞赋值
m。dule b l ock(c , b , a , clk) ;
。utput c , b ;
input c l k , a ;
reg c , b ;
always @( p。sedge elk)
b
c
begin
==
问
L的
nd
d
e
e
n e
.U
。·
伽
【 侣。 5.11 】 模为 60 的 BCD 码加法计数器
m。dule count60(qout , cout, data , load, c i n ,rese t , clk) ;
。utput [ 7 : 0] qout ;
。utput cout ;
input [7 : 0] dat a ;
input l oad, cin , c l k ,reset;
reg [ 7 : 0] q out ;
always @( p。sedge elk)
- 6 -
//el k 上升沿时刻计数
王金明: 《Verilog HDL 程 序设计教程》
begin
if (reset )
qout<=O;
else if ( load)
qout<=data ;
//同步复位
//同步置数
else if (cin)
begin
if (qout[3:0 ] == 9)
//低位是否为 9,是则
begin
qout[3:0]<=0 ;
if (qout[ 7 : 4 ] ==5) qout [ 7 : 4]<=0 ;
else
//囡 0, 并判断高位是否为 5
qout [7 : 4]<=qout[ 7 : 4 ]+1;
//高位不为 5,则加 l
end
else
qout [3 : 0]<=q out[3:0 ]+1;
//低位不为 9 ,则加 l
end
end
assign cout = ( (qout==8 ’ h59)&c in)?l: O;
//产生进位输出信号
endm。dule
【例 5.12】 BCD 码一七段数码管显示译码器
m。dule d ecode4_7 (decodeout ,indec ) ;
。utput [6:0] decodeout;
input [3 : 0] indec ;
reg [ 6 : 0] decodeout ;
always @( i ndec)
begin
case (indec)
4 ’ dO:decodeout=7 ’ b ll lll l O;
4 ’ d l : decodeout=7 ’ bOll OOOO;
4 ’ d2:decodeout=7 ’ b 1101101;
4 ’ d3:decodeout =7 ’ b l lllOO l;
4 ’ d4 : decodeout=7 ’ b011 0011;
4 ’ d5:decodeout =7 ’ b 1 0110 11;
4 ’ d6:decodeout=7 ’ b l Olll ll;
4 ’ d 7 : decodeout=7 ’ b ll l OOOO;
4 ’ d 8 : d ecodeout=7 ’ b l llll ll;
4 ’ d9 : decodeout =7 ’ b l lllO ll;
default: decodeout=7 ’ bx;
endcase
end
//用 case 语句进行译码
-7 -
程序文本
endm。dule
【 侣。 5. 13 】 用 casez 描述的数据选择器
m。dule mux_ casez(out , a , b , c , d , select) ;
。utput out ;
input a , b , c , d ;
input [3 : 0] select;
reg out ;
always @(se lec t 。z a 。z b 。z c 。z d)
begin
c asez (s e l ect)
4 ’ b ??? l : out = a ;
4 ’ b ?? l ? : ou t = b ;
4 ’ b ?l?? : o u t = c ;
4 ’ b l ??? : out = d ;
endcase
end
endm。dule
【 例 5. 14】 隐含锁存器举例
m。dule buri ed_ ff(c , b , a ) ;
。utput c ;
input b , a ;
reg c ;
always @(a o r b)
begin
if ( (b==l ) && (a==l ))
c =a&b ;
end
endm。dule
【 19~ 5. 15 】 用 for 语旬描述的七人投票表决器
m。dule vot er7 (pass , vote) ;
。utput pass ;
input [6 : 0] vot e ;
reg [ 2 : 0] sum;
integer i;
reg p ass ;
always @(vote)
begin
sum=O;
- 8 -