亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Verilog

Verilog 标量和向量

Verilog 需要表示单个位以及位组。例如,单个位顺序元件是触发器。然而,16 位顺序元素是一个可以保存 16 位的寄存器。为此,Verilog 具有 scalar矢量 网络和变量。

标量和向量

网或reg 没有范围规范的声明被认为是 1 位宽并且是 标量 .如果指定了范围,则 net 或 reg 成为一个称为 vector 的多位实体 .

<无脚本> scalar and vector in verilog
  
  
	wire 	    o_nor;           // single bit scalar net
	wire [7:0]  o_flop;          // 8-bit vector net
	reg         parity;          // single bit scalar variable
	reg  [31:0] addr;            // 32 bit vector variable to store address

  

该范围提供了寻址向量中各个位的能力。向量的最高有效位应指定为范围内的左侧值,而向量的最低有效位应指定在右侧。

  
  
	wire  [msb:lsb]   name;
	integer           my_msb;
	
	wire [15:0]        priority;      // msb = 15, lsb = 0
	wire [my_msb: 2]   prior;         // illegal

  

在上面的示例中将创建一个 16 位宽的网络,称为优先级。请注意,msblsb 应该是一个常量表达式,不能被变量替换。但它们可以是任何整数值——正数、负数或零;和 lsb value 可以大于、等于或小于 msb 值。

位选择

向量变量中的任何位都可以单独选择并分配一个新值,如下所示。这称为位选择 .如果位选择超出范围或位选择为 xz ,则返回的值将是 x .

<无脚本> bit-select in verilog
  
  
	reg [7:0]      addr;         // 8-bit reg variable [7, 6, 5, 4, 3, 2, 1, 0]
	
	addr [0] = 1;                // assign 1 to bit 0 of addr
	addr [3] = 0;                // assign 0 to bit 3 of addr
	addr [8] = 1;                // illegal : bit8  does not exist in addr

  

部分选择

<无脚本> part-select in verilog

可以选择一系列连续位,称为 part-select .有两种类型的部分选择,一种是常量部分选择,另一种是索引部分选择。

  
  
	reg [31:0]    addr;
	
	addr [23:16] = 8'h23;         // bits 23 to 16 will be replaced by the new value 'h23 -> constant part-select

  

具有可变部分选择允许它在循环中有效地使用以选择向量的部分。虽然起始位可以变化,但宽度必须保持不变。

[<start_bit> +: <width>]     // part-select increments from start-bit
[<start_bit> -: <width>]     // part-select decrements from start-bit
  
  
module des;
  reg [31:0]  data;
  int         i;
  
  initial begin
    data = 32'hFACE_CAFE;
    for (i = 0; i < 4; i++) begin
      $display ("data[8*%0d +: 8] = 0x%0h", i, data[8*i +: 8]);
    end
    
    $display ("data[7:0]   = 0x%0h", data[7:0]);
    $display ("data[15:8]  = 0x%0h", data[15:8]);
    $display ("data[23:16] = 0x%0h", data[23:16]);
    $display ("data[31:24] = 0x%0h", data[31:24]);
  end
  
endmodule

  
模拟日志
ncsim> run
data[8*0 +: 8] = 0xfe              // ~ data [8*0+8 : 8*0]
data[8*1 +: 8] = 0xca              // ~ data [8*1+8 : 8*1]
data[8*2 +: 8] = 0xce              // ~ data [8*2+8 : 8*2]
data[8*3 +: 8] = 0xfa              // ~ data [8*3+8 : 8*3]

data[7:0]   = 0xfe
data[15:8]  = 0xca
data[23:16] = 0xce
data[31:24] = 0xfa
ncsim: *W,RNQUIE: Simulation is complete.

常见错误

  
  
module tb;
   reg [15:0]    data;
   
   initial begin
      $display ("data[0:9] = 0x%0h", data[0:9]);   // Error : Reversed part-select index expression ordering
   end
endmodule

  

Verilog

  1. Verilog 教程
  2. Verilog 连接
  3. Verilog 作业
  4. Verilog 阻塞和非阻塞
  5. Verilog 函数
  6. Verilog 任务
  7. Verilog 内部和内部分配延迟
  8. Verilog 时钟发生器
  9. Verilog 数学函数
  10. Verilog 时间格式
  11. Verilog 时间刻度范围
  12. Verilog 文件 IO 操作