i wrote a simple video RAM for drawing lines on GLCD. Can somebody suggest improvements to make this faster?
If it matters, the code runs on dsPIC33EP512MC504.
Thank you
Code: Select all
var
VirtualMem: array[1024] of Byte;
procedure ClearVirtualMem;
var
i: Integer;
begin
for i := 0 to 1023 do
VirtualMem[i] := 0;
end;
procedure PutPixelOnVirtualMem(x, y: Integer; Color: Byte);
var
i, yy: Word;
begin
//i := x + ((y shr 3 ) and 7) shl 7;
i := x + ((y and $0038) shl 4);
if i > 1023 then
i := 0; //Draw on 0,0 to display bugs
yy := y and 7; //position inside a byte
VirtualMem[i].yy := Color;
end;
//line algorithm from http://members.chello.at/~easyfilter/Bresenham.pdf
procedure DrawLineOnVirtualMem(x0, y0, x1, y1: Integer; Color: Byte); //Bresenham line on virtual RAM
var
dx, dy, sx, sy, err, e2: Integer;
begin
dx := Abs(x1 - x0);
dy := - Abs(y1 - y0);
if x0 < x1 then
sx := 1
else
sx := -1;
if y0 < y1 then
sy := 1
else
sy := -1;
err := dx + dy;
repeat
PutPixelOnVirtualMem(x0, y0, Color);
if (x0 = x1) and (y0 = y1) then
Break;
e2 := err * 2;
if e2 >= dy then
begin
err := err + dy;
x0 := x0 + sx;
end;
if e2 <= dx then
begin
err := err + dx;
y0 := y0 + sy;
end;
until False;
end;
procedure DrawVirtualMem;
var
i, x, y, y7: Word;
begin
Glcd_Set_Side(0); //Left side of the screen
for y := 0 to 7 do
begin
y7 := y shl 7;
Glcd_Set_Page(y);
for x := 0 to 63 do
begin
Glcd_Set_X(x);
i := x + y7;
Glcd_Write_Data(VirtualMem[i]);
end;
end;
Glcd_Set_Side(64); //Right side of the screen
for y := 0 to 7 do
begin
y7 := y shl 7;
Glcd_Set_Page(y);
for x := 64 to 127 do
begin
Glcd_Set_X(x);
i := x + y7;
Glcd_Write_Data(VirtualMem[i]);
end;
end;
end;