mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Arm Forth Project

Wed Jan 15, 2014 10:27 pm

I recently ported my Linux X86 Forth compiler (isforth) to Arm using my Beagleboard XM (because I Stupidly broke my PI!). This Forth now runs just fine on the XM and on my rooted android phone using ADB Shell (cant get it to run from within an apk however as the system kills it instantly).

This port was originally planned for my PI as I wanted a Forth available for this board when I discovered it (prior to release) It is not 100% completed yet as some of the extensions I have for my x86 Linux forth need to be ported in and debugged.

About the forth....

1: Written entirely in Assembler.
2: Uses System calls, no external libraries linked
3: Subroutine (native) threaded (but not optimized)
4: Includes my Forth dynamic memory sub allocation module (not as fast as the one in Linux :)
5: Includes my terminal windowing system (movable windows and menus etc)
6: 100% open source for anyone interested (tho not yet officially released).
7: Kernel (core of the forth compiler) 20536 bytes, Extended compiler 79782 bytes.
8: 100% NOT ans Forth compliant (i strive for ans noncompliance!)

There are still a few quirks left to iron out and I did not yet get a chance to develop the GPIO module for the PI but this would be less than trivial to implement.

Anyone interested in being the first to run this on a Raspberry PI?

nounours_poilu
Posts: 2
Joined: Thu Jan 16, 2014 3:26 pm

Re: Arm Forth Project

Thu Jan 16, 2014 3:49 pm

Yes would be nice! ;)
I could spend some time to check it, let me know how to install it.

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 6566
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Arm Forth Project

Thu Jan 16, 2014 9:28 pm

nounours_poilu wrote:Yes would be nice! ;)
I could spend some time to check it, let me know how to install it.
+1
I still have a working (when last checked) z80 based, Jupiter Ace, which was Forth based and once wrote a Forth interpreter/compiler for a Z8671** system I designed (and saved from being scrapped, but, currently is "poorly", the battery-packed RAM chips having now failed - well past their expected "10 year" life by now). Done w/o assembly tools, everything "hand hex-coded" - still have the source code too.
Trev.
**On start-up ran an integral integer Basic interpreter which could directly call/link into machine code.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B, B+, and a A+) but Buster on the P3B+, P4B's & P400. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Avoncliff
Posts: 35
Joined: Fri Dec 09, 2011 5:24 pm

Re: Arm Forth Project

Thu Jan 16, 2014 11:21 pm

+1
I am sure I still have that game I wrote in forth 30+years ago somewhere.

What does it look like at present, an assembler source file?

David

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Fri Jan 17, 2014 3:26 pm

OOpts, I was expecting a notification of my thread getting replies but I never saw any so sorry for delayed response :)

Codename: 97.409 (or... PI to the 4th power)

You can download my tgz from www.isforth.com/a4.tar.bz2, extract this and do the following...

Firstly, because I build this for various arm architectures you might have to edit src/kernel/Makefile and set ARCH = armv6 which is what the PI is i believe. then...

make clean && make
./extend

The make has the gnu assembler assemble the Forth kernel sources to an executable called a4k. You can use this bare bones Forth compiler to build your turnkey applications against for a more trim executable (see below).

The ./extend is a small bash script that executes ./a4k and has it compile src/a4.f. This forth source file is a load file specifying what extensions to add to the kernel.

You can create a custom version of this file, remove all the pieces you don't need in your application and add an include of your app somewhere near the bottom. Then and create a custom ./extend to load it.. At the bottom of the load file is an fsave command. This saves out the newly extended kernel to a file called a4 with all the forth headers intact (I.E. the saved file ls a fully functional extended forth compiler).

When you create a custom executable which will never be used to compile any new code the word headers are just deadwood so can be discarded. You can replace the "fsave ..." at the bottom of your custom load file with "turnkey ...". This will save out the extended kernel (extended with your application) to an executable with no Forth headers.

Actually I have not tested turnkey in this ARM version of my X86 Linux Forth but it "should work" (tm).

HOWEVER. when you run this executable its not going to run your custom applications code unless you do one of the following just prior to the turnkey operation...

option 1: ' my-main is quit
option 2: ' my-main is default

(pronounced tick my-main is .... )

The difference between these is important. When Forth first runs it executes a deferred init chain called "default". This initializes all the modules that require it such as the terminal (set canonical mode etc), allocate buffers/whatever. After default runs the system then jumps to quit. Quit is the inner interpreter/compiler loop - in a turnkey application you can either nullify default so that instead of initializing all the various modules (which maybe you removed anyway) it jumps right into your main instead.

On the other hand, if your application requires those extensions that get initialized by default then you can re-vector quit to your main with option 2 above. This will allow all those modules to be initialized prior to jumping to your application code.

Once the extended compiler (./a4) is built you have a fully functional Forth compiler. There is a lot of example Forth code both in src/ext/* and src/examples/*. A nice flashy "I challenge you to do this with ncurses" example would be...

./a4 -fload src/examples/wintst.f

After loading type "main [enter]" to run it. q quits back to Forth and to quit Forth type "bye".

The displayed elapsed time (how long you were running Forth) is currently fubar and its on the todo list to find out WHY this elapsed time is fubar :)

NOTE: This is not "officially" released yet as I still need to fix some things and pick a license (maybe bsd, not sure but what ever it is it will NOT be viral, I will never make you obligated to release any sources for any executables you create with my compilers even though part of your executables are derived from my sources). Take THAT RMS!!! :)

Regards...

P.S. I'm almost always totally idle in #forth on irc.freenode.net as either mark4 or I440r (Eye 440 Are) which stands for Ibanez 440 radius, one of my guitars, not some rediculous 1337 speak :)

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Fri Jan 17, 2014 3:36 pm

Oooh dang forgot to mention (and this MIGHT be important lol)

I'm obstinate. I refuse to fix someone else's breakage in MY code and any Debian based distribution you might be running IS BROKEN. Debian based distributions do NOT honor the FHS and my Forth will probably segfault on you because of this. (easily fixed).

The Debian devs stupidly put some of the terminfo files in /lib/terminfo where they DO NOT belong. It's bad enough that they put them there at all but utterly moronic that they MOVED them there. If they had simply copied those files from /usr/share/terimfo then there would be no problem here.

The fix:

mv /lib/terminfo/* /usr/share/terminfo

..or if you don't want to mv those files back to their CORRECT location in the directory tree cp them instead.

Regards
MM

Avoncliff
Posts: 35
Joined: Fri Dec 09, 2011 5:24 pm

Re: Arm Forth Project

Fri Jan 17, 2014 9:50 pm

Just to let you know compile is fine.
At the moment loading a4.f bombs at
"fload src/ext/comment.f Illegal instruction"
Will look at why later....

nounours_poilu
Posts: 2
Joined: Thu Jan 16, 2014 3:26 pm

Re: Arm Forth Project

Sat Jan 18, 2014 2:03 pm

I made about the same experience, compilation is fine, once i call ./extend I get following unexplained result:
pi@raspberrypi ~/a4 $ ./extend
ok
fload src/a4.f

./extend: line 3: 26830 Done printf "fload src/a4.f\n"
26831 Illegal instruction | ./a4k
pi@raspberrypi ~/a4 $ ./extend
ok
fload src/a4.f

./extend: line 3: 26833 Done printf "fload src/a4.f\n"
26834 Illegal instruction | ./a4k
pi@raspberrypi ~/a4 $
I did it twice to see who is giving me the error number

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Sat Jan 18, 2014 8:24 pm

did you change the makefile to ensure that the build was for armv6 not v7?

Avoncliff
Posts: 35
Joined: Fri Dec 09, 2011 5:24 pm

Re: Arm Forth Project

Sat Jan 18, 2014 10:35 pm

Yes set it to armv6
problem is with ;
it gives the error on completing a word define

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Sun Jan 19, 2014 2:12 am

create a file called foo and put this in it (between the snips not including the snips)
< snip >
fload src.a4.f
< /snip >

then run..

strace ./a4k < foo &> bar
then paste the contents of bar for me :)

wish i hadnt broke my pi :/

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Sun Jan 19, 2014 9:43 pm

argh thats "fload source SLASH a4.f not dot a4.f

"fload src/a4.f"

Avoncliff
Posts: 35
Joined: Fri Dec 09, 2011 5:24 pm

Re: Arm Forth Project

Mon Jan 20, 2014 2:40 pm

It gets worse, I just tried this on a spare pi that I had just rebuilt the kernel, and it does not run at all.
$./a4k
killed
$
I tried strace on old pi. but forum does not want to attach it. This is end of file
open("src/a4.f", O_RDONLY) = 3
lseek(3, 0, SEEK_END) = 5092
mmap2(NULL, 5092, PROT_READ|PROT_WRITE, MAP_PRIVATE, 3, 0) = 0x4019e000
write(1, "\n", 1
) = 1
write(1, "\n", 1
) = 1
open("src/ext/comment.f", O_RDONLY) = 4
lseek(4, 0, SEEK_END) = 963
mmap2(NULL, 963, PROT_READ|PROT_WRITE, MAP_PRIVATE, 4, 0) = 0x401a3000
--- SIGILL (Illegal instruction) @ 0 (0) ---
+++ killed by SIGILL +++

Avoncliff
Posts: 35
Joined: Fri Dec 09, 2011 5:24 pm

Re: Arm Forth Project

Mon Jan 20, 2014 4:27 pm

I think the makefile cpu = cortex-A8 is wrong.
the BCM2835 is not cortex.
I have tried other options but then get the error:
compile.s: Assembler messages:
compile.s:543: Error: selected processor does not support ARM mode `movw r7,#2'
compile.s:544: Error: selected processor does not support ARM mode `movt r7,#15'
make: *** [a4k.o] Error 1

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Wed Jan 22, 2014 3:55 am

aha that makes sense. the movw is a 16 bit move instruction that im using in the cache invalidate function. ill post a fix for this tomorrow - what did you select instead of cortex-a8 ? that plus a slight modification to the cache invalidation code and we should be good to go :)

Avoncliff
Posts: 35
Joined: Fri Dec 09, 2011 5:24 pm

Re: Arm Forth Project

Wed Jan 22, 2014 7:40 pm

I think I used
CPU = arm1176jzf-s
ARCH = armv6zk
But whether more of the recommended should be used I do not know
-Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Thu Jan 23, 2014 3:15 am

updated the bzip, download again from www.isforth.com/a4.tar.bz2. let me know if there are any further hickups :)

i would not recommend using any optimizations when building this as i make certain assumptions about what code is layed down by what sorce. I am not a big fan of ANY forms of compiler optimization but i wont go into why :)

Avoncliff
Posts: 35
Joined: Fri Dec 09, 2011 5:24 pm

Re: Arm Forth Project

Thu Jan 23, 2014 12:22 pm

Thanks.
I have two Raspberrys on the older one it runs, not tested very far but I could compile a word.
On the newer unit running the latest raspian and kernel 3.10.27+ I get:
pi@DevSystem56 ~/forth/a4 $ strace ./a4k
execve("./a4k", ["./a4k"], [/* 16 vars */] <unfinished ...>
+++ killed by SIGKILL +++
Killed
pi@DevSystem56 ~/forth/a4 $

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Thu Jan 23, 2014 8:02 pm

I assume you ran the procedure outlined in the DEBIANFIX text file and explained above - if not do so now. if so then we still have a problem which is making me look bad! lol wish i didnt break my Pi!

To fix I will need to know what caused the kill.

Avoncliff
Posts: 35
Joined: Fri Dec 09, 2011 5:24 pm

Re: Arm Forth Project

Thu Jan 23, 2014 8:17 pm

Hi
If you are referring to the sudo cp -r /lib/terminfo/* /usr/share/terminfo
Yes that is done.
Still same report, note I am running this headless via ssh.
The older pi it runs, also via ssh, but not for long.....
If I run extend it returns no errors and creates a a4 version.
but that gives
pi@SoundRack ~/forth/a4 $ strace ./a4
execve("./a4", ["./a4"], [/* 17 vars */]) = 0
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
Segmentation fault

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Fri Jan 24, 2014 8:07 am

when you have a crash like this a strace can help me track it down (maybe?).

I do plan on getting a new Pi, i just cant afford to do that quite yet (no work). If we have not fixed the issue before i get my new pi it shouldn't take long after i do.

Or.. if there was some way someone could create a non sudo account on some pi in cyber space that I could ssh in to (i can give a public key to make it keyless) I could probably fix it that way too :)

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Wed Mar 05, 2014 9:59 pm

well this forth runs on my beagleboard-xm and now on a cubieboard out of the box. There is some problem with doing a make to rebuild the kernel on the cubie (running some sort of debian) because the binutils do not add a PAX flags program header to the build and my fsave extension assumes that header is present.. this means that after building a new a4k you can run it, it will compile all the extensions but the fsaved a4 wont execute at all. working on trying to figure out how to get debian binutils for arm to apply PAX flags.

Still dont have a PI though so cannot debug this illegal opcode problem, when (if?) I get work ill get a new PI and fix it good.

mark4
Posts: 15
Joined: Wed Jan 15, 2014 10:10 pm

Re: Arm Forth Project

Wed Mar 05, 2014 10:44 pm

ok scratch that, seems that none of (that i can find) the debian based distributions ship with a PAX enabled kernel. this is a VERY VERY good reason to never under any circumstances use any linux distribution thats based on debian.

im afraid im going to have to ship my forth with a "does not support broken distributions such as debian and those based on it" clause.

highly recommend you install a sane distribution if your using anything debian based. (tho this is not the cause of the illegal opcode error).

User avatar
DougieLawson
Posts: 41900
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Arm Forth Project

Wed Mar 05, 2014 10:47 pm

You can install it with sudo apt-get install pax
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on Twitter/LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

User avatar
jojopi
Posts: 3565
Joined: Tue Oct 11, 2011 8:38 pm

Re: Arm Forth Project

Thu Mar 06, 2014 4:04 am

DougieLawson wrote:You can install it with sudo apt-get install pax
pax is the POSIX replacement for tar and cpio. (pax-utils are the binaries for PaX, but it is not clear whether that helps.)

a4k is killed even before it reaches a breakpoint at start; Linux is obviously very unhappy with the binary. Linking using gcc and without src/ld/a4.ld produced a binary that at least started, and the important difference appeared to be that origin was then 0x80d8 instead of 0x1054.

So increasing to at least 0x2000+SIZEOF_HEADERS in a4.ld solves the SIGKILL, and ./extend then runs to completion, dumping an (obviously broken) a4.

Return to “Advanced users”