From 7884c2c2e7beea3f9a6cdce02d064d9a2a1e5a18 Mon Sep 17 00:00:00 2001 From: ax-6 Date: Sun, 30 Jun 2024 14:38:12 +0800 Subject: [PATCH] Added debug code to the memory part of Aqvm. --- aqvm/memory/memory.c | 77 +++++++++++++++++++++++++++++++++----------- aqvm/memory/memory.h | 15 +++++---- aqvm/memory/types.h | 6 +++- 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/aqvm/memory/memory.c b/aqvm/memory/memory.c index 444facd..5dd9e0d 100644 --- a/aqvm/memory/memory.c +++ b/aqvm/memory/memory.c @@ -71,8 +71,9 @@ struct AqvmMemory_Memory* AqvmMemory_CreateMemory(void* data, void* type, struct AqvmMemory_Memory* memory_ptr = (struct AqvmMemory_Memory*)malloc(sizeof(struct AqvmMemory_Memory)); if (memory_ptr == NULL) { - // TODO(WARNING): Handle the warning of memory allocation. - AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){1,"AqvmMemoryCreateMemory_MemoryAllocationWarning","Can't allocate memory.",NULL}); + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 2, "AqvmMemoryCreateMemory_MemoryAllocationFailure", + "Failed to allocate memory.", NULL}); return NULL; } @@ -89,21 +90,34 @@ void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr) { int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index, uint8_t type) { - if (memory == NULL || memory->type == NULL) { - // TODO(ERROR): The memory is NULL. + if (memory == NULL) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemorySetType_NullMemoryPointer", "The memory pointer is NULL.", + NULL}); return -1; } - if (index > memory->size) { - // TODO(ERROR): The index is out of memory range. + if (memory->type == NULL) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemorySetType_NullTypePointer", "The type pointer is NULL.", + NULL}); return -2; } - if (type > 0x0F) { - // TODO(ERROR): The type is out of range. + if (index > memory->size) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemorySetType_OutOfMemoryRange", + "The index is out of memory range.", NULL}); return -3; } + if (type > 0x0F) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemorySetType_OutOfTypeRange", "The type is out of range.", + NULL}); + return -4; + } + if (index % 2 != 0) { memory->type[index / 2] = (memory->type[index / 2] & 0xF0) | type; } else { @@ -114,14 +128,25 @@ int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index, } uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) { - if (memory == NULL || memory->type == NULL) { - // TODO(ERROR): The memory is NULL. - return 0x10; + if (memory == NULL) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemoryGetType_NullMemoryPointer", "The memory pointer is NULL.", + NULL}); + return 0x11; + } + + if (memory->type == NULL) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemoryGetType_NullTypePointer", "The type pointer is NULL.", + NULL}); + return 0x12; } if (index > memory->size) { - // TODO(ERROR): The index is out of memory range. - return 0x20; + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemoryGetType_OutOfMemoryRange", + "The index is out of memory range.", NULL}); + return 0x13; } if (index % 2 != 0) { @@ -133,19 +158,33 @@ uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index) { int AqvmMemory_WriteData(struct AqvmMemory_Memory* memory, size_t index, void* data_ptr, size_t size) { - if (memory == NULL || memory->data == NULL) { - // TODO(ERROR): The memory is NULL. + if (memory == NULL) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemoryWriteData_NullMemoryPointer", + "The memory pointer is NULL.", NULL}); return -1; } - if (index + size > memory->size) { - // TODO(ERROR): The index is out of memory range. + + if (memory->type == NULL) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemoryWriteData_NullTypePointer", "The type pointer is NULL.", + NULL}); return -2; } - if (data_ptr == NULL) { - // TODO(ERROR): The data is NULL. + + if (index > memory->size) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemoryWriteData_OutOfMemoryRange", + "The index is out of memory range.", NULL}); return -3; } + if (data_ptr == NULL) { + AqvmRuntimeDebugger_OutputReport((struct AqvmRuntimeDebugger_DebugReport){ + 1, "AqvmMemoryWriteData_NullDataPointer", "The data pointer is NULL.", NULL}); + return -4; + } + memcpy((void*)((uintptr_t)memory->data + index), data_ptr, size); return 0; } \ No newline at end of file diff --git a/aqvm/memory/memory.h b/aqvm/memory/memory.h index cf5b7ea..09c0a7d 100644 --- a/aqvm/memory/memory.h +++ b/aqvm/memory/memory.h @@ -45,20 +45,23 @@ void AqvmMemory_FreeMemory(struct AqvmMemory_Memory* memory_ptr); // Sets the type of the data at |index| bytes in |memory| to |type|. |type| // should be less than 4 bits. -// Returns 0 if successful. Returns -1 if memory is NULL. Returns -2 if the -// index is out of range. Returns -3 if the type is out of range. +// Returns 0 if successful. Returns -1 if the memory pointer is NULL. Returns -2 +// if the type pointer is NULL. Returns -3 if the index is out of range. Returns +// -3 if the type is out of range. int AqvmMemory_SetType(const struct AqvmMemory_Memory* memory, size_t index, uint8_t type); // Gets the type of the data at |index| bytes in |memory|. -// Returns the type that is less than 4 bits (0X0F) if successful. Returns 0x10 -// if the memory is NULL. Returns 0x20 if the index is out of memory range. +// Returns the type that is less than 4 bits (0X0F) if successful. Returns 0x11 +// if the memory pointer is NULL. Returns 0x12 if the type pointer is NULL. +// Returns 0x13 if the index is out of memory range. uint8_t AqvmMemory_GetType(struct AqvmMemory_Memory* memory, size_t index); // Writes the data that |data_ptr| points to of size |size| to the data of at // |index| bytes in |memory|. -// Returns 0 if successful. Returns -1 if the memory is NULL. Returns -2 if the -// index is out of memory range. Returns -3 if the data is NULL. +// Returns 0 if successful. Returns -1 if the memory pointer is NULL. Returns -2 +// if the type pointer is NULL. Returns -3 if the index is out of range. Returns +// -4 if the data pointer is NULL. int AqvmMemory_WriteData(struct AqvmMemory_Memory* memory, size_t index, void* data_ptr, size_t size); diff --git a/aqvm/memory/types.h b/aqvm/memory/types.h index 4e5749a..098b3f1 100644 --- a/aqvm/memory/types.h +++ b/aqvm/memory/types.h @@ -5,8 +5,8 @@ #ifndef AQ_AQVM_MEMORY_TYPES_H_ #define AQ_AQVM_MEMORY_TYPES_H_ -#include #include +#include // 0x00 is NULL type. @@ -23,4 +23,8 @@ typedef uint8_t aqchar; // 0x06 typedef bool aqbool; +// Portions exceeding 0x06 and falling within the range 0x0F are currently +// designated as reserved types. Portions extending beyond 0x0F cannot be +// utilised without exceeding the 4-bit size limit. + #endif \ No newline at end of file