From 18114bf7df8b792d24b1736ac8cf6f4af625ae18 Mon Sep 17 00:00:00 2001 From: Tennessee Carmel-Veilleux Date: Wed, 23 Jul 2014 15:36:35 -0400 Subject: [PATCH] Updated license in file and added FIXME markers --- dcc6502.c | 214 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 136 insertions(+), 78 deletions(-) diff --git a/dcc6502.c b/dcc6502.c index 0bef3ec..f85e703 100644 --- a/dcc6502.c +++ b/dcc6502.c @@ -1,15 +1,29 @@ -/**************************************************************/ -/* DCC6502.c -> Main module of: */ -/* Disassembler and Cycle Counter for the 6502 microprocessor */ -/* */ -/* (C) 1998-2014 Tennessee Carmel-Veilleux(veilleux@ameth.org)*/ -/* This code is offered as FREEware. You cannot modify nor */ -/* distribute modified versions of this software without */ -/* prior written consent of the author(s). The author shall */ -/* NOT be responsible for ANY damage purely physical, */ -/* emotional, material and magical to either you or anyone. */ -/**************************************************************/ - +/********************************************************************************** + * dcc6502.c -> Main module of: * + * Disassembler and Cycle Counter for the 6502 microprocessor * + * * + * This code is offered under the MIT License (MIT) * + * * + * Copyright (c) 2014 Tennessee Carmel-Veilleux #include #include @@ -42,6 +56,7 @@ typedef struct OPcode { unsigned char cross_page; /* 1 if cross-page boundaries affect cycles */ } OPcode; +// FIXME: Stop using the needless union and become native-endianness-agnostic typedef union { #ifdef LSB_FIRST @@ -52,13 +67,14 @@ typedef union unsigned short W; } word; -char name_table[56][4]={ +char name_table[56][4] = { "ADC", "AND", "ASL", "BCC", "BCS", "BEQ", "BIT", "BMI", "BNE", "BPL", "BRK", "BVC", "BVS", "CLC", "CLD", "CLI", "CLV", "CMP", "CPX", "CPY", "DEC", "DEX", "DEY", "EOR", "INC", "INX", "INY", "JMP", "JSR", "LDA", "LDX", "LDY", "LSR", "NOP", "ORA", "PHA", "PHP", "PLA", "PLP", "ROL", "ROR", "RTI", "RTS", "SBC", "SEC", "SED", "SEI", "STA", "STX", "STY", - "TAX", "TAY", "TSX", "TXA", "TXS", "TYA"}; + "TAX", "TAY", "TSX", "TXA", "TXS", "TYA" +}; /* Opcode table */ OPcode opcode_table[NUMBER_OPCODES] = { @@ -270,14 +286,16 @@ OPcode opcode_table[NUMBER_OPCODES] = { {0x98, 55, IMPLI, 2, 0} /* TYA */ }; +// FIXME: use stdint types +// FIXME: use g_ nomenclature for globals unsigned short org; /* Origin of addresses */ char hex_output = 0; /* 1 if hex output is desired at beginning of line */ char cycle_counting = 0; /* 1 if we want cycle counting */ char nes_mode = 0; /* 1 if NES commenting and warnings are enabled */ FILE *f; /* Input file */ unsigned char buffer[0xffff]; /* Memory buffer */ -unsigned short PC=0; /* Program counter */ -unsigned short max=0xffff; /* Maximum number of bytes to disassemble */ +unsigned short PC = 0; /* Program counter */ +unsigned short max = 0xffff; /* Maximum number of bytes to disassemble */ char line[512]; /* This function emits a comment header with information about the file @@ -285,7 +303,7 @@ char line[512]; void emit_header(char *filename, int fsize, unsigned short org) { fprintf(stdout, "; Source generated by DCC6502 version %s\n", VERSION_INFO); - fprintf(stdout, "; For more info about DCC6502, e-mail veilleux@ameth.org\n;\n"); + fprintf(stdout, "; For more info about DCC6502, see https://github.com/tcarmelveilleux/dcc6502\n"); fprintf(stdout, "; FILENAME: %s, File Size: %d, ORG: $%04X\n", filename, fsize, org); if (hex_output) fprintf(stdout, "; -> Hex output enabled\n"); if (cycle_counting) fprintf(stdout, "; -> Cycle counting enabled\n"); @@ -301,7 +319,9 @@ void append_cycle(char *input, unsigned char entry, unsigned short arg, unsigned cycles = opcode_table[entry].cycles; sprintf(tmpstr, " Cycles: %d ", cycles); - if (opcode_table[entry].cross_page) strcat(tmpstr, "*!* "); + if (opcode_table[entry].cross_page) { + strcat(tmpstr, "*!* "); + } strcat(input, tmpstr); } @@ -347,6 +367,7 @@ void append_nes(char *input, unsigned short arg) { } } +// FIXME: Refactor code to reduce line duplication and make more readable /* This function disassembles the opcode at the PC and outputs it in *output */ void disassemble(char *output) { unsigned char tmp_byte1, tmp_byte2, opcode; @@ -379,13 +400,16 @@ void disassemble(char *output) { case IMMED: PC++; tmp_byte1 = buffer[PC]; /* Get immediate value */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X:\t%s #$%02x\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1); - else + } else { sprintf(tmpstr, "$%04X\t%s #$%02x\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + if (cycle_counting) { + append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + } strncpy(output, tmpstr, 254); break; @@ -401,10 +425,14 @@ void disassemble(char *output) { sprintf(tmpstr, "$%04X\t%s $%02X%02X\t;", org+PC-2, name_table[opcode_table[entry].name], tmp_word.B.h, tmp_word.B.l); /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, tmp_word.W, org+PC-2); + if (cycle_counting) { + append_cycle(tmpstr, entry, tmp_word.W, org+PC-2); + } /* Add NES port info if necessary */ - if (nes_mode) append_nes(tmpstr, tmp_word.W); + if (nes_mode) { + append_nes(tmpstr, tmp_word.W); + } strncpy(output, tmpstr, 254); break; @@ -412,25 +440,31 @@ void disassemble(char *output) { PC++; tmp_byte1 = buffer[PC]; /* Get low byte of address */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X:\t%s $%02X\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1); - else + } else { sprintf(tmpstr, "$%04X\t%s $%02X\t\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1); - + } + /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + if (cycle_counting) { + append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + } strncpy(output, tmpstr, 254); break; case IMPLI: - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X:\t%s\t\t;", org+PC, opcode, name_table[opcode_table[entry].name]); - else + } else { sprintf(tmpstr, "$%04X\t%s\t\t;", org+PC, name_table[opcode_table[entry].name]); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, org+PC, org+PC); + if (cycle_counting) { + append_cycle(tmpstr, entry, org+PC, org+PC); + } strncpy(output, tmpstr, 254); break; @@ -441,13 +475,16 @@ void disassemble(char *output) { PC++; tmp_word.B.h = buffer[PC]; /* Get high byte of address */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X%02X:\t%s ($%02X%02X)\t;", org+PC-2, opcode, tmp_word.B.l, tmp_word.B.h, name_table[opcode_table[entry].name], tmp_word.B.h, tmp_word.B.l); - else + } else { sprintf(tmpstr, "$%04X\t%s ($%02X%02X)\t;", org+PC-2, name_table[opcode_table[entry].name], tmp_word.B.h, tmp_word.B.l); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, tmp_word.W, org+PC-2); + if (cycle_counting) { + append_cycle(tmpstr, entry, tmp_word.W, org+PC-2); + } strncpy(output, tmpstr, 254); break; @@ -458,16 +495,22 @@ void disassemble(char *output) { PC++; tmp_word.B.h = buffer[PC]; /* Get high byte of address */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X%02X:\t%s $%02X%02X,X\t;", org+PC-2, opcode, tmp_word.B.l, tmp_word.B.h, name_table[opcode_table[entry].name], tmp_word.B.h, tmp_word.B.l); - else + } else { sprintf(tmpstr, "$%04X\t%s $%02X%02X,X\t;", org+PC-2, name_table[opcode_table[entry].name], tmp_word.B.h, tmp_word.B.l); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, tmp_word.W, org+PC-2); + if (cycle_counting) { + append_cycle(tmpstr, entry, tmp_word.W, org+PC-2); + } /* Add NES port info if necessary */ - if (nes_mode) append_nes(tmpstr, tmp_word.W); + if (nes_mode) { + append_nes(tmpstr, tmp_word.W); + } + strncpy(output, tmpstr, 254); break; @@ -477,16 +520,22 @@ void disassemble(char *output) { PC++; tmp_word.B.h = buffer[PC]; /* Get high byte of address */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X%02X:\t%s $%02X%02X,Y\t;", org+PC-2, opcode, tmp_word.B.l, tmp_word.B.h, name_table[opcode_table[entry].name], tmp_word.B.h, tmp_word.B.l); - else + } else { sprintf(tmpstr, "$%04X\t%s $%02X%02X,Y\t;", org+PC-2, name_table[opcode_table[entry].name], tmp_word.B.h, tmp_word.B.l); - + } + /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, tmp_word.W, org+PC-2); + if (cycle_counting) { + append_cycle(tmpstr, entry, tmp_word.W, org+PC-2); + } /* Add NES port info if necessary */ - if (nes_mode) append_nes(tmpstr, tmp_word.W); + if (nes_mode) { + append_nes(tmpstr, tmp_word.W); + } + strncpy(output, tmpstr, 254); break; @@ -494,13 +543,16 @@ void disassemble(char *output) { PC++; tmp_byte1 = buffer[PC]; /* Get low byte of address */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X:\t%s $%02X,X\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1); - else + } else { sprintf(tmpstr, "$%04X\t%s $%02X,X\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + if (cycle_counting) { + append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + } strncpy(output, tmpstr, 254); break; @@ -509,13 +561,16 @@ void disassemble(char *output) { PC++; tmp_byte1 = buffer[PC]; /* Get low byte of address */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X:\t%s $%02X,Y\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1); - else + } else { sprintf(tmpstr, "$%04X\t%s $%02X,Y\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + if (cycle_counting) { + append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + } strncpy(output, tmpstr, 254); break; @@ -524,13 +579,16 @@ void disassemble(char *output) { PC++; tmp_byte1 = buffer[PC]; /* Get low byte of address */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X:\t%s ($%02X,X)\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1); - else + } else { sprintf(tmpstr, "$%04X\t%s ($%02X,X)\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + if (cycle_counting) { + append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + } strncpy(output, tmpstr, 254); break; @@ -539,13 +597,16 @@ void disassemble(char *output) { PC++; tmp_byte1 = buffer[PC]; /* Get low byte of address */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X:\t%s ($%02X),Y\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], tmp_byte1); - else + } else { sprintf(tmpstr, "$%04X\t%s ($%02X),Y\t;", org+PC-1, name_table[opcode_table[entry].name], tmp_byte1); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + if (cycle_counting) { + append_cycle(tmpstr, entry, org+PC-1, org+PC-1); + } strncpy(output, tmpstr, 254); break; @@ -554,25 +615,31 @@ void disassemble(char *output) { PC++; tmp_byte1 = buffer[PC]; /* Get relative modifier */ - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X %02X:\t%s $%04X\t\t;", org+PC-1, opcode, tmp_byte1, name_table[opcode_table[entry].name], (org+PC)+(signed char)(tmp_byte1)+1); - else + } else { sprintf(tmpstr, "$%04X\t%s $%04X\t;", org+PC-1, name_table[opcode_table[entry].name], (org+PC)+(signed char)(tmp_byte1)+1); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, org+PC, org+PC); + if (cycle_counting) { + append_cycle(tmpstr, entry, org+PC, org+PC); + } strncpy(output, tmpstr, 254); break; case ACCUM: - if (hex_output) + if (hex_output) { sprintf(tmpstr, "$%04X> %02X:\t%s A\t\t;", org+PC, opcode, name_table[opcode_table[entry].name]); - else + } else { sprintf(tmpstr, "$%04X\t%s A\t\t;", org+PC, name_table[opcode_table[entry].name]); + } /* Add cycle count if necessary */ - if (cycle_counting) append_cycle(tmpstr, entry, org+PC, org+PC); + if (cycle_counting) { + append_cycle(tmpstr, entry, org+PC, org+PC); + } strncpy(output, tmpstr, 254); break; @@ -584,8 +651,9 @@ void disassemble(char *output) { } void version(void) { - fprintf(stderr, "DCC6502 %s (C)1998-2014 Tennessee Carmel-Veilleux\n", VERSION_INFO); - fprintf(stderr, "This is free software. To see the LICENSE, use the -v parameter\n"); + fprintf(stderr, "DCC6502 %s (C)1998-2014 Tennessee Carmel-Veilleux \n", VERSION_INFO); + fprintf(stderr, "This software is licensed under the MIT license. See the LICENSE file.\n"); + fprintf(stderr, "See source on github: https://github.com/tcarmelveilleux/dcc6502.\n"); } void usage_helper(char *str) { @@ -603,20 +671,12 @@ void usage(void) { fprintf(stderr, "\n"); } -void license(void) { - fprintf(stderr, "(C) 1998-2014 Tennessee Carmel-Veilleux(veilleux@ameth.org)\n"); - fprintf(stderr, "This code is offered as FREEware. You cannot modify nor\n"); - fprintf(stderr, "distribute modified versions of this software without\n"); - fprintf(stderr, "prior written consent of the author(s). The author shall\n"); - fprintf(stderr, "NOT be responsible for ANY damage purely physical,\n"); - fprintf(stderr, "emotional, material and magical to either you or anyone.\n"); -} - +// FIXME: DE-KLUDGIFY THIS :D unsigned short hex2int (char *str, unsigned short dfl) { char HEX_digits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; int i, j; - char c, k,shift; - unsigned short tmp=0; + char c, k, shift; + unsigned short tmp = 0; shift = 0; for (i = 5; i >= 2; i--) { @@ -626,7 +686,9 @@ unsigned short hex2int (char *str, unsigned short dfl) { } c = toupper(str[i]); for (j = 0; j < 16; j++) { - if (c == HEX_digits[j]) k = j; + if (c == HEX_digits[j]) { + k = j; + } } tmp |= ((k & 0xf) << shift); shift += 4; @@ -682,7 +744,6 @@ int main(int argc, char *argv[]) { case '?': version(); usage(); - license(); exit(0); break; case 'n': @@ -696,7 +757,6 @@ int main(int argc, char *argv[]) { break; case 'v': version(); - license(); exit(0); break; case 'o': @@ -720,12 +780,10 @@ int main(int argc, char *argv[]) { case '?': version(); usage(); - license(); exit(0); break; case 'v': version(); - license(); exit(0); break; default: