整数常数的定义规则如下。
<null|+|→<size><sign:s|S><base: d|D|h|H|o|O|b|B><0~9|0~f|0~7|0~1|x|z>,其中size、sign和base是可选的。
//1. Unsized 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) //2. Sized 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 hign-impedance number //3. Using sign with 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 2'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 //4. Automatic left padding reg [11:0] a, b, c, d; initial begin a = 'h x; // yields xxx b = 'h 3x; // yields 03x c = 'h z3; // yields zz3 c = 'h 0z3; // yields 0z3 end reg [84:0] e, f, g; e = 'h5; // yields {82{1'b0},3'b101} f = 'hx; // yields {85{1'bx}} g = 'hz; // yields {85{1'bz}} //5. Use _ 27_195_000 16'b0011_0101_0001_1111 32 'h 12ab_f001 //6. sign-extend reg signed [15:0] h; reg [15:0] m; h = -12'h123; //16'FEDD h = 12'shEDD; //16'FEDD m = -12'h123; //16'FEDD m = 12'shEDD; //16'FEDD
我们要避免如下书写错误:
case (sel[1:0]) 00: y = a; 01: y = a; 10: y = a; //not execute 11: y = a; //not execute endcase