;
;    Mecrisp - A native code Forth implementation for MSP430 microcontrollers
;    Copyright (C) 2011  Matthias Koch
;
;    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/>.
;

; Terminalroutinen für die serielle Schnittstelle im F1612

;----------------------------------------------------------------------------
; USART 0

U0CTL           equ     070h
U0TCTL          equ     071h
U0RCTL          equ     072h
U0MCTL          equ     073h
U0BR0           equ     074h
U0BR1           equ     075h
U0RXBUF         equ     076h
U0TXBUF         equ     077h

;----------------------------------------------------------------------------
; USART 1

U1CTL           equ     078h
U1TCTL          equ     079h
U1RCTL          equ     07Ah
U1MCTL          equ     07Bh
U1BR0           equ     07Ch
U1BR1           equ     07Dh
U1RXBUF         equ     07Eh
U1TXBUF         equ     07Fh

; Konstanten für UxCTL

PENA   equ 128
PEV    equ  64
SPB    equ  32
CHAR   equ  16
LISTEN equ   8
SYNC   equ   4
MM     equ   2
SWRST  equ   1

; Konstanten für UxTCTL

CKPL   equ 64
SSEL1  equ 32
SSEL0  equ 16
URXSE  equ 8
TXWAKE equ 4
; Unused
TXEPT  equ 1

; Für Module Enable Register 1
UTXE0 equ 128
URXE0 equ 64

; Für Module Enable Register 2
UTXE1 equ 32
URXE1 equ 16

; Für IE1
UTXIE0 equ 128
URXIE0 equ 64

; Für IE2
UTXIE1 equ 32
URXIE1 equ 16

; Für IFG1
UTXIFG0 equ 128
URXIFG0 equ 64

; Für IFG2
UTXIFG1 equ 32
URXIFG1 equ 16


terminal_qkey_schnippsel macro
;------------------------------------------------------------------------------
; ( -- Flag ) Prüft, ob eine Taste gedrückt wurde
;------------------------------------------------------------------------------
  pushda #0
  bit.b #URXIFG0, &IFG1
  jz +
  mov #-1, @r4
+ ret
  endm

terminal_key_schnippsel macro
;------------------------------------------------------------------------------
; ( -- c ) Holt den Tastendruck
;------------------------------------------------------------------------------
- bit.b #URXIFG0, &IFG1
  jz -

  decd r4
  clr @r4
  mov.b &U0RXBUF, @r4
  ret
  endm

terminal_emit_schnippsel macro
;------------------------------------------------------------------------------
; ( c -- ) Gibt ein Zeichen aus
;------------------------------------------------------------------------------
- bit.b #UTXIFG0, &IFG1         ; USART0 TX buffer ready?
  jz -                          ; Jump if TX buffer not ready
  mov.b @r4, &U0TXBUF           ; TX -> RXed character
  drop
  ret
  endm

