- Top Stats

Top Posters Top Topic Starters Top Topics
AMSDOS 1290
arnoldemu 1128
TFM 1124
ervin 746
ronaldo 406
AMSDOS 56
arnoldemu 51
ervin 42
EgoTrip 35
Arnaud 33
FutureOS corner - 71991 Views TFM/FS 07:12, 01 October 10
Example Z80 as... - 67342 Views arnoldemu 10:59, 04 April 10
#CPCtelera 1.4... - 33890 Views ronaldo 13:59, 11 May 15
BASIC programm... - 31925 Views arnoldemu 16:23, 22 May 09
CHAMP Assemble... - 25537 Views Bruce Abbo... 12:59, 16 January 14


Author Topic: cpctelera / sdcc - order of variables in struct  (Read 437 times)

0 Members and 1 Guest are viewing this topic.

Offline teopl

  • CPC464
  • **
  • Posts: 34
  • Country: cs
  • Liked: 17
I found a strange problem.

If I use the order of variables presented in the attached image (left side) the game reads h_px wrongly (some big value, didn't print it).

And if I use order from the right side, all is ok...

Now I know that order does matter in C, but I am not sure what am I doing wrong. How do you make order in structs? Do you use u16 at all? :)

« Last Edit: 19:35, 29 March 19 by teopl »

Offline Arnaud

  • Supporter
  • 464 Plus
  • *
  • Posts: 482
  • Country: fr
  • Liked: 380
Re: cpctelera / sdcc - order of variables in struct
« Reply #1 on: 19:56, 29 March 19 »
Hello,
just tried with the following code (teoplEntityDirection is of course missing) :

Code: [Select]
typedef struct
{
u8 type;
u8 draw;

u16 x_px;
u8 y_px;

u16 x_px_prev;
u8 y_px_prev;

u8 w_px;
u8 h_px;

u8 index_tile;
u8* sprite;

u8 ticks;
u8 ticks_needed;

} STestStruct;

STestStruct gStruct;

void main(void) {

   gStruct.w_px = 10;
   gStruct.h_px = 11;

   printf("%d", gStruct.w_px);
   printf("%d", gStruct.h_px);

   // Loop forever
   while (1);
}

No problem, all works.

I haven't encounter structure order problem in my own developments.

Seems to come to the use of the structure in your functions.



Offline teopl

  • CPC464
  • **
  • Posts: 34
  • Country: cs
  • Liked: 17
Re: cpctelera / sdcc - order of variables in struct
« Reply #2 on: 20:17, 29 March 19 »
Thanks Arnaud, I feared that it could be something else then the order...

The TeoplEntityDirection is an enum:
Code: [Select]
typedef enum {

    TEOPL_ENTITY_DIRECTION_NONE,

    TEOPL_ENTITY_DIRECTION_UP,
    TEOPL_ENTITY_DIRECTION_DOWN,
    TEOPL_ENTITY_DIRECTION_LEFT,
    TEOPL_ENTITY_DIRECTION_RIGHT

} TeoplEntityDirection;

but I guess it doesn't matter.

Also, one more thing - all works well in both cases when using wincpctelera from visual studio...

I must admit this is frustrating, because it looks like all other entity values are correct (exept the h_px) so it doesn't look like some memory overwriting which I already had once :D



Offline Arnaud

  • Supporter
  • 464 Plus
  • *
  • Posts: 482
  • Country: fr
  • Liked: 380
Re: cpctelera / sdcc - order of variables in struct
« Reply #3 on: 20:29, 29 March 19 »
Are the values in w_px and h_px OK just after the initialisation ?

If you want, you can put your project here, i'll take a look. Or send it me in PM.
« Last Edit: 20:31, 29 March 19 by Arnaud »

Offline teopl

  • CPC464
  • **
  • Posts: 34
  • Country: cs
  • Liked: 17
Re: cpctelera / sdcc - order of variables in struct
« Reply #4 on: 20:49, 29 March 19 »
Yeah, I think they should be. I will send you the project in PM.

Thanks again.

Offline teopl

  • CPC464
  • **
  • Posts: 34
  • Country: cs
  • Liked: 17
Re: cpctelera / sdcc - order of variables in struct
« Reply #5 on: 23:53, 29 March 19 »
Just for the record, Arnaud found the problem:

I had empty Z80CCFLAGS in my build_config.mk file...  :picard:

After setting it to:

Code: [Select]
Z80CCFLAGS    := --max-allocs-per-node 8000 --opt-code-size
the issue is gone :)

Offline freemac

  • 464 Plus
  • *****
  • Posts: 300
  • Country: 00
  • Liked: 237
Re: cpctelera / sdcc - order of variables in struct
« Reply #6 on: 16:50, 26 May 19 »
You can cast struct into array of bytes, you can insert also declare an array of character just before like "HELLO IM HERE", and refound it using a hexadecimal software like HxD.

you can also initialize a global struct with values.

Code: [Select]
struct CALQUE_J1A {
   CALQUE marcher; // cyclique, avance, recule
   CALQUE haut;
};

// J1A.adresse : bank4_4000();
const struct CALQUE_J1A J1A= {
   .marcher={0,8,{{0,0},{0,0},{0,0}},BANK_4,MARCHE | MARCHER | RAPIDEMENT},
   .haut={9,0,{{0,0},{0,0},{0,0}},BANK_4,0},
};

(...)

bank0123();
LoadFile("J2.map", (char *)normDIR[PERSO_LIU_KANG]); // des sprites
LoadFile("J2.dir", (char *)mapping_direction_calque[PERSO_LIU_KANG]); // des directions
LoadFile("J2.pha", (char *)mapping_phase_calque[PERSO_LIU_KANG]); // des phases

Offline ronaldo

  • Dev
  • 6128 Plus
  • *****
  • Posts: 593
  • Country: es
    • Fremos Blog
  • Liked: 793
Re: cpctelera / sdcc - order of variables in struct
« Reply #7 on: 01:44, 27 May 19 »
Just for the record, Arnaud found the problem:
I had empty Z80CCFLAGS in my build_config.mk file...  :picard:
After setting it to:
Code: [Select]
Z80CCFLAGS    := --max-allocs-per-node 8000 --opt-code-sizethe issue is gone :)

That makes no sense at all. It seems more likely that there is an actual problem with some kind of undefined behaviour in your code, and different compilation options could be hidding the problem, not fixing it. Although a bug in the compiler is possible, it seems more plausible to me that what you describe is a bug in your code that either overwrites memory or fails to properly initialize variables.

In fact, the order at which you place elements in your structs defines their layout in memory. So, if you change order, you are effectively placing them in a different memory order and location. Also, if you change compilation options, you will produce a different code with different size and layout. All of this is probably hidding the problem, not fixing it.

I would rather have a deep look at your code. Most probably, the bug is still there.

Offline teopl

  • CPC464
  • **
  • Posts: 34
  • Country: cs
  • Liked: 17
Re: cpctelera / sdcc - order of variables in struct
« Reply #8 on: 13:21, 01 June 19 »
@freemac Idea about packing struct in byte array sounds interesting... But it's not clear to me from the code you posted, maybe you could post some working example?

@ronaldo yes, the issue sounded very much like a memory problem since I had strange problems like: it showed and appeared randomly when code is changed in places not even connected to this module.

but since then, I changed and updated the code a lot and I didn't have issue yet so I will presume it's gone :)

anyway, one thing to say: I also had problems (hanging, crashing, not able to play some songs ...) with arkos2 player (provided here on the forum) and I switched to latest cpctelera and arkos1 so since then all is good, so I will continue like that.


Online Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 868
  • Country: fr
  • Liked: 820
Re: cpctelera / sdcc - order of variables in struct
« Reply #9 on: 13:59, 01 June 19 »
@teopl If you have time and if you deem it useful, could you switch to the latest version of Arkos Tracker 2? There is a special source converter which converts the player/music source directly to the SDCC syntax. There is a tutorial here about that.
Targhan/Arkos

Arkos Tracker 2 - alpha 5 now released! - Follow the news on Twitter!
Disark A cross-platform Z80 disassembler/source converter

Imperial Mahjong
Orion Prime

Offline teopl

  • CPC464
  • **
  • Posts: 34
  • Country: cs
  • Liked: 17
Re: cpctelera / sdcc - order of variables in struct
« Reply #10 on: 14:19, 01 June 19 »
@Targhan Hi, thanks for tutorial and for Arkos also! I will definitely try it, just probably not very very soon because of time limitation and we will not go into music yet (maybe in month or so).
For now build system and code is reverted to use Arkos1/cpctelera and when we make enough progress in game, I will for sure first check if all is working before starting learning/composing for Arkos2...

That brings me to one question: Is it or will it be possible to import Arkos1 song to Arkos2? (and are there any problems with that if yes)

Also, I am almost totally unfamiliar with differences between Arkos1 and Arkos2 since I do the code and this is my first cpc project, so a lot to learn...

Online Targhan

  • Supporter
  • 6128 Plus
  • *
  • Posts: 868
  • Country: fr
  • Liked: 820
Re: cpctelera / sdcc - order of variables in struct
« Reply #11 on: 18:23, 01 June 19 »
Quote
That brings me to one question: Is it or will it be possible to import Arkos1 song to Arkos2? (and are there any problems with that if yes)

Yes absolutely, importing AT1 songs works very fine (your song may need some tweaking for some very special hardware sound, but that's it).

Quote
Also, I am almost totally unfamiliar with differences between Arkos1 and Arkos2 since I do the code and this is my first cpc project, so a lot to learn...

To sum up, AT2 is much better than AT1 :). Anyone who's used to AT1 can switch to AT2 almost seamlessly.
Targhan/Arkos

Arkos Tracker 2 - alpha 5 now released! - Follow the news on Twitter!
Disark A cross-platform Z80 disassembler/source converter

Imperial Mahjong
Orion Prime