Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define RED
- //#define YELLOW
- typedef struct {
- char instr[25];
- unsigned char lgtOfOperand;
- #define NONE 0
- #define REGISTERS 1
- #define MEMORY_WRITE 2
- #define RELATIVE_JUMP 3
- #define ABSOLUTE_JUMP 4
- #define CALL 5
- #define RETURN 6
- #define SP_MODIF 7
- #define JUMP_TO_HL 8
- #define CRASH 9
- #define PREFIX 10
- unsigned char operationType;
- } InstrAttrib;
- char outputFile[] = "output.txt";
- #define INVALID_PTR 0xFFFF
- FILE* ptrRom = NULL;
- FILE* output = NULL;
- InstrAttrib instructions[256] = {{"nop", 0, NONE},
- {"ld bc, $%.2hhX%.2hhX", 2, REGISTERS},
- {"ld (bc), a", 0, MEMORY_WRITE},
- {"inc bc", 0, REGISTERS},
- {"inc b", 0, REGISTERS},
- {"dec b", 0, REGISTERS},
- {"ld b, $%.2hhX", 1, REGISTERS},
- {"rlca", 0, REGISTERS},
- {"ld ($%.2hhX%.2hhX), sp", 2, MEMORY_WRITE},
- {"add hl, bc", 0, REGISTERS},
- {"ld a, (bc)", 0, REGISTERS},
- {"dec bc", 0, REGISTERS},
- {"inc c", 0, REGISTERS},
- {"dec c", 0, REGISTERS},
- {"ld c, $%.2hhX", 1, REGISTERS},
- {"rrca", 0, REGISTERS},
- {"stop", 0, CRASH},
- {"ld de, $%.2hhX%.2hhX", 2, REGISTERS},
- {"ld (de), a", 0, MEMORY_WRITE},
- {"inc de", 0, REGISTERS},
- {"inc d", 0, REGISTERS},
- {"dec d", 0, REGISTERS},
- {"ld d, $%.2hhX", 1, REGISTERS},
- {"rla", 0, REGISTERS},
- {"jr %.2hhX", 1, RELATIVE_JUMP},
- {"add hl, de", 0, REGISTERS},
- {"ld a, (de)", 0, REGISTERS},
- {"dec de", 0, REGISTERS},
- {"inc e", 0, REGISTERS},
- {"dec e", 0, REGISTERS},
- {"ld e, $%.2hhX", 1, REGISTERS},
- {"rra", 0, REGISTERS},
- {"jr nz, %.2hhX", 1, RELATIVE_JUMP},
- {"ld hl, $%.2hhX%.2hhX", 2, REGISTERS},
- {"ldi (hl), a", 0, MEMORY_WRITE},
- {"inc hl", 0, REGISTERS},
- {"inc h", 0, REGISTERS},
- {"dec h", 0, REGISTERS},
- {"ld h, $%.2hhX", 1, REGISTERS},
- {"daa", 0, REGISTERS},
- {"jr z, %.2hhX", 1, RELATIVE_JUMP},
- {"add hl, hl", 0, REGISTERS},
- {"ldi a, (hl)", 0, REGISTERS},
- {"dec hl", 0, REGISTERS},
- {"inc l", 0, REGISTERS},
- {"dec l", 0, REGISTERS},
- {"ld l, $%.2hhX", 1, REGISTERS},
- {"cpl", 0, REGISTERS},
- {"jr nc, %.2hhX", 1, RELATIVE_JUMP},
- {"ld sp, $%.2hhX%.2hhX", 2, SP_MODIF},
- {"ldd (hl), a", 0, MEMORY_WRITE},
- {"inc sp", 0, SP_MODIF},
- {"inc (hl)", 0, MEMORY_WRITE},
- {"dec (hl)", 0, MEMORY_WRITE},
- {"ld (hl), $%.2hhX", 1, MEMORY_WRITE},
- {"scf", 0, NONE},
- {"jr c, %.2hhX", 1, RELATIVE_JUMP},
- {"add hl, sp", 0, REGISTERS},
- {"ldd a, (hl)", 0, REGISTERS},
- {"dec sp", 0, SP_MODIF},
- {"inc a", 0, REGISTERS},
- {"dec a", 0, REGISTERS},
- {"ld a, $%.2hhX", 1, REGISTERS},
- {"ccf", 0, NONE},
- {"ld b, b", 0, REGISTERS},
- {"ld b, c", 0, REGISTERS},
- {"ld b, d", 0, REGISTERS},
- {"ld b, e", 0, REGISTERS},
- {"ld b, h", 0, REGISTERS},
- {"ld b, l", 0, REGISTERS},
- {"ld b, (hl)", 0, REGISTERS},
- {"ld b, a", 0, REGISTERS},
- {"ld c, b", 0, REGISTERS},
- {"ld c, c", 0, REGISTERS},
- {"ld c, d", 0, REGISTERS},
- {"ld c, e", 0, REGISTERS},
- {"ld c, h", 0, REGISTERS},
- {"ld c, l", 0, REGISTERS},
- {"ld c, (hl)", 0, REGISTERS},
- {"ld c, a", 0, REGISTERS},
- {"ld d, b", 0, REGISTERS},
- {"ld d, c", 0, REGISTERS},
- {"ld d, d", 0, REGISTERS},
- {"ld d, e", 0, REGISTERS},
- {"ld d, h", 0, REGISTERS},
- {"ld d, l", 0, REGISTERS},
- {"ld d, (hl)", 0, REGISTERS},
- {"ld d, a", 0, REGISTERS},
- {"ld e, b", 0, REGISTERS},
- {"ld e, c", 0, REGISTERS},
- {"ld e, d", 0, REGISTERS},
- {"ld e, e", 0, REGISTERS},
- {"ld e, h", 0, REGISTERS},
- {"ld e, l", 0, REGISTERS},
- {"ld e, (hl)", 0, REGISTERS},
- {"ld e, a", 0, REGISTERS},
- {"ld h, b", 0, REGISTERS},
- {"ld h, c", 0, REGISTERS},
- {"ld h, d", 0, REGISTERS},
- {"ld h, e", 0, REGISTERS},
- {"ld h, h", 0, REGISTERS},
- {"ld h, l", 0, REGISTERS},
- {"ld h, (hl)", 0, REGISTERS},
- {"ld h, a", 0, REGISTERS},
- {"ld l, b", 0, REGISTERS},
- {"ld l, c", 0, REGISTERS},
- {"ld l, d", 0, REGISTERS},
- {"ld l, e", 0, REGISTERS},
- {"ld l, h", 0, REGISTERS},
- {"ld l, l", 0, REGISTERS},
- {"ld l, (hl)", 0, REGISTERS},
- {"ld l, a", 0, REGISTERS},
- {"ld (hl), b", 0, MEMORY_WRITE},
- {"ld (hl), c", 0, MEMORY_WRITE},
- {"ld (hl), d", 0, MEMORY_WRITE},
- {"ld (hl), e", 0, MEMORY_WRITE},
- {"ld (hl), h", 0, MEMORY_WRITE},
- {"ld (hl), l", 0, MEMORY_WRITE},
- {"halt", 0, NONE},
- {"ld (hl), a", 0, MEMORY_WRITE},
- {"ld a, b", 0, REGISTERS},
- {"ld a, c", 0, REGISTERS},
- {"ld a, d", 0, REGISTERS},
- {"ld a, e", 0, REGISTERS},
- {"ld a, h", 0, REGISTERS},
- {"ld a, l", 0, REGISTERS},
- {"ld a, (hl)", 0, REGISTERS},
- {"ld a, a", 0, REGISTERS},
- {"add a, b", 0, REGISTERS},
- {"add a, c", 0, REGISTERS},
- {"add a, d", 0, REGISTERS},
- {"add a, e", 0, REGISTERS},
- {"add a, h", 0, REGISTERS},
- {"add a, l", 0, REGISTERS},
- {"add a, (hl)", 0, REGISTERS},
- {"add a, a", 0, REGISTERS},
- {"adc a, b", 0, REGISTERS},
- {"adc a, c", 0, REGISTERS},
- {"adc a, d", 0, REGISTERS},
- {"adc a, e", 0, REGISTERS},
- {"adc a, h", 0, REGISTERS},
- {"adc a, l", 0, REGISTERS},
- {"adc a, (hl)", 0, REGISTERS},
- {"adc a, a", 0, REGISTERS},
- {"sub a, b", 0, REGISTERS},
- {"sub a, c", 0, REGISTERS},
- {"sub a, d", 0, REGISTERS},
- {"sub a, e", 0, REGISTERS},
- {"sub a, h", 0, REGISTERS},
- {"sub a, l", 0, REGISTERS},
- {"sub a, (hl)", 0, REGISTERS},
- {"sub a, a", 0, REGISTERS},
- {"sbc a, b", 0, REGISTERS},
- {"sbc a, c", 0, REGISTERS},
- {"sbc a, d", 0, REGISTERS},
- {"sbc a, e", 0, REGISTERS},
- {"sbc a, h", 0, REGISTERS},
- {"sbc a, l", 0, REGISTERS},
- {"sbc a, (hl)", 0, REGISTERS},
- {"sbc a, a", 0, REGISTERS},
- {"and b", 0, REGISTERS},
- {"and c", 0, REGISTERS},
- {"and d", 0, REGISTERS},
- {"and e", 0, REGISTERS},
- {"and h", 0, REGISTERS},
- {"and l", 0, REGISTERS},
- {"and (hl)", 0, REGISTERS},
- {"and a", 0, REGISTERS},
- {"xor b", 0, REGISTERS},
- {"xor c", 0, REGISTERS},
- {"xor d", 0, REGISTERS},
- {"xor e", 0, REGISTERS},
- {"xor h", 0, REGISTERS},
- {"xor l", 0, REGISTERS},
- {"xor (hl)", 0, REGISTERS},
- {"xor a", 0, REGISTERS},
- {"or b", 0, REGISTERS},
- {"or c", 0, REGISTERS},
- {"or d", 0, REGISTERS},
- {"or e", 0, REGISTERS},
- {"or h", 0, REGISTERS},
- {"or l", 0, REGISTERS},
- {"or (hl)", 0, REGISTERS},
- {"or a", 0, REGISTERS},
- {"cp b", 0, REGISTERS},
- {"cp c", 0, REGISTERS},
- {"cp d", 0, REGISTERS},
- {"cp e", 0, REGISTERS},
- {"cp h", 0, REGISTERS},
- {"cp l", 0, REGISTERS},
- {"cp (hl)", 0, REGISTERS},
- {"cp a", 0, REGISTERS},
- {"ret nz", 0, RETURN},
- {"pop bc", 0, SP_MODIF},
- {"jp nz, $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
- {"jp $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
- {"call nz, $%.2hhX%.2hhX", 2, CALL},
- {"push bc", 0, SP_MODIF},
- {"add a, $%.2hhX", 1, REGISTERS},
- {"rst 00h", 0, CRASH},
- {"ret z", 0, RETURN},
- {"ret", 0, RETURN},
- {"jp z, $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
- {"PREFIX", 1, PREFIX},
- {"call z, $%.2hhX%.2hhX", 2, CALL},
- {"call $%.2hhX%.2hhX", 2, CALL},
- {"adc a, $%.2hhX", 1, REGISTERS},
- {"rst 08h", 0, CRASH},
- {"ret nc", 0, RETURN},
- {"pop de", 0, SP_MODIF},
- {"jp nc, $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
- {"INVALID", 0, CRASH},
- {"call nc, $%.2hhX%.2hhX", 2, CALL},
- {"push de", 0, SP_MODIF},
- {"sub $%.2hhX", 0, REGISTERS},
- {"rst 10h", 0, CRASH},
- {"ret c", 0, RETURN},
- {"reti", 0, RETURN},
- {"jp c, $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
- {"INVALID", 0, CRASH},
- {"call c, $%.2hhX%.2hhX", 2, CALL},
- {"INVALID", 0, CRASH},
- {"sbc a, $%.2hhX", 1, REGISTERS},
- {"rst 18h", 0, CRASH},
- {"ld ($FF00+%.2hhX), a", 1, MEMORY_WRITE},
- {"pop hl", 0, SP_MODIF},
- {"ld ($FF00+c), a", 0, MEMORY_WRITE},
- {"INVALID", 0, CRASH},
- {"INVALID", 0, CRASH},
- {"push hl", 0, SP_MODIF},
- {"and $%.2hhX", 1, REGISTERS},
- {"rst 20h", 0, CRASH},
- {"add sp, $%.2hhX", 1, SP_MODIF},
- {"jp (hl)", 0, JUMP_TO_HL},
- {"ld (%.2hhX%.2hhX), a", 2, MEMORY_WRITE},
- {"INVALID", 0, CRASH},
- {"INVALID", 0, CRASH},
- {"INVALID", 0, CRASH},
- {"xor $%.2hhX", 1, REGISTERS},
- {"rst 28h", 0, CRASH},
- {"ld a, ($FF00+%.2hhX)", 1, REGISTERS},
- {"pop af", 0, SP_MODIF},
- {"INVALID", 0, CRASH},
- {"di", 0, NONE},
- {"INVALID", 0, CRASH},
- {"push af", 0, SP_MODIF},
- {"or $%.2hhX", 1, REGISTERS},
- {"rst 30h", 0, CRASH},
- {"ld hl, add sp, $%.2hhX", 1, SP_MODIF},
- {"ld sp, hl", 0, SP_MODIF},
- {"ld a, ($%.2hhX%.2hhX)", 2, REGISTERS},
- {"ei", 0, NONE},
- {"INVALID", 0, CRASH},
- {"INVALID", 0, CRASH},
- {"cp $%.2hhX", 1, REGISTERS},
- {"rst 38h", 0, CRASH}};
- InstrAttrib prefixed[256] = {{"rlc b", 0, REGISTERS},
- {"rlc c", 0, REGISTERS},
- {"rlc d", 0, REGISTERS},
- {"rlc e", 0, REGISTERS},
- {"rlc h", 0, REGISTERS},
- {"rlc l", 0, REGISTERS},
- {"rlc (hl)", 0, MEMORY_WRITE},
- {"rlc a", 0, REGISTERS},
- {"rrc b", 0, REGISTERS},
- {"rrc c", 0, REGISTERS},
- {"rrc d", 0, REGISTERS},
- {"rrc e", 0, REGISTERS},
- {"rrc h", 0, REGISTERS},
- {"rrc l", 0, REGISTERS},
- {"rrc (hl)", 0, MEMORY_WRITE},
- {"rrc a", 0, REGISTERS},
- {"rl b", 0, REGISTERS},
- {"rl c", 0, REGISTERS},
- {"rl d", 0, REGISTERS},
- {"rl e", 0, REGISTERS},
- {"rl h", 0, REGISTERS},
- {"rl l", 0, REGISTERS},
- {"rl (hl)", 0, MEMORY_WRITE},
- {"rl a", 0, REGISTERS},
- {"rr b", 0, REGISTERS},
- {"rr c", 0, REGISTERS},
- {"rr d", 0, REGISTERS},
- {"rr e", 0, REGISTERS},
- {"rr h", 0, REGISTERS},
- {"rr l", 0, REGISTERS},
- {"rr (hl)", 0, MEMORY_WRITE},
- {"rr a", 0, REGISTERS},
- {"sla b", 0, REGISTERS},
- {"sla c", 0, REGISTERS},
- {"sla d", 0, REGISTERS},
- {"sla e", 0, REGISTERS},
- {"sla h", 0, REGISTERS},
- {"sla l", 0, REGISTERS},
- {"sla (hl)", 0, MEMORY_WRITE},
- {"sla a", 0, REGISTERS},
- {"sra b", 0, REGISTERS},
- {"sra c", 0, REGISTERS},
- {"sra d", 0, REGISTERS},
- {"sra e", 0, REGISTERS},
- {"sra h", 0, REGISTERS},
- {"sra l", 0, REGISTERS},
- {"sra (hl)", 0, MEMORY_WRITE},
- {"sra a", 0, REGISTERS},
- {"swap b", 0, REGISTERS},
- {"swap c", 0, REGISTERS},
- {"swap d", 0, REGISTERS},
- {"swap e", 0, REGISTERS},
- {"swap h", 0, REGISTERS},
- {"swap l", 0, REGISTERS},
- {"swap (hl)", 0, MEMORY_WRITE},
- {"swap a", 0, REGISTERS},
- {"srl b", 0, REGISTERS},
- {"srl c", 0, REGISTERS},
- {"srl d", 0, REGISTERS},
- {"srl e", 0, REGISTERS},
- {"srl h", 0, REGISTERS},
- {"srl l", 0, REGISTERS},
- {"srl (hl)", 0, MEMORY_WRITE},
- {"srl a", 0, REGISTERS},
- {"bit 0, b", 0, REGISTERS},
- {"bit 0, c", 0, REGISTERS},
- {"bit 0, d", 0, REGISTERS},
- {"bit 0, e", 0, REGISTERS},
- {"bit 0, h", 0, REGISTERS},
- {"bit 0, l", 0, REGISTERS},
- {"bit 0, (hl)", 0, REGISTERS},
- {"bit 0, a", 0, REGISTERS},
- {"bit 1, b", 0, REGISTERS},
- {"bit 1, c", 0, REGISTERS},
- {"bit 1, d", 0, REGISTERS},
- {"bit 1, e", 0, REGISTERS},
- {"bit 1, h", 0, REGISTERS},
- {"bit 1, l", 0, REGISTERS},
- {"bit 1, (hl)", 0, REGISTERS},
- {"bit 1, a", 0, REGISTERS},
- {"bit 2, b", 0, REGISTERS},
- {"bit 2, c", 0, REGISTERS},
- {"bit 2, d", 0, REGISTERS},
- {"bit 2, e", 0, REGISTERS},
- {"bit 2, h", 0, REGISTERS},
- {"bit 2, l", 0, REGISTERS},
- {"bit 2, (hl)", 0, REGISTERS},
- {"bit 2, a", 0, REGISTERS},
- {"bit 3, b", 0, REGISTERS},
- {"bit 3, c", 0, REGISTERS},
- {"bit 3, d", 0, REGISTERS},
- {"bit 3, e", 0, REGISTERS},
- {"bit 3, h", 0, REGISTERS},
- {"bit 3, l", 0, REGISTERS},
- {"bit 3, (hl)", 0, REGISTERS},
- {"bit 3, a", 0, REGISTERS},
- {"bit 4, b", 0, REGISTERS},
- {"bit 4, c", 0, REGISTERS},
- {"bit 4, d", 0, REGISTERS},
- {"bit 4, e", 0, REGISTERS},
- {"bit 4, h", 0, REGISTERS},
- {"bit 4, l", 0, REGISTERS},
- {"bit 4, (hl)", 0, REGISTERS},
- {"bit 4, a", 0, REGISTERS},
- {"bit 5, b", 0, REGISTERS},
- {"bit 5, c", 0, REGISTERS},
- {"bit 5, d", 0, REGISTERS},
- {"bit 5, e", 0, REGISTERS},
- {"bit 5, h", 0, REGISTERS},
- {"bit 5, l", 0, REGISTERS},
- {"bit 5, (hl)", 0, REGISTERS},
- {"bit 5, a", 0, REGISTERS},
- {"bit 6, b", 0, REGISTERS},
- {"bit 6, c", 0, REGISTERS},
- {"bit 6, d", 0, REGISTERS},
- {"bit 6, e", 0, REGISTERS},
- {"bit 6, h", 0, REGISTERS},
- {"bit 6, l", 0, REGISTERS},
- {"bit 6, (hl)", 0, REGISTERS},
- {"bit 6, a", 0, REGISTERS},
- {"bit 7, b", 0, REGISTERS},
- {"bit 7, c", 0, REGISTERS},
- {"bit 7, d", 0, REGISTERS},
- {"bit 7, e", 0, REGISTERS},
- {"bit 7, h", 0, REGISTERS},
- {"bit 7, l", 0, REGISTERS},
- {"bit 7, (hl)", 0, REGISTERS},
- {"bit 7, a", 0, REGISTERS},
- {"res 0, b", 0, REGISTERS},
- {"res 0, c", 0, REGISTERS},
- {"res 0, d", 0, REGISTERS},
- {"res 0, e", 0, REGISTERS},
- {"res 0, h", 0, REGISTERS},
- {"res 0, l", 0, REGISTERS},
- {"res 0, (hl)", 0, MEMORY_WRITE},
- {"res 0, a", 0, REGISTERS},
- {"res 1, b", 0, REGISTERS},
- {"res 1, c", 0, REGISTERS},
- {"res 1, d", 0, REGISTERS},
- {"res 1, e", 0, REGISTERS},
- {"res 1, h", 0, REGISTERS},
- {"res 1, l", 0, REGISTERS},
- {"res 1, (hl)", 0, MEMORY_WRITE},
- {"res 1, a", 0, REGISTERS},
- {"res 2, b", 0, REGISTERS},
- {"res 2, c", 0, REGISTERS},
- {"res 2, d", 0, REGISTERS},
- {"res 2, e", 0, REGISTERS},
- {"res 2, h", 0, REGISTERS},
- {"res 2, l", 0, REGISTERS},
- {"res 2, (hl)", 0, MEMORY_WRITE},
- {"res 2, a", 0, REGISTERS},
- {"res 3, b", 0, REGISTERS},
- {"res 3, c", 0, REGISTERS},
- {"res 3, d", 0, REGISTERS},
- {"res 3, e", 0, REGISTERS},
- {"res 3, h", 0, REGISTERS},
- {"res 3, l", 0, REGISTERS},
- {"res 3, (hl)", 0, MEMORY_WRITE},
- {"res 3, a", 0, REGISTERS},
- {"res 4, b", 0, REGISTERS},
- {"res 4, c", 0, REGISTERS},
- {"res 4, d", 0, REGISTERS},
- {"res 4, e", 0, REGISTERS},
- {"res 4, h", 0, REGISTERS},
- {"res 4, l", 0, REGISTERS},
- {"res 4, (hl)", 0, MEMORY_WRITE},
- {"res 4, a", 0, REGISTERS},
- {"res 5, b", 0, REGISTERS},
- {"res 5, c", 0, REGISTERS},
- {"res 5, d", 0, REGISTERS},
- {"res 5, e", 0, REGISTERS},
- {"res 5, h", 0, REGISTERS},
- {"res 5, l", 0, REGISTERS},
- {"res 5, (hl)", 0, MEMORY_WRITE},
- {"res 5, a", 0, REGISTERS},
- {"res 6, b", 0, REGISTERS},
- {"res 6, c", 0, REGISTERS},
- {"res 6, d", 0, REGISTERS},
- {"res 6, e", 0, REGISTERS},
- {"res 6, h", 0, REGISTERS},
- {"res 6, l", 0, REGISTERS},
- {"res 6, (hl)", 0, MEMORY_WRITE},
- {"res 6, a", 0, REGISTERS},
- {"res 7, b", 0, REGISTERS},
- {"res 7, c", 0, REGISTERS},
- {"res 7, d", 0, REGISTERS},
- {"res 7, e", 0, REGISTERS},
- {"res 7, h", 0, REGISTERS},
- {"res 7, l", 0, REGISTERS},
- {"res 7, (hl)", 0, MEMORY_WRITE},
- {"res 7, a", 0, REGISTERS},
- {"set 0, b", 0, REGISTERS},
- {"set 0, c", 0, REGISTERS},
- {"set 0, d", 0, REGISTERS},
- {"set 0, e", 0, REGISTERS},
- {"set 0, h", 0, REGISTERS},
- {"set 0, l", 0, REGISTERS},
- {"set 0, (hl)", 0, MEMORY_WRITE},
- {"set 0, a", 0, REGISTERS},
- {"set 1, b", 0, REGISTERS},
- {"set 1, c", 0, REGISTERS},
- {"set 1, d", 0, REGISTERS},
- {"set 1, e", 0, REGISTERS},
- {"set 1, h", 0, REGISTERS},
- {"set 1, l", 0, REGISTERS},
- {"set 1, (hl)", 0, MEMORY_WRITE},
- {"set 1, a", 0, REGISTERS},
- {"set 2, b", 0, REGISTERS},
- {"set 2, c", 0, REGISTERS},
- {"set 2, d", 0, REGISTERS},
- {"set 2, e", 0, REGISTERS},
- {"set 2, h", 0, REGISTERS},
- {"set 2, l", 0, REGISTERS},
- {"set 2, (hl)", 0, MEMORY_WRITE},
- {"set 2, a", 0, REGISTERS},
- {"set 3, b", 0, REGISTERS},
- {"set 3, c", 0, REGISTERS},
- {"set 3, d", 0, REGISTERS},
- {"set 3, e", 0, REGISTERS},
- {"set 3, h", 0, REGISTERS},
- {"set 3, l", 0, REGISTERS},
- {"set 3, (hl)", 0, MEMORY_WRITE},
- {"set 3, a", 0, REGISTERS},
- {"set 4, b", 0, REGISTERS},
- {"set 4, c", 0, REGISTERS},
- {"set 4, d", 0, REGISTERS},
- {"set 4, e", 0, REGISTERS},
- {"set 4, h", 0, REGISTERS},
- {"set 4, l", 0, REGISTERS},
- {"set 4, (hl)", 0, MEMORY_WRITE},
- {"set 4, a", 0, REGISTERS},
- {"set 5, b", 0, REGISTERS},
- {"set 5, c", 0, REGISTERS},
- {"set 5, d", 0, REGISTERS},
- {"set 5, e", 0, REGISTERS},
- {"set 5, h", 0, REGISTERS},
- {"set 5, l", 0, REGISTERS},
- {"set 5, (hl)", 0, MEMORY_WRITE},
- {"set 5, a", 0, REGISTERS},
- {"set 6, b", 0, REGISTERS},
- {"set 6, c", 0, REGISTERS},
- {"set 6, d", 0, REGISTERS},
- {"set 6, e", 0, REGISTERS},
- {"set 6, h", 0, REGISTERS},
- {"set 6, l", 0, REGISTERS},
- {"set 6, (hl)", 0, MEMORY_WRITE},
- {"set 6, a", 0, REGISTERS},
- {"set 7, b", 0, REGISTERS},
- {"set 7, c", 0, REGISTERS},
- {"set 7, d", 0, REGISTERS},
- {"set 7, e", 0, REGISTERS},
- {"set 7, h", 0, REGISTERS},
- {"set 7, l", 0, REGISTERS},
- {"set 7, (hl)", 0, MEMORY_WRITE},
- {"set 7, a", 0, REGISTERS}};
- int setOffsetFromBankedAddr(int bank, int addr) {
- if(addr < 0x4000) {
- return addr;
- } else if(addr < 0x8000) {
- return (bank - 1) * 0x4000 + addr;
- } else {
- return INVALID_PTR;
- }
- }
- int main(int argc, char* argv[]) {
- if(argc != 2) {
- exit(1);
- }
- ptrRom = fopen(argv[1],"r");
- if(ptrRom == NULL) {
- exit(2);
- }
- output = fopen(outputFile, "a");
- if(output == NULL) {
- exit(3);
- }
- fclose(output);
- remove(outputFile);
- output = fopen(outputFile, "a");
- unsigned char bank = 1;
- while(bank < 0x80) {
- unsigned char byte = 0;
- unsigned char operand1 = 0;
- unsigned char operand2 = 0;
- #ifdef RED
- unsigned int addr = 0x58EA;
- #endif
- #ifdef YELLOW
- unsigned int addr = 0x6BD4;
- #endif
- char keepReading = 1;
- fprintf(output, "\nBeginning reading bank %.2hhX\n", bank);
- while(keepReading == 1) {
- fseek(ptrRom, setOffsetFromBankedAddr(bank, addr), SEEK_SET);
- byte = (unsigned char) fgetc(ptrRom);
- int index = byte;
- operand1 = (unsigned char) fgetc(ptrRom);
- if(instructions[index].lgtOfOperand == 2) { // Activate little-endianness
- operand2 = operand1;
- operand1 = (unsigned char) fgetc(ptrRom);
- }
- fprintf(output, "ROM%.2hhX:%.4X\t\t", bank, addr); // Display address
- fprintf(output, instructions[index].instr, operand1, operand2); // Display instruction
- fprintf(output, "\t\t%.2hhX", byte); // Display hex
- if(instructions[index].lgtOfOperand != 0) {
- if(instructions[index].lgtOfOperand != 2) {
- fprintf(output, " %.2hhX", operand1);
- } else {
- fprintf(output, " %.2hhX %.2hhX", operand2, operand1);
- }
- }
- fprintf(output, "\n");
- addr++;
- addr += instructions[index].lgtOfOperand; // Skip reading the operand bytes.
- switch(instructions[index].operationType) {
- case PREFIX:
- fprintf(output, "Instruction : %s\n", prefixed[operand1].instr);
- if(prefixed[operand1].operationType == MEMORY_WRITE) {
- fprintf(output, "Affects (hl).\n");
- }
- break;
- case CRASH:
- fprintf(output, "Crashes / freezes the game.\n");
- keepReading = 0;
- break;
- case RELATIVE_JUMP:
- if(operand1 == 0xFF) {
- fprintf(output, "Jumps into a $FF, crashes.\n");
- keepReading = 0;
- } else if(operand1 > 0x80) {
- fprintf(output, "Jumps backwards, possible infinite loop.\n");
- keepReading = 0;
- } else {
- addr += operand1;
- fprintf(output, "Jumping to %.4X.\n", addr);
- }
- break;
- case NONE:
- if(byte == 118) { // HALT
- fprintf(output, "WARNING : Forces an interruption. Depending on memory corruption, the loaded bank, and stuff, this may crash / softlock.\n");
- }
- break;
- case JUMP_TO_HL:
- fprintf(output, "Jumps to hl.\n");
- keepReading = 0;
- break;
- case CALL: // Same messages. In our case, things aren't much different from jp'ing.
- case ABSOLUTE_JUMP:
- keepReading = 0;
- if(operand1 < 0x80) {
- addr = operand1 * 0x100 + operand2;
- fprintf(output, "Jumping to %.4X.\n", addr);
- keepReading = 1;
- } else if(operand1 < 0xA0) {
- fprintf(output, "Goes to VRAM. High chances of crashing.\n");
- } else if(operand1 < 0xC0) {
- fprintf(output, "Goes to SRAM. If non-enabled, will rst 38h. Otherwise, ACE ..?\n");
- } else if(operand1 < 0xE0) {
- fprintf(output, "Goes to WRAM. MAY trigger ACE !\n");
- keepReading = 0;
- } else if(operand1 < 0xFE) {
- fprintf(output, "High chances of rst 38h'ing.\n");
- } else if(operand1 < 0xFF) {
- if(operand2 < 0x80) {
- fprintf(output, "Extremely high chances of crashing.\n");
- } else {
- fprintf(output, "Slight chances to trigger ACE. Extremely slight.\n");
- }
- }
- break;
- case RETURN:
- fprintf(output, "Returns, probably into oblivion. Or maybe goes back to normal code execution ?\n");
- if(byte == 0xC9) {
- keepReading = 0;
- }
- break;
- case SP_MODIF:
- fprintf(output, "Wrecking the stack ? Low chances to exit this safely.\n");
- break;
- case MEMORY_WRITE:
- fprintf(output, "Memory write.\n");
- switch(instructions[index].lgtOfOperand) {
- case 1:
- if(byte == 0x36) {
- // ld (hl), $xx
- // Unknown location.
- break;
- }
- operand2 = operand1;
- operand1 = 0xFF;
- // Slide into
- case 2:
- fprintf(output, "Destination : $%.2hhX%.2hhX.\n", operand1, operand2);
- if(operand1 < 0x20) {
- fprintf(output, "If a 0x0A is written, unlocks SRAM. Locks it otherwise.\n");
- } else if(operand1 < 0x40) {
- fprintf(output, "Switches bank. Low probability of continuing on this bank, stopping.\n");
- keepReading = 0;
- } else if(operand1 < 0x60) {
- fprintf(output, "SRAM / RTC timer bank switch. Has chances to be harmful, but keeping going.\n");
- } else if(operand1 < 0x80) {
- fprintf(output, "Latch clock write. Low chances of latching data.\n");
- } else if(operand1 < 0xA0) {
- fprintf(output, "VRAM write. May or may not succeed.\n");
- } else if(operand1 < 0xC0) {
- fprintf(output, "SRAM write. If it wasn't enabled, won't succeed. Otherwise, may be harmful.\n");
- } else if(operand1 < 0xE0) {
- fprintf(output, "Write to WRAM. Huge chances to be harmful.\n");
- } else if(operand1 < 0xFE) {
- fprintf(output, "Write to Echo RAM. Equivalent to a write to $%.2hhX%.2hhX.\n", operand1 - 0x20, operand2);
- } else if(operand1 != 0xFF) {
- if(operand2 < 0xA0) {
- fprintf(output, "Write to OAM. Low chances of succeeding.\n");
- } else {
- fprintf(output, "Write is lost.\n");
- }
- } else {
- if(operand2 < 0x80) {
- fprintf(output, "Write to I/O registers. Almost certainly harmful.\n");
- } else if(operand2 != 0xFF) {
- fprintf(output, "Write to HRAM. High chances to be harmful.\n");
- } else {
- fprintf(output, "Changes the Interruption Mask. High chances to be harmful.");
- }
- }
- break;
- case 0:
- fprintf(output, "Unknown target location. Continuing, but has chances to crash / corrupt stuff.\n");
- break;
- }
- break;
- }
- if(addr > 0x7FFF) {
- fprintf(output, "Reaches end of current ROM bank.\n");
- keepReading = 0;
- }
- }
- fprintf(output, "\n");
- bank++;
- }
- fclose(ptrRom);
- exit(0);
- }
Add Comment
Please, Sign In to add comment