System Verilog LRM 学习笔记 — 字面常量

1. 整数型字面常量

 1.1 整数型字面常量表示方法有两种:

  1. 数字直接表示:会被系统识别为32bit宽的有符号数。
  2. 指定size和进制,如12‘hDBA, 有可选的宽度,(’), s/S有符号数指示,和数字组成。 默认是无符号数,只有含s/s符号数指示时被识别为有符号数, 如12‘shDBA.

1.2 负数是二进制补码的形式表示

1.3 整数型字面常量的补齐和截断,padded to left, truncated:

对于无符号数,如果向size小的数赋值,则需要将左侧多余的bit截断后赋值。如果向size大的数赋值,则将其左侧填0后赋值,但如果其左侧是x/z,则将其左侧补x/z,之后再进行赋值操作。

1.4 所有bit设置成0/1/x/z的表示方法:

‘0, ‘1, ‘X, ‘x, ‘Z, ‘z // sets all bits to specified value

 

//Example 1: Unsized literal constant numbers
   659 // is a decimal number
    'h 837FF // is a hexadecimal number
    'o7460 // is an octal number
    4af // is illegal (hexadecimal format requires 'h)

//Example 2: Sized literal constant numbers
    4'b1001 // is a 4-bit binary number
    5 'D 3 // is a 5-bit decimal number
    3'b01x // is a 3-bit number with the least

    // significant bit unknown
    12'hx // is a 12-bit unknown number
    16'hz // is a 16-bit high-impedance number

//Example 3: Using sign with literal constant numbers
    8 'd -6 // this is illegal syntax
    -8 'd 6 // this defines the two's-complement of 6,
    // held in 8 bits—equivalent to -(8'd 6)
    4 'shf // this denotes the 4-bit number '1111', to
    // be interpreted as a two's-complement number,
    // or '-1'. This is equivalent to -4'h 1
    -4 'sd15 // this is equivalent to -(-4'd 1), or '0001'
    16'sd? // the same as 16'sbz

//Example 4: Automatic left padding of literal constant numbers
    logic [11:0] a, b, c, d;
    logic [84:0] e, f, g;
    initial begin
      a = 'h x; // yields xxx
      b = 'h 3x; // yields 03x
      c = 'h z3; // yields zz3
      d = 'h 0z3; // yields 0z3
      e = 'h5; // yields {82{1'b0},3'b101}
      f = 'hx; // yields {85{1'hx}}
      g = 'hz; // yields {85{1'hz}}
    end

//Example 5: Automatic left padding of constant literal numbers using a single-bit value
    logic [15:0] a, b, c, d;
    a = '0; // sets all 16 bits to 0
    b = '1; // sets all 16 bits to 1
    c = 'x; // sets all 16 bits to x
    d = 'z; // sets all 16 bits to z

//Example 6: Underscores in literal constant numbers
    27_195_000 // unsized decimal 27195000
    16'b0011_0101_0001_1111 // 16-bit binary number
    32 'h 12ab_f001 // 32-bit hexadecimal number

2. 实数型字面常量

在SV中,实数型字面常量代表的是双精度浮点数,有普通表示法(如12.73),和科学表示法(如32e6)两种形式。

3. 时间常量

用整数或小数+时间单位(fs,ps,ns,us,ms,s)来表示,如2.1ns,49ps;

4. 字符串常量

本质是byte类型的可变数组,用“”来表示一个字符串。可以将字符串赋给整数变量,如

byte c1 = "A" ;
bit [7:0] d = "\n" ;
bit [8*12:1] stringvar = "Hello world\n"; //store the 12-character string "Hello world\n" 
                                          //requires a variable 8 x 12, or 96 bits wide.
bit [0:11] [7:0] stringvar = "Hello world\n" ;  //Alternatively, a multidimensional packed 
                                                //array can be used, with 8-bit subfields

//A string literal can be assigned to an unpacked array of bytes. If the size differs, it is left justified.
byte c3 [0:12] = "hello world\n" ;

左侧变量应该和字符串长度一致,是8bit的整数倍,否则需要进行补0或截断的操作。

阅读:129

发表评论

电子邮件地址不会被公开。 必填项已用*标注