data2rdram.c 3.65 KB
#ifdef __sgi__
#include <bstring.h>
#endif
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define	LINE_SIZE	(80)
#define	MAIN_SIZE	(2*1024*1024)
#define	HIDDEN_SIZE	(MAIN_SIZE/8)

#define	MAIN		0
#define	HIDDEN		1

static int		lineNumber = 0;

static void		usage(void);
static void		syntax(void);

main(int argc, char *argv[])
{
    int			c;
    FILE		*f;
    int			numberRdrams = 1;
    char		line[LINE_SIZE];
    unsigned long long	*mainBits;
    unsigned char	*hiddenBits;
    int			mainRdramSize, hiddenRdramSize;
    int			maxAddress;
    unsigned int	address;
    int			mainOrHidden = MAIN;
    unsigned long	l0, l1;
    unsigned long long	ll;
    unsigned int	b;

    while ((c = getopt(argc, argv, "r:")) != EOF) {
	switch (c) {
	case 'r':
	    numberRdrams = atoi(optarg);
	    break;
	case '?':
	    usage();
	    exit(1);
	}
    }

    if ((argc - optind) > 1) {
	usage();
	exit(1);
    }

    if ((numberRdrams < 1) || (numberRdrams > 2)) {
	fprintf(stderr, "data2rdram: number of rdrams must be 1 or 2\n");
	exit(1);
    }
    if ((argc - optind) == 1) {
	if ((f = fopen(argv[optind], "r")) == NULL) {
	    fprintf(stderr, "data2rdram: %s: cannot open (%s)\n",
		    argv[optind], strerror(errno));
	    exit(1);
	}
    } else {
	f = stdin;
    }

    mainRdramSize = numberRdrams * MAIN_SIZE;
    hiddenRdramSize = maxAddress = numberRdrams * HIDDEN_SIZE;
    mainBits = (unsigned long long *)malloc(mainRdramSize);
    hiddenBits = (unsigned char *)malloc(hiddenRdramSize);

    if ((mainBits == NULL) || (hiddenBits == NULL)) {
		fprintf(stderr, "data2rdram: malloc failed (%s)\n",
			strerror(errno));
		exit(1);
    }

    bzero(mainBits, mainRdramSize);
    bzero(hiddenBits, hiddenRdramSize);

    while (fgets(line, LINE_SIZE, f) != NULL) {
	lineNumber++;
	if ((line[0] == '\n') || ((line[0] == '/') && (line[1] == '/'))) {
		continue;
	}
	if (sscanf(line, "@%x", &address) == 1) {
	    mainOrHidden = MAIN;
	} else if (sscanf(line, "%%%x", &address) == 1) {
	    mainOrHidden = HIDDEN;
	} else {
	    if (mainOrHidden == MAIN) {
		if (address >= maxAddress) {
		    fprintf(stderr,
			"data2rdram: address (0x%x) out of range at line %d\n",
			 address, lineNumber);
		    exit(1);
		}

	        if (sscanf(line, "%x_%x", &l0, &l1) == 2) {
#ifdef __sgi__
		    mainBits[address++] = (long long)l0 << 32LL | (long long)l1;
#else
                    l0 = ntohl(l0);
                    l1 = ntohl(l1);
		    mainBits[address++] = (long long)l1 << 32LL | (long long)l0;
#endif
		} else if (sscanf(line, "%llx", &ll) == 1) {
#ifdef __sgi__
		    mainBits[address++] = ll;
#else
                    l1 = (unsigned long) (ll >> 32);
                    l0 = (unsigned long) (ll & 0xffffffff); 
                    l1 = ntohl(l1);
                    l0 = ntohl(l0);
		    mainBits[address++] = (long long)l0 << 32LL | (long long)l1;
                    
#endif
		} else {
		    syntax();
		}
	    } else {
		if (address >= maxAddress) {
		    fprintf(stderr,
			"data2rdram: address (0x%x) out of range at line %d\n",
			 address, lineNumber);
		    exit(1);
		}
		if (sscanf(line, "%x", &b) == 1) {
		    hiddenBits[address++] = (unsigned char)b;
		} else {
		    syntax();
		}
	    }
	}
    }
    if ((write(1, mainBits, mainRdramSize) != mainRdramSize) ||
	(write(1, hiddenBits, hiddenRdramSize) != hiddenRdramSize)) {
	    fprintf(stderr, "data2rdram: %s: cannot write rdram file\n");
	    exit(1);
    }
}

static void
usage(void)
{
    fprintf(stderr, "usage: data2rdram [-r <num rdrams>] [.data file]\n");
    exit(1);
}

static void
syntax(void)
{
    fprintf(stderr, "data2rdram: syntax error on line %d\n", lineNumber);
    exit(1);
}