0

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?

4
  • initial and always can accept the same code, only one runs once at the start and the other runs on an edge trigger (@(posedge clk)) or level trigger (@*). ie anything in an initial can go inside an always. Commented Jun 24, 2015 at 10:55
  • you are right. But can initial or always be put inside if-condition? @Morgan Commented Jun 24, 2015 at 11:07
  • 1
    Do it the other way around. Verilog is trying to describe hardware. Placing a block inside an if you are saying sometimes the hardware exists and sometimes it does not. Parameters can be used to define constants which can control generate statements like this but you can not create and destroy hardware on the fly using a variable. Commented Jun 24, 2015 at 13:25
  • FPGAs do not have support for FILEIO, this is not an FPGA or Xilinx question, this is a simulation question. Commented Jun 25, 2015 at 7:10

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I was taking the same approach but in SAME always block and it did not work that way; Can you explain it?. Another difference that you did it index by index and I was storing the whole array at one time.
Note that initial and always block can not be mixed together, so use it separately and as per you example you have a mem of imagTrans(158*159)25122 array each of 8 bit value, if you did not intend to store it as a single value you may have to concatenate each value into one single array were the size matches the original

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.