Post Reply 
FORTH for the SHARP PC-E500 (S)
01-13-2022, 03:40 AM
Post: #61
RE: FORTH for the SHARP PC-E500 (S)
(01-12-2022 11:06 PM)Helix Wrote:  I'm still learning Forth, following the book Starting Forth 2nd edition. (I know it's outdated, but it's didactic and enjoyable. I have also the Forth Programmer's Handbook for a more modern reference.)

I have a problem with the KEY? word. If no key is pressed, it returns "error -57" instead of 0.
The same problem occurs with EKEY? Furthermore, if a key is pressed, EKEY? returns 255. I think it should be -1.

It is good to hear you are putting Forth500 through its paces Smile

You made a good point. The EKEY? word calls CHAR-READY? but then throws exception -57 if CHAR-READY? returns true. This is a small part of the older pceForth version, which looks wrong to me. The EKEY? Forth standard word requires EKEY? to return true or false.

I will correct this discrepancy soon and release an update.

I may also add a new module LOCALS.FTH that I wrote from scratch to implement the LOCALS word set (I heard that Forth purists turn their nose up to LOCALS, arguing that it is superfluous since everything can be done via the stacks.)

I was tinkering with some code I wrote to add dynamic scoping of arguments and locals to Forth definitions. This turns out to be surprisingly simple to implement in Forth itself, so I wouldn't be surprised if someone hasn't done this already.

- 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...
Visit this user's website Find all posts by this user
Quote this message in a reply
01-16-2022, 09:22 PM
Post: #62
RE: FORTH for the SHARP PC-E500 (S)
Helix,

The EKEY? and KEY? exception problem is fixed. That was annoying. Thanks for reporting. Sorry to have missed that. I try to test Forth500 as much as possible before releasing updates, but there is only so much time one person can put into this.

The Forth500 update includes the following changes:

- updated BREAK key pressing exception to display Break instead of <Error -28

- updated REPRESENT to produce 12 or 13 non-rounded digits for single floating point results instead of 10, which means that single precision arithmetic is practically performed with 12 or even 13 digits without rounding

- updated double floating point computation results to round to 20 digits rather than ignoring the guard digits

- fixed EKEY? issue that affected KEY? as well

- 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...
Visit this user's website Find all posts by this user
Quote this message in a reply
01-18-2022, 12:32 AM
Post: #63
RE: FORTH for the SHARP PC-E500 (S)
Thank you for the update! Now EKEY? works as expected. Smile

Jean-Charles
Find all posts by this user
Quote this message in a reply
02-19-2022, 09:40 PM
Post: #64
RE: FORTH for the SHARP PC-E500 (S)
I think I have found another quirk. Smile
M*/ ignores negative numbers.
For example -1. 1 1 M*/ D. gives 1 instead of -1
Same result with 1. -1 1 M*/

Jean-Charles
Find all posts by this user
Quote this message in a reply
02-20-2022, 09:13 PM
Post: #65
RE: FORTH for the SHARP PC-E500 (S)
(02-19-2022 09:40 PM)Helix Wrote:  I think I have found another quirk. Smile
M*/ ignores negative numbers.
For example -1. 1 1 M*/ D. gives 1 instead of -1
Same result with 1. -1 1 M*/

It's easy to fix and I will update the repo soon. There is an internal RAM register clash that resets the sign bit of the result. This issue should not have happened. I may have overlooked this specific case when testing the latest round of speed optimizations and code size reductions to make the Forth500 system fit in about 20K Big Grin

-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...
Visit this user's website Find all posts by this user
Quote this message in a reply
02-20-2022, 11:12 PM
Post: #66
RE: FORTH for the SHARP PC-E500 (S)
Thanks for the update Wink

Jean-Charles
Find all posts by this user
Quote this message in a reply
04-03-2022, 12:53 AM
Post: #67
RE: FORTH for the SHARP PC-E500 (S)
I haven't had the time to work on a text file editor for Forth500 to edit Forth source code. I usually INCLUDE my programs from COM: or use CLOAD with the CE-126P. Editing text files on the RAM disk can be done with a separate BASIC program called edit500 available at: http://www.andrewwoods3d.com/pce500/

Or you could use my new Forth500 TLOAD command defined below that loads Forth from a TEXT file created in BASIC with the built-in BASIC editor:

Code:
\ TLOAD.FTH load TEXT file with line numbers as Forth source
\ Author: Robert van Engelen
\ TLOAD filename

.( Loading TLOAD...)

ANEW _TLOAD_

DECIMAL

: TLOADED       ( c-addr u -- )
  \ open file and save fileid to the return stack
  R/O OPEN-FILE THROW >R
  \ read header 16 bytes + 0 0 CR bytes
  FIB 19 R@ READ-FILE 0= SWAP 19 = AND
  \ and check if it is a TEXT file header
  FIB 5 S\" \xff\x00\x08\x00\x34" S= AND IF
    BEGIN
      \ while not EOF and line number high byte is not $FF
      R@ READ-CHAR 0= SWAP $FF <> AND WHILE
      \ read and nip line number low byte
      R@ READ-CHAR NIP 0= WHILE
      \ get line len byte
      R@ READ-CHAR 0= WHILE ( -- len1 )
      \ read the len number of bytes into the FIB
      FIB OVER R@ READ-FILE 0= WHILE ( -- len1 len2 )
      \ all bytes read?
      OVER = WHILE ( -- len1 )
      \ evaluate the line read
      FIB SWAP ['] EVALUATE CATCH ?DUP IF
        \ drop string that failed to evaluate, close file and rethrow
        2DROP R> CLOSE-FILE DROP THROW
      THEN
    AGAIN
    ELSE DROP THEN
    ELSE 2DROP THEN
    ELSE DROP THEN
    THEN
    THEN
  ELSE
    ." not TEXT"
  THEN
  \ pop fileid from the return stack and close file
  R> CLOSE-FILE DROP
;

: TLOAD         ( "name" -- ) PARSE-NAME TLOADED ;

This approach is similar to the PC-G850(V)(S) C programming with its built-in line-number-based text editor. It is a bit odd to use line numbers. But hey, this is a throwback to the 80s Smile

How to use TLOAD:

1. in BASIC key in TEXT, the prompt will change to < to indicate the TEXT mode
2. type in your Forth program line-by-line with line numbers
3. use RENUM in case you can't insert a line
4. then SAVE "name" to save your program to RAM disk as name.BAS (don't forget this step!)
5. go back to Forth500 with CALL&B0000 (or CALL&B9000 on a 32K machine)
6. key in TLOAD name.BAS
7. done!

- 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...
Visit this user's website Find all posts by this user
Quote this message in a reply
04-03-2022, 11:06 PM (This post was last modified: 04-03-2022 11:10 PM by Helix.)
Post: #68
RE: FORTH for the SHARP PC-E500 (S)
(04-03-2022 12:53 AM)robve Wrote:  I haven't had the time to work on a text file editor for Forth500 to edit Forth source code. I usually INCLUDE my programs from COM: or use CLOAD with the CE-126P. Editing text files on the RAM disk can be done with a separate BASIC program called edit500 available at: http://www.andrewwoods3d.com/pce500/

Or you could use my new Forth500 TLOAD command defined below that loads Forth from a TEXT file created in BASIC with the built-in BASIC editor:

Thank you for this suggestion and for this clever program. However I'm currently busy with other things, and I will not test them soon.
I've just spent an hour trying the INCLUDE command, that I have never used so far, but for some reason it didn't work. Maybe I'm messing something in the file format, or I've forgotten one important detail, but I give up.
I will certainly return to Forth and to the PC-E500 next winter.

However, if this program works as expected, it should completly satisfy my needs Smile.
So, thank you again!

Jean-Charles
Find all posts by this user
Quote this message in a reply
04-04-2022, 12:47 AM (This post was last modified: 04-12-2022 02:17 AM by robve.)
Post: #69
RE: FORTH for the SHARP PC-E500 (S)
(04-03-2022 11:06 PM)Helix Wrote:  I've just spent an hour trying the INCLUDE command, that I have never used so far, but for some reason it didn't work. Maybe I'm messing something in the file format, or I've forgotten one important detail, but I give up.

Have you tried this? To load from COM:, make sure to set the serial parameters first on the E500 to correspond to the settings of your sending program, which should be done once (and for all) in BASIC with OPEN "baud rate, parity, word length, stop bit, A, C, &H1A, XON" AS #1 then CLOSE #1. The file to send should have an ending &1A (ctrl-Z) end of file.

PS. I should add that commands like INCLUDE open and then close the file, even when an error occurred. However, if files or COM: cannot be opened, then try fileid CLOSE-FILE . for some fileid>0 values to close the file. This closes the file if for some reason it stayed open e.g. after a program aborted due to an error when files are open.

- 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...
Visit this user's website Find all posts by this user
Quote this message in a reply
06-21-2022, 02:16 AM
Post: #70
RE: FORTH for the SHARP PC-E500 (S)
Compute digits of pi.
The following Forth500 program is based on the C code to compute the digits of pi by Dik T. Winter, CWI Amsterdam. It computes up to 9864 digits of pi, but is memory-restricted to about 6000 digits max:
Code:
.( Loading BIG-PI...)

ANEW _BIG_PI_

DECIMAL

0 VALUE b       0 VALUE c
0 VALUE e       0 VALUE g
0. 2VALUE d     

\ array f located at HERE + 40 bytes (hold area)
: f!    CELLS HERE + 40 + ! ;
: f@    CELLS HERE + 40 + @ ;

: big-pi    ( +n -- )
  DUP 4 < ABORT" too small"
  \ c=7*n/2; c-=c%14
  7 UM* D2/ D>S DUP 14 MOD - TO c
  \ check for sufficient space to store array f
  c UNUSED 40 - 1 RSHIFT U> ABORT" out of memory"
  \ f[0...c-1]=2000
  c 0 DO 2000 I f! LOOP
  \ e=0
  0 TO e
  CR
  BEGIN
    \ d=0; g=2*c-1; b=c
    0. TO d
    c 2* 1- TO g
    c TO b
    BEGIN
      \ d+=f[b]*10000; f[b]=d%g; d/=g; g-=2; b--
      b f@ 10000 UM* +TO d
      d g 0 D/MOD
      TO d
      D>S b f!
      -2 +TO g
      -1 +TO b
    b WHILE
      \ d*=b
      d b UMD* TO d
    REPEAT
    \ printf("%.4u",e+d/10000); e=d%10000
    d 10000 SM/REM
    e + 0 <# # # # # #> TYPE
    TO e
    \ c-=14
    -14 +TO c
  c 0= UNTIL
;

A screenful with the first 152 digits of pi (this takes one minute to compute):
Code:
152 big-pi
3141592653589793238462643383279502884197
1693993751058209749445923078164062862089
9862803482534211706798214808651328230664
70938446095505822317253594081284 OK[0]

A C program for the PC-G850(V)(S):
Code:
1 unsigned long a=10000,d;
2 unsigned b,c,e,*f,g;
3 main(){
4  printf("digits?");scanf("%u",&c);
5  c=7*c/2;c-=c%14;f=malloc(4*c+4);
6  for(;b-c;)f[b++]=a/5;
7  for(;d=0,g=c*2;c-=14,printf("%.4lu",e+d/a),e=d%a)
8   for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
9 }

The PC-G850VS calculates the first 152 digits of pi in 1 minute and 15 seconds.

This slightly modified version in C with 64 bit integers correctly computes up to 54935 digits of pi:
Code:
uint64_t a=10000,b,c,d,e,*f,g;
main(){printf("digits?");scanf("%llu",&c);c*=3.5;c-=c%14;f=malloc(sizeof(*f)*(c+1));
for(;b-c;)f[b++]=a/5;for(;d=0,g=c*2;c-=14,printf("%.4llu",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);}

The 54936th digit is off by one, followed by 10000 instead of 0000:

362524395716152714669005814610000
___________________________^x____
where a 7 should appear in place of the 6, falling the 1 to carry over to the 6. When pushing for 100000 digits this happens a few more times. I reckon the algorithm can be fixed to perform the carry. But this algorithm is quite slow for long digit sequences. There are also better algorithms, e.g. BBP, Chudnovsky.

- 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...
Visit this user's website Find all posts by this user
Quote this message in a reply
06-22-2022, 02:10 AM
Post: #71
RE: FORTH for the SHARP PC-E500 (S)
Thanks for sharing, these interesting algorithms will be a nice test for the C compiler I'm writing. I think once I've added integer division and remainder I can try the C program you provided for the PC-G850(V)(S).
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: