Systemverilog for design 笔记(一)

转载请标明出处

一、     System Verilog 声明的位置

1.       包(packages)

Verilog要求局部声明:

variables, nets, tasks and functions的声明需要在模块内部的module...endmodule关键词之间

System Verilog 增加了用户定义类型typedef

i.              包的定义

包在packageand endpackage.之间定义。

包中可以包含的可综合的结构有:

         parameter and localparam constant definitions常量

const variable definitions变量

typedef user-defined types用户定义类型

• Fully automatic task and function definitions

import statements from other packages

• Operator overload definitions

模块中的每个实例可以对parameter重新定义,但是包中不能。包中的参数不能重新定义

为了能够综合,包中的定义的task和function必须声明为automatic,且不能包含静态变量 

Example: 一个包的定义

package definitions;                                                                //定义一个包

parameter VERSION = "1.1";                                                //定义包中一个参数

typedef enum{ADD, SUB, MUL} opcodes_t;                   //定义包中枚举opcodes_t

typedef struct {

logic [31:0] a, b;

opcodes_t opcode;

} instruction_t;                                                                         //定义包中结构体instruction_t

function automatic [31:0] multiplier (input [31:0] a, b);//定义函数function,其中参数为a,b

return a * b; // 用户定义的function代码从这开始

endfunction                                                                              //function定义结束

endpackage                                                                               //包定义结束

i. 引用包的内容

模块和接口可以用四种方式引用包中的定义和声明:

1. 用范围解析操作符” :: ”直接引用

2. 将包中特定子项导入(import)到module or interface中

3. 将通配符(*)导入包中的子项到module or interface

4. 将包中子项导入(通过import且可使用*)到$unit声明域中(module

For 1:

module ALU

(input definitions::instruction_t IW,                                                        //用范围解析操作符::引用definitions包中的instruction_t结构体,并对其命名为IW

input logic clock,

output logic [31:0] result

);

always_ff @(posedge clock) begin

case (IW.opcode)

definitions::ADD : result = IW.a + IW.b; //用范围解析操作符::引用definitions包中的枚举ADD

definitions::SUB : result = IW.a - IW.b;

definitions::MUL : result = definitions::

multiplier(IW.a, IW.b);

endcase

end

endmodule

For 2:

module ALU
(input definitions::instruction_t IW,
input logic clock,
output logic [31:0] result
);

import definitions::ADD;                     //import导入definitions包中的枚举ADD
import definitions::SUB;
import definitions::MUL;
import definitions::multiplier;
always_comb begin                          
  case (IW.opcode)
    ADD : result = IW.a + IW.b;            //前面导入包中特定子项后,该子项在模块/接口中可见,可直接使用
    SUB : result = IW.a - IW.b;
    MUL : result = multiplier(IW.a, IW.b);

  endcase
end
endmodule

import导入方式是将每个子项逐个导入,在有许多子项需要从包中导入时并不实用,此时使用通配符导入会更实用。

  For 3:

通配符导入方式:import definitions::*; // wildcard import 通配符导入不能自动导入包中所有内容,只有在模块/接口中实际引用的子项被导入进来了。

module ALU
(input definitions::instruction_t IW,
input logic clock,
output logic [31:0] result
);
import definitions::*; // 通配符导入。实际上是将包添加到标识符搜索路径中
always_comb begin
  case (IW.opcode)
    ADD : result = IW.a + IW.b;//引用ADD时,在definitions包中查找该名称的定义
    SUB : result = IW.a - IW.b;
    MUL : result = multiplier(IW.a, IW.b);
  endcase
end
endmodule

For 4:

$编译单元声明(不可综合):

只作用于同时编译的源文件(每次编译为一个单元,多次编译之间互不影响)

SV允许在package, module, interface and program block 外部进行声明。这些外部声明在“编译单元域”(compilation-unit scope)中,并且对所有同时编译的module可见

编译单元contain:

  1. 时间单元和精度声明 2.Variable declarations3. Net declarations 4. Constant declarations    5. User-defined data types, using typedef, enum or class        6. Task and function definitions

可在$unit中声明的可综合结构有:

  1. typedef用户定义类型定义(最好放在包内)   2.Automatic functions    

3.Automatic tasks  4. parameter and localparam constants      5.import

 

因此,1.不要在$unit空间进行声明,声明在命名包中进行

2.可将包import$unit

3.import语句要出现在包中子项被引用前

 

import definitions::instruction_t;       //或者import definitions::*;

 

module ALU

(input instruction_t IW,

input logic clock,

output logic [31:0] result

);

加入条件编译后:(条件编译包文件的文件名:definitions.pkg)

`ifndef DEFS_DONE // if the already-compiled flag is not set...

`define DEFS_DONE // set the flag

package definitions;

********//包定义

endpackage

import definitions::*; // import package into $unit

`endif

需要包含条件编译包文件的设计文件:

`include "definitions.pkg" // 编译包文件

module ALU

(input instruction_t IW,   //前面已通过import definitions::*导入包中子项instruction_t

......

endmodule

 

 

ii. 综合指导

1.了能够综合,包中定义的taskfunction必须声明为automatic,且不能包含静态变量。

2.综合不支持包中变量声明,静态任务和静态函数。

仿真时,包中变量会被导入该变量的模块共享,而不是通过module port传递数据的模块间通信,是不可综合的

2. 未命名语句块的声明

命名块中的局部变量声明:

Verilog在命名的begin...end fork ... join块中声明局部变量。可对局部变量进行层次化引用(引用命名块里面的局部变量,但是不可综合)

SV可在未命名的begin...end fork ... join块中声明局部变量。,不能对局部变量进行层次化引用(因为块没名啊= =

3. 仿真时间单位和精度

对比:

A.Verilog使用‘timescale 1ns / 10ps1ns是时间单位,10ps是时间精度(多个源文件中可对时间单位进行重新定义,若一个源文件未定义,根据编译顺序决定其时间单位)

B.SV可给时间值指定时间单位:forever #5ns clock = ~clock;

SV中支持范围级时间单位:

module adder (input wire [63:0] a, b,);

timeunit 1ns;     //局部时间单元,只在该mudule中有效

timeprecision 10ps;  //局部时间精度,只在该mudule中有效

...

endmodule

时间值单位及精度具体使用哪个来确定采取就近原则(个人理解)

原文地址:https://www.cnblogs.com/daisyuer/p/9199508.html