BASIC nach Forth - 14. Oktober 2006


BASIC nach Forth - 14. Oktober 2006

von cas » Sa 14. Okt 2006, 12:17
Heute gehts es um Geräusche. Folgendes Basic Programm zum Erzeugen eines Sirenentons habe ich im Buch "Atari Basic spielend lernen" gefunden:

Code: Alles auswählen
10 REM EUROPAEISCHE SIRENE
15 LOW=57:HIGH=45:P=45
20 FOR AGAIN=1 TO 20
30 SOUND 0,P,10,14
40 FOR WAIT = 1 TO 180: NEXT WAIT
50 P=LOW:LOW=HIGH:HIGH=P
60 NEXT AGAIN
70 SOUND 0,0,0,0
80 END


Im volksFORTH ist kein SOUND Befehl eingebaut, also bauen wir uns einen Atari-Basic kompatiblem Sound-Befehl in Forth:

Code: Alles auswählen
\ Atari 8bit Sound Befehl

$D200 CONSTANT AUDBASE

: SOUND ( CH# FREQ DIST VOL -- )
  SWAP $10 * + ROT DUP + AUDBASE +
  ROT OVER C! 1+ C! ;


Und nun kommt die Sirene in Forth:
Code: Alles auswählen
: SIRENE
  57 54  ( Sound Werte fuer Sirenentoene )
  20 0 DO 
     OVER
     0 SWAP 10 14 SOUND
     500 0 DO LOOP ( Warteschleife )
     SWAP  ( Toene wechseln )
  LOOP
  0 0 0 0 SOUND ;


Das BASIC Programm benutzt 5 Variablen (LOW, HIGH, P, AGAIN, WAIT), das Forth Programm kommt wieder ohne jede Variablendeklaration aus, alle Werte liegen auf dem Stack. Daher ist Forth so Speichereffizient.

Bis morgen

Carsten

von Bunsen » Sa 14. Okt 2006, 20:34
Hallo Carsten,

nette Idee diese Form eines forth-Kurses. So lernt man in kleinen Schritten Tag für Tag ein bisschen forth kennen, quasi nebenbei.

Weiter so! Gruß, Gunnar.