Author Topic: [SDCC] weird memory problem  (Read 884 times)

0 Members and 1 Guest are viewing this topic.

Offline ervin

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.325
  • Country: au
    • index.php?action=treasury
  • Liked: 987
  • Likes Given: 1153
[SDCC] weird memory problem
« on: 02:15, 15 August 17 »
Hi folks.

I'm wondering if anyone knowledgable with SDCC can help me with a strange problem.
While writing my game for cpcretrodev2017, I ran into a problem I've never seen before.

I've got a lot of arrays set up at the top of my main.c file.
They are followed by the function main().
This is how I've always done SDCC dev.

Now, the weird thing is that I seem to have hit a wall, despite my program only compiling to around 4K.
When I add one more variable, and try to reference that new variable in code, I get weird crashes.

I've tried a lot of different things, and the only thing that fixed it was to either remove one of the arrays, or move an array to my asm.s file, and define it in z80. The compiler then seemed to put the array definition into a different part of memory, thereby fixing the crash I was getting.

It seems that too many variables/arrays defined in C confuses the compiler. It looks very much like the compiler placed data into the code area at compile time, overwriting some code.

Has anyone ever seen anything like this before?
My (cancelled) entry for the CPCRetroDev 2017 Competition http://www.cpcwiki.eu/forum/programming/my-cpcretrodev-2017-entry/
FAST line drawing in CPCtelera http://www.cpcwiki.eu/forum/programming/drawing-lines-with-cpctelera-sdcc/
RUNCPC My entry for the CPCRetroDev 2015 Competition http://www.cpc-power.com/index.php?page=detail&num=12494

Offline ervin

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.325
  • Country: au
    • index.php?action=treasury
  • Liked: 987
  • Likes Given: 1153
Re: [SDCC] weird memory problem
« Reply #1 on: 02:42, 15 August 17 »
Interesting...

I've got 2 buffers (1024 bytes each) that I use to determine which tiles have changed from frame-to-frame.
I've put them into fixed locations in RAM, and the crashes are gone.
(This is in cpctelera, by the way).

Code: [Select]
__at(0x1000) u8 buffer0[1024];
__at(0x1400) u8 buffer1[1024];

This is perfectly fine (as far as data organisation in the program goes), as those buffers will always be there, so they may as well be fixed in place!
« Last Edit: 03:05, 15 August 17 by ervin »
My (cancelled) entry for the CPCRetroDev 2017 Competition http://www.cpcwiki.eu/forum/programming/my-cpcretrodev-2017-entry/
FAST line drawing in CPCtelera http://www.cpcwiki.eu/forum/programming/drawing-lines-with-cpctelera-sdcc/
RUNCPC My entry for the CPCRetroDev 2015 Competition http://www.cpc-power.com/index.php?page=detail&num=12494

Offline Docent

  • CPC6128
  • ****
  • Posts: 166
  • Country: pl
  • Liked: 107
  • Likes Given: 0
Re: [SDCC] weird memory problem
« Reply #2 on: 23:45, 15 August 17 »
Can you post some example c source and object code that crashes?

Offline rpalmer

  • 6128 Plus
  • ******
  • Posts: 552
  • Country: au
  • Liked: 351
  • Likes Given: 18
Re: [SDCC] weird memory problem
« Reply #3 on: 01:32, 16 August 17 »
Are you indexing beyond the arrays?

This will corrupt anything after them which may be code.

Offline ervin

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.325
  • Country: au
    • index.php?action=treasury
  • Liked: 987
  • Likes Given: 1153
Re: [SDCC] weird memory problem
« Reply #4 on: 02:23, 16 August 17 »
Can you post some example c source and object code that crashes?

If I have time I'll try to put together a small example program that behaves the same way.
 :)
My (cancelled) entry for the CPCRetroDev 2017 Competition http://www.cpcwiki.eu/forum/programming/my-cpcretrodev-2017-entry/
FAST line drawing in CPCtelera http://www.cpcwiki.eu/forum/programming/drawing-lines-with-cpctelera-sdcc/
RUNCPC My entry for the CPCRetroDev 2015 Competition http://www.cpc-power.com/index.php?page=detail&num=12494

Offline ervin

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.325
  • Country: au
    • index.php?action=treasury
  • Liked: 987
  • Likes Given: 1153
Re: [SDCC] weird memory problem
« Reply #5 on: 02:24, 16 August 17 »
Are you indexing beyond the arrays?

This will corrupt anything after them which may be code.

I don't think I am, but I'll double check.
I've got tables being referenced by fields in other tables, so... maybe?
My (cancelled) entry for the CPCRetroDev 2017 Competition http://www.cpcwiki.eu/forum/programming/my-cpcretrodev-2017-entry/
FAST line drawing in CPCtelera http://www.cpcwiki.eu/forum/programming/drawing-lines-with-cpctelera-sdcc/
RUNCPC My entry for the CPCRetroDev 2015 Competition http://www.cpc-power.com/index.php?page=detail&num=12494

Offline ervin

  • Supporter
  • 6128 Plus
  • *
  • Posts: 1.325
  • Country: au
    • index.php?action=treasury
  • Liked: 987
  • Likes Given: 1153
Re: [SDCC] weird memory problem
« Reply #6 on: 04:31, 16 August 17 »
Interesting... I *must* have been using an out-of-bounds index, because now I can't reproduce the problem.
Even when I take out the fixed location definitions for buffer0 and buffer1, the program runs correctly.
Nonetheless, it makes sense for them to stay at their fixed locations.
« Last Edit: 07:28, 16 August 17 by ervin »
My (cancelled) entry for the CPCRetroDev 2017 Competition http://www.cpcwiki.eu/forum/programming/my-cpcretrodev-2017-entry/
FAST line drawing in CPCtelera http://www.cpcwiki.eu/forum/programming/drawing-lines-with-cpctelera-sdcc/
RUNCPC My entry for the CPCRetroDev 2015 Competition http://www.cpc-power.com/index.php?page=detail&num=12494