TG9541 / stm8ef

STM8 eForth - a user friendly Forth for simple µCs with docs
https://github.com/TG9541/stm8ef/wiki
Other
311 stars 66 forks source link

Strange behaviour of LITERAL #457

Closed Eelkhoorn closed 1 year ago

Eelkhoorn commented 1 year ago

When used within a "if then" block LITERAL behaves strange.

HEX ABCD DUP : test1 LITERAL ; : test2 IF LITERAL THEN ; test1 . ABCD ok ( this is expected) -1 test2 . B4 ok ( ???)

TG9541 commented 1 year ago

Hoi @Eelkhoorn!

I'm not sure if that's what you intend to do... but the following code will show a more consistent behavior:

HEX ABCD
: test2 IF [ SWAP ] LITERAL THEN ; 

The result is now consistent with the input: -1 test2 . ABCD ok.

Without the SWAP you'd be using the address of the dummy IF jump target (patched by THEN).

Eelkhoorn commented 1 year ago

Hoi Thomas

OK, that makes sense. Now I understand what happens here. I wanted to make a vector table, like:

here ( start of table) ' some_word , ' some_other_word , ... ... ' last_word ,

: execute_vector ( n f--) IF [ SWAP ] 2* LITERAL + @ EXECUTE THEN ;

So, it is no issue at all, just my poor understanding of IF THEN compilation. Thank you very much for the explanation.

I will look at the other issue (#456) tomorrow. I don't directly recall why I came up with that. It might indeed be for power consumption reasons, I made a small solar battery charger with a STM8L that uses ADC and DAC some years ago.

On Sat, 28 Jan 2023 10:30:34 -0800 Thomas @.***> wrote:

Hoi @Eelkhoorn!

I'm not sure if that's what you intend to do... but the following code will show a more consistent behavior:

HEX ABCD
: test2 IF [ SWAP ] LITERAL THEN ; 

The result is now consistent with the input: -1 test2 . ABCD ok.

Without the SWAP you'd be using the address of the dummy IF jump target (patched by THEN).

TG9541 commented 1 year ago

@Eelkhoorn maybe the FC-table implementation in STM8-Modbus MBPROTO matches your use case: it allows patching a table at runtime and it's well tested. FC>XT is the access word and @?EXEC the execution word. You'll also find an extension mechanism with a default word.

Eelkhoorn commented 1 year ago

Hoi Thomas

That is very interesting, thank you. I am currently trying to integrate stm8ef in muforth. I intend to leave the interpreter and compiler out of the kernel to save flash.

On Sat, 28 Jan 2023 21:40:06 -0800 Thomas @.***> wrote:

@Eelkhoorn maybe the FC-table implementation in STM8-Modbus MBPROTO matches your use case: it allows patching a table at runtime and it's well tested. FC>XT is the access word and @?EXEC the execution word. You'll also find an extension mechanism with a default word.

TG9541 commented 1 year ago

Wow, that sounds interesting! If you get it working please share!

Eelkhoorn commented 1 year ago

Here is the first attempt of using muforth to take over the interpreting and compiling functions of stm8ef. I tested it with two targets: MINDEV and W1209-FD. It is probably full of bugs, comments and suggestions for improvement are welcome. I tried it on a W1209-FD application for a simple timer, with the menu structure copied from the logging thermostat project, and it works fine. The flash space reduction is significant: kernel takes 2179 bytes, the application takes 1213 bytes, 4799 bytes left.

https://github.com/Eelkhoorn/stm8ef-mu

On Sun, 29 Jan 2023 12:17:09 -0800 Thomas @.***> wrote:

Wow, that sounds interesting! If you get it working please share!

TG9541 commented 1 year ago

I'm looking forward to testing https://github.com/Eelkhoorn/stm8ef-mu !