This commit is contained in:
IonutParau 2025-07-01 22:52:54 +02:00
commit fa54ed843d
4 changed files with 120 additions and 6 deletions

View File

@ -17,6 +17,7 @@ fn addEngineSources(c: *std.Build.Step.Compile) void {
// components
"src/components/eeprom.c",
"src/components/filesystem.c",
"src/components/drive.c",
"src/components/screen.c",
"src/components/gpu.c",
"src/components/keyboard.c",

View File

@ -1,5 +1,4 @@
@echo off
zig build
copy lua\lua54.dll zig-out\bin\
copy raylib\lib\raylib.dll zig-out\bin
zig build run

114
src/components/drive.c Normal file
View File

@ -0,0 +1,114 @@
#include "../neonucleus.h"
#include <stdio.h>
#include <string.h>
void nn_drive_destroy(void *_, nn_component *component, nn_drive *drive) {
if(!nn_decRef(&drive->refc)) return;
if(drive->deinit == NULL) {
drive->deinit(component, drive->userdata);
}
}
nn_driveControl nn_drive_getControl(nn_component *component, nn_drive *drive) {
return drive->control(component, drive->userdata);
}
void nn_drive_getLabel(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
char buf[NN_LABEL_SIZE];
size_t l = NN_LABEL_SIZE;
drive->getLabel(component, drive->userdata, buf, &l);
if(l == 0) {
nn_return(computer, nn_values_nil());
} else {
nn_return(computer, nn_values_string(buf, l));
}
}
void nn_drive_setLabel(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
size_t l = 0;
nn_value label = nn_getArgument(computer, 0);
const char *buf = nn_toString(label, &l);
if(buf == NULL) {
nn_setCError(computer, "bad label (string expected)");
return;
}
l = drive->setLabel(component, drive->userdata, buf, l);
nn_return(computer, nn_values_string(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);
nn_return(computer, nn_values_integer(sector_size));
}
void nn_drive_getPlatterCount(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
size_t platter_count = drive->getPlatterCount(component, drive->userdata);
nn_return(computer, nn_values_integer(platter_count));
}
void nn_drive_getCapacity(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
size_t capacity = drive->getCapacity(component, drive->userdata);
nn_return(computer, nn_values_integer(capacity));
}
void nn_drive_readSector(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
nn_value sectorValue = nn_getArgument(computer, 0);
int sector = nn_toInt(sectorValue);
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));
}
void nn_drive_writeSector(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
nn_value sectorValue = nn_getArgument(computer, 0);
int sector = nn_toInt(sectorValue);
size_t sector_size = drive->getSectorSize(component, drive->userdata);
nn_value bufValue = nn_getArgument(computer, 1);
char *buf = nn_toString(bufValue, &sector_size);
drive->writeSector(component, drive->userdata, sector, buf);
}
void nn_drive_readByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
nn_value offsetValue = nn_getArgument(computer, 0);
size_t disk_offset = nn_toInt(offsetValue);
size_t sector_size = drive->getSectorSize(component, drive->userdata);
int sector = disk_offset / sector_size;
size_t sector_offset = disk_offset % sector_size;
char buf[sector_size];
drive->readSector(component, drive->userdata, sector, &buf);
nn_return(computer, nn_values_integer(buf[sector_offset]));
}
void nn_drive_writeByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
nn_value offsetValue = nn_getArgument(computer, 0);
nn_value writeValue = nn_getArgument(computer, 1);
size_t disk_offset = nn_toInt(offsetValue);
char write = nn_toInt(writeValue);
size_t sector_size = drive->getSectorSize(component, drive->userdata);
int sector = disk_offset / sector_size;
size_t sector_offset = disk_offset % sector_size;
char buf[sector_size];
drive->readSector(component, drive->userdata, sector, &buf);
buf[sector_offset] = write;
drive->writeSector(component, drive->userdata, sector, buf);
}
void nn_loadDriveTable(nn_universe *universe) {
nn_componentTable *driveTable = nn_newComponentTable("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.");
nn_defineMethod(driveTable, "setLabel", false, (void *)nn_drive_setLabel, NULL, "setLabel(value:string):string - Sets the label of the drive. Returns the new value, which may be truncated.");
nn_defineMethod(driveTable, "getSectorSize", true, (void *)nn_drive_getSectorSize, NULL, "getSectorSize():number - Returns the size of a single sector on the drive, in bytes.");
nn_defineMethod(driveTable, "getPlatterCounter", true, (void *)nn_drive_getPlatterCount, NULL, "getPlatterCount():number - Returns the number of platters in the drive.");
nn_defineMethod(driveTable, "getCapacity", true, (void *)nn_drive_getCapacity, NULL, "getCapacity():number - Returns the total capacity of the drive, in bytes.");
nn_defineMethod(driveTable, "readSector", false, (void *)nn_drive_readSector, NULL, "readSector(sector:number):string - Read the current contents of the specified sector.");
nn_defineMethod(driveTable, "writeSector", false, (void *)nn_drive_writeSector, NULL, "writeSector(sector:number, value:string) - Write the specified contents to the specified sector.");
nn_defineMethod(driveTable, "readByte", false, (void *)nn_drive_readByte, NULL, "readByte(offset:number):number - Read a single byte at the specified offset.");
nn_defineMethod(driveTable, "writeByte", false, (void *)nn_drive_writeByte, NULL, "writeByte(offset:number, value:number) - Write a single byte to the specified offset.");
}
nn_component *nn_addDrive(nn_computer *computer, nn_address address, int slot, nn_drive *drive) {
nn_componentTable *driveTable = nn_queryUserdata(nn_getUniverse(computer), "NN:DRIVE");
return nn_newComponent(computer, address, slot, driveTable, drive);
}

View File

@ -47,7 +47,7 @@ double nn_getTime(nn_universe *universe) {
void nn_loadCoreComponentTables(nn_universe *universe) {
nn_loadEepromTable(universe);
nn_loadFilesystemTable(universe);
//nn_loadDriveTable(universe);
nn_loadDriveTable(universe);
nn_loadScreenTable(universe);
nn_loadGraphicsCardTable(universe);
nn_loadKeyboardTable(universe);