From 779d5a0c1994502ad5c278216a6edf6d659a6e66 Mon Sep 17 00:00:00 2001 From: IonutParau Date: Wed, 9 Jul 2025 18:07:27 +0200 Subject: [PATCH] big progress on baremetal --- src/component.c | 6 +- src/component.h | 2 +- src/components/drive.c | 1 - src/components/filesystem.c | 19 +++---- src/components/gpu.c | 18 +++--- src/components/screen.c | 20 +++---- src/components/screen.h | 8 +-- src/computer.c | 8 +-- src/computer.h | 2 +- src/emulator.c | 2 +- src/lock.c | 16 +++--- src/neonucleus.h | 109 +++++++++++++++++++++++------------- src/unicode.c | 9 ++- src/universe.c | 3 +- src/utils.c | 43 +++++++++++--- src/value.c | 11 ++-- 16 files changed, 166 insertions(+), 111 deletions(-) diff --git a/src/component.c b/src/component.c index 91bc671..ca16b6b 100644 --- a/src/component.c +++ b/src/component.c @@ -30,7 +30,7 @@ void nn_destroyComponentTable(nn_componentTable *table) { nn_dealloc(&alloc, table, sizeof(nn_componentTable)); } -void nn_defineMethod(nn_componentTable *table, const char *methodName, bool direct, nn_componentMethod *methodFunc, void *methodUserdata, const char *methodDoc) { +void nn_defineMethod(nn_componentTable *table, const char *methodName, nn_bool_t direct, nn_componentMethod *methodFunc, void *methodUserdata, const char *methodDoc) { if(table->methodCount == NN_MAX_METHODS) return; nn_method method; method.method = methodFunc; @@ -47,7 +47,7 @@ void nn_defineMethod(nn_componentTable *table, const char *methodName, bool dire table->methodCount++; } -const char *nn_getTableMethod(nn_componentTable *table, size_t idx, bool *outDirect) { +const char *nn_getTableMethod(nn_componentTable *table, size_t idx, nn_bool_t *outDirect) { if(idx >= table->methodCount) return NULL; nn_method method = table->methods[idx]; if(outDirect != NULL) *outDirect = method.direct; @@ -87,7 +87,7 @@ void *nn_getComponentUserdata(nn_component *component) { return component->statePtr; } -bool nn_invokeComponentMethod(nn_component *component, const char *name) { +nn_bool_t nn_invokeComponentMethod(nn_component *component, const char *name) { nn_componentTable *table = component->table; for(size_t i = 0; i < table->methodCount; i++) { nn_method method = table->methods[i]; diff --git a/src/component.h b/src/component.h index d78c18c..815f213 100644 --- a/src/component.h +++ b/src/component.h @@ -9,7 +9,7 @@ typedef struct nn_method { nn_componentMethod *method; void *userdata; char *doc; - bool direct; + nn_bool_t direct; } nn_method; typedef struct nn_componentTable { diff --git a/src/components/drive.c b/src/components/drive.c index 5beed0b..c500540 100644 --- a/src/components/drive.c +++ b/src/components/drive.c @@ -1,5 +1,4 @@ #include "../neonucleus.h" -#include void nn_drive_destroy(void *_, nn_component *component, nn_drive *drive) { if(!nn_decRef(&drive->refc)) return; diff --git a/src/components/filesystem.c b/src/components/filesystem.c index 9e1afca..cbb5f31 100644 --- a/src/components/filesystem.c +++ b/src/components/filesystem.c @@ -1,5 +1,4 @@ #include "../neonucleus.h" -#include void nn_fs_destroy(void *_, nn_component *component, nn_filesystem *fs) { if(!nn_decRef(&fs->refc)) return; @@ -9,12 +8,12 @@ void nn_fs_destroy(void *_, nn_component *component, nn_filesystem *fs) { } } -bool nn_fs_illegalPath(const char *path) { +nn_bool_t nn_fs_illegalPath(const char *path) { // absolute disaster const char *illegal = "\"\\:*?<>|"; for(size_t i = 0; illegal[i] != '\0'; i++) { - if(strchr(path, illegal[i]) != NULL) return true; + if(nn_strchr(path, illegal[i]) != NULL) return true; } return false; } @@ -271,7 +270,7 @@ void nn_fs_list(nn_filesystem *fs, void *_, nn_component *component, nn_computer // operation succeeded nn_value arr = nn_values_array(alloc, fileCount); for(size_t i = 0; i < fileCount; i++) { - nn_values_set(arr, i, nn_values_string(alloc, files[i], strlen(files[i]))); + nn_values_set(arr, i, nn_values_string(alloc, files[i], nn_strlen(files[i]))); nn_deallocStr(alloc, files[i]); } nn_dealloc(alloc, files, sizeof(char *) * fileCount); @@ -310,7 +309,7 @@ void nn_fs_close(nn_filesystem *fs, void *_, nn_component *component, nn_compute nn_value fdValue = nn_getArgument(computer, 0); size_t fd = nn_toInt(fdValue); - bool closed = fs->close(component, fs->userdata, fd); + nn_bool_t closed = fs->close(component, fs->userdata, fd); nn_return(computer, nn_values_boolean(closed)); // do not ask where it comes from, balance is hard @@ -331,7 +330,7 @@ void nn_fs_write(nn_filesystem *fs, void *_, nn_component *component, nn_compute return; } - bool closed = fs->write(component, fs->userdata, fd, buf, len); + nn_bool_t closed = fs->write(component, fs->userdata, fd, buf, len); nn_return(computer, nn_values_boolean(closed)); // do not ask where it comes from, balance is hard @@ -368,11 +367,11 @@ void nn_fs_read(nn_filesystem *fs, void *_, nn_component *component, nn_computer nn_fs_seekCost(fs, nn_fs_countChunks(fs, readLen, component), component, computer); } -bool nn_fs_validWhence(const char *s) { +nn_bool_t nn_fs_validWhence(const char *s) { return - strcmp(s, "set") == 0 || - strcmp(s, "cur") == 0 || - strcmp(s, "end") == 0; + nn_strcmp(s, "set") == 0 || + nn_strcmp(s, "cur") == 0 || + nn_strcmp(s, "end") == 0; } void nn_fs_seek(nn_filesystem *fs, void *_, nn_component *component, nn_computer *computer) { diff --git a/src/components/gpu.c b/src/components/gpu.c index b815a47..e9c0c73 100644 --- a/src/components/gpu.c +++ b/src/components/gpu.c @@ -8,12 +8,12 @@ typedef struct nni_gpu { nn_gpuControl ctrl; int currentFg; int currentBg; - bool isFgPalette; - bool isBgPalette; + nn_bool_t isFgPalette; + nn_bool_t isBgPalette; // TODO: think about buffers and stuff } nni_gpu; -bool nni_samePixel(nn_scrchr_t a, nn_scrchr_t b) { +nn_bool_t nni_samePixel(nn_scrchr_t a, nn_scrchr_t b) { return a.codepoint == b.codepoint && a.fg == b.fg && @@ -23,7 +23,7 @@ bool nni_samePixel(nn_scrchr_t a, nn_scrchr_t b) { ; } -bool nni_inBounds(nni_gpu *gpu, int x, int y) { +nn_bool_t nni_inBounds(nni_gpu *gpu, int x, int y) { if(gpu->currentScreen == NULL) return false; return x >= 0 && @@ -77,7 +77,7 @@ void nni_gpu_bind(nni_gpu *gpu, void *_, nn_component *component, nn_computer *c nn_setCError(computer, "bad argument #1 (address expected)"); return; } - bool reset = false; + nn_bool_t reset = false; if(resetVal.tag == NN_VALUE_BOOL) reset = nn_toBoolean(resetVal); nn_component *c = nn_findComponent(computer, (nn_address)addr); @@ -124,7 +124,7 @@ void nni_gpu_set(nni_gpu *gpu, void *_, nn_component *component, nn_computer *co int x = nn_toInt(nn_getArgument(computer, 0)) - 1; int y = nn_toInt(nn_getArgument(computer, 1)) - 1; const char *s = nn_toCString(nn_getArgument(computer, 2)); - bool isVertical = nn_toBoolean(nn_getArgument(computer, 3)); + nn_bool_t isVertical = nn_toBoolean(nn_getArgument(computer, 3)); if(s == NULL) { nn_setCError(computer, "bad argument #3 (string expected in set)"); @@ -201,7 +201,7 @@ void nni_gpu_setResolution(nni_gpu *gpu, void *_, nn_component *component, nn_co int w = nn_toInt(nn_getArgument(computer, 0)); int h = nn_toInt(nn_getArgument(computer, 1)); - bool changed = w != lw || h != lh; + nn_bool_t changed = w != lw || h != lh; if(w <= 0) w = 1; if(h <= 0) h = 1; @@ -224,7 +224,7 @@ void nni_gpu_setResolution(nni_gpu *gpu, void *_, nn_component *component, nn_co void nni_gpu_setBackground(nni_gpu *gpu, void *_, nn_component *component, nn_computer *computer) { if(gpu->currentScreen == NULL) return; int color = nn_toInt(nn_getArgument(computer, 0)); - bool isPalette = nn_toBoolean(nn_getArgument(computer, 1)); + nn_bool_t isPalette = nn_toBoolean(nn_getArgument(computer, 1)); if(isPalette && (color < 0 || color >= gpu->currentScreen->paletteColors)) { nn_setCError(computer, "invalid palette index"); @@ -256,7 +256,7 @@ void nni_gpu_getBackground(nni_gpu *gpu, void *_, nn_component *component, nn_co void nni_gpu_setForeground(nni_gpu *gpu, void *_, nn_component *component, nn_computer *computer) { if(gpu->currentScreen == NULL) return; int color = nn_toInt(nn_getArgument(computer, 0)); - bool isPalette = nn_toBoolean(nn_getArgument(computer, 1)); + nn_bool_t isPalette = nn_toBoolean(nn_getArgument(computer, 1)); if(isPalette && (color < 0 || color >= gpu->currentScreen->paletteColors)) { nn_setCError(computer, "invalid palette index"); diff --git a/src/components/screen.c b/src/components/screen.c index 72e28ac..40a95bb 100644 --- a/src/components/screen.c +++ b/src/components/screen.c @@ -173,35 +173,35 @@ nn_scrchr_t nn_getPixel(nn_screen *screen, int x, int y) { return screen->buffer[x + y * screen->maxWidth]; } -bool nn_isDirty(nn_screen *screen) { +nn_bool_t nn_isDirty(nn_screen *screen) { return screen->isDirty; } -void nn_setDirty(nn_screen *screen, bool dirty) { +void nn_setDirty(nn_screen *screen, nn_bool_t dirty) { screen->isDirty = dirty; } -bool nn_isPrecise(nn_screen *screen) { +nn_bool_t nn_isPrecise(nn_screen *screen) { return screen->isPrecise; } -void nn_setPrecise(nn_screen *screen, bool precise) { +void nn_setPrecise(nn_screen *screen, nn_bool_t precise) { screen->isPrecise = precise; } -bool nn_isTouchModeInverted(nn_screen *screen) { +nn_bool_t nn_isTouchModeInverted(nn_screen *screen) { return screen->isTouchModeInverted; } -void nn_setTouchModeInverted(nn_screen *screen, bool touchModeInverted) { +void nn_setTouchModeInverted(nn_screen *screen, nn_bool_t touchModeInverted) { screen->isTouchModeInverted = touchModeInverted; } -bool nn_isOn(nn_screen *buffer) { +nn_bool_t nn_isOn(nn_screen *buffer) { return buffer->isOn; } -void nn_setOn(nn_screen *buffer, bool on) { +void nn_setOn(nn_screen *buffer, nn_bool_t on) { buffer->isOn = on; } @@ -310,9 +310,9 @@ void nn_getStd8BitPalette(int color[256]) { } static int nni_4bit_colors[16]; -static bool nni_4bit_did = false; +static nn_bool_t nni_4bit_did = false; static int nni_8bit_colors[256]; -static bool nni_8bit_did = false; +static nn_bool_t nni_8bit_did = false; int nn_mapDepth(int color, int depth) { if(depth == 1) { diff --git a/src/components/screen.h b/src/components/screen.h index bf5ae5e..f569b89 100644 --- a/src/components/screen.h +++ b/src/components/screen.h @@ -21,10 +21,10 @@ typedef struct nn_screen { int *palette; int aspectRatioWidth; int aspectRatioHeight; - bool isOn; - bool isTouchModeInverted; - bool isPrecise; - bool isDirty; + nn_bool_t isOn; + nn_bool_t isTouchModeInverted; + nn_bool_t isPrecise; + nn_bool_t isDirty; nn_address keyboards[NN_MAX_SCREEN_KEYBOARDS]; size_t keyboardCount; } nn_screen; diff --git a/src/computer.c b/src/computer.c index 0dca095..ea6fac4 100644 --- a/src/computer.c +++ b/src/computer.c @@ -211,7 +211,7 @@ const char *nn_indexUser(nn_computer *computer, size_t idx) { return computer->users[idx]; } -bool nn_isUser(nn_computer *computer, const char *name) { +nn_bool_t nn_isUser(nn_computer *computer, const char *name) { if(computer->userCount == 0) return true; for(size_t i = 0; i < computer->userCount; i++) { if(nn_strcmp(computer->users[i], name) == 0) return true; @@ -236,7 +236,7 @@ double nn_getCallCost(nn_computer *computer) { return computer->callCost; } -bool nn_isOverworked(nn_computer *computer) { +nn_bool_t nn_isOverworked(nn_computer *computer) { return computer->state == NN_STATE_OVERWORKED; } @@ -319,7 +319,7 @@ void nn_removeHeat(nn_computer *computer, double heat) { if(computer->temperature < computer->roomTemperature) computer->temperature = computer->roomTemperature; } -bool nn_isOverheating(nn_computer *computer) { +nn_bool_t nn_isOverheating(nn_computer *computer) { return computer->temperature > NN_OVERHEAT_MIN; } @@ -486,7 +486,7 @@ void nn_return_number(nn_computer *computer, double number) { nn_return(computer, nn_values_number(number)); } -void nn_return_boolean(nn_computer *computer, bool boolean) { +void nn_return_boolean(nn_computer *computer, nn_bool_t boolean) { nn_return(computer, nn_values_boolean(boolean)); } diff --git a/src/computer.h b/src/computer.h index 6718cd0..b7af071 100644 --- a/src/computer.h +++ b/src/computer.h @@ -10,7 +10,7 @@ typedef struct nn_signal { typedef struct nn_computer { char state; - bool allocatedError; + nn_bool_t allocatedError; char *err; void *userdata; nn_guard *lock; diff --git a/src/emulator.c b/src/emulator.c index bd39460..86616eb 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -76,7 +76,7 @@ int ne_eeprom_getData(nn_component *component, void *_, char *buf) { void ne_eeprom_setData(nn_component *component, void *_, const char *buf, size_t len) {} -bool ne_eeprom_isReadonly(nn_component *component, void *userdata) { +nn_bool_t ne_eeprom_isReadonly(nn_component *component, void *userdata) { return false; } diff --git a/src/lock.c b/src/lock.c index 7b336a7..8777507 100644 --- a/src/lock.c +++ b/src/lock.c @@ -3,7 +3,7 @@ #ifndef NN_BAREMETAL #include "tinycthread.h" -static bool nni_libcLock(void *_, mtx_t *lock, int action, int flags) { +static nn_bool_t nni_libcLock(void *_, mtx_t *lock, int action, int flags) { if(action == NN_LOCK_INIT) { mtx_init(lock, mtx_recursive); } else if(action == NN_LOCK_DEINIT) { @@ -16,7 +16,7 @@ static bool nni_libcLock(void *_, mtx_t *lock, int action, int flags) { } else if(action == NN_LOCK_RELEASE) { mtx_unlock(lock); } - return true; + return NN_TRUE; } nn_LockManager nn_libcMutex() { @@ -29,8 +29,8 @@ nn_LockManager nn_libcMutex() { #endif -static bool nni_noLock(void *_, void *__, int action, int flags) { - return true; +static nn_bool_t nni_noLock(void *_, void *__, int action, int flags) { + return NN_TRUE; } nn_LockManager nn_noMutex() { @@ -53,8 +53,8 @@ void nn_lock(nn_Context *context, nn_guard *guard) { context->lockManager.proc(context->lockManager.userdata, guard, NN_LOCK_RETAIN, NN_LOCK_DEFAULT); } -bool nn_tryLock(nn_Context *context, nn_guard *guard) { - if(guard == NULL) return true; +nn_bool_t nn_tryLock(nn_Context *context, nn_guard *guard) { + if(guard == NULL) return NN_TRUE; return context->lockManager.proc(context->lockManager.userdata, guard, NN_LOCK_RETAIN, NN_LOCK_IMMEDIATE); } @@ -77,10 +77,10 @@ void nn_incRef(nn_refc *refc) { nn_addRef(refc, 1); } -bool nn_removeRef(nn_refc *refc, size_t count) { +nn_bool_t nn_removeRef(nn_refc *refc, size_t count) { return ((*refc) -= count) == 0; } -bool nn_decRef(nn_refc *refc) { +nn_bool_t nn_decRef(nn_refc *refc) { return nn_removeRef(refc, 1); } diff --git a/src/neonucleus.h b/src/neonucleus.h index 53d0d84..36e04f6 100644 --- a/src/neonucleus.h +++ b/src/neonucleus.h @@ -3,7 +3,37 @@ #include #include + +#ifndef NN_NEONUCLEUS #include +#endif + +#ifdef bool +typedef bool nn_bool_t; +#else +typedef unsigned char nn_bool_t; +#define bool nn_bool_t +#endif + +#ifdef true + +#define NN_TRUE true + +#else + +#define NN_TRUE 1 +#define true NN_TRUE + +#endif + +#ifdef false +#define NN_FALSE false +#else + +#define NN_FALSE 0 +#define false NN_FALSE + +#endif #ifdef __cplusplus extern "C" { @@ -113,7 +143,7 @@ typedef struct nn_Alloc { #define NN_LOCK_RETAIN 2 #define NN_LOCK_RELEASE 3 -typedef bool nn_LockProc(void *userdata, void *lock, int action, int flags); +typedef nn_bool_t nn_LockProc(void *userdata, void *lock, int action, int flags); typedef struct nn_LockManager { void *userdata; @@ -152,6 +182,9 @@ typedef struct nn_Context { // libc-like utils void nn_memset(void *buf, unsigned char byte, size_t len); +void nn_memcpy(void *dest, const void *src, size_t len); +char *nn_strcpy(char *dest, const char *src); +const char *nn_strchr(const char *str, int ch); int nn_strcmp(const char *a, const char *b); size_t nn_strlen(const char *a); @@ -202,7 +235,7 @@ typedef struct nn_value { union { intptr_t integer; double number; - bool boolean; + nn_bool_t boolean; const char *cstring; nn_string *string; nn_array *array; @@ -227,20 +260,20 @@ void nn_deallocStr(nn_Alloc *alloc, char *s); nn_guard *nn_newGuard(nn_Context *context); void nn_lock(nn_Context *context, nn_guard *guard); -bool nn_tryLock(nn_Context *context, nn_guard *guard); +nn_bool_t nn_tryLock(nn_Context *context, nn_guard *guard); void nn_unlock(nn_Context *context, nn_guard *guard); void nn_deleteGuard(nn_Context *context, nn_guard *guard); void nn_addRef(nn_refc *refc, size_t count); void nn_incRef(nn_refc *refc); /* Returns true if the object should be freed */ -bool nn_removeRef(nn_refc *refc, size_t count); +nn_bool_t nn_removeRef(nn_refc *refc, size_t count); /* Returns true if the object should be freed */ -bool nn_decRef(nn_refc *refc); +nn_bool_t nn_decRef(nn_refc *refc); // Unicode (more specifically, UTF-8) stuff -bool nn_unicode_validate(const char *s); +nn_bool_t nn_unicode_validate(const char *s); // returned string must be nn_deallocStr()'d char *nn_unicode_char(nn_Alloc *alloc, unsigned int *codepoints, size_t codepointCount); // returned array must be nn_dealloc()'d @@ -283,14 +316,14 @@ char *nn_data_aes_decrypt(nn_Alloc *alloc, const char *buf, size_t *len, const c // ECDH // if longKeys is on, instead of taking 32 bytes, the keys take up 48 bytes. -size_t nn_data_ecdh_keylen(bool longKeys); +size_t nn_data_ecdh_keylen(nn_bool_t longKeys); // use nn_data_ecdh_keylen to figure out the expected length for the buffers -void nn_data_ecdh_generateKeyPair(nn_Context *context, bool longKeys, char *publicKey, char *privateKey); +void nn_data_ecdh_generateKeyPair(nn_Context *context, nn_bool_t longKeys, char *publicKey, char *privateKey); -bool nn_data_ecdsa_check(bool longKeys, const char *buf, size_t buflen, const char *sig, size_t siglen); -char *nn_data_ecdsa_sign(nn_Alloc *alloc, const char *buf, size_t *buflen, const char *key, bool longKeys); +nn_bool_t nn_data_ecdsa_check(nn_bool_t longKeys, const char *buf, size_t buflen, const char *sig, size_t siglen); +char *nn_data_ecdsa_sign(nn_Alloc *alloc, const char *buf, size_t *buflen, const char *key, nn_bool_t longKeys); -char *nn_data_ecdh_getSharedKey(nn_Alloc *alloc, size_t *len, const char *privateKey, const char *publicKey, bool longKeys); +char *nn_data_ecdh_getSharedKey(nn_Alloc *alloc, size_t *len, const char *privateKey, const char *publicKey, nn_bool_t longKeys); // ECC char *nn_data_hamming_encode(nn_Alloc *alloc, const char *buf, size_t *len); @@ -329,12 +362,12 @@ void nn_popSignal(nn_computer *computer); const char *nn_addUser(nn_computer *computer, const char *name); void nn_deleteUser(nn_computer *computer, const char *name); const char *nn_indexUser(nn_computer *computer, size_t idx); -bool nn_isUser(nn_computer *computer, const char *name); +nn_bool_t nn_isUser(nn_computer *computer, const char *name); void nn_setCallBudget(nn_computer *computer, double callBudget); double nn_getCallBudget(nn_computer *computer); void nn_callCost(nn_computer *computer, double cost); double nn_getCallCost(nn_computer *computer); -bool nn_isOverworked(nn_computer *computer); +nn_bool_t nn_isOverworked(nn_computer *computer); void nn_triggerIndirect(nn_computer *computer); /* The memory returned can be freed with nn_free() */ @@ -394,7 +427,7 @@ void nn_setRoomTemperature(nn_computer *computer, double roomTemperature); void nn_addHeat(nn_computer *computer, double heat); void nn_removeHeat(nn_computer *computer, double heat); /* Checks against NN_OVERHEAT_MIN */ -bool nn_isOverheating(nn_computer *computer); +nn_bool_t nn_isOverheating(nn_computer *computer); // NULL if there is no error. const char *nn_getError(nn_computer *computer); @@ -437,14 +470,14 @@ typedef void nn_componentMethod(void *componentUserdata, void *methodUserdata, n nn_componentTable *nn_newComponentTable(nn_Alloc *alloc, const char *typeName, void *userdata, nn_componentConstructor *constructor, nn_componentDestructor *destructor); void nn_destroyComponentTable(nn_componentTable *table); -void nn_defineMethod(nn_componentTable *table, const char *methodName, bool direct, nn_componentMethod *methodFunc, void *methodUserdata, const char *methodDoc); -const char *nn_getTableMethod(nn_componentTable *table, size_t idx, bool *outDirect); +void nn_defineMethod(nn_componentTable *table, const char *methodName, nn_bool_t direct, nn_componentMethod *methodFunc, void *methodUserdata, const char *methodDoc); +const char *nn_getTableMethod(nn_componentTable *table, size_t idx, nn_bool_t *outDirect); const char *nn_methodDoc(nn_componentTable *table, const char *methodName); // Component calling /* Returns false if the method does not exist */ -bool nn_invokeComponentMethod(nn_component *component, const char *name); +nn_bool_t nn_invokeComponentMethod(nn_component *component, const char *name); void nn_simulateBufferedIndirect(nn_component *component, double amount, double amountPerTick); void nn_resetCall(nn_computer *computer); void nn_addArgument(nn_computer *computer, nn_value arg); @@ -459,7 +492,7 @@ size_t nn_getReturnCount(nn_computer *computer); nn_value nn_values_nil(); nn_value nn_values_integer(intptr_t integer); nn_value nn_values_number(double num); -nn_value nn_values_boolean(bool boolean); +nn_value nn_values_boolean(nn_bool_t boolean); nn_value nn_values_cstring(const char *string); nn_value nn_values_string(nn_Alloc *alloc, const char *string, size_t len); nn_value nn_values_array(nn_Alloc *alloc, size_t len); @@ -468,7 +501,7 @@ nn_value nn_values_table(nn_Alloc *alloc, size_t pairCount); void nn_return_nil(nn_computer *computer); void nn_return_integer(nn_computer *computer, intptr_t integer); void nn_return_number(nn_computer *computer, double number); -void nn_return_boolean(nn_computer *computer, bool boolean); +void nn_return_boolean(nn_computer *computer, nn_bool_t boolean); void nn_return_cstring(nn_computer *computer, const char *cstr); void nn_return_string(nn_computer *computer, const char *str, size_t len); nn_value nn_return_array(nn_computer *computer, size_t len); @@ -486,7 +519,7 @@ nn_pair nn_values_getPair(nn_value obj, size_t idx); intptr_t nn_toInt(nn_value val); double nn_toNumber(nn_value val); -bool nn_toBoolean(nn_value val); +nn_bool_t nn_toBoolean(nn_value val); const char *nn_toCString(nn_value val); const char *nn_toString(nn_value val, size_t *len); @@ -541,7 +574,7 @@ typedef struct nn_eeprom { void (*set)(nn_component *component, void *userdata, const char *buf, size_t len); int (*getData)(nn_component *component, void *userdata, char *buf); void (*setData)(nn_component *component, void *userdata, const char *buf, size_t len); - bool (*isReadonly)(nn_component *component, void *userdata); + nn_bool_t (*isReadonly)(nn_component *component, void *userdata); void (*makeReadonly)(nn_component *component, void *userdata); } nn_eeprom; nn_component *nn_addEeprom(nn_computer *computer, nn_address address, int slot, nn_eeprom *eeprom); @@ -589,18 +622,18 @@ typedef struct nn_filesystem { size_t (*spaceUsed)(nn_component *component, void *userdata); size_t (*spaceTotal)(nn_component *component, void *userdata); - bool (*isReadOnly)(nn_component *component, void *userdata); + nn_bool_t (*isReadOnly)(nn_component *component, void *userdata); // general operations size_t (*size)(nn_component *component, void *userdata, const char *path); - bool (*remove)(nn_component *component, void *userdata, const char *path); + nn_bool_t (*remove)(nn_component *component, void *userdata, const char *path); size_t (*lastModified)(nn_component *component, void *userdata, const char *path); size_t (*rename)(nn_component *component, void *userdata, const char *from, const char *to); - bool (*exists)(nn_component *component, void *userdata, const char *path); + nn_bool_t (*exists)(nn_component *component, void *userdata, const char *path); // directory operations - bool (*isDirectory)(nn_component *component, void *userdata, const char *path); - bool (*makeDirectory)(nn_component *component, void *userdata, const char *path); + nn_bool_t (*isDirectory)(nn_component *component, void *userdata, const char *path); + nn_bool_t (*makeDirectory)(nn_component *component, void *userdata, const char *path); // The returned array should be allocated with the supplied allocator. // The strings should be null terminated. Use nn_strdup for the allocation to guarantee nn_deallocStr deallocates it correctly. // For the array, the *exact* size of the allocation should be sizeof(char *) * (*len), @@ -611,8 +644,8 @@ typedef struct nn_filesystem { // file operations size_t (*open)(nn_component *component, void *userdata, const char *path, const char *mode); - bool (*close)(nn_component *component, void *userdata, int fd); - bool (*write)(nn_component *component, void *userdata, int fd, const char *buf, size_t len); + nn_bool_t (*close)(nn_component *component, void *userdata, int fd); + nn_bool_t (*write)(nn_component *component, void *userdata, int fd, const char *buf, size_t len); size_t (*read)(nn_component *component, void *userdata, int fd, char *buf, size_t required); // moved is an out pointer that says how many bytes the pointer moved. size_t (*seek)(nn_component *component, void *userdata, int fd, const char *whence, int off, int *moved); @@ -678,8 +711,8 @@ typedef struct nn_scrchr_t { unsigned int codepoint; int fg; int bg; - bool isFgPalette; - bool isBgPalette; + nn_bool_t isFgPalette; + nn_bool_t isBgPalette; } nn_scrchr_t; nn_screen *nn_newScreen(nn_Context *context, int maxWidth, int maxHeight, int maxDepth, int editableColors, int paletteColors); @@ -727,14 +760,14 @@ void nn_getStd8BitPalette(int color[256]); void nn_setPixel(nn_screen *screen, int x, int y, nn_scrchr_t pixel); nn_scrchr_t nn_getPixel(nn_screen *screen, int x, int y); -bool nn_isDirty(nn_screen *screen); -void nn_setDirty(nn_screen *screen, bool dirty); -bool nn_isPrecise(nn_screen *screen); -void nn_setPrecise(nn_screen *screen, bool precise); -bool nn_isTouchModeInverted(nn_screen *screen); -void nn_setTouchModeInverted(nn_screen *screen, bool touchModeInverted); -bool nn_isOn(nn_screen *buffer); -void nn_setOn(nn_screen *buffer, bool on); +nn_bool_t nn_isDirty(nn_screen *screen); +void nn_setDirty(nn_screen *screen, nn_bool_t dirty); +nn_bool_t nn_isPrecise(nn_screen *screen); +void nn_setPrecise(nn_screen *screen, nn_bool_t precise); +nn_bool_t nn_isTouchModeInverted(nn_screen *screen); +void nn_setTouchModeInverted(nn_screen *screen, nn_bool_t touchModeInverted); +nn_bool_t nn_isOn(nn_screen *buffer); +void nn_setOn(nn_screen *buffer, nn_bool_t on); nn_component *nn_addScreen(nn_computer *computer, nn_address address, int slot, nn_screen *screen); diff --git a/src/unicode.c b/src/unicode.c index dc5ac77..863bc90 100644 --- a/src/unicode.c +++ b/src/unicode.c @@ -1,5 +1,4 @@ #include "neonucleus.h" -#include // both tables copied from: https://github.com/MightyPirates/OpenComputers/blob/52da41b5e171b43fea80342dc75d808f97a0f797/src/main/scala/li/cil/oc/util/FontUtils.scala @@ -159,11 +158,11 @@ static const unsigned char nn_unicode_charWidth_wide_table[] = { 0, 15, 7, 7, 0, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -static bool nn_unicode_is_continuation(unsigned char byte) { +static nn_bool_t nn_unicode_is_continuation(unsigned char byte) { return (byte >> 6) == 0b10; } -bool nn_unicode_validate(const char *b) { +nn_bool_t nn_unicode_validate(const char *b) { const unsigned char* s = (const unsigned char*)b; while (*s) { if(s[0] <= 0x7F) { @@ -219,7 +218,7 @@ char *nn_unicode_char(nn_Alloc *alloc, unsigned int *codepoints, size_t codepoin size_t codepointLen = 0; char c[NN_MAXIMUM_UNICODE_BUFFER]; nn_unicode_codepointToChar(c, codepoint, &codepointLen); - memcpy(buf + j, c, codepointLen); + nn_memcpy(buf + j, c, codepointLen); j += codepointLen; } buf[j] = '\0'; @@ -302,7 +301,7 @@ void nn_unicode_codepointToChar(char *buffer, unsigned int codepoint, size_t *le size_t codepointSize = nn_unicode_codepointSize(codepoint); *len = codepointSize; - memset(buffer, 0, 4); // Clear static array + nn_memset(buffer, 0, 4); // Clear static array if (codepointSize == 1) { buffer[0] = (char)codepoint; diff --git a/src/universe.c b/src/universe.c index e230741..388a093 100644 --- a/src/universe.c +++ b/src/universe.c @@ -1,6 +1,5 @@ #include "neonucleus.h" #include "universe.h" -#include nn_universe *nn_newUniverse(nn_Context ctx) { nn_universe *u = nn_alloc(&ctx.allocator, sizeof(nn_universe)); @@ -24,7 +23,7 @@ void nn_unsafeDeleteUniverse(nn_universe *universe) { void *nn_queryUserdata(nn_universe *universe, const char *name) { for(size_t i = 0; i < universe->udataLen; i++) { - if(strcmp(universe->udata[i].name, name) == 0) { + if(nn_strcmp(universe->udata[i].name, name) == 0) { return universe->udata[i].userdata; } } diff --git a/src/utils.c b/src/utils.c index a37eed6..7e8de75 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,7 +1,4 @@ #include "neonucleus.h" -#include -#include -#include #ifndef NN_BAREMETAL @@ -42,6 +39,8 @@ void nn_dealloc(nn_Alloc *alloc, void *memory, size_t size) { #ifndef NN_BAREMETAL +#include + static void *nn_libcAllocProc(void *_, void *ptr, size_t oldSize, size_t newSize, void *__) { if(newSize == 0) { //printf("Freed %lu bytes from %p\n", oldSize, ptr); @@ -86,21 +85,22 @@ nn_Context nn_libcContext() { // Utilities, both internal and external char *nn_strdup(nn_Alloc *alloc, const char *s) { - size_t l = strlen(s); + size_t l = nn_strlen(s); char *m = nn_alloc(alloc, l+1); if(m == NULL) return m; - return strcpy(m, s); + return nn_strcpy(m, s); } void *nn_memdup(nn_Alloc *alloc, const void *buf, size_t len) { char *m = nn_alloc(alloc, len); if(m == NULL) return m; - return memcpy(m, buf, len); + nn_memcpy(m, buf, len); + return m; } void nn_deallocStr(nn_Alloc *alloc, char *s) { if(s == NULL) return; - nn_dealloc(alloc, s, strlen(s)+1); + nn_dealloc(alloc, s, nn_strlen(s)+1); } size_t nn_rand(nn_Rng *rng) { @@ -210,9 +210,27 @@ void nn_memset(void *buf, unsigned char byte, size_t len) { for(size_t i = 0; i < len; i++) bytes[i] = byte; } +void nn_memcpy(void *dest, const void *src, size_t len) { + char *destBytes = dest; + const char *srcBytes = src; + for(size_t i = 0; i < len; i++) { + destBytes[i] = srcBytes[i]; + } +} + +char *nn_strcpy(char *dest, const char *src) { + size_t i = 0; + while(src[i]) { + dest[i] = src[i]; + i++; + } + dest[i] = 0; + return dest; +} + int nn_strcmp(const char *a, const char *b) { size_t i = 0; - while(true) { + while(NN_TRUE) { unsigned char ca = a[i]; unsigned char cb = b[i]; @@ -229,6 +247,15 @@ int nn_strcmp(const char *a, const char *b) { } } +const char *nn_strchr(const char *str, int ch) { + size_t i = 0; + while(NN_TRUE) { + if(str[i] == ch) return str + i; + if(str[i] == 0) return NULL; + i++; + } +} + size_t nn_strlen(const char *a) { size_t l = 0; while(a[l]) l++; diff --git a/src/value.c b/src/value.c index 38264b1..04075f3 100644 --- a/src/value.c +++ b/src/value.c @@ -1,5 +1,4 @@ #include "neonucleus.h" -#include nn_value nn_values_nil() { return (nn_value) {.tag = NN_VALUE_NIL}; @@ -13,7 +12,7 @@ nn_value nn_values_number(double num) { return (nn_value) {.tag = NN_VALUE_NUMBER, .number = num}; } -nn_value nn_values_boolean(bool boolean) { +nn_value nn_values_boolean(nn_bool_t boolean) { return (nn_value) {.tag = NN_VALUE_BOOL, .boolean = boolean}; } @@ -26,7 +25,7 @@ nn_value nn_values_string(nn_Alloc *alloc, const char *string, size_t len) { if(buf == NULL) { return nn_values_nil(); } - memcpy(buf, string, len); + nn_memcpy(buf, string, len); buf[len] = '\0'; nn_string *s = nn_alloc(alloc, sizeof(nn_string)); @@ -171,7 +170,7 @@ double nn_toNumber(nn_value val) { return 0; } -bool nn_toBoolean(nn_value val) { +nn_bool_t nn_toBoolean(nn_value val) { if(val.tag == NN_VALUE_NIL) return false; if(val.tag == NN_VALUE_BOOL) return val.boolean; return true; @@ -189,7 +188,7 @@ const char *nn_toString(nn_value val, size_t *len) { if(val.tag == NN_VALUE_CSTR) { c = val.cstring; - l = strlen(c); + l = nn_strlen(c); } if(val.tag == NN_VALUE_STR) { c = val.string->data; @@ -212,7 +211,7 @@ size_t nn_measurePacketSize(nn_value *vals, size_t len) { if(len == 0) len = 1; // ask OC size += len; } else if(val.tag == NN_VALUE_CSTR) { - size_t len = strlen(val.cstring); + size_t len = nn_strlen(val.cstring); if(len == 0) len = 1; // ask OC size += len; } else if(val.tag == NN_VALUE_BOOL || val.tag == NN_VALUE_NIL) {