mirror of
https://github.com/NeoFlock/neonucleus.git
synced 2025-09-24 09:03:32 +02:00
emulator drive
This commit is contained in:
parent
0961fc0ceb
commit
20fecebb90
BIN
data/drive.img
Normal file
BIN
data/drive.img
Normal file
Binary file not shown.
@ -60,10 +60,20 @@ void nn_drive_writeSector(nn_drive *drive, void *_, nn_component *component, nn_
|
|||||||
int sector = nn_toInt(sectorValue);
|
int sector = nn_toInt(sectorValue);
|
||||||
size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
||||||
nn_value bufValue = nn_getArgument(computer, 1);
|
nn_value bufValue = nn_getArgument(computer, 1);
|
||||||
const char *buf = nn_toString(bufValue, §or_size);
|
|
||||||
|
size_t buf_size = 0;
|
||||||
|
const char *buf = nn_toString(bufValue, &buf_size);
|
||||||
|
if (buf_size < sector_size) {
|
||||||
|
char padded[sector_size];
|
||||||
|
memset(padded, 0, sector_size);
|
||||||
|
memcpy(padded, buf, buf_size);
|
||||||
|
drive->writeSector(component, drive->userdata, sector, padded);
|
||||||
|
} else {
|
||||||
drive->writeSector(component, drive->userdata, sector, buf);
|
drive->writeSector(component, drive->userdata, sector, buf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void nn_drive_readByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_readByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
|
// TODO: calculate the right index aaaaargh
|
||||||
nn_value offsetValue = nn_getArgument(computer, 0);
|
nn_value offsetValue = nn_getArgument(computer, 0);
|
||||||
size_t disk_offset = nn_toInt(offsetValue);
|
size_t disk_offset = nn_toInt(offsetValue);
|
||||||
size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
size_t sector_size = drive->getSectorSize(component, drive->userdata);
|
||||||
@ -76,6 +86,7 @@ void nn_drive_readByte(nn_drive *drive, void *_, nn_component *component, nn_com
|
|||||||
nn_return(computer, nn_values_integer(buf[sector_offset]));
|
nn_return(computer, nn_values_integer(buf[sector_offset]));
|
||||||
}
|
}
|
||||||
void nn_drive_writeByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
void nn_drive_writeByte(nn_drive *drive, void *_, nn_component *component, nn_computer *computer) {
|
||||||
|
// TODO: calculate the right index aaaaargh
|
||||||
nn_value offsetValue = nn_getArgument(computer, 0);
|
nn_value offsetValue = nn_getArgument(computer, 0);
|
||||||
nn_value writeValue = nn_getArgument(computer, 1);
|
nn_value writeValue = nn_getArgument(computer, 1);
|
||||||
size_t disk_offset = nn_toInt(offsetValue);
|
size_t disk_offset = nn_toInt(offsetValue);
|
||||||
|
@ -238,6 +238,47 @@ bool ne_fs_exists(nn_component *component, ne_fs *fs, const char *path) {
|
|||||||
return FileExists(p) || DirectoryExists(p);
|
return FileExists(p) || DirectoryExists(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct ne_drive {
|
||||||
|
FILE *file;
|
||||||
|
} ne_drive;
|
||||||
|
|
||||||
|
void ne_drive_close(nn_component *component, ne_drive *drive) {
|
||||||
|
fclose(drive->file);
|
||||||
|
}
|
||||||
|
nn_driveControl ne_drive_getControl(nn_component *component, ne_drive *_) {
|
||||||
|
return (nn_driveControl){};
|
||||||
|
}
|
||||||
|
size_t ne_drive_getPlatterCount(nn_component *component, ne_drive *_) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
size_t ne_drive_getSectorSize(nn_component *component, ne_drive *_) {
|
||||||
|
return 512;
|
||||||
|
}
|
||||||
|
size_t ne_drive_getCapacity(nn_component *component, ne_drive *drive) {
|
||||||
|
fseek(drive->file, 0, SEEK_END);
|
||||||
|
return ftell(drive->file);
|
||||||
|
}
|
||||||
|
void ne_drive_readSector(nn_component *component, ne_drive *drive, int shifted_sector, char *buf) {
|
||||||
|
int sector = shifted_sector - 1;
|
||||||
|
size_t sectorSize = ne_drive_getSectorSize(component, drive);
|
||||||
|
|
||||||
|
size_t offset = sector * sectorSize;
|
||||||
|
fseek(drive->file, offset, SEEK_SET);
|
||||||
|
fread(buf, sizeof(char), sectorSize, drive->file);
|
||||||
|
}
|
||||||
|
void ne_drive_writeSector(nn_component *component, ne_drive *drive, int shifted_sector, const char *buf) {
|
||||||
|
int sector = shifted_sector - 1;
|
||||||
|
size_t sectorSize = ne_drive_getSectorSize(component, drive);
|
||||||
|
|
||||||
|
size_t offset = sector * sectorSize;
|
||||||
|
fseek(drive->file, offset, SEEK_SET);
|
||||||
|
fwrite(buf, sizeof(char), sectorSize, drive->file);
|
||||||
|
|
||||||
|
// this is probably not needed but i believe someone isn't running the deinit
|
||||||
|
fflush(drive->file);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int keycode_to_oc(int keycode) {
|
int keycode_to_oc(int keycode) {
|
||||||
switch (keycode) {
|
switch (keycode) {
|
||||||
case KEY_NULL:
|
case KEY_NULL:
|
||||||
@ -533,6 +574,48 @@ int main() {
|
|||||||
};
|
};
|
||||||
nn_addFileSystem(computer, "OpenOS", 1, &genericFS);
|
nn_addFileSystem(computer, "OpenOS", 1, &genericFS);
|
||||||
|
|
||||||
|
ne_drive drive = {
|
||||||
|
.file = fopen("data/drive.img", "r+")
|
||||||
|
};
|
||||||
|
assert(drive.file != NULL);
|
||||||
|
|
||||||
|
nn_drive genericDrive = {
|
||||||
|
.refc = 0,
|
||||||
|
.userdata = &drive,
|
||||||
|
.deinit = (void *)ne_drive_close,
|
||||||
|
.control = (void *)ne_drive_getControl,
|
||||||
|
.getLabel = ne_eeprom_getLabel,
|
||||||
|
.setLabel = ne_eeprom_setLabel,
|
||||||
|
.getPlatterCount = (void *)ne_drive_getPlatterCount,
|
||||||
|
.getSectorSize = (void *)ne_drive_getSectorSize,
|
||||||
|
.getCapacity = (void *)ne_drive_getCapacity,
|
||||||
|
.readSector = (void *)ne_drive_readSector,
|
||||||
|
.writeSector = (void *)ne_drive_writeSector,
|
||||||
|
};
|
||||||
|
|
||||||
|
nn_addDrive(computer, "drive.img", 4, &genericDrive);
|
||||||
|
|
||||||
|
ne_drive drive = {
|
||||||
|
.file = fopen("data/drive.img", "r+")
|
||||||
|
};
|
||||||
|
assert(drive.file != NULL);
|
||||||
|
|
||||||
|
nn_drive genericDrive = {
|
||||||
|
.refc = 0,
|
||||||
|
.userdata = &drive,
|
||||||
|
.deinit = (void *)ne_drive_close,
|
||||||
|
.control = (void *)ne_drive_getControl,
|
||||||
|
.getLabel = ne_eeprom_getLabel,
|
||||||
|
.setLabel = ne_eeprom_setLabel,
|
||||||
|
.getPlatterCount = (void *)ne_drive_getPlatterCount,
|
||||||
|
.getSectorSize = (void *)ne_drive_getSectorSize,
|
||||||
|
.getCapacity = (void *)ne_drive_getCapacity,
|
||||||
|
.readSector = (void *)ne_drive_readSector,
|
||||||
|
.writeSector = (void *)ne_drive_writeSector,
|
||||||
|
};
|
||||||
|
|
||||||
|
nn_addDrive(computer, "drive.img", 4, &genericDrive);
|
||||||
|
|
||||||
nn_screen *s = nn_newScreen(&alloc, 80, 32, 16, 16, 256);
|
nn_screen *s = nn_newScreen(&alloc, 80, 32, 16, 16, 256);
|
||||||
nn_addKeyboard(s, "shitty keyboard");
|
nn_addKeyboard(s, "shitty keyboard");
|
||||||
nn_mountKeyboard(computer, "shitty keyboard", 2);
|
nn_mountKeyboard(computer, "shitty keyboard", 2);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user