Post Reply 
FORTH for the PC-G850(V)(S)
11-13-2022, 10:15 PM (This post was last modified: 05-21-2023 12:47 PM by robve.)
Post: #8
RE: FORTH for the PC-G850(V)(S)
Forth850 can be extended "on the fly" with your own Z80 machine code using the PC-G850(V)(S) built-in Z80 Assembler.

To clarify, here are the steps. A BEEP example follows below.
  • first define the word in Forth850 with NFA, <name>
  • HERE HEX . displays the ORG xxxx address to specify in the assembly code
  • write the assembly in the TEXT editor with this ORG xxxxH address
  • run the Assembler, which adds the code to the <name> you defined in Forth850
  • write down the number of bytes assembled, this is important
  • in Forth850, DECIMAL <number> ALLOT makes the code part of <name> so that HERE HEX . displays the next address after the code (use negative -nnnn ALLOT to undo ALLOT when making code changes, or pick a larger ALLOT value to reserve space for assembly code additions)
  • IMPORTANT: rerun the Assembler, because the first assembly was overwritten by Forth850 (the hold area is HERE) before you ALLOTed
A couple of (obvious) points to keep in mind when writing machine code for Forth850:
  • register BC holds the IP (instruction pointer) and should not be modified, i.e. save it somewhere to restore it before returning
  • register DE holds the TOS (top of stack) and can be used and set
  • the stack can be used to push/pop Forth parameters
  • register IY holds the address of "next" and should not be modified
  • return from the code with JP (IY), never use RET

Example BEEP word.

In Forth850:
Code:
NFA, BEEP
HERE HEX .
This displays the ORG address for the assembly of the new BEEP word. Suppose the address is 20FA, then in TEXT editor (line numbers not shown) we write:
Code:
      ORG 20FA
      di              ;
      di              ; disable interrupts
      ld hl,0000h     ;
      xor a           ;
loop: out (18h),a     ; loop, out audio port
wait: dec l           ;   loop
      jr nz,wait      ;   until --l=0
      cpl             ; switch on/off
      dec h           ;
      jr nz,loop      ; until --h=0
      ei              ; enable interrupts
      jp (iy)         ; next

Assemble the code, which is 18 bytes long. Return to Forth850 with BASIC and CALL256 and enter:
Code:
18 ALLOT

Important: run the Assembler again to save the code in the ALLOTed space.

Now BEEP works in Forth850.

Alternatively, the BEEP word can also be defined with HEX codes in Forth850 as follows, which takes more effort but with the same result:
Code:
NFA, BEEP       ( -- )
  HEX
  F3 C,         \       di              ;
  F3 C,         \       di              ; disable interrupts
  21 C, 0 ,     \       ld hl,0000h     ;
  AF C,         \       xor a           ;
  D3 C, 18 C,   \ loop: out (18h),a     ; loop, out audio port
  D2 C,         \ wait: dec l           ;   loop
  20 C, FD C,   \       jr nz,wait      ;   until --l=0
  2F C,         \       cpl             ; switch on/off
  25 C,         \       dec h           ;
  20 C, F7 C,   \       jr nz,loop      ; until --h=0
  FB C,         \       ei              ; enable interrupts
  FD C, E9 C,   \       jp (iy)         ; next
  DECIMAL       \ 18 bytes

A simple example that flips the bytes of the TOS stored in register DE, which takes 5 bytes of machine code:
Code:
NFA, FLIP
HERE HEX .
xxxx
5 ALLOT
Code:
      ORG xxxxH
      ld a,e
      ld e,d
      ld d,a
      jp (iy)

As always when writing assembly, if something is seriously wrong with it then we may crash and have to start over. In the worst case we have to install Forth850 again when the dictionary is damaged by "random POKEs". However, it is often not necessary to reset the machine when asked for MEMORY CLEAR (Y/N) just say N (NO) and give it another try after fixing the problem.

- 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
Post Reply 


Messages In This Thread
FORTH for the PC-G850(V)(S) - robve - 11-06-2022, 01:56 PM
RE: FORTH for the PC-G850(V)(S) - robve - 11-08-2022, 09:23 PM
RE: FORTH for the PC-G850(V)(S) - Sukiari - 11-09-2022, 07:45 PM
RE: FORTH for the PC-G850(V)(S) - robve - 11-10-2022, 09:36 PM
RE: FORTH for the PC-G850(V)(S) - xerxes - 11-11-2022, 01:55 AM
RE: FORTH for the PC-G850(V)(S) - robve - 11-12-2022, 12:28 AM
RE: FORTH for the PC-G850(V)(S) - robve - 11-12-2022, 06:26 PM
RE: FORTH for the PC-G850(V)(S) - robve - 11-13-2022 10:15 PM
RE: FORTH for the PC-G850(V)(S) - robve - 11-14-2022, 07:35 PM
RE: FORTH for the PC-G850(V)(S) - robve - 11-29-2022, 07:56 PM
RE: FORTH for the PC-G850(V)(S) - robve - 12-07-2022, 03:07 AM
RE: FORTH for the PC-G850(V)(S) - robve - 01-01-2023, 05:45 PM
RE: FORTH for the PC-G850(V)(S) - F-73P - 01-03-2023, 03:32 AM
RE: FORTH for the PC-G850(V)(S) - robve - 01-03-2023, 06:57 PM
RE: FORTH for the PC-G850(V)(S) - F-73P - 01-05-2023, 07:52 AM
RE: FORTH for the PC-G850(V)(S) - robve - 05-20-2023, 11:11 PM
RE: FORTH for the PC-G850(V)(S) - robve - 07-31-2023, 01:53 AM



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