ISSOtm

Invalid Sound Bank Bot

Jul 18th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 25.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define RED
  5. //#define YELLOW
  6.  
  7. typedef struct {
  8.     char            instr[25];
  9.     unsigned char   lgtOfOperand;
  10.     #define NONE 0
  11.     #define REGISTERS 1
  12.     #define MEMORY_WRITE 2
  13.     #define RELATIVE_JUMP 3
  14.     #define ABSOLUTE_JUMP 4
  15.     #define CALL 5
  16.     #define RETURN 6
  17.     #define SP_MODIF 7
  18.     #define JUMP_TO_HL 8
  19.     #define CRASH 9
  20.     #define PREFIX 10
  21.     unsigned char   operationType;
  22. } InstrAttrib;
  23.  
  24. char outputFile[] = "output.txt";
  25.  
  26. #define INVALID_PTR 0xFFFF
  27. FILE* ptrRom    = NULL;
  28. FILE* output    = NULL;
  29. InstrAttrib instructions[256] = {{"nop", 0, NONE},
  30.                                 {"ld bc, $%.2hhX%.2hhX", 2, REGISTERS},
  31.                                 {"ld (bc), a", 0, MEMORY_WRITE},
  32.                                 {"inc bc", 0, REGISTERS},
  33.                                 {"inc b", 0, REGISTERS},
  34.                                 {"dec b", 0, REGISTERS},
  35.                                 {"ld b, $%.2hhX", 1, REGISTERS},
  36.                                 {"rlca", 0, REGISTERS},
  37.                                 {"ld ($%.2hhX%.2hhX), sp", 2, MEMORY_WRITE},
  38.                                 {"add hl, bc", 0, REGISTERS},
  39.                                 {"ld a, (bc)", 0, REGISTERS},
  40.                                 {"dec bc", 0, REGISTERS},
  41.                                 {"inc c", 0, REGISTERS},
  42.                                 {"dec c", 0, REGISTERS},
  43.                                 {"ld c, $%.2hhX", 1, REGISTERS},
  44.                                 {"rrca", 0, REGISTERS},
  45.                                 {"stop", 0, CRASH},
  46.                                 {"ld de, $%.2hhX%.2hhX", 2, REGISTERS},
  47.                                 {"ld (de), a", 0, MEMORY_WRITE},
  48.                                 {"inc de", 0, REGISTERS},
  49.                                 {"inc d", 0, REGISTERS},
  50.                                 {"dec d", 0, REGISTERS},
  51.                                 {"ld d, $%.2hhX", 1, REGISTERS},
  52.                                 {"rla", 0, REGISTERS},
  53.                                 {"jr %.2hhX", 1, RELATIVE_JUMP},
  54.                                 {"add hl, de", 0, REGISTERS},
  55.                                 {"ld a, (de)", 0, REGISTERS},
  56.                                 {"dec de", 0, REGISTERS},
  57.                                 {"inc e", 0, REGISTERS},
  58.                                 {"dec e", 0, REGISTERS},
  59.                                 {"ld e, $%.2hhX", 1, REGISTERS},
  60.                                 {"rra", 0, REGISTERS},
  61.                                 {"jr nz, %.2hhX", 1, RELATIVE_JUMP},
  62.                                 {"ld hl, $%.2hhX%.2hhX", 2, REGISTERS},
  63.                                 {"ldi (hl), a", 0, MEMORY_WRITE},
  64.                                 {"inc hl", 0, REGISTERS},
  65.                                 {"inc h", 0, REGISTERS},
  66.                                 {"dec h", 0, REGISTERS},
  67.                                 {"ld h, $%.2hhX", 1, REGISTERS},
  68.                                 {"daa", 0, REGISTERS},
  69.                                 {"jr z, %.2hhX", 1, RELATIVE_JUMP},
  70.                                 {"add hl, hl", 0, REGISTERS},
  71.                                 {"ldi a, (hl)", 0, REGISTERS},
  72.                                 {"dec hl", 0, REGISTERS},
  73.                                 {"inc l", 0, REGISTERS},
  74.                                 {"dec l", 0, REGISTERS},
  75.                                 {"ld l, $%.2hhX", 1, REGISTERS},
  76.                                 {"cpl", 0, REGISTERS},
  77.                                 {"jr nc, %.2hhX", 1, RELATIVE_JUMP},
  78.                                 {"ld sp, $%.2hhX%.2hhX", 2, SP_MODIF},
  79.                                 {"ldd (hl), a", 0, MEMORY_WRITE},
  80.                                 {"inc sp", 0, SP_MODIF},
  81.                                 {"inc (hl)", 0, MEMORY_WRITE},
  82.                                 {"dec (hl)", 0, MEMORY_WRITE},
  83.                                 {"ld (hl), $%.2hhX", 1, MEMORY_WRITE},
  84.                                 {"scf", 0, NONE},
  85.                                 {"jr c, %.2hhX", 1, RELATIVE_JUMP},
  86.                                 {"add hl, sp", 0, REGISTERS},
  87.                                 {"ldd a, (hl)", 0, REGISTERS},
  88.                                 {"dec sp", 0, SP_MODIF},
  89.                                 {"inc a", 0, REGISTERS},
  90.                                 {"dec a", 0, REGISTERS},
  91.                                 {"ld a, $%.2hhX", 1, REGISTERS},
  92.                                 {"ccf", 0, NONE},
  93.                                 {"ld b, b", 0, REGISTERS},
  94.                                 {"ld b, c", 0, REGISTERS},
  95.                                 {"ld b, d", 0, REGISTERS},
  96.                                 {"ld b, e", 0, REGISTERS},
  97.                                 {"ld b, h", 0, REGISTERS},
  98.                                 {"ld b, l", 0, REGISTERS},
  99.                                 {"ld b, (hl)", 0, REGISTERS},
  100.                                 {"ld b, a", 0, REGISTERS},
  101.                                 {"ld c, b", 0, REGISTERS},
  102.                                 {"ld c, c", 0, REGISTERS},
  103.                                 {"ld c, d", 0, REGISTERS},
  104.                                 {"ld c, e", 0, REGISTERS},
  105.                                 {"ld c, h", 0, REGISTERS},
  106.                                 {"ld c, l", 0, REGISTERS},
  107.                                 {"ld c, (hl)", 0, REGISTERS},
  108.                                 {"ld c, a", 0, REGISTERS},
  109.                                 {"ld d, b", 0, REGISTERS},
  110.                                 {"ld d, c", 0, REGISTERS},
  111.                                 {"ld d, d", 0, REGISTERS},
  112.                                 {"ld d, e", 0, REGISTERS},
  113.                                 {"ld d, h", 0, REGISTERS},
  114.                                 {"ld d, l", 0, REGISTERS},
  115.                                 {"ld d, (hl)", 0, REGISTERS},
  116.                                 {"ld d, a", 0, REGISTERS},
  117.                                 {"ld e, b", 0, REGISTERS},
  118.                                 {"ld e, c", 0, REGISTERS},
  119.                                 {"ld e, d", 0, REGISTERS},
  120.                                 {"ld e, e", 0, REGISTERS},
  121.                                 {"ld e, h", 0, REGISTERS},
  122.                                 {"ld e, l", 0, REGISTERS},
  123.                                 {"ld e, (hl)", 0, REGISTERS},
  124.                                 {"ld e, a", 0, REGISTERS},
  125.                                 {"ld h, b", 0, REGISTERS},
  126.                                 {"ld h, c", 0, REGISTERS},
  127.                                 {"ld h, d", 0, REGISTERS},
  128.                                 {"ld h, e", 0, REGISTERS},
  129.                                 {"ld h, h", 0, REGISTERS},
  130.                                 {"ld h, l", 0, REGISTERS},
  131.                                 {"ld h, (hl)", 0, REGISTERS},
  132.                                 {"ld h, a", 0, REGISTERS},
  133.                                 {"ld l, b", 0, REGISTERS},
  134.                                 {"ld l, c", 0, REGISTERS},
  135.                                 {"ld l, d", 0, REGISTERS},
  136.                                 {"ld l, e", 0, REGISTERS},
  137.                                 {"ld l, h", 0, REGISTERS},
  138.                                 {"ld l, l", 0, REGISTERS},
  139.                                 {"ld l, (hl)", 0, REGISTERS},
  140.                                 {"ld l, a", 0, REGISTERS},
  141.                                 {"ld (hl), b", 0, MEMORY_WRITE},
  142.                                 {"ld (hl), c", 0, MEMORY_WRITE},
  143.                                 {"ld (hl), d", 0, MEMORY_WRITE},
  144.                                 {"ld (hl), e", 0, MEMORY_WRITE},
  145.                                 {"ld (hl), h", 0, MEMORY_WRITE},
  146.                                 {"ld (hl), l", 0, MEMORY_WRITE},
  147.                                 {"halt", 0, NONE},
  148.                                 {"ld (hl), a", 0, MEMORY_WRITE},
  149.                                 {"ld a, b", 0, REGISTERS},
  150.                                 {"ld a, c", 0, REGISTERS},
  151.                                 {"ld a, d", 0, REGISTERS},
  152.                                 {"ld a, e", 0, REGISTERS},
  153.                                 {"ld a, h", 0, REGISTERS},
  154.                                 {"ld a, l", 0, REGISTERS},
  155.                                 {"ld a, (hl)", 0, REGISTERS},
  156.                                 {"ld a, a", 0, REGISTERS},
  157.                                 {"add a, b", 0, REGISTERS},
  158.                                 {"add a, c", 0, REGISTERS},
  159.                                 {"add a, d", 0, REGISTERS},
  160.                                 {"add a, e", 0, REGISTERS},
  161.                                 {"add a, h", 0, REGISTERS},
  162.                                 {"add a, l", 0, REGISTERS},
  163.                                 {"add a, (hl)", 0, REGISTERS},
  164.                                 {"add a, a", 0, REGISTERS},
  165.                                 {"adc a, b", 0, REGISTERS},
  166.                                 {"adc a, c", 0, REGISTERS},
  167.                                 {"adc a, d", 0, REGISTERS},
  168.                                 {"adc a, e", 0, REGISTERS},
  169.                                 {"adc a, h", 0, REGISTERS},
  170.                                 {"adc a, l", 0, REGISTERS},
  171.                                 {"adc a, (hl)", 0, REGISTERS},
  172.                                 {"adc a, a", 0, REGISTERS},
  173.                                 {"sub a, b", 0, REGISTERS},
  174.                                 {"sub a, c", 0, REGISTERS},
  175.                                 {"sub a, d", 0, REGISTERS},
  176.                                 {"sub a, e", 0, REGISTERS},
  177.                                 {"sub a, h", 0, REGISTERS},
  178.                                 {"sub a, l", 0, REGISTERS},
  179.                                 {"sub a, (hl)", 0, REGISTERS},
  180.                                 {"sub a, a", 0, REGISTERS},
  181.                                 {"sbc a, b", 0, REGISTERS},
  182.                                 {"sbc a, c", 0, REGISTERS},
  183.                                 {"sbc a, d", 0, REGISTERS},
  184.                                 {"sbc a, e", 0, REGISTERS},
  185.                                 {"sbc a, h", 0, REGISTERS},
  186.                                 {"sbc a, l", 0, REGISTERS},
  187.                                 {"sbc a, (hl)", 0, REGISTERS},
  188.                                 {"sbc a, a", 0, REGISTERS},
  189.                                 {"and b", 0, REGISTERS},
  190.                                 {"and c", 0, REGISTERS},
  191.                                 {"and d", 0, REGISTERS},
  192.                                 {"and e", 0, REGISTERS},
  193.                                 {"and h", 0, REGISTERS},
  194.                                 {"and l", 0, REGISTERS},
  195.                                 {"and (hl)", 0, REGISTERS},
  196.                                 {"and a", 0, REGISTERS},
  197.                                 {"xor b", 0, REGISTERS},
  198.                                 {"xor c", 0, REGISTERS},
  199.                                 {"xor d", 0, REGISTERS},
  200.                                 {"xor e", 0, REGISTERS},
  201.                                 {"xor h", 0, REGISTERS},
  202.                                 {"xor l", 0, REGISTERS},
  203.                                 {"xor (hl)", 0, REGISTERS},
  204.                                 {"xor a", 0, REGISTERS},
  205.                                 {"or b", 0, REGISTERS},
  206.                                 {"or c", 0, REGISTERS},
  207.                                 {"or d", 0, REGISTERS},
  208.                                 {"or e", 0, REGISTERS},
  209.                                 {"or h", 0, REGISTERS},
  210.                                 {"or l", 0, REGISTERS},
  211.                                 {"or (hl)", 0, REGISTERS},
  212.                                 {"or a", 0, REGISTERS},
  213.                                 {"cp b", 0, REGISTERS},
  214.                                 {"cp c", 0, REGISTERS},
  215.                                 {"cp d", 0, REGISTERS},
  216.                                 {"cp e", 0, REGISTERS},
  217.                                 {"cp h", 0, REGISTERS},
  218.                                 {"cp l", 0, REGISTERS},
  219.                                 {"cp (hl)", 0, REGISTERS},
  220.                                 {"cp a", 0, REGISTERS},
  221.                                 {"ret nz", 0, RETURN},
  222.                                 {"pop bc", 0, SP_MODIF},
  223.                                 {"jp nz, $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
  224.                                 {"jp $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
  225.                                 {"call nz, $%.2hhX%.2hhX", 2, CALL},
  226.                                 {"push bc", 0, SP_MODIF},
  227.                                 {"add a, $%.2hhX", 1, REGISTERS},
  228.                                 {"rst 00h", 0, CRASH},
  229.                                 {"ret z", 0, RETURN},
  230.                                 {"ret", 0, RETURN},
  231.                                 {"jp z, $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
  232.                                 {"PREFIX", 1, PREFIX},
  233.                                 {"call z, $%.2hhX%.2hhX", 2, CALL},
  234.                                 {"call $%.2hhX%.2hhX", 2, CALL},
  235.                                 {"adc a, $%.2hhX", 1, REGISTERS},
  236.                                 {"rst 08h", 0, CRASH},
  237.                                 {"ret nc", 0, RETURN},
  238.                                 {"pop de", 0, SP_MODIF},
  239.                                 {"jp nc, $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
  240.                                 {"INVALID", 0, CRASH},
  241.                                 {"call nc, $%.2hhX%.2hhX", 2, CALL},
  242.                                 {"push de", 0, SP_MODIF},
  243.                                 {"sub $%.2hhX", 0, REGISTERS},
  244.                                 {"rst 10h", 0, CRASH},
  245.                                 {"ret c", 0, RETURN},
  246.                                 {"reti", 0, RETURN},
  247.                                 {"jp c, $%.2hhX%.2hhX", 2, ABSOLUTE_JUMP},
  248.                                 {"INVALID", 0, CRASH},
  249.                                 {"call c, $%.2hhX%.2hhX", 2, CALL},
  250.                                 {"INVALID", 0, CRASH},
  251.                                 {"sbc a, $%.2hhX", 1, REGISTERS},
  252.                                 {"rst 18h", 0, CRASH},
  253.                                 {"ld ($FF00+%.2hhX), a", 1, MEMORY_WRITE},
  254.                                 {"pop hl", 0, SP_MODIF},
  255.                                 {"ld ($FF00+c), a", 0, MEMORY_WRITE},
  256.                                 {"INVALID", 0, CRASH},
  257.                                 {"INVALID", 0, CRASH},
  258.                                 {"push hl", 0, SP_MODIF},
  259.                                 {"and $%.2hhX", 1, REGISTERS},
  260.                                 {"rst 20h", 0, CRASH},
  261.                                 {"add sp, $%.2hhX", 1, SP_MODIF},
  262.                                 {"jp (hl)", 0, JUMP_TO_HL},
  263.                                 {"ld (%.2hhX%.2hhX), a", 2, MEMORY_WRITE},
  264.                                 {"INVALID", 0, CRASH},
  265.                                 {"INVALID", 0, CRASH},
  266.                                 {"INVALID", 0, CRASH},
  267.                                 {"xor $%.2hhX", 1, REGISTERS},
  268.                                 {"rst 28h", 0, CRASH},
  269.                                 {"ld a, ($FF00+%.2hhX)", 1, REGISTERS},
  270.                                 {"pop af", 0, SP_MODIF},
  271.                                 {"INVALID", 0, CRASH},
  272.                                 {"di", 0, NONE},
  273.                                 {"INVALID", 0, CRASH},
  274.                                 {"push af", 0, SP_MODIF},
  275.                                 {"or $%.2hhX", 1, REGISTERS},
  276.                                 {"rst 30h", 0, CRASH},
  277.                                 {"ld hl, add sp, $%.2hhX", 1, SP_MODIF},
  278.                                 {"ld sp, hl", 0, SP_MODIF},
  279.                                 {"ld a, ($%.2hhX%.2hhX)", 2, REGISTERS},
  280.                                 {"ei", 0, NONE},
  281.                                 {"INVALID", 0, CRASH},
  282.                                 {"INVALID", 0, CRASH},
  283.                                 {"cp $%.2hhX", 1, REGISTERS},
  284.                                 {"rst 38h", 0, CRASH}};
  285.  
  286. InstrAttrib prefixed[256] =    {{"rlc b", 0, REGISTERS},
  287.                                 {"rlc c", 0, REGISTERS},
  288.                                 {"rlc d", 0, REGISTERS},
  289.                                 {"rlc e", 0, REGISTERS},
  290.                                 {"rlc h", 0, REGISTERS},
  291.                                 {"rlc l", 0, REGISTERS},
  292.                                 {"rlc (hl)", 0, MEMORY_WRITE},
  293.                                 {"rlc a", 0, REGISTERS},
  294.                                 {"rrc b", 0, REGISTERS},
  295.                                 {"rrc c", 0, REGISTERS},
  296.                                 {"rrc d", 0, REGISTERS},
  297.                                 {"rrc e", 0, REGISTERS},
  298.                                 {"rrc h", 0, REGISTERS},
  299.                                 {"rrc l", 0, REGISTERS},
  300.                                 {"rrc (hl)", 0, MEMORY_WRITE},
  301.                                 {"rrc a", 0, REGISTERS},
  302.                                 {"rl b", 0, REGISTERS},
  303.                                 {"rl c", 0, REGISTERS},
  304.                                 {"rl d", 0, REGISTERS},
  305.                                 {"rl e", 0, REGISTERS},
  306.                                 {"rl h", 0, REGISTERS},
  307.                                 {"rl l", 0, REGISTERS},
  308.                                 {"rl (hl)", 0, MEMORY_WRITE},
  309.                                 {"rl a", 0, REGISTERS},
  310.                                 {"rr b", 0, REGISTERS},
  311.                                 {"rr c", 0, REGISTERS},
  312.                                 {"rr d", 0, REGISTERS},
  313.                                 {"rr e", 0, REGISTERS},
  314.                                 {"rr h", 0, REGISTERS},
  315.                                 {"rr l", 0, REGISTERS},
  316.                                 {"rr (hl)", 0, MEMORY_WRITE},
  317.                                 {"rr a", 0, REGISTERS},
  318.                                 {"sla b", 0, REGISTERS},
  319.                                 {"sla c", 0, REGISTERS},
  320.                                 {"sla d", 0, REGISTERS},
  321.                                 {"sla e", 0, REGISTERS},
  322.                                 {"sla h", 0, REGISTERS},
  323.                                 {"sla l", 0, REGISTERS},
  324.                                 {"sla (hl)", 0, MEMORY_WRITE},
  325.                                 {"sla a", 0, REGISTERS},
  326.                                 {"sra b", 0, REGISTERS},
  327.                                 {"sra c", 0, REGISTERS},
  328.                                 {"sra d", 0, REGISTERS},
  329.                                 {"sra e", 0, REGISTERS},
  330.                                 {"sra h", 0, REGISTERS},
  331.                                 {"sra l", 0, REGISTERS},
  332.                                 {"sra (hl)", 0, MEMORY_WRITE},
  333.                                 {"sra a", 0, REGISTERS},
  334.                                 {"swap b", 0, REGISTERS},
  335.                                 {"swap c", 0, REGISTERS},
  336.                                 {"swap d", 0, REGISTERS},
  337.                                 {"swap e", 0, REGISTERS},
  338.                                 {"swap h", 0, REGISTERS},
  339.                                 {"swap l", 0, REGISTERS},
  340.                                 {"swap (hl)", 0, MEMORY_WRITE},
  341.                                 {"swap a", 0, REGISTERS},
  342.                                 {"srl b", 0, REGISTERS},
  343.                                 {"srl c", 0, REGISTERS},
  344.                                 {"srl d", 0, REGISTERS},
  345.                                 {"srl e", 0, REGISTERS},
  346.                                 {"srl h", 0, REGISTERS},
  347.                                 {"srl l", 0, REGISTERS},
  348.                                 {"srl (hl)", 0, MEMORY_WRITE},
  349.                                 {"srl a", 0, REGISTERS},
  350.                                 {"bit 0, b", 0, REGISTERS},
  351.                                 {"bit 0, c", 0, REGISTERS},
  352.                                 {"bit 0, d", 0, REGISTERS},
  353.                                 {"bit 0, e", 0, REGISTERS},
  354.                                 {"bit 0, h", 0, REGISTERS},
  355.                                 {"bit 0, l", 0, REGISTERS},
  356.                                 {"bit 0, (hl)", 0, REGISTERS},
  357.                                 {"bit 0, a", 0, REGISTERS},
  358.                                 {"bit 1, b", 0, REGISTERS},
  359.                                 {"bit 1, c", 0, REGISTERS},
  360.                                 {"bit 1, d", 0, REGISTERS},
  361.                                 {"bit 1, e", 0, REGISTERS},
  362.                                 {"bit 1, h", 0, REGISTERS},
  363.                                 {"bit 1, l", 0, REGISTERS},
  364.                                 {"bit 1, (hl)", 0, REGISTERS},
  365.                                 {"bit 1, a", 0, REGISTERS},
  366.                                 {"bit 2, b", 0, REGISTERS},
  367.                                 {"bit 2, c", 0, REGISTERS},
  368.                                 {"bit 2, d", 0, REGISTERS},
  369.                                 {"bit 2, e", 0, REGISTERS},
  370.                                 {"bit 2, h", 0, REGISTERS},
  371.                                 {"bit 2, l", 0, REGISTERS},
  372.                                 {"bit 2, (hl)", 0, REGISTERS},
  373.                                 {"bit 2, a", 0, REGISTERS},
  374.                                 {"bit 3, b", 0, REGISTERS},
  375.                                 {"bit 3, c", 0, REGISTERS},
  376.                                 {"bit 3, d", 0, REGISTERS},
  377.                                 {"bit 3, e", 0, REGISTERS},
  378.                                 {"bit 3, h", 0, REGISTERS},
  379.                                 {"bit 3, l", 0, REGISTERS},
  380.                                 {"bit 3, (hl)", 0, REGISTERS},
  381.                                 {"bit 3, a", 0, REGISTERS},
  382.                                 {"bit 4, b", 0, REGISTERS},
  383.                                 {"bit 4, c", 0, REGISTERS},
  384.                                 {"bit 4, d", 0, REGISTERS},
  385.                                 {"bit 4, e", 0, REGISTERS},
  386.                                 {"bit 4, h", 0, REGISTERS},
  387.                                 {"bit 4, l", 0, REGISTERS},
  388.                                 {"bit 4, (hl)", 0, REGISTERS},
  389.                                 {"bit 4, a", 0, REGISTERS},
  390.                                 {"bit 5, b", 0, REGISTERS},
  391.                                 {"bit 5, c", 0, REGISTERS},
  392.                                 {"bit 5, d", 0, REGISTERS},
  393.                                 {"bit 5, e", 0, REGISTERS},
  394.                                 {"bit 5, h", 0, REGISTERS},
  395.                                 {"bit 5, l", 0, REGISTERS},
  396.                                 {"bit 5, (hl)", 0, REGISTERS},
  397.                                 {"bit 5, a", 0, REGISTERS},
  398.                                 {"bit 6, b", 0, REGISTERS},
  399.                                 {"bit 6, c", 0, REGISTERS},
  400.                                 {"bit 6, d", 0, REGISTERS},
  401.                                 {"bit 6, e", 0, REGISTERS},
  402.                                 {"bit 6, h", 0, REGISTERS},
  403.                                 {"bit 6, l", 0, REGISTERS},
  404.                                 {"bit 6, (hl)", 0, REGISTERS},
  405.                                 {"bit 6, a", 0, REGISTERS},
  406.                                 {"bit 7, b", 0, REGISTERS},
  407.                                 {"bit 7, c", 0, REGISTERS},
  408.                                 {"bit 7, d", 0, REGISTERS},
  409.                                 {"bit 7, e", 0, REGISTERS},
  410.                                 {"bit 7, h", 0, REGISTERS},
  411.                                 {"bit 7, l", 0, REGISTERS},
  412.                                 {"bit 7, (hl)", 0, REGISTERS},
  413.                                 {"bit 7, a", 0, REGISTERS},
  414.                                 {"res 0, b", 0, REGISTERS},
  415.                                 {"res 0, c", 0, REGISTERS},
  416.                                 {"res 0, d", 0, REGISTERS},
  417.                                 {"res 0, e", 0, REGISTERS},
  418.                                 {"res 0, h", 0, REGISTERS},
  419.                                 {"res 0, l", 0, REGISTERS},
  420.                                 {"res 0, (hl)", 0, MEMORY_WRITE},
  421.                                 {"res 0, a", 0, REGISTERS},
  422.                                 {"res 1, b", 0, REGISTERS},
  423.                                 {"res 1, c", 0, REGISTERS},
  424.                                 {"res 1, d", 0, REGISTERS},
  425.                                 {"res 1, e", 0, REGISTERS},
  426.                                 {"res 1, h", 0, REGISTERS},
  427.                                 {"res 1, l", 0, REGISTERS},
  428.                                 {"res 1, (hl)", 0, MEMORY_WRITE},
  429.                                 {"res 1, a", 0, REGISTERS},
  430.                                 {"res 2, b", 0, REGISTERS},
  431.                                 {"res 2, c", 0, REGISTERS},
  432.                                 {"res 2, d", 0, REGISTERS},
  433.                                 {"res 2, e", 0, REGISTERS},
  434.                                 {"res 2, h", 0, REGISTERS},
  435.                                 {"res 2, l", 0, REGISTERS},
  436.                                 {"res 2, (hl)", 0, MEMORY_WRITE},
  437.                                 {"res 2, a", 0, REGISTERS},
  438.                                 {"res 3, b", 0, REGISTERS},
  439.                                 {"res 3, c", 0, REGISTERS},
  440.                                 {"res 3, d", 0, REGISTERS},
  441.                                 {"res 3, e", 0, REGISTERS},
  442.                                 {"res 3, h", 0, REGISTERS},
  443.                                 {"res 3, l", 0, REGISTERS},
  444.                                 {"res 3, (hl)", 0, MEMORY_WRITE},
  445.                                 {"res 3, a", 0, REGISTERS},
  446.                                 {"res 4, b", 0, REGISTERS},
  447.                                 {"res 4, c", 0, REGISTERS},
  448.                                 {"res 4, d", 0, REGISTERS},
  449.                                 {"res 4, e", 0, REGISTERS},
  450.                                 {"res 4, h", 0, REGISTERS},
  451.                                 {"res 4, l", 0, REGISTERS},
  452.                                 {"res 4, (hl)", 0, MEMORY_WRITE},
  453.                                 {"res 4, a", 0, REGISTERS},
  454.                                 {"res 5, b", 0, REGISTERS},
  455.                                 {"res 5, c", 0, REGISTERS},
  456.                                 {"res 5, d", 0, REGISTERS},
  457.                                 {"res 5, e", 0, REGISTERS},
  458.                                 {"res 5, h", 0, REGISTERS},
  459.                                 {"res 5, l", 0, REGISTERS},
  460.                                 {"res 5, (hl)", 0, MEMORY_WRITE},
  461.                                 {"res 5, a", 0, REGISTERS},
  462.                                 {"res 6, b", 0, REGISTERS},
  463.                                 {"res 6, c", 0, REGISTERS},
  464.                                 {"res 6, d", 0, REGISTERS},
  465.                                 {"res 6, e", 0, REGISTERS},
  466.                                 {"res 6, h", 0, REGISTERS},
  467.                                 {"res 6, l", 0, REGISTERS},
  468.                                 {"res 6, (hl)", 0, MEMORY_WRITE},
  469.                                 {"res 6, a", 0, REGISTERS},
  470.                                 {"res 7, b", 0, REGISTERS},
  471.                                 {"res 7, c", 0, REGISTERS},
  472.                                 {"res 7, d", 0, REGISTERS},
  473.                                 {"res 7, e", 0, REGISTERS},
  474.                                 {"res 7, h", 0, REGISTERS},
  475.                                 {"res 7, l", 0, REGISTERS},
  476.                                 {"res 7, (hl)", 0, MEMORY_WRITE},
  477.                                 {"res 7, a", 0, REGISTERS},
  478.                                 {"set 0, b", 0, REGISTERS},
  479.                                 {"set 0, c", 0, REGISTERS},
  480.                                 {"set 0, d", 0, REGISTERS},
  481.                                 {"set 0, e", 0, REGISTERS},
  482.                                 {"set 0, h", 0, REGISTERS},
  483.                                 {"set 0, l", 0, REGISTERS},
  484.                                 {"set 0, (hl)", 0, MEMORY_WRITE},
  485.                                 {"set 0, a", 0, REGISTERS},
  486.                                 {"set 1, b", 0, REGISTERS},
  487.                                 {"set 1, c", 0, REGISTERS},
  488.                                 {"set 1, d", 0, REGISTERS},
  489.                                 {"set 1, e", 0, REGISTERS},
  490.                                 {"set 1, h", 0, REGISTERS},
  491.                                 {"set 1, l", 0, REGISTERS},
  492.                                 {"set 1, (hl)", 0, MEMORY_WRITE},
  493.                                 {"set 1, a", 0, REGISTERS},
  494.                                 {"set 2, b", 0, REGISTERS},
  495.                                 {"set 2, c", 0, REGISTERS},
  496.                                 {"set 2, d", 0, REGISTERS},
  497.                                 {"set 2, e", 0, REGISTERS},
  498.                                 {"set 2, h", 0, REGISTERS},
  499.                                 {"set 2, l", 0, REGISTERS},
  500.                                 {"set 2, (hl)", 0, MEMORY_WRITE},
  501.                                 {"set 2, a", 0, REGISTERS},
  502.                                 {"set 3, b", 0, REGISTERS},
  503.                                 {"set 3, c", 0, REGISTERS},
  504.                                 {"set 3, d", 0, REGISTERS},
  505.                                 {"set 3, e", 0, REGISTERS},
  506.                                 {"set 3, h", 0, REGISTERS},
  507.                                 {"set 3, l", 0, REGISTERS},
  508.                                 {"set 3, (hl)", 0, MEMORY_WRITE},
  509.                                 {"set 3, a", 0, REGISTERS},
  510.                                 {"set 4, b", 0, REGISTERS},
  511.                                 {"set 4, c", 0, REGISTERS},
  512.                                 {"set 4, d", 0, REGISTERS},
  513.                                 {"set 4, e", 0, REGISTERS},
  514.                                 {"set 4, h", 0, REGISTERS},
  515.                                 {"set 4, l", 0, REGISTERS},
  516.                                 {"set 4, (hl)", 0, MEMORY_WRITE},
  517.                                 {"set 4, a", 0, REGISTERS},
  518.                                 {"set 5, b", 0, REGISTERS},
  519.                                 {"set 5, c", 0, REGISTERS},
  520.                                 {"set 5, d", 0, REGISTERS},
  521.                                 {"set 5, e", 0, REGISTERS},
  522.                                 {"set 5, h", 0, REGISTERS},
  523.                                 {"set 5, l", 0, REGISTERS},
  524.                                 {"set 5, (hl)", 0, MEMORY_WRITE},
  525.                                 {"set 5, a", 0, REGISTERS},
  526.                                 {"set 6, b", 0, REGISTERS},
  527.                                 {"set 6, c", 0, REGISTERS},
  528.                                 {"set 6, d", 0, REGISTERS},
  529.                                 {"set 6, e", 0, REGISTERS},
  530.                                 {"set 6, h", 0, REGISTERS},
  531.                                 {"set 6, l", 0, REGISTERS},
  532.                                 {"set 6, (hl)", 0, MEMORY_WRITE},
  533.                                 {"set 6, a", 0, REGISTERS},
  534.                                 {"set 7, b", 0, REGISTERS},
  535.                                 {"set 7, c", 0, REGISTERS},
  536.                                 {"set 7, d", 0, REGISTERS},
  537.                                 {"set 7, e", 0, REGISTERS},
  538.                                 {"set 7, h", 0, REGISTERS},
  539.                                 {"set 7, l", 0, REGISTERS},
  540.                                 {"set 7, (hl)", 0, MEMORY_WRITE},
  541.                                 {"set 7, a", 0, REGISTERS}};
  542.  
  543.  
  544. int setOffsetFromBankedAddr(int bank, int addr) {
  545.     if(addr < 0x4000) {
  546.         return addr;
  547.     } else if(addr < 0x8000) {
  548.         return (bank - 1) * 0x4000 + addr;
  549.     } else {
  550.         return INVALID_PTR;
  551.     }
  552. }
  553.  
  554. int main(int argc, char* argv[]) {
  555.     if(argc != 2) {
  556.         exit(1);
  557.     }
  558.    
  559.     ptrRom = fopen(argv[1],"r");
  560.     if(ptrRom == NULL) {
  561.         exit(2);
  562.     }
  563.    
  564.     output = fopen(outputFile, "a");
  565.     if(output == NULL) {
  566.         exit(3);
  567.     }
  568.     fclose(output);
  569.     remove(outputFile);
  570.     output = fopen(outputFile, "a");
  571.    
  572.     unsigned char bank = 1;
  573.     while(bank < 0x80) {
  574.         unsigned char byte = 0;
  575.         unsigned char operand1 = 0;
  576.         unsigned char operand2 = 0;
  577. #ifdef RED
  578.         unsigned int addr = 0x58EA;
  579. #endif
  580. #ifdef YELLOW
  581.         unsigned int addr = 0x6BD4;
  582. #endif
  583.         char keepReading = 1;
  584.        
  585.         fprintf(output, "\nBeginning reading bank %.2hhX\n", bank);
  586.         while(keepReading == 1) {
  587.             fseek(ptrRom, setOffsetFromBankedAddr(bank, addr), SEEK_SET);
  588.             byte = (unsigned char) fgetc(ptrRom);
  589.             int index = byte;
  590.             operand1 = (unsigned char) fgetc(ptrRom);
  591.             if(instructions[index].lgtOfOperand == 2) { // Activate little-endianness
  592.                 operand2 = operand1;
  593.                 operand1 = (unsigned char) fgetc(ptrRom);
  594.             }
  595.            
  596.             fprintf(output, "ROM%.2hhX:%.4X\t\t", bank, addr); // Display address
  597.             fprintf(output, instructions[index].instr, operand1, operand2); // Display instruction
  598.             fprintf(output, "\t\t%.2hhX", byte); // Display hex
  599.             if(instructions[index].lgtOfOperand != 0) {
  600.                 if(instructions[index].lgtOfOperand != 2) {
  601.                     fprintf(output, " %.2hhX", operand1);
  602.                 } else {
  603.                     fprintf(output, " %.2hhX %.2hhX", operand2, operand1);
  604.                 }
  605.             }
  606.             fprintf(output, "\n");
  607.            
  608.             addr++;
  609.             addr += instructions[index].lgtOfOperand; // Skip reading the operand bytes.
  610.            
  611.             switch(instructions[index].operationType) {
  612.                 case PREFIX:
  613.                     fprintf(output, "Instruction : %s\n", prefixed[operand1].instr);
  614.                     if(prefixed[operand1].operationType == MEMORY_WRITE) {
  615.                         fprintf(output, "Affects (hl).\n");
  616.                     }
  617.                 break;
  618.                 case CRASH:
  619.                     fprintf(output, "Crashes / freezes the game.\n");
  620.                     keepReading = 0;
  621.                 break;
  622.                 case RELATIVE_JUMP:
  623.                     if(operand1 == 0xFF) {
  624.                         fprintf(output, "Jumps into a $FF, crashes.\n");
  625.                         keepReading = 0;
  626.                     } else if(operand1 > 0x80) {
  627.                         fprintf(output, "Jumps backwards, possible infinite loop.\n");
  628.                         keepReading = 0;
  629.                     } else {
  630.                         addr += operand1;
  631.                         fprintf(output, "Jumping to %.4X.\n", addr);
  632.                     }
  633.                 break;
  634.                 case NONE:
  635.                     if(byte == 118) { // HALT
  636.                         fprintf(output, "WARNING : Forces an interruption. Depending on memory corruption, the loaded bank, and stuff, this may crash / softlock.\n");
  637.                     }
  638.                 break;
  639.                 case JUMP_TO_HL:
  640.                     fprintf(output, "Jumps to hl.\n");
  641.                     keepReading = 0;
  642.                 break;
  643.                 case CALL: // Same messages. In our case, things aren't much different from jp'ing.
  644.                 case ABSOLUTE_JUMP:
  645.                     keepReading = 0;
  646.                     if(operand1 < 0x80) {
  647.                         addr = operand1 * 0x100 + operand2;
  648.                         fprintf(output, "Jumping to %.4X.\n", addr);
  649.                         keepReading = 1;
  650.                     } else if(operand1 < 0xA0) {
  651.                         fprintf(output, "Goes to VRAM. High chances of crashing.\n");
  652.                     } else if(operand1 < 0xC0) {
  653.                         fprintf(output, "Goes to SRAM. If non-enabled, will rst 38h. Otherwise, ACE ..?\n");
  654.                     } else if(operand1 < 0xE0) {
  655.                         fprintf(output, "Goes to WRAM. MAY trigger ACE !\n");
  656.                         keepReading = 0;
  657.                     } else if(operand1 < 0xFE) {
  658.                         fprintf(output, "High chances of rst 38h'ing.\n");
  659.                     } else if(operand1 < 0xFF) {
  660.                         if(operand2 < 0x80) {
  661.                             fprintf(output, "Extremely high chances of crashing.\n");
  662.                         } else {
  663.                             fprintf(output, "Slight chances to trigger ACE. Extremely slight.\n");
  664.                         }
  665.                     }
  666.                 break;
  667.                 case RETURN:
  668.                     fprintf(output, "Returns, probably into oblivion. Or maybe goes back to normal code execution ?\n");
  669.                     if(byte == 0xC9) {
  670.                         keepReading = 0;
  671.                     }
  672.                 break;
  673.                 case SP_MODIF:
  674.                     fprintf(output, "Wrecking the stack ? Low chances to exit this safely.\n");
  675.                 break;
  676.                 case MEMORY_WRITE:
  677.                     fprintf(output, "Memory write.\n");
  678.                     switch(instructions[index].lgtOfOperand) {
  679.                         case 1:
  680.                             if(byte == 0x36) {
  681.                                 // ld (hl), $xx
  682.                                 // Unknown location.
  683.                                 break;
  684.                             }
  685.                             operand2 = operand1;
  686.                             operand1 = 0xFF;
  687.                             // Slide into
  688.                         case 2:
  689.                             fprintf(output, "Destination : $%.2hhX%.2hhX.\n", operand1, operand2);
  690.                             if(operand1 < 0x20) {
  691.                                 fprintf(output, "If a 0x0A is written, unlocks SRAM. Locks it otherwise.\n");
  692.                             } else if(operand1 < 0x40) {
  693.                                 fprintf(output, "Switches bank. Low probability of continuing on this bank, stopping.\n");
  694.                                 keepReading = 0;
  695.                             } else if(operand1 < 0x60) {
  696.                                 fprintf(output, "SRAM / RTC timer bank switch. Has chances to be harmful, but keeping going.\n");
  697.                             } else if(operand1 < 0x80) {
  698.                                 fprintf(output, "Latch clock write. Low chances of latching data.\n");
  699.                             } else if(operand1 < 0xA0) {
  700.                                 fprintf(output, "VRAM write. May or may not succeed.\n");
  701.                             } else if(operand1 < 0xC0) {
  702.                                 fprintf(output, "SRAM write. If it wasn't enabled, won't succeed. Otherwise, may be harmful.\n");
  703.                             } else if(operand1 < 0xE0) {
  704.                                 fprintf(output, "Write to WRAM. Huge chances to be harmful.\n");
  705.                             } else if(operand1 < 0xFE) {
  706.                                 fprintf(output, "Write to Echo RAM. Equivalent to a write to $%.2hhX%.2hhX.\n", operand1 - 0x20, operand2);
  707.                             } else if(operand1 != 0xFF) {
  708.                                 if(operand2 < 0xA0) {
  709.                                     fprintf(output, "Write to OAM. Low chances of succeeding.\n");
  710.                                 } else {
  711.                                     fprintf(output, "Write is lost.\n");
  712.                                 }
  713.                             } else {
  714.                                 if(operand2 < 0x80) {
  715.                                     fprintf(output, "Write to I/O registers. Almost certainly harmful.\n");
  716.                                 } else if(operand2 != 0xFF) {
  717.                                     fprintf(output, "Write to HRAM. High chances to be harmful.\n");
  718.                                 } else {
  719.                                     fprintf(output, "Changes the Interruption Mask. High chances to be harmful.");
  720.                                 }
  721.                             }
  722.                         break;
  723.                         case 0:
  724.                             fprintf(output, "Unknown target location. Continuing, but has chances to crash / corrupt stuff.\n");
  725.                         break;
  726.                     }
  727.                 break;
  728.             }
  729.             if(addr > 0x7FFF) {
  730.                 fprintf(output, "Reaches end of current ROM bank.\n");
  731.                 keepReading = 0;
  732.             }
  733.         }
  734.        
  735.         fprintf(output, "\n");
  736.         bank++;
  737.     }
  738.    
  739.     fclose(ptrRom);
  740.     exit(0);
  741. }
Add Comment
Please, Sign In to add comment