Self modifying code
Posted: Sun Mar 02, 2008 4:06 am
I've been writing a ton of self modifying code for optimizations on my latest project. Probably the largest amount I've written in a single project. The combinations of LUTs and self modifying code really makes for some really fast code. I did a search and didn't find a topic specifically on this. So... what are some of the crazy examples you guys have done with self modifying code?
Here are a one from the recent project...
for a sample frequency divider:
The routine uses a float point incrementor for the frequency scaler. Ranges from .01 to 1.99. The destination buffer is 262 bytes (matching 262 scanlines
). The float point value is taken form a 100hz increment look up table (frequency_request/100). Output ranges from 0khz to 32khz on a fixed 15.7khz output.
Here are a one from the recent project...
for a sample frequency divider:
Code: Select all
lda #$7f
sta sm_lp_cntr+1
lda #$02
pha
cly
clx
__main_lp:
clc
sm_src:
lda $0000,x ;soure/destination setup/written to before routine call
sm_dst:
sta $0000,y ;destination address always starts off with LSB of 00
sm_freq_flt_cntr: ;8bit floating point counter
lda #$00
sm_freq_flt_add: ;8bit float incrementor
adc #$00 ;no need for CLC since CPY clears it
sta sm_freq_flt_cntr+1
bcc .skip
inx
.skip
sm_freq_chr_add: ;the whole # part of the incrementor
inx ;either INX or NOP
iny
sm_lp_cntr:
cpy #$00 ;initialized as #$7f
bcc sm_src
txa
clc
adc sm_src+1
sta sm_src+1
bcc .skip
inc sm_src+2
.skip
clx
pla
dec a
beq .out
cmp #$01
bcs .upper_7f
.last_eight
pha
lda #$08
sta sm_lp_cntr+1
cly
inc sm_dst+1
bcc .skip2
inc sm_dst+2
.skip2
bra __main_lp
.upper_7f
pha
stz sm_lp_cntr+1 ;don't clear Y
bra __main_lp
.outThe routine uses a float point incrementor for the frequency scaler. Ranges from .01 to 1.99. The destination buffer is 262 bytes (matching 262 scanlines