perfaware/part1: Catch unknown instructions

This commit is contained in:
doyle 2023-03-06 22:19:08 +11:00 committed by committed-name
parent f5346147e2
commit 954c982856
2 changed files with 69 additions and 54 deletions

View File

@ -31,7 +31,7 @@ S86_Globals s86_globals;
#define S86_STRINGIFY(token) S86_STRINGIFY2(token)
#define S86_ASSERT(expr) \
if (!(expr)) { \
S86_PrintLnFmt("Assertion triggered [file=" __FILE__ ", line=" S86_STRINGIFY(__LINE__) ", expr=" #expr "]"); \
S86_PrintLnFmt("Assertion triggered [file=\"" __FILE__ ":" S86_STRINGIFY(__LINE__) "\", expr=\"" #expr "\"]"); \
__debugbreak(); \
} \
@ -40,11 +40,19 @@ S86_Globals s86_globals;
#define S86_STR8_FMT(string) (int)((string).size), (string).data
#define S86_CAST(Type) (Type)
S86_Buffer S86_ReadFile(char const *file_path);
bool S86_BufferIsValid(S86_Buffer buffer);
S86_Buffer S86_FileRead(char const *file_path);
void S86_FileFree(S86_Buffer buffer);
void S86_PrintLn(S86_Str8 string);
void S86_PrintLnFmt(char const *fmt, ...);
S86_Buffer S86_ReadFile(char const *file_path)
bool S86_BufferIsValid(S86_Buffer buffer)
{
bool result = buffer.data && buffer.size;
return result;
}
S86_Buffer S86_FileRead(char const *file_path)
{
S86_Buffer result = {0};
@ -108,10 +116,10 @@ end:
return result;
};
bool S86_BufferIsValid(S86_Buffer buffer)
void S86_FileFree(S86_Buffer buffer)
{
bool result = buffer.data && buffer.size;
return result;
if (S86_BufferIsValid(buffer))
VirtualFree(buffer.data, 0, MEM_RELEASE);
}
void S86_PrintLn(S86_Str8 string)
@ -242,7 +250,7 @@ int main(int argc, char **argv)
};
char const *file_path = argv[1];
S86_Buffer buffer = S86_ReadFile(file_path);
S86_Buffer buffer = S86_FileRead(file_path);
if (!S86_BufferIsValid(buffer)) {
S86_PrintLnFmt("File read failed [path=\"%s\"]", argv[1], buffer.size);
return -1;
@ -256,16 +264,18 @@ int main(int argc, char **argv)
uint8_t byte1 = (uint8_t)buffer.data[buffer_index + 1];
uint16_t byte01 = (uint16_t)byte0 << 8 | (uint16_t)byte1 << 0;
S86_InstructionType instruction_type = S86_InstructionType_Count;
for (size_t instruction_index = 0;
instruction_index < S86_ARRAY_UCOUNT(S86_INSTRUCTIONS);
instruction_type == S86_InstructionType_Count && instruction_index < S86_ARRAY_UCOUNT(S86_INSTRUCTIONS);
instruction_index++)
{
S86_Instruction instruction = S86_INSTRUCTIONS[instruction_index];
if ((byte01 & instruction.op_mask) != instruction.op_bits)
continue;
if ((byte01 & instruction.op_mask) == instruction.op_bits)
instruction_type = instruction_index;
}
S86_InstructionType type = S86_CAST(S86_InstructionType)instruction_index;
switch (type) {
S86_ASSERT(instruction_type != S86_InstructionType_Count && "Unknown instruction");
switch (instruction_type) {
case S86_InstructionType_MOVRegOrMemToOrFromReg: {
uint8_t d = (byte0 & 0b0000'0010) >> 1;
uint8_t w = (byte0 & 0b0000'0001) >> 0;
@ -299,21 +309,27 @@ int main(int argc, char **argv)
} break;
case S86_InstructionType_MOVImmediateToRegOrMem: {
S86_ASSERT(!"Unhandled instruction");
} break;
case S86_InstructionType_MOVImmediateToReg: {
S86_ASSERT(!"Unhandled instruction");
} break;
case S86_InstructionType_MOVMemToAccum: {
S86_ASSERT(!"Unhandled instruction");
} break;
case S86_InstructionType_MOVAccumToMem: {
S86_ASSERT(!"Unhandled instruction");
} break;
case S86_InstructionType_MOVRegOrMemToSegReg: {
S86_ASSERT(!"Unhandled instruction");
} break;
case S86_InstructionType_MOVSegRegToRegOrMem: {
S86_ASSERT(!"Unhandled instruction");
} break;
default: {
@ -321,5 +337,4 @@ int main(int argc, char **argv)
} break;
}
}
}
}

Binary file not shown.