Matrix manipulation

Consider 256x256 matrix, consider sliding 5x5 window. Corrupt atleast 1 element to value 'hFF or 'h00. 5x5window in non-overlapping. Original image can already have corrupted values, in that case do not corrupt. How do I implement this?


function void corrupt_matrix(ref bit[7:0] img[256][256]);
  
endfunction

function bit check_5_5_window();

   return has_corrupt_bit;
endfunction

In reply to UVM_SV_101:

I think it’s best to use four levels of for loops

for(int R=0;R<256;R+=5)
  for(int C=0;C<256;C+=5) begin
     corrupted = 0;
     for(int i=0;i<5;i++)
       for(int j=0;j<5;j++)
         // Short-circuit for performance
         corrupted = corrupted || img[R+i][C+j] inside {8'hFF, '8h00}
     if (corrupted) continue; // to next window
     else begin
       bit [4:0] elements[];
       void'(std::randomize(elements) with {
           elements.size inside {[1:5]}; // choose 1 to 5 elements to corrupt
           foreach(elements[i]) elements[i] <25; }); // position in 5x5 window
       foreach(elements[i]) img[R+(elements[i]/5)][C+elements[i]%5] = 0;
     end
  end

Since 256 does not divide evenly by 5, I’ll leave it up to you to figure out how to handle that.