From 578cac237497d8796a917d657ac13ff663c5d4b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krystian=20Bac=C5=82awski?= Date: Thu, 23 Mar 2023 20:20:40 +0100 Subject: [PATCH] Make framework configurable for 64k intro (backported from dce1b96) --- build/common.mk | 2 +- config.mk | 9 ++++++++- include/effect.h | 4 ++++ include/system/debug.h | 2 +- include/system/memory.h | 5 +++++ include/system/mutex.h | 10 ++++++++++ system/Makefile | 10 ++++++++-- system/kernel/exception.c | 2 +- system/kernel/interrupt.c | 18 ++++++++++++++++++ system/kernel/intr-entry.S | 4 ++++ system/kernel/memory.c | 8 ++++++-- system/kernel/task.c | 11 ----------- system/loader.c | 4 +++- 13 files changed, 69 insertions(+), 20 deletions(-) diff --git a/build/common.mk b/build/common.mk index b8ae74a1..43ea5de5 100644 --- a/build/common.mk +++ b/build/common.mk @@ -29,7 +29,7 @@ LDSCRIPT := $(TOPDIR)/system/amiga.lds # Don't reload library base for each call CPPFLAGS += -D__CONSTLIBBASEDECL__=const CPPFLAGS += -DCHIPMEM_KB=$(CHIPMEM) -DFASTMEM_KB=$(FASTMEM) -DLOGOUT=$(LOGOUT) -CPPFLAGS += -DPROFILER=$(PROFILER) +CPPFLAGS += -DPROFILER=$(PROFILER) -DMULTITASK=$(MULTITASK) -DMEMDEBUG=$(MEMDEBUG) include $(TOPDIR)/config.mk diff --git a/config.mk b/config.mk index ef2f91e2..471c79d2 100644 --- a/config.mk +++ b/config.mk @@ -21,9 +21,16 @@ AMIGAOS := 0 # 1 => Turn on profiler that reports minimum-average-maximum number of raster # lines measured between calls to ProfilerStart / ProfilerStop. # The measurement is reported every 50 frames (i.e. once a second). -# 0 => Disable the profiler. PROFILER := 1 +# 1 => Enable multitasking feature. It can be necessary to run background thread +# that loads data from disk while the effects are running. +MULTITASK := 1 + +# 1 => Enable dynamic memory allocation debugging incl. nice diagnostic printout +# when memory corruption is detected or we run out of memory. +MEMDEBUG := 1 + # [only when AMIGAOS=1] Amount of chip and fast (or public) memory # (in kilobytes!) passed to our custom memory allocator. # To calculate total memory taken after an executable file diff --git a/include/effect.h b/include/effect.h index baf9f92c..acf12bde 100644 --- a/include/effect.h +++ b/include/effect.h @@ -120,9 +120,13 @@ typedef struct Profile { #define ProfilerStop(NAME) #endif +#if MULTITASK /* Puts a task into sleep waiting for Vertical Blank interrupt. * Let's background task do its job. */ void TaskWaitVBlank(void); +#else +#define TaskWaitVBlank WaitVBlank +#endif void _ProfilerStart(ProfileT *prof); void _ProfilerStop(ProfileT *prof); diff --git a/include/system/debug.h b/include/system/debug.h index 0fc9698e..24335b59 100644 --- a/include/system/debug.h +++ b/include/system/debug.h @@ -14,7 +14,7 @@ void Log(const char *format, ...) __noreturn void Panic(const char *format, ...) __attribute__ ((format (printf, 1, 2))); #else -#define Log(...) +#define Log(...) ((void)0) #define Panic(...) HALT() #endif #endif diff --git a/include/system/memory.h b/include/system/memory.h index 55626347..9abce961 100644 --- a/include/system/memory.h +++ b/include/system/memory.h @@ -20,8 +20,13 @@ #endif #ifdef _SYSTEM +#if MEMDEBUG void MemCheck(int verbose); u_int MemAvail(u_int attributes); +#else +#define MemCheck(_) { (void)0; } +#define MemAvail(_) 0 +#endif void AddMemory(void *ptr, u_int byteSize, u_int attributes); #endif diff --git a/include/system/mutex.h b/include/system/mutex.h index 2beead4e..21eb41e9 100644 --- a/include/system/mutex.h +++ b/include/system/mutex.h @@ -6,6 +6,7 @@ struct Task; +#if MULTITASK typedef struct Mutex { volatile struct Task *owner; TAILQ_HEAD(, Task) waitList; @@ -23,5 +24,14 @@ static inline void MutexInit(MutexT *mtx) { void MutexLock(MutexT *mtx); void MutexUnlock(MutexT *mtx); +#else +typedef struct Mutex {} MutexT; + +#define MUTEX(name) MutexT name = (MutexT){} + +static inline void MutexInit(MutexT *mtx) { (void)mtx; } +static inline void MutexLock(MutexT *mtx) { (void)mtx; } +static inline void MutexUnlock(MutexT *mtx) { (void)mtx; } +#endif #endif /* !__SYSTEM_MUTEX_H__ */ diff --git a/system/Makefile b/system/Makefile index 798197d7..d0b39ca4 100644 --- a/system/Makefile +++ b/system/Makefile @@ -32,11 +32,17 @@ SOURCES := \ kernel/interrupt.c \ kernel/intr-entry.S \ kernel/memory.c \ - kernel/mutex.c \ - kernel/task.c \ kernel/trap-entry.S \ kernel/trap.c +include $(TOPDIR)/config.mk + +ifeq ($(MULTITASK), 1) +SOURCES += \ + kernel/mutex.c \ + kernel/task.c +endif + CFLAGS.amigaos = -Wno-strict-prototypes BUILD-FILES = crt0.o diff --git a/system/kernel/exception.c b/system/kernel/exception.c index 8657a6f7..ef2d097b 100644 --- a/system/kernel/exception.c +++ b/system/kernel/exception.c @@ -30,7 +30,7 @@ void SetupExceptionVector(BootDataT *bd) { ExcVec[EXC_FMTERR] = FmtErrTrap; /* Intialize TRAP instruction handlers. */ - ExcVec[EXC_TRAP(0)] = YieldHandler; + ExcVec[EXC_TRAP(0)] = MULTITASK ? YieldHandler : TrapInstTrap; for (i = EXC_TRAP(1); i <= EXC_TRAP(15); i++) ExcVec[i] = TrapInstTrap; diff --git a/system/kernel/interrupt.c b/system/kernel/interrupt.c index d6188bf5..26004555 100644 --- a/system/kernel/interrupt.c +++ b/system/kernel/interrupt.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -24,6 +25,23 @@ void SetIntVector(u_int irq, IntHandlerT code, void *data) { iv->data = data; } +#if MULTITASK +#define IntrNest CurrentTask->intrNest +#else +static __code short IntrNest = 0; +#endif + +void IntrEnable(void) { + Assume(IntrNest > 0); + if (--IntrNest == 0) + CpuIntrEnable(); +} + +void IntrDisable(void) { + CpuIntrDisable(); + IntrNest++; +} + /* List of interrupt servers. */ typedef struct IntChain { IntServerT *head; diff --git a/system/kernel/intr-entry.S b/system/kernel/intr-entry.S index 19013efe..8ac91ae0 100644 --- a/system/kernel/intr-entry.S +++ b/system/kernel/intr-entry.S @@ -71,6 +71,7 @@ ENTRY(EnterIntr) END(EnterIntr) ENTRY(LeaveIntr) +#if MULTITASK /* * Check if we need to reschedule a task - usually as a result waking * up a higher priorty task while running interrupt service routine. @@ -91,6 +92,7 @@ ENTRY(LeaveIntr) clr.b (a0) /* clear reschedule flag */ movem.l (sp)+,d0-d1/a0-a1 /* restore registers */ jra _L(YieldHandler)+4 /* force a task switch (skip or.w to sr) */ +#endif .Lnoswitch: movem.l (sp)+,d0-d1/a0-a1 @@ -140,10 +142,12 @@ END(DummyInterruptHandler) rte /* restore SR and PC */ .endm +#if MULTITASK ENTRY(YieldHandler) SAVECTX jsr _L(TaskSwitch) LOADCTX END(YieldHandler) +#endif # vim: ft=gas:ts=8:sw=8:noet: diff --git a/system/kernel/memory.c b/system/kernel/memory.c index 5dc19cf3..d565827c 100644 --- a/system/kernel/memory.c +++ b/system/kernel/memory.c @@ -119,7 +119,7 @@ static inline WordT *BtPrev(WordT *bt) { return (void *)bt - BtSize(ft); } -static const char *MemoryName(u_int attributes) { +__unused static const char *MemoryName(u_int attributes) { if (attributes & MEMF_CHIP) return "chip"; if (attributes & MEMF_FAST) @@ -364,6 +364,7 @@ static void *ArenaMemResize(ArenaT *ar, void *old_ptr, u_int size) { return new_ptr; } +#if MEMDEBUG #define Msg(...) if (verbose) Log(__VA_ARGS__) static void ArenaCheck(ArenaT *ar, int verbose) { @@ -380,7 +381,7 @@ static void ArenaCheck(ArenaT *ar, int verbose) { for (; bt < ar->end; prev = bt, bt = BtNext(bt)) { int flag = !!BtGetPrevFree(bt); - int is_last = !!BtGetIsLast(bt); + __unused int is_last = !!BtGetIsLast(bt); Msg("$%08lx: [%c%c:%ld] %c\n", (uintptr_t)bt, "FU"[BtUsed(bt)], " P"[flag], BtSize(bt), " *"[is_last]); if (BtFree(bt)) { @@ -410,6 +411,7 @@ static void ArenaCheck(ArenaT *ar, int verbose) { MutexUnlock(&MemMtx); } +#endif static ArenaT *ArenaOf(void *ptr) { ArenaT *ar; @@ -480,6 +482,7 @@ void *MemResize(void *old_ptr, u_int size) { return NULL; } +#if MEMDEBUG void MemCheck(int verbose) { ArenaT *ar; for (ar = FirstArena; ar != NULL; ar = ar->succ) @@ -494,3 +497,4 @@ u_int MemAvail(u_int attributes) { avail += ar->totalFree; return avail; } +#endif diff --git a/system/kernel/task.c b/system/kernel/task.c index 4b8cbe6e..05336e68 100644 --- a/system/kernel/task.c +++ b/system/kernel/task.c @@ -11,17 +11,6 @@ static TaskListT ReadyList = TAILQ_HEAD_INITIALIZER(ReadyList); static TaskListT WaitList = TAILQ_HEAD_INITIALIZER(WaitList); u_char NeedReschedule = 0; -void IntrEnable(void) { - Assume(CurrentTask->intrNest > 0); - if (--CurrentTask->intrNest == 0) - CpuIntrEnable(); -} - -void IntrDisable(void) { - CpuIntrDisable(); - CurrentTask->intrNest++; -} - void TaskInit(TaskT *tsk, const char *name, void *stkptr, u_int stksz) { bzero(tsk, sizeof(TaskT)); strlcpy(tsk->name, name, MAX_TASK_NAME_SIZE); diff --git a/system/loader.c b/system/loader.c index 336613fd..e56c05d6 100644 --- a/system/loader.c +++ b/system/loader.c @@ -69,11 +69,13 @@ void Loader(BootDataT *bd) { /* Lower interrupt priority level to nominal. */ SetIPL(IPL_NONE); +#if MULTITASK TaskInit(CurrentTask, "main", bd->bd_stkbot, bd->bd_stksz); +#endif CallFuncList(&__INIT_LIST__); { - int retval = main(); + __unused int retval = main(); Log("[Loader] main() returned %d.\n", retval); }