Jump to content
IGNORED

From Basic to Forth - Issue 2 - reading the Keyboard


cas

Recommended Posts

This issue for our Forth course will show two different ways to read a keypress from the keyboard. The first one is the same technique used in the BASIC Program, the second is portable across many F83 Standard and ANSI Standard Forth versions unsing the Forth "key" and "key?" words

 

Basic Version

10 REM READ A KEYPRESS
20 PRINT "PLEASE PRESS A KEY"
30 POKE 764,255
35 IF PEEK(764)=255 THEN 35
36 KEY=PEEK(764)
40 CLOSE #1:OPEN #1,4,0,"K:":GET #1,A
50 PRINT "THE KEYCODE OF"; CHR$(A); " IS "; KEY
60 GOTO 30

 

volksForth version reading directly from the shasow registers

 

: READKEY ( Read Keyboard direct from Shadow register )
 ." press a key" CR  ( print text and a Carriage Return )
 255 764 C!			( initialize internal memory location 764 [CH] with 255 = no key pressed )
 BEGIN				  ( starting "endless" loop )
	764 C@		  ( read 1 byte [char] from memory location 764 and place it on the stack )
	255 < IF		 ( if the value is less then 255 )
	   764 C@	   ( we read the value again, because the comparison has consumed the orevious read value )
	   ." the keycode of " DUP EMIT ."  is " . CR ( print the keycode char and it's value )
	   255 764 C!   ( reset the memory location 764 )
	THEN			  ( end of IF )
 REPEAT;			   ( endless loop )

 

alternative version, using portable Forth words

 

KEY? leaves a flag on the stack, TRUE (-1) if a keypress is waiting, FALSE (0) if no keypress is waiting.

KEY places the ASCII Value of the Keypress on the stack

EMIT prints the ASCII Character of the topmost stackvalue

"." (dot) print the numerical value of the topmost stackitem

BEGIN ... REPEAT creates a loop without exit condition (endless loop)

DUP duplicates the topmost stackitem

 

: READKEY ( Read Keycode with Forth KEY Words )
 ." press a key" CR
 BEGIN
	KEY? IF
	   KEY
	  ." the keycode of "  DUP EMIT ." is "  . CR
	THEN
 REPEAT;

Link to comment
Share on other sites

Hi Carsten-

 

OK, you got me curious enough to get out my old "Starting Forth" (Leo Brodie) book. You've used "C!" several times, and according to SF, that means "store an 8-bit value to memory" while C@ means "fetch an 8-bit value from memory."

 

Do you have a list available of the standard words used in VolksForth? (Or do they pretty much follow the Brodie book model?)

 

FWIW: In addition to the RPN, many/most Forth words lack any degree of intuitiveness, and there are a bunch of them. Not the end of the world, but certainly an impediment to learning. Does Forth have any pre-processors as are available in C, or are there Forth dialects that would have more intuitive words? For instance, instead of "C!" why not "STOR8" for example?

 

A related question -- is Forth (in general) tokenized internally for quicker processing?

 

-Larry

Edited by Larry
Link to comment
Share on other sites

Hi Carsten-

 

OK, you got me curious enough to get out my old "Starting Forth" (Leo Brodie) book. You've used "C!" several times, and according to SF, that means "store an 8-bit value to memory" while C@ means "fetch an 8-bit value from memory."

 

Do you have a list available of the standard words used in VolksForth? (Or do they pretty much follow the Brodie book model?)

 

FWIW: In addition to the RPN, many/most Forth words lack any degree of intuitiveness, and there are a bunch of them. Not the end of the world, but certainly an impediment to learning. Does Forth have any pre-processors as are available in C, or are there Forth dialects that would have more intuitive words? For instance, instead of "C!" why not "STOR8" for example?

 

A related question -- is Forth (in general) tokenized internally for quicker processing?

 

-Larry

 

Hallo Larry,

 

I'll try to answer your questions one by one.

 

1) The Leo Brodie book. VolksForth is a Forth that is designed after the Forth 83 standard. The early issues of Starting Forth are for Forth 79 or FIG-Forth. There are several things different between the Forth in the book and volksForth. The volksForth Handbook (german) has an chapter on all differences between Starting Forth and volksForth. I will translate that chapter next. Later versions of the book cover Forth 83, if I recall correctly. There is also a online version of Starting Forth available, that is updated to ANSI Forth standard, which is more similar to the Forth 83 standard.

 

2) list of words if volksForth: yes, that is available, but only in german language. I'm working on a translation.

 

3) volksForth has an ALIAS word. With ALIAS you can rename allmost all words. For example:

 

 

' C! ALIAS STORE8

 

this is a single quote ( ' ) (called "tick"), then the existing word, the word ALIAS and then the new word.

 

After that you can use STORE8 instead of C!, but then your source is non-standard.

 

The naming of the Forth standard words follow a certain schema, but I takes some time to learn this

 

4) Forth internal representation: VolksForth is a indirect threaded Forth. There is also direct threaded Forth, token-threaded Forth or native-code Forth. The "Moving Forth Papers" have a very good explanation on the pros and cons of the different threading techniques

 

a short table for 16bit Forth on 6502 architecture (like volksForth, other CPU architectures may vary, depending on the CPU instruction set)

 

technique	  speed	   size
-----------------------------------------------------------------------------
token			 slow		 very compact (1 byte per word)
indirect		  ok			 compact (2 bytes per word)
direct			 ok			 larger (3 bytes per word)
native-code   fast		   big (3 - ~20 bytes per word)

 

native code Forth compile to direct machine code, like C-Compilers. Their execution speed is comparable as C-Compilers. Big-Forth (Atari ST, MS-DOS and Linux) is a sucessor of volksForth that is a native code compiling Forth.

 

Best regards

 

Carsten

Link to comment
Share on other sites

Hello Larry,

 

on the "Starting Forth" Book: My notes about volksForth differences to "Starting Forth" are references to the german translation of the 1st edition of the book. translating that back into english is probably not so much useful.

 

There is now an official, updated (2007) version of "Starting Forth" online --> http://www.forth.com/starting-forth/ (updated by Marcel Hendrix and Forth Inc.)

 

I will go through that new version and will not all differences between the Forth used in the online version (an ANSI Forth, such as iForth or SwiftForth) and volksForth.

 

So if you find an example in your book, that does not work with volksForth, take a look at the online version. I will try to have the differences document online during the next days.

 

Carsten

Link to comment
Share on other sites

The latest Forth 200x Standard (October 2007) is helpful getting an overview of words that exist in (ANSI) Standard Forth --> http://forth200x.org/documents/forth07-2.pdf

 

Although VolksFORTH is not 100% ANSI Forth compatible, the standard document is my guiding line whenever I implement new words for VolksFORTH.

 

If you find a Word in the Standard that is currently not defined in VolksFORTH, and you cannot define it yourself, let me know, and I will try to implement it.

 

Carsten

Edited by cas
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...