Page 1 of 1

Rules of Thumb for Memory Management on the C64?

Posted: Sun Dec 30, 2018 4:00 pm
by load81
Learning 6502 assembly really is a slow process, but I'm enjoying it. I'm still playing with string output and changing boarder, background, and text colors and generally getting to know my assembler of choice (64tass) as I do.

While messing with strings it seems I habitually put my executable code right up against my text data with no extra space to move around. While space efficient, this seems "wrong" somehow. Add to that the fact that I'm going to be messing with sprites soon and I'm really starting to wonder... what are some rules of thumb for managing memory? Specifically, I'm looking for traditional locations for placing strings, sprite data, SID audio, etc? Also, any source-level conventions I should know (like, "always call Zero Page 'ZP.'") that I should know would be handy, too.

Also, are there any good/simple 64tass sources out there I can learn from? I've found some 6502 assembly code on GitHub, but it's almost always ACME which I've just never taken a liking to.

Re: Rules of Thumb for Memory Management on the C64?

Posted: Sun Dec 30, 2018 4:40 pm
by White Flame
The primary constraints of where to put things on the C64 come from the video banking. Since the built-in font ROM is always visible to the video chip in 2 locations, the banking tends to be outside those if you're running custom graphics. Other than that, it comes to contiguous space for variable use, which is broken up a bit by the I/O page at $d000.

Probably the most handy tip is to parallelize arrays. So for instance if you have an array of 16-bit pointers and an 8-bit data point per entry, instead of trying to multiply by 3 to find offsets, use 3 separate tables for each of the entries:

Code: Select all

 ldx entryNum
 lda tableLo,x
 sta ptr
 lda tableHi,x
 sta ptr+1
 lda tableData,x
 jsr routine

; 256 entries in the table, each "entry" is effectively 3 bytes
tableLo:  <256 bytes of space>
tableHi:  <256 bytes of space>
tableData:  <256 bytes of space>
The fastest and easiest is to use fixed-size, fixed location buffers for everything, ensuring 8-bit offsets are always used. Of course, this might not be as flexible as a full malloc-style heap, but shuffling around pointers & doing pointer math is not the 6502's strong suit anyway. If there is dynamic memory allocation, it's usually of the same type of structure out of a pool array.

Re: Rules of Thumb for Memory Management on the C64?

Posted: Fri Jan 04, 2019 3:19 am
by JimBoyd
load81 wrote:
While messing with strings it seems I habitually put my executable code right up against my text data with no extra space to move around. While space efficient, this seems "wrong" somehow.
What's wrong with that?
As long as the text data is static (it doesn't change) it will be fine.

Re: Rules of Thumb for Memory Management on the C64?

Posted: Fri Jan 04, 2019 1:36 pm
by cbmeeks
About six years ago I started a little C64 tutorial repo that I never really finished. In it, I have some suggestions on memory layout that was used by a well respected C64 games programmer.

Might be useful...

https://github.com/cbmeeks/breadbin-leg ... igurations

https://github.com/cbmeeks/breadbin-legacy/wiki