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

Binary file not shown.