Merge pull request #9 from speedy-lex/main

emulator drive
This commit is contained in:
Quantum Tomato
2025-07-02 22:53:40 +02:00
committed by GitHub
3 changed files with 96 additions and 6 deletions

View File

@@ -51,6 +51,11 @@ void nn_drive_readSector(nn_drive *drive, void *_, nn_component *component, nn_c
nn_value sectorValue = nn_getArgument(computer, 0);
int sector = nn_toInt(sectorValue);
size_t sector_size = drive->getSectorSize(component, drive->userdata);
// we leave the +1 intentionally to compare the end of the real sector
if (sector < 1 || (sector * sector_size > drive->getCapacity(component, drive->userdata))) {
nn_setCError(computer, "bad argument #1 (sector out of range)");
return;
}
char buf[sector_size];
drive->readSector(component, drive->userdata, sector, buf);
nn_return_string(computer, buf, sector_size);
@@ -60,16 +65,31 @@ void nn_drive_writeSector(nn_drive *drive, void *_, nn_component *component, nn_
int sector = nn_toInt(sectorValue);
size_t sector_size = drive->getSectorSize(component, drive->userdata);
nn_value bufValue = nn_getArgument(computer, 1);
const char *buf = nn_toString(bufValue, &sector_size);
size_t buf_size = 0;
const char *buf = nn_toString(bufValue, &buf_size);
if (buf_size != sector_size) {
nn_setCError(computer, "bad argument #2 (expected buffer of length `sectorSize`)");
return;
}
// we leave the +1 intentionally to compare the end of the real sector
if (sector < 1 || (sector * sector_size > drive->getCapacity(component, drive->userdata))) {
nn_setCError(computer, "bad argument #1 (sector out of range)");
return;
}
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 disk_offset = nn_toInt(offsetValue) - 1;
size_t sector_size = drive->getSectorSize(component, drive->userdata);
int sector = disk_offset / sector_size;
int sector = (disk_offset / sector_size) + 1;
size_t sector_offset = disk_offset % sector_size;
if (disk_offset >= drive->getCapacity(component, drive->userdata)) {
nn_setCError(computer, "bad argument #1 (index out of range)");
return;
}
char buf[sector_size];
drive->readSector(component, drive->userdata, sector, buf);
@@ -78,12 +98,20 @@ void nn_drive_readByte(nn_drive *drive, void *_, nn_component *component, nn_com
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 disk_offset = nn_toInt(offsetValue) - 1;
intptr_t write = nn_toInt(writeValue);
size_t sector_size = drive->getSectorSize(component, drive->userdata);
int sector = disk_offset / sector_size;
int sector = (disk_offset / sector_size) + 1;
size_t sector_offset = disk_offset % sector_size;
if (write < -128 || write > 255) {
nn_setCError(computer, "bad argument #2 (byte out of range)");
return;
}
if (disk_offset >= drive->getCapacity(component, drive->userdata)) {
nn_setCError(computer, "bad argument #1 (index out of range)");
return;
}
char buf[sector_size];
drive->readSector(component, drive->userdata, sector, buf);