(PC-G850) lisp in 99 lines of C
07-14-2022, 09:47 PM
Post: #1
 robve Senior Member Posts: 398 Joined: Sep 2020
(PC-G850) lisp in 99 lines of C
I wrote a Lisp interpreter for the Sharp PC-G850 in 99 lines of C with a writeup in an article to explain the inner workings in detail. The C program compiles and runs on the PC-G850(V)(S) itself:

I created a tinylisp GitHub project repo with the code, examples, and the article writeup.

The idea is to exploit something similar to NaN boxing of IEEE floats, but with Sharp BCD floats instead, "BCD float boxing". A BCD float is stored in 8 bytes. The first 2 bytes store the exponent in BCD and the sign of the number. The next 5 bytes store the 10 digit BCD mantissa followed by a zero byte:
$|\overbrace{b~\underbrace{b~b~b}_{\it tag}~|~b~b~b~b~|~b~b~b~b}^{\it 10's\,complement\,BCD\,exponent}|~\overbrace{s~d~u~u}^{\it control}~|~\overbrace{b~b~b~b~|~\cdots~|~b~b~b~b}^{\it 10\,digit\,BCD\,mantissa}~|~\overbrace{0~0~0~0~|~0~0~0~0}^{\it 2\,BCD\,guard\,digits}~|$
where
• $$s$$ is the sign bit of the float, $$s=1$$ for negative numbers
• $$d$$ is the degree bit, $$d=1$$ to display degrees in $$D^{o}M'S.S"$$ format in BASIC
• $$u$$ is an unused bit, likely a mantissa carry bit used by the system
• 10's complement BCD exponent consists of 12 bits for 3 BCD digits to represent exponents -99 (901 BCD) to 99 (099 BCD)
• 10 digit BCD mantissa consists of 10 BCD digits with the normalized mantissa, the leading BCD digit is nonzero unless all mantissa digits are zero
• 2 BCD guard digits are always zero after internal rounding to 10 significant digits
I experimented with some C code to try placing a tag and data in a BCD float, which was a bit of a challenge. We cannot use the mantissa or its trailing guard digits to store extra information. The mantissa is always normalized to BCD and the guard digits are always reset to zero when passing floats through functions, even when no arithmetic operations are applied to the float. Our target bits to box a tag with data in a float are the three bits in the upper half of the leading byte of the float. These three bits of the float remain unmodified when passing the floating point value through functions as arguments and as return values.

To copy lisp to the Sharp PC-G850(V)(S) use SIO or PocketTools to load via audio cassette interface (CE-124 or CE-126p):

PC: bas2img --pc=G850VS --type=asm -l0x408 lisp850-opt.c
PC: bin2wav --pc=G850VS lisp850-opt.img
G850: BASIC (PRO MODE)
PC: play lisp850-opt.wav
G850: TEXT
G850: Basic
G850: Text<-Basic
G850: BASIC (2x PRO MODE)
G850: NEW
G850: 2ndF TEXT (C)
G840: G (go)

The bas2img option -l0x400 adds line numbers to the C source automatically. I've included the wav files in the GitHub repo in case you don't have PocketTools.

- Rob

"I count on old friends" -- HP 71B,Prime|Ti VOY200,Nspire CXII CAS|Casio fx-CG50...|Sharp PC-G850,E500,2500,1500,14xx,13xx,12xx...
07-15-2022, 06:41 AM
Post: #2
 EdS2 Senior Member Posts: 554 Joined: Apr 2014
RE: (PC-G850) lisp in 99 lines of C
Marvellous - thank you!
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)