;KoroKoro Tx 1
; Ir Out

	LIST	P=PIC16F84
	LIST	R=DECIMAL
	include	"p16f84.inc"
	__config _CP_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC

;---------------------------------------
;PortA 0-4 LED Kathode (LoActive Scan)
;PortB 0-4 LED Anode (HiActive)
;      5   Ir Out (LoActive)
;      6   X in
;      7   Y in
;---------------------------------------

    cblock  0x0c

    tx_reg
    tx_lcnt
    tx_wcnt
    out_b
    wcnt1
    wcnt2
    x_cnt
    y_cnt
    x_new
    y_new
    x_old
    y_old
    x_pos
    y_pos
    lp_cnt
    xy_num
    bit_cnt

    endc

;---------------------------------------
    org 0

    goto    start
    retfie
    retfie
    retfie
    retfie
start
    bcf     STATUS,RP0
    movlw   0xFF
    movwf   PORTA
    movlw   0x20
    movwf   out_b
    movwf   PORTB

    bsf     STATUS,RP0
    movlw   0xE0
    movwf   TRISA
    movlw   0xC0
    movwf   TRISB
    movlw   0x7F
    movwf   OPTION_REG

    bcf     STATUS,RP0

;---------------------------------------
Main_Loop

    clrf    x_new
X_wait_low
    btfsc   PORTB,6
     goto   X_wait_low
X_rise
    btfss   PORTB,6
     goto   X_rise
X_fall
    incf    x_new,f
    btfsc   PORTB,6
     goto   X_fall

    clrf    y_new
Y_wait_low
    btfsc   PORTB,7
     goto   Y_wait_low
Y_rise
    btfss   PORTB,7
     goto   Y_rise
Y_fall
    incf    y_new,f
    btfsc   PORTB,7
     goto   Y_fall

;---------------------------------------
X_check
    movf    x_old,w	
    subwf   x_new,w     ; d=new-old
     bnc    X_check20
X_check10               ; new>=old
    sublw   1           ; 1-d
     bc     X_check30
    movf    x_new,w     ; 1<d
    movwf   x_old
    decf    x_old,f     ; old = new-1
    goto    X_check30
X_check20               ; new<old
    sublw   0xFF        ; 0xFF-d
     bz     X_check30
    movf    x_new,w     ; d != -1
    movwf   x_old
    incf    x_old,f     ; old = new+1
X_check30

Y_check
    movf    y_old,w	
    subwf   y_new,w     ; d=new-old
     bnc    Y_check20
Y_check10               ; new>=old
    sublw   1           ; 1-d
     bc     Y_check30
    movf    y_new,w     ; 1<d
    movwf   y_old
    decf    y_old,f     ; old = new-1
    goto    Y_check30
Y_check20               ; new<old
    sublw   0xFF        ; 0xFF-d
     bz     Y_check30
    movf    y_new,w     ; d != -1
    movwf   y_old
    incf    y_old,f     ; old = new+1
Y_check30

;---------------------------------------
X_position
    movlw   1
    movwf   x_pos
    clrf    xy_num
    movlw   124
    movwf   x_cnt
    movf    x_old,w
    subwf   x_cnt,f
    skpc
     goto   X_pos_exit
    movlw   3
    movwf   lp_cnt
X_pos_loop
    clrc
    rlf     x_pos,f
    movlw   5
    addwf   xy_num,f
    movlw   8
    subwf   x_cnt,f
    skpc
     goto   X_pos_exit
    decfsz  lp_cnt,f
     goto   X_pos_loop
    clrc
    rlf     x_pos,f
    movlw   5
    addwf   xy_num,f
X_pos_exit

;---------------------------------------
Y_position
    movlw   1
    movwf   y_pos
    movlw   124
    movwf   y_cnt
    movf    y_old,w
    subwf   y_cnt,f
    skpc
     goto   Y_pos_exit
    movlw   3
    movwf   lp_cnt
Y_pos_loop
    clrc
    rlf     y_pos,f
    movlw   1
    addwf   xy_num,f
    movlw   8
    subwf   y_cnt,f
    skpc
     goto   Y_pos_exit
    decfsz  lp_cnt,f
     goto   Y_pos_loop
    clrc
    rlf     y_pos,f
    movlw   1
    addwf   xy_num,f
Y_pos_exit

;---------------------------------------
LED
    movf    x_pos,w
    movwf   out_b
    bsf     out_b,5
    movf    out_b,w
    movwf   PORTB
    movlw   0x1F
    xorwf   y_pos,w
    movwf   PORTA


;---------------------------------------
TxIrData
    movf    xy_num,w
    andlw   0x1f
    movwf   tx_reg
    movlw   6
    movwf   bit_cnt
    bcf     out_b,5	    ; StartBit
TxIrLoop10
    movlw   31
    movwf   lp_cnt
TxIrLoop20
    call    IrCarrier   ; 31x
    decfsz  lp_cnt,f
     goto   TxIrLoop20
    call    IrNextData  ; 32
    decfsz  bit_cnt,f
     goto   TxIrLoop10
TxIrLoop30
    movf    xy_num,w
    xorlw   0x1f
    andlw   0x1f
    movwf   tx_reg
    movlw   24
    movwf   bit_cnt
TxIrLoop40
    movlw   31
    movwf   lp_cnt
TxIrLoop50
    call    IrCarrier   ; 31x
    decfsz  lp_cnt,f
     goto   TxIrLoop50
    call    IrNextData  ; 32
    decfsz  bit_cnt,f
     goto   TxIrLoop40

    goto    Main_Loop

;---------------------------------------
IrCarrier
    movf    out_b,w
    movwf   PORTB
    goto    $+1
    goto    $+1
    goto    $+1
    goto    $+1
    goto    $+1
    goto    $+1
    bsf     PORTB,5
    goto    $+1
    goto    $+1
    return

;---------------------------------------
IrNextData
    nop
    movf    out_b,w
    movwf   PORTB

    btfsc   tx_reg,0
    bcf     out_b,5
    btfss   tx_reg,0
    bsf     out_b,5
    clrc
    rrf     tx_reg,f

    goto    $+1
    goto    $+1
    goto    $+1
    bsf     PORTB,5
    goto    $+1
    return

;---------------------------------------
    END
