big ahh recession

This commit is contained in:
2025-07-02 21:29:59 +02:00
parent 5c3ec01a5a
commit 0961fc0ceb
20 changed files with 399 additions and 204 deletions

View File

@@ -21,7 +21,7 @@ void nn_drive_getLabel(nn_drive *drive, void *_, nn_component *component, nn_com
if(l == 0) {
nn_return(computer, nn_values_nil());
} else {
nn_return(computer, nn_values_string(buf, l));
nn_return_string(computer, buf, l);
}
}
void nn_drive_setLabel(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
@@ -33,7 +33,7 @@ void nn_drive_setLabel(nn_drive *drive, void *_, nn_component *component, nn_com
return;
}
l = drive->setLabel(component, drive->userdata, buf, l);
nn_return(computer, nn_values_string(buf, l));
nn_return_string(computer, buf, l);
}
void nn_drive_getSectorSize(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
size_t sector_size = drive->getSectorSize(component, drive->userdata);
@@ -53,7 +53,7 @@ void nn_drive_readSector(nn_drive *drive, void *_, nn_component *component, nn_c
size_t sector_size = drive->getSectorSize(component, drive->userdata);
char buf[sector_size];
drive->readSector(component, drive->userdata, sector, buf);
nn_return(computer, nn_values_string(buf, sector_size));
nn_return_string(computer, buf, sector_size);
}
void nn_drive_writeSector(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
nn_value sectorValue = nn_getArgument(computer, 0);
@@ -93,7 +93,7 @@ void nn_drive_writeByte(nn_drive *drive, void *_, nn_component *component, nn_co
}
void nn_loadDriveTable(nn_universe *universe) {
nn_componentTable *driveTable = nn_newComponentTable("drive", NULL, NULL, (void *)nn_drive_destroy);
nn_componentTable *driveTable = nn_newComponentTable(nn_getAllocator(universe), "drive", NULL, NULL, (void *)nn_drive_destroy);
nn_storeUserdata(universe, "NN:DRIVE", driveTable);
nn_defineMethod(driveTable, "getLabel", false, (void *)nn_drive_getLabel, NULL, "getLabel():string - Get the current label of the drive.");

View File

@@ -41,7 +41,7 @@ void nn_eeprom_getLabel(nn_eeprom *eeprom, void *_, nn_component *component, nn_
if(l == 0) {
nn_return(computer, nn_values_nil());
} else {
nn_return(computer, nn_values_string(buf, l));
nn_return_string(computer, buf, l);
}
// Latency, energy costs and stuff
@@ -61,7 +61,7 @@ void nn_eeprom_setLabel(nn_eeprom *eeprom, void *_, nn_component *component, nn_
return;
}
l = eeprom->setLabel(component, eeprom->userdata, buf, l);
nn_return(computer, nn_values_string(buf, l));
nn_return_string(computer, buf, l);
// Latency, energy costs and stuff
nn_eepromControl control = nn_eeprom_getControl(component, eeprom);
@@ -74,14 +74,15 @@ void nn_eeprom_setLabel(nn_eeprom *eeprom, void *_, nn_component *component, nn_
void nn_eeprom_get(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
size_t cap = eeprom->getSize(component, eeprom->userdata);
char *buf = nn_malloc(cap);
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
char *buf = nn_alloc(alloc, cap);
if(buf == NULL) {
nn_setCError(computer, "out of memory");
return;
}
size_t len = eeprom->get(component, eeprom->userdata, buf);
nn_return(computer, nn_values_string(buf, len));
nn_free(buf);
nn_return_string(computer, buf, len);
nn_dealloc(alloc, buf, cap);
nn_eepromControl control = nn_eeprom_getControl(component, eeprom);
nn_randomLatency(control.randomLatencyMin, control.randomLatencyMax);
@@ -119,7 +120,8 @@ void nn_eeprom_set(nn_eeprom *eeprom, void *_, nn_component *component, nn_compu
void nn_eeprom_getData(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
size_t cap = eeprom->getDataSize(component, eeprom->userdata);
char *buf = nn_malloc(cap);
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
char *buf = nn_alloc(alloc, cap);
if(buf == NULL) {
nn_setCError(computer, "out of memory");
return;
@@ -128,9 +130,9 @@ void nn_eeprom_getData(nn_eeprom *eeprom, void *_, nn_component *component, nn_c
if(len < 0) {
nn_return(computer, nn_values_nil());
} else {
nn_return(computer, nn_values_string(buf, len));
nn_return_string(computer, buf, len);
}
nn_free(buf);
nn_dealloc(alloc, buf, cap);
nn_eepromControl control = nn_eeprom_getControl(component, eeprom);
nn_randomLatency(control.randomLatencyMin, control.randomLatencyMax);
@@ -178,7 +180,8 @@ void nn_eeprom_makeReadonly(nn_eeprom *eeprom, void *_, nn_component *component,
// TODO: make good
void nn_eeprom_getChecksum(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
size_t cap = eeprom->getDataSize(component, eeprom->userdata);
char *buf = nn_malloc(cap);
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
char *buf = nn_alloc(alloc, cap);
if(buf == NULL) {
nn_setCError(computer, "out of memory");
return;
@@ -188,9 +191,9 @@ void nn_eeprom_getChecksum(nn_eeprom *eeprom, void *_, nn_component *component,
for(size_t i = 0; i < len; i++) {
sum += buf[i];
}
nn_free(buf);
nn_dealloc(alloc, buf, cap);
nn_return(computer, nn_values_string((void *)&sum, sizeof(sum)));
nn_return_string(computer, (void *)&sum, sizeof(sum));
nn_eepromControl control = nn_eeprom_getControl(component, eeprom);
nn_randomLatency(control.randomLatencyMin, control.randomLatencyMax);
@@ -200,7 +203,7 @@ void nn_eeprom_getChecksum(nn_eeprom *eeprom, void *_, nn_component *component,
}
void nn_loadEepromTable(nn_universe *universe) {
nn_componentTable *eepromTable = nn_newComponentTable("eeprom", NULL, NULL, (void *)nn_eeprom_destroy);
nn_componentTable *eepromTable = nn_newComponentTable(nn_getAllocator(universe), "eeprom", NULL, NULL, (void *)nn_eeprom_destroy);
nn_storeUserdata(universe, "NN:EEPROM", eepromTable);
nn_defineMethod(eepromTable, "getSize", true, (void *)nn_eeprom_getSize, NULL, "getSize(): integer - Returns the maximum code capacity of the EEPROM.");

View File

@@ -69,7 +69,7 @@ void nn_fs_getLabel(nn_filesystem *fs, void *_, nn_component *component, nn_comp
if(l == 0) {
nn_return(computer, nn_values_nil());
} else {
nn_return(computer, nn_values_string(buf, l));
nn_return_string(computer, buf, l);
}
// Latency, energy costs and stuff
@@ -89,7 +89,7 @@ void nn_fs_setLabel(nn_filesystem *fs, void *_, nn_component *component, nn_comp
return;
}
l = fs->setLabel(component, fs->userdata, buf, l);
nn_return(computer, nn_values_string(buf, l));
nn_return_string(computer, buf, l);
nn_fs_readCost(fs, 1, component, computer);
}
@@ -270,18 +270,20 @@ void nn_fs_list(nn_filesystem *fs, void *_, nn_component *component, nn_computer
nn_setCError(computer, "bad path (illegal path)");
return;
}
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
size_t fileCount = 0;
char **files = fs->list(component, fs->userdata, path, &fileCount);
char **files = fs->list(alloc, component, fs->userdata, path, &fileCount);
if(files != NULL) {
// operation succeeded
nn_value arr = nn_values_array(fileCount);
nn_value arr = nn_values_array(alloc, fileCount);
for(size_t i = 0; i < fileCount; i++) {
nn_values_set(arr, i, nn_values_string(files[i], strlen(files[i])));
nn_free(files[i]);
nn_values_set(arr, i, nn_values_string(alloc, files[i], strlen(files[i])));
nn_deallocStr(alloc, files[i]);
}
nn_free(files);
nn_dealloc(alloc, files, sizeof(char *) * fileCount);
nn_return(computer, arr);
}
@@ -356,7 +358,8 @@ void nn_fs_read(nn_filesystem *fs, void *_, nn_component *component, nn_computer
if(len > capacity) len = capacity;
size_t byteLen = len;
char *buf = nn_malloc(byteLen);
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
char *buf = nn_alloc(alloc, byteLen);
if(buf == NULL) {
nn_setCError(computer, "out of memory");
return;
@@ -365,9 +368,9 @@ void nn_fs_read(nn_filesystem *fs, void *_, nn_component *component, nn_computer
size_t readLen = fs->read(component, fs->userdata, fd, buf, byteLen);
if(readLen > 0) {
// Nothing read means EoF.
nn_return(computer, nn_values_string(buf, readLen));
nn_return_string(computer, buf, readLen);
}
nn_free(buf);
nn_dealloc(alloc, buf, byteLen);
// do not ask where it comes from, balance is hard
nn_fs_readCost(fs, nn_fs_countChunks(fs, readLen, component), component, computer);
@@ -410,7 +413,7 @@ void nn_fs_seek(nn_filesystem *fs, void *_, nn_component *component, nn_computer
}
void nn_loadFilesystemTable(nn_universe *universe) {
nn_componentTable *fsTable = nn_newComponentTable("filesystem", NULL, NULL, (void *)nn_fs_destroy);
nn_componentTable *fsTable = nn_newComponentTable(nn_getAllocator(universe), "filesystem", NULL, NULL, (void *)nn_fs_destroy);
nn_storeUserdata(universe, "NN:FILESYSTEM", fsTable);
nn_defineMethod(fsTable, "getLabel", false, (void *)nn_fs_getLabel, NULL, "getLabel(): string - Returns the label of the filesystem.");

View File

@@ -4,6 +4,7 @@
#include <string.h>
typedef struct nni_gpu {
nn_Alloc alloc;
nn_screen *currentScreen;
nn_address screenAddress;
nn_gpuControl ctrl;
@@ -34,8 +35,10 @@ bool nni_inBounds(nni_gpu *gpu, int x, int y) {
true;
}
nni_gpu *nni_newGPU(nn_gpuControl *ctrl) {
nni_gpu *gpu = nn_malloc(sizeof(nni_gpu));
nni_gpu *nni_newGPU(nn_Alloc *alloc, nn_gpuControl *ctrl) {
nni_gpu *gpu = nn_alloc(alloc, sizeof(nni_gpu));
if(gpu == NULL) return NULL;
gpu->alloc = *alloc;
gpu->currentScreen = NULL;
gpu->screenAddress = NULL;
gpu->ctrl = *ctrl;
@@ -50,10 +53,11 @@ void nni_gpuDeinit(nni_gpu *gpu) {
if(gpu->currentScreen != NULL) {
nn_destroyScreen(gpu->currentScreen);
}
nn_Alloc a = gpu->alloc;
if(gpu->screenAddress != NULL) {
nn_free(gpu->screenAddress);
nn_deallocStr(&a, gpu->screenAddress);
}
nn_free(gpu);
nn_dealloc(&a, gpu, sizeof(nni_gpu));
}
nn_scrchr_t nni_gpu_makePixel(nni_gpu *gpu, const char *s) {
@@ -111,9 +115,9 @@ void nni_gpu_bind(nni_gpu *gpu, void *_, nn_component *component, nn_computer *c
gpu->currentScreen = screen;
if(gpu->screenAddress != NULL) {
nn_free(gpu->screenAddress);
nn_deallocStr(&gpu->alloc, gpu->screenAddress);
}
gpu->screenAddress = nn_strdup(addr);
gpu->screenAddress = nn_strdup(&gpu->alloc, addr);
nn_addHeat(computer, gpu->ctrl.bindHeat);
nn_callCost(computer, gpu->ctrl.bindCost);
@@ -170,7 +174,7 @@ void nni_gpu_get(nni_gpu *gpu, void *_, nn_component *component, nn_computer *co
void nni_gpu_getScreen(nni_gpu *gpu, void *_, nn_component *component, nn_computer *computer) {
if(gpu->screenAddress == NULL) return;
nn_return(computer, nn_values_string(gpu->screenAddress, 0));
nn_return_string(computer, gpu->screenAddress, 0);
}
void nni_gpu_maxResolution(nni_gpu *gpu, void *_, nn_component *component, nn_computer *computer) {
@@ -360,7 +364,7 @@ void nni_gpu_copy(nni_gpu *gpu, void *_, nn_component *component, nn_computer *c
int changes = 0, clears = 0;
nn_scrchr_t *tmpBuffer = nn_malloc(sizeof(nn_scrchr_t) * w * h);
nn_scrchr_t *tmpBuffer = nn_alloc(&gpu->alloc, sizeof(nn_scrchr_t) * w * h);
if(tmpBuffer == NULL) {
nn_setCError(computer, "out of memory");
return;
@@ -388,7 +392,7 @@ void nni_gpu_copy(nni_gpu *gpu, void *_, nn_component *component, nn_computer *c
}
}
nn_free(tmpBuffer);
nn_dealloc(&gpu->alloc, tmpBuffer, sizeof(nn_scrchr_t) * w * h);
nn_addHeat(computer, gpu->ctrl.pixelChangeHeat * changes);
nn_callCost(computer, gpu->ctrl.pixelChangeCost * changes);
@@ -411,7 +415,7 @@ void nni_gpu_getViewport(nni_gpu *gpu, void *_, nn_component *component, nn_comp
}
void nn_loadGraphicsCardTable(nn_universe *universe) {
nn_componentTable *gpuTable = nn_newComponentTable("gpu", NULL, NULL, (void *)nni_gpuDeinit);
nn_componentTable *gpuTable = nn_newComponentTable(nn_getAllocator(universe), "gpu", NULL, NULL, (void *)nni_gpuDeinit);
nn_storeUserdata(universe, "NN:GPU", gpuTable);
nn_defineMethod(gpuTable, "bind", false, (void *)nni_gpu_bind, NULL, "bind(addr: string[, reset: boolean = false]): boolean - Bind a GPU to a screen. Very expensive. If reset is true, it will clear the screen.");
@@ -432,7 +436,7 @@ void nn_loadGraphicsCardTable(nn_universe *universe) {
nn_component *nn_addGPU(nn_computer *computer, nn_address address, int slot, nn_gpuControl *control) {
nn_componentTable *gpuTable = nn_queryUserdata(nn_getUniverse(computer), "NN:GPU");
nni_gpu *gpu = nni_newGPU(control);
nni_gpu *gpu = nni_newGPU(nn_getAllocator(nn_getUniverse(computer)), control);
if(gpu == NULL) {
return NULL;
}

View File

@@ -1,7 +1,7 @@
#include "../neonucleus.h"
void nn_loadKeyboardTable(nn_universe *universe) {
nn_componentTable *keyboardTable = nn_newComponentTable("keyboard", NULL, NULL, NULL);
nn_componentTable *keyboardTable = nn_newComponentTable(nn_getAllocator(universe), "keyboard", NULL, NULL, NULL);
nn_storeUserdata(universe, "NN:KEYBOARD", keyboardTable);
}

View File

@@ -1,10 +1,11 @@
#include "screen.h"
#include <string.h>
nn_screen *nn_newScreen(int maxWidth, int maxHeight, int maxDepth, int editableColors, int paletteColors) {
nn_screen *screen = nn_malloc(sizeof(nn_screen));
screen->buffer = nn_malloc(sizeof(nn_scrchr_t) * maxWidth * maxHeight);
screen->lock = nn_newGuard();
nn_screen *nn_newScreen(nn_Alloc *alloc, int maxWidth, int maxHeight, int maxDepth, int editableColors, int paletteColors) {
nn_screen *screen = nn_alloc(alloc, sizeof(nn_screen));
screen->alloc = *alloc;
screen->buffer = nn_alloc(alloc, sizeof(nn_scrchr_t) * maxWidth * maxHeight);
screen->lock = nn_newGuard(alloc);
screen->refc = 1;
screen->width = maxWidth;
screen->height = maxHeight;
@@ -16,7 +17,7 @@ nn_screen *nn_newScreen(int maxWidth, int maxHeight, int maxDepth, int editableC
screen->depth = maxDepth;
screen->editableColors = editableColors;
screen->paletteColors = paletteColors;
screen->palette = nn_malloc(sizeof(int) * screen->paletteColors);
screen->palette = nn_alloc(alloc, sizeof(int) * screen->paletteColors);
memset(screen->palette, 0, sizeof(int) * screen->paletteColors);
screen->aspectRatioWidth = 1;
screen->aspectRatioHeight = 1;
@@ -34,10 +35,11 @@ void nn_retainScreen(nn_screen *screen) {
void nn_destroyScreen(nn_screen *screen) {
if(!nn_decRef(&screen->refc)) return;
nn_deleteGuard(screen->lock);
nn_free(screen->buffer);
nn_free(screen->palette);
nn_free(screen);
nn_Alloc a = screen->alloc;
nn_deleteGuard(&a, screen->lock);
nn_dealloc(&a, screen->buffer, sizeof(nn_scrchr_t) * screen->maxWidth * screen->maxHeight);
nn_dealloc(&a, screen->palette, sizeof(int) * screen->paletteColors);
nn_dealloc(&a, screen, sizeof(nn_screen));
}
void nn_lockScreen(nn_screen *screen) {
@@ -85,14 +87,14 @@ void nn_setAspectRatio(nn_screen *screen, int width, int height) {
void nn_addKeyboard(nn_screen *screen, nn_address address) {
if(screen->keyboardCount == NN_MAX_SCREEN_KEYBOARDS) return;
screen->keyboards[screen->keyboardCount++] = nn_strdup(address);
screen->keyboards[screen->keyboardCount++] = nn_strdup(&screen->alloc, address);
}
void nn_removeKeyboard(nn_screen *screen, nn_address address) {
size_t j = 0;
for(size_t i = 0; i < screen->keyboardCount; i++) {
if(strcmp(screen->keyboards[i], address) == 0) {
nn_free(screen->keyboards[i]);
nn_deallocStr(&screen->alloc, screen->keyboards[i]);
} else {
screen->keyboards[j] = screen->keyboards[i];
j++;
@@ -206,12 +208,12 @@ void nn_screenComp_destroy(void *_, nn_component *component, nn_screen *screen)
void nn_screenComp_getKeyboards(nn_screen *screen, void *_, nn_component *component, nn_computer *computer) {
nn_lockScreen(screen);
nn_value arr = nn_values_array(nn_getKeyboardCount(screen));
nn_value arr = nn_values_array(&screen->alloc, nn_getKeyboardCount(screen));
size_t len = arr.array->len;
for(size_t i = 0; i < len; i++) {
size_t addrlen = strlen(nn_getKeyboard(screen, i));
nn_value addr = nn_values_string(nn_getKeyboard(screen, i), addrlen);
nn_value addr = nn_values_string(&screen->alloc, nn_getKeyboard(screen, i), addrlen);
nn_values_set(arr, i, addr);
}
@@ -220,7 +222,7 @@ void nn_screenComp_getKeyboards(nn_screen *screen, void *_, nn_component *compon
}
void nn_loadScreenTable(nn_universe *universe) {
nn_componentTable *screenTable = nn_newComponentTable("screen", NULL, NULL, (void *)nn_screenComp_destroy);
nn_componentTable *screenTable = nn_newComponentTable(nn_getAllocator(universe), "screen", NULL, NULL, (void *)nn_screenComp_destroy);
nn_storeUserdata(universe, "NN:SCREEN", screenTable);
nn_defineMethod(screenTable, "getKeyboards", false, (void *)nn_screenComp_getKeyboards, NULL, "getKeyboards(): string[] - Returns the keyboards registered to this screen.");

View File

@@ -4,6 +4,7 @@
#include "../neonucleus.h"
typedef struct nn_screen {
nn_Alloc alloc;
nn_scrchr_t *buffer;
nn_guard *lock;
nn_refc refc;