hgr: lines: working on hgr lines

This commit is contained in:
Vince Weaver 2021-06-30 23:33:04 -04:00
parent 76275d1ee7
commit fef858fa6f
5 changed files with 447 additions and 1 deletions

View File

@ -0,0 +1,48 @@
include ../../../Makefile.inc
DOS33 = ../../../utils/dos33fs-utils/dos33
TOKENIZE = ../../../utils/asoft_basic-utils/tokenize_asoft
LINKER_SCRIPTS = ../../../linker_scripts
EMPTY_DISK = ../../../empty_disk
all: lines.dsk
lines.dsk: HELLO LINES LINES_ROM
cp $(EMPTY_DISK)/empty.dsk lines.dsk
$(DOS33) -y lines.dsk SAVE A HELLO
$(DOS33) -y lines.dsk BSAVE -a 0x0C00 LINES
$(DOS33) -y lines.dsk BSAVE -a 0x0C00 LINES_ROM
###
HELLO: hello.bas
$(TOKENIZE) < hello.bas > HELLO
###
TRIANGLE.BAS: triangle.bas
$(TOKENIZE) < triangle.bas > TRIANGLE.BAS
###
LINES: lines.o
ld65 -o LINES lines.o -C $(LINKER_SCRIPTS)/apple2_c00.inc
lines.o: lines.s
ca65 -o lines.o lines.s -l lines.lst
###
LINES_ROM: lines_rom.o
ld65 -o LINES_ROM lines_rom.o -C $(LINKER_SCRIPTS)/apple2_c00.inc
lines_rom.o: lines_rom.s
ca65 -o lines_rom.o lines_rom.s -l lines_rom.lst
###
clean:
rm -f *~ *.o *.lst HELLO LINES LINES_ROM

View File

@ -0,0 +1,2 @@
5 HOME
10 PRINT CHR$(4);"CATALOG"

251
graphics/hgr/lines/lines.s Normal file
View File

@ -0,0 +1,251 @@
; Hi-res Bresenham Lines
; by Vince `deater` Weaver <vince@deater.net>
; based on pseudo-code from
; https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
; D0+ used by HGR routines
HGR_COLOR = $E4
HGR_PAGE = $E6
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
FRAME = $FF
; soft-switches
KEYPRESS = $C000
KEYRESET = $C010
; ROM routines
HGR2 = $F3D8 ; set hires page2 and clear $4000-$5fff
HGR = $F3E2 ; set hires page1 and clear $2000-$3fff
HPLOT0 = $F457 ; plot at (Y,X), (A)
HCOLOR1 = $F6F0 ; set HGR_COLOR to value in X
COLORTBL = $F6F6
PLOT = $F800 ; PLOT AT Y,A (A colors output, Y preserved)
NEXTCOL = $F85F ; COLOR=COLOR+3
SETCOL = $F864 ; COLOR=A
SETGR = $FB40 ; set graphics and clear LO-RES screen
BELL2 = $FBE4
WAIT = $FCA8 ; delay 1/2(26+27A+5A^2) us
lines:
jsr HGR2
restart:
lda #0
sta COUNT
lines_loop:
lda FRAME
and #$7
; lda #$7f
; sta HGR_COLOR
lda #0
sta B_X1
lda #36
sta B_Y2
lda COUNT
cmp #10
;end:
beq restart
asl
asl
sta B_Y1
sta B_X2
jsr draw_line
inc COUNT
inc FRAME
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
ldx #0
jsr do_abs
; dy = -abs(y2-y1)
; sy = y1 < y2 ? 1 : -1
inx
jsr do_abs
jsr neg ; dy = -abs(y2-y1)
; err = dx+dy
; B_DY is in A already
clc
adc B_DX
sta B_ERR
;======================
; iterative plot points
line_loop:
; tya
; pha
; txa
; pha
; hplot X1,Y1
ldx B_X1
lda B_Y1
ldy #0
jsr HPLOT0 ; plot at (Y,X), (A)
; pla
; tax
; pla
; tay
; check if hit end
ldy B_X1
cpy B_X2
bne line_no_end
lda B_Y1
cmp B_Y2
beq done_line
line_no_end:
;========================
; step bresenham
;========================
; err2 = 2*err
lda B_ERR
asl
pha ; save err2 for later
ldx #0 ; setup for common_inc
; if err2 >= dy:
; err = err + dy
; x1 = x1 + sx
; B_ERR already in A
cmp B_DY ; check equal first
beq do_x
clc ; signed compare
sbc B_DY
bvc blah2
eor #$80
blah2:
bmi skip_x ; ble
do_x:
lda B_DY
jsr common_inc
skip_x:
inx ; setup common inc
; if err2 <= dx:
; err = err + dx
; y1 = y1 + sy
pla ; restore err2
clc ; signed compare
sbc B_DX
bvc blah
eor #$80
blah:
bpl skip_y
do_y:
lda B_DX
jsr common_inc
skip_y:
jmp line_loop
;=====================================
; common increment
;=====================================
common_inc:
clc
adc B_ERR
sta B_ERR
lda B_X1,X
clc
adc B_SX,X
sta B_X1,X
done_line:
rts
;=====================================
; init, do the abs and sx calculations
; x=0, for X
; x=1, for Y
;=====================================
do_abs:
ldy #$ff
sec
lda B_X1,X
sbc B_X2,X ; A = x1 - x2
bpl is_pos
ldy #$1
neg:
eor #$ff
clc
adc #1
is_pos:
sty B_SX,X
sta B_DX,X
rts

View File

@ -0,0 +1,145 @@
; ROM lines example, to use as comparison for the bresenham version
; by Vince `deater` Weaver <vince@deater.net>
; D0+ used by HGR routines
HGR_COLOR = $E4
HGR_PAGE = $E6
B_X1_L = $F0
B_X1_H = $F1
B_Y1 = $F2
B_X2_L = $F3
B_X2_H = $F4
B_Y2 = $F5
COUNT = $F9
FRAME = $FF
; soft-switches
KEYPRESS = $C000
KEYRESET = $C010
; ROM routines
HGR2 = $F3D8 ; set hires page2 and clear $4000-$5fff
HGR = $F3E2 ; set hires page1 and clear $2000-$3fff
HPLOT0 = $F457 ; plot at (Y,X), (A)
HGLIN = $F53A ; line to (X,A),(Y)
HCOLOR1 = $F6F0 ; set HGR_COLOR to value in X
COLORTBL = $F6F6
PLOT = $F800 ; PLOT AT Y,A (A colors output, Y preserved)
NEXTCOL = $F85F ; COLOR=COLOR+3
SETCOL = $F864 ; COLOR=A
SETGR = $FB40 ; set graphics and clear LO-RES screen
BELL2 = $FBE4
WAIT = $FCA8 ; delay 1/2(26+27A+5A^2) us
lines:
jsr HGR2
reset:
ldx #0
stx COUNT
stx B_X1_H
stx B_X1_L
stx B_Y1
stx B_X2_H
stx B_X2_L
lda #191
sta B_Y2
left_lines_loop:
jsr draw_line
lda B_Y1
clc
adc #8
sta B_Y1
lda B_X2_L ; 280/24 = 140/12 = 70/6 = 11
clc
adc #11
sta B_X2_L
bcc noc
inc B_X2_H
noc:
inc COUNT
inc FRAME
lda COUNT
cmp #24
end:
bne left_lines_loop
;================
; right lines
;================
; at this point, count = 24
; X1, Y1 = 0,192
; X2, Y2 = 264,192
; want x1,y1 to go from 264,0 to 0,0
; want x2,y2 to go from 264,192 to 264,0
ldx #0
stx B_Y1
ldx B_X2_L
stx B_X1_L
ldx B_X2_H
stx B_X1_H
right_lines_loop:
jsr draw_line
lda B_Y2
sec
sbc #8
sta B_Y2
lda B_X1_L
sec
sbc #11
sta B_X1_L
bcs noc2
dec B_X1_H
noc2:
dec COUNT
inc FRAME
lda COUNT
beq reset
bne right_lines_loop
;================
; draw line
draw_line:
lda FRAME
; and #$7
tax
; ldx #7
jsr HCOLOR1 ; set color
ldy B_X1_H
ldx B_X1_L
lda B_Y1
jsr HPLOT0 ; plot at (Y,X), (A)
ldx B_X2_H
lda B_X2_L
ldy B_Y2
jsr HGLIN ; line to (X,A), (Y)
rts

View File

@ -38,7 +38,7 @@ HPOSN = $F411 ; (Y,X),(A) (values stores in HGRX,XH,Y)
HPLOT0 = $F457 ; plot at (Y,X), (A)
COLOR_SHIFT = $F47E
HLINRL = $F530 ; (X,A),(Y)
HGLIN = $F53A ; line to (A,X),(Y)
HGLIN = $F53A ; line to (X,A),(Y)
COLORTBL = $F6F6