Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mad pascal optimizes indexing wrong #128

Closed
aydindemircioglu opened this issue Feb 2, 2024 · 10 comments
Closed

mad pascal optimizes indexing wrong #128

aydindemircioglu opened this issue Feb 2, 2024 · 10 comments

Comments

@aydindemircioglu
Copy link

i think mad pascal again over-optimizes incorrectly, i do not have a minimal example at hand,
but my code is like this:

    weaponNames:   array[0..8] Of String[16] =   ('H', 'M', 'X', 'Y', 'Z'~);
    fp_N : array[0..2] of byte absolute $e400;
    fp_weapon: array[0..16*2-1] of byte absolute $e462;
    fp_Gang: array[0..2] of String[15] absolute $e482;
    fp_name: array[0..16*2-1] of String[16];

    fp_N[0] := 4;
    fp_N[1] := 2;
    fp_Gang[0] := 'Tunkar'~;
    fp_Gang[1] := 'Mari Bani'~;
    fp_name[0] := 'Peter 1'~;
<...>
    fp_name[16] := 'Borke 1'~;
<...>
    fp_name[17] := 'Borke 2'~;
    fp_weapon[0] := 0;
    fp_weapon[1] := 1;
    fp_weapon[2] := 2;
    fp_weapon[3] := 3;
    fp_weapon[16] := 3;
    fp_weapon[17] := 5;

        For s := 0 To 1 Do
            For i := 0 To fp_N[s]-1 Do
                Begin;
                    If fp_energy[s SHL 4 +i] = 0 Then continue;

                    // player stats
                    // fill old stuff first 
                    FillChar (Pointer (MAP_SCR_ADDRESS+19*40), 5*40, ' '~);
                    ofs := 20*s;
                    CRT_GotoxY(ofs,20);
                    CRT_Write(fp_Name[s SHL 4+i]);
< more stuff involving s SHL 4 +i >
                    CRT_Write(fp_weapon[s SHL 4 + i]);
                    CRT_Write(weaponNames[fp_weapon[s SHL 4 +i]]);
...

this results that the weapon names from player from gang 1 (=0,1,2,3) are OK, but weapon names from players from the other side (with index 16, 17, 18) show the wrong string, possibly because mad-pascal tries to optimize something incorrectly?
this is resolved by just introducing a new byte z like this.

        For s := 0 To 1 Do
            For i := 0 To fp_N[s]-1 Do
                Begin;
                    z := s SHL 4 + i;
                   CRT_Write(weaponNames[fp_weapon[z]]);
...

(i know introducing this byte z is better than writing s SHL 4 + i all the times, but this is debug stadium, optimization will be done later).

am using mad pascal 1.7.0 still, not sure if this was fixed already.

@tebe6502
Copy link
Owner

tebe6502 commented Feb 2, 2024

in the latest version of MP, there is an error message when trying to use the modifier ABSOLUTE for a string array

fp_Gang: array[0..2] of String[15] absolute $e482;

compiler has no way of instantiating such an array with valid pointers to strings
such an array will contain pointers with a value of 0

@aydindemircioglu
Copy link
Author

ok, but how to do it else? what i want would be at $e482 a space of 15 chars (+1 for the string length) that the user can fill (gang name is typed in at start of the game).

@tebe6502
Copy link
Owner

tebe6502 commented Feb 3, 2024

fp_Gang: array[0..2] of pointer absolute $e482;
gang0, gang1, gang2 : string[15];

begin
fp_Gang[0] := @gang0;
fp_Gang[1] := @gang1;
fp_Gang[2] := @gang2;

fp_Gang[2]:='string2';
fp_Gang[1]:='string1';
fp_Gang[0]:='string0';

writeln(Pstring(fp_Gang[2]));

end.

@zbyti
Copy link
Collaborator

zbyti commented Feb 3, 2024

{$define basicoff}
{$define romoff}

type TGang = record
   name: string[15];
   logo: string[15];
end;

var
   fp_Gang: array[0..2] of ^TGang absolute $e482;
   gang0, gang1, gang2 : TGang;

begin

gang0.name := 'string1';
gang1.name := 'string1';
gang2.name := 'string2';
gang0.logo := 'red';
gang1.logo := 'white';
gang2.logo := 'black';

fp_Gang[2] := @gang2;
fp_Gang[1] := @gang1;
fp_Gang[0] := @gang0;

writeln(Pstring(fp_Gang[2].name));
writeln(Pstring(fp_Gang[1]));
writeln(Pstring(fp_Gang[2].logo));
writeln(Pstring(fp_Gang[1].logo));

repeat until false;

end.

@aydindemircioglu
Copy link
Author

thanks to both of you!
i will try to see if the problem is there with the latest version and if i use correct code.
it may take a couple of days (or weeks), need to finish other work first.

@zbyti
Copy link
Collaborator

zbyti commented Feb 4, 2024

var
   fp_Gang: array[0..2] of String[15] absolute $e482;

begin
   repeat until false;
end.
Mad Pascal Compiler version 1.7.1 [2024/02/03] for 6502
Compiling test.pas
test.pas (5,52) Error: ABSOLUTE modifier is not available for this type of array

@zbyti zbyti closed this as completed Feb 4, 2024
@aydindemircioglu
Copy link
Author

but that was not the initial problem. in the code above weaponnames are a usual array of strings, not fixed to an absolute address. the fp_weapon was absolute, but its an array of byte. yet, it seemed to me that mad pascal confused the indexing. as said i will check this later and reopen if need be.

@zbyti
Copy link
Collaborator

zbyti commented Feb 4, 2024

I'll check it

I remember that I had a similar problem in previous versions (2-3 years ago) and I wrote a workaround then.

I also had a problem when I was trying to calculate an index inside array brackets (the problematic array was nested within another array), and the solution was to make this calculation one step earlier

@zbyti zbyti reopened this Feb 4, 2024
@zbyti
Copy link
Collaborator

zbyti commented Feb 4, 2024

@aydindemircioglu I wrote some simple examples considering your case and got the correct result, please provide full example of your code.

@aydindemircioglu
Copy link
Author

thanks, as said, might be due to my older version. the code is embedded in a long procedure, i have to take my time to get a minimal example. for now i will then close this issue, and will reopen it if i am able to reproduce with the minimal example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants