( apal | 2020. 06. 01., h - 09:42 )

.set    DDRB,   0x17
.set    PINB,   0x16
.set    PORTB,  0x18

reset:
        sbi     DDRB,   0       ; PB0: UART output at the standard baud rate of 921600.
        cbi     DDRB,   1       ; PB1: phase A input
        cbi     DDRB,   2       ; PB2: phase B input

                                ; PB3 + PB4 connect a 16.5888 MHz crystal

        eor     r1, r1          ; the constant "0", zero register
        ldi     r20,    2       ; the constant "2" (1<<1)
        ldi     r21,    1       ; the constant "1"
        ldi     r22,    0       ; counter

        in      r17, PINB       ; inital state

.macro  BL0
        in      r16, PINB
        sbrc    r16, 2
        eor     r16, r20
        sub     r17, r16
        sbrc    r17, 1
        subi    r22, 0xff       ; do this if bit 1 is set
        sbrc    r17, 2
        subi    r22, 0x02       ; do this if bit 2 is set
.endm

.macro  BL1
        in      r17, PINB
        sbrc    r17, 2
        eor     r17, r20
        sub     r16, r17
        sbrc    r16, 1
        subi    r22, 0xff
        sbrc    r16, 2
        subi    r22, 0x02
.endm

.macro  USTART
        BL0
        nop
        BL1
        out     PORTB, r1       ; UART start bit
.endm

.macro  UBIT0
        BL0
        mov     r23, r22
        BL1
        out     PORTB, r23      ; UART bit 0
.endm

.macro  UBITx
        BL0
        ror     r23
        BL1
        out     PORTB, r23      ; UART bits 1, 2, ... 7.
.endm

.macro  USTOP
        BL0
        nop
        BL1
        out     PORTB, r21      ; UART stop bit
.endm
        
.macro  UFINAL
        BL0
        ;nop                    ; clock cycle is reserved for RJMP
        BL1
        ;out    PORTB, r21      ; clock cycle is reserved for RJMP
.endm
        

start:
        USTART          ; UART start bit
        UBIT0           ; UART bit 0
        UBITx           ; UART bit 1
        UBITx           ; UART bit 2
        UBITx           ; UART bit 3
        UBITx           ; UART bit 4
        UBITx           ; UART bit 5
        UBITx           ; UART bit 6
        UBITx           ; UART bit 7
        USTOP           ; UART stop bit
        USTOP           ; UART stop bit
        USTOP           ; UART stop bit
        USTOP           ; UART stop bit
        USTOP           ; UART stop bit
        USTOP           ; UART stop bit
        UFINAL
        rjmp            start

;szerk: az elobb belejavitottam egy kicsit, jobban makrozva, hogy valamivel olvashatobb legyen ;)