Verilog Icarus给出了未定义的值

时间:2018-04-19 19:44:26

标签: verilog risc icarus

我正在开展一个项目,我在Icarus Verilog 0.9.7中设计一个简单的RISC机器。

这段代码是模块化的,我最后会把几个组件放在一起,但是在处理内存控制器时我遇到了一个问题。对于我的所有测试值,我得到未定义的输出,或者' Z'。我怎样才能解决这个问题?我没有看到为什么没有填充这些值的原因。

定义内存控制器应具有的所有行为将超出此问题的上下文,因此我将尝试简要介绍Verilog的主题。

这是我目前的Verilog代码。工作台有两个文件,mem.sv和SrcMemoryController.sv:

    `default_nettype none

module SrcMemoryController(
    inout [31:0] cpu_bus,
    inout [31:0] mem_bus,
    input ma_in,
    input md_in, 
    input md_out, 
    input read,
    input enable,
    output [15:0] address
);

  reg [31:0] ma = 32'bz;
  reg [31:0] md = 32'bz;
  wire [31:0] cpb = cpu_bus;
  wire [31:0] meb = mem_bus;

  always @(*) begin

    if(ma_in) ma = cpb;

    if(md_in) md = cpb;

    if(read) begin
      if(enable) md = meb;
    end

  end

  assign cpu_bus = (md_out) ? md : 32'bz;
  assign mem_bus = (~read && enable) ? md : 32'bz;
  assign address = ma;

endmodule

mem.sv目前有效并且不是此问题的根源。无论如何,我会把它包括在里面。

`default_nettype none

module Memory (
  inout [31:0] mem_bus,
  input [15:0] address,
  input read, 
  input enable
);

  reg [31:0] storage [65535:0];

  always @(enable) begin
    if(enable) begin
      if(read) begin

      end
      if(!read) begin
        storage[address] = mem_bus;
      end
    end
  end

  assign mem_bus = (read) ? storage[address] : 32'bz;

endmodule

这里也是测试平台。它可能不需要,但发布它可能有所帮助。

    `default_nettype none
`include "mem.sv"

module tb_MemoryController;

  wire [31:0] mem_bus;
  wire [31:0] cpu_bus;
  reg [31:0] bus_sim;
  reg bus_sim_out = 0;

  // Tri-state buffer
  assign cpu_bus = bus_sim_out ? bus_sim : 32'bz; 

  reg ma_in, md_in, md_out;
  reg read, enable;
  wire [15:0] address;

  Memory vmem( mem_bus, address, read, enable );
  SrcMemoryController dut( cpu_bus, mem_bus, ma_in, md_in, md_out,
                       read, enable, address );

  initial begin
    $dumpfile("mem_file.vcd");
    $dumpvars(0, dut);
    $dumpvars(0, vmem);

    test_memory(0, 0);
    test_memory(10, 0);
    #1 assert_empty(mem_bus);
    #1 assert_empty(cpu_bus);
    test_memory(65535, 65535);
    test_memory(1231, 123);
    test_memory(1231231, 65535);
    test_memory(0, 65535);
    test_memory(100, 100);
    test_memory(515, 515);
    #1 assert_empty(mem_bus);
    #1 assert_empty(cpu_bus);
    $finish();
  end

  task test_memory;
    input [31:0] data;
    input [15:0] addr;
    begin
      #1
      zero_inputs();
      #1
      write_value(data, addr);
      #1
      load_spoof(); 
      #1
      read_value(addr);
      #1
      assertEquals(cpu_bus, data);
      zero_inputs();
    end
  endtask

  task write_value;
    input [31:0] data;
    input [15:0] addr;
    begin
      #1
      enable <= 0;
      bus_sim <= addr;
      bus_sim_out <= 1;
      ma_in <= 1;
      #1
      bus_sim <= data;
      bus_sim_out <= 1;
      ma_in <= 0;
      md_in <= 1;
      read <= 0;
      enable <= 1; 
    end
  endtask

  task read_value;
    input [15:0] addr;
    begin
      #1 
      enable <= 0;
      bus_sim <= addr;
      bus_sim_out <= 1;
      ma_in <= 1;
      #1
      ma_in <= 0;
      read <= 1;
      enable <= 1;
      bus_sim_out <= 0;
      md_out <= 1;
    end
  endtask

  task load_spoof;
    begin
      #1
      bus_sim <= 32'hABCDABCD;
      bus_sim_out <= 1;
      ma_in <= 1;
      md_in <= 1;
      read <= 0;
      enable <= 0; 
      #1
      zero_inputs();
    end
  endtask

  task zero_inputs;
    begin
      ma_in <= 0;
      md_in <= 0;
      md_out <= 0;
      read <= 0;
      enable <= 0;
      bus_sim_out <= 0;
    end
  endtask

  task pulseClock;
    begin
    end
  endtask

  task assertEquals;
    input [31:0] val;
    input [31:0] exp;
    begin
      if (val == exp) $display("[TEST][PASSED] %d", val);
      else $display("[TEST][FAILED] Got %d, expected %d", val, exp);
    end
  endtask


  task assert_empty;
    input [31:0] a;
    begin
      if (a === 32'bz) $display("[TEST][PASSED] (Bus empty)", a);
      else $display("[TEST][FAILED] (Value on bus)", a, 32'bz);
    end
  endtask

endmodule

感谢所有帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

Kevin的评论中给出的答案,我将详细说明它是如何修复的。

我将mem.sv中的always @(enable)更改为始终@(*),在此更改后,其余代码立即开始工作并具有完整功能。非常感谢Kevin1494!