Re: various PETTIL design considerations
Posted: Wed Sep 19, 2018 8:56 pm
chitselb wrote:
Code: Select all
: CASE[ ( -- ADR ) COMPILE (CASE#) HERE 0 , ; IMMEDIATE
: ]END-CASE ( ADR -- ) HERE SWAP ! ; IMMEDIATE
pretty sure this should be :
: CASE[ ( -- ADR ) COMPILE (CASE#) HERE 0 C, ; IMMEDIATE
: ]END-CASE ( ADR -- ) HERE SWAP C! ; IMMEDIATECode: Select all
SCR# 93
// (CASE#) -- INLINE BRANCH ADDRESS
HEX
CODE (CASE#) ( N -- )
CLC,
IP )Y LDA, IP SBC,
.A LSR, N STA,
0 ,X LDA, N CMP,
CS IF, TYA, THEN,
.A ASL, TAY, INY, INY,
IP )Y LDA, W STA, INY,
IP )Y LDA, W 1+ STA, 1 # LDY,
IP )Y LDA, PHA, DEY,
IP )Y LDA, IP STA,
PLA, IP 1+ STA,
INX, INX, W 1- JMP,
END-CODE
SCR# 94
// CASE[ ]END-CASE
HEX
: CASE[ ( -- ADR )
COMPILE (CASE#)
HERE 0 , ; IMMEDIATE
: ]END-CASE ( ADR -- )
HERE SWAP ! ; IMMEDIATE
Code: Select all
: >MARK ( -- ADR 1 )
HERE 0 , 1 ;
: >RESOLVE ( ADR 1 -- )
?COMP
1 ?PAIRS HERE SWAP ! ;
Code: Select all
: >MARK ( -- ADR )
HERE 0 , ;
: >RESOLVE ( ADR -- )
HERE SWAP ! ;
Code: Select all
: THEN ( ADR CS -- )
>RESOLVE ; IMMEDIATE
Code: Select all
: CASE# ( -- ADR CS )
COMPILE (CASE#)
>MARK ; IMMEDIATE
The demo then becomes:
Code: Select all
SCR# 98
// TEST OF CASE#
HEX
: POWER ." POWERING UP SYSTEMS." ;
: TAKEOFF ." VERTICAL ASCENT." ;
: HOVER ." AIRWOLF HOVERING." ;
: TURBO ." TURBOS ENGAGED!" ;
: LAND ." LANDING AIRWOLF." ;
VARIABLE HELICOPTER
: AIRWOLF ( -- )
HELICOPTER @ CASE#
POWER TAKEOFF HOVER TURBO
LAND
THEN
CR .S ;
: TEST ( -- ) 6 -1 DO
I HELICOPTER ! CR AIRWOLF
LOOP ;
IIRC you plan to remove the headers for your final application ( you'll probably find a way to strip out the compiler words as well, since they will no longer be needed ). In that event, the first version, the one that takes an inline byte count would be more memory efficient for you.
Here are the two versions in high level Forth.
Here is the version of (CASE#) that takes an inline byte count in high level Forth:
Code: Select all
SCR# 99
// (CASE#) -- HIGH LEVEL BYTE COUNT
HEX
: (CASE#) ( N -- )
R> COUNT 2DUP 2* + >R
ROT TUCK SWAP
U< AND
2* +
@ EXECUTE ;
: CASE[ ( -- ADR )
COMPILE (CASE#) HERE 0 C, ;
IMMEDIATE
: ]END-CASE ( ADR -- )
HERE OVER - 1- 2/ SWAP C! ;
IMMEDIATE
Code: Select all
SCR# 9B
// (CASE#) -- HIGH LEVEL BRANCH
HEX
: (CASE#) ( N -- )
R> 2DUP DUP @ DUP>R
SWAP - 2/ 1-
U< ROT AND
1+ 2* +
@ EXECUTE ;
: CASE# ( -- ADR CS )
COMPILE (CASE#)
>MARK ; IMMEDIATE
And here are some notes about memory size.
Code: Select all
All versions of mini case structure default to case 0 if the number is outside the range of cases.
The two code level versions of (CASE#) allow a maximum of 127 compiled cases, 0 - 126
The high level version of (CASE#) with inline byte count allows a maximum of 255 cases, 0 - 254.
The high level version of (CASE#) with inline branch allows as many cases as will fit in available memory.
Here are some statistics about the sizes. The bodies tally includes the code fields of the words.
(CASE#) with inline count ( 1 byte )
Size of bodies: Whole word:
(CASE#) 41 bytes 51 bytes
CASE[ 14 bytes 22 bytes
]END-CASE 18 bytes 30 bytes
-------------------------------
Total: 73 bytes 103 bytes
(CASE#) with inline branch address ( 2 bytes )
Size of bodies: Whole word:
(CASE#) 48 bytes 58 bytes
CASE[ 14 bytes 22 bytes
]END-CASE 10 bytes 22 bytes
-------------------------------
Total: 72 bytes 102 bytes
(CASE#) with inline branch address
using >MARK and >RESOLVE
Size of bodies: Whole word:
(CASE#) 48 bytes 58 bytes
CASE[ 10 bytes 18 bytes
]END-CASE 6 bytes 18 bytes
-------------------------------
Total: 64 bytes 94 bytes
(CASE#) with inline branch address
using CASE# and THEN
Size of bodies: Whole word:
(CASE#) 48 bytes 58 bytes
CASE# 10 bytes 18 bytes
-------------------------------
Total 58 bytes 76 bytes
High level (CASE#) with inline count ( 1 byte )
Size of bodies: Whole word:
(CASE#) 34 bytes 44 bytes
CASE[ 14 bytes 22 bytes
]END-CASE 18 bytes 30 bytes
-------------------------------
Total: 66 bytes 96 bytes
High level (CASE#) with inline branch address with DUP>R
Size of bodies: Whole word:
(CASE#) 38 bytes 48 bytes
CASE# 10 bytes 18 bytes
-------------------------------
Total: 48 bytes 66 bytes
Jim
BTW this is for a Forth-83 ITC system with 16 bit addressing.