user263210
user263210

Reputation: 447

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

I am getting an image matrix [converted from image to text file using Matlab] from a text file in an 1-D array. After applying linear Median filtering, I want to save the new array back to text file [and then back to an image using Matlab] to visualize the effects.

`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];

Now, after I get the transformed array, I want to store it back to the file. The two methods I know are $writememh and $fwrite. The problem with both is that these methods are used inside initial AND initial can not be put inside if-condition. [I need if-condition to store the array AFTER transformation is done]

This may roughly look like this:

        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

My question is:

  1. Is there any file-io method without 'initial'?

  2. If not, then how to rewrite the code to get same functionality?

Upvotes: 0

Views: 1498

Answers (1)

Emman
Emman

Reputation: 1234

File input output operation can be done without initial blocks based on some condition, I have shown one example were file write happens based on high and low conditions of reset.

   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

Output in output1.txt

F=1
F=1
F=1

Output in output2.txt

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

Upvotes: 1

Related Questions