Optimizing RAM with CC65/CL65 for Retro Game Development

CC65/CL65 Ram Optimization - Retro Game Coders

Sure! Here’s a rewritten version of your content:

While working on enhancing my Rogue-Like game, I hit memory limitations once again.


Incorporating new features into my Rogue-Like game brought me face-to-face with memory limitations once more.

The first feature is a “field of view” (commonly referred to as “Fog of War”), which restricts visibility on the screen.

The second related feature is an automatic spell that reveals the entire screen.

To support these features, I had to develop a basic “Pythag” routine to calculate the distance between the player and enemies for their movement and attack actions. I last implemented such math when designing a website for a brewery that needed a “find my nearest” bar/restaurant feature!

It’s not surprising that I’m hitting RAM limits for two main reasons:

  1. I’m targeting a platform with only 32KB of total RAM.
  2. My code is high-level and carries extra overhead for multi-platform retro coding, whereas similar games typically relied on highly efficient assembly language.

Nonetheless, I aim to retain as many interesting features as possible, and since I’m not finished yet, I wonder if I can reduce some compilation bloat.

Yes, I can!

CCL65 provides command-line parameters that can assist us. However, it’s vital to keep in mind a general principle for older 8-bit systems.

Examine Your Zero Page and BSS Usage

BSS (Block Started by Symbol) variables—global and static variables—consume RAM, so it’s wise to limit their use.

Zero-page variables are quicker to access but also utilize very limited memory (the initial page, as the name implies).

One target platform is CP/M, which prefers global variables.

Consider minimizing global/static variable use or strategically placing frequently accessed ones in zero-page memory.

Optimize for Size (-O flag)

Make use of the -O or -Os flags for space optimization:

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

Disable Runtime Type Information

If runtime type checking isn’t necessary, defining this can conserve RAM:

-DCC65_NO_RUNTIME_TYPE_CHECKS

Optimize Stack Usage

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

--static-locals
This moves local variables to static storage, reducing stack usage but potentially increasing global RAM utilization.

Use –codesize to Prioritize Code Over RAM

--codesize 100
This focuses optimization on reducing code size, which may indirectly aid RAM usage.

Memory Configuration Tweaks (.cfg file)

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

Reduce Heap Size

If dynamic memory allocation isn’t utilized, consider reducing or eliminating the heap section.

Reduce Stack Size

If your program doesn’t employ deep recursion or large automatic variables, you can decrease the stack size.


Original article by retrogamecoders.com

Main Menu