Verilog基本模型(Basic Modelling)

本文介绍了Verilog编程的基础,包括模组的概念、原语的使用,特别是内置原语和用户自定义原语的语法及示例,以及模块实例化和端口声明的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

andnandornor,xorxnor

Output, Input(s)

Buffer and inverter

bufnot

Output(s), Input

Tristate logic

bufif0bufif1notif0,notif1

Output, Input, Enable

Pullup and pulldown

pulluppulldown

Output


Name

Switch Type

Terminals

MOS

nmospmos,rnmosrpmos

Output, Input, Enable

CMOS

cmosrcmos

Output, Input, N-Enable, P-Enable

Bidirectional pass

tranrtran

Inout1, Inout2

Bidirectional pass with control

tranif0tranif1,rtranif0rtranif1

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值