IF macro for ca65

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Bananmos
Posts: 471
Joined: Wed Mar 09, 2005 9:08 am
Contact:

Re: IF macro for ca65

Post by Bananmos » Sat Oct 20, 2012 6:34 pm

Thanks for the tip about .macro "functions"... they really help making my code look nicer as testing various state flags in the AI code is just an if CUSTOM_CONDITION :)

The and/or support you talked about in another thread sure sounded interesting, even though this is already very useful as it stands.

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: IF macro for ca65

Post by Movax12 » Sat Oct 20, 2012 9:28 pm

I feel I kind of rushed things a bit before, so I'm trying to get things polished up a bit more this time but I'm almost ready to release some more stuff after I write up some decent documentation too.

I'm using dopple's smbdis.asm to test the macros, so maybe I can share that as well.

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: IF macro for ca65

Post by Movax12 » Mon Oct 22, 2012 11:24 am

Still working on the documentation but I wanted to share this..

I'll document it better later, time to head to work..

http://mynesdev.blogspot.ca/2012/10/ca6 ... again.html

http://pastebin.com/azMMvh4r .. updated 1 times.

Documentation updated! I will add more but it explains most things well.

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: IF macro for ca65

Post by Movax12 » Sun Oct 28, 2012 11:28 am

Another post on this topic, I tried to explain a bit about how it is working, sort of in between technical and more explanative, I hope this proves useful to others: http://mynesdev.blogspot.ca/2012/10/hl- ... nical.html

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: IF macro for ca65

Post by Movax12 » Sun Dec 30, 2012 5:19 pm

I've decided to keep this code updated here: https://www.assembla.com/code/ca65hl/git/nodes
I'm also going to use the wiki on that page to document it's use, though there is not much there yet.

This version also includes integrated <, >, <>, >= <= operators (unsigned) as well as the assignment macro and assignment macro supported operators &, |, ^ ( bitwise) +, - (clear/set carry) --, ++ (with carry), >>, <<, *, / (times and divide also shift left or right with powers of two.)

It will also default to behaving as if any non zero result is true if no cpu flags are specified and (hopefully) it makes sense to do so.

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: IF macro for ca65

Post by Movax12 » Mon Oct 14, 2013 1:33 am

Announcing a new feature addition. Now featuring support for parentheses in expressions! Although I am confident I have everything working, please let me know if you find any mistakes. I would consider this untested, beta code.

https://www.assembla.com/code/ca65hl/git/nodes/devel

Following the same rules as the previous version, you can test 'expressions' that represent or result in a CPU flag being set/clear and branch appropriately. The latest code allows parentheses to create more complex logical conditions, as well as a negate operator that can distribute across parenthesis. The code generated aims to be efficient and will short circuit as soon as the outcome can be determined.

The only limitation is that all OR logic must be before all AND logic in the same parentheses set. ( Add parentheses to work around this.)

Example:

Code: Select all


    ; branch to label:
    * = 8000
    
    foo = $00
    bar = $01
    baz = $03
    myLabel = * + $50
    
    if ( ( foo >= #$03 && ! bar ) || ((zero && negative) || baz = #1)) goto myLabel

    ; code output:    
    8000   A5 00      LDA $00
    8002   C9 03      CMP #$03
    8004   90 04      BCC $800A
    8006   A5 01      LDA $01
    8008   F0 46      BEQ $8050
    800A   D0 02      BNE $800E
    800C   30 42      BMI $8050
    800E   A5 03      LDA $03
    8010   C9 01      CMP #$01
    8012   F0 3C      BEQ $8050

; if-endif ( else supported )

    * = 8000    
    
    foo = $00
    bar = $01
    baz = $03
    
    if (  !( bar & #$03 && baz == N set ) || (( V set || baz < #$10 ) || foo = #1) )
        nop
    endif

    ; code output:
    8000   A5 01      LDA $01
    8002   29 03      AND #$03
    8004   F0 12      BEQ $8018
    8006   A5 03      LDA $03
    8008   10 0E      BPL $8018
    800A   70 0C      BVS $8018
    800C   A5 03      LDA $03
    800E   C9 10      CMP #$10
    8010   90 06      BCC $8018
    8012   A5 00      LDA $00
    8014   C9 01      CMP #$01
    8016   D0 01      BNE $8019
    8018   EA         NOP
    8019

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: IF macro for ca65

Post by Movax12 » Tue Oct 22, 2013 5:38 pm

Quick note: Added for-next style loop. Nestable.

1)C-style of loop:
for ( <thing to do 1st>, <Flag to test - continue while true >, <stuff to do at end of loop> )

Example:

Code: Select all

for ( x := #$3, not negative, dex )
; code
next

; more complex:

for ( x := foo, (x > #10) && (foo <> #1), dex:dex)
    ; some code
next
2) BASIC-style syntax:

This actually tries to optimize some, and will break if you mess with your counter register or variable without thinking:

Code: Select all

for x := #$AA to #$10 step -3
    ;some code
next
It will loop until your counter hits the first invalid (for the loop) value. If possible it will skip CMP and use BPL/BMI or a lone BNE. 'step' can be omitted and a step of -1 or 1 decided on if possible. You can use variables rather than immediate values, but no optimizations are possible.

https://www.assembla.com/code/ca65hl/git/nodes/devel

Next is to fix 'elseif'.

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: IF macro for ca65

Post by Movax12 » Thu Oct 24, 2013 11:00 am

Nested 'elseif' seems to be working properly: (random nonsense code)

Image

teremochek
Posts: 12
Joined: Thu Mar 07, 2013 6:32 am

Re: IF macro for ca65

Post by teremochek » Fri Oct 25, 2013 11:06 pm

Looks interestingly. Will be great to watch video as everything works.

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: Please Help me Understand This Schematic Portion

Post by Movax12 » Sat Oct 26, 2013 6:29 am

Are you saying you would like to see a video? What specifically would you like to see in a video on this topic?

teremochek
Posts: 12
Joined: Thu Mar 07, 2013 6:32 am

Re: IF macro for ca65

Post by teremochek » Mon Oct 28, 2013 8:39 am

Movax12 wrote:What specifically would you like to see in a video on this topic?
Nothing specifically, just want to understand
How macro operators work. And how you can use operators in disassembler. This only works in Mario Disasm, or it can be used with any disassembler?
Anyway thanks.

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: IF macro for ca65

Post by Movax12 » Mon Oct 28, 2013 9:15 am

This works with the assembler (ca65). I'm really not sure where I would start with a video, or if this topic really lends itself well to a video. Perhaps someday I'll make a video of something. The wiki here explains how the macros work, but the latest information in this thread is not documented in the wiki just yet.

slobu
Posts: 275
Joined: Tue Jul 12, 2011 10:58 am

Re: IF macro for ca65

Post by slobu » Mon Oct 28, 2013 11:58 am

I still harbor fantasies of this becoming more like batari BASIC. I think "all" it would take is a canned kernel to update the screen and some functions for tiles, sprites, input and sound.

End users would still have to make sure not to use up too many cycles but that's life.

3gengames
Formerly 65024U
Posts: 2269
Joined: Sat Mar 27, 2010 12:57 pm

Re: IF macro for ca65

Post by 3gengames » Mon Oct 28, 2013 12:07 pm

But Atari, despite being harder to make stuff for, is sumpler in what it is needed to do and such. I don't think you'd be able to make anything scrolling/map-updating+sound+sprites with a little basic thingy. Maybe if you made it work on modules, that'd be cool to try out.

slobu
Posts: 275
Joined: Tue Jul 12, 2011 10:58 am

Re: IF macro for ca65

Post by slobu » Mon Oct 28, 2013 12:15 pm

3gengames wrote:But Atari, despite being harder to make stuff for, is sumpler in what it is needed to do and such. I don't think you'd be able to make anything scrolling/map-updating+sound+sprites with a little basic thingy. Maybe if you made it work on modules, that'd be cool to try out.
Yeah. batari BASIC does work in the way you just described. It has different canned kernels for a specific set of needs.

set kernel_options pfheights pfcolors
(use the canned kernel that has adjustable playfield heights & colors)

would give you a different game engine than

set kernel_options pfheights playercolors
(use kernel that allows adjustable playfield height and the first sprite can have multiple colors)


Likewise I'd imagine an NES version would have different kernels whether you want single screen games using no MMC or side-scrolling using MMC1.

Post Reply