parameter win_w=300;
parameter win_h=80;



module ellipse_draw(

input logic            clk50,reset,clk25,
input logic [4:0]      x_in,
input logic [6:0]      y_in,
input logic [7:0]      data_rom_f, data_rom_b,
input logic [4:0]      ecc_h,
input logic [7:0]      data_valid,

output logic [13:0]    addr_rom_f, addr_rom_b,
output logic [12:0]    addr_rom_h,
output logic [14:0]    addr_win,
output logic [7:0]     data_win,
output logic           wren,rden,ready);

logic [4:0]       rad;
logic [7:0]       x_count;
logic [1:0]       four; //0,1,back 2,3,front
logic [6:0]       cent_y;
logic [6:0]       win_y;
logic [8:0]       win_x;
logic [8:0]       data_rom_real;
logic [2:0]       ecc_count;


assign rad = x_in;


always_ff @(posedge clk25) begin
	if(reset)   begin 
           x_count <=0;
           four <=0;
           ecc_count <=0;
         end
        else if (x_count == 5*rad&&four<=3&&ecc_count<=4) begin
           x_count <=0;
           four <= four + 'd1;
         end//else if
        else if (x_count == 5*rad&&four==4&&ecc_count<=4) begin
           x_count <=0;
           four <=0;
           ecc_count <= ecc_count + 'd1; 
         end //else if
        else if(ecc_count==5) begin
           ecc_count <= 0;
           ready <=1;
         end
        else 
           x_count <= x_count + 'd1;   
 
end  //always



//------------------------- rom address && data ---------------------------
always@*begin
    addr_rom_h =  (5*rad+2)*(rad-1)/2 + x_count;
       if (four==0)
	addr_rom_b = (1+5*rad*(rad-1)+ ( 5*rad + x_count[7:0]));
       else if (four==1)
	addr_rom_b = (1+5*rad*(rad-1)+ ( 5*rad - x_count[7:0]));
       else if (four==2)
	addr_rom_f = (1+5*rad*(rad-1)+ ( 5*rad + x_count[7:0]));
       else if (four==3)
	addr_rom_f = (1+5*rad*(rad-1)+ ( 5*rad - x_count[7:0]));
end


always@*begin
    if (four==0 || four ==1)
      data_rom_real = data_rom_b;
    else if (four ==2||four==3)
      data_rom_real = data_rom_f;
    else ;
end //always


//--------------------------frame_buffer address-------------------
always@*begin
      cent_y =(5*y_in - 10 + ecc_count)% win_h;
       if (four==0) begin
        win_y = (cent_y - ecc_h)% win_h;
        win_x = 150 + x_count;
        addr_win = win_y*300 + win_x;
       end
       else if (four==1) begin
        win_y = (cent_y - ecc_h)% win_h;
        win_x = 150 - x_count;
        addr_win = win_y*300 + win_x;
       end
       else if (four==2) begin
	win_y = (cent_y + ecc_h)% win_h;
        win_x = 150 + x_count;
        addr_win = win_y*300 + win_x;
       end
       else if (four==3) begin
        win_y = (cent_y + ecc_h)% win_h;
        win_x = 150 - x_count;
        addr_win = win_y*300 + win_x;
       end
       else ;
end

//-------------------------



always_ff @(posedge clk50)begin
   if (clk25==1)
        rden=1;
   else if(data_valid==0 && clk25==0)begin
	data_win = data_rom_real;
        rden = 0;
        wren = 1;
   end
   else if (clk25==0) begin
        rden = 0;
        wren = 0;
   end
   else ;
end //always



endmodule








    
