module Buffer(
input logic clk0,
//interface between top
input logic [15:0]  writedata0,
input logic write0,
input chipselect0,
input [5:0]  address0, 
input reset0,
//interface between vga emulator
input logic VBLANK,
output logic [15:0] 	x0, y0, flag0,
									//3 bullets
							x1,y1,flag1,x2,y2,flag2,x3,y3,flag3,
								   //3 enemies
							x4,y4,flag4,eid4,x5,y5,flag5,eid5,x6,y6,flag6,eid6,
								  //floor, brick
							x7,x8,
								  //3 explosions,
							x9,x10,x11,y9,y10,y11,flag9,flag10,flag11,
									//airflow
							x12,y12,flag12,
									//number
							flag13,flag14,flag15,
									//press
							flag16,
									//over
							flag17
);
// 2nd level buffer
logic [15:0] 	block0_x0, block0_y0, block0_flag0,
									//3 bullets
					block0_x1,block0_y1,block0_flag1,block0_x2,block0_y2,block0_flag2,block0_x3,block0_y3,block0_flag3,
								   //3 enemies
					block0_x4,block0_y4,block0_flag4,block0_eid4,block0_x5,block0_y5,block0_flag5,block0_eid5,block0_x6,block0_y6,block0_flag6,block0_eid6,
								  //floor, brick
					block0_x7,block0_x8,
								  //3 explosions,
					block0_x9,block0_x10,block0_x11,block0_y9,block0_y10,block0_y11,block0_flag9,block0_flag10,block0_flag11,
									//airflow
					block0_x12,block0_y12,block0_flag12,
									//number
					block0_flag13,block0_flag14,block0_flag15,
									//press
					block0_flag16,
									//over
					block0_flag17;
//1st level buffer
logic [15:0] 	block1_x0, block1_y0, block1_flag0,
									//3 bullets
					block1_x1,block1_y1,block1_flag1,block1_x2,block1_y2,block1_flag2,block1_x3,block1_y3,block1_flag3,
								   //3 enemies
					block1_x4,block1_y4,block1_flag4,block1_eid4,block1_x5,block1_y5,block1_flag5,block1_eid5,block1_x6,block1_y6,block1_flag6,block1_eid6,
								  //floor, brick
					block1_x7,block1_x8,
								  //3 explosions,
					block1_x9,block1_x10,block1_x11,block1_y9,block1_y10,block1_y11,block1_flag9,block1_flag10,block1_flag11,
									//airflow
					block1_x12,block1_y12,block1_flag12,
									//number
					block1_flag13,block1_flag14,block1_flag15,
									//press
					block1_flag16,
									//over
					block1_flag17;
logic flag_block1; 
logic flag_block0;

//1st level assignments
always_ff @(posedge clk0)
	if (reset0) begin
	flag_block1<=1;
	flag_block0<=0;
	//chara
	block1_x0 <= 0; 
	block1_y0 <= 100; 
	block1_flag0 <= 1;
	//bullet1
   block1_x1 <=500;	
   block1_y1 <= 100; 
	block1_flag1<=1;
	//bullet2
	block1_x2 <= 500; 
	block1_y2 <= 200; 
   block1_flag2<=1;
	//bullet3
	block1_x3 <= 500; 
	block1_y3 <= 300; 
   block1_flag3<=1;
	
	//enemy1
	block1_x4 <= 500; 
	block1_y4 <= 200;
   block1_flag4 <= 2;
	block1_eid4<= 1;
	
	//enemy2
	block1_x5 <= 100; 
	block1_y5 <= 200; 
   block1_flag5 <= 2;
	block1_eid5<=2;
	
	//enemy3
	block1_x6 <= 400; 
	block1_y6 <= 200; 
   block1_flag6 <= 2;
	block1_eid6<=2;
	
	//floor
	block1_x7 <= 0; 
	 
   
	//brick
	block1_x8 <= 0;
   

	
	//explosion1
	block1_x9 <= 100; 
	block1_y9 <= 300; 
   block1_flag9 <= 1;
	
	//explosion2
	block1_x10 <= 300; 
	block1_y10 <= 300; 
   block1_flag10 <= 1;
	
	//explosion3
	block1_x11 <= 500; 
	block1_y11 <= 300; 
   block1_flag11 <= 1;
	
	//airflow
	block1_x12<= 300; 
	block1_y12 <= 0; 
   block1_flag12 <= 6;
	
	//number1
	
	//39:y13<=writedata0;
	block1_flag13<=0;
	
	//number2
	
	//40:y14<=writedata0;
	block1_flag14<=2;
	
	//number3
	
	//40:y15<=writedata0;
	block1_flag15<=3;
	
	//press
	block1_flag16<=1;
	
	//start
	block1_flag17<=1;
	//background
	//x10<=0;
	end
	else if (write0 && !flag_block1&&chipselect0)begin
		 case (address0)
	 
	 //chara
	0:block1_x0 <= writedata0; 
	1:block1_y0 <= writedata0; 
	2:block1_flag0 <= writedata0;
	//bullet1
   3:block1_x1 <=writedata0;	
   4:block1_y1 <= writedata0; 
	5:block1_flag1 <= writedata0; 
	
	//bullet2
	6:block1_x2 <= writedata0; 
	7:block1_y2 <= writedata0; 
   8:block1_flag2 <= writedata0;
	
	//bullet3
	9:block1_x3 <= writedata0; 
	10:block1_y3 <= writedata0; 
   11:block1_flag3 <= writedata0;
	
	//enemy1
	12:block1_x4 <= writedata0; 
	13:block1_y4 <= writedata0; 
   14:block1_flag4 <= writedata0;
	15:block1_eid4 <= writedata0;
	
	//enemy2
	16:block1_x5 <= writedata0; 
	17:block1_y5 <= writedata0; 
   18:block1_flag5 <= writedata0;
	19:block1_eid5 <= writedata0;
	
	//enemy3
	20:block1_x6 <= writedata0; 
	21:block1_y6 <= writedata0; 
   22:block1_flag6 <= writedata0;
	23:block1_eid6 <= writedata0;
	
	//floor
	24:block1_x7 <= writedata0; 
	
	//brick
	25:block1_x8 <= writedata0;
	
	//explosion1
	26:block1_x9<= writedata0; 
	27:block1_y9 <= writedata0; 
   28:block1_flag9<= writedata0;
		
	//explosion2
	29:block1_x10<= writedata0; 
	30:block1_y10 <= writedata0; 
   31:block1_flag10<= writedata0;
	
		
	//explosion3
	32:block1_x11<= writedata0; 
	33:block1_y11 <= writedata0; 
   34:block1_flag11<= writedata0;
	
	//airflow
	35:block1_x12<= writedata0; 
	36:block1_y12 <= writedata0; 
   37:block1_flag12 <= writedata0;
	
	//number1
	
	38:block1_flag13<=writedata0;
	
	//number2
	
	39:block1_flag14<=writedata0;
	
	//number3
	
	40:block1_flag15<=writedata0;
	
	//press to start
	41:block1_flag16<=writedata0;
	
	//gameover
	42: begin 
	   block1_flag17<=writedata0;
	   flag_block1<=1;
		end
	
		endcase
	end
	

//2nd level assignments
	
  else if(flag_block1 && !flag_block0)begin
		block0_x0 <= block1_x0; 
		block0_y0 <= block1_y0; 
		block0_flag0 <= block1_flag0 ;
	//bullet1
		block0_x1 <=block1_x1;	
		block0_y1 <= block1_y1; 
		block0_flag1 <= block1_flag1; 
	
	//bullet2
		block0_x2 <= block1_x2; 
		block0_y2 <=block1_y2  ; 
		block0_flag2 <=block1_flag2 ;
	
	//bullet3
		block0_x3 <= block1_x3; 
		block0_y3 <=block1_y3 ; 
		block0_flag3 <= block1_flag3;
	
	//enemy1
		block0_x4 <= block1_x4; 
		block0_y4 <= block1_y4; 
		block0_flag4 <= block1_flag4;
		block0_eid4 <= block1_eid4;
	
	//enemy2
		block0_x5 <= block1_x5; 
		block0_y5 <= block1_y5; 
		block0_flag5 <= block1_flag5;
		block0_eid5 <= block1_eid5;

	//enemy3
		block0_x6 <=block1_x6 ; 
		block0_y6 <=block1_y6 ; 
		block0_flag6 <= block1_flag6;
		block0_eid6 <=block1_eid6 ;
	
	//floor
		block0_x7 <= block1_x7; 

	//brick
		block0_x8 <= block1_x8;
	
	//explosion1
		block0_x9<= block1_x9; 
		block0_y9 <= block1_y9; 
		block0_flag9<= block1_flag9;
	
	//explosion2
		block0_x10<=block1_x10 ; 
		block0_y10 <=block1_y10 ; 
		block0_flag10<=block1_flag10 ;
	
		
	//explosion3
		block0_x11<=block1_x11 ; 
		block0_y11 <=block1_y11 ; 
		block0_flag11<=block1_flag11;
	
	//airflow
		block0_x12<=block1_x12 ; 
		block0_y12 <=block1_y12; 
		block0_flag12 <=block1_flag12;
	
	//number1
	
		block0_flag13<=block1_flag13;

	//number2
	
		block0_flag14<=block1_flag14;
	
	//number3
	
		block0_flag15<=block1_flag15;
	
	//press to start
		block0_flag16<=block1_flag16;
	
	//gameover
		block0_flag17<=block1_flag17;
	   flag_block1<=0;
		flag_block0<=1;
	end
  
   else if(flag_block0 && !VBLANK)begin
		x0 <= block0_x0; 
		y0 <= block0_y0; 
		flag0 <= block0_flag0 ;
	//bullet1
		x1 <=block0_x1;	
		y1 <= block0_y1; 
		flag1 <= block0_flag1; 
	
	//bullet2
		x2 <= block0_x2; 
		y2 <=block0_y2  ; 
		flag2 <=block0_flag2 ;
	
	//bullet3
		x3 <= block0_x3; 
		y3 <=block0_y3 ; 
		flag3 <= block0_flag3;
	
	//enemy1
		x4 <= block0_x4; 
		y4 <= block0_y4; 
		flag4 <= block0_flag4;
		eid4 <= block0_eid4;
	
	//enemy2
		x5 <= block0_x5; 
		y5 <= block0_y5; 
		flag5 <= block0_flag5;
		eid5 <= block0_eid5;
	
	//enemy3
		x6 <=block0_x6 ; 
		y6 <=block0_y6 ; 
		flag6 <= block0_flag6;
		eid6 <=block0_eid6 ;
	
	//floor
		x7 <= block0_x7; 
	
	//brick
		x8 <= block0_x8;
	
	//explosion1
		x9<= block0_x9; 
		y9 <= block0_y9; 
		flag9<= block0_flag9;
		
	//explosion2
		x10<=block0_x10 ; 
		y10 <=block0_y10 ; 
		flag10<=block0_flag10 ;
	
		
	//explosion3
		x11<=block0_x11 ; 
		y11 <=block0_y11 ; 
		flag11<=block0_flag11;
	
	//airflow
		x12<=block0_x12 ; 
		y12 <=block0_y12; 
		flag12 <=block0_flag12;
		//number1
	
		flag13<=block0_flag13;
	
	//number2
	
		flag14<=block0_flag14;
	
	//number3
	
		flag15<=block0_flag15;
	
	//press to start
		flag16<=block0_flag16;
	
	//gameover
		flag17=block0_flag17;
		 flag_block0=0;
	end

	
endmodule
	