如何在不使用' initial'的情况下将数组保存到文本文件? -Verilog

时间:2015-06-24 09:43:34

标签: file-io verilog simulation

我从一维数组中的文本文件中获取了一个图像矩阵[使用Matlab从图像转换为文本文件]。应用线性中值滤波后,我想将新数组保存回文本文件[然后使用Matlab返回到图像]以显示效果。

`define xlen 158
`define ylen 159
`define totLen `xlen * `ylen

module median1(
input clk
);

 reg [7:0] imagOrig[0:`totLen-1];
 reg [7:0] imagTrans[0:`totLen-1];
 reg [7:0] xIndex =1;
 reg [7:0] yIndex =0;

 int writeTrans;
 reg chk =0;

 initial    $readmemh("imagVecHex.txt", imagOrig);

 always @ (clk) begin
    if (yIndex <`ylen) begin
         //Median

        if (imagOrig[yIndex * `ylen + xIndex]    > imagOrig[yIndex * `ylen + xIndex -1] &&      //if B is median 
            imagOrig[yIndex * `ylen + xIndex]    < imagOrig[yIndex * `ylen + xIndex +1]   )
                imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex];

 else if (imagOrig[yIndex * `ylen + xIndex]    < imagOrig[yIndex * `ylen + xIndex -1] &&        //if B is median 
             imagOrig[yIndex * `ylen + xIndex]    > imagOrig[yIndex * `ylen + xIndex +1]   )
                imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex];

 else   if (imagOrig[yIndex * `ylen + xIndex -1] > imagOrig[yIndex * `ylen + xIndex] &&         //if A is median
            imagOrig[yIndex * `ylen + xIndex -1] < imagOrig[yIndex * `ylen + xIndex +1]   )
                imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex -1];

 else if (imagOrig[yIndex * `ylen + xIndex -1] < imagOrig[yIndex * `ylen + xIndex] &&           //if A is median
             imagOrig[yIndex * `ylen + xIndex -1] > imagOrig[yIndex * `ylen + xIndex +1]   )
                imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex -1];

 else   if (imagOrig[yIndex * `ylen + xIndex +1] > imagOrig[yIndex * `ylen + xIndex] &&         //if C is median
            imagOrig[yIndex * `ylen + xIndex +1] < imagOrig[yIndex * `ylen + xIndex -1]   )
                imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex +1];

 else if (imagOrig[yIndex * `ylen + xIndex +1] < imagOrig[yIndex * `ylen + xIndex] &&           //if C is median
             imagOrig[yIndex * `ylen + xIndex +1] > imagOrig[yIndex * `ylen + xIndex -1]   )
                imagTrans [yIndex * `ylen + xIndex] =imagOrig[yIndex * `ylen + xIndex +1];

//***if two or more are equall
 else if (imagOrig[yIndex * `ylen + xIndex]    == imagOrig[yIndex * `ylen + xIndex -1] ||       //if B == (A || C)
             imagOrig[yIndex * `ylen + xIndex]    == imagOrig[yIndex * `ylen + xIndex +1]    )
                imagTrans [yIndex * `ylen + xIndex]  =imagOrig[yIndex * `ylen + xIndex];

 else if (imagOrig[yIndex * `ylen + xIndex -1] == imagOrig[yIndex * `ylen + xIndex] ||          //if A == (B || C)
             imagOrig[yIndex * `ylen + xIndex -1] == imagOrig[yIndex * `ylen + xIndex +1]   )
                imagTrans [yIndex * `ylen + xIndex]  =imagOrig[yIndex * `ylen + xIndex -1];

 else if (imagOrig[yIndex * `ylen + xIndex +1] == imagOrig[yIndex * `ylen + xIndex] ||          //if C == (A || B)
             imagOrig[yIndex * `ylen + xIndex +1] == imagOrig[yIndex * `ylen + xIndex -1]   )
                imagTrans [yIndex * `ylen + xIndex]  =imagOrig[yIndex * `ylen + xIndex +1];

 else if (imagOrig[yIndex * `ylen + xIndex]    == imagOrig[yIndex * `ylen + xIndex -1] &&       //if A == B == C
             imagOrig[yIndex * `ylen + xIndex]    == imagOrig[yIndex * `ylen + xIndex +1]   )
                imagTrans [yIndex * `ylen + xIndex]  =imagOrig[yIndex * `ylen + xIndex];

现在,在我获得转换后的数组后,我想将它存储回文件中。 我所知道的两种方法是$writememh$fwrite。两者的问题是这些方法在initial中使用,并且初始值不能放在if-condition内。 [我需要if-condition来在完成转换后存储数组]

这可能大致如下:

        xIndex =xIndex +1;
        if (xIndex ==`xlen-1) begin
            xIndex =1;
            yIndex =yIndex +1;
            if (yIndex ==`ylen)    //When the last entry is processed the raise the flag 'chk'
                chk =1;
        end
    end
end

if (chk ==1) begin    //After 'chk' is raised write the array to memory and reset the flag
    writeTrans = $fopen("imagVecHexTrans.txt","w");
    $fwrite(writeTrans,"%h %h\n",imagTrans);
    $fclose(writeTrans);
    chk =0;
end

endmodule

我的问题是:

  1. 是否有任何file-io方法没有&#39; initial&#39;?

  2. 如果没有,那么如何重写代码以获得相同的功能?

1 个答案:

答案 0 :(得分:1)

文件输入输出操作可以在没有基于某些条件的初始块的情况下完成,我已经展示了一个例子是根据高低复位条件进行文件写入。

   module tb();
      reg   out,temp;
      reg   clk,reset;
      integer f,f1,i;

      always #5 clk=~clk;

      initial begin
        clk=0; reset=0;
        #50; reset=1;
        #50; reset=0;
        #50;
      end

      always @ * begin // level sensitive
      // always @ (posedge clk) begin // edge sensitive

      if ( reset == 1 )begin
        f = $fopen("output1.txt","w");
        for (i = 0; i<4; i=i+1) begin
          temp <= 1'b1;
          $display("OUT %b", temp);
          $fwrite(f,"%b\n", temp);
        end
        $fclose(f);  
      end

      else begin
        f1 = $fopen("output2.txt","w");
        for (i = 0; i<5; i=i+1) begin
          temp <= 1'b0;
          $display("OUT %b", temp);
          $fwrite(f1,"%b\n", temp);
        end
        $fclose(f1);  
      end
      end

    endmodule

输出在output1.txt

F=1
F=1
F=1

输出在output2.txt

F1=0
F1=0
F1=0
F1=0