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

How to save an array to a text file without using 'initial'? -Verilog

我正在从一维数组中的文本文件中获取图像矩阵[使用 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. 有没有没有'initial'的file-io方法?

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

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

   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