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或截斷的操作。

本博客所有文章均同步發表於www.mx1980.cn/blog

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章