IF .. ELSE .. THEN with relative addressing
TG9541 opened this issue · 4 comments
Some applications, e.g. ISR handlers, Flash page write, or STM8L Low-Power-Run Mode require code that doesn't depend on the STM8 eForth core. While coding in assembler or C is possible, it would be great if Forth structures like IF .. ELSE .. THEN
could be used with JRxx
or BTJx
.
An example is the improved I2C driver.
Here is my first sketch but I'm not sure if that's the best way to package this:
\ STM8eForth : control structures with relative addressing TG9541-201124
\ ------------------------------------------------------------------------------
#require >Y
: THEN ( -- ) [COMPILE] [ HERE OVER - 1- SWAP C! [COMPILE] ] ; IMMEDIATE
: >REL ( -- ) HERE 0 C, ; \ like >MARK for rel. branch
: ELSE ( -- ) [COMPILE] [ $20 C, [COMPILE] ] >REL \ JRA rel
SWAP [COMPILE] THEN ; IMMEDIATE
: JREQ ( F:Z -- ) [COMPILE] [ $27 C, [COMPILE] ] >REL ; IMMEDIATE
: IF ( n -- ) COMPILE >Y [COMPILE] JREQ ; IMMEDIATE
What worked quite well is requiring >REL
in a combined bit-test-and-IF word like [ addr b# ]B@IF
here:
#require >REL
: ]B@IF ( -- ) 2* $7201 + , , ] >REL ; \ BTJF a,#bit
A #require ]B@IF
thus redefines ELSE
and THEN
, and in order to avoid surprises, also IF
. There is currently no protection against relative offsets larger than +/- 127.
Hi Richard, thanks, and greetings your way!
To tell the truth this is the first time I actually use an I2C peripheral. Bit-banging I2C is the way of gravity because since, to work with a device only needs to read one datasheet, not two. I'm now moving from an 8K EEPROM to a DS1621 to learn how the I2C API should look like. Obviously there needs to be error handling and a handshake with the application.