6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat May 11, 2024 3:22 pm

All times are UTC




Post new topic Reply to topic  [ 69 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next
Author Message
PostPosted: Fri Feb 25, 2022 9:39 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8433
Location: Southern California
IamRob wrote:
GARTHWILSON wrote:

The CFA of a primitive usually points to the body which is where the machine code is. On a secondary, the CFA points to the nest code [called DOCOL in FIG]. A secondary always has a body, whereas a primitive might not; for example DROP's CFA usually just points to the code for POP.

I would consider the code at POP to be the body for DROP. The only difference being the header is separate from the body. Which is mostly what I will be doing with this conversion.

My POP is part of the POP2, POP3, and POP4 string of instructions which various words end with are like this:
Code:
POP4:   INX2
POP3:   INX2            ; POP3 would be 4 clocks faster if we
POP2:   INX2            ; TXA, CLC, ADC #6, TAX  but it's not used
POP:    INX2            ; often enough to justify the extra bytes
        GO_NEXT         ; that the separate routine would take up.
 ;-------------------

Those are not Forth word in themselves; but in many cases it makes sense to have it separate even when they are actual words, like when it cuts into the middle of another definition and you don't want it to include the first part. For example, my EXECUTE and PERFORM are this way:
Code:
         HEADER "EXECUTE", NOT_IMMEDIATE        ; ( addr -- )
EXECUTE: PRIMITIVE
         LDA    0,X
 xeq1:   STA    W
         INX2
         JMP    W-1
 ;-------------------
         HEADER "PERFORM", NOT_IMMEDIATE        ; ( addr -- )
PERFORM: PRIMITIVE                              ; same as  @ EXECUTE
         LDA    (0,X)
         BRA    xeq1
 ;-------------------


Quote:
Obviously assuming a non-primitive word here

which is called a secondary. Primitives and secondaries are also called code definitions and colon definitions, respectively.

While I'm here, I'll comment on related terminology, since somewhere else someone recently referred to the headers as the dictionary. A dictionary includes not just the entries arranged in a way that provides a way to find them, but includes their definitions too. The whole thing is the dictionary (excluding of course the stacks and maybe something else I'm forgetting).

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 25, 2022 10:09 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10800
Location: England
Dr Jefyll wrote:
[
In FIG Forth, a definition in the dictionary has a Name Field, Link Field, Code Field and Parameter Field, in that order. The addresses at which those fields are found are the NFA, LFA, CFA and PFA (abbreviated).


I like this! A Code Field is a place where there's some interesting data. A CFA is the address of the code field. Is that it?

If I've understood correctly, and I'm not at all expert in this, then I feel perhaps a bit confused by, for example, Garth's statement
Quote:
The CFA of a primitive usually points to...

because it doesn't seem clear if that's the CFA (an address) or the CF itself (a piece of data, which might well be a two byte address.)

Likewise with IamRob's statement
Quote:
...distinguish between a word definition's CFA, and the CFA's that are compiled that make up the rest of the definition...

because it feels to me that these might not be Code Field Addresses but Code Fields.

But this might be my own confusion, and not a problem with the terminology.


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 25, 2022 10:47 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8433
Location: Southern California
It's the same kind of confusion I mentioned here. I'm not aware of any group ever having nailed it down well. FIG's word CFA takes the address of the first byte of the parameter field (the manual says the PFA) and subtracts 2 to give you the address of the first byte of the field that holds the address specifying where the code starts. So is it the code-field address? Or the code field address-specifying address? Or... We just call it the CFA though. IamRob mentioned the list of addresses in a secondary's parameter field as being CFAs, which is a slightly different use of the word and yet I don't think anyone would put up any argument. ANS, in an effort to make something that works also for high-end computers' Forths which I don't intend to get into, calls it an "execution token," "XT" for short; but header structure (if there is any) may be totally different from what we normally deal with in the 65 world.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 25, 2022 6:14 pm 
Offline

Joined: Fri Apr 15, 2016 1:03 am
Posts: 136
DECIMAL & HEX are overwriting TOS in A.


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 25, 2022 7:51 pm 
Offline

Joined: Sun Apr 26, 2020 3:08 am
Posts: 357
leepivonka wrote:
DECIMAL & HEX are overwriting TOS in A.

Thanks for following along and trouble shooting. Those were supposed to be LDY and STY. Will correct that.


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 25, 2022 8:27 pm 
Offline

Joined: Sun Apr 26, 2020 3:08 am
Posts: 357
GARTHWILSON wrote:
It's the same kind of confusion I mentioned here. I'm not aware of any group ever having nailed it down well. FIG's word CFA takes the address of the first byte of the parameter field (the manual says the PFA) and subtracts 2 to give you the address of the first byte of the field that holds the address specifying where the code starts. So is it the code-field address? Or the code field address-specifying address? Or... We just call it the CFA though. IamRob mentioned the list of addresses in a secondary's parameter field as being CFAs, which is a slightly different use of the word and yet I don't think anyone would put up any argument. ANS, in an effort to make something that works also for high-end computers' Forths which I don't intend to get into, calls it an "execution token," "XT" for short; but header structure (if there is any) may be totally different from what we normally deal with in the 65 world.

A lot of what you are saying here is a large part of the reason for so much confusion, especially for beginners. And basically, I feel some of these references need to stop being quoted over and over again.

Who's to say why we can't make up, adjust or even re-write any standards ourselves to make a better standard, or at least a better understood one? Obviously the people who wrote those standards in the first place maybe didn't see or have the entire picture.

I can see and understand the Code Field area of a definition being better understood as being the "Body" of a definition better than calling the primitive part of a definition, "The Body".

"The Body", would also not include the CFA, as that is part of the header. And, "The Body" should not include primitives as primitives do not necessarily have to be joined to the header, but Code Field addresses always are.

The CFA though, can point to a primitive, or it can point to code that interprets what is in the body. I am using the word "primitive" to mean code that is created to indicate the intention of a word without the help of interpreted words or a higher language.

Therefore, there would basically be a Header, a Code Field Address (the CFA is named separately due to its duel use) and a Body. The Code Field Address always points to low level code. But the low level code can have one of two uses. It can be used to handle parameters directly (making it a primitive), or it can be used to interpret whatever is in the "The Body". Whether it be a number for a variable or constant, or, a compiled CFA of a word.

Does this sound more reasonably understood by everyone? Anyone game for creating our own standard and making it accessible to everyone?


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 25, 2022 9:51 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8433
Location: Southern California
IamRob wrote:
Who's to say why we can't make up, adjust or even re-write any standards ourselves to make a better standard, or at least a better understood one? Obviously the people who wrote those standards in the first place maybe didn't see or have the entire picture.

<snip>

Does this sound more reasonably understood by everyone?

More understood? I don't know; although I don't really find anything incorrect in what you wrote. Some of the innards like how the threading works, and does>, etc. can be real brain twisters to understand; but once they're in place and working, we can close the hood ("bonnet" for the British) on them and leave the engine smoothly running, hidden underneath, and there's seldom a need to re-visit them.

Quote:
Anyone game for creating our own standard and making it accessible to everyone?

I would just say, "Be careful." It only becomes a standard if it becomes widely adopted. Making one's own "standard" (which is not a standard) is a great strength of Forth, but also a great criticism of Forth. Forth has always given us the tools to do whatever we want, unlike other languages; but I also remember reading in Forth Dimension magazine, before the internet, that the three biggest obstacles to Forth's acceptance are: No effective standard, no effective standard, and no effective standard. Even at the time, I kind of wondered what they meant, because Forth 83 was probably the most common at the time. Perhaps it was that it was not very suitable for the high-end 32-bit processors that were becoming more common, so that field of Forth was turning into the Wild West.

Then the X3J14 committee laboriously came up with ANS Forth '94, and although they did some good things, I viewed other parts as not being very suitable for 6502. As I read Jack Woehr's book "Forth: The New Model," about the committee's work, it became clear how difficult (or impossible) a job it was to come up with something that would make everyone happy. There had to be some compromises.

In any case, when I have a need for a new word, I try to use names and stack inputs and outputs that conforms to what someone else is doing, to avoid being part of the "no-effective-standard" problem.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Sat Feb 26, 2022 8:18 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10800
Location: England
For me, a picture or a diagram is always a huge help. It takes a lot of words to do the same work, and if we're not all agreed, with complete clarity, on what the words mean, even more so.


Top
 Profile  
Reply with quote  
PostPosted: Sat Feb 26, 2022 8:56 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8433
Location: Southern California
BigEd wrote:
a picture or a diagram is always a huge help.

Yes, always worth a reminder. (Somehow laziness dictates that it's easier to type a thousand words than to find or draw an appropriate diagram or two and make them presentable; yet the reader will shy away from the long writings.) I gave a diagram of the word structure here, and our own Dr. Brad Rodriguez has a good article on the different threading methods at http://www.bradrodriguez.com/papers/moving1.htm, with diagrams, yet somehow I'm sure all this could still be made more clear. I'm constantly straining for that on my website, and never satisfied (although I don't have any articles that directly address Forth. The closest is doing things in assembly language in a very Forth-like way, without actually using Forth.) I'm afraid the terminology details IamRob raises above will never be fully resolved though. For anyone new to Forth, I would still recommend Leo Brodie's book, "Starting Forth." It's available online in a somewhat modernized version but with the original cartoons, at http://www.forth.com/starting-forth/index.html.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Sat Feb 26, 2022 4:02 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10800
Location: England
Thanks Garth, very helpful (to me at least). From Moving Forth:


Image

It feels like part of the problem (at least in my head) is that the Code Field is certainly a field, and always contains an address, and so perhaps it feels natural to speak of it as the CFA, whereas the address of the Code Field would in my mind be the thing to refer to as the CFA. So it seems that most uses of the term CFA are actually meaning the Code Field.


Top
 Profile  
Reply with quote  
PostPosted: Sat Feb 26, 2022 8:43 pm 
Offline

Joined: Wed Aug 21, 2019 6:10 pm
Posts: 217
Part of the confusion may be that the contents of the code field IS an address -- an address of executable code. Sometimes people MEAN "the address in the code field", but instead WRITE "CFA", or, IOW, the address OF the code field, not the one IN the code field.

Perhaps that is happening in one or two of the examples given.

That is also why there is no code field in subroutine, direct or bit threaded code... in subroutine or direct threaded code, the word always starts with executable code, and in bit threaded code, whether it is a secondary or a primitive is inferred from its entry in the compiled definition.

JSR DUP : JSR ONEPL : JSR SWAP : JMP CFETCH
; No code field

JSR ENTER : !word DUP,ONEPL,SWAP,CFETCH,EXIT
; No code field

!word DUP+$80,ONEPL+$80,SWAP+$80
!word CFETCH+$80,EXIT+$80
; No code field


Top
 Profile  
Reply with quote  
PostPosted: Sat Feb 26, 2022 9:10 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8433
Location: Southern California
There again it might be a matter of terminology. I would say for example that STC does have a CFA, but its use is limited to compilation, and the compiler uses it to look up the address to put in the operand field of a JSR instruction. (There may be more to it too, for example so it knows that if it's shorter than a threshold you choose, just straightline it rather than putting in the JSR/RTS overhead.)

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 27, 2022 2:40 am 
Offline

Joined: Wed Aug 21, 2019 6:10 pm
Posts: 217
GARTHWILSON wrote:
There again it might be a matter of terminology. I would say for example that STC does have a CFA, but its use is limited to compilation, and the compiler uses it to look up the address to put in the operand field of a JSR instruction. (There may be more to it too, for example so it knows that if it's shorter than a threshold you choose, just straightline it rather than putting in the JSR/RTS overhead.)


But there is no code field, so in what sense does it have an address?

"CFA" is not a generic term for Forth, it's a term specific to Forth implemented with indirect threaded code (or some other code done with a similar indirection).

In other words, the address to put into the operand field of a JSR instruction is NOT the address of a field that contains the address of an executable routine. The address to compile for direct threaded cde is NOT the address of a field that contains the address of an executable routine. Both are the addresses of executable routines themselves.

"Compile a list of values that specify which words to execute" is something that every Forth will do. "Compile a list of values that point to the address of a field that contains an address of an executable routine" is something that an Indirect Threaded model Forth will do ...

... after all, that is the INDIRECT in Indirect Threaded: point to something which points to the thing that you execute.

Code:
EXIT:  PLX
NEXT:   INX
        INX
        JMP ($0000,X)

ENTER: PHX
        STX W
        LDA $0000,X
        TAX
        INX
        INX
        INX
        JMP ($0000,X)


... doesn't USE "an address that points to an address that points to executable code" to do that, it uses an address that points directly to executable code. The trade-off is that a compiled definition has to be prefaced by an executable stub.

Or consider a bit-threaded model where definitions are composed of cells aligned on even addresses, primitives are called with subroutine vectors (so are always odd) and compiled words called with the address of their definition. As well, executable code is all in the range of $8000-$FFFF, so any address in $0000-$7FFF is assumed to be a branch to code in the $8000-$FFFF range, rather than a call.

Code:

NEXT:   INY
        INY
NEXT1:  LDA $0000,Y
        BPL BRANCH
        BIT #$0001
        BEQ ENTER
        PHA
        RTS
ENTER:  PHY
        TAY
        BRA NEXT1
BRANCH: ORA #$8000
        TAY
        BRA NEXT1


BigEd wrote:
If I've understood correctly, and I'm not at all expert in this, then I feel perhaps a bit confused by, for example, Garth's statement
Quote:
The CFA of a primitive usually points to...

because it doesn't seem clear if that's the CFA (an address) or the CF itself (a piece of data, which might well be a two byte address.) ...


... is right, here ... for a primitive, the code field contents points to the parameter field, so for a primitive, the CFA "points to the pointer to," the parameter field". In other words, for fig-Forth primitives, the CFA is not a pointer to the primitive code, it's a handle to the primitive code.

The fig-Forth word CFA is a shorthand, which in a verbose system could be named PFA>CFA to make clear what it translates from as well as what it translates to -- but I like the fig-Forth version better, perhaps because I started with a version of fig-Forth customized for the C64.


Last edited by BruceRMcF on Sun Feb 27, 2022 11:34 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 27, 2022 3:52 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8433
Location: Southern California
BruceRMcF wrote:
GARTHWILSON wrote:
There again it might be a matter of terminology. I would say for example that STC does have a CFA, but its use is limited to compilation, and the compiler uses it to look up the address to put in the operand field of a JSR instruction. (There may be more to it too, for example so it knows that if it's shorter than a threshold you choose, just straightline it rather than putting in the JSR/RTS overhead.)

But there is no code field, so in what sense does it have an address?

In other words, the address to put into the operand field of a JSR instruction is NOT the address of a field that contains the address of an executable routine.

The operand is the beginning address of the code, ie, the executable routine, to execute. If you JSR into something that's not code, you'll crash. The term "code" does not carry any obligation to be an indirect. The terminology confusion seems to be in whether it's direct, indirect, or doubly indirect. One way or another, it still leads to a field, ie, a range of bytes, of executable code, which has a starting address; hence the term "code-field address."

Quote:
Or consider a bit-threaded model

This is interesting. I've never heard of bit-threaded, only subroutine- (with optional inline optimization), direct-, indirect-, and token-threaded code.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 27, 2022 11:39 am 
Offline

Joined: Wed Aug 21, 2019 6:10 pm
Posts: 217
GARTHWILSON wrote:
BruceRMcF wrote:
GARTHWILSON wrote:
There again it might be a matter of terminology. I would say for example that STC does have a CFA, but its use is limited to compilation, and the compiler uses it to look up the address to put in the operand field of a JSR instruction. (There may be more to it too, for example so it knows that if it's shorter than a threshold you choose, just straightline it rather than putting in the JSR/RTS overhead.)

But there is no code field, so in what sense does it have an address?

In other words, the address to put into the operand field of a JSR instruction is NOT the address of a field that contains the address of an executable routine.

The operand is the beginning address of the code, ie, the executable routine, to execute. ...


And in fig-Forth terms, the beginning address of the code of a primitive is NOT the CFA, it is the PFA. In subroutine or direct threaded Forth, the code field is snipped out and both primitives and compiled code either are executable routines ... or at least, for direct threaded compiled words, presents itself as an executable routine.
~~~~~~~~~~~

GARTHWILSON wrote:
Quote:
Or consider a bit-threaded model

This is interesting. I've never heard of bit-threaded, only subroutine- (with optional inline optimization), direct-, indirect-, and token-threaded code.


There's also token/bit-threaded. You don't have to reserve all 256 bytes for tokens ... you can reserve 128 or 127 for tokens and have one byte primitives and two byte calls.

I'm interested in trying this one out in xForth for the X16/C64, if I ever get it out of alpha state (eg, at present DOES> is borked, and I don't have time for debugging sessions at the moment). Now, I would have to experiment with what works best for the 6502, since the only example I saw, back in the 90s I think, was for the Z80. I'd have to tinker to see which allocation of stacks and IP to registers (X/Y/S) works best for the 65C02, but with primitives being a single byte dispatch, I might try a (zp,X) instruction pointer on top of the return stack.

The idea of this version is that executable code is in the high half of a 64K memory space, but now the compiled address is compiled with the high byte first, in the reverse to the normal 6502 order. The high byte of the OP vector is initialized with the page of the table of vectors of primitives ... a certain number are reserved for "FASTCODE" words, while general CODE words begin with an "embed code" token and compiled/called as compiled words.

Code:
EXIT:   INX
        INX
NEXT:   INC RS,X
        BNE NEXT0
        INC RS+1,X
NEXT0:  LDA (RS,X)
        BMI ENTER
        ASL
        STA OPCODE
        JMP (OPCODE)
ENTER:  DEX
        DEX
        STA RS+1,X
        INC RS+2,X
        BNE +
        INC RS+3,X
        LDA (RS+2,X)
        STA RS,X
        BRA NEXT0


For compiling words, you need an XT, word, so that you only compile a byte for a token and compile a word address in big-endian order. You can't get away with just using , as you can in fig-Forth.

Whether that bit-threading model has Forth words compiled into the high or low half of the memory map, the execution token for a TBTC model on the 6502 benefits from the fact that there won't be compiled code in the zero page, so EXECUTE just checks whether the high byte of the execution token is zero when deciding whether it is a primitive token or a compiled Forth word address. I think it might be something like:

Code:
EXECUTE:
        PLA
        PLY
        BNE +
        STA OPCODE
        JMP (OPCODE)
+       DEX
        DEX
        STA RS,X
        STY RS+1,X
        JMP NEXT0


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 69 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron