;   ***************************************************************
;   * Copyright (c) 2001, Embed Inc (http://www.embedinc.com)     *
;   *                                                             *
;   * Permission to copy this file is granted as long as this     *
;   * copyright notice is included in its entirety at the         *
;   * beginning of the file, whether the file is copied in whole  *
;   * or in part and regardless of whether other information is   *
;   * added to the copy.                                          *
;   *                                                             *
;   * The contents of this file may be used in any way,           *
;   * commercial or otherwise.  This file is provided "as is",    *
;   * and Embed Inc makes no claims of suitability for a          *
;   * particular purpose nor assumes any liability resulting from *
;   * its use.                                                    *
;   ***************************************************************
;
;   Top module for the HAL haloween SpookyEyes device.
;
         include "hal.inc"
         extern  regs        ;force general registers to be defined
         extern  stack_init  ;initialize software stack
         extern  intr_init   ;initialize interrupt system
         extern  port_init   ;initialize I/O ports
         extern  rand_init   ;initialize random number generator
         extern  eyes_init   ;initialize eyes controller module
         extern  soun_init   ;initialize sound handling module
         extern  ad_init     ;initialize A/D handling module

         extern  startup     ;system startup after individual modules initialized
;
;***********************************************************************
;
;   Set static processor configuration bits.
;
         __config b'11111101110010'
                 ;  11------11----  code protection disabled
                 ;  --1-----------  no in circuit debugging, RB6,RB7 general I/O
                 ;  ---X----------  unused
                 ;  ----1---------  flash memory is writeable by program
                 ;  -----1--------  EEPROM read protection disabled
                 ;  ------0-------  low volt in circ prog off, RB3 is general I/O
                 ;  -------1------  brown out reset enabled
                 ;  ----------0---  power up timer enabled
                 ;  -----------0--  watchdog timer disabled
                 ;  ------------10  high speed oscillator mode
;
;***********************************************************************
;
;   Global state.
;
.bank#v(gbank) udata
;
;   Declare global flag bytes GFL0 - GFLn.  The assembly constant
;   NFLAGB is set to the number of these flag bytes by the /FLAG
;   preprocessor directives in HAL.INS.ASPIC.
;
         flags_define        ;define the variables for the global flag bits
;
;***********************************************************************
;
;   Executable code.
;
;   Reset vector.
;
.reset   code    0
         clrf    intcon      ;disable all maskable interrupts
         gjump   start       ;jump to relocatable startup code
;
;   Relocatable code.
;
;   This code only initializes the individual modules.  The remaining
;   system initialization is done in the CMD_INIT module, which jumps
;   to MAIN when done.
;
.start   code
start    unbank
;
;   Init the interrupt system to completely off.  INTCON has already been
;   cleared.
;
         dbankif pie1        ;separately disable all individual interrupts
         clrf    pie1
         dbankif pie2
         clrf    pie2
         dbankif pir1        ;clear any pending interrupt conditions
         clrf    pir1
         dbankif pir2
         clrf    pir2

         flags_clear         ;initialize all global flag bits to 0
;
;   Initialize the separate modules.
;
         gcallnr stack_init  ;init the software stack
         gcallnr port_init   ;init I/O ports
         gcallnr rand_init   ;init random number generator
         gcallnr eyes_init   ;init module controlling the LED "eyes"
         gcallnr soun_init   ;init module controlling the output sound
         gcallnr ad_init     ;init module controlling the A/D converter

         gcallnr intr_init   ;init interrupt management and start interrupts
;
;   All the individual modules have been initialized.  Now start up the
;   overall system.
;
         gjump   startup

         end