Transfer between ram and structs of arrays using ca65 macros

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Transfer between ram and structs of arrays using ca65 macros

Post by GradualGames »

These two macros, ins_lo and ins_hi, enable a series of higher level macros to be written which autodetect the parameters both for immediate, zp or absolute but also if they start with entity_ where they are then indexed by x or y. Makes it very simple to transfer between zp, ram, immediate and structures of arrays for entities using single lines like this:

Code: Select all

move16 #100, entity_x_velocity   ; automatically stores lo byte in entity_x_velocity_lo,x and hi byte in entity_x_velocity_hi,x
or say between "this" and "that" entity like this:

Code: Select all

move16 entity_x_velocity, entity_x_velocity, x, y  ;moves velocity of entity indexed by x to entity indexed by y, automatically using the _lo and _hi arrays for each

Code: Select all


.macro ins_lo reg, ins, arg, suf

    .local entity_lo
    .if !.xmatch (.left (1, {arg}), #)
        .if .xmatch (.left (1, .ident(.sprintf("%.7s", .string(arg)))), entity_)
            .ifnblank suf
                entity_lo = .ident(.concat(.string(arg), suf))
            .else
                entity_lo = arg
            .endif
            .ifnblank reg
                ins entity_lo,reg
            .else
                ins entity_lo,x
            .endif
        .else
            ins arg
        .endif
    .else
        ins #<(.right (.tcount ({arg})-1, {arg}))
    .endif

.endmacro

.macro ins_hi reg, ins, arg

    .local entity_hi
    .if !.xmatch (.left (1, {arg}), #)
        .if .xmatch (.left (1, .ident(.sprintf("%.7s", .string(arg)))), entity_)
             entity_hi = .ident(.concat(.string(arg), "_hi"))
            .ifnblank reg
                ins entity_hi,reg
            .else
                ins entity_hi,x
            .endif
        .else
            ins arg+1
        .endif
    .else
        ins #>(.right (.tcount ({arg})-1, {arg}))
    .endif

.endmacro

.macro move16 source, dest, rega, regb

    ins_lo rega, lda, source, "_lo"
    ins_lo regb, sta, dest, "_lo"
    ins_hi rega, lda, source
    ins_hi regb, sta, dest

.endmacro

;...a whole series of other possible 16 bit and 8 bit macros using ins_lo and ins_hi follows in my codebase, you can easily imagine how they would look based on move16.

Last edited by GradualGames on Mon Aug 07, 2017 10:31 am, edited 3 times in total.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Transfer between ram and structs of arrays using ca65 ma

Post by thefox »

GradualGames wrote:*edit* Much like the rubber duckie technique, posting this has made me scrutinize these macros. I'm trying to get rid of that .scope ... .endscope in my ins_lo and ins_hi macros, because I think it's causing ca65 to emit absolute addressing instead of zp addressing in some cases.
If you're using the scope to make entity_lo/entity_hi local to the macro, you can get the same effect with .local.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: Transfer between ram and structs of arrays using ca65 ma

Post by GradualGames »

thefox wrote:
GradualGames wrote:*edit* Much like the rubber duckie technique, posting this has made me scrutinize these macros. I'm trying to get rid of that .scope ... .endscope in my ins_lo and ins_hi macros, because I think it's causing ca65 to emit absolute addressing instead of zp addressing in some cases.
If you're using the scope to make entity_lo/entity_hi local to the macro, you can get the same effect with .local.
Cool...works, but interestingly I'm still getting absolute addressing in some cases, see my new post. I may have been incorrect in thinking the .scope was what was causing ca65 to emit absolute addressing.
User avatar
GradualGames
Posts: 1106
Joined: Sun Nov 09, 2008 9:18 pm
Location: Pennsylvania, USA
Contact:

Re: Transfer between ram and structs of arrays using ca65 ma

Post by GradualGames »

Updated OP with dramatically cleaner versions of ins_lo and ins_hi. The other above mentioned issues are gone now (no .scope... .endscope still remain in the macros so they shouldn't introduce address size change problems where they didn't already exist)
Post Reply