Console Device is ready to use

+ started doing clock devices
+ console device is now located at devices[1], devices[0] is clock
This commit is contained in:
Ærþ 2022-02-19 13:10:48 +07:00 committed by GitHub
parent 219af6eb76
commit d02a5d3dfd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 124 additions and 52 deletions

36
device/clock.c Normal file
View file

@ -0,0 +1,36 @@
#define CLOCK vm->devices[0]
void clock_write(struct VMinst* vm, void* data, uint64_t length) {
}
void clock_out(struct VMinst* vm) {
}
void clock_read(struct VMinst* vm, void* buffer, uint64_t length, uint64_t where) {
}
void clock_wait(struct VMinst* vm, uint64_t data) {
}
void clock_poweroff(struct VMinst* vm) {
#ifdef LLLIVMDEBUG
puts("clock_poweroff<");
#endif
free(CLOCK.buffer);
#ifdef LLLIVMDEBUG
puts("clock_poweroff>");
#endif
}
void setup_clock(struct VMinst* vm) {
CLOCK.buffer = malloc(10240);
CLOCK.write = &clock_write;
CLOCK.out = &clock_out;
CLOCK.read = &clock_read;
CLOCK.wait = &clock_wait;
CLOCK.poweroff = &clock_poweroff;
}

50
device/console.c Normal file
View file

@ -0,0 +1,50 @@
#define CONSOLE vm->devices[1]
void console_write(struct VMinst* vm, void* data, uint64_t length) {
uint16_t* write_bс = ((uint16_t*)CONSOLE.buffer);
uint8_t* write_b = (uint8_t*)(CONSOLE.buffer + 2);
for (uint64_t i = 0; i < length; ++i) {
if (*write_bс == 5120) return;
write_b[(*write_bс)++] = ((uint8_t*)data)[i];
}
}
void console_out(struct VMinst* vm) {
puts((char*)(CONSOLE.buffer + 2));
}
void console_read(struct VMinst* vm, void* buffer, uint64_t length, uint64_t where) {
uint16_t* read_bс = ((uint16_t*)(CONSOLE.buffer + 5120));
uint8_t* read_b = ((uint8_t*)(CONSOLE.buffer + 5120) + where) + 2;
for (uint64_t i = 0; i < length; ++i) {
((uint8_t*)buffer)[i] = read_b[i];
}
*read_bс = 0;
}
void console_wait(struct VMinst* vm, uint64_t data) {
uint16_t* read_bс = ((uint16_t*)(vm->devices[1].buffer + 5120));
uint8_t* read_b = ((uint8_t*)vm->devices[1].buffer + 5120 + 2);
printf("enter data: ");
for (; *read_bс < data;)
read_b[(*read_bс)++] = getchar();
}
void console_poweroff(struct VMinst* vm) {
#ifdef LLLIVMDEBUG
puts("console_poweroff<");
#endif
free(CONSOLE.buffer);
#ifdef LLLIVMDEBUG
puts("console_poweroff>");
#endif
}
void setup_console(struct VMinst* vm) {
CONSOLE.buffer = malloc(10240);
CONSOLE.write = &console_write;
CONSOLE.out = &console_out;
CONSOLE.read = &console_read;
CONSOLE.wait = &console_wait;
CONSOLE.poweroff = &console_poweroff;
}

View file

@ -1,55 +1,25 @@
#define DEV_C 1
/*
(C) M. Æ.
*/
void c_write(struct VMinst* vm, void* data, uint64_t length) {
uint16_t* write_bс = ((uint16_t*)vm->devices[0].buffer);
uint8_t* write_b = (uint8_t*)(vm->devices[0].buffer + 2);
for (uint64_t i = 0; i < length; ++i) {
if (*write_bс == 5120) return;
write_b[(*write_bс)++] = ((uint8_t*)data)[i];
}
}
#define DEV_C 2
void c_out(struct VMinst* vm) {
puts((char*)(vm->devices[0].buffer + 2));
}
void c_read(struct VMinst* vm, void* buffer, uint64_t length, uint64_t where) {
uint16_t* read_bс = ((uint16_t*)(vm->devices[0].buffer + 5120));
uint8_t* read_b = ((uint8_t*)(vm->devices[0].buffer + 5120) + where) + 2;
for (uint64_t i = 0; i < length; ++i) {
((uint8_t*)buffer)[i] = read_b[i + 1];
}
*read_bс = 0;
}
void c_wait(struct VMinst* vm, uint64_t data) {
uint16_t* read_bс = ((uint16_t*)(vm->devices[0].buffer + 5120));
uint8_t* read_b = ((uint8_t*)vm->devices[0].buffer + 5120);
for (uint64_t i = 0; i < data; i++) {
if (*read_bс == 5120) return;
read_b[++(*read_bс)] = getchar();
}
}
void c_poweroff(struct VMinst* vm) {
free(vm->devices[0].buffer);
}
void setup_console(struct VMinst* vm) {
vm->devices[0].buffer = malloc(10240);
vm->devices[0].write = &c_write;
vm->devices[0].out = &c_out;
vm->devices[0].read = &c_read;
vm->devices[0].wait = &c_wait;
vm->devices[0].poweroff = &c_poweroff;
}
#include "device/clock.c"
#include "device/console.c"
void dev_init(struct VMinst* vm) {
vm->devices = (struct IODevice*)malloc(sizeof(struct IODevice) * 1);
vm->devices = (struct IODevice*)malloc(sizeof(struct IODevice) * DEV_C);
setup_clock(vm);
setup_console(vm);
#ifdef LIVMDEBUG
puts("dev_init");
#endif
}
void dev_delete(struct VMinst* vm) {
for (uint64_t d = 0; d < DEV_C; d++) vm->devices[d].poweroff(vm);
free(vm->devices);
#ifdef LIVMDEBUG
puts("dev_delete");
#endif
}

10
livm.c
View file

@ -77,9 +77,15 @@ struct VMinst* loadRAM(struct VMinst* vm, uint64_t* RAM, uint64_t length) {
struct VMinst* deleteVM(struct VMinst* vm) {
LIVMDELETING;
free(vm->ram);
dev_delete(vm);
return vm;
}
void print_ullram(struct VMinst* vm) {
for (int i = 0; i < 100; i++) printf("%lu ", vm->uram64[i]);
printf("\n");
}
struct VMinst* runVM(struct VMinst* vm) {
LIVMSTARTWORK;
register uint64_t ip = vm->ip,
@ -134,7 +140,9 @@ struct VMinst* runVM(struct VMinst* vm) {
#include "xcase.c"
}
#ifdef LLLIVMDEBUG
getchar();
if (getchar() == 'x') {
print_ullram(vm);
};
#endif
}
end: LIVMENDWORK;

16
main.c
View file

@ -12,17 +12,25 @@
#include "livm.c"
uint64_t str2u64(char* data) {
uint64_t* result = (uint64_t*)data;
return *result;
}
int main() {
struct VMinst vm = createVM(256); // alloc 256 bytes
uint64_t vmram[] = { // "proper" way to write out "Hello, LIVM!"
// start:
1, 1, 8, 0, // goto code
// data:
0x4c202c6f6c6c6548, 0x00000000214d5649, 0, 0, // "Hello, LIVM!"
str2u64("You ente"), str2u64("red: "), 0, 0,
// code:
381, 3, 0, 32, 12, // device.write(console, "Hello, LIVM!", 12) - write string (char*) to console's buffer
382, 1, 0, // device.out(console) - write out console's buffer
12, 0 // stop VM
384, 2, 1, 4,
383, 4, 1, 48, 4, 0,
381, 3, 1, 32, 20,
382, 1, 1,
12, 0
};
loadRAM(&vm, vmram, sizeof vmram / sizeof(uint64_t));
//uint64_t dt[] = {5485433203209299272, 7810728294139909705, 0};

View file

@ -286,7 +286,7 @@ case 257: uram16[args[0]] = uram16[uram64[args[1]]]; break;
case 258: ram16[args[0]] = ram16[uram64[args[1]]]; break;
case 259: uram8[args[0]] = uram8[uram64[args[1]]]; break;
case 260: ram8[args[0]] = ram8[uram64[args[1]]]; break;
// **a = b
// *a = b
case 261: uram64[uram64[args[0]]] = (uint64_t) args[1]; break;
case 262: ram64[uram64[args[0]]] = (int64_t) args[1]; break;
case 263: ramd[uram64[args[0]]] = (double) args[1]; break;
@ -297,7 +297,7 @@ case 267: uram16[uram64[args[0]]] = (uint16_t) args[1]; break;
case 268: ram16[uram64[args[0]]] = (int16_t) args[1]; break;
case 269: uram8[uram64[args[0]]] = (uint8_t) args[1]; break;
case 270: ram8[uram64[args[0]]] = (int8_t) args[1]; break;
// **a = *b
// *a = *b
case 271: uram64[uram64[args[0]]] = uram64[args[1]]; break;
case 272: ram64[uram64[args[0]]] = ram64[args[1]]; break;
case 273: ramd[uram64[args[0]]] = ramd[args[1]]; break;
@ -308,7 +308,7 @@ case 277: uram16[uram64[args[0]]] = uram16[args[1]]; break;
case 278: ram16[uram64[args[0]]] = ram16[args[1]]; break;
case 279: uram8[uram64[args[0]]] = uram8[args[1]]; break;
case 280: ram8[uram64[args[0]]] = ram8[args[1]]; break;
// **a = **b
// *a = **b
case 281: uram64[uram64[args[0]]] = uram64[uram64[args[1]]]; break;
case 282: ram64[uram64[args[0]]] = ram64[uram64[args[1]]]; break;
case 283: ramd[uram64[args[0]]] = ramd[uram64[args[1]]]; break;