| Menu | JAQForum Ver 26.09 |
Forum Index : Microcontroller and PC projects : PicoMite - integrated compact C-compiler / C-runtime
OK, I see. No chance to change `pm_var()` to not raising a MMBasic error but instead returning a NULL pointer? Edit: "or simply declare the variable in BASIC beforehand" - yes, possible, too, but that requires knowledge about the internals of the CSUB from outside in the BASIC program - "not very beautiful".. You are right! The correct way would be (if static would be possible): static struct globals_s *globals = (static struct globals_s*)0; if (globals == (static struct globals_s*)0) { globals = GetMemory(...); // Allocate memory on 1st usage. } I know: "no politics" in this forum.. - but: "Ukraine, hold on!" ![]() Edited 2026-11-20 03:49 by bfwolf |
||||||
Hi bfwolf, I think there's a much simpler solution to your use case, and it already works with the existing CSUB API. No changes to `pm_var()` or `pm_dim()` are needed. You can use a BASIC `INTEGER` variable as an opaque context handle and pass it by reference to a CSUB. The same CSUB handles both initialization and subsequent calls: - If the handle is zero, allocate a new context using `GetMemory()` and return its address through the argument. - Otherwise, use the existing context. context.c: #include <stdint.h> #include "PicoCFunctions.h" struct globals_s { uint32_t counter; uint32_t values[100]; }; void Context(long long *handle) { struct globals_s *ctx; if (*handle == 0) { ctx = GetMemory(sizeof(*ctx)); *handle = (long long)(uintptr_t)ctx; } else { ctx = (struct globals_s *)(uintptr_t)*handle; } ctx->counter++; ctx->values[0] = ctx->counter; } MMBasic: CSUB Context INTEGER "context.c" END CSUB INTEGER ctx1, ctx2 Context ctx1 Context ctx1 Context ctx1 Context ctx2 Context ctx2 Here `ctx1` and `ctx2` represent two completely independent instances of the same C structure. Each is allocated on its first call and retains its state across subsequent calls. There's no need for static variables in the CSUB, no lookup of BASIC variables by name, and no special initialization or cleanup hooks. When the next BASIC `RUN` resets the heap, both contexts are automatically reclaimed along with their BASIC handles. You can also explicitly release a context using `FreeMemory()` when it's no longer needed, and set its handle back to zero. So effectively, BASIC owns the context handles, while C owns the internal structure and implementation. I think this is a cleaner solution, especially when multiple independent contexts are needed. And thank you for your kind words. Much appreciated! |
||||||
Would this firmware be a good place to begin learning "C", along with a learning-C book? .....Brian |
||||||
I'd start to learn on a PC because any errors (bad pointer, array index out of range, divide by zero, ...) will at least not crash the whole machine leaving you to wonder why. You can also use a debugger on a PC which can catch such and show where. John |
||||||
It depends on goal and limitations. In case you do not want to ocupy PC and free it for other person, it may have some profit. For other cases, I guess, it is better to use normal computer with a lot of helpers, IDEs, internat access to fast review hints and solve issue, and so on.. Also, may be usefult for you: https://coddy.tech/ - some interactive cources. You can goofle others, there are not only one. WBR. MikeV73. |
||||||
My personal opinion is you don't want to try to learn C by using BASIC to host it. It is going to be very difficult. You are better off bringing up Microsoft's Visual Studio and putting simple programs into it and compiling them. And the Integrated Development Environment as a very powerful debugger where you can step through things and see what is happening. That would be a more productive path. |
||||||
| The Back Shed's forum code is written, and hosted, in Australia. |