首页 > 解决方案 > Verilog 数组偏移

问题描述

module inst_mem(inst_out, pc_addr_out, clk, rstb);
output reg [31:0] inst_out;
input             clk, rstb;
input      [7:0]  pc_addr_out;
reg        [31:0] array[7:0];
//integer           n = 0;
initial begin 
    array[0] <= 32'b00001111000011110000111100001111;
    array[1] <= 32'b00011111000011110000111100001111; 
    array[2] <= 32'b00101111000011110000111100001111;
    array[3] <= 32'b00111111000011110000111100001111;
    array[4] <= 32'b01001111000011110000111100001111;
    array[5] <= 32'b01011111000011110000111100001111;
    array[6] <= 32'b01101111000011110000111100001111;
    array[7] <= 32'b01111111000011110000111100001111;
end 
always @(posedge clk or negedge rstb)
    if (!rstb)
        inst_out <= 32'd0;
    else begin
        inst_out <= array[pc_addr_out];
    //  inst_out <= array[n];
    end
endmodule

我为指令存储器编写代码,我发现当我访问具有这样的 reg 类型的数组时,第一个时钟被延迟

inst_out <= array[pc_addr_out];

但是当我使用整数类型'n'访问arry

inst_out <= array[n];

它工作正常。所以,我想访问整数类型的数组,但我不知道。我尝试像 C 语言一样进行转换,但它不起作用。有什么好方法可以将 reg 类型更改为 int 类型?

标签: verilog

解决方案


问题在于输入和输出的时间,而不是类型。您的内存是 ROM,它应该只是组合逻辑,不需要时钟或复位。

module inst_mem(
   output wire [31:0] inst_out,
   input  wire [7:0]  pc_addr_out
);

reg        [31:0] array[7:0];
initial begin 
    array[0] <= 32'b00001111000011110000111100001111;
    array[1] <= 32'b00011111000011110000111100001111; 
    array[2] <= 32'b00101111000011110000111100001111;
    array[3] <= 32'b00111111000011110000111100001111;
    array[4] <= 32'b01001111000011110000111100001111;
    array[5] <= 32'b01011111000011110000111100001111;
    array[6] <= 32'b01101111000011110000111100001111;
    array[7] <= 32'b01111111000011110000111100001111;
end 
assign inst_out <= array[pc_addr_out];
    end
endmodule

推荐阅读