Compare commits

...

6 Commits

Author SHA1 Message Date
Tennessee Carmel-Veilleux
680c212992
Merge pull request #3 from ratherlargerobot/fread-brk-bugfix
Use fread() return value to count bytes
2018-09-02 15:28:46 -04:00
Nathan Rosenquist
8b01d8b204 Use fread() return value to count bytes
This commit changes the behavior of dcc6502, so that it consults the
return value of fread(), and then increments the value of byte_count
by the number returned by fread(), rather than implicitly incrementing
it every time.

Previously, a warning occurred during compilation, because the return value
of fread() was ignored. Instead, the number of bytes in the input file were
counted implicitly by incrementing a byte_count variable after every fread()
call.

Additionally, I created a two-byte test input file consisting of the
bytes #$a9ff, which corresponds to LDA #$FF. When dcc6502 tried to
disassemble this input file, it reported that the file had a size of three
bytes. It reported the first two opcodes correctly, but then incorrectly
displayed a BRK as the third opcode.

After this change, the input file now has a reported size of two bytes,
without the phantom BRK opcode at the end.
2018-09-01 20:44:41 -04:00
Tennessee Carmel-Veilleux
c003395dd7 Bumped version to 2.1 after -s added 2018-02-21 09:43:20 -05:00
Tennessee Carmel-Veilleux
2c46640706
Merge pull request #2 from KaleviKolttonen/input_file_offset
Add a command line option -s for specifying file offset to start disa…
2018-02-21 09:40:34 -05:00
Kalevi Kolttonen
ab33fe1c84 Minor cleanups for new command line option -s code 2018-02-21 16:31:05 +02:00
Kalevi Kolttonen
1ff0a0949b Add a command line option -s for specifying file offset to start disassembling from 2018-02-21 15:21:22 +02:00

View File

@ -31,7 +31,7 @@
#include <ctype.h>
#include <errno.h>
#define VERSION_INFO "v2.0"
#define VERSION_INFO "v2.1"
#define NUMBER_OPCODES 151
/* Exceptions for cycle counting */
@ -75,6 +75,7 @@ typedef struct options_s {
int hex_output; /* 1 if hex dump output is desired at beginning of line */
unsigned long max_num_bytes;
uint16_t org; /* Origin of addresses */
long offset; /* File offset to start disassembly from */
} options_t;
/* Opcode table */
@ -613,6 +614,7 @@ static void usage(void) {
fprintf(stderr, " -?/-h : Show this help message\n");
fprintf(stderr, " -o ORIGIN : Set the origin (base address of disassembly) [default: 0x8000]\n");
fprintf(stderr, " -m NUM_BYTES : Only disassemble the first NUM_BYTES bytes\n");
fprintf(stderr, " -s NUM_BYTES : Disassemble after skipping NUM_BYTES from start of input file\n");
fprintf(stderr, " -d : Enable hex dump within disassembly\n");
fprintf(stderr, " -n : Enable NES register annotations\n");
fprintf(stderr, " -v : Get only version information\n");
@ -653,6 +655,7 @@ static void parse_args(int argc, char *argv[], options_t *options) {
options->nes_mode = 0;
options->org = 0x8000;
options->max_num_bytes = 65536;
options->offset = 0;
while (arg_idx < argc) {
/* First non-dash-starting argument is assumed to be filename */
@ -703,6 +706,17 @@ static void parse_args(int argc, char *argv[], options_t *options) {
}
options->max_num_bytes = tmp_value;
break;
case 's':
if ((arg_idx == (argc - 1)) || (argv[arg_idx + 1][0] == '-')) {
usage_and_exit(1, "Missing argument to -s switch");
}
/* Get argument and parse it */
arg_idx++;
if (!str_arg_to_ulong(argv[arg_idx], &tmp_value)) {
usage_and_exit(1, "Invalid argument to -s switch");
}
options->offset = (long)tmp_value;
break;
default:
version();
usage();
@ -727,6 +741,7 @@ int main(int argc, char *argv[]) {
FILE *input_file; /* Input file */
uint16_t pc; /* Program counter */
options_t options; /* Command-line options parsing results */
int result = 0;
parse_args(argc, argv, &options);
@ -744,10 +759,18 @@ int main(int argc, char *argv[]) {
exit(2);
}
if (options.offset) {
result = fseek(input_file, options.offset, SEEK_SET);
if (result < 0) {
fprintf(stderr, "fseek(%s, %ld, SEEK_SET) failed: %s (%d)\n", options.filename, options.offset, strerror(errno), result);
exit(2);
}
}
byte_count = 0;
while(!feof(input_file) && ((options.org + byte_count) <= 0xFFFFu) && (byte_count < options.max_num_bytes)) {
fread(&buffer[options.org + byte_count], 1, 1, input_file);
byte_count++;
size_t bytes_read = fread(&buffer[options.org + byte_count], 1, 1, input_file);
byte_count += bytes_read;
}
fclose(input_file);