Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IF .. ELSE .. THEN with relative addressing #382

Closed
TG9541 opened this issue Nov 25, 2020 · 4 comments
Closed

IF .. ELSE .. THEN with relative addressing #382

TG9541 opened this issue Nov 25, 2020 · 4 comments
Assignees

Comments

@TG9541
Copy link
Owner

TG9541 commented Nov 25, 2020

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.

@TG9541 TG9541 self-assigned this Nov 25, 2020
@TG9541
Copy link
Owner Author

TG9541 commented Nov 25, 2020

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.

@VK6TT
Copy link
Collaborator

VK6TT commented Nov 27, 2020 via email

@TG9541
Copy link
Owner Author

TG9541 commented Nov 27, 2020

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.

@TG9541
Copy link
Owner Author

TG9541 commented Dec 2, 2020

I used the code above as-is. #383 contains some examples for words one can use instead of IF (e.g. ]A<IF). Other condition words can be made from any JRxx or BTJx.

Examples are in the improved I2C code here.

@TG9541 TG9541 closed this as completed Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants