;=======================================================
; Проект 18. Бегущая строка на матрице 8x8
; ATmega8, 8 МГц. PB0..PB7 — строки, PD0..PD2 — адрес столбца
; Буфер из 16 столбцов, кольцевая адресация
;=======================================================
.include "m8def.inc"
.def    temp   = r16
.def    col    = r17
.def    shift  = r18
.def    frames = r19
.def    idx    = r20
.def    d1     = r21

.cseg
.org    0x0000
        rjmp    RESET

IMG:    .db 0b01111110, 0b00010001, 0b00010001, 0b01111110
        .db 0b00000000, 0b01111111, 0b01001001, 0b01001001
        .db 0b00000000, 0b00111110, 0b01000001, 0b01000001
        .db 0b00100010, 0b00000000, 0b00000000, 0b00000000

RESET:
        ldi     temp, low(RAMEND)
        out     SPL, temp
        ldi     temp, high(RAMEND)
        out     SPH, temp
        ldi     temp, 0xFF
        out     DDRB, temp
        ldi     temp, 0x07
        out     DDRD, temp
        clr     shift

MAIN:
        ldi     frames, 30              ; сколько кадров держать положение
FRAME:
        clr     col
SCAN:
        mov     idx, col                ; столбец экрана
        add     idx, shift              ; плюс текущее смещение
        andi    idx, 0x0F               ; кольцо из 16 столбцов
        ldi     ZL, low(IMG<<1)
        ldi     ZH, high(IMG<<1)
        add     ZL, idx
        clr     temp
        adc     ZH, temp
        lpm     temp, Z
        out     PORTB, temp
        out     PORTD, col
        rcall   DELAY1MS
        clr     temp
        out     PORTB, temp
        inc     col
        cpi     col, 8
        brne    SCAN
        dec     frames
        brne    FRAME

        inc     shift                   ; сдвигаем строку
        andi    shift, 0x0F
        rjmp    MAIN

DELAY1MS:
        ldi     d1, 250
A1:     nop
        nop
        nop
        nop
        nop
        nop
        dec     d1
        brne    A1
        ret
