nga-c: refactor block file i/o device

FossilOrigin-Name: 5a5f7688c8ffa2c9af3c4942cbf6261ade675ccebf003f8bd78bd57de226d316
This commit is contained in:
crc 2023-12-10 23:38:07 +00:00
parent 987c3a5cc1
commit 4155ac0300
2 changed files with 34 additions and 28 deletions

View file

@ -52,5 +52,7 @@
- use C preprocessor to help with readability in arg. processing
- remove separate temp. string buffer in `s:dedup`
- use constants for device identifiers in nga-c
- use constants for device identifiers in the Forth part of
the device handlers
================================================================

View file

@ -11,42 +11,46 @@
**************************************************************/
void io_blocks(NgaState *vm) {
CELL op, buffer, block;
V read_block(NgaState *vm) {
CELL buffer = stack_pop(vm);
CELL block = stack_pop(vm);
int32_t m[1024];
op = stack_pop(vm);
if (op == 0) {
buffer = stack_pop(vm);
block = stack_pop(vm);
int fp = open(vm->BlockFile, O_RDONLY, 0666);
lseek(fp, 4096 * block, SEEK_SET);
read(fp, m, 4096);
for (int i = 0; i < 1024; i++) {
vm->memory[buffer + i] = (CELL)m[i];
}
close(fp);
int fp = open(vm->BlockFile, O_RDONLY, 0666);
lseek(fp, 4096 * block, SEEK_SET);
read(fp, m, 4096);
for (int i = 0; i < 1024; i++) {
vm->memory[buffer + i] = (CELL)m[i];
}
close(fp);
}
if (op == 1) {
buffer = stack_pop(vm);
block = stack_pop(vm);
int fp = open(vm->BlockFile, O_WRONLY, 0666);
lseek(fp, 4096 * block, SEEK_SET);
for (int i = 0; i < 1024; i++) {
m[i] = (int32_t)vm->memory[buffer + i];
}
write(fp, m, 4096);
close(fp);
V write_block(NgaState *vm) {
CELL buffer = stack_pop(vm);
CELL block = stack_pop(vm);
int32_t m[1024];
int fp = open(vm->BlockFile, O_WRONLY, 0666);
lseek(fp, 4096 * block, SEEK_SET);
for (int i = 0; i < 1024; i++) {
m[i] = (int32_t)vm->memory[buffer + i];
}
write(fp, m, 4096);
close(fp);
}
if (op == 2) {
buffer = stack_pop(vm);
strlcpy(vm->BlockFile, string_extract(vm, buffer), 1024);
V set_block_file(NgaState *vm) {
CELL buffer = stack_pop(vm);
strlcpy(vm->BlockFile, string_extract(vm, buffer), 1024);
}
V io_blocks(NgaState *vm) {
switch (stack_pop(vm)) {
case 0: read_block(vm); break;
case 1: write_block(vm); break;
case 2: set_block_file(vm); break;
}
}
void query_blocks(NgaState *vm) {
V query_blocks(NgaState *vm) {
stack_push(vm, 0);
stack_push(vm, DEVICE_BLOCKS);
}