#include "loader.h"

AFIELD a_field;
BFIELD b_field;


main(argc,argv)
int argc;
char *argv[];
{
    BITFILE *bf;
    int absbyte;


    bf = bitfopen(argv[1],"rb");
    if (bf == NULL)
    {
	printf("Can't open input file\n");
	exit(1);
    }



    /* The main parsing loop */

    while(1)
    {
	switch(getbit(bf))
	{
	    case 0:
		absbyte = getnbits(bf,8);
		printf("Load absolute byte %x\n",absbyte);
		break;

	    case 1:
		/* a relocatable item */

		switch(getnbits(bf,2))
		{
		    case 0:
			linkitem(bf);
			break;
		    case 1:
			printf("Program relative word %x\n",getaddr(bf));
			break;
		    case 2:
			printf("Data relative word %x\n",getaddr(bf));
			break;
		    case 3:
			printf("Common relative word %x\n",getaddr(bf));
			break;
		}
		break;

	    default:
		printf("Premature EOF\n");
		exit(1);

	}
    }

}


linkitem(bf)
BITFILE *bf;
{
    int itemno;

    switch(itemno = getnbits(bf,4))
    {
    case 0:

    case 1:

    case 2:

    case 3:

    case 4:
	getbfield(bf,&b_field);
	printf("B-field item no. %d. Symbol is \"%s\"\n",itemno, b_field.symbol);
	break;

    case 5:

    case 6:

    case 7:
	getafield(bf,&a_field);
	getbfield(bf,&b_field);
	printf("A- and B-field item no. %d. Address type %x address %x\n",
		itemno, a_field.addrtype,a_field.address);
	printf("                     Symbol \"%s\"\n",b_field.symbol);
	break;

    case 8:

    case 9:

    case 10:

    case 11:

    case 12:

    case 13:
	getafield(bf,&a_field);
	printf("A-field item no %d. Address type %x address %x\n",
		itemno, a_field.addrtype,a_field.address);
	break;

    case 14:
	getafield(bf,&a_field);
	printf("End of module.  Address type %x address %x\n",
		a_field.addrtype,a_field.address);
	bytesync(bf);
	break;

    case 15:

	printf("End of file symbol.\n");
	exit(0);
	break;
    }
}
