t-edson / P65Pas

CPU 6502 Pascal Compiler/IDE/Debugger
GNU General Public License v3.0
119 stars 27 forks source link

byte range invalid #40

Closed odflor closed 1 year ago

odflor commented 2 years ago

If I define a variable as byte only can assign values from 0 to 254 or $0 to $FE.

Normally a byte value varies from 0 to 255 or $0 to $FF.

This way is impossible to poke a 255 or $FF value on the screen with variables.

test this program and change from 254 to 255 to see compiler error:

program Hello; //uses Commodore128; uses Commodore64; var i : byte; p : byte;

screen1: [255]byte absolute 1024; screen2: [255]byte absolute 1279; screen3: [255]byte absolute 1534; screen4: [255]byte absolute 1789;

color1: [255]byte absolute 55296; color2: [255]byte absolute 55551; color3: [255]byte absolute 55806; color4: [255]byte absolute 56061; begin screenBack := 0; screenBorder := 12;

p := 0;

for i := 0 to 254 do screen1[i] := p; screen2[i] := p; screen3[i] := p;

if i < 235 then
  screen4[i] := p;
end; // if

inc(p);

if p > 254 then
  p := 0;
end; // if

color1[i] := 3;
color2[i] := 4;
color3[i] := 5;

if i < 235 then
  color4[i] := 6;
end; // if

end; // for

asm RTS end end.

t-edson commented 1 year ago

Hi. The FOR range from 0 to 255, with variables Byte, is always problematic for compilers. I remember some other compiler for 6502, have the same problem.

Anyway, I have created an option in the compiler to can have a secure FOR i:=0 to 255. It's fixed changing the FOR to a REPEAT and an IF sentences. Now the problem is solved when activated that option. image

t-edson commented 1 year ago

Just for information. In new version of the compiler, you can fill all the content of an array using the clear() method, like:

var
  buffer: array[256] of byte;
begin
  //Fill all the array with the $FF value
  buffer.clear($FF);
end.

With this method you can fill, with an optimized code, arrays as big as 1024 bytes.