Skip to content

Commit

Permalink
Added debug code to the memory part of Aqvm.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Jun 30, 2024
1 parent 998794d commit 7884c2c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
77 changes: 58 additions & 19 deletions aqvm/memory/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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;
}
15 changes: 9 additions & 6 deletions aqvm/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 5 additions & 1 deletion aqvm/memory/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#ifndef AQ_AQVM_MEMORY_TYPES_H_
#define AQ_AQVM_MEMORY_TYPES_H_

#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>

// 0x00 is NULL type.

Expand All @@ -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

0 comments on commit 7884c2c

Please sign in to comment.