Code: Select all
If ( X1 - X0 ) < 0Code: Select all
If ( X1 - X0 ) < 0Code: Select all
x0 = sx ? x0 + 1 :
x0 - 1;Code: Select all
x0 = sx ? x0 + 1 :
x0 - 1;
Code: Select all
if( x1 < x0 )
Code: Select all
if e2 > -dy then Code: Select all
module LineGen( input clk108,
input countflag, //flag, plot inside borders
input vblank,
input vsync,
output reg [9:0] X, //H component of pixel to BRAM
output reg [8:0] Y //V component of pixel to BRAM
);
reg [9:0] x0 = 0;
reg [9:0] x1 = 639;
reg [8:0] y0 = 0;
reg [8:0] y1 = 479;
reg [9:0] dx;
reg [8:0] dy;
reg [9:0] err;
reg [10:0] e2;
reg [0:0] sx;
reg [2:0] state;
parameter prep_state = 0, errcalc_state = 1, draw_state = 2;
//state machine for plotting a line
always @(posedge clk108)
if ( vblank & vsync )
state <= prep_state;
else
case (state)
prep_state: //calculate dx/dy and slope of X
state <= errcalc_state;
errcalc_state: //calculate error
state <= draw_state;
draw_state:
if ( !countflag )
state <= prep_state;
endcase
always @(posedge clk108) begin
case (state)
prep_state:
begin
if ( x1 > x0 ) begin //negative "\" slope
dx <= x1 - x0;
sx <= 0;
end
else begin //positive "/" slope
dx <= x0 - x1;
sx <= 1;
end
if ( y1 > y0)
dy <= y1 - y0;
else
dy <= y0 - y1;
end
errcalc_state:
begin
if ( dx > dy )
err <= dx - dy;
else
err <= dy - dx;
end
draw_state:
begin
e2 <= err + err;
X <= x0;
Y <= y0; //output origin
if ( e2 < dy ) begin
err <= err + dy;
x0 <= sx ? x0 + 1 :
x0 - 1;
X <= x0;
end
else if (( x0 = err ) | ( y0 != y1 )) begin
y0 <= y0 + 1;
Y <= y0;
end
end
endcase
end
endmodule