6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Sep 19, 2024 9:06 pm

All times are UTC




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: Tue Sep 17, 2013 2:05 am 
Offline
User avatar

Joined: Sat Sep 29, 2012 10:15 pm
Posts: 899
I just broke my FIG-FORTH on CHOCHI after moving the binary loader. Banging my head against it. In the meantime I noticed a very ominous comment:
Code:
;
;    The following offset adjusts all code fields to avoid an
;    address ending $XXFF. This must be checked and altered on
;    any alteration , for the indirect jump at W-1 to operate !
;
;          .ORG *+2         ;.ORIGIN *+2
                nop                     
                nop                     

Is this due to the old page-wrap bug or is it actually meaningful?

_________________
In theory, there is no difference between theory and practice. In practice, there is. ...Jan van de Snepscheut


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 17, 2013 2:50 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8509
Location: Southern California
I don't think you need it for 65c02 since it corrected that bug in the NMOS one, but I kept it because it made it easier for SEE, the ANS decompiling word, IIRC. Otherwise it might have had to do with using characters above 7F in names so I could have names like I²C_ACK, ΔT, etc.. I can't remember my exact reason, and it would take time to figure it out again.

_________________
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: Tue Sep 17, 2013 3:07 am 
Offline
User avatar

Joined: Sat Sep 29, 2012 10:15 pm
Posts: 899
I am using Arlet's core - I am almost sure it doesn't have the NMOS bug.

Do you need to actually look through the assembly listing and insert nops until none of the CFA's fall on $FF?

_________________
In theory, there is no difference between theory and practice. In practice, there is. ...Jan van de Snepscheut


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 17, 2013 4:11 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8509
Location: Southern California
I didn't start with the fig-Forth listing, although I do have it here. As I'm compiling, whether with the 6502 metacompiler or having the workbench computer compile its own applications into RAM, I just have CREATE include:
Code:
   HERE 1 AND
   IF
       <add an extra byte>
   THEN
to get things going again word-aligned.

The HEADER macro in my '816 Forth kernel assembly source includes the lines:
Code:
         IF  $ & 1      ; If the next address is odd,
             DFB  0     ; lay down a zero byte before you
         ENDI           ;
         DWL last_NFA   ; lay down the link field.
so it's pretty automatic.

Use of the HEADER macro lets me have for example,
Code:
         HEADER "@", NOT_IMMEDIATE     ; (addr -- n )
FETCH:   PRIMITIVE
         LDA     (0,X)
         PUT_TOS
 ;------------------
or
Code:
         HEADER "RP@", NOT_IMMEDIATE   ; ( -- addr )   Get the addr pointed to by the return-stack pointer.
RPfetch: PRIMITIVE
         TSC            ; Transfer 16-bit stack-pointer value to A
         JMP   PUSH     ; and push it onto the data stack.
 ;------------------

_________________
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: Tue Sep 17, 2013 4:31 am 
Offline

Joined: Sat Dec 13, 2003 3:37 pm
Posts: 1004
No, because there's not a word alignment issue with the 6502. The details you're talking about were about preventing a word from starting $XXFF, because of the 6502 JMP (XXXX) bug.

In theory, yes you need to be careful to check the CFA of all your assembled words don't start on $XXFF.

It's something to be conscious of, but thankfully there simply aren't that many of them. You can either add a NOP or a .BYTE $0, doesn't really matter -- it's "lost space", since it's between words, when you do it.

I have not done this with mine, as my simulator doesn't have this problem.

You can simply GREP for FF in your final listing, and I bet they'll pop right out.

Here what an: egrep " ..ff " looks like on my assembly listing:
Code:
0744 05ff  94 04                 STY 4,X
0918 06ff              ;
0919 06ff              ;                                       0<
0920 06ff              ;                                       SCREEN 28 LINE 6
0921 06ff              ;         
0922 06ff  82 30 bc    L619      .BYTE $82,'0',$BC
1263 08ff  f2 08                 .WORD L952     ; link to BL 
1459 09ff  f2 09                 .WORD L1138    ; link to FLD
1808 0bff  55 06                 .WORD SPAT             
2389 0fff  2e                    .BYTE $2E
2527 10ff  0a 08                 .WORD CAT      ;| 6502 only. The code field
3099 14ff  d6 08                 .WORD ZERO         
3237 15ff  e5 15                 .WORD L2857    ; link to .LINE
3536 17ff  be 0b                 .WORD QCOMP   
3694 18ff  52 08                 .WORD DOCOL

it's a quick check to see if there's anything there. Nothing here is out of line.

For those system with alignment issues, most folks handle that with a Forth Header macro that manages that problem for them.


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 17, 2013 8:06 am 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
A murky subject. Yes, the issue is the NMOS bug for opcode $6C -- jump abs indirect. To avoid the bug, the ls byte of a Forth Code Field must not be stored at an address ending in binary 11111111. ie: the CF mustn't straddle a page boundary. Garth's approach goes one step further and ensures that no Code Field straddles a word boundary -- the CF is never stored at an address ending in binary 1. As a result, he'll sometimes waste a byte unnecessarily, but that's a minor matter, and apparently the $6C bug isn't the only factor he's considering.

FIG's CREATE has a mechanism similar to what Garth described above. FIG's CREATE (see source Screen 50) includes the sequence DP C@ 0FD = ALLOT just before the Link Field gets laid down. Why 0FD? Because the goal is to anticipate 0FF after the Link Field has gotten laid down.

whartung wrote:
You can either add a NOP or a .BYTE $0, doesn't really matter -- it's "lost space", since it's between words, when you do it.
Between words is the logical place -- acceptable, or even preferable. But that's not how FIG's and Garth's compilers do it. The wasted byte, if any, gets embedded within the header, after the Name Field. (CREATE is smaller and faster with this behavior. But it could be rewritten to actually put the wasted byte between words as Will says.)


In regard to Forth words produced from assembly source, you have freedom to do anything that'll prevent Code Fields from straddling a page boundary.
Code:
;    The following offset adjusts all code fields to avoid an
;    address ending $XXFF. This must be checked and altered on
;    any alteration , for the indirect jump at W-1 to operate !
;
;          .ORG *+2         ;.ORIGIN *+2
                nop                     
                nop   
The effect of this is to offset the entire dictionary right from the beginning, and move all the Code Fields! Not a great solution, since there's a risk that fixing one fault will generate another. (A CF that was previously okay might get moved onto a page boundary.) It seems to me the odds of that happening will be smaller if we place the wasted bytes higher in memory, not at the origin. Also, an automated solution (a Macro) is preferable to this manual tweak.

cheers
Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 23, 2013 11:24 pm 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
So for those of us who are using a 65c02, we can just comment the ".origin *+2" line out, saving valuable bytes through modern technology?

And just to be sure: The function of those ".word *+2" instructions that look so similar to the untrained eye are actually the addresses of the code that follows in the next line, or what Starting Forth calls the "code pointer" (http://www.forth.com/starting-forth/sf9/sf9.html)? So that I can go to, say, the label LIT, get the address that is there, and use it to run the LIT code?

One reason I'm asking is because the Ophis assembler does these things differently and I'm wondering if the equivalent code would be something like

Code:
LABEL:
       .word LABEL+2
       <code starts here>


Just started off with Forth (gforth in Ubuntu in VirtualBox, because there doesn't seem to be anything useful on the Mac for free), and though I'm already convinced it is the official programming language of mad scientists, it's a lot of fun. I'm just glad I had a HP calculator in school.


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 24, 2013 12:12 am 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
Quote:
So for those of us who are using a 65c02, we can just comment the ".origin *+2" line out, saving valuable bytes through modern technology?
Nicely put -- yes! And you may also delete the assembly source lines that implement DP C@ 0FD = ALLOT as part of CREATE.

Code:
LABEL:
       .word LABEL+2
       <code starts here>
And yes, you've got the right idea here. Many (not all) words do locate the executable machine code immediately after the Code Field; ie, in the Paramater Field. (In other cases the Code Field may point elsewhere, such as to NEST or BRANCH.)

Back when I was first learning Forth I found it helpful to memorize the structure of a Forth word and its header. With Fig Forth, it's like this:
  • Name Field
  • Link Field
  • Code Field
  • Parameter Field (optional, but almost anything might be stored here!)

-- Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 24, 2013 12:48 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8509
Location: Southern California
scotws, I think we're talking about two different things here. The *+2 of the CF is not related to alignment.

In indirect-threaded code (ITC) Forth such at fig-Forth, the *+2 in the code field points to the address of the machine code if it's a primitive, ie, code definition. If it's a secondary, ie, a colon definition, then the code field will instead point to the address of the machine code at label nest. If it's a constant, it will point to the code at label const, and the parameter field will contain the number that the constant is to return. The list goes on.

In fact, with CREATE and related words, you can make your own kinds of words and essentially extend the compiler itself, like to write new defining words. I've never used fig-Forth, so I just looked up CREATE in chapter 11 of "Starting Forth" because I remembered there was a small difference there, and it says that in fig-Forth, you define CREATE as:
Code:
: CREATE  <BUILDS  DOES>  ;

DOES> marks the end of the compile-time behavior of the word, and the beginning of the run-time behavior. You can also define the runtime behavior in assembly if desired, using ;CODE (caveat: I have not looked to see if that's what it's called in fig-Forth).

[Edit:] BTW, it is also possible to compile headerless code (meaning no name field or link field) if you don't need FIND to be able to look up words, meaning the final product will only run a pre-compiled application and never need to interpret or compile. This might be for a product where for example you don't want anyone reverse-engineering your code, or where the memory is limited so you don't want to waste space on headers.

_________________
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: Tue Sep 24, 2013 7:17 am 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
Thanks for the background; I think I'm going to go get a pencil and think about the details for a while. Amazing language, though it does make you wonder how Charles Moore's mind works.


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 24, 2013 7:52 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8509
Location: Southern California
Without taking time to look back at your other posts, it kind of sounds like you have not been through the book "Starting Forth" yet. It is available online free at http://www.forth.com/starting-forth/index.html. You may have never expected to find a technical book entertaining, but Leo Brodie is quite a commedian. This is the classic Forth-starter, and although it existed at least as far back as 1981, it has been revised a few times (my paperback is from 1987 and includes Forth-83) and what's online here now includes ANS Forth and speaks of computers having much wider buses and so on. I see they took out a lot of chapter three about the screen editor, as using blocks or screens of 16 lines by 64 characters is awfully old-fashioned and today we usually keep our source code in text files and edit it with a nice professional programmer's text editor using a hi-res monitor. I had to work in screens for a short time in 1990 and it drove me nuts.

_________________
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: Thu Sep 26, 2013 3:28 pm 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
Thanks for the recommendation -- I'm slowly working my way though it, in fact, and though I'm grateful for the humor, I've been hopping over to other tutorials -- one of the first programs I try to write in any new language is "guess the number", because it forces you to understand printing, inputs, loops, and branching all at once, and _Starting Forth_ takes forever to get to inputs.

I can tell already that is a language that I will only learn by practice, practice, practice because it is so counter-intuitive. I'll stick with it, and let you know what it does to my brain :D .


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 26, 2013 8:00 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8509
Location: Southern California
scotws wrote:
I can tell already that is a language that I will only learn by practice, practice, practice because it is so counter-intuitive. I'll stick with it, and let you know what it does to my brain :D .

When I was first getting into Forth in 1990, I liked it but had my reservations, thinking there were various things that it couldn't do well, or even at all. As I gained experience with it, I began to see that it was not only able, but afforded very, very elegant solutions that did not initially meet the eye.

_________________
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  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 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: