Code ready, needs debug

This commit is contained in:
Turingon 2024-08-28 17:34:26 +02:00
parent 0b0258802f
commit 19900f36ae
4 changed files with 179 additions and 5 deletions

18
.vscode/c_cpp_properties.json vendored Normal file
View file

@ -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
}

24
.vscode/launch.json vendored Normal file
View file

@ -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
}
]
}
]
}

59
.vscode/settings.json vendored Normal file
View file

@ -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
}

View file

@ -1,8 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <cjson/cJSON.h>
#include <string.h> // 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;
}