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).
*/
#define MAX_TASKS (3) // Including main and idle task.
#define MAX_EVENTS (10)
#define MAX_MUTEXES (3)
#define MAX_SEMAPHORES (0)
#define MAX_TASKS (4) // Including main and idle task.
#define MAX_EVENTS (16)
#define MAX_MUTEXES (8)
#define MAX_SEMAPHORES (2)
#define MAX_TIMERS (0)
#define IDLE_STACK_SIZE (0x1000) // Keep in mind this stack is used in interrupt contex! TODO: Change this.

View File

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

View File

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

View File

@ -22,6 +22,11 @@
#include "internal/list.h"
#ifdef __cplusplus
extern "C"
{
#endif
typedef ListNode SlabHeap;
@ -61,3 +66,7 @@ void* slabCalloc(SlabHeap *slab, size_t clrSize);
* @param ptr The object slot pointer.
*/
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)
{
u32 tmp;
@ -43,3 +48,7 @@ static inline void spinlockUnlock(u32 *lock)
"sev"
: : "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 UNLIKELY(expr) __builtin_expect((expr), false)
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -18,17 +18,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include "kernel.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Creates a new kernel event.
*

View File

@ -21,6 +21,7 @@
#include "kernel.h"
#ifdef __cplusplus
extern "C"
{
@ -28,8 +29,6 @@ extern "C"
// TODO: Implement this using semaphores?
/**
* @brief Creates a new kernel mutex.
*

View File

@ -19,17 +19,15 @@
*/
#include <stdint.h>
#include <stdbool.h>
#include "kernel.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Creates a new kernel semaphore.
*

View File

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

View File

@ -1,6 +1,6 @@
/*
* This file is part of open_agb_firm
* Copyright (C) 2021 derrek, profi200
* This file is part of libn3ds
* Copyright (C) 2024 derrek, profi200
*
* 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
@ -17,13 +17,12 @@
*/
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include "types.h"
#include "kernel.h"
#include "internal/config.h"
#include "internal/kernel_private.h"
#include "internal/kmemcpy_set.h"
#include "memory.h"
#include "internal/slabheap.h"
#include "internal/util.h"
#include "internal/list.h"
@ -41,7 +40,7 @@ static TaskCb *g_curDeadTask = NULL; // TODO: Improve dead task handling.
static KRes scheduler(TaskState curTaskState);
noreturn static void kernelIdleTask(void);
[[noreturn]] static void kernelIdleTask(void);
static void initKernelState(void)
{
@ -66,7 +65,7 @@ void kernelInit(uint8_t priority)
initKernelState();
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));
if(idleT == NULL || iStack == NULL || mainT == NULL)
{
@ -88,7 +87,7 @@ void kernelInit(uint8_t priority)
mainT->prio = priority;
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);
g_numTasks = 2;
}
@ -102,7 +101,7 @@ KHandle createTask(size_t stackSize, uint8_t priority, TaskFunc entry, void *tas
SlabHeap *const taskSlabPtr = &g_taskSlab;
TaskCb *const newT = (TaskCb*)slabAlloc(taskSlabPtr);
void *const stack = malloc(stackSize);
u8 *const stack = malloc(stackSize);
if(newT == NULL || stack == NULL)
{
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));
kmemset((u32*)regs, 0, sizeof(cpuRegs));
clear32((u32*)regs, 0, sizeof(cpuRegs));
regs->lr = (u32)entry;
newT->prio = priority;
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();
listPush(&g_runQueues[priority], &newT->node);
g_readyBitmap |= 1u<<priority;
g_readyBitmap |= BIT(priority);
g_numTasks++;
kernelUnlock();
@ -180,7 +179,7 @@ bool waitQueueWakeN(ListNode *waitQueue, u32 wakeCount, KRes res, bool reschedul
TaskCb *const curTask = g_curTask;
const u8 curPrio = curTask->prio;
listPushTail(&runQueues[curPrio], &curTask->node);
readyBitmap = 1u<<curPrio;
readyBitmap = BIT(curPrio);
}
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(listPop(waitQueue), TaskCb, node);
readyBitmap |= 1u<<task->prio;
readyBitmap |= BIT(task->prio);
task->res = res;
listPushTail(&runQueues[task->prio], &task->node);
} while(!listEmpty(waitQueue) && --wakeCount);
@ -240,7 +239,7 @@ static KRes scheduler(TaskState curTaskState)
}
listPush(&runQueues[curPrio], &curTask->node);
readyBitmap |= 1u<<curPrio;
readyBitmap |= BIT(curPrio);
}
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);
if(listEmpty(&runQueues[readyPrio])) readyBitmap &= ~(1u<<readyPrio);
if(listEmpty(&runQueues[readyPrio])) readyBitmap &= ~BIT(readyPrio);
g_readyBitmap = readyBitmap;
TaskCb *oldTask = curTask;
@ -261,7 +260,7 @@ static KRes scheduler(TaskState curTaskState)
}
// TODO: Cleanup deleted tasks in here? Or create a worker task?
noreturn static void kernelIdleTask(void)
[[noreturn]] static void kernelIdleTask(void)
{
do
{

View File

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

View File

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

View File

@ -16,8 +16,7 @@
* 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 "ktimer.h"
#include "internal/list.h"

View File

@ -1,6 +1,6 @@
/*
* 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
* it under the terms of the GNU General Public License as published by
@ -19,7 +19,7 @@
#include <stddef.h>
#include <stdlib.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);
void *pool = malloc(objSize * num);
u8 *pool = malloc(objSize * num);
if(!pool) return;
do
{
@ -48,7 +48,7 @@ void* slabAlloc(SlabHeap *slab)
void* slabCalloc(SlabHeap *slab, size_t clrSize)
{
void *const ptr = slabAlloc(slab);
if(ptr) kmemset(ptr, 0, clrSize);
if(ptr) clear32(ptr, 0, clrSize);
return ptr;
}

View File

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

View File

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