; ***************************************************************************
; copyright   : (C) 2023 by Martin Preuss
; email       : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome".                              *
; * Please see toplevel file COPYING of that project for license details.   *
; ***************************************************************************




.nolist
.include "include/tn84def.inc" ; Define device ATtiny84
.list
;
.equ clock=1000000 ; Define the clock frequency



; ***************************************************************************
; defs about modules used

.equ MODULE_ID_OS         = 0
.equ MODULE_ID_TIMER      = 1
.equ MODULE_ID_LED_A3     = 2
.equ MODULE_ID_WAN        = 3
.equ MODULE_ID_PIN        = 4
.equ MODULE_ID_COUNT      = 5



; ***************************************************************************
; defs for OS

; OS module
.include "os/defs.asm"


; timer module
.equ TIMER_ENABLE_100Hz      = 1
.equ TIMER_ENABLE_10Hz       = 1
.equ TIMER_ENABLE_1Hz        = 1

.include "timer/defs.asm"



; led module
.include "led/defs.asm"



; wan module
.include "wan/defs.asm"



; pin module
.include "pin/defs.asm"



; ***************************************************************************
; data segment

.dseg
.org SRAM_START

ledA3Sram:   .byte LED_SRAM_SIZE
wanA7A1Sram: .byte WAN_IFACE_SIZE



; ***************************************************************************
; code segment

.cseg
.org 000000

; ***************************************************************************
; Reset and interrupt vectors

	rjmp Main ; Reset vector
	reti ; EXT_INT0
	rjmp pinIsrInt0 ; PCI0
	rjmp pinIsrInt1 ; PCI1
	reti ; WATCHDOG
	reti ; ICP1
	reti ; OC1A
	reti ; OC1B
	reti ; OVF1
	rjmp timerIrqOC0A ; OC0A
	reti ; OC0B
	reti ; OVF0
	reti ; ACI
	reti ; ADCC
	reti ; ERDY
	reti ; USI_STR
	reti ; USI_OVF
;
; ***************************************************************************
; Interrupt service routines from modules

.include "timer/irq.asm"
.include "wan/irq.asm"
.include "pin/irq.asm"



; ***************************************************************************
; Info list of used modules

osModuleInfoList:

modInfoOs:
  rjmp Os_SignalHandler    ; jump to handler
  .db MODULE_ID_OS, 0      ; id, reserved byte (0)
  .dw 0                    ; pointer to flash data
  .dw 0                    ; pointer to SRAM data
  .db "OS     ", 0         ; name

modInfoTimer:
  rjmp Timer_SignalHandler ; jump to handler
  .db MODULE_ID_TIMER, 0   ; id, reserved byte (0)
  .dw 0                    ; pointer to flash data
  .dw 0                    ; pointer to SRAM data
  .db "TIMER  ", 0         ; name

modInfoLedA3:
  rjmp Led_SignalHandler   ; jump to handler
  .db MODULE_ID_LED_A3, 0  ; id, reserved byte (0)
  .dw ledA3Flash           ; pointer to flash data
  .dw ledA3Sram            ; pointer to SRAM data
  .db "LED    ", 0         ; name

modInfoWan:
  rjmp Wan_SignalHandler   ; jump to handler
  .db MODULE_ID_WAN, 0     ; id, reserved byte (0)
  .dw wanA7A1Flash         ; pointer to flash data
  .dw wanA7A1Sram          ; pointer to SRAM data
  .db "WAN    ", 0         ; name

modInfoPin:
  rjmp Pin_SignalHandler   ; jump to handler
  .db MODULE_ID_PIN, 0     ; id, reserved byte (0)
  .dw 0                     ; pointer to flash data
  .dw 0                    ; pointer to SRAM data
  .db "PIN    ", 0         ; name

  .dw 0 ; end of module list



; ***************************************************************************
; Flash data for modules

ledA3Flash:   .db DDRA+0x20, PORTA+0x20, PINA+0x20, (1<<PORTA3)

wanA7A1Flash: .db 1, 0                                          ; id, flags
              .db DDRA+0x20, PORTA+0x20, PINA+0x20, (1<<PORTA1) ; port info for DATA line
              .db DDRA+0x20, PORTA+0x20, PINA+0x20, (1<<PORTA7) ; port info for ATTN line

pinInt0Entries:
;     flags                   PCMSK  port_addr  pin_mask     handler_lo        handler_hi         data_lo          data_hi
  .db PIN_ENTRY_FLAGS_ON_LOW, 0x80,  DDRA+0x20, (1<<PORTA7), LOW(wanIntAttn0), HIGH(wanIntAttn0), LOW(modInfoWan), HIGH(modInfoWan)
  .db 0, 0, 0, 0, 0, 0, 0, 0 ; end of list

pinInt1Entries:
;     flags reserved port_addr pin_mask handler_lo handler_hi data_lo data_hi
  .db 0, 0, 0, 0, 0, 0, 0, 0 ; end of list



; ***************************************************************************
; Signal maps for modules

; map for signals emitted by OS module
osSignalMap:
  ;   SIGNAL ID          SLOT ID       MODINFO pointer for handling module
  .db 0, 0, 0, 0 ; end of table

; map for signals emitted by timer module
timerSignalMap:
  ;   SIGNAL ID               SLOT ID              MODINFO pointer for handling module
  .db TIMER_SIGNAL_TICK_10HZ, OS_SLOT_TICK,        LOW(modInfoOs), HIGH(modInfoOs)       ; OS module
  .db TIMER_SIGNAL_TICK_10HZ, LED_SLOT_TIMER_TICK, LOW(modInfoLedA3), HIGH(modInfoLedA3) ; LED module
;  .db TIMER_SIGNAL_TICK_1HZ,  WAN_SLOT_DEBUG, LOW(modInfoWan), HIGH(modInfoWan) ; WAN module
  .db 0, 0, 0, 0 ; end of table



; ***************************************************************************
; Code from used modules

.include "os/main.asm"
.include "timer/main.asm"
.include "led/main.asm"
.include "wan/main.asm"
.include "pin/main.asm"


;
; ***************************************************************************
;  main code

Main:
  .ifdef SPH               ; if SPH is defined
    ldi r16, High(RAMEND)
    out SPH, r16           ; init MSB stack pointer
  .endif
  ldi r16, Low(RAMEND)
  out SPL, r16             ; init LSB stack pointer

  rcall OS_Init            ; init all modules

  ldi r16, 0
  ldi r17, MODULE_ID_LED_A3
  ldi r18, LED_SLOT_SET_PATTERN
  ldi xh, HIGH(blinkPattern)
  ldi xl, LOW(blinkPattern)
  rcall OS_CallModuleHandler

  sei ; Enable interrupts
  rjmp OS_EventLoop



blinkPattern:  .db 20, 10, 5, 5, 5, 5, 5, 20, 10, 20, 0xff, 0xff   ; 2s on, 1s off, 300ms on, 300ms off, restart


;
; End of source code
;
; (Add Copyright information here, e.g.
; .db "(C)2022 by Martin Preuss", 0, 0

