不同宽度的索引寄存器

时间:2018-06-24 20:57:26

标签: indexing verilog variable-assignment assign

有没有办法索引具有相同偏移量但可变宽度的多位寄存器?我要这样做:

module foo (input [1:0] data_bits);
always @ (posedge clk) begin
case (data_bits)
    2'b11:
        assign wire_data_out = {3(1'b0), data_received[7:3]};
    2'b10:
        assign wire_data_out = {2(1'b0), data_received[7:2]};
    2'b01:
        assign wire_data_out = {(1'b0), data_received[7:1]};
    2'b00:
        assign wire_data_out = data_received;
endcase
end

在搜索答案时,我找到了索引零件选择运算符“ +:或-:”,但是它需要固定的宽度,这不符合我的需求。有没有更简单的方法来编写上面的代码?

1 个答案:

答案 0 :(得分:2)

always @ (posedge clk)
   wire_data_out <= data_received>>data_bits;

顺便说一句

  1. 您不能在Always块中使用分配。
  2. 在计时部分,您应该使用非阻塞分配。

这是您要求的一行。但是我不会那样写代码。它要求读者知道Verilog将在顶部加零,而在底部加零。我会使用您编写的代码(但是没有语法错误:-):

always @ (posedge clk) begin
   case (data_bits)
   2'b00: wire_data_out <=       data_received;
   2'b01: wire_data_out <= {1'b0,data_received[7:1]};
   2'b10: wire_data_out <= {2'b0,data_received[7:2]};
   2'b11: wire_data_out <= {3'b0,data_received[7:3]};
   endcase

最终结果将是完全相同的逻辑,但可读性更好。