\begin{document}
\title{Beispiele für Gforth\discretionary{–}{}{–}R8C}
\author{Bernd Paysan}
\maketitle

\begin{abstract}
  Auf der Wiki--Seite für das Gforth--R8C haben sich in den letzten
  paar Monaten einige Beispiele zusammengefunden, die wir hier
  abdrucken.
\end{abstract}

\begin{multicols*}{2}

\section{Lauflicht im R8C}

Ein einfaches Lauflicht, das nach dem Reset gleich losläuft, bis man über's Terminal eine Taste drückt:

\begin{verbatim}
rom
: licht!  led!  &100 ms ;
: lauf  1 licht! 2 licht! 4 licht! 8 licht!
  4 licht! 2 licht! ;
: dauerlauf  BEGIN  lauf  key?  UNTIL ;
' dauerlauf is bootmessage
ram
savesystem
\end{verbatim}

\section{ADC--Wandler im R8C}

Eine Schleife, die vom ADC liest und auf dem LCD ausgibt:

\begin{verbatim}
: adcmeter
  BEGIN  6 adc@ 0 <# #s #> lcdpage lcdtype
         &200 ms  key? UNTIL ;
\end{verbatim}

\section{Multitasker}

Und hier gibt's einen ganz einfachen Multitasker, der genau \emph{einen} Hintergrundtask erlaubt:

\begin{verbatim}
rom

Variable bgtask ram $20 cells allot rom
:noname  bgtask @ 0= ?EXIT
    rp@ bgtask @ sp@ cell+ bgtask ! sp! rp! ;
IS pause
: task r> bgtask $20 cells + !
  bgtask $20 cells + bgtask $10 cells + !
  bgtask $10 cells + bgtask ! ;
:noname echo @ IF
     BEGIN pause key? UNTIL THEN (key) ;
is key

ram
\end{verbatim}

Als Beispiel eine adaptierte Version des Lauflichts von oben:

\begin{verbatim}
rom
: licht!  led!  &100 ms ;
: lauf  1 licht! 2 licht! 4 licht! 8 licht!
  4 licht! 2 licht! ;
: dauerlauf
  task  BEGIN  lauf  AGAIN ;
ram
\end{verbatim}

\section{Lauftext}

Analog zum Lauflicht kann man auch einen Lauftext erzeugen --- und über das Poti die Textgeschwindigkeit einstellen:

\begin{verbatim}
rom

Create text
," GNU Forth EC R8C -- Mikroprozessor -- "
Create ledtable 1 c, 2 c, 4 c, 8 c, 4 c, 2 c,
Variable /text

: lauftext  task
  BEGIN  text count /text @ over mod /string
         16 min dup >r lcdpage lcdtype
         r@ 16 < IF  text 1+ 16 r@ - lcdtype
         THEN
         rdrop 1 /text +!
         /text @ 6 mod ledtable + c@ led!
         6 adc@ 2/ ms
  AGAIN ;

ram
\end{verbatim}

\end{multicols*}
\end{document}