Skip to content

Commit

Permalink
implement CASE and related words
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrit committed Jan 18, 2021
1 parent cc7b0c9 commit 28419bf
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 11 deletions.
99 changes: 91 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The resulting `forth.com` executable can be run in CP/M. For example<sup>4</sup>
```
A>FORTH ↵
Z80 fig-FORTH 1.3b
Z80 fig-FORTH 1.3c
: CUBE ( N -> N. CUBE A NUMBER ) ↵
DUP DUP ( NOW THERE ARE THREE COPIES ) ↵
* * ↵
Expand All @@ -25,48 +25,130 @@ A>
```

## Custom Words
This fig-FORTH implementation includes the following custom words:

`FILE cccc`
This fig-FORTH implementation includes the following custom words<sup>5</sup>:

`(OF)`&nbsp;&nbsp;&nbsp;&nbsp;` n1 n2 --- n1 ` _(if no match)_
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` n1 n2 --- ` _(if there is a match)_
<ul>
The run-time procedure compiled by `OF`. See the description of the
run-time behaviour of `OF`.
</ul>

`CASE`&nbsp;&nbsp;&nbsp;&nbsp;` --- addr n ` _(compiling)_
<ul>
Used in a colon definition in the form: `CASE...OF...ENDOF...ENDCASE`.
Note that `OF ... ENDOF` pairs may be repeated as necessary.

At compile time `CASE` saves the current value of `CSP` and resets
it to the current position of the stack. This information is used
by `ENDCASE` to resolve forward references left on the stack by any
`ENDOF`s which precede it. `n` is left for compiler error checking.

`CASE` has no run-time effects.
</ul>

`ENDCASE`&nbsp;&nbsp;&nbsp;&nbsp;` addr1...addrn n --- ` _(compiling)_
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` n --- ` _(if no match)_
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` --- ` _(if a match was found)_
<ul>
Used in a colon definition in the form: `CASE...OF...ENDOF...ENDCASE`.
Note that `OF ... ENDOF` pairs may be repeated as necessary.

At run-time, `ENDCASE` drops the select value if it does not equal any
case values. `ENDCASE` then serves as the destination of forward
branches from all previous `ENDOF`s.

At compile-time. `ENDCASE` compiles a `DROP` then computes forward
branch offsets until all addresses left by previous `ENDOF`s have been
resolved. Finally, the value of `CSP` saved by `CASE` is restored. `n`
is used for error checking.
</ul>

`ENDOF`&nbsp;&nbsp;&nbsp;&nbsp;` addr1 n1 --- addr2 n2 ` _(compiling)_
<ul>
Used in a colon definition in the form: `CASE...OF...ENDOF...ENDCASE`.
Note that `OF ... ENDOF` pairs may be repeated as necessary.

At run-time, `ENDOF` transfers control to the code following the next
`ENDCASE` provided there was a match at the last `OF`. If the was no
match at the last `OF`, `ENDOF` is the location to which execution
will branch.

At compile-time `ENDOF` emplaces `BRANCH` reserving a branch offset,
leaves the address `addr2` and `n2` for error checking. `ENDOF` also
resolves the pending forward branch from `OF` by calculating the offset
from `addr1` to `HERE` and storing it at `addr1`.
</ul>

`FILE`&nbsp;&nbsp;&nbsp;&nbsp;` cccc `
<ul>
Close the current .FTH file, and opens the given file. Note that a
file can be loaded automatically on startup by specifying its name on
the command line, e.g. `FORTH SCREENS.FTH`. Startup will be aborted
with a `No File` error message if the file cannot be opened.
</ul>

`FTYPE`&nbsp;&nbsp;&nbsp;&nbsp;` --- addr `
<ul>
A constant containing the three character file type used by `FILE`.
Defaults to **.FTH**.
</ul>

`OF`&nbsp;&nbsp;&nbsp;&nbsp;` --- addr n ` _(compiling)_
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` n1 n2 --- n1 ` _(if no match)_
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` n1 n2 --- ` _(if there is a match)_
<ul>
Used in a colon definition in the form: `CASE...OF...ENDOF...ENDCASE`.
Note that `OF ... ENDOF` pairs may be repeated as necessary.

At run-time, `OF` checks `n1` and `n2` for equality. If equal, `n1` and `n2`
are both dropped from the stack, and execution continues to the next `ENDOF`.
If not equal, only `n2` is dropped, and execution jumps to whatever follows
the next `ENDOF`.
</ul>

## RomWBW extensions
Support for RomWBW HBIOS features<sup>5</sup> is included when fig-FORTH
Support for RomWBW HBIOS features<sup>6</sup> is included when fig-FORTH
is built with the `-DROMWBW` flag:

`.B`&nbsp;&nbsp;&nbsp;&nbsp;` n -- `
<ul>
Print a BCD value, converted to decimal. No following blank is printed.
</ul>

`AT`&nbsp;&nbsp;&nbsp;&nbsp;` col row --- `
<ul>
Position the text cursor at the given position. Both column and
row positions are zero indexed, thus `0 0 AT` will move the cursor
to the top left. Note that `AT` does *not* update `OUT`.
</ul>

`CLS`&nbsp;&nbsp;&nbsp;&nbsp;` --- `
<ul>
Clear VDU screen.
</ul>

`KEY?`&nbsp;&nbsp;&nbsp;&nbsp;` --- c t ¦ f `
<ul>
Check if a key has been pressed. Returns false if no key has been
pressed. Returns true and the key's ascii code if a key has been
pressed.
</ul>

`STIME`&nbsp;&nbsp;&nbsp;&nbsp;` addr --- `
<ul>
Set the RTC time. addr is the address of the 6 byte date/time buffer,
YMDHMS. Each byte is BCD encoded.
</ul>

`TIME`&nbsp;&nbsp;&nbsp;&nbsp;` --- addr `
<ul>
Get the RTC time and leave the address of the 6 byte date/time buffer,
YMDHMS. Each byte is BCD encoded.
</ul>

## fig-FORTH Editor
The fig-FORTH EDITOR<sup>6</sup> is included in the `SCREENS.FTH` file:
The fig-FORTH EDITOR<sup>7</sup> is included in the `SCREENS.FTH` file:
```
FILE SCREENS ↵ ok
7 12 INDEX ↵
Expand Down Expand Up @@ -132,5 +214,6 @@ easy to create illegal instructions, resulting in systems hangs or crashes.
2. William Ragsdale, _'fig-FORTH INSTALLATION MANUAL'_ (San Carlos, CA: FORTH INTEREST GROUP, 1980)
3. Thomas Anderson, _The Telemark Assembler (TASM) User's Manual (1998)_, Vintagecomputer <http://www.vintagecomputer.net/software/TASM/TASMMAN.HTM> [Accessed 14 December 2020]
4. John James, _‘What Is Forth? A Tutorial Introduction’_, in BYTE, 5.8 (1980), 100–26
5. Wayne Warthen, _RomWBW Architecture_, (RetroBrew Computers Group, 2020)
6. Bill Stoddart, _'EDITOR USER MANUAL'_, (London, UK: FIG United Kingdom, ND)
5. Charles Eaker, _'JUST IN CASE'_ in FORTH DIMENSIONS, II/3 (1980), 37-40
6. Wayne Warthen, _RomWBW Architecture_, (RetroBrew Computers Group, 2020)
7. Bill Stoddart, _'EDITOR USER MANUAL'_, (London, UK: FIG United Kingdom, ND)
101 changes: 98 additions & 3 deletions figforth.asm
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@
; Upgraded to v1.3 by Dimitri Theulings - 11/2020 - changes marked 1.3
; Changes are based on '8080 FIG-FORTH 1.3 VERSION 0 18JUL81'
; ----------------------------------------------------------------------
; Added CASE statement as proposed by Charles Eaker, FD II/3, 37-40 - DT
; ----------------------------------------------------------------------
;
; Release & Version numbers
;
FIGREL .EQU 1 ;FIG RELEASE #
FIGREV .EQU 3 ;FIG REVISION #
USRVER .EQU 62H ;USER VERSION # DT
USRVER .EQU 63H ;USER VERSION # DT - 2021/01/17
;
;Console & printer drivers are in external source named
;CONPRTIO.FTH & disc drivers in DISCIO.FTH. It has 4 screen
Expand Down Expand Up @@ -1638,7 +1640,7 @@ GREAT2: JHPUSH
.BYTE '0'
.BYTE '>'+$80
.WORD GREAT-4
ZGREA .WORD DOCOL
ZGREA: .WORD DOCOL
.WORD ZERO,GREAT
.WORD SEMIS
;
Expand Down Expand Up @@ -3446,11 +3448,104 @@ WHILE: .WORD DOCOL
.WORD IFF
.WORD TWOP
.WORD SEMIS
;
.BYTE 0C4H ;CASE
.TEXT "CAS"
.BYTE 'E'+$80
.WORD WHILE-8
CASE: .WORD DOCOL
.WORD QCOMP
.WORD CSPP
.WORD AT
.WORD SCSP
.WORD LIT
.WORD 4
.WORD SEMIS
;
.BYTE 0C4H ;(OF)
.TEXT "(OF"
.BYTE ')'+$80
.WORD CASE-7
POF: .WORD $+2
POP HL
POP DE
OR A
SBC HL,DE
LD A,L
OR H
JP NZ,POF1
INC BC
INC BC
JP (IX)
POF1: PUSH DE
JP BRAN1
;
.BYTE 0C2H ;OF
.BYTE 'O'
.BYTE 'F'+$80
.WORD POF-7
OFF: .WORD DOCOL
.WORD LIT
.WORD 04H
.WORD QPAIR
.WORD COMP
.WORD POF
.WORD HERE
.WORD ZERO
.WORD COMMA
.WORD LIT
.WORD 5
.WORD SEMIS
;
.BYTE 0C5H ;ENDOF
.TEXT "ENDO"
.BYTE 'F'+$80
.WORD OFF-5
ENDOF: .WORD DOCOL
.WORD LIT
.WORD 5
.WORD QPAIR
.WORD COMP
.WORD BRAN
.WORD HERE
.WORD ZERO
.WORD COMMA
.WORD SWAP
.WORD TWO
.WORD THEN
.WORD LIT
.WORD 4
.WORD SEMIS
;
.BYTE 0C7H ;ENDCASE
.TEXT "ENDCAS"
.BYTE 'E'+$80
.WORD ENDOF-8
ECASE: .WORD DOCOL
.WORD LIT
.WORD 4
.WORD QPAIR
.WORD COMP
.WORD DROP
ECAS1: .WORD SPAT
.WORD CSPP
.WORD AT
.WORD EQUAL
.WORD ZEQU
.WORD ZBRAN
.WORD ECAS2-$
.WORD TWO
.WORD THEN
.WORD BRAN
.WORD ECAS1-$
ECAS2: .WORD CSPP
.WORD STORE
.WORD SEMIS
;
.BYTE 86H ;SPACES
.TEXT "SPACE"
.BYTE 'S'+$80
.WORD WHILE-8
.WORD ECASE-10
SPACS: .WORD DOCOL
.WORD ZERO
.WORD MAX
Expand Down

0 comments on commit 28419bf

Please sign in to comment.