add LOG macro and convert WD279x printfs to LOG() calls

Tue, 28 Dec 2010 19:55:13 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Tue, 28 Dec 2010 19:55:13 +0000
changeset 71
22452603e214
parent 70
5bbe76e71698
child 72
c66c98c7a768

add LOG macro and convert WD279x printfs to LOG() calls

src/utils.h file | annotate | diff | revisions
src/wd279x.c file | annotate | diff | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/utils.h	Tue Dec 28 19:55:13 2010 +0000
     1.3 @@ -0,0 +1,14 @@
     1.4 +#ifndef _H_UTILS
     1.5 +#define _H_UTILS
     1.6 +
     1.7 +#ifndef NDEBUG
     1.8 +/// Log a message to stderr
     1.9 +#  define LOG(x, ...) do { fprintf(stderr, "%s:%d:%s(): " x "\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); } while (0)
    1.10 +/// Log a message to stderr if 'cond' is true
    1.11 +#  define LOG_IF(cond, x, ...) do { if (cond) fprintf(stderr, "%s:%d:%s(): " x "\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); } while (0)
    1.12 +#else
    1.13 +#define LOG(x, ...)
    1.14 +#define LOG_IF(cond, x, ...)
    1.15 +#endif
    1.16 +
    1.17 +#endif // _H_UTILS
     2.1 --- a/src/wd279x.c	Tue Dec 28 19:23:57 2010 +0000
     2.2 +++ b/src/wd279x.c	Tue Dec 28 19:55:13 2010 +0000
     2.3 @@ -4,6 +4,9 @@
     2.4  #include "musashi/m68k.h"
     2.5  #include "wd279x.h"
     2.6  
     2.7 +#define NDEBUG
     2.8 +#include "utils.h"
     2.9 +
    2.10  /// WD2797 command constants
    2.11  enum {
    2.12  	CMD_MASK				= 0xF0,		///< Bit mask to detect command bits
    2.13 @@ -172,7 +175,7 @@
    2.14  			if (ctx->cmd_has_drq) {
    2.15  				temp = ctx->status & ~0x03;
    2.16  				temp |= (ctx->data_pos < ctx->data_len) ? 0x02 : 0x00;
    2.17 -				printf("\tWDFDC rd sr, has drq, pos=%lu len=%lu, sr=0x%02X\n", ctx->data_pos, ctx->data_len, temp);
    2.18 +				LOG("\tWDFDC rd sr, has drq, pos=%lu len=%lu, sr=0x%02X", ctx->data_pos, ctx->data_len, temp);
    2.19  			} else {
    2.20  				temp = ctx->status & ~0x01;
    2.21  			}
    2.22 @@ -382,12 +385,12 @@
    2.23  				case CMD_READ_SECTOR:
    2.24  				case CMD_READ_SECTOR_MULTI:
    2.25  					ctx->head = (val & 0x02) ? 1 : 0;
    2.26 -					printf("WD279X: READ SECTOR cmd=%02X chs=%d:%d:%d\n", cmd, ctx->track, ctx->head, ctx->sector);
    2.27 +					LOG("WD279X: READ SECTOR cmd=%02X chs=%d:%d:%d", cmd, ctx->track, ctx->head, ctx->sector);
    2.28  					// Read Sector or Read Sector Multiple
    2.29  
    2.30  					// Check to see if the cyl, hd and sec are valid
    2.31  					if ((ctx->track > (ctx->geom_tracks-1)) || (ctx->head > (ctx->geom_heads-1)) || (ctx->sector > ctx->geom_spt) || (ctx->sector == 0)) {
    2.32 -						fprintf(stderr, "*** WD2797 ALERT: CHS parameter limit exceeded! CHS=%d:%d:%d, maxCHS=%d:%d:%d\n",
    2.33 +						LOG("*** WD2797 ALERT: CHS parameter limit exceeded! CHS=%d:%d:%d, maxCHS=%d:%d:%d",
    2.34  								ctx->track, ctx->head, ctx->sector,
    2.35  								ctx->geom_tracks-1, ctx->geom_heads-1, ctx->geom_spt);
    2.36  						// CHS parameters exceed limits
    2.37 @@ -414,13 +417,13 @@
    2.38  						lba = (((ctx->track * ctx->geom_heads * ctx->geom_spt) + (ctx->head * ctx->geom_spt) + ctx->sector) + i) - 1;
    2.39  						// convert LBA to byte address
    2.40  						lba *= ctx->geom_secsz;
    2.41 -						printf("\tREAD lba = %lu\n", lba);
    2.42 +						LOG("\tREAD lba = %lu", lba);
    2.43  
    2.44  						// Read the sector from the file
    2.45  						fseek(ctx->disc_image, lba, SEEK_SET);
    2.46 +						// TODO: check fread return value! if < secsz, BAIL! (call it a crc error or secnotfound maybe? also log to stderr)
    2.47  						ctx->data_len += fread(&ctx->data[ctx->data_len], 1, ctx->geom_secsz, ctx->disc_image);
    2.48 -						printf("\tREAD len=%lu, pos=%lu, ssz=%d\n", ctx->data_len, ctx->data_pos, ctx->geom_secsz);
    2.49 -						// TODO: check fread return value! if < secsz, BAIL! (call it a crc error or secnotfound maybe? also log to stderr)
    2.50 +						LOG("\tREAD len=%lu, pos=%lu, ssz=%d", ctx->data_len, ctx->data_pos, ctx->geom_secsz);
    2.51  					}
    2.52  
    2.53  					ctx->status = 0;