dos33fsprogs/graphics/gr/lines/lines_small.s

227 lines
2.8 KiB
ArmAsm
Raw Normal View History

2021-03-27 20:31:52 +00:00
; Bresenham Lines
; by Vince `deater` Weaver <vince@deater.net>
2021-03-28 03:13:17 +00:00
; based on pseudo-code from
; https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
2021-03-27 20:31:52 +00:00
.include "zp.inc"
.include "hardware.inc"
2021-03-28 03:13:17 +00:00
; 174 -- initial
; 170 -- inline init
; 166 -- inline step
; 161 -- merge absolute_value/negative code
; 159 -- merging init X/Y paths
; 156 -- more merging X/Y init
2021-03-28 03:43:31 +00:00
; 154 -- note PLOT doesn't touch Y
; 152 -- merge into common_inc
; 151 -- share an RTS
2021-03-28 04:27:06 +00:00
; 150 -- use X when plotting
2021-03-28 22:30:50 +00:00
; 148 -- re-arrange do abs (thanks to 42BS)
2021-03-27 20:31:52 +00:00
B_X1 = $F0
B_Y1 = $F1
B_X2 = $F2
B_Y2 = $F3
B_DX = $F4
B_DY = $F5
B_SX = $F6
B_SY = $F7
B_ERR = $F8
COUNT = $F9
lines:
jsr SETGR ; set lo-res 40x40 mode
; A=$D0 afterward
2021-03-28 04:27:06 +00:00
restart:
2021-03-27 20:31:52 +00:00
lda #0
sta COUNT
lines_loop:
jsr NEXTCOL
lda #0
sta B_X1
2021-03-28 03:13:17 +00:00
lda #36
2021-03-27 20:31:52 +00:00
sta B_Y2
lda COUNT
cmp #10
2021-03-28 04:27:06 +00:00
;end:
beq restart
2021-03-27 20:31:52 +00:00
asl
asl
sta B_Y1
sta B_X2
jsr draw_line
inc COUNT
jmp lines_loop
;============================
; draw line
; from x1,y1 to x2,y2
;============================
draw_line:
; from wikipedia
; dx = abs(x2 - x1)
; sx = x1 < x2 ? 1 : -1
; dy = -abs(y2 - y1)
; sy = y1 < y2 ? 1 : -1
; err = dx+dy
init_bresenham:
; dx = abs(x2-x1)
; sx = x1<x2 ? 1 : -1
2021-03-28 03:13:17 +00:00
ldx #0
jsr do_abs
; dy = -abs(y2-y1)
; sy = y1 < y2 ? 1 : -1
2021-03-27 20:31:52 +00:00
inx
2021-03-28 03:13:17 +00:00
jsr do_abs
jsr neg ; dy = -abs(y2-y1)
2021-03-27 20:31:52 +00:00
2021-03-28 03:13:17 +00:00
; err = dx+dy
; B_DY is in A already
clc
adc B_DX
sta B_ERR
2021-03-27 20:31:52 +00:00
2021-03-28 03:43:31 +00:00
;======================
; iterative plot points
2021-03-27 20:31:52 +00:00
line_loop:
2021-03-28 03:13:17 +00:00
; plot X1,Y1
2021-03-27 20:31:52 +00:00
ldy B_X1
2021-03-28 04:27:06 +00:00
ldx B_Y1
txa
2021-03-28 03:43:31 +00:00
jsr PLOT ; PLOT AT Y,A (A colors output, Y preserved)
2021-03-27 20:31:52 +00:00
2021-03-28 03:13:17 +00:00
; check if hit end
2021-03-28 03:43:31 +00:00
; ldy B_X1
2021-03-27 20:31:52 +00:00
cpy B_X2
bne line_no_end
2021-03-28 04:27:06 +00:00
; lda B_Y1
cpx B_Y2
2021-03-27 20:31:52 +00:00
beq done_line
line_no_end:
;========================
2021-03-28 04:27:06 +00:00
; step bresenham
2021-03-27 20:31:52 +00:00
;========================
2021-03-28 03:13:17 +00:00
; err2 = 2*err
2021-03-27 20:31:52 +00:00
lda B_ERR
2021-03-28 03:13:17 +00:00
asl
2021-03-28 04:27:06 +00:00
pha ; save err2 for later
ldx #0 ; setup for common_inc
2021-03-27 20:31:52 +00:00
2021-03-28 03:13:17 +00:00
; if err2 >= dy:
; err = err + dy
; x1 = x1 + sx
2021-03-28 03:43:31 +00:00
2021-03-28 04:27:06 +00:00
; B_ERR already in A
cmp B_DY ; check equal first
2021-03-28 03:13:17 +00:00
beq do_x
2021-03-28 04:27:06 +00:00
clc ; signed compare
2021-03-27 20:31:52 +00:00
sbc B_DY
2021-03-28 03:13:17 +00:00
bvc blah2
eor #$80
blah2:
bmi skip_x ; ble
do_x:
2021-03-28 03:43:31 +00:00
lda B_DY
jsr common_inc
2021-03-27 20:31:52 +00:00
skip_x:
2021-03-28 03:13:17 +00:00
2021-03-28 03:43:31 +00:00
2021-03-28 04:27:06 +00:00
inx ; setup common inc
2021-03-28 03:13:17 +00:00
; if err2 <= dx:
2021-03-27 20:31:52 +00:00
; err = err + dx
2021-03-28 03:13:17 +00:00
; y1 = y1 + sy
2021-03-28 04:27:06 +00:00
pla ; restore err2
2021-03-28 03:13:17 +00:00
clc ; signed compare
sbc B_DX
bvc blah
eor #$80
blah:
2021-03-27 20:31:52 +00:00
bpl skip_y
2021-03-28 03:13:17 +00:00
2021-03-28 03:43:31 +00:00
do_y:
lda B_DX
jsr common_inc
2021-03-27 20:31:52 +00:00
skip_y:
2021-03-28 03:13:17 +00:00
jmp line_loop
2021-03-28 03:43:31 +00:00
;=====================================
; common increment
;=====================================
common_inc:
clc
adc B_ERR
sta B_ERR
lda B_X1,X
clc
adc B_SX,X
sta B_X1,X
2021-03-28 03:13:17 +00:00
done_line:
2021-03-27 20:31:52 +00:00
rts
2021-03-28 03:43:31 +00:00
;=====================================
; init, do the abs and sx calculations
; x=0, for X
; x=1, for Y
;=====================================
2021-03-28 03:13:17 +00:00
do_abs:
2021-03-28 22:30:50 +00:00
ldy #$ff
2021-03-28 03:13:17 +00:00
sec
lda B_X1,X
sbc B_X2,X ; A = x1 - x2
2021-03-27 20:31:52 +00:00
2021-03-28 22:30:50 +00:00
bpl is_pos
2021-03-28 03:13:17 +00:00
ldy #$1
2021-03-27 20:31:52 +00:00
neg:
eor #$ff
clc
adc #1
2021-03-28 22:30:50 +00:00
is_pos:
2021-03-28 03:13:17 +00:00
sty B_SX,X
sta B_DX,X
2021-03-27 20:31:52 +00:00
rts
2021-03-28 03:13:17 +00:00