// BRANDON Micromouse
// Test multitasking operating system for NMIT-0020 68HC11 board

#include "regs.asm"             // 68HC11 register defines
#include "defs.asm"             // definitions


// Start of each code/data section

// -----------------------------------------
        SECT SECT_ZEROPAGE
// Used for small variables which are accessed often
*
*   ZERO PAGE SECTION
*

        ORG ZERO_PAGE_START
PORTO_Val       RMB 1  // storage for current Port O value
// -----------------------------------------
        SECT SECT_DATA
// Used for all global data
*
*   DATA SECTION
*

// -----------------------------------------
        SECT SECT_CCODE
*
*   MAIN CODE
*
        ORG C_CODE_START
// -----------------------------------------
        SECT SECT_CODE
*
*   MAIN CODE
*
        ORG KERNEL_CODE_START
#undef INT_RESET
#define INT_RESET Reset  // set reset vector
Reset:

        LDAA    #0x80           ; RAM 8000-80FF, Regs 0000-003F
        STAA    0x103D
SoftReset:
        BSET TMSK2 0x01         ; set main counter to 500khz
        
        // Initialise serial system
        LDAA #BAUD_9600
        STAA BAUD                 // Baud rate = 9600
        LDAA #SCCR2_TE+SCCR2_RE
        STAA SCCR2                // Enable transmit and recieve using polling
        BSET #OPTION OPTION_ADPU  // power up A/D converter

        LDAA #0
        STAA SIO_Locked // unlock SIO to allow outputting of status

        LDS     #INIT_STACK     ; load initialisation stack

        JSR SIO_PutString
        FCB 13,10,13,10,13,10
        FCC 'Knl:  Checking memory...'
        FCB 13,10,0

        JSR MM_CheckMem

        JSR SIO_PutString
        FCC 'Knl:  Test complete'
        FCB 13,10,13,10,0

        JSR SIO_PutString
        FCC 'Knl:  Starting init code...'
        FCB 13,10,13,10,0

        JSR     Init            // initialise all systems

        JSR SIO_PutString
        FCB 13,10
        FCC 'Knl:  Starting main code...'
        FCB 13,10,0

        JSR _LED_off

        JMP Start // start the main code

PutPortO
        SEI
        LDAA PORTO_Val
        STAA PORTO
        CLI
        RTS

_Shutdown // void Shutdown() // resets the hc11
        SEI
        LDAA #0
        STAA TMSK2  //  Kill interrupt sources
        STAA TMSK1  //
        STAA PORTO  // switch off motors, IR leds etc

        JMP SoftReset



BadInterrupt                    // default interrupt handler
        RTI
// -----------------------------------------
        SECT SECT_INIT
// Each assembly module can put code in this section which will be
// executed at startup
*
*   INITIALIZATION CODE
*
Init
        LDAA #PORTO_LED_ENABLE
        STAA PORTO_Val
        JSR PutPortO

// -----------------------------------------
        SECT SECT_OTHER
// All code/data with fixed destination addresses must go here
*
*  VARIOUS OTHER CODE
*
        ORG PORTA
        FCB 16
// -----------------------------------------



#include "procman.asm"
#include "memman.asm"
#include "eventman.asm"
#include "serialio.asm"
#include "startup.asm"
#include "lcd.asm"
#include "keypad.asm"

// -----------------------------------------
        SECT SECT_CODE
_EXITS_VAL:
        FCB 1,2,4,8
_NUM_EXITS:
        FCB 0,1,1,2,1,2,2,3
        FCB 1,2,2,3,2,3,3,4
_DX:
        FDB 0,1,-1,0
_DY:
        FDB 1,0,0,-1
_DIRNAME
        FCB 'N,'E,'W,'S

// -----------------------------------------
        SECT SECT_INIT
        RTS            // return to caller at end of init section
// -----------------------------------------
        SECT SECT_OTHER
        ORG INT_VECTORS_START
#include "vectors.asm"            // interrupt vectors
// -----------------------------------------