1

每当我尝试使用 I/O 进行 verilog 的任何操作时,我似乎都会遇到一些问题。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
4

1 回答 1

4

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

下面是如何从文件中读取字节到 SystemVerilog 队列的片段。如果您需要坚持使用普通的旧 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,您需要这个额外的位。

I'm not familiar with $feof and don't see it in the Verilog 2001 spec, so that may be something specific to Modelsim. Or it could be the source of the "function not supported."

于 2012-05-16T18:31:33.527 回答