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

6502 coding style.
http://forums.nesdev.com/viewtopic.php?f=5&t=16887
Page 2 of 2

Author:  Oziphantom [ Wed Jan 03, 2018 12:09 am ]
Post subject:  Re: 6502 coding style.

Interesting to see how it rolls on the "other side" I talked about indenting on the C64 and got told "THERE IS NO TAB KEY ON 64 FOR A REASON"... however I replied with "THERE IS ON 128 - EVOLVE!"

I think a few more broad-line rules should be discussed ;)

My "ruless... well guidelines" are
Don't use magic numbers, make constants.
Make sure all constants start k(uppercase letter) this way my tools can catch when I forget to do a #
Make blocks for groupings, and put "classes" i.e code that is grouped together in its own block and section
If maths is repeated make a function that does it, i.e convert X,Y to a screen locations is
Code:
fCalcScreenAddrChar .function base,x,y
.endf base+(y*40)+x
Don't make magic bytes in order. Either make a structure and declare it as a struct instance or if small enough ( 1~2 simple params ) use build in functions to convert an AOS to SOA.
variables and data should be inlined wherever possible, or near by as much as possible.
Code:
.section ZP
    myVar .byte ?
.send ZP
myFunc
    lda myVar
    bpl _skip
        ldx #size(myData)-1
    -   lda myData,x
        .section Bank0Data
            myData .byte VIC.Colours.(black,red,green,yellow)
        .send Bank0Data
        sta VIC.Sprite.Colour0
        dex
        bpl -
_skip
    rts
Flags variables should be marked as such, if they are do thing if negative thingNF is positive thingPF. If zero thingZF, Not Zero thingNEF
If a branch targets an rts name it EXIT
If a branch targets a jmp name it bJ<place it jumps to>
After each code stop there is a blank line. So rts, rti, an always taken branch, or opposing branch pair
functions lowerCaseStart
Data UpperCaseStart
IF_DEFS all caps
Each bank should be in its own block so you have to enter BANK_XX.func if you call it from outside of the bank

Author:  hackfresh [ Wed Jan 03, 2018 1:36 pm ]
Post subject:  Re: 6502 coding style.

I like notepad++ for its

1. Code folding
2. Special highlighting for (ZP variables, macros, fixed bank functions) . This could be achieved with naming conventions going forward but in my reverse engineering it was easier to do it this way.


I only do one level of indenting but can see where it would look nice. I have lots of comments and as above try and get rid of as many "magic numbers" as possible

One small example:

Image

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