timesynk/test/vm_memory.c

65 lines
1.8 KiB
C

#include <stdlib.h>
#include <string.h>
#include <stdio.h> // printf
char *vm_createMemory(size_t bytes) {
return malloc(bytes);
}
char *assign_string(char *to_mem, int to_loc, char *string) {
return (strcpy(to_mem+to_loc, string));
}
void *assign_int(char *to_mem, int to_loc, int value) {
return(memcpy(to_mem+to_loc, &value, sizeof(int)));
}
void *m_assign_int(char *from_mem, int from_loc, char *to_mem, int to_loc) {
return(memcpy(to_mem+to_loc, from_mem+from_loc, sizeof(int)));
}
void *m_min_int(char *from_mem, int from_loc, char *to_mem, int to_loc) {
int new_value = *(int*)&to_mem[to_loc] - *(int*)&from_mem[from_loc];
return(memcpy(to_mem+to_loc, &new_value, sizeof(int)));
}
void *min_int(char *to_mem, int to_loc, int value) {
int new_value = *(int*)&to_mem[to_loc] - value;
return(memcpy(to_mem+to_loc, &new_value, sizeof(int)));
}
int main(int argc, char *argv[]) {
char *memory = vm_createMemory(16);
memset(memory, 0, 16);
printf("dumping memory\n");
int offset = 0;
while(offset < 16) {
printf("%d (0x%X): %d(%X)\n", offset, offset, memory[offset], memory[offset]);
offset++;
}
printf("setting %d's value to %d\n", 0, 128);
assign_int(memory, 0, 128);
printf("copying 0x%X to 0x%X as int\n", 0, 4);
m_assign_int(memory, 0, memory, 4);
printf("okay, 0x%X = %d\n", 4, *(int*)&memory[4]);
printf("negating 7 from 0x%X\n", 4);
min_int(memory, 4, 7);
printf("okay, 0x%X = %d\n", 4, *(int*)&memory[4]);
char *test_string = "string";
printf("setting 0x%X to string %s\n", 8, test_string);
assign_string(memory, 8, test_string);
printf("0x%X: %s\n", 8, memory+8);
offset = 0;
while(offset < 16) {
printf("0x%X\n", offset);
printf("\tint: %d, float: %f, char: %c\n", *(int*)(memory+offset), *(float*)(memory+offset), memory[offset]);
offset++;
}
return 0;
}