From 19900f36ae87a483cb5688e5a746d477b4de928c Mon Sep 17 00:00:00 2001 From: Turingon Date: Wed, 28 Aug 2024 17:34:26 +0200 Subject: [PATCH] Code ready, needs debug --- .vscode/c_cpp_properties.json | 18 ++++++++ .vscode/launch.json | 24 ++++++++++ .vscode/settings.json | 59 +++++++++++++++++++++++++ disassembler.c | 83 ++++++++++++++++++++++++++++++++--- 4 files changed, 179 insertions(+), 5 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..9c23898 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/luxdragon/Documents/QBoy", + "program": "/home/luxdragon/Documents/QBoy/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bb879da --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/disassembler.c b/disassembler.c index 19a2e30..30f3cd3 100644 --- a/disassembler.c +++ b/disassembler.c @@ -1,8 +1,10 @@ #include #include +#include +#include // for easier work with cJSON // How to compile? Like this! -// gcc disassembler.c -o disassembler +// gcc -o disassembler disassembler.c -lcjson // Large chunks of the code are from my Intel 8080 emulator @@ -31,19 +33,81 @@ buffer* read_file(char** argv) { return b; } +cJSON* read_cjson() { + FILE *fs = fopen("Opcodes.json", "rb"); + if (fs = NULL) { + printf("ERROR: Unable to open the Opcodes.json file.\n"); + return NULL; + } + + // read the file contents into a string + fseek(fs, 0L, SEEK_END); + int fsize = ftell(fs); + fseek(fs, 0L, SEEK_SET); + + buffer *b = malloc(sizeof(buffer)); + b->buffer = malloc(fsize); + b->length = fsize; + + fread(b->buffer, fsize, 1, fs); + fclose(fs); + + // parse the JSON data + cJSON *json = cJSON_Parse(b->buffer); + if (json == NULL) { + const char *error_ptr = cJSON_GetErrorPtr(); + if (error_ptr != NULL) { + printf("Error: %s\n", error_ptr); + } + cJSON_Delete(json); + return NULL; + } + free(b); + return json; +} + void print_buffer(buffer* b) { for (int i = 0; i < 40; i++) { printf("%02x \n", b->buffer[i]); } } -int disassemble8080(buffer* b, int pc) { +int disassemble8080(buffer* b, int pc, cJSON* json) { unsigned char *code = &(b->buffer[pc]); int opbytes = 1; printf("%04x ", pc); + char mnemonic[10]; - // Will parse the opcodes.json for this - // To be done after the MMT blog post + // Here begins the cJSON part + + cJSON *unprefixed = cJSON_GetObjectItem(json, "unprefixed"); + if (unprefixed == NULL) { + printf("Error: 'unprefixed' not found in JSON.\n"); + return 0; // We don't want to continue + } + cJSON *command = cJSON_GetObjectItem(unprefixed, code); + if (command == NULL) { + printf("Error: Command with opcode %s not found.\n", opcode); + return 0; + } + cJSON *mnemonic_item = cJSON_GetObjectItem(command, "mnemonic"); + cJSON *bytes_item = cJSON_GetObjectItem(command, "bytes"); + + + if (mnemonic_item != NULL && cJSON_IsString(mnemonic_item)) { + strcpy(mnemonic, mnemonic_item->valuestring); + } + + if (bytes_item != NULL && cJSON_IsNumber(bytes_item)) { + *bytes = bytes_item->valueint; + } + // Here ends the cJSON part + printf("%d\n", opbytes); + printf("%s\n", mnemonic); + + if (opbytes > 5 || opbytes < 1) { + opbytes = 1; + } printf("\n"); return opbytes; } @@ -59,10 +123,19 @@ int main (int argc, char** argv) { return 1; } print_buffer(buff); + JSON *json = read_cjson(); + + if (json == NULL) { + printf("main terminated\n"); + return 1; + } + + while (pc < 400) {//buff->length) { - pc += disassemble8080(buff, pc); + pc += disassemble8080(buff, pc, json); } free(buff); + cJSON_Delete(json); // basically free(json) return 0; } \ No newline at end of file