C/++ for the SNES

Discussion of hardware and software development for Super NES and Super Famicom. See the SNESdev wiki for more information.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
psycopathicteen
Posts: 3140
Joined: Wed May 19, 2010 6:12 pm

Re: C/++ for the SNES

Post by psycopathicteen »

Most programmers write code like this when transitioning from 68k to 65xx:

Code: Select all


lda $00      ;; add $01 to $00
clc
adc $01
sta $00

lda $00      ;; add $02 to $00
clc
adc $02
sta $00

lda $1000    ;; move $1000 to the $03 so it can run faster
sta $03

lda $03      ;; add $00 to $03
clc
adc $00
sta $03

lda $03
sta $1000    ;; move $03 back to $1000
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: C/++ for the SNES

Post by tepples »

To teach a smug 68000 weenie how to make a 6502 scream, you might have to show him a few peephole optimizations one at a time.

Remove loads after stores:

Code: Select all

lda $00
clc
adc $01
sta $00      ;; remove lda $00 after store
clc
adc $02
sta $00
lda $1000    ;; move $1000 to the $03 so it can run faster
sta $03      ;; remove lda $03 after store
clc
adc $00
sta $03
sta $1000
Remove stores whose value is provably unused:

Code: Select all

lda $00
clc
adc $01
clc          ;; remove unused sta $00
adc $02
sta $00
lda $1000
clc
adc $00
sta $03
sta $1000
Addition of this type is commutative:

Code: Select all

lda $00
clc
adc $01
clc
adc $02
sta $00
lda $00      ;; group accesses to same address
clc
adc $1000
sta $03
sta $1000
Which allows removing another load after store:

Code: Select all

lda $00
clc
adc $01
clc
adc $02
sta $00      ;; remove lda $00 after store
clc
adc $1000
sta $03
sta $1000
Thus this section of code is provably equivalent yet small enough for repeating unused store analysis with $00 and $03 in the rest of the snippet. If it turns out they're not needed, you end up with perfectly idiomatic 6502 assembly:

Code: Select all

lda $00
clc
adc $01
clc
adc $02      ;; remove unused sta $00
clc
adc $1000    ;; remove unused sta $03
sta $1000
You already know all this, but the process illustrated in this example might help someone else adapt to the 6502. Each step might produce its own "Oh!" moment.

(And now this is used as an example on wiki.superfamicom.org.)
Sik
Posts: 1589
Joined: Thu Aug 12, 2010 3:43 am

Re: C/++ for the SNES

Post by Sik »

If you want to make a 68000 programmer suffer, go with Z80 instead. Not too few registers to result in a mentality change, but not enough registers to be comfortable. Enjoy watching how heavily that stack gets hammered.
psycopathicteen
Posts: 3140
Joined: Wed May 19, 2010 6:12 pm

Re: C/++ for the SNES

Post by psycopathicteen »

Those are pretty much the kinds of "hardcore optimizations" that games like Space Megaforce use.
Post Reply