【问题标题】:Verilog I/O reading a characterVerilog I/O 读取字符
【发布时间】:2012-05-16 12:50:56
【问题描述】:

每当我尝试使用 Verilog 的 I/O 进行任何操作时,我似乎都会遇到一些问题。 Modelsim 要么抛出某些函数不支持的函数,要么什么都不做。我只需要逐个字符地读取文件并通过端口发送每一位。谁能帮忙

module readFile(clk,reset,dEnable,dataOut,done);
parameter size = 4;  
  //to Comply with S-block rules which is a 4x4 array will multiply by
// size so row is the number of size bits wide
parameter bits = 8*size;

input clk,reset,dEnable;
output dataOut,done;

wire [1:0] dEnable;
reg dataOut,done;
reg [7:0] addr;

integer file;
reg [31:0] c;
reg eof;

always@(posedge clk)
begin
 if(file == 0 && dEnable == 2'b10)begin      
    file = $fopen("test.kyle");      
  end    
end

always@(posedge clk) begin
  if(addr>=32 || done==1'b1)begin
    c <= $fgetc(file);
   //  c <= $getc();
    eof <= $feof(file);
    addr <= 0;
  end
end  

always@(posedge clk)
begin
  if(dEnable == 2'b10)begin
    if($feof(file))
        done <= 1'b1;
      else
        addr <= addr+1;
  end
end
//done this way because blocking statements should not really be used
always@(addr)
begin:Access_Data
  if(reset == 1'b0) begin   
    dataOut <= 1'bx;
    file <= 0;
  end
  else if(addr<32)
    dataOut <= c[31-addr];
end 

 endmodule

【问题讨论】:

    标签: file-io io verilog modelsim


    【解决方案1】:

    我建议一次将整个文件读入一个数组,然后遍历该数组以输出值。

    这是一个关于如何将文件中的字节读入 SystemVerilog 队列的 sn-p。如果您需要坚持使用普通的旧 Verilog,您可以使用常规数组执行相同的操作。

    reg [8:0] c;
    byte      q[$];
    int       i;
    
    // Read file a char at a time
    file = $fopen("filename", "r");
    c = $fgetc(file);
    while (c != 'h1ff) begin
        q.push_back(c);
        $display("Got char [%0d] 0x%0h", i++, c);
        c = $fgetc(file);
    end
    

    请注意,c 定义为 9 位 reg。原因是$fgetc 在到达文件末尾时会返回-1。为了区分 EOF 和有效的 0xFF,您需要这个额外的位。

    我不熟悉$feof,并且在 Verilog 2001 规范中看不到它,所以这可能是 Modelsim 特有的。或者它可能是“不支持的功能”的来源。

    【讨论】:

      猜你喜欢
      • 2017-04-25
      • 2011-09-20
      • 2012-07-05
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 2014-05-25
      相关资源
      最近更新 更多