资源描述
王金明:《Verilog HDL程序设计教程》
- 1 -
【例 3.1】4 位全加器
module adder4(cout,sum,ina,inb,cin);
output[3:0] sum;
output cout;
input[3:0] ina,inb;
input cin;
assign {cout,sum}=ina+inb+cin;
endmodule
【例 3.2】4 位计数器
module count4(out,reset,clk);
output[3:0] out;
input reset,clk;
reg[3:0] out;
always @(posedge clk)
begin
if (reset) out<=0; //同步复位
else out<=out+1; //计数
end
endmodule
【例 3.3】4 位全加器的仿真程序
`timescale 1ns/1ns
`include "adder4.v"
module adder_tp; //测试模块的名字
reg[3:0] a,b; //测试输入信号定义为reg型
reg cin;
wire[3:0] sum; //测试输出信号定义为wire型
wire cout;
integer i,j;
adder4 adder(sum,cout,a,b,cin); //调用测试对象
always #5 cin=~cin; //设定cin的取值
initial
begin
a=0;b=0;cin=0;
for(i=1;i<16;i=i+1)
#10 a=i; //设定a的取值
end 程序文本
- 2 -
initial
begin
for(j=1;j<16;j=j+1)
#10 b=j; //设定b的取值
end
initial //定义结果显示格式
begin
$monitor($time,,,"%d + %d + %b={%b,%d}",a,b,cin,cout,sum);
#160 $finish;
end
endmodule
【例 3.4】4 位计数器的仿真程序
`timescale 1ns/1ns
`include "count4.v"
module coun4_tp;
reg clk,reset; //测试输入信号定义为reg型
wire[3:0] out; //测试输出信号定义为wire型
parameter DELY=100;
count4 mycount(out,reset,clk); //调用测试对象
always #(DELY/2) clk = ~clk; //产生时钟波形
initial
begin //激励信号定义
clk =0; reset=0;
#DELY reset=1;
#DELY reset=0;
#(DELY*20) $finish;
end
//定义结果显示格式
initial $monitor($time,,,"clk=%d reset=%d out=%d", clk, reset,out);
endmodule
【例 3.5】 “与-或-非”门电路
module AOI(A,B,C,D,F); //模块名为AOI(端口列表A,B,C,D,F)
input A,B,C,D; //模块的输入端口为A,B,C,D
output F; //模块的输出端口为F 王金明:《Verilog HDL程序设计教程》
- 3 -
wire A,B,C,D,F; //定义信号的数据类型
assign F= ~((A&B)|(C&D)); //逻辑功能描述
endmodule
【例 5.1】用 case语句描述的 4 选 1 数据选择器
module mux4_1(out,in0,in1,in2,in3,sel);
output out;
input in0,in1,in2,in3;
input[1:0] sel;
reg out;
always @(in0 or in1 or in2 or in3 or sel) //敏感信号列表
case(sel)
2'b00: out=in0;
2'b01: out=in1;
2'b10: out=in2;
2'b11: out=in3;
default: out=2'bx;
endcase
endmodule
【例 5.2】同步置数、同步清零的计数器
module count(out,data,load,reset,clk);
output[7:0] out;
input[7:0] data;
input load,clk,reset;
reg[7:0] out;
always @(posedge clk) //clk上升沿触发
begin
if (!reset) out = 8'h00; //同步清0,低电平有效
else if (load) out = data; //同步预置
else out = out + 1; //计数
end
endmodule
【例 5.3】用 always 过程语句描述的简单算术逻辑单元
`define add 3'd0
`define minus 3'd1
`define band 3'd2
`define bor 3'd3
`define bnot 3'd4 程序文本
- 4 -
module alu(out,opcode,a,b);
output[7:0] out;
reg[7:0] out;
input[2:0] opcode; //操作码
input[7:0] a,b; //操作数
always@(opcode or a or b) //电平敏感的always块
begin
case(opcode)
`add: out = a+b; //加操作
`minus: out = a-b; //减操作
`band: out = a&b; //求与
`bor: out = a|b; //求或
`bnot: out=~a; //求反
default: out=8'hx; //未收到指令时,输出任意态
endcase
end
endmodule
【例 5.4】用 initial过程语句对测试变量 A、B、C 赋值
`timescale 1ns/1ns
module test;
reg A,B,C;
initial
begin
A = 0; B = 1; C = 0;
#50 A = 1; B = 0;
#50 A = 0; C = 1;
#50 B = 1;
#50 B = 0; C = 0;
#50 $finish ;
end
endmodule
【例 5.5】用 begin-end 串行块产生信号波形
`timescale 10ns/1ns
module wave1;
reg wave;
parameter cycle=10;
initial
begin 王金明:《Verilog HDL程序设计教程》
- 5 -
wave=0;
#(cycle/2) wave=1;
#(cycle/2) wave=0;
#(cycle/2) wave=1;
#(cycle/2) wave=0;
#(cycle/2) wave=1;
#(cycle/2) $finish ;
end
initial $monitor($time,,,"wave=%b",wave);
endmodule
【例 5.6】用 fork-join 并行块产生信号波形
`timescale 10ns/1ns
module wave2;
reg wave;
parameter cycle=5;
initial
fork
wave=0;
#(cycle) wave=1;
#(2*cycle) wave=0;
#(3*cycle) wave=1;
#(4*cycle) wave=0;
#(5*cycle) wave=1;
#(6*cycle) $finish;
join
initial $monitor($time,,,"wave=%b",wave);
endmodule
【例 5.7】持续赋值方式定义的 2 选 1 多路选择器
module MUX21_1(out,a,b,sel);
input a,b,sel;
output out;
assign out=(sel==0)?a:b;
//持续赋值,如果sel为0,则out=a ;否则out=b
endmodule
【例 5.8】阻塞赋值方式定义的 2 选 1 多路选择器
module MUX21_2(out,a,b,sel);
input a,b,sel; 程序文本
- 6 -
output out;
reg out;
always@(a or b or sel)
begin
if(sel==0) out=a; //阻塞赋值
else out=b;
end
endmodule
【例 5.9】非阻塞赋值
module non_block(c,b,a,clk);
output c,b;
input clk,a;
reg c,b;
always @(posedge clk)
begin
b<=a;
c<=b;
end
endmodule
【例 5.10】阻塞赋值
module block(c,b,a,clk);
output c,b;
input clk,a;
reg c,b;
always @(posedge clk)
begin
b=a;
c=b;
end
endmodule
【例 5.11】模为 60 的 BCD码加法计数器
module count60(qout,cout,data,load,cin,reset,clk);
output[7:0] qout;
output cout;
input[7:0] data;
input load,cin,clk,reset;
reg[7:0] qout;
always @(posedge clk) //clk上升沿时刻计数 王金明:《Verilog HDL程序设计教程》
- 7 -
begin
if (reset) qout<=0; //同步复位
else if(load) qout<=data; //同步置数
else if(cin)
begin
if(qout[3:0]==9) //低位是否为9,是则
begin
qout[3:0]<=0; //回0,并判断高位是否为5
if (qout[7:4]==5) qout[7:4]<=0;
else
qout[7:4]<=qout[7:4]+1; //高位不为5,则加1
end
else //低位不为9,则加1
qout[3:0]<=qout[3:0]+1;
end
end
assign cout=((qout==8'h59)&cin)?1:0; //产生进位输出信号
endmodule
【例 5.12】BCD码—七段数码管显示译码器
module decode4_7(decodeout,indec);
output[6:0] decodeout;
input[3:0] indec;
reg[6:0] decodeout;
always @(indec)
begin
case(indec) //用case语句进行译码
4'd0:decodeout=7'b1111110;
4'd1:decodeout=7'b0110000;
4'd2:decodeout=7'b1101101;
4'd3:decodeout=7'b1111001;
4'd4:decodeout=7'b0110011;
4'd5:decodeout=7'b1011011;
4'd6:decodeout=7'b1011111;
4'd7:decodeout=7'b1110000;
4'd8:decodeout=7'b1111111;
4'd9:decodeout=7'b1111011;
default: decodeout=7'bx;
endcase
end 程序文本
- 8 -
endmodule
【例 5.13】用 casez 描述的数据选择器
module mux_casez(out,a,b,c,d,select);
output out;
input a,b,c,d;
input[3:0] select;
reg out;
always @(select or a or b or c or d)
begin
casez(select)
4'b???1: out = a;
4'b??1?: out = b;
4'b?1??: out = c;
4'b1???: out = d;
endcase
end
endmodule
【例 5.14】隐含锁存器举例
module buried_ff(c,b,a);
output c;
input b,a;
reg c;
always @(a or b)
begin
if((b==1)&&(a==1)) c=a&b;
end
endmodule
【例 5.15】用 for 语句描述的七人投票表决器
module voter7(pass,vote);
output pass;
input[6:0] vote;
reg[2:0] sum;
integer i;
reg pass;
always @(vote)
begin
sum=0; 王金明:《Verilog HDL程序设计教程》
- 9 -
for(i=0;i<=6;i=i+1) //for语句
if(vote[i]) sum=sum+1;
if(sum[2]) pass=1; //若超过4人赞成,则pass=1
else pass=0;
end
endmodule
【例 5.16】用 for 语句实现 2 个 8 位数相乘
module mult_for(outcome,a,b);
parameter size=8;
input[size:1] a,b; //两个操作数
output[2*size:1] outcome; //结果
reg[2*size:1] outcome;
integer i;
always @(a or b)
begin
outcome=0;
for(i=1; i<=size; i=i+1) //for语句
if(b[i]) outcome=outcome +(a << (i-1));
end
endmodule
【例 5.17】用 repeat 实现 8 位二进制数的乘法
module mult_repeat(outcome,a,b);
parameter size=8;
input[size:1] a,b;
output[2*size:1] outcome;
reg[2*size:1] temp_a,outcome;
reg[size:1] temp_b;
always @(a or b)
begin
outcome=0;
temp_a=a;
temp_b=b;
repeat(size) //repeat语句,size为循环次数
begin
if(temp_b[1]) //如果temp_b的最低位为1,就执行下面的加法
outcome=outcome+temp_a;
temp_a=temp_a<<1; //操作数a左移一位 程序文本
- 10 -
temp_b=temp_b>>1; //操作数b右移一位
end
end
endmodule
【例 5.18】同一循环的不同实现方式
module loop1; //方式1
integer i;
initial
for(i=0;i<4;i=i+1) //for语句
begin
$display(“i=%h”,i);
end
endmodule
module loop2; //方式2
integer i;
initial begin
i=0;
while(i<4) //while语句
begin
$display ("i=%h",i);
i=i+1;
end
end
endmodule
module loop3; //方式3
integer i;
initial begin
i=0;
repeat(4) //repeat语句
begin
$display ("i=%h",i);
i=i+1;
end
end
endmodule
【例 5.19】使用了`include 语句的 16 位加法器 王金明:《Verilog HDL程序设计教程》
- 11 -
`include "adder.v"
module adder16(cout,sum,a,b,cin);
output cout;
parameter my_size=16;
output[my_size-1:0] sum;
input[my_size-1:0] a,b;
input cin;
adder my_adder(cout,sum,a,b,cin); //调用adder模块
endmodule
//下面是adder模块代码
module adder(cout,sum,a,b,cin);
parameter size=16;
output cout;
output[size-1:0] sum;
input cin;
input[size-1:0] a,b;
assign {cout,sum}=a+b+cin;
endmodule
【例 5.20】条件编译举例
module compile(out,A,B);
output out;
input A,B;
`ifdef add //宏名为add
assign out=A+B;
`else
assign out=A-B;
`endif
endmodule
【例 6.1】加法计数器中的进程
module count(data,clk,reset,load,cout,qout);
output cout;
output[3:0] qout;
reg[3:0] qout;
input[3:0] data;
input clk,reset,load;
程序文本
- 12 -
always @(posedge clk) //进程1,always过程块
begin
if (!reset) qout= 4'h00; //同步清0,低电平有效
else if (load) qout= data; //同步预置
else qout=qout + 1; //加法计数
end
assign cout=(qout==4'hf)?1:0; //进程2,用持续赋值产生进位信号
endmodule
【例 6.2】任务举例
module alutask(code,a,b,c);
input[1:0] code;
input[3:0] a,b;
output[4:0] c;
reg[4:0] c;
task my_and; //任务定义,注意无端口列表
input[3:0] a,b; //a,b,out名称的作用域范围为task任务内部
output[4:0] out;
integer i;
begin
for(i=3;i>=0;i=i-1)
out[i]=a[i]&b[i]; //按位与
end
endtask
always@(code or a or b)
begin
case(code)
2'b00: my_and(a,b,c);
/* 调用任务my_and,需注意端口列表的顺序应与任务定义中的一致,这里的 a,b,c
分别对应任务定义中的a,b,out */
2'b01: c=a|b; //或
2'b10: c=a-b; //相减
2'b11: c=a+b; //相加
endcase
end
endmodule
王金明:《Verilog HDL程序设计教程》
- 13 -
【例 6.3】测试程序
`include "alutask.v"
module alu_tp;
reg[3:0] a,b;
reg[1:0] code;
wire[4:0] c;
parameter DELY = 100;
alutask ADD(code,a,b,c); //调用被测试模块
initial begin
code=4'd0; a= 4'b0000; b= 4'b1111;
#DELY code=4'd0; a= 4'b0111; b= 4'b1101;
#DELY code=4'd1; a= 4'b0001; b= 4'b0011;
#DELY code=4'd2; a= 4'b1001; b= 4'b0011;
#DELY code=4'd3; a= 4'b0011; b= 4'b0001;
#DELY code=4'd3; a= 4'b0111; b= 4'b1001;
#DELY $finish;
end
initial $monitor($time,,,"code=%b a=%b b=%b c=%b", code,a,b,c);
endmodule
【例 6.4】函数
function[7:0] get0;
input[7:0] x;
reg[7:0] count;
integer i;
begin
count=0;
for (i=0;i<=7;i=i+1)
if (x[i]=1'b0) count=count+1;
get0=count;
end
endfunction
【例 6.5】用函数和 case语句描述的编码器(不含优先顺序)
module code_83(din,dout);
input[7:0] din;
output[2:0] dout;
程序文本
- 14 -
function[2:0] code; //函数定义
input[7:0] din; //函数只有输入,输出为函数名本身
casex (din)
8'b1xxx_xxxx : code = 3'h7;
8'b01xx_xxxx : code = 3'h6;
8'b001x_xxxx : code = 3'h5;
8'b0001_xxxx : code = 3'h4;
8'b0000_1xxx : code = 3'h3;
8'b0000_01xx : code = 3'h2;
8'b0000_001x : code = 3'h1;
8'b0000_000x : code = 3'h0;
default: code = 3'hx;
endcase
endfunction
assign dout = code(din) ; //函数调用
endmodule
【例 6.6】阶乘运算函数
module funct(clk,n,result,reset);
output[31:0] result;
input[3:0] n;
input reset,clk;
reg[31:0] result;
always @(posedge clk) //在clk的上升沿时执行运算
begin
if(!reset) result<=0; //复位
else begin
result <= 2 * factorial(n); //调用factorial函数
end
end
function[31:0] factorial; //阶乘运算函数定义(注意无端口列表)
input[3:0] opa; //函数只能定义输入端, 输出端口为函数名本身
reg[3:0] i;
begin
factorial = opa ? 1 : 0;
for(i= 2; i <= opa; i = i+1) //该句若要综合通过,opa应赋具体的数值
factorial = i* factorial; //阶乘运算
end 王金明:《Verilog HDL程序设计教程》
- 15 -
endfunction
endmodule
【例 6.7】测试程序
`define clk_cycle 50
`include "funct.v"
module funct_tp;
reg[3:0] n;
reg reset,clk;
wire[31:0] result;
initial //定义激励向量
begin
n=0; reset=1; clk=0;
for(n=0;n<=15;n=n+1)
#100 n=n;
end
initial $monitor($time,,,"n=%d result=%d",n,result);
//定义输出显示格式
always # `clk_cycle clk=~clk; //产生时钟信号
funct funct_try(.clk(clk),.n(n),.result(result),.reset(reset));
//调用被测试模块
endmodule
【例 6.8】顺序执行模块 1
module serial1(q,a,clk);
output q,a;
input clk;
reg q,a;
always @(posedge clk)
begin
q=~q;
a=~q;
end
endmodule
【例 6.9】顺序执行模块 2
module serial2(q,a,clk);
output q,a; 程序文本
- 16 -
input clk;
reg q,a;
always @(posedge clk)
begin
a=~q;
q=~q;
end
endmodule
【例 6.10】并行执行模块 1
module paral1(q,a,clk);
output q,a;
input clk;
reg q,a;
always @(posedge clk)
begin
q=~q;
end
always @(posedge clk)
begin
a=~q;
end
endmodule
【例 6.11】并行执行模块 2
module paral2(q,a,clk);
output q,a;
input clk;
reg q,a;
always @(posedge clk)
begin
a=~q;
end
always @(posedge clk)
begin
q=~q;
end
endmodule
【例 7.1】调用门元件实现的 4 选 1 MUX 王金明:《Verilog HDL程序设计教程》
- 17 -
module mux4_1a(out,in1,in2,in3,in4,cntrl1,cntrl2);
output out;
input in1,in2,in3,in4,cntrl1,cntrl2;
wire notcntrl1,notcntrl2,w,x,y,z;
not (notcntrl1,cntrl2),
(notcntrl2,cntrl2);
and (w,in1,notcntrl1,notcntrl2),
(x,in2,notcntrl1,cntrl2),
(y,in3,cntrl1,notcntrl2),
(z,in4,cntrl1,cntrl2);
or (out,w,x,y,z);
endmodule
【例 7.2】用 case语句描述的 4 选 1 MUX
module mux4_1b(out,in1,in2,in3,in4,cntrl1,cntrl2);
output out;
input in1,in2,in3,in4,cntrl1,cntrl2;
reg out;
always@(in1 or in2 or in3 or in4 or cntrl1 or cntrl2)
case({cntrl1,cntrl2})
2'b00:out=in1;
2'b01:out=in2;
2'b10:out=in3;
2'b11:out=in4;
default:out=2'bx;
endcase
endmodule
【例 7.3】行为描述方式实现的 4 位计数器
module count4(clk,clr,out);
input clk,clr;
output[3:0] out;
reg[3:0] out;
always @(posedge clk or posedge clr)
begin
if (clr) out<=0;
else out<=out+1;
end
endmodule
程序文本
- 18 -
【例 7.4】数据流方式描述的 4 选 1 MUX
module mux4_1c(out,in1,in2,in3,in4,cntrl1,cntrl2);
output out;
input in1,in2,in3,in4,cntrl1,cntrl2;
assign out=(in1 & ~cntrl1 & ~cntrl2)|(in2 & ~cntrl1 & cntrl2)|
(in3 & cntrl1 & ~cntrl2)|(in4 & cntrl1 & cntrl2);
endmodule
【例 7.5】用条件运算符描述的 4 选 1 MUX
module mux4_1d(out,in1,in2,in3,in4,cntrl1,cntrl2);
output out;
input in1,in2,in3,in4,cntrl1,cntrl2;
assign out=cntrl1 ? (cntrl2 ? in4:in3):(cntrl
展开阅读全文