Chapter 2

P21Forth Implementation Details

P21Forth 1.02 is a direct threaded Forth. This means that all the executable words in the dictionary must begin with executable MuP21 instructions. High level defintions created with (colon) or other defining words will be a : list of address of other words. In a direct threaded Forth high level definitions begin with machine code and then are a list of addresses. Defining the word NEWWORD in the following way,

            : NEWWORD WORD1 WORD2 WORD3 ;
Would create a new executable word called NEWWORD that would contain a list of the addresses of the words WORD1, WORD2, and WORD3. Executing the new NEWWORD word would now have the same effect as executing WORD1 then WORD2 then WORD3.

Low level words are executable words that are written in the assembler of the machine. The Forth word CODE will create a new word written in the MuP21 assembler. These words are written in the Forth assembly language of the MuP21. This is detailed in Chapter 9 on the P21Forth Assembler. Words written in the assembler may be used just like words written in the high level Forth, but they will be much faster.

P21Forth only uses 16 kilowords of memory for the OK operating system code and the initial name and code dictionaries.

          1 Megaword DRAM Memory Map

         FFFFF     Top of DRAM - NAMES
         F0000                     |
         E0000                     V
         C0000     Disk Block Buffers
         AAAAA     Video Buffer 1
         9AAAA     Video Buffer 2
         30000          ^
         20000          |
         10000     Code |
         00000     P21Forth bottom 16K
The Forth word UNUSED will place the amount of free memory between the top of the code dictionary (growing upward) and the start of the video buffer, and between the bottom of the name dictionary (growning downward) and the top of the disk block buffers and place this number on the stack. P21Forth 1.02 will initially report over 877055 words of free memory.
             Lower 16 Kiloword DRAM Memory Map

         4000   reset top of  CODE dictionary at boot
         3F00   Terminal Input Buffer
         3F00   Start of Name dictionary
         2E10   bottom of initial Name dictionary
         2882   top of initial CODE dictionary
                /   /
         880    Start of CODE dictionary
         840    Return Stack  (builds up)
         83A    User Variables 6
         800    Data Stack    (builds up)
         600    Character table in OK OS
         000    DRAM boot code in OK, OK OS
The MuP21 has a 21 bit addressing space. It addresses 1 megaword of 20 bit DRAM memory in locations 00000-FFFFF. If the carry bit is set on MuP21 in a memory addressing register the MuP21 will address one of several SRAM spaces or the I/O address space. The only address space directly available to the P21Forth is the one megaword DRAM space. P21Forth provides words to access the SRAM and I/O port address space. Internally these words set the carry bit, but P21Forth only represents numbers from 0 to FFFFF, so it is not possible to address the SRAM memory on MuP21 directly in P21Forth.

CODE Dictionary

The CODE dictionary in P21Forth is the place in memory where executable code and data is stored. Executable code is stored in Forth words. These words can be created in various ways, but are essentially high level Colon definitions or CODE words. CODE words are sequences of MuP21 assembler code that may be very fast. These words normally end in the word next which transfers execution to the next Forth word. High level words, or Colon defintions, will always begin with a short sequence of MuP21 assembler code, usually a call to routine called do:. What follows this is a list of addresses that are threaded together. The last address in the list is usually the address of the word EXIT which unthreads back to the correct calling Forth word.

NAME Dictionary

The NAME dictionary is the structure that contains word names and links to their executable code in the CODE dictionary. The NAME dictionary is used by the Forth command interpreter and the compiler. The structure of each entry in the name dictionary is as follows:

CODE-address, LINK-address, Count, char1, char2, ... , Lex

The CODE-address contains the address of the executable code or data address in the CODE dictionary. The LINK-address contains the address of the Count cell in the next entry in the NAME dictionary. The Count cell contains a number with the length of the name that follows in char1, char2, ... Finally Lex contains lexical information about this word such as if it is IMMEDIATE or COMPILE-ONLY.

Register Usage in P21Forth

The MuP21 microprocessor has very few registers. There is a data stack with 6 cells, a return stack with 4 cells, a memory addressing register (A), and a program counter. P21Forth keeps stacks in memory to support the ANS Forth environment. There are only three registers that are used accross Forth words. These are very straightforward. The T register (Top of data stack) contains SP the pointer to the Forth data stack in memory. The R register (top of the return stack) contain RP the pointer to the Forth return stack in memory. The A register holds the IP or interpreter pointer for Forth. The first things that Forth words do is to manipulate these registers and the stacks in memory that these registers point to.

P21Forth 1.02 has many words that are written in high level Colon definitions that would perform much faster if written in CODE. Some of the CODE words are functional, but could be rewritten to run much faster. The graphics bit block transfer words for instance only transfer one cell between memory and video display on each inner loop. If these words were to transfer four cells on each inner loop they would be about four times faster. At the present time GPUT will transfer about 20 full screen graphics per second, it could be optimized to run about four times that fast.

End of Chapter 2

Return to Table of Contents

Previous Chapter

Next Chapter