CC65/CL65 Ram Optimization

in #developers7 days ago

image.png

Adding more features while coding my Rogue-Like game meant I butted up against memory limitations again.

The two features were "field of view" (sometimes called "Fog of War), where you don't get to see the whole screen at once.

Related to the first new feature is the second which is a spell (currently automatically casts) that reveals the whole screen.

Due to the above, I needed to code a basic "Pythag" routine to see how close the player was to the enemies before making them move or attack. The last time I used that piece of math was back when I built a website for a brewery and they wanted a "find my nearest" restaurant/bar finder!

Now, this tipping me over the RAM limits is not very surprising for two reasons:

  1. I am coding with a target platform that only has 32KB of total RAM.
  2. My code is relatively high-level and has extra overhead for multi-platform retro coding whereas these sorts of games used to utilize highly efficient assembly.

Still, I want to make sure I lose as few neat features as possible, plus I am not close to finished, so can I claw back some bloat from the compiler itself.

Yes!

CCL65 has some command-line parameters that can help us out, but first there is a general rule of thumb with old 8 bit systems.

Check Your zero page and BSS Usage

BSS (Block Started by Symbol) variables, that is global and static variables, take up RAM, so it is best to minimize their use.

Zero-page variables are faster to access but take up even more limited memory (the first page as the name suggests).

Unfortunately, one of my targets is CP/M which prefers global variables.

Consider using fewer global/static variables or tactically placing frequently accessed ones in zeropage.

Now on to the compiler switches.

Optimize for Size (-O flag)

Try using -O or -Os to optimize for space:

cl65 -O
This applies basic optimizations that can help reduce RAM and code size.

Disable Runtime Type Information

If you don’t need runtime type checking, defining this can save RAM:

-DCC65_NO_RUNTIME_TYPE_CHECKS

Optimize Stack Usage

By default, cc65 places local variables on the stack. If you have functions with large local variables, you might be able to save stack space with:

--static-locals
This moves local variables to a static storage area, which can reduce stack usage but may increase global RAM usage.

Use --codesize to Prioritize Code Over RAM

--codesize 100
This shifts optimization toward reducing code size, which might indirectly help with RAM usage.

Memory Configuration Tweaks (.cfg file)

If you are using a custom memory configuration file (.cfg), you might be able to tweak it to save RAM:

Reduce Heap Size

If you're not using dynamic memory allocation, reduce or remove the heap section.

Reduce Stack Size

If your program doesn't use deep recursion or large automatic variables, you can lower the stack size.

Sort:  

I hate to think how much memory my programs use :) I've been there trying to squeeze the most out of a machine.