;;drawing line from 2 cordinates
.proc line_up
    ;;#                               *      (X_end, Y_end) #
    ;;#                                                     #
    ;;#                         *                           #
    ;;#                    *                                #
    ;;# (X_pos, Y_pos)                                      #
    ;;
    ;;NOTE THAT X_pos <= X_end, Y_pos >= Y_end. Max 45deg!
    .include "line.inc"; Defines memory positions, ex X_pos

    ;;example values ~~~~~ SHOULD BE PRECOMPILED
    ;LDA #$90
    ;STA X_pos
    ;STA Y_pos
    ;LDA #$aa
    ;STA X_end
    ;LDA #$80
    ;STA Y_end
    ;;~~~~~~~~~~

    ;;We need to clear this memory
    LDA #$00
    STA <V
    STA <dy_2
    STA $FD ; for pixel_draw

    ;; V = 2*(dx -dy)
    ;; where: dy = Y_pos - Y_end, dx = X_end - X_start
    ;; This logic is comented out because line.s does it woithout any
    ;;extra cost. May be needed in the future so it will stay as coments!
    LDA Y_pos
    SEC
    SBC Y_end
    STA >V
    STA >dy_2;  >dy_2 = dy. Needed for dy_2 (not for V)
    LDA X_end
    SEC
    SBC X_pos
    STA dx
    SEC
    SBC >dy_2
    STA >V; <V = dx - dy
    mult_16 >V, <V; V = 2*(dx -dy)

    ;dy_2 = dy*2
    mult_16 >dy_2, <dy_2

    ;;D = 2*dy - dx + 2*255
    ;;Our D is bigger then wikipedia because D is unsigned.
    LDA >dy_2
    STA >D
    LDA <dy_2
    STA <D
    Add_16 >D, <D, #$ff, #$01
    Sub_16 >D, <D, dx, #$00
for_x:
    jsr pixel_draw
    ;;Increment X until X_pos = X_end and Y_pos = Y_end
    INC X_pos
    LDX X_pos
    CPX X_end
    BEQ end

    ;;If D <  %00000010 00000000:  case_2
    ;;else case 1.
    Lag_16 >D, <D, #$00, #$02, case_2
case_1:
    DEC Y_pos
    Sub_16 >D, <D, >V, <V; D = D - V
    JMP for_x
case_2:
    Add_16 >D, <D, >dy_2, <dy_2;D = D + 2*dy
    JMP for_x
end:
    RTS
.endproc