From 215017e547ed4a24c15accc0eb80ef30b5ba1eac Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 16 Mar 2026 11:12:51 +0100 Subject: [PATCH] Mon 16 Mar 11:09:06 CET 2026 --- src/context.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/context.c diff --git a/src/context.c b/src/context.c new file mode 100644 index 0000000..a00a6b0 --- /dev/null +++ b/src/context.c @@ -0,0 +1,99 @@ +#include "config.h" +#include "types.h" +#include "error.h" +#include "lexer.h" +#include "ops.h" +#include "mem.h" +#include "stack.h" +#include "reg.h" +#include "utils.h" +#include "vm.h" +#include "printf.h" +#include "ccall.h" +#include "context.h" +#include "event.h" +#include "debug.h" +#include "log.h" + +context_t *Context[MAXTASKS]; +int ContextTop=0; + +context_t *ContextAllocate() { + context_t *c = (context_t *)MEMALLOC(sizeof(context_t)); + MEMSET(c,0,sizeof(context_t)); + return c; +} + +/* + Initialize one context +*/ +context_t *ContextInit(context_t* C, mem_t *M,stack_t *DS, stack_t *FS, reg_t *R) { + int index=ContextTop; +#if DEBUG > 0 + log(LOGDEBUG1,"ContextInit(%d)\n",index); +#endif + if (!C) C=(context_t*)ContextAllocate(); + Context[index]=C; + C->index=index; + if (M) { + C->M=M; + } else { + + } + if (DS) { + C->DS=DS; + } else if (!C->DS) { + C->DS=StackAllocate(DEFSTACKSIZE); + } + if (FS) { + C->FS=FS; + } else if (!C->FS) { + C->FS=StackAllocate(DEFSTACKSIZE); + } + if (R) { + C->R=R; + } else if (!C->R) { + C->R=RegAllocate(); + } + RegInit(C->R); + R->context=index; // assign context id to register set + C->next=NULL; + if (index>0) { + Context[index-1]->next=C; + } + ContextTop++; + return C; +} + +/* + Reset entire context (Stack, Code segment, release events, ...) +*/ +int ContextReset(context_t *c) { +#if DEBUG > 0 + log(LOGDEBUG1,"ContextReset(%d)\n",CONTEXTID(c)); +#endif + if (c->S) ReleaseSegment(c->M,c->S); + c->S=NULL; + StackReset(c->DS); + StackReset(c->FS); + RegInit(c->R); + c->R->context=CONTEXTID(c); + c->R->state=PIDLE; + c->evmask=0; +} + +/* + new +*/ +void ContextResetAll(context_t *c0) { + if (c0==NULL) c0=Context[0]; + if (!ContextTop) return; + context_t *c=c0; + mem_t *M=c->M; + while(c) { + ContextReset(c); + c=c->next; + } + MemReset(M); + EventInit(1); +}