Verilog基本模型(Basic Modelling)
Verilog的基本组成单元为模组(Module)。
语法(Syntax)
模组关键字 模组名 [ ( 端口列表 ) ];
模组组成项;
endmodule
模组关键字 = module | macromodule
module_word module_name [ ( port_list ) ];
module_items;
endmodule
module_word = module | macromodule
例程(Example)
module Mod1(A, B, C);
input A, B;
ouput C;
assign C = A & B;
endmodule
原语(Primitives)
原语极为小的元件,Verilog中有些内置的原语(built-in primitives),即门模型和开关模型。用户可以自己定义原语(UDP User Defined Primitives)。
Built-in Primitives
语法
gate_type [ ( strength ) ] [ #( delay ) ] [ instance_name ] [ instance_range ] ( terminal, terminal, ... );
switch_type [ #( delay ) ] [ instance_name ] [ instance_range ] ( terminal, terminal, ... );
内置原语列表如下:
Name |
Gate Type |
Terminals |
Logic |
and, nand, or, nor,xor, xnor |
Output, Input(s) |
Buffer and inverter |
buf, not |
Output(s), Input |
Tristate logic |
bufif0, bufif1, notif0,notif1 |
Output, Input, Enable |
Pullup and pulldown |
pullup, pulldown |
Output |
Name |
Switch Type |
Terminals |
MOS |
nmos, pmos,rnmos, rpmos |
Output, Input, Enable |
CMOS |
cmos, rcmos |
Output, Input, N-Enable, P-Enable |
Bidirectional pass |
tran, rtran |
Inout1, Inout2 |
Bidirectional pass with control |
tranif0, tranif1,rtranif0, rtranif1 |
Inout1, Inout2, Control |
例程
and u1 (Q, A, B);
and #(2.1, 2.8) u2 (Q, A, B);
and (pull0, strong1) (Q, A, B);
UDP(User Defined Primitive)
语法
primitive 元件名 (输出, 输入, ...);
端口声明
[ reg 输出; ]
[ initial 输出 = 初始值; ]
table
真值表
endtable
endprimitive
primitive UDP_name (output, input, ...);
port_declaration
[ reg output; ]
[ initial output = initial_value; ]
table
truth_table
endtable
endprimitive
真值表中符号及定义表:
Symbol |
Definition |
0 |
Logic 0 |
1 |
Logic 1 |
x or X |
Unknown |
? |
Don't care if input is 0, 1 or X |
b or B |
Don't care if input is 0 or 1 |
- |
Output does not change (sequential UDP only) |
(vw) |
Input transition from logic v to logic w |
r or R |
Rising input transition: (01) |
f or F |
Falling input transition: (10) |
p or P |
Positive input transition: (01), (0x) or (x1) |
n or N |
Negative input transition: (10), (1x) or (x0) |
* |
Any possible input transition: (??) |
例程
primitive Mux (y, a, b, sel); // 组合逻辑 UDP
output y;
input a, b, sel;
table
// a b sel : y
0 ? 0 : 0;
1 ? 0 : 1;
? 0 1 : 0;
? 1 1 : 1;
endtable
endprimitive
primitive Dff (q, d, clk, rst); // 时序逻辑 UDP
output q;
input clk, rst, d;
reg q;
initial q = 0;
table
// d clk rst : old q : q
? ? 0 : ? : 0;
0 R 1 : ? : 0;
1 (01) 1 : ? : 1;
? N 1 : ? : -;
* ? 1 : ? : -;
? ? (0?): ? : -;
endtable
endprimitive
实例化(Instantiation)
在模块中定义一个子元件成为对其实例化。
语法
module_name [ strength ] [ #( token_expression ) ] instance_name [ instance_range ] ( port_connection );
token_expression = delay_expression | parameter_expression | .parameter_name(parameter_expression)
port_connection = expression, expression, ... | .port_name(expression), .port_name(expression), ...
例程
Dff #(4) u1 (.Clk(Clock), .D(D_In), .Q(Q_Out));
Dff u2 (Clock, D_In, Q_Out);
Cnt u3 (Clk, , A&&B, Q);
Nand (weak1, pull0) #(2) u4 (Q, A, B);
端口声明(Port Declaration)
端口为模块的接口。
语法
端口方向 [ 端口大小 ] 端口名, 端口名, ...;
端口方向 数据类型 [ 端口大小 ] 端口名, 端口名, ...;
端口方向 = 输入 | 输出 | 方向
port_direction [ port_size ] port_name, port_name, ...;
port_direction data_type [ port_size ] port_name, port_name, ...;
port_direction = input | output | inout
例程
input Clk;
output [7:0] Q;
input wire Clk; // Verilog-2001
output reg [7:0] Q; // Verilog-2001
module Cnt (output reg [7:0] Q,
input wire Clk, Reset, Enable,
input wire [7:0] D ); // Verilog-2001 ANSI-style