; *******************************************************************
;   GP-FLASH-LED-FAST.ASM
;
;  Program demonstrates bit level control of I/O ports on LEDs
;   Before main loop starts, program writes byte value to gate,
;   where LEDs are connected - possibility to control all bits
;   in gate (all devices on gate connected) by one instruction.
;   Loop consists of sequence of bit oriented instructions, where
;   are bits of gate controlled separately. Result is LED flashing
;  Loop runs without delays on MCU fullspeed. Program is useful in
;   debugging environment only, or with scope
; *******************************************************************
; 5.3.2001 v2.0
; simulator - ok
; devbrd - ok

RAMStart     EQU  $0040
RomStart     EQU  $E000
VectorStart  EQU  $FFDC

$Include 'gpgtregs.inc'

        org     RamStart

internal_error  ds      1       ; internal errors counter

        org     RomStart

; - GPIO_INIT ------------------------------------------------------------------------------
; all-gpios initialisation - type: output, state: log.1, pullups-off
gpio_init:
        lda     #$FF
        sta     PTA
        sta     PTB
        sta     PTC
        sta     PTD
        sta     PTE
        sta     DDRA
        sta     DDRB
        sta     DDRC
        sta     DDRD
        sta     DDRE
        clra
        sta     PTAPUE
        sta     PTCPUE
        sta     PTDPUE
        rts
;- GPIO_INIT --------------------------------------------------------------------------------

;- MAIN --------------------------------------------------------------------------------
; Everything begins here
Main:
        rsp             ; stack pointer reset
        clra            ; register init
        clrx
        sta     internal_error ; clear internal errors counter
        mov     #$31,CONFIG1 ; MCU runs w/o LVI and COP support
        bsr     gpio_init ; GPIO initialization

main_loop:
        bclr    4,PTD   ; pin 21 - PTD4 - Yellow LED
        bset    4,PTD
        bclr    5,PTD   ; pin 22 - PTD5 - Red LED
        bset    5,PTD
        bra     main_loop ; runs infinitely

;- MAIN -------------------------------------------------------------------------------------

;- DUMMY_ISR --------------------------------------------------------------------------------
; Dummy interrupt handler - these interrupt requests will normaly never be activated, but..

dummy_isr:
       inc      internal_error
       rti
;- DUMMY_ISR --------------------------------------------------------------------------------

;- INTERRUPT VECTOR TABLE -------------------------------------------------------------------
        org  VectorStart
        dw  dummy_isr    ; Time Base Vector
        dw  dummy_isr    ; ADC Conversion Complete
        dw  dummy_isr    ; Keyboard Vector
        dw  dummy_isr    ; SCI Transmit Vector
        dw  dummy_isr    ; SCI Receive Vector
        dw  dummy_isr    ; SCI Error Vector
        dw  dummy_isr    ; SPI Transmit Vector
        dw  dummy_isr    ; SPI Receive Vector
        dw  dummy_isr    ; TIM2 Overflow Vector
        dw  dummy_isr    ; TIM2 Channel 1 Vector
        dw  dummy_isr    ; TIM2 Channel 0 Vector
        dw  dummy_isr    ; TIM1 Overflow Vector
        dw  dummy_isr    ; TIM1 Channel 1 Vector
        dw  dummy_isr    ; TIM1 Channel 0 Vector
        dw  dummy_isr    ; PLL Vector
        dw  dummy_isr    ; ~IRQ1 Vector
        dw  dummy_isr    ; SWI Vector
        dw  main         ; Reset Vector
;- INTERRUPT TABLE --------------------------------------------------------------------------
