Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
libseven: add assert API
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarLambda committed Nov 12, 2022
1 parent 63605a4 commit d7585c3
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ make_dist() {
sha256sum -b $PROJECT-$VERSION.zip > $PROJECT-$VERSION.zip.sha256sum
}

(PROJECT=libseven VERSION=0.9.0 make_dist)
(PROJECT=libseven VERSION=0.9.1 make_dist)
(PROJECT=minrt VERSION=0.2.0 make_dist)
2 changes: 2 additions & 0 deletions examples/vsync-callback/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

void vblank_callback(u16 irqs)
{
(void)irqs;

BG_PALETTE[0] += 1;
}

Expand Down
1 change: 1 addition & 0 deletions libseven/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_library(seven STATIC
src/hw/sram.s
src/hw/svc.s
src/hw/timer.s
src/util/assert.c
src/util/bit.s
src/util/debug.s
src/util/log.c
Expand Down
1 change: 1 addition & 0 deletions libseven/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ SOURCES = \
src/hw/sram.s \
src/hw/svc.s \
src/hw/timer.s \
src/util/assert.c \
src/util/bit.s \
src/util/debug.s \
src/util/log.c \
Expand Down
2 changes: 1 addition & 1 deletion libseven/include/seven/base/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#define LIBSEVEN_VERSION_MAJOR 0
#define LIBSEVEN_VERSION_MINOR 9
#define LIBSEVEN_VERSION_PATCH 0
#define LIBSEVEN_VERSION_PATCH 1

#define LIBSEVEN_VERSION \
_LIBSEVEN_STR2(LIBSEVEN_VERSION_MAJOR) "." \
Expand Down
71 changes: 71 additions & 0 deletions libseven/include/seven/util/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef _LIBSEVEN_UTIL_ASSERT_H
#define _LIBSEVEN_UTIL_ASSERT_H

#include <seven/base.h>

_LIBSEVEN_EXTERN_C

#ifdef NDEBUG

#define assertCheck(condition) ((void)0)
#define assertCheckWithFilename(condition, filename) ((void)0)
#define assertCheckWithLocation(condition, function, filename, line) ((void)0)

#else

#ifndef _LIBSEVEN_ASSERT_FUNC
#ifdef __GNUC__
#define _LIBSEVEN_ASSERT_FUNC __PRETTY_FUNCTION__
#elif __STDC_VERSION >= 199901L
#define _LIBSEVEN_ASSERT_FUNC __func__
#elif __GNUC__ >= 2
#define _LIBSEVEN_ASSERT_FUNC __FUNCTION__
#else
#define _LIBSEVEN_ASSERT_FUNC ((char*)0)
#endif
#endif

#ifndef _LIBSEVEN_ASSERT_FILE
#ifdef __FILE_NAME__
#define _LIBSEVEN_ASSERT_FILE __FILE_NAME__
#else
#define _LIBSEVEN_ASSERT_FILE __FILE__
#endif
#endif

#define assertCheck(condition) \
assertCheckWithFilename(condition, _LIBSEVEN_ASSERT_FILE)

#define assertCheckWithFilename(condition, filename) \
assertCheckWithLocation(condition, _LIBSEVEN_ASSERT_FUNC, filename, __LINE__)

#define assertCheckWithLocation(condition, function, filename, line) \
do { \
if (!(condition)) { \
assertRaise(#condition, function, filename, line); \
} \
} while(0)

#endif /* !NDEBUG */

typedef void AssertHandlerFn(const char*, const char*, const char*, u32);

extern AssertHandlerFn* assertSetHandler(AssertHandlerFn *handler);

extern AssertHandlerFn* assertGetHandler(void);

extern void NORETURN assertRaise(
const char *message,
const char *function,
const char *file,
u32 line);

_LIBSEVEN_EXTERN_C_END

#endif /* !_LIBSEVEN_UTIL_ASSERT_H */
3 changes: 2 additions & 1 deletion libseven/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = '0.9.0'
version = '0.9.1'

sources = [
'src/hw/dma.s',
Expand All @@ -7,6 +7,7 @@ sources = [
'src/hw/sram.s',
'src/hw/svc.s',
'src/hw/timer.s',
'src/util/assert.c',
'src/util/bit.s',
'src/util/debug.s',
'src/util/log.c',
Expand Down
41 changes: 41 additions & 0 deletions libseven/src/util/assert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdlib.h>

#include <seven/hw/irq.h>
#include <seven/util/assert.h>

static struct {
AssertHandlerFn *handler;
u32 panic;
} ASSERT;

extern AssertHandlerFn* assertSetHandler(AssertHandlerFn *handler)
{
AssertHandlerFn *previous = ASSERT.handler;
ASSERT.handler = handler;
return previous;
}

extern AssertHandlerFn* assertGetHandler(void)
{
return ASSERT.handler;
}

extern void NORETURN assertRaise(
const char *message,
const char *function,
const char *file,
u32 line)
{
REG_IME = 0;

AssertHandlerFn *h = ASSERT.handler;

// Prevent a nested assert
if (h && !ASSERT.panic)
{
ASSERT.panic = 1;
h(message, function, file, line);
}

_Exit(EXIT_FAILURE);
}
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('sdk-seven', 'c',
version: '0.5.0',
version: '0.5.1',
license: 'Zlib',
meson_version: '>=0.60.0',
default_options: ['warning_level=2', 'c_std=c99'])
Expand Down

0 comments on commit d7585c3

Please sign in to comment.