; *******************************************************************
;   GP-FLASH-LED-SLOW.ASM
;
;  Program demostrates how to make delayloops for a long time
;   and how to calculate duration of the delayloop
;  Commonly used unit for CPU timings is 1T = 1Tick = 1 BUScycle
;   It means 4T = 4 BUScycles. To convert this imaginary time to
;   real world, user uses constant, 1/BUSFREQ, which says how time
;   takes 1 buscycle, and multiplies number of ticks by this constant
;  e.g.: In DBG environment runs MCU on 2.4576MHz BUSCLK, it gives
;   approx. 406ns per 1 buscycle. 1T=406ns here.
;  In CPU manual can be found, DIV instruction takes 7 buscycles,
;   takes 7T, in this case takes 7*406ns=2.442us
;  Program sets-up LEDs (one light, one not). In the loop
;   complements their states and waits in delayloop for approx. 0.65s
;  Runs infinitely.
; *******************************************************************
; 6.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
count   ds      3               ; timing counters

        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

        mov     #$20,PTD ; Y-LED on, R-LED off
main_loop:
        lda     PTD     ; pin 19 - PTD4 - Yellow LED
        eor     #$30    ; pin 18 - PTD5 - Red LED
        sta     PTD
        clra            ; Wait for 5*65536*6us
        sta     count+1
        sta     count+2
        mov     #5,count
main_wait:
        dbnz    count+2,main_wait ; 5t = 2us
        dbnz    count+1,main_wait ; 5t + 256x 5t = 1285t = 514us
        dbnz    count,main_wait ; 5t + 256x (5t + 256x 5t) = 328965t = 132ms
                                ; 5x (5t + 256x (5t + 256x 5t)) = 1644825t = 0.65s
                                ; note: 2.46MHz CGMXCLK expected where 1t=400ns (9.83MHz external clk)
        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 --------------------------------------------------------------------------
