Adventures in Collapse OS
I like Forth. Although Forth is a stack-base, reverse-polish-notation system with many peculiar shenanigans, I can’t help myself; I love and hate it at the same time. This might be related to me liking old HP calculators.
A while ago I discovered Collapse OS, a minimal Forth-based, multi-arch “operating” system able to bootstrap itself. Means, once you have Collapse OS running on one architecture, such as Intel 8086, you can crosscompile against another architecture, such as Z80 or 6502, inside Collapse OS itself. How wonderful!
As a Forth enthusiast, I grabbed the latest package, build it, and dropped
directly in the emulator console, provided by cvm/cos-serial
, a binary
running in native code.
$ cd cvm
$ ./cos-serial
Using blkfs blkfs
Collapse OS ok
42 foo !
foo word not found ok
create foo
create word not found ok
CREATE foo
ok
42 foo !
ok
foo @ .
42 ok
foo @ 23 + .
65 ok
OK, cells need to be explicitly created and the CREATE
word is in uppercase.
S" Hello Collapse OS" TYPE
TYPE word not found ok
S" Hello Collapse OS"
ok
.S
SP 00 RS 04 -- ok
." Hello"
ok
.
25816stack underflow ok
Hm. What’s going on here? I would’ve expected that string to be printed to the
console. I am confused. Let’s check doc/dict.txt
.
See doc/usage for the concepts of strings and lines.
Alright… Checked it, but nothing of substance. Let me try something.
: test ." Hello" ;
ok
test
Hello ok
Wut? OK, another oddity. I can wrap around that. But very strange.
OK, let’s try a loop…
: test 10 0 DO I . LOOP ;
DO word not found ok
…? OK, let’s look at the docs again.
Loops work a bit like conditionals, and there’s 3 forms:
BEGIN..AGAIN –> Loop forever BEGIN..UNTIL –> Loop conditionally X >R BEGIN..NEXT –> Loop X times
Oh my, this looks like I need cook up everything on my own. Let’s try something else.
0 VALUE I
ok
: loopi BEGIN I . I 1 + TO I I 10 = UNTIL ;
ok
loopi
0123456789 ok
That’s like back in Programming 101 :-)
But now for something else; as a hobby-astronomer I need access to the clock of the OS and preferably floating point numbers. Both don’t exist in Collapse OS.
As of the RTC, I do understand that this is neither really portable and that probably none of the implemented target architectures actually have a RTC.
Wrt to floating point, I can work around by using fixed point arithmetic, but
here I’m restricted by the wordsize of 16 bit; yes, I could mangle together
multiple words to get a better precision, but I found that I can’t recognize
an overflow and that I can’t read out the carry bit from FLAGS (in 8086).
Looking at the CHANGES
file, I found this note:
- 8086A: add ADC.
- 8086A: add []i form to ADD ADC SUB CMP.
OK, now I have to figure out how I can make use of it, I probably have to
fiddle around with custom assembler using the CODE
word.
OK, I’m gonna explore a little more!