Verilog语法

1 Register

组合逻辑-->寄存器-->组合逻辑-->寄存器
Register是一个变量,用于存储值,并不代表一个真正的硬件DFF。

reg A,C;
// assignments are always done inside a procedure
A = 1;
C = A; //C gets the logical value 1
A = 0;  //C is still 1
C = 0;  //C is now 0

2 Vectors--矢量

  • Represent buses
wire [3:0] busA;
reg [1:4] busB;
reg [1:0] busC;
  • Left number is MS bit--左侧的数值是最高位
  • Slice management
/*
  busA位宽为4,表示4根线0,1,2,3
  busA[2:1] 表示选择其中的第2和第3根,分别为1,2
  busC=busA[2,1] 相当于 [1:0]连接到[1,2]     1与0相连,2与1相连
*/
busC = busA[2:1]
  • Vector assignment (by position!)
busB = busA
/*
1   最高位   3
2            2
3            1
4   最低位    0 
最高位连最高位
最低位连最低位
*/

3 Integer & Real Data Types

integer i,k;
real r;
  • Use as a register(in procedure)
i = 1; // assignments occur inside procedure
r = 2.9;
k = r; // k is rounded to 3
  • Integers are not initialized!--Integer是没有初始值的
  • Reals are initialized to 0,0!--Real初始值为0,0

4 Time Date Type

  • Special data type for simulation time measuring
  • Declaration
time my_time;
  • use inside procedure
//使用一个内置的系统函数
my_time=$time   // get current sim time 返回当前仿真的时间
  • Simulation runs at simulation time,not real time

5 Arrays

  • Syntax
//类型 数组名[start:end]
integer count[1:5];// 5 integers
reg var[-15:16]; // 32 1-bit regs
reg [7:0] mem[0:1023]; //1024 8-bit regs //memory  有1024个位置,每个位置放8位数据
  • Accessing array elements
  1. Entire element:
//mem[index] 索引
mem[10]= 8‘b10101010
  1. Element subfield(needs temp storage)
reg [7:0] temp;
..
temp = mem[10];
var[6] = temp[2]; //mem[10][2]
  • Limitation:Cannot access array subfield or entire array at once
var[2:9] = ???  //wrong  数组的2-9,不能进行切片赋值
  1. 数组是不允许进行切片赋值的
  2. vector是可以进行切片赋值的
  3. 没有多维数组,前面只是类似多维数组
reg ver[1:10] [1:100]; // wrong
  1. Arrays don't work for the Real data type
    real r[1:10]; //wrong 不能用于Real类型

6 String

  • Implemented with regs:
//一般8位存储一个字符
reg [8*13:1] string_val;// can hold up to 13 chars..
string_val = "Hello World";
string_val = "Hello";  //MS Bytes are filled with 0
string_val = "I am overflowed";  //超过存储空间会被截断,显示不全,存储的时候从低位开始存储,I显示不出来

  • Escaped chars:
  1. \n--换行
  2. \t--tab键
  3. \--\
  4. \'--\'
  5. %%--%

7 Logical Operators--逻辑运算符

  • &&--AND
  • ||--OR
  • !-NOF
  • Result is one bit value:0,1 or x
  A = 6; //非零数字表示真,1
  B = 0;
  C = x;

  A && B   //0
  A || B   //1
  C || B   //x

  C && B    //0

8 Bitwise Operators

按位操作

  • &--按位与
  • |--按位或
  • ~--按位取反
  • ^--按位异或
  • ~^ or ^~ --按位同或
a = 4'b1010
b = 4'b1100
c = a ^ b = 0110
d = ~a = 0101

9 Reduction Operator

递减操作符(Reduction Operators)为一目运算符,后面跟一变量,如“&C”表示将C的第一位与第二位相与,再将结果与第三位相与,再与第四位.......一直到最后一位。

递减操作运算符 含义
& AND
^ XOR
~& NAND
~
~^ or^~ XNOR
  • 单目运算符,满足多位输入,单位输出

对异或(^)运算的补充

异或运算符满足交换律与结合律

  • 一组二进制数据进行异或操作,有奇数个1时,结果为1,有偶数个1时,结果为0。
    一个无规律的二进制数据异或:
A = 1001010001110......0101001。
^A则可表示为: ^1001010001110......0101001 = 1^0^0^1^0......0^1^1^1^0
                                          =(1^1^1...^1^1)^(0^0^0...^0^0)    
                                          = (1^1^1...^1^1)^0
  • 当有奇数个1时,上式等价为1^0=1,当有偶数个1时,上式等价为0^0=0。

10 Shift Operator

  • --shift right

  • <<--shift left
    在移位过程中如果位数不够,用0进行填充
a = 4‘b1010
d = a >> 2    //d=0010
c = a << 1    //c=0100