; ----------------------------------------------------------------------
; CF430G2553 is a Forth based on CamelForth 
; for the Texas Instruments MSP430 
; 
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 3 of the License, or
; (at your option) any later version.
; 
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program.  If not, see <http://www.gnu.org/licenses/>.
; 
; See LICENSE TERMS in Brads file readme.txt as well.

; ----------------------------------------------------------------------
; init430G2553.s43 - reset interupt service routine - MSP430G2553
; ----------------------------------------------------------------------

; CamelForth RAM memory map:
;   UP          User Pointer, 2 bytes
;   UAREA       User area, 32 bytes
;   UAREA+20h   HOLD area, 40 bytes, grows down from end
;   UAREA+48h   PAD buffer, 88 bytes, must follow HOLD area
;   UAREA+A0h   Parameter stack, 128 B, grows down from end
;   UAREA+120h  Return stack, 128 B, grows down from end
;   UAREA+1A0h  TIB Terminal Input Buffer, 88 bytes

; Note: All must be word-aligned.
; See also the definitions of U0, S0, and R0 in the "system variables &
; constants" area.  A task w/o terminal input requires 200h bytes.
; Double all except TIB and PAD for 32-bit CPUs.


; RAM map
; name     celles  comment
                   ; -----
UAREA_SIZE  = 16   ; UAREA 32 bytes
                   ; -----
                   ; | LSTACK: leave stack 
                   ; | grows up 
                   ; | 
                   ; V
                   ;   128 bytes
                   ; ^
                   ; |
                   ; | grows down
PSTACK_SIZE = 54   ; | PSTACK: top of parameter stack area. 
                   ; -----
                   ;   128 bytes
                   ; ^
                   ; |
                   ; | grows down
RSTACK_SIZE = 54   ; | RSTACK: top of return stack area. 

;          aligned buffers only required for terminal tasks.
; names    bytes  
                   ; ^
                   ; |
                   ; | grows down
HOLD_SIZE   = 34   ; | HOLDAREA: 
                   ; -----
PAD_SIZE    = 84   ; scratch pad
                   ; -----
TIB_SIZE    = 84   ; terminal input buffer
                   ; -----

;        PUBLIC  UP,UAREA,PADAREA,LSTACK,PSTACK,RSTACK
        PUBLIC  PADAREA,LSTACK,PSTACK,RSTACK
        PUBLIC  TIBAREA,TIB_SIZE,UAREA_SIZE
        PUBLIC  reset,main
        PUBLIC  UAREA,UP,cor
        PUBLIC  ROMDICT,RAMDICT

        EXTERN  lastword,fenceadr,NOOP
;        EXTERN  runmagic,runsmal
        EXTERN  lastword
        EXTERN  COLDIP,BOOTIP
;        EXTERN  DEBUGIP
        
        RSEG    DATA16_Z  ; uninitialized RAM segment

; RAM user area    -   system label, holds active user area 
; initialiesed by BOOT
; see hilvl UINIT

UP:       DS16 1
UAREA:    DS16 UAREA_SIZE

; cor    -     cause of reset variable, copy of IFG1
cor:      DS16 1

; LSTACK   -   start leave stack
LSTACK:
          DS16  PSTACK_SIZE ; allocate parameter stack
; PSTACK   -   top of parameter stack          
PSTACK:                     
          DS16  RSTACK_SIZE ; allocate return stack
; RSTACK   -   top of return stack 
RSTACK:                     
          DS8   HOLD_SIZE   ; allocate hold area
; HOLDAREA   -   top of hold adrea
HOLDAREA:

; PADAREA    -   start scratch pad; must follow HOLDAREA
PADAREA:  DS8   PAD_SIZE

; TIBAREA    -   start Terminal Input Buffer
TIBAREA:  DS8   TIB_SIZE

; RAMDICT    -   end of system areas, start of free RAM
RAMDICT:

; ----------------------------------------------------------------------
; POWER ON RESET AND INITIALIZATION

#include "msp430G2553.h"                     ; #define controlled include file
#include "CF430G2553forth.h"                 ; header macros and register defs

        RSEG    CODE          ; place program in 'CODE' segment

; ----------------------------------------------------------------------
; MSP430G2553 Initialize system 
; (original: FR_EXP.lst and some of mecrisp (Koch)
; for LaunchPad 

; main   -   start_init, reset service routine
main:             ; Debugger requires the 'main' symbol. 
reset:            ; forth requires the reset symbol.
start_init:       ; I require the start_init symbol. ;-)

    MOV.W   #WDTPW+WDTHOLD,&WDTCTL  ; Stop watchdog timer
    MOV.B   &IFG1,&cor      ; save IFG1 to cof low byte
    
    mov.b   &CALBC1_8MHZ, &BCSCTL1   ; Set DCO
    mov.b   &CALDCO_8MHZ, &DCOCTL    ; to 8 MHz.
  
    mov.b   #006h, &P1SEL            ; Use P1.1/P1.2 for USCI_A0
    mov.b   #006h, &P1SEL2           ; Use P1.1/P1.2 for USCI_A0

; Configure UART (Koch) 
    bis.b   #UCSSEL_2,&UCA0CTL1     ;db2 SMCLK
    mov.b   #65,&UCA0BR0            ;db3 8MHz 9600 Insgesamt 833
    mov.b   #3,&UCA0BR1             ;db4 8MHz 9600
    mov.b   #UCBRS_2,&UCA0MCTL      ;db5 Modulation UCBRSx = 2
    bic.b   #UCSWRST,&UCA0CTL1      ;db6 **Initialize USCI state machine**


    clr.b   &IE1 ; Lösche die Interrupt-Flags von Oscillator Fault,
                 ; NMI, Flash-Violation.
    mov.w   #FWKEY, &FCTL1           ; Schreib- & Loeschzugriffe ausgeschaltet.
    mov.w   #FWKEY|FSSEL_1|19, &FCTL2 ; MCLK/20 for Flash Timing Generator

    mov.w   #FWKEY+LOCK, &FCTL3      ; Lock Flash memory against writing

; Enable port1 on Launchpad 
; P1.0 = LED1 red
; P1.6 = LED2 green
; P1.3 = button  
    BIS.B   #(BIT6+BIT0), &P1DIR
    BIS.B   #(BIT6+BIT3+BIT0), &P1OUT
    BIS.B   #BIT3,&P1REN ; pullup für S1 einschalten.

    MOV.B   #1,&(cor+1) ; hi byte

; setup forth registers
    MOV     #RSTACK,SP              ; set up return stack
    MOV     #PSTACK,PSP             ; set up parameter stack
    MOV     #UAREA,&UP              ; initial user pointer
    MOV     #BOOTIP,IP              ; set IP of starting word 
;    MOV     #COLDIP,IP              ; set IP of starting word 
;    MOV     #DEBUGIP,IP              ; set IP of starting word 
    MOV     #0,TOS                  ; clear top of pstack
    NEXT

init_end:

; ----------------------------------------------------------------------
; DEFAULT INTERRUPT HANDLER nullirq
         PUBLIC nullirq
isr_start:
nullirq: RETI
isr_end:

ROMDICT:
        END