2019-06-26 07:24:25 +00:00
|
|
|
// Find atan2(x, y) using the CORDIC method
|
|
|
|
// See http://bsvi.ru/uploads/CORDIC--_10EBA/cordic.pdf
|
|
|
|
.pc = $801 "Basic"
|
|
|
|
:BasicUpstart(main)
|
|
|
|
.pc = $80d "Program"
|
2019-06-28 07:34:02 +00:00
|
|
|
.label D018 = $d018
|
|
|
|
// Color Ram
|
|
|
|
.label COLS = $d800
|
2019-06-29 07:55:12 +00:00
|
|
|
// The number of iterations performed during 8-bit CORDIC atan2 calculation
|
|
|
|
.const CORDIC_ITERATIONS_8 = 8
|
2019-06-28 07:34:02 +00:00
|
|
|
.label CHARSET = $2000
|
|
|
|
.label SCREEN = $2800
|
2019-06-26 07:24:25 +00:00
|
|
|
main: {
|
2019-06-28 07:34:02 +00:00
|
|
|
.const toD0181_return = (>(SCREEN&$3fff)*4)|(>CHARSET)/4&$f
|
2019-07-31 11:47:21 +00:00
|
|
|
.label screen = 3
|
|
|
|
.label x = $a
|
|
|
|
.label y = 5
|
2019-06-28 07:34:02 +00:00
|
|
|
jsr init_font_hex
|
|
|
|
lda #toD0181_return
|
|
|
|
sta D018
|
|
|
|
lda #<SCREEN
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z screen
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #>SCREEN
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z screen+1
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #-$c
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z y
|
2019-06-26 07:24:25 +00:00
|
|
|
b1:
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #-$13
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z x
|
2019-06-26 07:24:25 +00:00
|
|
|
b2:
|
2019-06-29 07:55:12 +00:00
|
|
|
jsr atan2_8
|
2019-06-26 07:24:25 +00:00
|
|
|
txa
|
2019-06-28 07:34:02 +00:00
|
|
|
ldy #0
|
2019-06-26 07:24:25 +00:00
|
|
|
sta (screen),y
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z screen
|
2019-06-28 07:34:02 +00:00
|
|
|
bne !+
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z screen+1
|
2019-06-28 07:34:02 +00:00
|
|
|
!:
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z x
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #$15
|
2019-08-07 19:00:19 +00:00
|
|
|
cmp.z x
|
2019-06-26 07:24:25 +00:00
|
|
|
bne b2
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z y
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #$d
|
2019-08-07 19:00:19 +00:00
|
|
|
cmp.z y
|
2019-06-26 07:24:25 +00:00
|
|
|
bne b1
|
2019-06-28 07:34:02 +00:00
|
|
|
b4:
|
|
|
|
lda COLS+$c*$28+$13
|
|
|
|
clc
|
|
|
|
adc #1
|
|
|
|
sta COLS+$c*$28+$13
|
|
|
|
jmp b4
|
2019-06-26 07:24:25 +00:00
|
|
|
}
|
|
|
|
// Find the atan2(x, y) - which is the angle of the line from (0,0) to (x,y)
|
2019-07-03 21:23:39 +00:00
|
|
|
// Finding the angle requires a binary search using CORDIC_ITERATIONS_8
|
2019-06-29 07:55:12 +00:00
|
|
|
// Returns the angle in hex-degrees (0=0, 0x80=PI, 0x100=2*PI)
|
2019-07-31 11:47:21 +00:00
|
|
|
// atan2_8(signed byte zeropage($a) x, signed byte zeropage(5) y)
|
2019-06-29 07:55:12 +00:00
|
|
|
atan2_8: {
|
2019-07-31 11:47:21 +00:00
|
|
|
.label _7 = $b
|
|
|
|
.label xi = $b
|
|
|
|
.label xd = $c
|
|
|
|
.label angle = 2
|
|
|
|
.label i = $d
|
|
|
|
.label x = $a
|
|
|
|
.label y = 5
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z y
|
2019-06-26 07:24:25 +00:00
|
|
|
cmp #0
|
2019-06-28 07:34:02 +00:00
|
|
|
beq !+
|
|
|
|
bmi !b1+
|
|
|
|
jmp b1
|
|
|
|
!b1:
|
|
|
|
!:
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z y
|
2019-06-28 07:34:02 +00:00
|
|
|
eor #$ff
|
|
|
|
clc
|
|
|
|
adc #1
|
|
|
|
tax
|
2019-06-26 07:24:25 +00:00
|
|
|
b3:
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z x
|
2019-06-28 07:34:02 +00:00
|
|
|
cmp #0
|
|
|
|
beq !+
|
|
|
|
bmi !b4+
|
|
|
|
jmp b4
|
|
|
|
!b4:
|
|
|
|
!:
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z x
|
2019-06-28 07:34:02 +00:00
|
|
|
eor #$ff
|
|
|
|
clc
|
|
|
|
adc #1
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z _7
|
2019-06-28 07:34:02 +00:00
|
|
|
b6:
|
|
|
|
lda #0
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z angle
|
|
|
|
sta.z i
|
2019-06-28 07:34:02 +00:00
|
|
|
b10:
|
|
|
|
txa
|
|
|
|
cmp #0
|
|
|
|
bne b11
|
|
|
|
b12:
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z angle
|
2019-06-28 07:34:02 +00:00
|
|
|
lsr
|
|
|
|
tax
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z x
|
2019-06-28 07:34:02 +00:00
|
|
|
cmp #0
|
|
|
|
bpl b7
|
|
|
|
txa
|
|
|
|
eor #$ff
|
|
|
|
clc
|
|
|
|
adc #$80+1
|
|
|
|
tax
|
|
|
|
b7:
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z y
|
2019-06-28 07:34:02 +00:00
|
|
|
cmp #0
|
|
|
|
bpl b8
|
|
|
|
dex
|
|
|
|
txa
|
|
|
|
eor #$ff
|
|
|
|
tax
|
|
|
|
b8:
|
|
|
|
rts
|
|
|
|
b11:
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z xi
|
|
|
|
ldy.z i
|
2019-06-26 07:24:25 +00:00
|
|
|
cpy #0
|
|
|
|
beq !e+
|
|
|
|
!l:
|
|
|
|
cmp #$80
|
|
|
|
ror
|
|
|
|
dey
|
|
|
|
bne !l-
|
|
|
|
!e:
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z xd
|
|
|
|
ldy.z i
|
2019-06-28 07:34:02 +00:00
|
|
|
txa
|
2019-06-26 07:24:25 +00:00
|
|
|
cpy #0
|
|
|
|
beq !e+
|
|
|
|
!l:
|
|
|
|
cmp #$80
|
|
|
|
ror
|
|
|
|
dey
|
|
|
|
bne !l-
|
|
|
|
!e:
|
|
|
|
tay
|
2019-06-28 07:34:02 +00:00
|
|
|
txa
|
2019-06-26 07:24:25 +00:00
|
|
|
cmp #0
|
|
|
|
beq !+
|
2019-06-28 07:34:02 +00:00
|
|
|
bpl b13
|
2019-06-26 07:24:25 +00:00
|
|
|
!:
|
|
|
|
tya
|
|
|
|
eor #$ff
|
|
|
|
sec
|
2019-08-07 19:00:19 +00:00
|
|
|
adc.z xi
|
|
|
|
sta.z xi
|
2019-06-28 07:34:02 +00:00
|
|
|
txa
|
2019-06-26 07:24:25 +00:00
|
|
|
clc
|
2019-08-07 19:00:19 +00:00
|
|
|
adc.z xd
|
2019-06-26 07:24:25 +00:00
|
|
|
tax
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z angle
|
|
|
|
ldy.z i
|
2019-06-28 07:34:02 +00:00
|
|
|
sec
|
2019-06-29 07:55:12 +00:00
|
|
|
sbc CORDIC_ATAN2_ANGLES_8,y
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z angle
|
2019-06-28 07:34:02 +00:00
|
|
|
b14:
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z i
|
2019-06-29 08:08:46 +00:00
|
|
|
lda #CORDIC_ITERATIONS_8-1+1
|
2019-08-07 19:00:19 +00:00
|
|
|
cmp.z i
|
2019-06-28 07:34:02 +00:00
|
|
|
beq b12
|
|
|
|
jmp b10
|
|
|
|
b13:
|
2019-06-26 07:24:25 +00:00
|
|
|
tya
|
|
|
|
clc
|
2019-08-07 19:00:19 +00:00
|
|
|
adc.z xi
|
|
|
|
sta.z xi
|
2019-06-28 07:34:02 +00:00
|
|
|
txa
|
2019-06-26 07:24:25 +00:00
|
|
|
sec
|
2019-08-07 19:00:19 +00:00
|
|
|
sbc.z xd
|
2019-06-28 07:34:02 +00:00
|
|
|
tax
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z angle
|
|
|
|
ldy.z i
|
2019-06-28 07:34:02 +00:00
|
|
|
clc
|
2019-06-29 07:55:12 +00:00
|
|
|
adc CORDIC_ATAN2_ANGLES_8,y
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z angle
|
2019-06-28 07:34:02 +00:00
|
|
|
jmp b14
|
|
|
|
b4:
|
2019-08-07 19:00:19 +00:00
|
|
|
lda.z x
|
|
|
|
sta.z xi
|
2019-06-28 07:34:02 +00:00
|
|
|
jmp b6
|
|
|
|
b1:
|
2019-08-07 19:00:19 +00:00
|
|
|
ldx.z y
|
2019-06-28 07:34:02 +00:00
|
|
|
jmp b3
|
|
|
|
}
|
|
|
|
// Make charset from proto chars
|
2019-07-31 11:47:21 +00:00
|
|
|
// init_font_hex(byte* zeropage(6) charset)
|
2019-06-28 07:34:02 +00:00
|
|
|
init_font_hex: {
|
2019-07-31 11:47:21 +00:00
|
|
|
.label _0 = $d
|
|
|
|
.label idx = $b
|
|
|
|
.label proto_lo = 8
|
|
|
|
.label charset = 6
|
|
|
|
.label c1 = $a
|
|
|
|
.label proto_hi = 3
|
|
|
|
.label c = 5
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #0
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z c
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #<FONT_HEX_PROTO
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z proto_hi
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #>FONT_HEX_PROTO
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z proto_hi+1
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #<CHARSET
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z charset
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #>CHARSET
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z charset+1
|
2019-06-28 07:34:02 +00:00
|
|
|
b1:
|
|
|
|
lda #0
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z c1
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #<FONT_HEX_PROTO
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z proto_lo
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #>FONT_HEX_PROTO
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z proto_lo+1
|
2019-06-28 07:34:02 +00:00
|
|
|
b2:
|
|
|
|
lda #0
|
|
|
|
tay
|
|
|
|
sta (charset),y
|
|
|
|
lda #1
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z idx
|
2019-06-28 07:34:02 +00:00
|
|
|
ldx #0
|
|
|
|
b3:
|
|
|
|
txa
|
|
|
|
tay
|
|
|
|
lda (proto_hi),y
|
|
|
|
asl
|
|
|
|
asl
|
|
|
|
asl
|
|
|
|
asl
|
2019-08-07 19:00:19 +00:00
|
|
|
sta.z _0
|
2019-06-26 07:24:25 +00:00
|
|
|
txa
|
2019-06-28 07:34:02 +00:00
|
|
|
tay
|
|
|
|
lda (proto_lo),y
|
|
|
|
asl
|
2019-08-07 19:00:19 +00:00
|
|
|
ora.z _0
|
|
|
|
ldy.z idx
|
2019-06-28 07:34:02 +00:00
|
|
|
sta (charset),y
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z idx
|
2019-06-28 07:34:02 +00:00
|
|
|
inx
|
|
|
|
cpx #5
|
|
|
|
bne b3
|
|
|
|
lda #0
|
2019-08-07 19:00:19 +00:00
|
|
|
ldy.z idx
|
2019-06-28 07:34:02 +00:00
|
|
|
sta (charset),y
|
|
|
|
iny
|
|
|
|
sta (charset),y
|
|
|
|
lda #5
|
2019-06-26 07:24:25 +00:00
|
|
|
clc
|
2019-08-07 19:00:19 +00:00
|
|
|
adc.z proto_lo
|
|
|
|
sta.z proto_lo
|
2019-06-28 07:34:02 +00:00
|
|
|
bcc !+
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z proto_lo+1
|
2019-06-28 07:34:02 +00:00
|
|
|
!:
|
|
|
|
lda #8
|
|
|
|
clc
|
2019-08-07 19:00:19 +00:00
|
|
|
adc.z charset
|
|
|
|
sta.z charset
|
2019-06-28 07:34:02 +00:00
|
|
|
bcc !+
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z charset+1
|
2019-06-28 07:34:02 +00:00
|
|
|
!:
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z c1
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #$10
|
2019-08-07 19:00:19 +00:00
|
|
|
cmp.z c1
|
2019-06-28 07:34:02 +00:00
|
|
|
bne b2
|
|
|
|
lda #5
|
|
|
|
clc
|
2019-08-07 19:00:19 +00:00
|
|
|
adc.z proto_hi
|
|
|
|
sta.z proto_hi
|
2019-06-28 07:34:02 +00:00
|
|
|
bcc !+
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z proto_hi+1
|
2019-06-28 07:34:02 +00:00
|
|
|
!:
|
2019-08-07 19:00:19 +00:00
|
|
|
inc.z c
|
2019-06-28 07:34:02 +00:00
|
|
|
lda #$10
|
2019-08-07 19:00:19 +00:00
|
|
|
cmp.z c
|
2019-06-28 07:34:02 +00:00
|
|
|
bne b1
|
|
|
|
rts
|
2019-06-26 07:24:25 +00:00
|
|
|
}
|
2019-06-28 07:34:02 +00:00
|
|
|
// Bit patterns for symbols 0-f (3x5 pixels) used in font hex
|
|
|
|
FONT_HEX_PROTO: .byte 2, 5, 5, 5, 2, 6, 2, 2, 2, 7, 6, 1, 2, 4, 7, 6, 1, 2, 1, 6, 5, 5, 7, 1, 1, 7, 4, 6, 1, 6, 3, 4, 6, 5, 2, 7, 1, 1, 1, 1, 2, 5, 2, 5, 2, 2, 5, 3, 1, 1, 2, 5, 7, 5, 5, 6, 5, 6, 5, 6, 2, 5, 4, 5, 2, 6, 5, 5, 5, 6, 7, 4, 6, 4, 7, 7, 4, 6, 4, 4
|
2019-06-30 21:33:30 +00:00
|
|
|
// Angles representing ATAN(0.5), ATAN(0.25), ATAN(0.125), ...
|
|
|
|
CORDIC_ATAN2_ANGLES_8:
|
|
|
|
.fill CORDIC_ITERATIONS_8, 2*256*atan(1/pow(2,i))/PI/2
|
2019-06-26 07:24:25 +00:00
|
|
|
|