module scanlinebuffer (pixeldataA, pixeldataB, pixeldataC, nextpixelA, nextpixelB, nextpixelC, startframe, nextscanline, fbdata, fbnextscanline, fbdatavalid, globalreset, clk);

    output[20:0] pixeldataA; 
    wire[20:0] pixeldataA;
    output[20:0] pixeldataB; 
    wire[20:0] pixeldataB;
    output[20:0] pixeldataC; 
    wire[20:0] pixeldataC;
    input nextpixelA; 
    input nextpixelB; 

    input nextpixelC; 
    input startframe; 
    input nextscanline; 
    input[62:0] fbdata; 
    output fbnextscanline; 
    reg fbnextscanline;
    input fbdatavalid; 
    input globalreset; 
    input clk; 

    reg[1:0] state; 
    reg[1:0] next_state; 
    reg weA; 
    reg weB; 
    reg weC; 

	reg temp_fbnextscanline;

    scanline scanlineA (nextpixelA, weA, fbdata[62:0], pixeldataA, globalreset, clk); 
    scanline scanlineB (nextpixelB, weB, fbdata[62:0], pixeldataB, globalreset, clk); 
    scanline scanlineC (nextpixelC, weC, fbdata[62:0], pixeldataC, globalreset, clk); 

    always @(posedge clk or posedge globalreset)
    begin
       if (globalreset == 1'b1)
       begin
          state <= 0 ; 
          fbnextscanline <= 1'b0 ; 
       end
       else
       begin
          state <= next_state ; 
          if (fbdatavalid == 1'b1)
          begin
             fbnextscanline <= 1'b0 ; 
          end 
		  else
		  begin
		  	fbnextscanline <= temp_fbnextscanline;
		  end
		end
	end

    always @(state or nextscanline or fbdatavalid or startframe)
    begin
       case (state)
          0 :
                   begin
				       weB = 1'b0 ; 
				       weC = 1'b0 ; 
                      weA = fbdatavalid ; 
                      if (nextscanline == 1'b1 & startframe == 1'b1)
                      begin
                         next_state = 2 ; 
                      end
                      else
                      begin
                         next_state = 0 ; 
                      end 
                         if (startframe == 1'b1 & nextscanline == 1'b1)
                         begin
                            temp_fbnextscanline = 1'b1 ; 
                         end 

                   end
          1 :
                   begin
				       weB = 1'b0 ; 
				       weC = 1'b0 ; 
                      weA = fbdatavalid ; 
                      if (nextscanline == 1'b1)
                      begin
                         next_state = 2 ; 
                      end
                      else
                      begin
                         next_state = 1 ; 
                      end 

                         if (nextscanline == 1'b1)
                         begin
                            temp_fbnextscanline = 1'b1 ; 
                         end 
 

                   end
          2 :
                   begin
				       weA = 1'b0 ; 
				       weC = 1'b0 ; 
                      weB = fbdatavalid ; 
                      if (nextscanline == 1'b1)
                      begin
                         next_state = 3 ; 
                      end
                      else
                      begin
                         next_state = 2 ; 
                      end 
                         if (nextscanline == 1'b1)

                         begin
                            temp_fbnextscanline = 1'b1 ; 
                         end 
 
                   end
          3 :
                   begin
				       weA = 1'b0 ; 
				       weB = 1'b0 ; 
                      weC = fbdatavalid ; 
                      if (nextscanline == 1'b1)
                      begin
                         next_state = 1 ; 
                      end
                      else
                      begin
                         next_state = 3 ; 
                      end 
	                  if (nextscanline == 1'b1)
                         begin

                            temp_fbnextscanline = 1'b1 ; 
                         end 

                   end
       endcase 
    end 
 endmodule