Compare commits

...

3 Commits

19 changed files with 80 additions and 58 deletions

View File

@ -24,10 +24,10 @@
/* /*
* Maximum number of objects we can create (Slabheap). * Maximum number of objects we can create (Slabheap).
*/ */
#define MAX_TASKS (3) // Including main and idle task. #define MAX_TASKS (4) // Including main and idle task.
#define MAX_EVENTS (10) #define MAX_EVENTS (16)
#define MAX_MUTEXES (3) #define MAX_MUTEXES (8)
#define MAX_SEMAPHORES (0) #define MAX_SEMAPHORES (2)
#define MAX_TIMERS (0) #define MAX_TIMERS (0)
#define IDLE_STACK_SIZE (0x1000) // Keep in mind this stack is used in interrupt contex! TODO: Change this. #define IDLE_STACK_SIZE (0x1000) // Keep in mind this stack is used in interrupt contex! TODO: Change this.

View File

@ -46,4 +46,4 @@ KRes switchContext(KRes res, uintptr_t *oldSp, uintptr_t newSp);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -18,8 +18,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <assert.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include "types.h" #include "types.h"
#include "internal/list.h" #include "internal/list.h"
@ -75,4 +73,4 @@ static inline void kernelUnlock(void)
void _eventSlabInit(void); void _eventSlabInit(void);
void _mutexSlabInit(void); void _mutexSlabInit(void);
void _semaphoreSlabInit(void); void _semaphoreSlabInit(void);
void _timerInit(void); void _timerInit(void);

View File

@ -1,17 +1,20 @@
#pragma once #pragma once
// Based on https://github.com/torvalds/linux/blob/master/include/linux/list.h
#include <assert.h>
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#ifdef __cplusplus
extern "C"
{
#endif
// Based on https://github.com/torvalds/linux/blob/master/include/linux/list.h
#define LIST_INIT_VAL(name) ((ListNode){&(name), &(name)}) #define LIST_INIT_VAL(name) ((ListNode){&(name), &(name)})
#define LIST_ENTRY(ptr, type, member) \ #define LIST_ENTRY(ptr, type, member) \
({ \ ({ \
void *__mptr = (void*)(ptr); \ u8 *__mptr = (void*)(ptr); \
(type*)(__mptr - (size_t)&((type*)0)->member); \ (type*)(__mptr - (size_t)&((type*)0)->member); \
}) })
@ -101,3 +104,7 @@ static inline ListNode* listRemoveHead(ListNode *start)
#define listPop(start) listRemoveTail((start)) #define listPop(start) listRemoveTail((start))
#define listPushTail(start, node) listAddAfter((start), (node)) #define listPushTail(start, node) listAddAfter((start), (node))
#define listPopHead(start) listRemoveHead((start)) #define listPopHead(start) listRemoveHead((start))
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -22,6 +22,11 @@
#include "internal/list.h" #include "internal/list.h"
#ifdef __cplusplus
extern "C"
{
#endif
typedef ListNode SlabHeap; typedef ListNode SlabHeap;
@ -61,3 +66,7 @@ void* slabCalloc(SlabHeap *slab, size_t clrSize);
* @param ptr The object slot pointer. * @param ptr The object slot pointer.
*/ */
void slabFree(SlabHeap *slab, void *ptr); void slabFree(SlabHeap *slab, void *ptr);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -22,6 +22,11 @@
#ifdef __cplusplus
extern "C"
{
#endif
static inline void spinlockLock(u32 *lock) static inline void spinlockLock(u32 *lock)
{ {
u32 tmp; u32 tmp;
@ -43,3 +48,7 @@ static inline void spinlockUnlock(u32 *lock)
"sev" "sev"
: : "r" (0), "r" (lock) : "memory"); : : "r" (0), "r" (lock) : "memory");
} }
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -19,5 +19,14 @@
*/ */
#ifdef __cplusplus
extern "C"
{
#endif
#define LIKELY(expr) __builtin_expect((expr), true) #define LIKELY(expr) __builtin_expect((expr), true)
#define UNLIKELY(expr) __builtin_expect((expr), false) #define UNLIKELY(expr) __builtin_expect((expr), false)
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -75,4 +75,4 @@ void taskExit(void);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif

View File

@ -18,17 +18,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdbool.h>
#include "kernel.h" #include "kernel.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
/** /**
* @brief Creates a new kernel event. * @brief Creates a new kernel event.
* *
@ -82,4 +80,4 @@ void clearEvent(KHandle const kevent);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif

View File

@ -21,6 +21,7 @@
#include "kernel.h" #include "kernel.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
@ -28,8 +29,6 @@ extern "C"
// TODO: Implement this using semaphores? // TODO: Implement this using semaphores?
/** /**
* @brief Creates a new kernel mutex. * @brief Creates a new kernel mutex.
* *
@ -65,4 +64,4 @@ KRes unlockMutex(KHandle const kmutex);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif

View File

@ -19,17 +19,15 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include "kernel.h" #include "kernel.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
/** /**
* @brief Creates a new kernel semaphore. * @brief Creates a new kernel semaphore.
* *
@ -75,4 +73,4 @@ void signalSemaphore(KHandle const ksema, uint32_t signalCount, bool reschedule)
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif

View File

@ -21,13 +21,12 @@
#include "kernel.h" #include "kernel.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #endif
/*KHandle createTimer(bool pulse); /*KHandle createTimer(bool pulse);
void deleteTimer(KHandle const ktimer); void deleteTimer(KHandle const ktimer);
@ -40,4 +39,4 @@ KRes waitForTimer(KHandle const ktimer);*/
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif #endif

View File

@ -1,6 +1,6 @@
/* /*
* This file is part of open_agb_firm * This file is part of libn3ds
* Copyright (C) 2021 derrek, profi200 * Copyright (C) 2024 derrek, profi200
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -17,13 +17,12 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h> #include <string.h>
#include "types.h" #include "types.h"
#include "kernel.h" #include "kernel.h"
#include "internal/config.h" #include "internal/config.h"
#include "internal/kernel_private.h" #include "internal/kernel_private.h"
#include "internal/kmemcpy_set.h" #include "memory.h"
#include "internal/slabheap.h" #include "internal/slabheap.h"
#include "internal/util.h" #include "internal/util.h"
#include "internal/list.h" #include "internal/list.h"
@ -41,7 +40,7 @@ static TaskCb *g_curDeadTask = NULL; // TODO: Improve dead task handling.
static KRes scheduler(TaskState curTaskState); static KRes scheduler(TaskState curTaskState);
noreturn static void kernelIdleTask(void); [[noreturn]] static void kernelIdleTask(void);
static void initKernelState(void) static void initKernelState(void)
{ {
@ -66,7 +65,7 @@ void kernelInit(uint8_t priority)
initKernelState(); initKernelState();
TaskCb *const idleT = (TaskCb*)slabAlloc(&g_taskSlab); TaskCb *const idleT = (TaskCb*)slabAlloc(&g_taskSlab);
void *const iStack = malloc(IDLE_STACK_SIZE); u8 *const iStack = malloc(IDLE_STACK_SIZE);
TaskCb *const mainT = (TaskCb*)slabCalloc(&g_taskSlab, sizeof(TaskCb)); TaskCb *const mainT = (TaskCb*)slabCalloc(&g_taskSlab, sizeof(TaskCb));
if(idleT == NULL || iStack == NULL || mainT == NULL) if(idleT == NULL || iStack == NULL || mainT == NULL)
{ {
@ -88,7 +87,7 @@ void kernelInit(uint8_t priority)
mainT->prio = priority; mainT->prio = priority;
g_curTask = mainT; g_curTask = mainT;
g_readyBitmap = 1u<<1; // The idle task has priority 1 and is always ready. g_readyBitmap = BIT(1); // The idle task has priority 1 and is always ready.
listPush(&g_runQueues[1], &idleT->node); listPush(&g_runQueues[1], &idleT->node);
g_numTasks = 2; g_numTasks = 2;
} }
@ -102,7 +101,7 @@ KHandle createTask(size_t stackSize, uint8_t priority, TaskFunc entry, void *tas
SlabHeap *const taskSlabPtr = &g_taskSlab; SlabHeap *const taskSlabPtr = &g_taskSlab;
TaskCb *const newT = (TaskCb*)slabAlloc(taskSlabPtr); TaskCb *const newT = (TaskCb*)slabAlloc(taskSlabPtr);
void *const stack = malloc(stackSize); u8 *const stack = malloc(stackSize);
if(newT == NULL || stack == NULL) if(newT == NULL || stack == NULL)
{ {
slabFree(taskSlabPtr, newT); slabFree(taskSlabPtr, newT);
@ -111,7 +110,7 @@ KHandle createTask(size_t stackSize, uint8_t priority, TaskFunc entry, void *tas
} }
cpuRegs *const regs = (cpuRegs*)(stack + stackSize - sizeof(cpuRegs)); cpuRegs *const regs = (cpuRegs*)(stack + stackSize - sizeof(cpuRegs));
kmemset((u32*)regs, 0, sizeof(cpuRegs)); clear32((u32*)regs, 0, sizeof(cpuRegs));
regs->lr = (u32)entry; regs->lr = (u32)entry;
newT->prio = priority; newT->prio = priority;
newT->id = g_numTasks; // TODO: Make this more sophisticated. newT->id = g_numTasks; // TODO: Make this more sophisticated.
@ -123,7 +122,7 @@ KHandle createTask(size_t stackSize, uint8_t priority, TaskFunc entry, void *tas
kernelLock(); kernelLock();
listPush(&g_runQueues[priority], &newT->node); listPush(&g_runQueues[priority], &newT->node);
g_readyBitmap |= 1u<<priority; g_readyBitmap |= BIT(priority);
g_numTasks++; g_numTasks++;
kernelUnlock(); kernelUnlock();
@ -180,7 +179,7 @@ bool waitQueueWakeN(ListNode *waitQueue, u32 wakeCount, KRes res, bool reschedul
TaskCb *const curTask = g_curTask; TaskCb *const curTask = g_curTask;
const u8 curPrio = curTask->prio; const u8 curPrio = curTask->prio;
listPushTail(&runQueues[curPrio], &curTask->node); listPushTail(&runQueues[curPrio], &curTask->node);
readyBitmap = 1u<<curPrio; readyBitmap = BIT(curPrio);
} }
do do
@ -199,7 +198,7 @@ bool waitQueueWakeN(ListNode *waitQueue, u32 wakeCount, KRes res, bool reschedul
*/ */
//TaskCb *task = LIST_ENTRY(listPopHead(waitQueue), TaskCb, node); //TaskCb *task = LIST_ENTRY(listPopHead(waitQueue), TaskCb, node);
TaskCb *task = LIST_ENTRY(listPop(waitQueue), TaskCb, node); TaskCb *task = LIST_ENTRY(listPop(waitQueue), TaskCb, node);
readyBitmap |= 1u<<task->prio; readyBitmap |= BIT(task->prio);
task->res = res; task->res = res;
listPushTail(&runQueues[task->prio], &task->node); listPushTail(&runQueues[task->prio], &task->node);
} while(!listEmpty(waitQueue) && --wakeCount); } while(!listEmpty(waitQueue) && --wakeCount);
@ -240,7 +239,7 @@ static KRes scheduler(TaskState curTaskState)
} }
listPush(&runQueues[curPrio], &curTask->node); listPush(&runQueues[curPrio], &curTask->node);
readyBitmap |= 1u<<curPrio; readyBitmap |= BIT(curPrio);
} }
else if(UNLIKELY(curTaskState == TASK_STATE_DEAD)) else if(UNLIKELY(curTaskState == TASK_STATE_DEAD))
{ {
@ -249,7 +248,7 @@ static KRes scheduler(TaskState curTaskState)
} }
TaskCb *newTask = LIST_ENTRY(listPop(&runQueues[readyPrio]), TaskCb, node); TaskCb *newTask = LIST_ENTRY(listPop(&runQueues[readyPrio]), TaskCb, node);
if(listEmpty(&runQueues[readyPrio])) readyBitmap &= ~(1u<<readyPrio); if(listEmpty(&runQueues[readyPrio])) readyBitmap &= ~BIT(readyPrio);
g_readyBitmap = readyBitmap; g_readyBitmap = readyBitmap;
TaskCb *oldTask = curTask; TaskCb *oldTask = curTask;
@ -261,7 +260,7 @@ static KRes scheduler(TaskState curTaskState)
} }
// TODO: Cleanup deleted tasks in here? Or create a worker task? // TODO: Cleanup deleted tasks in here? Or create a worker task?
noreturn static void kernelIdleTask(void) [[noreturn]] static void kernelIdleTask(void)
{ {
do do
{ {
@ -269,4 +268,4 @@ noreturn static void kernelIdleTask(void)
kernelLock(); kernelLock();
scheduler(TASK_STATE_RUNNING); scheduler(TASK_STATE_RUNNING);
} while(1); } while(1);
} }

View File

@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include "types.h" #include "types.h"
#include "internal/list.h" #include "internal/list.h"

View File

@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include "types.h" #include "types.h"
#include "internal/list.h" #include "internal/list.h"

View File

@ -16,8 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/*#include <stdbool.h> /*#include <stdlib.h>
#include <stdlib.h>
#include "types.h" #include "types.h"
#include "ktimer.h" #include "ktimer.h"
#include "internal/list.h" #include "internal/list.h"

View File

@ -1,6 +1,6 @@
/* /*
* This file is part of open_agb_firm * This file is part of open_agb_firm
* Copyright (C) 2021 derrek, profi200 * Copyright (C) 2024 derrek, profi200
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -19,7 +19,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include "internal/slabheap.h" #include "internal/slabheap.h"
#include "internal/kmemcpy_set.h" #include "memory.h"
@ -29,7 +29,7 @@ void slabInit(SlabHeap *slab, size_t objSize, size_t num)
listInit(slab); listInit(slab);
void *pool = malloc(objSize * num); u8 *pool = malloc(objSize * num);
if(!pool) return; if(!pool) return;
do do
{ {
@ -48,7 +48,7 @@ void* slabAlloc(SlabHeap *slab)
void* slabCalloc(SlabHeap *slab, size_t clrSize) void* slabCalloc(SlabHeap *slab, size_t clrSize)
{ {
void *const ptr = slabAlloc(slab); void *const ptr = slabAlloc(slab);
if(ptr) kmemset(ptr, 0, clrSize); if(ptr) clear32(ptr, 0, clrSize);
return ptr; return ptr;
} }

View File

@ -58,9 +58,9 @@ static u32 printCardInfos(void)
int main(void) int main(void)
{ {
GFX_init(GFX_BGR8, GFX_RGB565); GFX_init(GFX_BGR8, GFX_BGR565, GFX_TOP_2D);
GFX_setBrightness(15, 15); GFX_setLcdLuminance(80);
consoleInit(SCREEN_BOT, NULL); consoleInit(GFX_LCD_BOT, NULL);
TIMER_sleepMs(1000); TIMER_sleepMs(1000);
TMIO_init(); TMIO_init();

View File

@ -11,9 +11,9 @@
int main(void) int main(void)
{ {
GFX_init(GFX_BGR8, GFX_RGB565); GFX_init(GFX_BGR8, GFX_BGR565, GFX_TOP_2D);
GFX_setBrightness(1, 32); GFX_setLcdLuminance(80);
consoleInit(SCREEN_BOT, NULL); consoleInit(GFX_LCD_BOT, NULL);
CSND_init(); CSND_init();
ee_puts("Sound test"); ee_puts("Sound test");