/* This file contains the basic initialization of the AVR core and the
 * Atmega328p controller on the Arduino Uno board.
 * Finally, the userland function main() is invoked.
 * @author Bernhard R. Fischer
 */

#include <avr/io.h>


.section .vectors

.org 0
   rjmp  __ctors_start     ; reset vector

.org 0x40
   rjmp t0_handler         ; timer 0 interrupt vector


.section .ctors
   ldi   r16,0             ; reset system status register
   out   _SFR_IO_ADDR(SREG),r16
   ldi   r16,lo8(RAMEND)   ; init stack pointer
   out   _SFR_IO_ADDR(SPL),r16
   ldi   r16,hi8(RAMEND)
   out   _SFR_IO_ADDR(SPH),r16

   ldi   r16,0x20          ; set port bit 5 (LED) to output mode
   out   _SFR_IO_ADDR(DDRB),r16

   rcall init_timer        ; init timer 0

                           ; jump to userland function main()
   ldi   r16,lo8(main)     ; load address of main to r16:r17
   ldi   r17,hi8(main)
   lsr   r17               ; divide by 2 because PC counts words
   ror   r16
   push  r16               ; push address to stack
   push  r17
 
   reti                    ; and return to it and enable interrupts

