Create in FigForth

Topics relating to various Forth models on the 6502, 65816, and related microprocessors and microcontrollers.
Post Reply
powersoft_51
Posts: 51
Joined: 22 Jun 2024

Create in FigForth

Post by powersoft_51 »

I have a running FigForth on my esp32 and teensy 4.1 with a 6502 emulator.
All tings are working as it should only one thing not:

COLD
fig-FORTH 1.0
CREATE JAN 100 ALLOT OK
JAN JAN ? MSG # 0 :cry:

It seems at the first okee, but when I won't to know the address of JAN it gives error # 0

Anny suggestion to solve this?

Cheers,
Jan
BruceRMcF
Posts: 388
Joined: 21 Aug 2019

Re: Create in FigForth

Post by BruceRMcF »

powersoft_51 wrote:
I have a running FigForth on my esp32 and teensy 4.1 with a 6502 emulator.
All tings are working as it should only one thing not:

COLD
fig-FORTH 1.0
CREATE JAN 100 ALLOT OK
JAN JAN ? MSG # 0 :cry:

It seems at the first okee, but when I won't to know the address of JAN it gives error # 0

Anny suggestion to solve this?

Cheers,
Jan
What's the code that you have for CREATE?

Because error message 0 is it can't be found in the dictionary, so it looks like the word you are creating either is still smudged or is not getting properly linked into the dictionary.
powersoft_51
Posts: 51
Joined: 22 Jun 2024

Re: Create in FigForth

Post by powersoft_51 »

This is the code in my assembler.

What I find strange is that the word constant which also
contains the word create works fine. Could there be
a word that makes sure that create does not work as
described earlier!

Code: Select all

;
;                                       CREATE
;                                       SCREEN 50 LINE 2
;
L2142     .BYTE $86,"CREAT",$C5
          .WORD L2113    ; link to ID
CREAT     .WORD DOCOL
          .WORD TIB      ;)
          .WORD HERE     ;|
          .WORD CLIT     ;|  6502 only, assures
          .BYTE $A0      ;|  room exists in dict.
          .WORD PLUS     ;|
          .WORD ULESS    ;|
          .WORD TWO      ;|
          .WORD QERR     ;)
          .WORD DFIND
          .WORD ZBRAN
L2155     .WORD $0F
          .WORD DROP
          .WORD NFA
          .WORD IDDOT
          .WORD CLIT
          .BYTE 4
          .WORD MESS
          .WORD SPACE
L2163     .WORD HERE
          .WORD DUP
          .WORD CAT
          .WORD WIDTH
          .WORD AT
          .WORD MIN
          .WORD ONEP
          .WORD ALLOT
          .WORD DP       ;)
          .WORD CAT      ;| 6502 only. The code field
          .WORD CLIT     ;| must not straddle page
          .BYTE $FD      ;| boundaries
          .WORD EQUAL    ;|
          .WORD ALLOT    ;)
          .WORD DUP
          .WORD CLIT
          .BYTE $A0
          .WORD TOGGL
          .WORD HERE
          .WORD ONE
          .WORD SUB
          .WORD CLIT
          .BYTE $80
          .WORD TOGGL
          .WORD LATES
          .WORD COMMA
          .WORD CURR
          .WORD AT
          .WORD STORE
          .WORD HERE
          .WORD TWOP
          .WORD COMMA
          .WORD SEMIS
enso1
Posts: 197
Joined: 30 Jul 2024

Re: Create in FigForth

Post by enso1 »

I think CREATE and DOES are meant to be inside a definition?

I've never seen anyone enter CREATE at the outer loop. But then again, I don't think I've ever seen any Forth programmers in person.
BruceRMcF
Posts: 388
Joined: 21 Aug 2019

Re: Create in FigForth

Post by BruceRMcF »

powersoft_51 wrote:
This is the code in my assembler.

What I find strange is that the word constant which also
contains the word create works fine. Could there be
a word that makes sure that create does not work as
described earlier!
The CONSTANT definition has SMUDGE in it, which itself is defined with TOGGLE, so it's toggles the smudge bit on or off.

Note from Dr. Ting's fig-Forth system manual that fig-Forth CREATE leaves the code field pointing to the parameter field, ready to compile a code definition, so I would expect that -- unlike ANS Forth CREATE -- it would remain smudged until after the code definition has been completed by ;CODE

If you unsmudge the word you created, then execute it, there is no code in the parameter field to be executed.
enso1
Posts: 197
Joined: 30 Jul 2024

Re: Create in FigForth

Post by enso1 »

Aren't fig error messages taken from the first block or something like that?
BruceRMcF
Posts: 388
Joined: 21 Aug 2019

Re: Create in FigForth

Post by BruceRMcF »

enso1 wrote:
Aren't fig error messages taken from the first block or something like that?
It's been so long since I've used a fig-Forth based system that I just refer to Dr. Ting's fig-Forth system guide for these kind of questions. On page 32, it says that if a the WARN user variable says that disk blocks are present, error messages start on SCREEN 4 of Drive 4, while if WARN contains 0, the error number if returned -- so yes, that is part of how a complete fig-Forth system could often fit in 8KB of RAM.

Some elaborations of a BLOCK system will put the drive number (whether actual or virtual) in the top two bits of the SCREEN number ... so having the error messages in Drive 0 means it's still SCREEN 4 in a "blocks zone" system. With 1KB Screens, 14bits available for the individual drive is 16MB, which in the early days of hard drives could be bigger than the largest hard drive in the system with room to spare.

Later on, as block files became more common, if you won't ever have more than 4MB in a block file, you can do something similar with the base block file based on the block number and up to 15 block files that can be located at 4MB boundaries ... 15 is just an example, but with 15, then a block in the base block file can be dedicated to keeping track of the block files placed at the 4MB boundaries, so the top 4 bits of the BLOCK number is the index into the installed block files.

In fig Forth, doing the first kind might be fairly straightforward, since it's at the SCREEN level. An SD card with a FAT16 file system could well have a set of 16MB files written to them. Sorting out the base sector of each file could be used to define the starting points that can be set-up as virtual drives 0-3.
Last edited by BruceRMcF on Sun Mar 30, 2025 9:28 pm, edited 1 time in total.
enso1
Posts: 197
Joined: 30 Jul 2024

Re: Create in FigForth

Post by enso1 »

Yeah looking at blocks from a PDP-11 forth,

Code: Select all

#Screen #   4
 0 (  ERROR, WARNING, AND OTHER MESSAGES - SCREENS 4 AND 5 )       
 1 EMPTY STACK                                                     
 2 STACK OR DICTIONARY FULL                                        
 3 HAS INCORRECT ADDRESS MODE                                      
 4 ISN'T UNIQUE                                                    
 5                                                                 
 6 DISC RANGE                                                      
...
Screen #   5
 0 (  ERROR MESSAGES, CONTINUED  )                                 
 1 COMPILATION ONLY, USE IN DEFINITION                             
 2 EXECUTION ONLY                                                  
 3 CONDITIONALS NOT PAIRED                                         
 4 DEFINITION NOT FINISHED                                         
 5 IN PROTECTED DICTIONARY                                         
 6 USE ONLY WHEN LOADING                                           
BruceRMcF
Posts: 388
Joined: 21 Aug 2019

Re: Create in FigForth

Post by BruceRMcF »

enso1 wrote:
I think CREATE and DOES are meant to be inside a definition?

I've never seen anyone enter CREATE at the outer loop. But then again, I don't think I've ever seen any Forth programmers in person.
CREATE and DOES> in more recent Forths don't work exactly the same as in fig-Forth, since in the 80's, people were starting to develop direct threaded Forth compilers and the fig-Forth approach was tailored to an indirect threaded Forth.

While CREATE is a low level factor of CONSTANT : and then indirectly through CONSTANT for VARIABLE USER and <BUILDS ... where <BUILDS is the word closest to the more modern version of CREATE ... it can indeed be used on the command line, but someone would have to know what they are doing to get functioning 6502 code into the parameter field.
Dietrich
Posts: 45
Joined: 01 Jan 2003

Re: Create in FigForth

Post by Dietrich »

Your Fig-Forth code is ok and behaves as it should.
However, CREATE is one of the more complex Forth words, because it enters the header of a new FORTH primitive into the dictionary and - in Fig-Forth! - marks this header as imcomplete - SMUDGE bit is set, meaning the outer interpreter and thus also the compiler will not see the new word. To complete the new definition just created, the Forth programmer must in addition specify, what the word has to do during compile time and during run time, adapt the header info accordingly and rset the smudge bit in the header.
Depending on what you want to achieve, this can be non trivial. Fortunately Forth has the required words to define the more common structures already available, so in most cases you don't need CREATE.

I'm aware, that all this is not really helpful and a bit confusing - so here is a simple example, how to create a defining word to create a variable

Code: Select all

: var create smudge , does> @ ;

;s
The code first creates the defining word VAR which generates a word header (CREATE), clears the SMUDGE bit and copies the number on top of the stack into the next dictionary location. This creates a data structure, which can hold a variable (2 bytes), but is not yet initialized. On runtime (DOES>) the address of the newly defined data structure is put on the stack (@) and can be used by the programmer.
Here is how to run it:

Code: Select all

0 var test ok
100 test ! ok
test @ . 100 ok
Of course this code as such is pretty useless because its simply a primitive high level variant of the VARIABLE word in the Fig Forth kernel, but I hope it makes things a bit more transparent.

have fun with Forth
Dietrich
My system: Elektor Junior Computer, GitHub https://github.com/Dietrich-L
powersoft_51
Posts: 51
Joined: 22 Jun 2024

Re: Create in FigForth

Post by powersoft_51 »

Thanks Dietrich for your reply.
In the book Starting Forth I found a remark about using create in FigForth.

When create is rewritten to

: create <builds does> ;

It is working without crashing my version of FigForth.

I can use create as mention in the book.
Now it is working for example create MyArray 10 , 20 ,

When give in MyArray the address is pushing on the stack, as I wonted.
BruceRMcF
Posts: 388
Joined: 21 Aug 2019

Re: Create in FigForth

Post by BruceRMcF »

powersoft_51 wrote:
Thanks Dietrich for your reply.
In the book Starting Forth I found a remark about using create in FigForth.

When create is rewritten to

: create <builds does> ;

It is working without crashing my version of FigForth.

I can use create as mention in the book.
Now it is working for example create MyArray 10 , 20 ,

When give in MyArray the address is pushing on the stack, as I wonted.
Yes, that looks to be defining a Forth-79 CREATE in fig-Forth.

The Forth-79 CREATE specification was adopted by ANS Forth ("Forth94") and Forth2012, so there would be a fair amount of Forth source from the 80's forward using CREATE in that sense, rather than the fig-Forth sense.

But in a fig-Forth system, that ought to be OK, since CREATE in fig-Forth is a compiler building block word, not one frequently used in new definitions.

To double check that it actually works like ANS Forth CREATE, off the top of my head, I think you can test it with:

: MakeMyConstant ( x "name" -- ) CREATE ( -- x ) , DOES> @ ;
10 MakeMyConstant MyConstant
MyConstant .
<<10 OK>>

One idea is to put the compatibility layer for Forth79 or Forth94 source in a vocabulary, to be able to bring it in when borrowing from existing Forth79 or Forth94 source.
Post Reply