/*
 * $Id: main.c,v 1.2 2006/03/16 21:27:38 jlivesey Exp $
 */

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "types.h"
#include "image.h"

char *ProgName = NULL;
char  *inFile = NULL;
u32 dumpLen = 0;
u32 dumpEntry = 0;
u32 dumpDest = 0;
u32 hexOut = 0;
u32 hexMax = 0;
u32 verbose = 0;
u32 byteswap = 0;

void
usage(int err)
{
    fprintf(stderr, "usage: %s [args]\n", ProgName);
    fprintf(stderr, "  -f file       # input file\n");
    fprintf(stderr, "  -l            # dump text+data length\n");
    fprintf(stderr, "  -e            # dump binary entry\n");
    fprintf(stderr, "  -t            # dump text location\n");
    fprintf(stderr, "  -H            # dump hex of program\n");
    fprintf(stderr, "  -M NN         # extend image to this length\n");
    exit(err);
}

static void
dumpHData(FILE *fp, const char *nm, u8 *data, u32 tot, u32 max)
{
    u32   i, j, word, words = 0;

    for( i = 0 ; i < tot ; i += 4, words++ ) {
        word = 0;
        for (j = 0; j < 4; j++) {
            word |= (data[i+j] << ((3 - j) * 8));
        }
        fprintf (fp, "%08x\n", word);
    }
    
    if (words < max) {
        for ( i = words; i < max; i++ ) {
            fprintf (fp, "%08x\n", 0);
        }
    } else {
        if (max == 0) {
	    for (i = words; (i & 0x1ff) != 0; i++) {
	        fprintf (fp, "%08x\n", 0);
	    }
        }
    }


    return;
}

void
dumpHex(Image *ip, u32 max)
{
    FILE *fout = stdout;

    if ( ip->textLen == 0 ) {
        fprintf(stderr, "No text segment!\n");
        exit(1);
    }
    if ( ip->dataLen ) {
        fprintf(stderr, "Has a data segment!\n");
        exit(1);
    }

    if (verbose)
      fprintf (stderr, "max 0x%x text 0x%x\n", max, ip->text);

    /*
     * If specified, max is realtive to start of text segment
     */
    if (max > ip->text) {
      max = (u32)max - (u32)ip->text;
      /*
       * Max is in bytes
       */
      max = max / 4;
    }

    if (verbose)
      fprintf (stderr, "max 0x%x\n", max);

    dumpHData(fout, "rom", ip->textData, ip->textLen, max);

    fclose(fout);

    return;
}

int
main (int ac, char *av[])
{
  int    errFlg = 0;
  int    c;
  Image  *ip;
  u32    imgLen;

  ProgName = av[0];

  while ( (c = getopt(ac, av, "ef:hHM:ltv")) != EOF ) {
    switch (c) {

    case 'f':
      inFile = optarg;
      break;

    case 'e':
        dumpEntry++;
        break;
        
    case 'l':
        dumpLen++;
        break;

    case 't':
        dumpDest++;
        break;

    case 'H':
        hexOut++;
        break;

    case 'M':
        hexMax = strtoul(optarg, NULL, 16);
        break;

    case 's':
        byteswap++;
        break;

    case 'v':
      verbose++;
      break;
      
    case 'h':
      usage(0);
      break;

    default:
      fprintf(stderr, "unknown option %c\n", c);
      errFlg++;
      break;
    }
  }

  if ( (inFile == NULL) || errFlg ) {
    usage(1);
  }

  printf ("%s: input file %s\n",
	  __FUNCTION__, inFile);

  printf ("%s: options: ", __FUNCTION__);
  if (dumpEntry)
    printf ("dump entry point, ");
  if (dumpLen)
    printf ("dump segment length, ");
  if (dumpDest)
    printf ("dump dest, ");
  if (hexOut)
    printf ("dump hex of test/data, ");
  if (hexMax)
    printf ("pad output to 0x%08x", hexMax);

  /* read file */
  ip = dolRead(inFile);
  if ( ip == NULL ) {
    fprintf(stderr, "unable to load dol file %s\n", inFile);
    exit(1);
  }
  
  if ( ip->dataLen ) {
    imgLen = (ip->data + ip->dataLen) - ip->text;
  }
  else {
    imgLen = ip->textLen;
  }
  
  if ( hexOut ) {
    dumpHex (ip, hexMax);
  }
  
  if ( dumpLen ) {
    printf("len %d\n", imgLen);
  }
  
  if ( dumpDest ) {
    printf("text %d 0x%08x\n", ip->text, ip->text);
  }
  
  if ( dumpEntry ) {
    printf("entry %d 0x%08x\n", ip->entry, ip->entry);
  }

  return (0);
}

/* eof */
