;;drawing line from 2 cordinates
.proc line
    ;;#  (X_pos, Y_pos)                                 #
    ;;#                     *                               #
    ;;#                         *                           #
    ;;#                             *                       #
    ;;#                                (X_end, Y_end)       #
    ;;
    ;;NOTE THAT X_pos <= X_end and line is going downwords   max 45deg!

    ;;Not values but register position in memory
    X_end = $04
    Y_end = $05
    X_pos = $FC
    Y_pos = $FB
    dy_2 = $0607
    dx = $08
    D = $0a0b

    ;;Set values
    LDA #$00
    STA $FD ; for pixel_draw

    ;example values ~~~~~ SHOULD BE PRECOMPILED
    LDA #$00
    STA X_pos
    STA Y_pos

    LDA #$f0
    STA X_end
    LDA #$80
    STA Y_end
    ;;~~~~~~~~~~

    ;;2*dy = 2*(Y_end -Y_pos)
    LDA Y_end
    SEC
    SBC Y_pos
    STA >dy_2
    LDA #$00
    STA <dy_2
    mult_16 >dy_2, <dy_2

    ;;dx = (X_end -X_pos)
    LDA X_end
    SEC
    SBC X_pos
    STA dx

    ;;D = 2*dy - dx + 2*255. 0 <=D <= 4*255
    ;;Our D is bigger then wikipedia to not use negative numbers
    LDA >dy_2
    STA >D
    LDA <dy_2
    STA <D
    Add_16 >D, <D, #$01, #$00
    Sub_16 >D, <D, dx, #$00


    ;;We wont use dx, only 2*dx from now on
    dx_2 = $0809 ;Lets reuse dx register ($08)
    LDA #$00
    STA <dx_2
    mult_16 >dx_2, <dx_2

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 <  %00000001 11111111 <==> >D ==0, then: case_2
    LDA <D; if <D > 0 then: case_1
    CMP #$00
    BEQ case_2
case_1:
    INC Y_pos
    ;; D = D - 2*dx
    Sub_16 >D, <D, >dx_2, <dx_2
case_2:
    ;;D = D + 2*dy
    Add_16 >D, <D, >dy_2, <dy_2
last:
    JMP for_x
end:
.endproc