Add better code coments

This commit is contained in:
hugova 2025-03-25 21:21:39 +01:00
parent 842033a014
commit 7a5585d2c4
4 changed files with 54 additions and 91 deletions

View file

@ -17,16 +17,12 @@
STA $FD ; for pixel_draw STA $FD ; for pixel_draw
;; V = 2*(dx -dy) ;; V = 2*(dx -dy)
;; where: dy = Y_end - Y_start, dx = OO - X_start
LDA Y_end
SEC SEC
LDA Y_end
SBC Y_pos SBC Y_pos
STA >V STA >V
STA >dy_2; >dy_2 = dy. Needed for dy_2 (not for V) STA >dy_2; >dy_2 = dy. Needed for dy_2 (not for V)
LDA X_end LDA dx
SEC
SBC X_pos
STA dx
SEC SEC
SBC >V SBC >V
STA >V; <V = dx - dy STA >V; <V = dx - dy
@ -34,15 +30,18 @@
;dy_2 = dy*2 ;dy_2 = dy*2
mult_16 >dy_2, <dy_2, ! mult_16 >dy_2, <dy_2, !
;; D = 2*dy - dx
;; In loop we have that D = D -V ;; This is an Bresenham's line algorithm, se wikipedia bellow.
;; So D needs to be at least >=V. ;;https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
;; V_max = 00000001 11111111 ;; We need to compute the Value D = 2*dy - dx,
;; For us to work with unsigned numbers we add 00000001 11111111 ;; but it may be or get negative.
;; to V and the branch logic to V! ;; IN the loop we may set D = D -V
;; Because math D needs to be at least >=V.
;; V_max = %00000001 11111111
;; We therefor need to add this offset to V 00000001 11111111
;; and to its branch logic later in the loop.
;;D = 2*dy - dx + 2*255 ;;D = 2*dy - dx + 2*255
;;Our D is bigger then wikipedia because D is unsigned.
LDA >dy_2 LDA >dy_2
STA >D STA >D
LDA <dy_2 LDA <dy_2
@ -53,35 +52,36 @@
jsr pixel_draw ;;only used first pixel. after this relative position is abused jsr pixel_draw ;;only used first pixel. after this relative position is abused
LDX X_pos LDX X_pos
for_x: for_x:
;; Paints A to address i btp_mem_pos* + Y ;; Paints A to address in |btp_mem_pos* + Y|
;; Y is our Y-pos-chunk-offset. ;; Y is pixel position in the chunk. Therefor it may be that Y = 0, 1, 2, 3, 4, ,5 ,6 ,7.
LDA byte_to_paint LDA byte_to_paint ;A byte containing a single 1. Coresponds to X position in the chunk.
ORA (>btp_mem_pos), Y ORA (>btp_mem_pos), Y
STA (>btp_mem_pos), Y STA (>btp_mem_pos), Y
increment_pixel_x: increment_pixel_x:
LSR byte_to_paint LSR byte_to_paint ; Rotates the pixel one bit to the left ON THE SCREEN.
BCC increment_pixel_x_end BCC increment_pixel_x_end; We need to move to the next chunk
move_8px_left: move_8px_left:
;; add +8 to btp_mem_pos. Find more of why in pixel_draw ;; Next chunk is 8 addresses away. Look in pixel_draw for more detail.
Add_16 >btp_mem_pos, <btp_mem_pos, #$08, #$00 Add_16 >btp_mem_pos, <btp_mem_pos, #$08, #$00
;; reset byte_to_paint ;; Restores byte to paint
LDA #%10000000 LDA #%10000000
STA byte_to_paint STA byte_to_paint
increment_pixel_x_end: increment_pixel_x_end:
INX INX
CPX X_end CPX X_end
BEQ end BEQ end ;We keep track on when to stop line draw with the X registry.
;;If D < %00000010 00000000: case_2 ;;If D < %00000010 00000000: case_2
;;else case 1. ;;else case 1.
Lag_16 >D, <D, #$00, #$02, case_2 Lag_16 >D, <D, #$00, #$02, case_2
case_1:; C =1 so we can use ! case_1:; C =1 so we can use !
Sub_16 >D, <D, >V, <V, ! ; D = D - V Sub_16 >D, <D, >V, <V, ! ; D = D - V
increment_y_pos: increment_y_pos:
INY INY ; Increment Y pos inside the buffer
CPY #$08 ; CPY #$08
BNE for_x BNE for_x
move_8px_down: ; Z=1 --> C=1 move_8px_down: ; Z=1 --> C=1
LDY #$00 LDY #$00
;; Switch to chunk bellow
Add_16 >btp_mem_pos, <btp_mem_pos, #$40, #$01; +320 Add_16 >btp_mem_pos, <btp_mem_pos, #$40, #$01; +320
JMP for_x JMP for_x
increment_y_pos_end: increment_y_pos_end:

View file

@ -8,35 +8,28 @@
;;# (X_end, Y_end) # ;;# (X_end, Y_end) #
;;NOTE THAT X_pos <= X_end, Y_pos <= Y_end. Min 45deg! ;;NOTE THAT X_pos <= X_end, Y_pos <= Y_end. Min 45deg!
.proc line_down_inv .proc line_down_inv
.include "line.inc"; Defines memory positions, ex X_pos ;; Look at line_down for referense
.include "line.inc"
;;We need to clear this memory
LDA #$00 LDA #$00
STA <V STA <V
STA <dx_2 STA <dx_2
STA $FD ; for pixel_draw STA $FD
;; V = 2*(dy -dx) LDA dx
;; where: dy = Y_end - Y_start, dx = X_end - X_start
LDA X_end
SEC
SBC X_pos
STA >V STA >V
STA >dx_2; >dx_2 = dx. Needed for dx_2 (not for V) STA >dx_2
LDA Y_end LDA Y_end
SEC SEC
SBC Y_pos SBC Y_pos
STA dy STA dy
SEC SEC
SBC >V SBC >V
STA >V; <V = dy - dx STA >V
mult_16 >V, <V; V = 2*(dy -dx) mult_16 >V, <V
;dx_2 = dx*2
mult_16 >dx_2, <dx_2 mult_16 >dx_2, <dx_2
;;D = 2*dy - dx + 2*255
;;Our D is bigger then wikipedia because D is unsigned.
LDA >dx_2 LDA >dx_2
STA >D STA >D
LDA <dx_2 LDA <dx_2
@ -44,7 +37,7 @@
Add_16 >D, <D, #$ff, #$01 Add_16 >D, <D, #$ff, #$01
Sub_16 >D, <D, dy, #$00 Sub_16 >D, <D, dy, #$00
jsr pixel_draw ;;only used first pixel. after this relative position is abused jsr pixel_draw
LDX Y_pos LDX Y_pos
for_y: for_y:
LDA byte_to_paint LDA byte_to_paint
@ -56,28 +49,24 @@ increment_y_pos:
BNE increment_y_pos_end BNE increment_y_pos_end
move_8px_down: move_8px_down:
LDY #$00 LDY #$00
Add_16 >btp_mem_pos, <btp_mem_pos,#$40 , #$01; ; +320 bytes Add_16 >btp_mem_pos, <btp_mem_pos,#$40 , #$01;
increment_y_pos_end: increment_y_pos_end:
INX INX
CPX Y_end CPX Y_end
BEQ end BEQ end
;;If D < %00000010 00000000: case_2
;;else case 1.
Lag_16 >D, <D, #$00, #$02, case_2 Lag_16 >D, <D, #$00, #$02, case_2
case_1:; C = 1 because LAG case_1:
Sub_16 >D, <D, >V, <V, !; D = D - V Sub_16 >D, <D, >V, <V, !
increment_pixel_x: increment_pixel_x:
LDA byte_to_paint LDA byte_to_paint
LSR byte_to_paint LSR byte_to_paint
BCC for_y BCC for_y
move_8px_left: move_8px_left:
;; add +8 to btp_mem_pos. Find more of why in pixel_draw
Add_16 >btp_mem_pos, <btp_mem_pos, #$08, #$00 Add_16 >btp_mem_pos, <btp_mem_pos, #$08, #$00
;; reset byte_to_paint
LDA #%10000000 LDA #%10000000
STA byte_to_paint STA byte_to_paint
JMP for_y JMP for_y
case_2: ;; C =0 because LAG_16 so we can use ! case_2:
Add_16 >D, <D, >dx_2, <dx_2, ! ;D = D + 2*dx Add_16 >D, <D, >dx_2, <dx_2, ! ;D = D + 2*dx
JMP for_y JMP for_y
end: end:

View file

@ -10,41 +10,26 @@
;;NOTE THAT X_pos <= X_end, Y_pos >= Y_end. Max 45deg! ;;NOTE THAT X_pos <= X_end, Y_pos >= Y_end. Max 45deg!
.proc line_up .proc line_up
.include "line.inc"; Defines memory positions, ex X_pos ;; Look at line_down for referense
.include "line.inc"
;;We need to clear this memory
LDA #$00 LDA #$00
STA <V STA <V
STA <dy_2 STA <dy_2
STA $FD ; for pixel_draw STA $FD
;; V = 2*(dx -dy)
;; where: dy = Y_end - Y_start, dx = OO - X_start
LDA Y_pos LDA Y_pos
SEC SEC
SBC Y_end SBC Y_end
STA >V STA >V
STA >dy_2; >dy_2 = dy. Needed for dy_2 (not for V) STA >dy_2
LDA X_end LDA dx
SEC
SBC X_pos
STA dx
SEC SEC
SBC >V SBC >V
STA >V; <V = dx - dy STA >V
mult_16 >V, <V; V = 2*(dx -dy) mult_16 >V, <V
;dy_2 = dy*2
mult_16 >dy_2, <dy_2, ! mult_16 >dy_2, <dy_2, !
;; D = 2*dy - dx
;; In loop we have that D = D -V
;; So D needs to be at least >=V.
;; V_max = 00000001 11111111
;; For us to work with unsigned numbers we add 00000001 11111111
;; to V and the branch logic to V!
;;D = 2*dy - dx + 2*255
;;Our D is bigger then wikipedia because D is unsigned.
LDA >dy_2 LDA >dy_2
STA >D STA >D
LDA <dy_2 LDA <dy_2
@ -78,11 +63,11 @@ decrement_y_pos:
BNE for_x BNE for_x
move_8px_up: move_8px_up:
LDY #$07 LDY #$07
Sub_16 >btp_mem_pos, <btp_mem_pos, #$40, #$01 ;-320 Sub_16 >btp_mem_pos, <btp_mem_pos, #$40, #$01
jmp for_x jmp for_x
decrement_y_pos_end: decrement_y_pos_end:
case_2: case_2:
Add_16 >D, <D, >dy_2, <dy_2 ;D = D + 2*dy Add_16 >D, <D, >dy_2, <dy_2
JMP for_x JMP for_x
end: end:
RTS RTS

View file

@ -9,35 +9,28 @@
;;NOTE THAT Y_pos >) Y_end, X_pos <= X_end. Min 45deg! ;;NOTE THAT Y_pos >) Y_end, X_pos <= X_end. Min 45deg!
.proc line_up_inv .proc line_up_inv
;; Look at line_down for referense
.include "line.inc" .include "line.inc"
;We need to clear this memory
LDA #$00 LDA #$00
STA <V STA <V
STA <dx_2 STA <dx_2
STA $FD ; for pixel_draw STA $FD
;; V = 2*(dy -dx) LDA dx
;; where: dy = Y_pos - Y_end, dx = X_end - X_start
LDA X_end
SEC
SBC X_pos
STA >V STA >V
STA >dx_2; >dy_2 = dy. Needed for dy_2 (not for V) STA >dx_2
LDA Y_pos LDA Y_pos
SEC SEC
SBC Y_end SBC Y_end
STA dy STA dy
SEC SEC
SBC >V SBC >V
STA >V; <V = dx - dy STA >V
mult_16 >V, <V; V = 2*(dx -dy) mult_16 >V, <V
;dy_2 = dy*2
mult_16 >dx_2, <dx_2 mult_16 >dx_2, <dx_2
;;D = 2*dx - dy + 2*255
;;Our D is bigger then wikipedia because D is unsigned.
LDA >dx_2 LDA >dx_2
STA >D STA >D
LDA <dx_2 LDA <dx_2
@ -57,29 +50,25 @@ decrement_y_pos:
BNE decrement_y_pos_end BNE decrement_y_pos_end
move_8px_up: move_8px_up:
LDY #$07 LDY #$07
Sub_16>btp_mem_pos, <btp_mem_pos,#$40 , #$01; ; +320 bytes Sub_16>btp_mem_pos, <btp_mem_pos,#$40 , #$01
decrement_y_pos_end: decrement_y_pos_end:
DEX DEX
CPX Y_end CPX Y_end
BEQ end BEQ end
;;If D < %00000010 00000000: case_2
;;else case 1.
Lag_16 >D, <D, #$00, #$02, case_2 Lag_16 >D, <D, #$00, #$02, case_2
case_1: case_1:
Sub_16 >D, <D, >V, <V, !; D = D - V Sub_16 >D, <D, >V, <V, !
increment_pixel_x: increment_pixel_x:
LDA byte_to_paint LDA byte_to_paint
LSR byte_to_paint LSR byte_to_paint
BCC for_y BCC for_y
move_8px_left: move_8px_left:
;; add +8 to btp_mem_pos. Find more of why in pixel_draw
Add_16 >btp_mem_pos, <btp_mem_pos, #$08, #$00 Add_16 >btp_mem_pos, <btp_mem_pos, #$08, #$00
;; reset byte_to_paint
LDA #%10000000 LDA #%10000000
STA byte_to_paint STA byte_to_paint
JMP for_y JMP for_y
case_2: case_2:
Add_16 >D, <D, >dx_2, <dx_2;D = D + 2*dy Add_16 >D, <D, >dx_2, <dx_2
JMP for_y JMP for_y
end: end:
RTS RTS