Using 16-bit address pointers in tables

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

Since the addressing range in CDFJ is 32K, we only need 16 bits to store pointers. In other words, pointers in C (which are 32 bit) have the high 16 bits zero.  When storing these pointers in tables (for example, a table of pointers to frames of sprite data), if you use a C structure then you end up with word values in the table (4 bytes per entry). This is frustrating because we know the high 16-bits are 0 and we really only want a HALF-word pointer (the low 16-bits).

In other words, each table entry only NEEDS to be 2 bytes in size, but C dumps 4 bytes.  There appears to be NO WAY at compile-time for C code to manipulate a 32 bit pointer value and "cast" it down to 16 bits.

unsigned char frame_00[] = {
 0,1,2,  // whatever data
};

unsigned char frame_01[] = {
 0,1,2,  // whatever data
};

unsigned char *frameTable[] = {
 frame_00,
 frame_01,
 // and so forth, up to hundreds of frames
};

The result, then, is that we waste 2 bytes for every frame. In Lemmings, my character set "frame table" has 317 entries at the moment. That's 634 bytes of waste. Ouch!

A way around this has been to embed the frame data in the 6502 code -- and pass the frame label ('frame_00') back to the C code. Or better yet, put the frame table of pointers in 6502, too. In assembler we can use 16-bit addresses in our reference tables, which is exactly what we want. But this is really fiddly and after a while becomes a bit of a pain to manage. And, another issue, we only have 4K of address space in the 6502 code, so there's only so much we can put there. What I/we really want is to be able to define our frames in C structures, access them that way, but also to have our frame table discard the high 16-bit of addresses in any tables pointing to those structures. ALL in the C code.

Although it seems to be impossible to have the C compiler do the cast for us (I've tried and tried and tried - many ways), we CAN embed (ARM) assembler code to do that for us in the C code, and define frameTable that way. We define it as an array of uint16_t (i.e., unsigned short) -- a short value, NOT a pointer!  So on usage, it needs to be cast back to an unsigned char pointer...

extern uint16_t frameLowTable[];  // put this in a header for usage elsewhere

// here's the table of pointers (16 bits per entry)
__asm__(".section .rodata\n"
        ".global frameLowTable\n"
        "frameLowTable:\n"
        ".hword frame_00\n"
        ".hword frame_01\n"

The one addition we have to make, to prevent the optimising linker from dumping the frame data itself, is to mark the frames as "used"...

__attribute__((used)) unsigned char frame_00[] = {
 0,1,2,  // whatever data
};

__attribute__((used)) unsigned char frame_01[] = {
 3,1,4,  // whatever data
};


And now we can use it. Let's say we're looking up frame 1...

// include the header that defines frameLowTable as an external

int frameNumber = 1;
unsigned char *fp = (unsigned char *) frameLowTable[frameNumber];   // IGNORE the compiler warning

// now we're pointing at the 32 bit address of the frame data
// e.g., in this example fp[2] == 4



Real-word example:  In Lemmings I have
96 animation frames (=192 bytes of 16-bit pointer saved)
317 characters in the charset (=634 bytes of 16-bit pointer saved)
Total savings: 826 bytes

JUST for changing to 16-bit pointers in char/sprite pointers.

Now if you're like me, you can also add a pragma to ignore the compiler warning.
"Treat warnings as errors" is my mantra. My code should compile with 0 warnings.

So, every access I wrap like this (to disable the warning)...

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
                unsigned char *fp = (unsigned char *)(frameLowTable[ch]);
#pragma GCC diagnostic pop

It may seem overkill (and you could do this globally, but don't!)... but having a clean compile with zero warnings is really critical to good programming practices; many is the time a warning actually tells you about something that's seriously wrong which you need to fix. I can't stress it enough - don't ignore warnings, and don't leave them unattended in your code!  You'll thank me later.

Andrew Davie

Back-porting to GridLock saved just over 900 bytes. (!!)

SpiceWare

Awesome!

I gave John Champeau a heads up so he's aware that you've figured this out.

I hope to be inspired enough to get back to work on Draconian Deluxe in January, after the holidays, and try this out myself.

Bomberman94

Please consider continue working on Draconian - I love this game and every new thing/twist/enhancement would be amazing!

SpiceWare

Quote from: Andrew Davie on 28 Oct 2025, 10:50 PManother issue, we only have 4K of address space in the 6502 code, so there's only so much we can put there.

We don't actually have the 4K limit when putting data into the 6502 source.  In Draconian my 6502 code is laid out like this:
$0000 = INCBIN driver
$0800 = INCBIN C code
        INCLUDE data in 6502 assembly file
        INCBIN audio samples
$689E = INCLUDE tables in 6502 assembly file - needs to be in a fixed location
$7000 = 4K of 6502 code for Draconian.

Extract from the source:
    ORG $0000
    ; Include CDF Driver
    INCBIN "cdfdriver_563.bin"

    ORG $0800
C_CODE:
    INCBIN "./main/bin/testarm.bin"

;===============================================================================
; Movable ARM data
;-------------------------------------------------------------------------------
;       ARM data that can move around, pointed to by a stationary table
;===============================================================================
    include "images.dood"
sample_alert:
    INCBIN "./samples/alert.pds"
   
sample_battlestations:
    INCBIN "./samples/battlestations.pds"
   
sample_blastoff:
    INCBIN "./samples/BLASTOffChipmunk.pds"

sample_conditionred:
    INCBIN "./samples/condred3.pds"

sample_spyship:
    INCBIN "./samples/SpyShipSighted.pds"

STATIONARY_START = $689E
    echo "----",[STATIONARY_START - *]d , "bytes free for ARM Code, samples, & graphics"

;===============================================================================
; Stationary ARM data
;-------------------------------------------------------------------------------
;       ARM data that shouldn't move when C code changes - tables, etc.
;===============================================================================
    ORG STATIONARY_START
    ; RORG STATIONARY_START
   
   include "stationary_data.asm"

    ORG $7000

; 6502 code in bank 6