verilog语法
2.关键词 Verilog HDL 定义了一系列保留字,叫做关键词。注意只有小 写的关键词才是保留字。例如,标识符always (这是个关键 词)与标识符ALWAYS(非关键词)是不同的。
always and assign begin buf buf if0 bufif1 case casex casez cmos deassign default defparam disable edge else end endcase endmodule endfunction endprimitive endspecify endtable endtask event for force forever fork function highz0 highz1 if ifnone initial inout input integer join large macrmodule medium module nand negedge nmos nor not notif0 notif1 or output parameter pmos posedge primitive pull0 pull1 pullup pulldown rcmos real realtime reg release repeat rnmos rpmos rtran rtranif0 rtranif1 scalared small specify specparam strong0 strong1 supply0 supply1 table task time trantranif0 tranif1 tri tri0 tri1 triand trior trireg vectored wait wand weak0 weak1 while wire wor xnor xor
3.空白符和注释
module MUX2_1 (out, a, b, sel); 格式自由 // Port declarations 单行注释 使用空白符提高可读性及代码组 output out; 到行末结束 织。Verilog忽略空白符除非用于 input sel, // control input 分开其它的语言标记。 b, /* data inputs */ a; /* The netlist logic selects input ”a” when 多行注释,在/* */内 sel = 0 and it selects ”b” when sel = 1. */ not (sel_, sel); and (a1, a, sel_), (b1, b, sel); // What does this // line do? or (out, a1, b1); endmodule
第二讲 Verilog 语法
Verilog模块module Verilog语法要素 Verilog数据类型及逻辑系统
2.1 模块module
模块是verilog设计中的基本功能块,在第一讲有简单交待, 这里详细介绍模块内部构成 module 模块名 ( 端口列 表 ); 端口声明,参数声明
C格式来声明端口
//D 触发器 module D_FF (input d, clk, clr, output reg q,qb); ………… ………… endmodule
例设计三位全加器
模块名
端口列表
sum[2:0]
module adder ( cout,sum,a,b,cin ); input [2:0] a,b; input cin; output cout;
例SR触发器模块
//SR 触发器 module SR_FF (Q, Q_n,S,R); output Q, Q_n; //端口声明 input S,R; nand n1(Q, S,Q_n); nand n2(Q_n,R,Q);
S
Q
R
Q_n
nand为verilog中的 与非门门级原语部件
endmodule
12 'H83a 8'b1100_ 0001 64'hff01 9'O17 32'bz01x 3‟b1010_ 1101 6.3 32e- 4 4.1E3 unsized decimal (zero-extended to 32 bits) unsized hexadecimal (zero- extended to 32 bits) 8-bit binary 64-bit hexadecimal (zero- extended to 64 bits) 9-bit octal Z-extended to 32 bits 3-bit number, truncated to 3’b101 decimal notation scientific notation for 0.0032 scientific notation for 4100
合法和非法标识符
合法的: shift_reg_a busa_index bus263 非法的: 34net //不能用数字开头 a*b_net //不能含有非字母符号*
n@263 //不能含有非字母符号@
标识符书写注意项:
1、用有意义的有效的名字如Sum 、CPU_addr等。 2、用下划线区分词。 3、采用一些前缀或后缀,如 时钟采用Clk 前缀:Clk_50,Clk_CPU; 低电平采用_n 后缀:Enable_n; 4、统一一定的缩写如全局复位信号Rst。 5、同一信号在不同层次保持一致性,如同一时钟信号必须在各模块 保持一致。 6、参数采用大写,如SIZE 。
wire,reg和其它类型 的变量声明
可选 低层模块实例 always和initial块,所有 行为语句都在块内 必须出现
数据流语句 (assign)
任务和函数
endmodule
module能够表示: 物理块,如IC或ASIC单元 逻辑块,如一个CPU设计的ALU部分 整个系统 每一个模块的描述从关键词module开始,有一个名称(如 SN74LS74,DFF,ALU等等),由关键词endmodule结束。
端口声明
a[2:0]
cout
adder
cin
b[2:0]
output [2:0] sum;
assign {cout,sum}=a+b+cin; endmodule
数据流语句
这个例子描述了一个三位的加法器。从例子中可以看出整个 Verilog HDL程序是嵌套在module和endmodule声明语句里 的,只出现了一个assign语句。
模块端口等价于芯片的管脚(pin) 模块通过端口与外部通信
端口列表和端口声明
端口等价于硬件 的引脚(pin)
端口在模块名字 后的括号中列出
端口可以说明为 input, output及 inout
端口声明
input 输入端口 output 输出端口 inout 双向端口
也可以采用类似ANSI
D_FF d2 (d[ 2], clk, clr, q[ 2], qb[ 2]);
D_FF d3 (d[ 3], clk, clr, q[ 3], qb[ 3]);
当设计大规模系统时,端口太多,记住端口顺序不大可 能,可以采用按名连接方法。
D_FF d0 (.d(d[ 0]), .clk(clk), .clr(clr), .q(q[ 0]), .qb(qb[ 0]));
模块中只出现在低层次模块实例化
模块实例化(module instances)
可以将模块的实例通过端口连接起来构成一个大的系 统或元件。 在上面的例子中,REG4有模块DFF的四个实例。注 意,每个实例都有自己的名字(d0, d1, d2, d3)。实例 名是每个对象唯一的标记,通过这个标记可以查看每 个实例的内部。 实例中端口的次序与模块定义的次序相同。
模块实例化与调用程序不同。每个实例都是模块的一 个完全的拷贝,相互独立、并行。
.端口与外部信号的连接
在调用模块时,可以用顺序连接和按名连接把模块定义的 端口与外部信号连接起来
顺序连接:需要连接的信号需要与模块声明的端口列表一致; 按名连接:端口和外部信号按名字连clk, clr, q[ 0], qb[ 0]); D_FF d1 (d[ 1], clk, clr, q[ 1], qb[ 1]);
4.整数常量和实数常量
Verilog中,常量(literals)可是整数也可以是实数 整数的大小可以定义也可以不定义。整数表示为: <size>‟<base><value> 其中 size :大小,由二进制数表示的位数(bit)表示。缺省为32位 base:数基,可为2(b)、8(o)、10(d)、16(h)进制。缺省为10进制 value:是所选数基内任意有效数字,包括X、Z。 实数常量可以用十进制或科学表示法表示。
4位寄存器设计
module D_FF (d, clk, clr, q, qb); .... endmodule module REG4( d, clk, clr, q, qb); output [3: 0] q, qb; input [3: 0] d; input clk, clr; D_FF d0 (d[ 0], clk, clr, q[ 0], qb[ 0]); D_FF d1 (d[ 1], clk, clr, q[ 1], qb[ 1]); D_FF d2 (d[ 2], clk, clr, q[ 2], qb[ 2]); D_FF d3 (d[ 3], clk, clr, q[ 3], qb[ 3]); endmodule
模块中的5个部分并没全部出现, 只出现在低层次模块实例 化
D触发器模块
//D 触发器 module D_FF (d, clk,clr,q,qb); output q, qb; input d,clk,clr; always块行为描述语句 reg q, qb;//输出端口q, qb值保存 always @(posedge clk) begin if (clr) q=1‟b0; else 该模块内包括always行为 q=d; 块语句 end not (qb,q); endmodule