View Full Version : CREATE with , vs. P,
DennisD
01-26-05, 06:00 PM
A lot of example FORTH code I find on the Web uses CREATE to initialize arrays. Most this code is not written for Harvard-architecture machines, so I'm trying to determine how best to port it to a 'Pod.
Is it ok to do something like:
CREATE MYARRAY 10 , 20 , 30 , 40 , EEWORD
SAVE-RAM
or do I have to create the values in Flash and copy them to RAM, the way I see Mike Keesling doing in the example Lynx6 inverse kinematics code on the download page, e.g. something like:
: ARRAYOF4 < BUILDS HERE P, 8 ALLOT DOES> P@ ; EEWORD
\ note: I know there's no space after the < in < BUILDS, but the msg editor is taking it as an editing command if I string them together...
ARRAYOF4 RAMARRAY
CREATE FLASHARRAY 10 P, 20 P, 30 P, 40 P, EEWORD
6 0 DO FLASHARRAY I + P@ RAMARRAY I + ! 2 +LOOP
SAVE-RAM
? Can someone explain the difference in end result between the two snippets above?
Thanks,
Dennis
nmitech
01-27-05, 10:29 AM
Here is a related thread,
http://www.newmicros.com/discussion/showthread.php?postid=2931
RMDumse
01-27-05, 10:34 AM
I think the question there, Dennis, is whether you want a array of constants or an array of variables. If you use Mike's approach with P, will put the values into the program space. When moved to Flash with EEWORD, they will be permanent, frozen, or constant and cannot be (easily) changed. The operation for getting those values out of the program space has to be done with the P@.
On the other hand if you use , the value will be placed in RAM !at download time!. So the array of RAM variables will be initialized only by the download. At power up, or after running and storing something else in this location, the value can be different. The SAVR-RAM feature does save and reload the RAM values into FLASH, but this only ensures initial value loading. Subsequent changes will modify the value, so it is a variable. To get that value, a @ should be used.
And no, as I see the two examples, they don't seem to be much different. Here's one thing though, you can force initialization with the array in RAM if you have the copy in Flash. You can't as easily reset the from SAVE-RAM without changing everything else in RAM
DennisD
01-27-05, 03:37 PM
Thanks for the replies. Some of the tables are constants (in which case I'll just use P,), and some are variables. Your point about being able to recover the initial values from Flash is a good one; I guess for that reason I'll build tables in Program memory and move it to Data memory whenever I want to reinitialize.
On a related note, what is the difference between
CREATE MYTABLE 10 ALLOT EEWORD
and
: TABLEOF10 < BUILDS HERE P, 10 ALLOT DOES> P@ ; EEWORD
TABLE0F10 MYTABLE EEWORD
? Is the effect on both program and data memory the same in both cases?
I'm trying to understand the difference in usage between CREATE and < BUILDS DOES>. Is it just that < BUILDS DOES> is only usable inside a word definition, and CREATE is immediate?
Thanks,
Dennis
RMDumse
01-27-05, 04:17 PM
Dennis, I think something didn't come through on the previous post. I think you're trying to figure the difference between CREATE and BUILDS DOES (those are missing the greater than and less than signs because the form editor seems to eat those).
I think the answer is one more cell. CREATE is essentially the same as VARIABLE. BUILDS compiles a pointer and a variable.
I'll look at this further
DennisD
01-27-05, 04:22 PM
Oops, sorry, I edited my post to put spaces after the < in < BUILDS, so it should show up now.
Thanks,
Dennis
RMDumse
01-27-05, 04:47 PM
Okay, I can read that better now.
The code is the same in both cases.
However, you now have defined a defining word (high zute, big magic in Forth) so now you can create many more 10 locations arrays with your defining word, TABLEOF10.
DennisD
01-27-05, 05:27 PM
Ok, it's becoming clearer. One final question on CREATE. Does it actually reserve a cell of memory, or does it just associate a name with the current address? In other words, does
CREATE MYTABLE 10 ALLOT
actually allocate 11 words, one due to CREATE and 10 due to ALLOT? Should I ALLOT 1 less than the size of array I want?
RMDumse
01-27-05, 05:32 PM
Well, I could work this out for you and report, or I could tell you a quick way to figure it out for yourself. Let me assume the "teach a guy to fish..." postition and have you figure it out and report to every one else.
If you did
CREATE MYTABLE1
and
CREATE MYTABLE2
if you do
MYTABLE1 . MYTABLE2 .
do they come up the same number? If they're the same number, then it allots nothing. If they are one apart, then one location is created.
Without testing, I'd assume CREATE does not actually allot anyting. So what is the truth of my speculation?
RMDumse
02-28-05, 12:36 PM
Well, haven't heard anything back, so I don't know if we got anywhere. So...
Here's a section from the new manual:
12.16.4. <BUILDS DOES>
"Defining words" created with <BUILDS and DOES> may have a variety of purposes. Sometimes they are used to build Data objects in RAM, and sometimes they are used to build objects in ROM (i.e., in Program space). In the <BUILDS code you can allocate either space by using the appropriate memory operators.
Program Space
...
CFAč Code Field
PFAč DOES> Action Pointer
Allocate with PHERE PALLOTP, PC,
...
Data Space
...
Allocate with HERE ALLOT, C,
...
For maximum flexibility, DOES> will leave on the stack the address in Program space of the user-allocated data. If you need to allocate data in Data space, you must also store (in Program space) a pointer to that data. For example, here is how you might define VARIABLE using <BUILDS and DOES>.
: VARIABLE
<BUILDS Defines a new Forth word, header and empty body;
HERE gets the address in Data space (HERE) and appends that to Program space;
0 , appends a zero cell to Data space.
DOES> The "run-time" action will start with the Program address on the stack;
P@ fetch the cell stored at that address (a pointer to Data) and return that.
;
This constructs the following:
Program Space
...
CFAč Code Field
PFAč DOES> Action Pointer
RAM pointer
...
Data Space
...
0 (data)
...
Words with constant data, on the other hand, can be allocated entirely in Program space. Here's how you might define CONSTANT:
: CONSTANT ( n -- )
<BUILDS Defines a new Forth word, header and empty body;
P, appends the constant value (n) to Program space.
DOES> The "run-time" action will start with the Program address on the stack;
P@ fetch the cell stored at that address (the constant) and return that.
;
This constructs the following:
Program Space
...
CFAč Code Field
PFAč DOES> Action Pointer
N (constant value)
...
Data Space
...
RMDumse
02-28-05, 12:44 PM
Well, haven't heard anything back, so I don't know if we got anywhere. So...
(Notice, I had trouble with the << and >> so I put doubles in with spaces. Ignore.)
Here's a section from the new manual:
12.16.4. << BUILDS DOES >>
"Defining words" created with << BUILDS and DOES >> may have a variety of purposes. Sometimes they are used to build Data objects in RAM, and sometimes they are used to build objects in ROM (i.e., in Program space). In the <BUILDS code you can allocate either space by using the appropriate memory operators.
Program Space
...
CFAč Code Field
PFAč DOES >> Action Pointer
Allocate with PHERE PALLOTP, PC,
...
Data Space
...
Allocate with HERE ALLOT, C,
...
For maximum flexibility, DOES >> will leave on the stack the address in Program space of the user-allocated data. If you need to allocate data in Data space, you must also store (in Program space) a pointer to that data. For example, here is how you might define VARIABLE using << BUILDS and DOES >>.
: VARIABLE
<< BUILDS Defines a new Forth word, header and empty body;
HERE gets the address in Data space (HERE) and appends that to Program space;
0 , appends a zero cell to Data space.
DOES >> The "run-time" action will start with the Program address on the stack;
P@ fetch the cell stored at that address (a pointer to Data) and return that.
;
This constructs the following:
Program Space
...
CFAč Code Field
PFAč DOES >> Action Pointer
RAM pointer
...
Data Space
...
0 (data)
...
Words with constant data, on the other hand, can be allocated entirely in Program space. Here's how you might define CONSTANT:
: CONSTANT ( n -- )
<< BUILDS Defines a new Forth word, header and empty body;
P, appends the constant value (n) to Program space.
DOES >> The "run-time" action will start with the Program address on the stack;
P@ fetch the cell stored at that address (the constant) and return that.
;
This constructs the following:
Program Space
...
CFAč Code Field
PFAč DOES >> Action Pointer
N (constant value)
...
Data Space
...
vBulletin v3.0.7, Copyright ©2000-2012, Jelsoft Enterprises Ltd.