diff --git a/disassembler b/disassembler index dd6a179..8f52924 100755 Binary files a/disassembler and b/disassembler differ diff --git a/disassembler.c b/disassembler.c index 6dfc525..9b0040e 100644 --- a/disassembler.c +++ b/disassembler.c @@ -85,19 +85,19 @@ int disassemble8080(buffer* b, int pc, cJSON* json) { char opcode_str[5]; sprintf(opcode_str, "0x%02X", *code); - printf("%s\n", opcode_str); + //printf("%s\n", opcode_str); if (strcmp(opcode_str, "0xCB") == 0) { - printf("We have a prefixed command!\n"); - unsigned char *code = &(b->buffer[pc+1]); + pc++; + unsigned char *code = &(b->buffer[pc]); sprintf(opcode_str, "0x%02X", *code); - printf("%s\n", opcode_str); + //printf("%s\n", opcode_str); cJSON *prefixed = cJSON_GetObjectItem(json, "cbprefixed"); - if (unprefixed == NULL) { - printf("Error: 'unprefixed' not found in JSON.\n"); + if (prefixed == NULL) { + printf("Error: 'prefixed' not found in JSON.\n"); return 0; } cJSON *command = cJSON_GetObjectItem(prefixed, opcode_str); @@ -108,10 +108,68 @@ int disassemble8080(buffer* b, int pc, cJSON* json) { cJSON *mnemonic_item = cJSON_GetObjectItem(command, "mnemonic"); cJSON *bytes_item = cJSON_GetObjectItem(command, "bytes"); - printf("Mnemnoic: %s\t", mnemonic); + + if (mnemonic_item != NULL && cJSON_IsString(mnemonic_item)) { + strcpy(mnemonic, mnemonic_item->valuestring); + } + + if (bytes_item != NULL && cJSON_IsNumber(bytes_item)) { + opbytes = bytes_item->valueint; + } + + printf("%s \t", mnemonic); + + // Extract and print operands + int operand_count = 0; + cJSON *operands = cJSON_GetObjectItem(command, "operands"); + + if (operands != NULL && cJSON_IsArray(operands)) { + + operand_count = cJSON_GetArraySize(operands); + //printf("Number of operands: %d\n", operand_count); + for (int i = 0; i < operand_count; i++) { + if (i == 1) { + printf(", "); + } + cJSON *operand = cJSON_GetArrayItem(operands, i); + cJSON *name = cJSON_GetObjectItem(operand, "name"); + cJSON *immediate = cJSON_GetObjectItem(operand, "immediate"); + cJSON *bytes = cJSON_GetObjectItem(operand, "bytes"); + if (immediate != NULL && cJSON_IsBool(immediate)) { + printf("%s", cJSON_IsTrue(immediate) ? "" : "("); + } + if (bytes != NULL && cJSON_IsNumber(bytes)) { + if (bytes->valueint == 1) { + unsigned char *code = &(b->buffer[pc+1]); + sprintf(opcode_str, "0x%02X", *code); + printf("%s", opcode_str); + } + if (bytes->valueint == 2) { + unsigned char *code = &(b->buffer[pc+2]); + sprintf(opcode_str, "0x%02X", *code); + printf("%s", opcode_str); + sprintf(opcode_str, "%02X", *(code-1)); + printf("%s", opcode_str); + } + + } + if (name != NULL && cJSON_IsString(name) && (bytes == NULL)) { + //printf("Operand %d: %s", i + 1, name->valuestring); + printf("%s", name->valuestring); + } + if (bytes != NULL && cJSON_IsNumber(bytes)) { + //printf(", Bytes: %d", bytes->valueint); + } + if (immediate != NULL && cJSON_IsBool(immediate)) { + printf("%s", cJSON_IsTrue(immediate) ? "" : ")"); + //printf(", Immediate: %s", cJSON_IsTrue(immediate) ? "true" : "false"); + } + } + } printf("\n"); + printf(" ^ We have a prefixed command!\n"); return opbytes+1; } @@ -138,7 +196,7 @@ int disassemble8080(buffer* b, int pc, cJSON* json) { opbytes = bytes_item->valueint; } - printf("Mnemnoic: %s\t", mnemonic); + printf("%s \t", mnemonic); // Extract and print operands int operand_count = 0; @@ -147,22 +205,44 @@ int disassemble8080(buffer* b, int pc, cJSON* json) { if (operands != NULL && cJSON_IsArray(operands)) { operand_count = cJSON_GetArraySize(operands); - printf("Number of operands: %d\n", operand_count); + //printf("Number of operands: %d\n", operand_count); for (int i = 0; i < operand_count; i++) { + if (i == 1) { + printf(", "); + } cJSON *operand = cJSON_GetArrayItem(operands, i); cJSON *name = cJSON_GetObjectItem(operand, "name"); cJSON *immediate = cJSON_GetObjectItem(operand, "immediate"); - cJSON *bytes = cJSON_GetObjectItem(operand, "bytes"); - if (name != NULL && cJSON_IsString(name)) { - printf("Operand %d: %s", i + 1, name->valuestring); + cJSON *bytes = cJSON_GetObjectItem(operand, "bytes"); + if (immediate != NULL && cJSON_IsBool(immediate)) { + printf("%s", cJSON_IsTrue(immediate) ? "" : "("); + } + if (bytes != NULL && cJSON_IsNumber(bytes)) { + if (bytes->valueint == 1) { + unsigned char *code = &(b->buffer[pc+1]); + sprintf(opcode_str, "0x%02X", *code); + printf("%s", opcode_str); + } + if (bytes->valueint == 2) { + unsigned char *code = &(b->buffer[pc+2]); + sprintf(opcode_str, "0x%02X", *code); + printf("%s", opcode_str); + sprintf(opcode_str, "%02X", *(code-1)); + printf("%s", opcode_str); + } + + } + if (name != NULL && cJSON_IsString(name) && (bytes == NULL)) { + //printf("Operand %d: %s", i + 1, name->valuestring); + printf("%s", name->valuestring); } if (bytes != NULL && cJSON_IsNumber(bytes)) { - printf(", Bytes: %d", bytes->valueint); + //printf(", Bytes: %d", bytes->valueint); } if (immediate != NULL && cJSON_IsBool(immediate)) { - printf(", Immediate: %s", cJSON_IsTrue(immediate) ? "true" : "false"); + printf("%s", cJSON_IsTrue(immediate) ? "" : ")"); + //printf(", Immediate: %s", cJSON_IsTrue(immediate) ? "true" : "false"); } - printf("\n"); } } diff --git a/snake1.gb b/snake1.gb new file mode 100644 index 0000000..82648a5 Binary files /dev/null and b/snake1.gb differ