1、单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,m,*,Verilog,硬件描述语言,(二),2,课程内容,一、,Verilog HDL,运算符,二、,Verilog HDL,语句,三、,可综合设计,一、Verilog HDL 运算符,按功能分:,算术运算符、逻辑运算符、关系运算符、缩减运算符、条件运算符、位运算符、移位运算符、拼接运算符等类。,按操作数的个数分:,单目运算符、双目运算符、三目运算符。,一、Verilog HDL 运算符,算术运算符,(Arithmetic operator),+,加,-,减,*乘,/,除,%,求模,一、Verilog HDL
2、 运算符,逻辑运算符,(,Logical operator,),&,逻辑与,|,逻辑或,!,逻辑非,一、Verilog HDL 运算符,A B,A&B,A|B,!A !B,1 1,1,1,0 0,0,0,1,0 1,0 1,0,1,1 0,0 0,0,0,1 1,一、Verilog HDL 运算符,位运算符,(,Bitwise operator,),按位取反,&,按位与,|,按位或,按位异或,按位同或,一、Verilog HDL 运算符,&,0 1 x,0,0 0 0,1,0 1 x,x,0 x x,按位与真值表,|,0 1 x,0,0 1 x,1,1 1 1,x,x 1 x,按位或真值表,0
3、 1 x,0,0 1 x,1,1 0 x,x,x x x,按位异或真值表,一、Verilog HDL 运算符,关系运算符,(Relational operator),小于,大于,=,大于或等于,注意:,“,n,或,A,右移,=,=!=!=,&,|,&,|,?:,二、Verilog HDL 语句,分类,类 别,语 句,可综合性,过程语句,always,initial,块语句,串行块,begin-end,并行块,fork-join,赋值语句,持续赋值,assign,过程赋值,=,、,=,条件语句,if-else,case,循环语句,for,repeat,while,forever,编译向导,def
4、ine,include,ifdef,else,endif,二、Verilog HDL 语句,过程语句:,always,always(),begin,/,过程赋值,/if-,else,case,选择语句,end,二、Verilog HDL 语句,敏感信号类型:,(a),(a or b),(,posedge,clock),(,negedge,clock),(,posedge,clk,or,negedge,reset),举例:,DFF,module DFF(d,clk,reset,q,qb,);,output q,qb,;,input,clk,reset,d,;,reg,q,qb,;,always(
5、posedge,clk,),begin,if(!reset),begin,q=,0;qb=1;,end,else,begin,q=,d;qb=d;,end,end,endmodule,二、Verilog HDL 语句,特点:,只有两种状态:执行状态和等待状态,一般由敏感信号的变化来启动,各个,always,间通过信号线进行通信,一个,always,中只允许描述对应于一个时钟信号的同步时序逻辑,always,之间是并发执行的,二、Verilog HDL 语句,块语句:,begin end,总是在,always,内部,按顺序执行,二、Verilog HDL 语句,举例:,reg,qa,qb,qc
6、always(,posedge,clk,),begin,qa,=d;,qb,=,qa,;,qc=,qb,;,end,二、Verilog HDL 语句,赋值语句:,持续赋值语句,过程赋值语句,二、Verilog HDL 语句,持续赋值语句:,assign,c=a&b,;,二、Verilog HDL 语句,过程赋值语句:,非阻塞赋值,“,=,”,阻塞赋值,“,=”,分为两步骤:右式计算、左式更新,二、Verilog HDL 语句,非阻塞赋值,:,当前语句的执行不会阻塞下一语句的执行,语句之间并发执行,左式更新在块结束后才进行,二、Verilog HDL 语句,阻塞赋值,:,当前语句的执行会阻
7、塞下一语句的执行,语句之间顺序执行,右式计算和左式更新同时进行,举例,:,module,nonblocking(clk,reset,a,b,);,input,clk,reset,;input 3:0a;,output 3:0b;,reg,3:0b;reg 3:0y;,always(,posedge,clk,or,negedge,reset),begin,if(!reset,),begin,y=,0;b=0;,end,else,begin,y=,a;b=y;,end,end,endmodule,结果,:,举例,:,module,nonblocking(clk,reset,a,b,);,input
8、clk,reset,;input 3:0a;,output 3:0b;,reg,3:0b;reg 3:0y;,always(,posedge,clk,or,negedge,reset),begin,if(!reset,),begin,y=,0;b=0;,end,else,begin,b=y;,y=a;,end,end,endmodule,结果,:,举例,:,module,blocking(clk,reset,a,b,);,input,clk,reset,;input 3:0a;,output 3:0b;,reg,3:0b;reg 3:0y;,always(,posedge,clk,or,ne
9、gedge,reset),begin,if(!reset,),begin,y=0;b=0;,end,else,begin,y=a;b=y;,end,end,endmodule,结果,:,举例,:,module,blocking(clk,reset,a,b,);,input,clk,reset,;input 3:0a;,output 3:0b;,reg,3:0b;reg 3:0y;,always(,posedge,clk,or,negedge,reset),begin,if(!reset,),begin,y=0;b=0;,end,else,begin,b=y;y=a;,end,end,endmo
10、dule,结果,:,举例,:,module,nonblocking(clk,reset,a,b,);,input,clk,reset,;input 3:0a;,output 3:0b;,reg,3:0b;reg 3:0y;,always(,posedge,clk,or,negedge,reset),begin,if(!reset,),begin,y=0;b=0;,end,else,begin,y=a;b=y;,end,end,endmodule,结果,:,举例,:,module,nonblocking(clk,reset,a,b,);,input,clk,reset,;input 3:0a;,
11、output 3:0b;,reg,3:0b;reg 3:0y;,always(,posedge,clk,or,negedge,reset),begin,if(!reset,),begin,y=0;b=0;,end,else,begin,y=a;b=y;,end,end,endmodule,结果,:,阻塞非阻塞使用原则:,有,clock,的,always,进程要使用,non-blocking,always(posedge,clk,or,negedge,reset_n,),begin,if(!,reset_n,),counter=8b00;,else,counter=,counter,+1;,en
12、d,二、Verilog HDL 语句,always(sel,or a or b),begin,case(,sel,),2b00:c=a;,2b01:c=b;,endcase,end,无,clock,的,always,进程使用,blocking,二、Verilog HDL 语句,二、Verilog HDL 语句,continuous,assignment,使用,blocking,一个,always,进程中不要同时使用,blocking,与,non-blocking,assign y=,a&b,;,二、Verilog HDL 语句,书,P286,页图,8-10,错误,参考,MIT,课件,说明:,二
13、Verilog HDL 语句,条件语句:,if,else,case,endcase,二、Verilog HDL 语句,if,else,:,if(,表达式,),语句,1,;,if(,表达式,),语句,1,;,else,语句,2,;,if(,表达式,1),语句,1,;,else if(,表达式,2),语句,2,;,else if(,表达式,3),语句,3,;,else if(,表达式,n),语句,n,;,else,语句,n+1,;,二、Verilog HDL 语句,特点,:,不,完整的,if else,容易导致产生,latch,总是在,always,内部按顺序执行,二、Verilog HDL 语
14、句,Latch,与,DFF,比较,:,latch,由电平触发,,DFF,由时钟沿触发,latch,容易产生毛刺(,glitch,),DFF,则不易产生毛刺,latch,消耗的门资源比,DFF,要少,但耗费的,LE,资源要多,latch,将静态时序分析变得极为复杂,二、Verilog HDL 语句,case endcase,:,case(,敏感表达式,),值,1,:语句,1,;,值,2,:语句,2,;,值,n,:语句,n,;,default,:语句,n+1,;,endcase,二、Verilog HDL 语句,特点,:,不,完整的,case endcase,容易导致产生,latch,总是在,al
15、ways,内部根据敏感量执行,二、Verilog HDL 语句,比较,:,if else,带有优先级,case endcase,延时小,举例,:四位选择器,always(sel),begin,if(sel=2b00),out=in0;,else if(sel=2b01),out=in1;,else if(sel=2b10),out=in2;,else,out=in3;,end,always(sel),begin,case(sel),2b00:out=in0;,2b01:out=in1;,2b10:out=in2;,default:out=in3;,end,举例,:,七段数码管显示译码器,mod
16、ule decode4_7(decodeout,indec);,output6:0,decodeout,;,input3:0,indec,;,reg6:0,decodeout,;,always(,indec,),begin,case(indec,),4d0:decodeout=7b1111110,;,4d1:decodeout=7b0110000,;,4d2:decodeout=7b1101101,;,4d3:decodeout=7b1111001,;,4d4:decodeout=7b0110011,;,4d5:decodeout=7b1011011,;,4d6:decodeout=7b101
17、1111,;,4d7:decodeout=7b1110000,;,4d8:decodeout=7b1111111,;,4d9:decodeout=7b1111011,;,default:,decodeout,=7b1111111;,endcase,end,endmodule,三、可综合设计,要点,:,不使用初始化语句,不使用任务和函数,不使用带有延时的描述,不使用,for,循环,在,always,里面慎用乘法和除法,举例,:,module test_13(clk,rst_n,data,num);,input,clk,rst_n;input 12:0data;,output,3:0num;,re
18、g,3:0i;reg 3:0num;,always,(posedge,clk,),begin,if(!rst_n,),num=0;,else,begin,for(i,=0;i13;i=i+1),if(datai,),num,=num+1;,end,end,endmodule,结果,:,举例,:,module,multip(clk,rst_n,a,b,num,);,input,clk,rst_n,;,input 7:0a;,input 7:0b;,output 15:0num;,reg,15:0num;,always(posedge,clk,),begin,if(!rst_n,),num,=0;,else,num,=a*b;,end,endmodule,结果,:,举例,:,module,div(clk,rst_n,a,b,num,);,input,clk,rst_n,;,input 7:0a;,input 7:0b;,output 7:0num;,reg,7:0num;,always(posedge,clk,),begin,if(!rst_n,),num,=0;,else,num,=a/b;,end,endmodule,结果,:,竞赛实验板说明,:,结构示意图,:,






