6502 coding style.

You can talk about almost anything that you want to on this board.

Moderator: Moderators

Oziphantom
Posts: 1565
Joined: Tue Feb 07, 2017 2:03 am

Re: 6502 coding style.

Post by Oziphantom »

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

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

.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
hackfresh
Posts: 101
Joined: Sun May 03, 2015 8:19 pm

Re: 6502 coding style.

Post by hackfresh »

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
Post Reply