data/full_repos/permissive/161706166/src/pdp1_vga_crt.v
 line:41: before: "["
1: b"%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:126: Define or directive not defined: '`v_visible_offset'\nassign current_y = (vertical_counter >= `v_visible_offset && vertical_counter < `v_visible_offset_end) ? vertical_counter - `v_visible_offset : 11'b0;\n                                        ^~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:126: syntax error, unexpected &&, expecting TYPE-IDENTIFIER\nassign current_y = (vertical_counter >= `v_visible_offset && vertical_counter < `v_visible_offset_end) ? vertical_counter - `v_visible_offset : 11'b0;\n                                                          ^~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:126: Define or directive not defined: '`v_visible_offset_end'\nassign current_y = (vertical_counter >= `v_visible_offset && vertical_counter < `v_visible_offset_end) ? vertical_counter - `v_visible_offset : 11'b0;\n                                                                                ^~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:126: Define or directive not defined: '`v_visible_offset'\nassign current_y = (vertical_counter >= `v_visible_offset && vertical_counter < `v_visible_offset_end) ? vertical_counter - `v_visible_offset : 11'b0;\n                                                                                                                            ^~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:127: Define or directive not defined: '`h_visible_offset'\nassign current_x = (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset) ? horizontal_counter - (`h_visible_offset + `h_center_offset): 11'b0;\n                                          ^~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:127: Define or directive not defined: '`h_center_offset'\nassign current_x = (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset) ? horizontal_counter - (`h_visible_offset + `h_center_offset): 11'b0;\n                                                              ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:127: syntax error, unexpected &&, expecting TYPE-IDENTIFIER\nassign current_x = (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset) ? horizontal_counter - (`h_visible_offset + `h_center_offset): 11'b0;\n                                                                               ^~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:127: Define or directive not defined: '`h_visible_offset_end'\nassign current_x = (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset) ? horizontal_counter - (`h_visible_offset + `h_center_offset): 11'b0;\n                                                                                                       ^~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:127: Define or directive not defined: '`h_center_offset'\nassign current_x = (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset) ? horizontal_counter - (`h_visible_offset + `h_center_offset): 11'b0;\n                                                                                                                               ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:127: Define or directive not defined: '`h_visible_offset'\nassign current_x = (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset) ? horizontal_counter - (`h_visible_offset + `h_center_offset): 11'b0;\n                                                                                                                                                                         ^~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:127: Define or directive not defined: '`h_center_offset'\nassign current_x = (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset) ? horizontal_counter - (`h_visible_offset + `h_center_offset): 11'b0;\n                                                                                                                                                                                             ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:318: Define or directive not defined: '`h_line_timing'\n   if (horizontal_counter == `h_line_timing)\n                             ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:318: syntax error, unexpected ')', expecting TYPE-IDENTIFIER\n   if (horizontal_counter == `h_line_timing)\n                                           ^\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:325: Define or directive not defined: '`h_visible_offset'\n   inside_visible_area <= (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset);\n                                                 ^~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:325: Define or directive not defined: '`h_center_offset'\n   inside_visible_area <= (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset);\n                                                                     ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:325: syntax error, unexpected &&, expecting TYPE-IDENTIFIER\n   inside_visible_area <= (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset);\n                                                                                      ^~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:325: Define or directive not defined: '`h_visible_offset_end'\n   inside_visible_area <= (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset);\n                                                                                                              ^~~~~~~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:325: Define or directive not defined: '`h_center_offset'\n   inside_visible_area <= (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset);\n                                                                                                                                      ^~~~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:327: Define or directive not defined: '`h_line_timing'\n   if (horizontal_counter == `h_line_timing)    \n                             ^~~~~~~~~~~~~~\n%Error: data/full_repos/permissive/161706166/src/pdp1_vga_crt.v:327: syntax error, unexpected ')', expecting TYPE-IDENTIFIER\n   if (horizontal_counter == `h_line_timing)    \n                                           ^\n%Error: Exiting due to 20 error(s)\n"
module pdp1_vga_crt (
  input clk,

  input [10:0] horizontal_counter,
  input [10:0] vertical_counter,                               
                        
  output reg [7:0] red_out,
  output reg [7:0] green_out,
  output reg [7:0] blue_out,               
  
  input  [9:0] pixel_x_i,
  input  [9:0] pixel_y_i,
  input  [2:0] pixel_brightness,
  
  input  variable_brightness,
        
  input  pixel_available
);
             

                     
parameter
   DATA_WIDTH                    = 'd32,
   offset                        = 2'd3,
   brightness                    = 8'd242;    


          
function automatic [11:0] dim_pixel;
   input [11:0] luma;
	dim_pixel = (luma > 12'd3864 && luma < 12'd3936) ? 12'd2576 : luma - 1'b1;
endfunction                   




task output_pixel;
   input [7:0] intensity;
   begin
 
   red_out <= inside_visible_area ? {5'b0, intensity[7:5]} : 8'b0;
   
      if (intensity >= 8'h80) begin      
         green_out <= inside_visible_area ? intensity      : 8'b0;
         blue_out <= inside_visible_area ? intensity       : 8'b0;
         red_out <= inside_visible_area ? intensity[7:6]   : 8'b0;               
      end
      else begin
         green_out <= inside_visible_area ? intensity      : 8'b0;
         blue_out <= inside_visible_area ? intensity[7]    : 8'b0;
      end                           

   end
endtask




wire [7:0] rowbuff_rdata;
wire [7:0] p31_w, p21_w, p13_w, p23_w, p33_w;
wire [255:0] taps1, taps2, taps3, taps4;

wire [31:0] shiftout_1_w, shiftout_2_w,
            shiftout_3_w, shiftout_4_w;   

wire [9:0] current_y, current_x;



reg  [12:0] rowbuff_rdaddress, rowbuff_wraddress;
reg  [7:0] rowbuff_wdata;
reg  [0:0] rowbuff_wren;


reg [7:0] p11, p12, p13,
          p21, p22, p23,
          p31, p32, p33;


reg [31:0] shiftout_1, shiftout_2, shiftout_3, shiftout_4;

reg [9:0] pixel_x, pixel_y;    
reg [9:0] pixel_1_x, pixel_1_y, pixel_2_x, pixel_2_y, 
          pixel_3_x, pixel_3_y, pixel_4_x, pixel_4_y;

reg [11:0] luma, luma_1, luma_2, luma_3, luma_4;

integer i;
         
reg [31:0] pass_counter = 32'd1;
reg [9:0]  erase_counter;
                    
reg [31:0] search_counter;

reg [9:0] next_pixel_x, next_pixel_y;

reg [9:0] buffer_pixel_x[63:0];
reg [9:0] buffer_pixel_y[63:0];
  
reg [5:0] buffer_read_ptr, buffer_write_ptr;
reg [15:0] pixel_out;

reg prev_wren_i, prev_prev_wren_i, wren;
      
reg inside_visible_area;




assign p21_w = p21;
assign p31_w = p31;

assign current_y = (vertical_counter >= `v_visible_offset && vertical_counter < `v_visible_offset_end) ? vertical_counter - `v_visible_offset : 11'b0;
assign current_x = (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset) ? horizontal_counter - (`h_visible_offset + `h_center_offset): 11'b0;
       
         

         

pdp1_vga_rowbuffer rowbuffer(
   .data(rowbuff_wdata),
   .rdaddress(rowbuff_rdaddress),
   .clock(clk),
   .wraddress(rowbuff_wraddress),
   .wren(rowbuff_wren),
   .q(rowbuff_rdata));
                   
                     

line_shift_register line1(.clock(clk), .shiftout(p13_w), .shiftin(p21_w));    
line_shift_register line2(.clock(clk), .shiftout(p23_w), .shiftin(p31_w));    
line_shift_register line3(.clock(clk), .shiftout(p33_w), .shiftin(current_x > 0 ? rowbuff_rdata : 0));                        





pixel_ring_buffer ring_buffer_1(.clock(clk),  .shiftin(shiftout_1),  .shiftout(shiftout_1_w),  .taps(taps1) );
pixel_ring_buffer ring_buffer_2(.clock(clk),  .shiftin(shiftout_2),  .shiftout(shiftout_2_w),  .taps(taps2) );
pixel_ring_buffer ring_buffer_3(.clock(clk),  .shiftin(shiftout_3),  .shiftout(shiftout_3_w),  .taps(taps3) );
pixel_ring_buffer ring_buffer_4(.clock(clk),  .shiftin(shiftout_4),  .shiftout(shiftout_4_w),  .taps(taps4) );

       

                   
always @(posedge clk) begin                               
     next_pixel_x <= buffer_pixel_x[buffer_read_ptr];
     next_pixel_y <= buffer_pixel_y[buffer_read_ptr];
    
     search_counter <= search_counter + 1'b1;
                                   
     { pixel_1_y, pixel_1_x, luma_1 } <= shiftout_1_w;
     { pixel_2_y, pixel_2_x, luma_2 } <= shiftout_2_w;
     { pixel_3_y, pixel_3_x, luma_3 } <= shiftout_3_w;
     { pixel_4_y, pixel_4_x, luma_4 } <= shiftout_4_w;
     
     if(wren) begin                                

          if (variable_brightness && pixel_brightness > 3'b0 && pixel_brightness < 3'b100)
          begin
            { buffer_pixel_y[buffer_write_ptr], buffer_pixel_x[buffer_write_ptr] } <= { ~pixel_x_i, pixel_y_i };
            
            { buffer_pixel_y[buffer_write_ptr + 3'd1], buffer_pixel_x[buffer_write_ptr + 3'd1] } <= { ~pixel_x_i + 1'b1, pixel_y_i };
            { buffer_pixel_y[buffer_write_ptr + 3'd2], buffer_pixel_x[buffer_write_ptr + 3'd2] } <= { ~pixel_x_i, pixel_y_i + 1'b1};
            { buffer_pixel_y[buffer_write_ptr + 3'd3], buffer_pixel_x[buffer_write_ptr + 3'd3] } <= { ~pixel_x_i - 1'b1, pixel_y_i };
            { buffer_pixel_y[buffer_write_ptr + 3'd4], buffer_pixel_x[buffer_write_ptr + 3'd4] } <= { ~pixel_x_i, pixel_y_i - 1'b1};

            buffer_write_ptr <= buffer_write_ptr + 3'd5;                                                                                                                          
          end
          
          else 
          
          begin
            { buffer_pixel_y[buffer_write_ptr], buffer_pixel_x[buffer_write_ptr] } <= { ~pixel_x_i, pixel_y_i };
            buffer_write_ptr <= buffer_write_ptr + 1'b1;                                                 
          end
          
          if (buffer_write_ptr == buffer_read_ptr)
               search_counter <= 0;                                            
     end                           
     
     begin                                       

     shiftout_1 <= luma_4[11:4] ? { pixel_4_y, pixel_4_x, pass_counter[2:0] == 3'b0 ? dim_pixel(luma_4) : luma_4 } : 0;
     shiftout_2 <= luma_1[11:4] ? { pixel_1_y, pixel_1_x, pass_counter[2:0] == 3'b0 ? dim_pixel(luma_1) : luma_1 } : 0;
     shiftout_3 <= luma_2[11:4] ? { pixel_2_y, pixel_2_x, pass_counter[2:0] == 3'b0 ? dim_pixel(luma_2) : luma_2 }: 0;
     shiftout_4 <= luma_3[11:4] ? { pixel_3_y, pixel_3_x, pass_counter[2:0] == 3'b0 ? dim_pixel(luma_3) : luma_3 }: 0;                                
     

     

     
     if (buffer_write_ptr != buffer_read_ptr && search_counter > 1024 && (!luma_1[11:4] || !luma_2[11:4] || !luma_3[11:4] || !luma_4[11:4]))
     begin
         if (luma_4[11:4] == 0)  
               shiftout_1 <= { { next_pixel_y, next_pixel_x, 12'd4095 } };
         else if (luma_1[11:4] == 0)  
               shiftout_2 <= { { next_pixel_y, next_pixel_x, 12'd4095 } };
         else if (luma_2[11:4] == 0)  
               shiftout_3 <= { { next_pixel_y, next_pixel_x, 12'd4095 } };
         else if (luma_3[11:4] == 0)  
               shiftout_4 <= { { next_pixel_y, next_pixel_x, 12'd4095 } };
      
         buffer_read_ptr <= buffer_read_ptr + 1'b1;

         next_pixel_x <= buffer_pixel_x[buffer_read_ptr + 1'b1];
         next_pixel_y <= buffer_pixel_y[buffer_read_ptr + 1'b1];

         search_counter <= 0;                                                                   
      end
     

   
   else if (buffer_write_ptr != buffer_read_ptr && 
      (  (pixel_1_x == next_pixel_x && pixel_1_y == next_pixel_y)
      || (pixel_2_x == next_pixel_x && pixel_2_y == next_pixel_y)
      || (pixel_3_x == next_pixel_x && pixel_3_y == next_pixel_y)
      || (pixel_4_x == next_pixel_x && pixel_4_y == next_pixel_y)
      ))
   begin
      if (pixel_1_x == next_pixel_x && pixel_1_y == next_pixel_y)
         shiftout_2 <= { next_pixel_y, next_pixel_x, 12'd4095};      
         
      else if (pixel_2_x == next_pixel_x && pixel_2_y == next_pixel_y)
         shiftout_3 <= { next_pixel_y, next_pixel_x, 12'd4095};      
         
      else if (pixel_3_x == next_pixel_x && pixel_3_y == next_pixel_y)
         shiftout_4 <= { next_pixel_y, next_pixel_x, 12'd4095};      

      else if (pixel_4_x == next_pixel_x && pixel_4_y == next_pixel_y)
         shiftout_1 <= { next_pixel_y, next_pixel_x, 12'd4095};      
         

      buffer_read_ptr <= buffer_read_ptr + 1'b1;               
      
      next_pixel_x <= buffer_pixel_x[buffer_read_ptr + 1'b1];
      next_pixel_y <= buffer_pixel_y[buffer_read_ptr + 1'b1];

      search_counter <= 0;
     
   end
     

     else 
         for (i=8; i>0; i=i-1'b1)               
            if ((taps1[i * DATA_WIDTH-1 -: 10] == next_pixel_y && taps1[i * DATA_WIDTH-11 -: 10] == next_pixel_x && taps1[i * DATA_WIDTH-21 -: 8])
              ||(taps2[i * DATA_WIDTH-1 -: 10] == next_pixel_y && taps2[i * DATA_WIDTH-11 -: 10] == next_pixel_x && taps2[i * DATA_WIDTH-21 -: 8])
              ||(taps3[i * DATA_WIDTH-1 -: 10] == next_pixel_y && taps3[i * DATA_WIDTH-11 -: 10] == next_pixel_x && taps3[i * DATA_WIDTH-21 -: 8])
              ||(taps4[i * DATA_WIDTH-1 -: 10] == next_pixel_y && taps4[i * DATA_WIDTH-11 -: 10] == next_pixel_x && taps4[i * DATA_WIDTH-21 -: 8])
               )
               
               search_counter <= 0;                                                                                                
     end
end            

always @(posedge clk) begin                                

                                             
   rowbuff_rdaddress <= {current_y[2:0], current_x};
   rowbuff_wren <= 1'b1;
               

   p11 <= p12; p12 <= p13; p13 <= p13_w;
   p21 <= p22; p22 <= p23; p23 <= p23_w;
   p31 <= p32; p32 <= p33; p33 <= p33_w;                       


   if ( p22 < brightness) 
	begin   		
		pixel_out <= ( {8'b0, p11[7:1]} + p12 + p13 + p21 + p22 + p23 + p31 + p32 + p33[7:1] ) >> 3;
		p21 <= pixel_out;		
	end
   else                       
      pixel_out <= p22;
   
   output_pixel(pixel_out);

   if (erase_counter < current_x)
      begin                         
         rowbuff_wraddress <= {current_y[2:0], erase_counter}; 
         rowbuff_wdata <= 0;
         erase_counter <= erase_counter + 1'b1;                               
      end         
   
   else

   for (i=8; i>0; i=i-1'b1)               
      if (current_y < taps1[i * DATA_WIDTH-1 -: 10] && taps1[i * DATA_WIDTH-1 -: 10] - current_y <= 3'd7 && taps1[i * DATA_WIDTH - 21 -: 8] > 0) 
         begin  rowbuff_wraddress <= {taps1[i * DATA_WIDTH - 8 -: 3], taps1[i * DATA_WIDTH - 11 -: 10]}; rowbuff_wdata <= taps1[i * DATA_WIDTH - 21 -: 8]; end                        
      else
      if (current_y < taps2[i * DATA_WIDTH-1 -: 10] && taps2[i * DATA_WIDTH-1 -: 10] - current_y <= 3'd7 && taps2[i * DATA_WIDTH - 21 -: 8] > 0) 
         begin  rowbuff_wraddress <= {taps2[i * DATA_WIDTH - 8 -: 3], taps2[i * DATA_WIDTH - 11 -: 10]}; rowbuff_wdata <= taps2[i * DATA_WIDTH - 21 -: 8]; end                        
      else
      if (current_y < taps3[i * DATA_WIDTH-1 -: 10] && taps3[i * DATA_WIDTH-1 -: 10] - current_y <= 3'd7 && taps3[i * DATA_WIDTH - 21 -: 8] > 0) 
         begin  rowbuff_wraddress <= {taps3[i * DATA_WIDTH - 8 -: 3], taps3[i * DATA_WIDTH - 11 -: 10]}; rowbuff_wdata <= taps3[i * DATA_WIDTH - 21 -: 8]; end                                           
      else
      if (current_y < taps4[i * DATA_WIDTH-1 -: 10] && taps4[i * DATA_WIDTH-1 -: 10] - current_y <= 3'd7 && taps4[i * DATA_WIDTH - 21 -: 8] > 0) 
         begin  rowbuff_wraddress <= {taps4[i * DATA_WIDTH - 8 -: 3], taps4[i * DATA_WIDTH - 11 -: 10]}; rowbuff_wdata <= taps4[i * DATA_WIDTH - 21 -: 8]; end                                           
      else            
   

   if (horizontal_counter == `h_line_timing)
      erase_counter <= 0;

end
         

always @(posedge clk) begin         
   inside_visible_area <= (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset);
      
   if (horizontal_counter == `h_line_timing)    
      pass_counter <= pass_counter + 1'b1;

   prev_prev_wren_i <= prev_wren_i;
   prev_wren_i <= pixel_available;
   wren <= prev_prev_wren_i & ~prev_wren_i;
      
end     

  
endmodule
module pdp1_vga_crt (
  input clk,

  input [10:0] horizontal_counter,
  input [10:0] vertical_counter,                               
                        
  output reg [7:0] red_out,
  output reg [7:0] green_out,
  output reg [7:0] blue_out,               
  
  input  [9:0] pixel_x_i,
  input  [9:0] pixel_y_i,
  input  [2:0] pixel_brightness,
  
  input  variable_brightness,
        
  input  pixel_available
);

             

                     
parameter
   DATA_WIDTH                    = 'd32,
   offset                        = 2'd3,
   brightness                    = 8'd242;    


          
function automatic [11:0] dim_pixel;
   input [11:0] luma;
	dim_pixel = (luma > 12'd3864 && luma < 12'd3936) ? 12'd2576 : luma - 1'b1;
endfunction                   




task output_pixel;
   input [7:0] intensity;
   begin
 
   red_out <= inside_visible_area ? {5'b0, intensity[7:5]} : 8'b0;
   
      if (intensity >= 8'h80) begin      
         green_out <= inside_visible_area ? intensity      : 8'b0;
         blue_out <= inside_visible_area ? intensity       : 8'b0;
         red_out <= inside_visible_area ? intensity[7:6]   : 8'b0;               
      end
      else begin
         green_out <= inside_visible_area ? intensity      : 8'b0;
         blue_out <= inside_visible_area ? intensity[7]    : 8'b0;
      end                           

   end
endtask




wire [7:0] rowbuff_rdata;
wire [7:0] p31_w, p21_w, p13_w, p23_w, p33_w;
wire [255:0] taps1, taps2, taps3, taps4;

wire [31:0] shiftout_1_w, shiftout_2_w,
            shiftout_3_w, shiftout_4_w;   

wire [9:0] current_y, current_x;



reg  [12:0] rowbuff_rdaddress, rowbuff_wraddress;
reg  [7:0] rowbuff_wdata;
reg  [0:0] rowbuff_wren;


reg [7:0] p11, p12, p13,
          p21, p22, p23,
          p31, p32, p33;


reg [31:0] shiftout_1, shiftout_2, shiftout_3, shiftout_4;

reg [9:0] pixel_x, pixel_y;    
reg [9:0] pixel_1_x, pixel_1_y, pixel_2_x, pixel_2_y, 
          pixel_3_x, pixel_3_y, pixel_4_x, pixel_4_y;

reg [11:0] luma, luma_1, luma_2, luma_3, luma_4;

integer i;
         
reg [31:0] pass_counter = 32'd1;
reg [9:0]  erase_counter;
                    
reg [31:0] search_counter;

reg [9:0] next_pixel_x, next_pixel_y;

reg [9:0] buffer_pixel_x[63:0];
reg [9:0] buffer_pixel_y[63:0];
  
reg [5:0] buffer_read_ptr, buffer_write_ptr;
reg [15:0] pixel_out;

reg prev_wren_i, prev_prev_wren_i, wren;
      
reg inside_visible_area;




assign p21_w = p21;
assign p31_w = p31;

assign current_y = (vertical_counter >= `v_visible_offset && vertical_counter < `v_visible_offset_end) ? vertical_counter - `v_visible_offset : 11'b0;
assign current_x = (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset) ? horizontal_counter - (`h_visible_offset + `h_center_offset): 11'b0;
       
         

         

pdp1_vga_rowbuffer rowbuffer(
   .data(rowbuff_wdata),
   .rdaddress(rowbuff_rdaddress),
   .clock(clk),
   .wraddress(rowbuff_wraddress),
   .wren(rowbuff_wren),
   .q(rowbuff_rdata));
                   
                     

line_shift_register line1(.clock(clk), .shiftout(p13_w), .shiftin(p21_w));    
line_shift_register line2(.clock(clk), .shiftout(p23_w), .shiftin(p31_w));    
line_shift_register line3(.clock(clk), .shiftout(p33_w), .shiftin(current_x > 0 ? rowbuff_rdata : 0));                        





pixel_ring_buffer ring_buffer_1(.clock(clk),  .shiftin(shiftout_1),  .shiftout(shiftout_1_w),  .taps(taps1) );
pixel_ring_buffer ring_buffer_2(.clock(clk),  .shiftin(shiftout_2),  .shiftout(shiftout_2_w),  .taps(taps2) );
pixel_ring_buffer ring_buffer_3(.clock(clk),  .shiftin(shiftout_3),  .shiftout(shiftout_3_w),  .taps(taps3) );
pixel_ring_buffer ring_buffer_4(.clock(clk),  .shiftin(shiftout_4),  .shiftout(shiftout_4_w),  .taps(taps4) );

       

                   
always @(posedge clk) begin                               
     next_pixel_x <= buffer_pixel_x[buffer_read_ptr];
     next_pixel_y <= buffer_pixel_y[buffer_read_ptr];
    
     search_counter <= search_counter + 1'b1;
                                   
     { pixel_1_y, pixel_1_x, luma_1 } <= shiftout_1_w;
     { pixel_2_y, pixel_2_x, luma_2 } <= shiftout_2_w;
     { pixel_3_y, pixel_3_x, luma_3 } <= shiftout_3_w;
     { pixel_4_y, pixel_4_x, luma_4 } <= shiftout_4_w;
     
     if(wren) begin                                

          if (variable_brightness && pixel_brightness > 3'b0 && pixel_brightness < 3'b100)
          begin
            { buffer_pixel_y[buffer_write_ptr], buffer_pixel_x[buffer_write_ptr] } <= { ~pixel_x_i, pixel_y_i };
            
            { buffer_pixel_y[buffer_write_ptr + 3'd1], buffer_pixel_x[buffer_write_ptr + 3'd1] } <= { ~pixel_x_i + 1'b1, pixel_y_i };
            { buffer_pixel_y[buffer_write_ptr + 3'd2], buffer_pixel_x[buffer_write_ptr + 3'd2] } <= { ~pixel_x_i, pixel_y_i + 1'b1};
            { buffer_pixel_y[buffer_write_ptr + 3'd3], buffer_pixel_x[buffer_write_ptr + 3'd3] } <= { ~pixel_x_i - 1'b1, pixel_y_i };
            { buffer_pixel_y[buffer_write_ptr + 3'd4], buffer_pixel_x[buffer_write_ptr + 3'd4] } <= { ~pixel_x_i, pixel_y_i - 1'b1};

            buffer_write_ptr <= buffer_write_ptr + 3'd5;                                                                                                                          
          end
          
          else 
          
          begin
            { buffer_pixel_y[buffer_write_ptr], buffer_pixel_x[buffer_write_ptr] } <= { ~pixel_x_i, pixel_y_i };
            buffer_write_ptr <= buffer_write_ptr + 1'b1;                                                 
          end
          
          if (buffer_write_ptr == buffer_read_ptr)
               search_counter <= 0;                                            
     end                           
     
     begin                                       

     shiftout_1 <= luma_4[11:4] ? { pixel_4_y, pixel_4_x, pass_counter[2:0] == 3'b0 ? dim_pixel(luma_4) : luma_4 } : 0;
     shiftout_2 <= luma_1[11:4] ? { pixel_1_y, pixel_1_x, pass_counter[2:0] == 3'b0 ? dim_pixel(luma_1) : luma_1 } : 0;
     shiftout_3 <= luma_2[11:4] ? { pixel_2_y, pixel_2_x, pass_counter[2:0] == 3'b0 ? dim_pixel(luma_2) : luma_2 }: 0;
     shiftout_4 <= luma_3[11:4] ? { pixel_3_y, pixel_3_x, pass_counter[2:0] == 3'b0 ? dim_pixel(luma_3) : luma_3 }: 0;                                
     

     

     
     if (buffer_write_ptr != buffer_read_ptr && search_counter > 1024 && (!luma_1[11:4] || !luma_2[11:4] || !luma_3[11:4] || !luma_4[11:4]))
     begin
         if (luma_4[11:4] == 0)  
               shiftout_1 <= { { next_pixel_y, next_pixel_x, 12'd4095 } };
         else if (luma_1[11:4] == 0)  
               shiftout_2 <= { { next_pixel_y, next_pixel_x, 12'd4095 } };
         else if (luma_2[11:4] == 0)  
               shiftout_3 <= { { next_pixel_y, next_pixel_x, 12'd4095 } };
         else if (luma_3[11:4] == 0)  
               shiftout_4 <= { { next_pixel_y, next_pixel_x, 12'd4095 } };
      
         buffer_read_ptr <= buffer_read_ptr + 1'b1;

         next_pixel_x <= buffer_pixel_x[buffer_read_ptr + 1'b1];
         next_pixel_y <= buffer_pixel_y[buffer_read_ptr + 1'b1];

         search_counter <= 0;                                                                   
      end
     

   
   else if (buffer_write_ptr != buffer_read_ptr && 
      (  (pixel_1_x == next_pixel_x && pixel_1_y == next_pixel_y)
      || (pixel_2_x == next_pixel_x && pixel_2_y == next_pixel_y)
      || (pixel_3_x == next_pixel_x && pixel_3_y == next_pixel_y)
      || (pixel_4_x == next_pixel_x && pixel_4_y == next_pixel_y)
      ))
   begin
      if (pixel_1_x == next_pixel_x && pixel_1_y == next_pixel_y)
         shiftout_2 <= { next_pixel_y, next_pixel_x, 12'd4095};      
         
      else if (pixel_2_x == next_pixel_x && pixel_2_y == next_pixel_y)
         shiftout_3 <= { next_pixel_y, next_pixel_x, 12'd4095};      
         
      else if (pixel_3_x == next_pixel_x && pixel_3_y == next_pixel_y)
         shiftout_4 <= { next_pixel_y, next_pixel_x, 12'd4095};      

      else if (pixel_4_x == next_pixel_x && pixel_4_y == next_pixel_y)
         shiftout_1 <= { next_pixel_y, next_pixel_x, 12'd4095};      
         

      buffer_read_ptr <= buffer_read_ptr + 1'b1;               
      
      next_pixel_x <= buffer_pixel_x[buffer_read_ptr + 1'b1];
      next_pixel_y <= buffer_pixel_y[buffer_read_ptr + 1'b1];

      search_counter <= 0;
     
   end
     

     else 
         for (i=8; i>0; i=i-1'b1)               
            if ((taps1[i * DATA_WIDTH-1 -: 10] == next_pixel_y && taps1[i * DATA_WIDTH-11 -: 10] == next_pixel_x && taps1[i * DATA_WIDTH-21 -: 8])
              ||(taps2[i * DATA_WIDTH-1 -: 10] == next_pixel_y && taps2[i * DATA_WIDTH-11 -: 10] == next_pixel_x && taps2[i * DATA_WIDTH-21 -: 8])
              ||(taps3[i * DATA_WIDTH-1 -: 10] == next_pixel_y && taps3[i * DATA_WIDTH-11 -: 10] == next_pixel_x && taps3[i * DATA_WIDTH-21 -: 8])
              ||(taps4[i * DATA_WIDTH-1 -: 10] == next_pixel_y && taps4[i * DATA_WIDTH-11 -: 10] == next_pixel_x && taps4[i * DATA_WIDTH-21 -: 8])
               )
               
               search_counter <= 0;                                                                                                
     end
end            

always @(posedge clk) begin                                

                                             
   rowbuff_rdaddress <= {current_y[2:0], current_x};
   rowbuff_wren <= 1'b1;
               

   p11 <= p12; p12 <= p13; p13 <= p13_w;
   p21 <= p22; p22 <= p23; p23 <= p23_w;
   p31 <= p32; p32 <= p33; p33 <= p33_w;                       


   if ( p22 < brightness) 
	begin   		
		pixel_out <= ( {8'b0, p11[7:1]} + p12 + p13 + p21 + p22 + p23 + p31 + p32 + p33[7:1] ) >> 3;
		p21 <= pixel_out;		
	end
   else                       
      pixel_out <= p22;
   
   output_pixel(pixel_out);

   if (erase_counter < current_x)
      begin                         
         rowbuff_wraddress <= {current_y[2:0], erase_counter}; 
         rowbuff_wdata <= 0;
         erase_counter <= erase_counter + 1'b1;                               
      end         
   
   else

   for (i=8; i>0; i=i-1'b1)               
      if (current_y < taps1[i * DATA_WIDTH-1 -: 10] && taps1[i * DATA_WIDTH-1 -: 10] - current_y <= 3'd7 && taps1[i * DATA_WIDTH - 21 -: 8] > 0) 
         begin  rowbuff_wraddress <= {taps1[i * DATA_WIDTH - 8 -: 3], taps1[i * DATA_WIDTH - 11 -: 10]}; rowbuff_wdata <= taps1[i * DATA_WIDTH - 21 -: 8]; end                        
      else
      if (current_y < taps2[i * DATA_WIDTH-1 -: 10] && taps2[i * DATA_WIDTH-1 -: 10] - current_y <= 3'd7 && taps2[i * DATA_WIDTH - 21 -: 8] > 0) 
         begin  rowbuff_wraddress <= {taps2[i * DATA_WIDTH - 8 -: 3], taps2[i * DATA_WIDTH - 11 -: 10]}; rowbuff_wdata <= taps2[i * DATA_WIDTH - 21 -: 8]; end                        
      else
      if (current_y < taps3[i * DATA_WIDTH-1 -: 10] && taps3[i * DATA_WIDTH-1 -: 10] - current_y <= 3'd7 && taps3[i * DATA_WIDTH - 21 -: 8] > 0) 
         begin  rowbuff_wraddress <= {taps3[i * DATA_WIDTH - 8 -: 3], taps3[i * DATA_WIDTH - 11 -: 10]}; rowbuff_wdata <= taps3[i * DATA_WIDTH - 21 -: 8]; end                                           
      else
      if (current_y < taps4[i * DATA_WIDTH-1 -: 10] && taps4[i * DATA_WIDTH-1 -: 10] - current_y <= 3'd7 && taps4[i * DATA_WIDTH - 21 -: 8] > 0) 
         begin  rowbuff_wraddress <= {taps4[i * DATA_WIDTH - 8 -: 3], taps4[i * DATA_WIDTH - 11 -: 10]}; rowbuff_wdata <= taps4[i * DATA_WIDTH - 21 -: 8]; end                                           
      else            
   

   if (horizontal_counter == `h_line_timing)
      erase_counter <= 0;

end
         

always @(posedge clk) begin         
   inside_visible_area <= (horizontal_counter >= `h_visible_offset + `h_center_offset && horizontal_counter < `h_visible_offset_end + `h_center_offset);
      
   if (horizontal_counter == `h_line_timing)    
      pass_counter <= pass_counter + 1'b1;

   prev_prev_wren_i <= prev_wren_i;
   prev_wren_i <= pixel_available;
   wren <= prev_prev_wren_i & ~prev_wren_i;
      
end     

  
endmodule