6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Apr 27, 2024 5:02 pm

All times are UTC




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Thu Dec 01, 2022 3:40 pm 
Offline

Joined: Wed Jan 08, 2014 3:31 pm
Posts: 565
After completing the NAND game I've developed a basic competency in the Hack computer's architecture and assembly language. See https://en.wikipedia.org/wiki/Hack_computer for an overview.

It's an interesting computer architecture because it is Von Neumann, but partitions memory into a program section and a data section. This is because the load A immediate instruction assumes zero for the most significant bit to allow packing a literal into a 16 bit instruction. But that limits data pointers to values below 0x8000. Conversely any instruction which creates a program counter value assumes 1 for that bit, so all code must reside above 0x7fff. The designers assumed programs are written to a ROM mapped to high memory. Basically like a 1980s video game machine.

It's such a crude architecture that creating a VM was the first order of business for its creators. But they chose a Java-like programming language. This got me thinking about how to create a Forth implementation, and I realized it would be difficult because it looks like data structures can't be built in program memory. Indeed the assembler reference doesn't mention directives for byte, word, or string constants. Everything is done via load immediate.

This is a big problem for a Forth implementation because you would need these to build the Forth dictionary for built-in words, and building in RAM at startup would be a massive headache. Granted this is an educational architecture, so its limitations are not something you would see in many real world systems.

But this made me wonder, are their real world architectures that are unable to host a Forth system? It would seem like any Harvard architecture might have a problem due to the need to construct the dictionary.


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 01, 2022 4:46 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1927
Location: Sacramento, CA, USA
I am aware that Dr. Brad Rodriguez has at least one CamelForth running on a Harvard architecture, but I'm pressed for time at the moment, so I can't give you a link.

_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 01, 2022 6:43 pm 
Offline

Joined: Wed Jan 08, 2014 3:31 pm
Posts: 565
barrym95838 wrote:
I am aware that Dr. Brad Rodriguez has at least one CamelForth running on a Harvard architecture, but I'm pressed for time at the moment, so I can't give you a link.

That was enough info to find this:
https://www.bradrodriguez.com/papers/moving7.htm

He had the issues I thought about and a few more. To work around the issues he physically modified the board so some memory was mapped to both code and data space! He also couldn't add new words because the 8051 can't write to its own program space.

So I guess the bottom line is that my instincts were correct, a Harvard like architecture will have issues with Forth.


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 01, 2022 7:26 pm 
Offline

Joined: Sun May 13, 2018 5:49 pm
Posts: 247
That's an interesting architecture. I don't think the fact that data and program memories are segregated is a show-stopper, as pointers in RAM can be pointers to other RAM locations or to ROM locations (eg. you can load a ROM pointer from RAM and then JMP to it). You won't be able to make new CODE words, but if the core Forths words are all in ROM (and these would all have to be code words, I would think), you should be able to make a lot of Forth work. I could also imagine a token-threaded forth where the token sequence for a new word is in data RAM.

The limiting issue I see with this processor is that there is no way to get the current value in the PC onto the data bus. While all of the instructions needed for creating a data stack in RAM are there, there is no way to create a call/return stack. This processor doesn't have a return mechanism in general and can't even do the self-modifying code tricks used on ancient computers without hardware return stacks.

If defining new code words is disallowed and everything is written using only the Forth words in ROM, you might? be able to get it to work? (not entirely sure here). You'd need to keep track of "return" addresses separately using your own stack. Conveniently, the most significant bit could be used to determine if a pointer points to a RAM word or a ROM word, as the ROM words will all need to be CODE words and will need to be JMPed to while the RAM words will all need to use the inner interpreter. I would need to use more brain power than is currently available to figure out how create/does would work.

Building the initial dictionary for the ROM words would be painful.

I'm sure I'm missing something - what's the fatal flaw?


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 01, 2022 10:18 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
Besides 65xx, the other processor I have extensive experience with is the one in PIC16 microcontrollers which has a Harvard architecture and is sort of a RISC but does not have a big register file like RISCs usually do and it still requires usually four and sometimes eight clock ticks to retire each instruction.  I have implemented Forth-like flow-control structure macros (and other macros) in assembly-language programming which hide a lot of the PIC16 mickeymousities and dramatically improves productivity, reduces bugs, improves maintainability, etc.; but I've thought many times about this matter of implementing actual Forth on a PIC16, and I don't think it's possible.  There are just too many problems with it.  Even things you can do, like table look-ups, cannot use the same code for tables in RAM versus ROM.

My microcontroller projects have tapered off in recent years though, and my last major, major project with one was ten years ago, and I've only done two lesser ones since then.  I started another one for a friend but gave up after I put 200 hours into it and running into problems I couldn't solve.  I should have just moved up to a better microcontroller; but I didn't think the project would be that hard, so I didn't want to take the time to get familiar with a new family.  After I got familiar with PIC16 for that first project in 1996 which used a PIC16F84 for work, Microchip has added workarounds for persistent problems, so now there's the PIC16F1 line, the PIC18, and PIC24, in order of increasing power.  (PIC17 has been dropped.)  They're still kluges though. (PIC32 is a different architecture altogether.)

By comparison, the 65xx seems like a natural for Forth.  It's almost as if Mensch & Peddle had Forth in mind when they designed the '02 (although if they really had, further improvements could have been made).

My HP-41cx calculator/computer with the Nut processor has a Forth available for it, but it seems to have been largely to satisfy the writer's itch and prove it could be done at all.  The architecture is not very suitable for Forth.  I have the internal details of the Forth but have not looked extensively into it.  The 41 has separate address spaces for RAM versus ROM (even though there are modules that have RAM in the ROM area, for development), and ROM has 10-bit "bytes" and RAM has 56-bit registers consisting of seven addressable bytes each.  The processor has 56-bit registers too.  The RAM space is more limited and is further divided between main RAM and the extended RAM which is for files; so although there's a text editor, your source-code files can't be more than a few KB long.

The HP-71B hand-held computer OTOH, although its Saturn processor is still a bit strange, is far better suited to Forth.  It has a 4-bit data bus and a 20-bit address bus (and 12-, 20-, and 64-bit registers, IIRC), and no particular distinction between RAM and ROM, data versus program.  This computer is where I initially got my start in Forth, a year before I got into Forth on the 6502.  It was a bit of a challenge, because the HP-71's Forth's manual assumed you were already familiar with Forth and just needed to know the peculiarities of the unusual address sizes, whereas Leo Brodie's book "Starting Forth" assumed you had something to practice on.  I did eventually get going though.  I had already written a really nice text editor for the 71, which helped.  The Forth module's implementation of Forth was not nearly as good as the 71's BASIC (which is, by far, the best BASIC I've ever seen, particularly with the Language EXtension files contributed by the users' groups); but since it is Forth, I was able to improve it a lot.

Although my interest is in the smaller processors and I have not gotten into modern high-end processors, my perception is that the latter have blurred the lines between RISC and CISC, having very complex instructions that get broken down into multiple simpler ones internally before being executed, and those may not even get executed in the expected order.  But in spite of Harvard architectures, they do extremely well at Forth, and are really hot performers, and have been used for some very high-end industrial, aerospace, and medical applications of a million lines or more of code.  The Big Cheese in the field is of course Forth, Inc..

_________________
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 Dec 01, 2022 10:45 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
Our own Brad Rodriguez, somewhat of a giant in the field of Forth, has some comments about doing Forth on different microcontrollers in the topic at http://anycpu.org/forum/viewtopic.php?f=3&t=132 .

_________________
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 Dec 01, 2022 11:07 pm 
Offline
User avatar

Joined: Mon Apr 23, 2012 12:28 am
Posts: 760
Location: Huntsville, AL
Well I haven't posted here on 6502.org often in the past few years. As a consequence I lost my post.

I think you'd have to classify the PIC16 family as a "Turing Complete" instruction set architecture. Therefore, although it could be wildly inefficient, I would think that there is an implementation for a Forth interpreter that can be developed. It may require that both the program and data for the Forth program be implemented in external serial memory devices. It would require the development a custom instruction set that a PIC-based interpreter would use to implement the Forth primitives. I also think that even the small internal data memory of a PIC16C5x should be sufficient to implement all of the standard Forth interpreter registers that Brad R. describes.

Just to be clear, I don't think this is an ideal means for a Forth interpreter, but I do think it is feasible. The implementation would essentially would utilize the PIC16 program space to implement the interpreter for the Forth VM, and the data space would provide the Forth VM working registers. A simple I2C or SPI serial RAM, like those available from Microchip, or FRAM / MRAM SPI devices easily provide 64kB of merged program / data space.

I do agree that within the Harvard architecture of the PIC16 family it would be fairly difficult to implement a Forth VM if no external von Neumann memory devices were used to hold the Forth program and data.

Thoughts?

_________________
Michael A.


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 02, 2022 12:05 am 
Offline

Joined: Fri Apr 15, 2022 1:56 pm
Posts: 45
Location: San Antonio, TX, USA
SamCoVT wrote:
The limiting issue I see with this processor is that there is no way to get the current value in the PC onto the data bus. While all of the instructions needed for creating a data stack in RAM are there, there is no way to create a call/return stack.
The nand2tetris course achieves function calls/returns for this processor using a "virtual machine translator" which generates machine code from bytecode. The bytecode for a function call translates to machine code which pushes the correct return address for the call site to the software stack before jumping to the function address. Here's a screenshot from the course material (captured from the material for project 7 here: https://www.nand2tetris.org/course.)
Attachment:
hack_vm.png
hack_vm.png [ 183.42 KiB | Viewed 7933 times ]
The nandgame (https://nandgame.com/) achieves a similar outcome via a macro assembler and macros which generate the necessary code at function call sites to 'manually' push the return address to a software stack.

Machine code to populate data structures in RAM, similarly can be generated by the virtual machine translator or macro assembler and I woutld think these approaches provide an adequate foundation for creating a Forth interpreter. Of course a true compiler that can run on this architecture, for any language will require some means to move data from RAM into program memory.


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 02, 2022 12:22 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
MichaelM wrote:
Thoughts?

I think that would be the only way for the PIC16, ie, to have it emulate another processor which would in turn run Forth in external memory for program, data, registers, stacks, and anything 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 Dec 02, 2022 6:04 am 
Offline

Joined: Thu Jan 21, 2016 7:33 pm
Posts: 269
Location: Placerville, CA
As David Wheeler supposedly remarked, any problem in computer science can be solved with another layer of indirection ;)


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 02, 2022 3:30 pm 
Offline

Joined: Fri Apr 15, 2022 1:56 pm
Posts: 45
Location: San Antonio, TX, USA
commodorejohn wrote:
As David Wheeler supposedly remarked, any problem in computer science can be solved with another layer of indirection ;)
…except for the problem of too many layers of indirection. :wink:


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 02, 2022 3:50 pm 
Offline

Joined: Wed Jan 08, 2014 3:31 pm
Posts: 565
@All, thanks for the replies. Based upon the responses and thinking about it more. If you built a high level language that compiled to Hack assembly you could probably solve most of the thorny problems. For example a series of load A immediate instructions followed by a store in RAM could build the dictionary pointing back to code addresses in high memory. It would be inefficient because all that code would be wasted after initialization and reduce available RAM, but it could work. The two stacks are easy to implement in software, but they would also be slow.

So I think the answer is that such an architecture could host Forth, but it would be a bad choice to do it.

Adding another ROM memory mapped into data space would probably be a huge win for such an endeavor because it would eliminate the dictionary initialization problem.


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 02, 2022 5:05 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
You could perhaps split the initialisation application from the runtime application.

Edit : in the case of loading, perhaps less so in the case of ROM code.


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 20, 2022 6:41 pm 
Offline
User avatar

Joined: Mon May 12, 2014 6:18 pm
Posts: 365
Quote:
After I got familiar with PIC16 for that first project in 1996 which used a PIC16F84 for work, Microchip has added workarounds for persistent problems, so now there's the PIC16F1 line, the PIC18, and PIC24, in order of increasing power. (PIC17 has been dropped.) They're still kluges though. (PIC32 is a different architecture altogether.)
I feel for you, Garth. I recently started using the PIC32 and it really seems like an upgrade over what came before it. It comes in DIP if that's of interest to you. It was very easy to set up and start working on Windows. 64K RAM, 256K flash, and single-cycle 32-bit CPU at 50MHz is pretty awesome. You might want to check it out if you are open to a new chip.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 09, 2023 10:26 pm 
Offline

Joined: Mon Jan 09, 2023 9:33 pm
Posts: 23
If all primitives are in 'CODE' space and compounds are in 'DATA' space, no problem.
PS. primitives does machine code, compounds does references


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

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:  
cron