t-edson / P65Pas

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

Initialize variables #54

Closed Skertan closed 1 year ago

Skertan commented 1 year ago
program NewProgram;
uses Commodore64;
var
  c: char = 'A';
begin
  CHROUT(c);
end.

Error: Cannot initialize absolute variable "c" in location $80D.

Squall-FF8 commented 1 year ago

Strangely enough that works for X16: image

t-edson commented 1 year ago

My fault. I've playing with the memory manager in the trunk version. It's updated now. However there's still a problem: Commodore64 defines some free bytes in the zero-page and the compiler is locating variable "c" in that free space. And, formally, variables that are out of the main program block (starts at $0801) cannot be initialized that way.

t-edson commented 1 year ago

One simple solution could be including the directive {$SET_DATA_ADDR ''} after the USES section so we force to not use the bytes from zero page: image Other solution could be to modify the Commodore64 unit.

Skertan commented 1 year ago

I put {$SET_DATA_ADDR ''} and it works. Does it have some side effects, since the original value defines a few bytes from zero page. If there are no side effects this should be the standard in the included Commodore64.pas in the repository.

t-edson commented 1 year ago

At the moment compiler can work well without using zero-page. Even when using arrays, compiler generates self-modified binary code to work. But in the future it will be needed to have some zero-page to implement some routines. Moreover, having some bytes in zero-page gives the code some extra speed. And compiler uses it to allocate registers or temporal values. I think it's useful.

t-edson commented 1 year ago

Maybe you can use {$SET_DATA_ADDR ''} and if you want some zero-page variables you can use "ABSOLUTE" variable declaration or "ZEROPAGE" variable declaration (Not yet implemented). However, I cannot promise compiler won't need, in the future, some bytes from zero-page. As we know the 6502 CPU is very dependent of the zero-age.

Squall-FF8 commented 1 year ago

Yes, ZeroPage variables reduce the generated code and increase the performance.

In order to avoid current problems with the compiler, you may put the initialized variable at the end of declarations. This way there is big chance the earlier declared variable to "deplete" ZeroPage, so initialized ones will be in $80D+ space. Of course that is very program depended and no 100% guarantee that will happen.

t-edson commented 1 year ago

There is new mode to assign RAM to variables (Recently created to help on implementing Pointers). It is called "decDatSec" and it assures the variable is allocated in the code section. Sadly, it's not accessible from PASCAL. It could be added as: VAR c: char DATASECTION; Currently there is only ABSOLUTE, and in the future ZEROPAGE attribute for declaring variables. Anyway I don't want to invent new reserved words, because that's not the Pascal spirit. Maybe the best solution will be just not to locate variables initialized in the zero page.

t-edson commented 1 year ago

It's fixed. Compiler now force initialized variables to be located in the Data Section, close to the Code section, so they won't use the location defined by {$SET_DATA_ADDR}, and will be able to be initialized. image