;
;    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 mit Schlafmodus

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

terminal_key_schnippsel macro
;------------------------------------------------------------------------------
; ( -- c ) Holt den Tastendruck
;------------------------------------------------------------------------------
  mov #UCA0RXIFG, r7
  call #terminal_schlafroutine
  decd r4
  clr @r4
  mov.b &UCA0RXBUF, @r4
  ret
  endm

terminal_emit_schnippsel macro
;------------------------------------------------------------------------------
; ( c -- ) Gibt ein Zeichen aus
;------------------------------------------------------------------------------
  mov #UCA0TXIFG, r7
  call #terminal_schlafroutine
  mov.b @r4, &UCA0TXBUF
  drop
  ret
  endm

;------------------------------------------------------------------------------
terminal_schlafroutine:
  push sr
  dint
  nop
  bis.b r7, &ie2 ; Interruptquelle für einkommende Zeichen aktivieren

- bit.b r7, &IFG2 ; Probe, 
  jnz +
    bit #gie, @sp ; Waren Interrupts aktiviert ?
    jnc -         ; Wenn nicht, ganz normal in Schleife warten.

    bis #lpm3+gie, sr ; Wenn doch, dann kann ich den Prozessor ausschalten.
Schnuffelstelle_Terminal:
    jmp -

+ bic.b r7, &ie2 ; Interrupts für einkommende Zeichen wieder abschalten
  nop
  reti

;------------------------------------------------------------------------------
irq_wecken_tx:
irq_wecken_rx:
  cmp #Schnuffelstelle_Terminal, 2(sp)
  jne +
    bic #lpm3+gie, 0(sp)
+ reti

