nesdev.com
http://forums.nesdev.com/

Transfer between ram and structs of arrays using ca65 macros
http://forums.nesdev.com/viewtopic.php?f=2&t=16311
Page 1 of 1

Author:  GradualGames [ Sun Aug 06, 2017 8:01 am ]
Post subject:  Transfer between ram and structs of arrays using ca65 macros

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:
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:
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:

.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.


Author:  thefox [ Sun Aug 06, 2017 9:15 am ]
Post subject:  Re: Transfer between ram and structs of arrays using ca65 ma

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.

Author:  GradualGames [ Sun Aug 06, 2017 9:49 am ]
Post subject:  Re: Transfer between ram and structs of arrays using ca65 ma

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.

Author:  GradualGames [ Mon Aug 07, 2017 10:27 am ]
Post subject:  Re: Transfer between ram and structs of arrays using ca65 ma

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)

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/