首页 > 解决方案 > 在 Verilog 中实现内存、程序计数器和加法器。对我的输出感到困惑

问题描述

我要设计一个加法器、程序计数器、内存、时钟和测试台模块。

我想从测试台初始化每个内存地址为 0,然后将我拥有的与我的程序位于同一目录中的 .hex 文件加载到内存中。文本文件如下所示:

“……

20042000

000d2820

0c000013

…………”

我只是无法将 text.hex 文件加载到内存中。这是我的代码:

module testbench; //create the testbench 

    instruction_memory memory();


    integer i;


    initial begin
    for(i=0; i<2048; i = i+1) begin
    memory.mem[i] = 0;
                              end
    end

        initial begin
        $writememh("text.hex",memory.mem);
        end

    integer m;

        initial begin
        for(m=0; m<2048; m = m+1) begin
        $display(memory.mem[m]);
        $display(m);
                                  end
        end





endmodule //end the testbench

module instruction_memory(); // creating the module that 
                            //simulates the instruction //memory
    reg[31:0]mem[0:2047];


endmodule //ending the instruction memory module

module clock(); //starting the clock module

endmodule //ending the clock module

module pc(); //starting the program counter module

endmodule // ending the program counter module

module adder();

endmodule // ending the adder module

当我运行它时,我的输出如下所示:

0

1

0

2

0

3

...

...

0 2047

为什么它返回 0 而不是“000d2820”或其他单词?

所以我做了一件事。我用 0 初始化了内存,但我似乎无法将文件读入其中。我该怎么做呢?

编辑:这是我现在的代码:

module testbench; //create the testbench 

    instruction_memory memory();


    integer i;
        initial begin
        for(i=0; i<2048; i = i+1) begin
        memory.mem[i] = 0;
                                  end
        end


    initial begin
    #100;
    end


    integer n;

        initial begin
        for(n=0; n<2048; n = n+1) begin
        $display(memory.mem[n]);
        $display(n);
                                  end
        end


    initial begin
    #100;
    end


    initial begin
        $readmemh("C:\\Users\\19724\\Desktop\\spring2020\\DL\\programs\\program3\\text.hex",memory.mem);
    end 

    initial begin
    #100;
    end

    integer m;

        initial begin
        for(m=0; m<2048; m = m+1) begin
        $display(memory.mem[m]);
        $display(m);
                                  end
        end





endmodule //end the testbench

module instruction_memory(); // creating                                                            //the module that 
                            //simulates the instruction //memory

    reg[31:0]mem[0:2047];


endmodule //ending the instruction memory module

此代码重复与上面相同的输出^..

0

1

0

2

..

..

0

2047

除了现在两次,在我初始化为零之后,我有两个循环显示输出,并且在我“读取十六进制文件”之后,我把它放在引号中,因为由于某种原因,即使我写了绝对路径它也不起作用..有人知道为什么它不会读取我的十六进制文件吗?

标签: memoryverilog

解决方案


那么如何防止它们并行运行呢?
如何用零初始化指令存储器,然后从我的 text.hex 文件中初始化我有值的每个地址,而不是并行执行此操作?
你是什​​么意思我清除内存,你指的是当我用零初始化内存时?
至于“将文件读入其中”,我认为这就是我正在使用“writememh(“text.hex”,memory.mem)“

如果您使用单个首字母,事情会变得简单得多。我经常通过在它们之间放置延迟来分割部分:

  1. 让我更容易在我的仿真波形中看到发生了什么。

  2. 确保代码真正按顺序执行。

例子:

...
initial
begin
   for(i=0; i<2048; i = i+1) begin
      memory.mem[i] = 0;

   #1000;

   $readmemh("text.hex",memory.mem);

   #1000;
   for(m=0; m<2048; m = m+1)
     $display(memory.mem[m]);

  $display(m);
end
....

检查您的模拟日志以查看 $readmemh 是否给出错误消息,因为模拟器通常需要特殊路径来查找文件。


推荐阅读