Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define setRegisterLetter(out,var1,var2,var3) \
- out = 0b01000000; \
- out |= !var1 & var3 | var1 & var2; \
- out |= (!(var1 | var2)) << 1; \
- out |= (!var1 & var2 | var2 & !var3 | var1 & !var2 & var3) << 2; \
- out |= (var1 & !(var2 & var3)) << 3
- int Disassembler8080(unsigned char *codebuffer, int pc)
- {
- unsigned char *code = &codebuffer[pc];
- int opbytes = 1;
- unsigned char bit1 = *code << 7; bit1 >>= 7;
- unsigned char bit2 = *code << 6; bit2 >>= 7;
- unsigned char bit3 = *code << 5; bit3 >>= 7;
- unsigned char bit4 = *code << 4; bit4 >>= 7;
- unsigned char bit5 = *code << 3; bit5 >>= 7;
- unsigned char bit6 = *code << 2; bit6 >>= 7;
- unsigned char bit7 = *code << 1; bit7 >>= 7;
- unsigned char bit8 = *code >> 7;
- unsigned char output = 0b00000000;
- printf("%04x :", pc);
- if (!bit8 && bit7)
- { /*
- B C D E H L M A
- B 01000010
- C 01000011
- D 01000100
- E 01000101
- H 01001000
- L 01001100
- M 01001101
- A 01000001
- left - bit6 bit5 bit4
- right - bit3 bit2 bit1
- decod = 0010 - 000
- 0011 - 001
- 0100 - 010
- 0101 - 011
- 1000 - 100
- 1100 - 101
- 1101 - 110
- 0001 - 111
- */
- if (*code == 0x76) //HTL
- printf("HTL\n");
- else //MOV
- {
- printf("MOV ");
- setRegisterLetter(output,bit6,bit5,bit4);
- printf("%c,", output);
- setRegisterLetter(output,bit3,bit2,bit1);
- printf("%c\n", output);
- }
- return opbytes;
- }
- if (bit8 && !bit7)
- {
- if (bit6)
- {
- if (bit5 ^ bit4) //XRA OR ORA
- {
- if (bit4) //if bit4 = 0, then bit5 = 1
- output = 'X';
- else
- output = 'O';
- printf("%cRA ", output);
- setRegisterLetter(output,bit3,bit2,bit1);
- printf("%c\n", output);
- }
- else
- {
- if (bit5) //CMP
- printf("CMP ");
- else //ANA
- printf("ANA ");
- setRegisterLetter(output,bit3,bit2,bit1);
- printf("%c\n", output);
- }
- }
- else
- {
- if (bit5)
- {
- if (bit4) //SBB
- output = 'B';
- else //SUB
- output = 'U';
- printf("S%cB ", output);
- setRegisterLetter(output,bit3,bit2,bit1);
- printf("%c\n", output);
- }
- else //ADD OR ADC
- {
- output = 0b01000100 - bit4; //if bit4 = 1, 0b01000100 ('D') - 1 = 0b01000011 ('C')
- printf("AD%c ", output);
- setRegisterLetter(output,bit3,bit2,bit1);
- printf("%c\n", output);
- }
- }
- return opbytes;
- }
- switch (*code)
- {
- case 0x00:
- printf("NOP");
- break;
- case 0x01:
- printf("LXI B,#$%02x%02x", code[2], code[1]);
- opbytes = 3;
- break;
- case 0x02:
- printf("STAX B");
- break;
- case 0x03:
- printf("INX B");
- break;
- case 0x04:
- printf("INR B");
- break;
- case 0x05:
- printf("DCR B");
- break;
- case 0x06:
- printf("MVI B,#$%02x", code[1]);
- opbytes = 2;
- break;
- case 0x07:
- printf("RLC");
- break;
- case 0x08:
- printf("NOP");
- break;
- case 0x09:
- printf("DAD B");
- break;
- case 0x0a:
- printf("LDAX B");
- break;
- case 0x0b:
- printf("DCX B");
- break;
- case 0x0c:
- printf("INR C");
- break;
- case 0x0d:
- printf("DCR C");
- break;
- case 0x0e:
- printf("MVI C,#$%02x", code[1]);
- opbytes = 2;
- break;
- case 0x0f:
- printf("RRC");
- break;
- case 0x10:
- printf("NOP");
- break;
- case 0x11:
- printf("LXI B,#$%02x%02x", code[2], code[1]);
- opbytes = 3;
- break;
- case 0x12:
- printf("STAX D");
- break;
- case 0x13:
- printf("INX D");
- break;
- case 0x14:
- printf("INR D");
- break;
- case 0x15:
- printf("DCR D");
- break;
- case 0x16:
- printf("MVI D,#$%02x", code[1]);
- opbytes = 2;
- break;
- case 0x17:
- printf("RAL");
- break;
- case 0x18:
- printf("NOP");
- break;
- case 0x19:
- printf("DAD D");
- break;
- case 0x1a:
- printf("LDAX D");
- break;
- case 0x1b:
- printf("DCX D");
- break;
- case 0x1c:
- printf("INR E");
- break;
- case 0x1d:
- printf("DCR E");
- break;
- case 0x1e:
- printf("MVI E,#$%02x", code[1]);
- opbytes = 2;
- break;
- case 0x1f:
- printf("RAR");
- break;
- case 0x20:
- printf("NOP");
- break;
- case 0x21:
- printf("LXI H,#$%02x%02x", code[2], code[1]);
- opbytes = 3;
- break;
- case 0x22:
- printf("SHLD #%02x%02x", code[2], code[1]);
- opbytes = 3;
- break;
- case 0x23:
- printf("INX H");
- break;
- case 0x24:
- printf("INR H");
- break;
- case 0x25:
- printf("DCR H");
- break;
- case 0x26:
- printf("MVI H,#$%02x", code[1]);
- opbytes = 2;
- break;
- case 0x27:
- printf("DAA");
- break;
- case 0x28:
- printf("NOP");
- break;
- case 0x29:
- printf("DAD H");
- break;
- case 0x2a:
- printf("LHLD $%02x%02x", code[2], code[1]);
- opbytes = 3;
- break;
- case 0x2b:
- printf("DCX H");
- break;
- case 0x2c:
- printf("INR L");
- break;
- case 0x2d:
- printf("DCR L");
- break;
- case 0x2e:
- printf("MVI L,#$%02x", code[1]);
- opbytes = 2;
- break;
- case 0x2f:
- printf("CMA");
- break;
- case 0x30:
- printf("NOP");
- break;
- case 0x31:
- printf("LXI SP,#$%02x%02x", code[2], code[1]);
- opbytes = 3;
- break;
- case 0x32:
- printf("STA #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0x33:
- printf("INX SP");
- break;
- case 0x34:
- printf("INR M");
- break;
- case 0x35:
- printf("DCR M");
- break;
- case 0x36:
- printf("MVI M,#$%02x", code[1]);
- opbytes = 2;
- break;
- case 0x37:
- printf("STC");
- break;
- case 0x38:
- printf("NOP");
- break;
- case 0x39:
- printf("DAD SP");
- break;
- case 0x3a:
- printf("LDA #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0x3b:
- printf("DCX SP");
- break;
- case 0x3c:
- printf("INR A");
- break;
- case 0x3d:
- printf("DCR A");
- break;
- case 0x3e:
- printf("MVI A,#$%02x", code[1]);
- opbytes = 2;
- break;
- case 0x3f:
- printf("CMC");
- break;
- case 0xc0:
- printf("RNZ");
- break;
- case 0xc1:
- printf("POP B");
- break;
- case 0xc2:
- printf("JNZ #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xc3:
- printf("JMP #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xc4:
- printf("CNZ #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xc5:
- printf("PUSH B");
- break;
- case 0xc6:
- printf("ADI #$%02x", code[1]);
- opbytes = 2;
- break;
- case 0xc7:
- printf("RST 0");
- break;
- case 0xc8:
- printf("RZ");
- break;
- case 0xc9:
- printf("RET");
- break;
- case 0xca:
- printf("JZ #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xcb:
- printf("NOP");
- break;
- case 0xcc:
- printf("CZ #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xcd:
- printf("CALL #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xce:
- printf("ACI #$%02x", code[1]);
- opbytes = 2;
- break;
- case 0xcf:
- printf("RST 1");
- break;
- case 0xd0:
- printf("RNC");
- break;
- case 0xd1:
- printf("POP D");
- break;
- case 0xd2:
- printf("JNC #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xd3:
- printf("OUT #$%02x", code[1]);
- opbytes = 2;
- break;
- case 0xd4:
- printf("CNC #$%02x", code[2]);
- opbytes = 2;
- break;
- case 0xd5:
- printf("PUSH D");
- break;
- case 0xd6:
- printf("SUI #$%02x", code[1]);
- opbytes = 2;
- break;
- case 0xd7:
- printf("RST 2");
- break;
- case 0xd8:
- printf("RC");
- break;
- case 0xd9:
- printf("NOP");
- break;
- case 0xda:
- printf("JC #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xdb:
- printf("IN #$%02x", code[1]);
- opbytes = 2;
- break;
- case 0xdc:
- printf("CC #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xdd:
- printf("NOP");
- break;
- case 0xde:
- printf("SBI #$%02x", code[2]);
- opbytes = 2;
- break;
- case 0xdf:
- printf("RST 3");
- break;
- case 0xe0:
- printf("RPO");
- break;
- case 0xe1:
- printf("POP H");
- break;
- case 0xe2:
- printf("JPO #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xe3:
- printf("XTHL");
- break;
- case 0xe4:
- printf("CPO #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xe5:
- printf("PUSH H");
- break;
- case 0xe6:
- printf("ANI #$%02x", code[1]);
- opbytes = 2;
- break;
- case 0xe7:
- printf("RST 4");
- break;
- case 0xe8:
- printf("RPE");
- break;
- case 0xe9:
- printf("PCHL");
- break;
- case 0xea:
- printf("JPE #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xeb:
- printf("XCHG");
- break;
- case 0xec:
- printf("CPE #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xed:
- printf("NOP");
- break;
- case 0xee:
- printf("XRI #$%02x", code[1]);
- opbytes = 2;
- break;
- case 0xef:
- printf("RST 5");
- break;
- case 0xf0:
- printf("RP");
- break;
- case 0xf1:
- printf("POP PSW");
- break;
- case 0xf2:
- printf("JP #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xf3:
- printf("DI");
- break;
- case 0xf4:
- printf("CP #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xf5:
- printf("PUSH PSW");
- break;
- case 0xf6:
- printf("ORI #$%02x", code[1]);
- break;
- case 0xf7:
- printf("RST 6");
- break;
- case 0xf8:
- printf("RM");
- break;
- case 0xf9:
- printf("SPHL");
- break;
- case 0xfa:
- printf("JM #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xfb:
- printf("EI");
- break;
- case 0xfc:
- printf("CM #$%02x", code[2]);
- opbytes = 3;
- break;
- case 0xfd:
- printf("NOP");
- break;
- case 0xfe:
- printf("CPI #$%02x", code[1]);
- break;
- case 0xff:
- printf("RST 7");
- break;
- default:
- printf("Nonexistent Opcode");
- break;
- }
- printf("\n");
- return opbytes;
- }
- int main(int argc, char **argv)
- {
- FILE *f = fopen(argv[1], "rb");
- if (f == NULL)
- {
- printf("error: Couldn't open %s\n", argv[1]);
- exit(1);
- }
- //Get the file size and read it into a memory buffer
- fseek(f, 0L, SEEK_END);
- int fsize = ftell(f);
- fseek(f, 0L, SEEK_SET);
- unsigned char *buffer = malloc(fsize);
- fread(buffer, fsize, 1, f);
- fclose(f);
- int pc = 0;
- while (pc < fsize)
- {
- pc += Disassembler8080(buffer, pc);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment