/*
** GEN_S19.C, Peter H. Anderson, MSU, 22 April, '96
**
*/
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

#define TRUE 1
#define FALSE 0

FILE *f_src, *f_write;

void banner(void);
void str_toupper(char *s1, char *s2);
int ishex(char c);
void remove_last_comma(char *s);
void make_hex_byte(char *result, char *in);
void make_org_string(char *s_w, char *s_r);
void make_data_string(char *s_w, char *s_r);

void main(void)
{
	char s_r[100], s_w[100];
	int line=0;
	banner();
	if ((f_src = fopen("a:prom.dta", "rt"))== NULL)
	{
	   printf("Error Opening A:PROM.DTA File\n");
	   exit(0);
	}
	if ((f_write=fopen("a:temp.asm", "wt"))==NULL)
	{
	   printf("Error Opening A:TEMP.ASM\n");
	}
	while (fgets(s_r, 100, f_src) != NULL)
	{
	   ++line;
	   str_toupper(s_r, s_r);
	   if(s_r[0] == '<')
	   {
		 make_org_string(s_w, s_r);
		 fprintf(f_write, "%s\n", s_w);
		 printf("Line %d - %s\n", line, s_r);
	   }
	   else if ( (isspace(s_r[0])) || (s_r[0] == '*') )
	   {
		 printf("Line %d - %s\n", line, s_r);
	   }
	   else if (s_r[0] == '.')
	   {
		 printf(".");
		 fcloseall();
		 break;
	   }
	   else
	   {
		 make_data_string(s_w, s_r);
		 fprintf(f_write, "%s\n", s_w);
		 printf("Line %d - %s\n", line, s_r);
	   }
	}
	system("a:as11.exe temp.asm -l >a:temp.lst");
	system("copy a:temp.s19 a:prom.s19");
	system("erase :temp.s19");
	printf("Done!");
}

void banner(void)
{
   clrscr();
   printf("\n\nGEN_S19.  Copyright, Peter H. Anderson, Morgan State Univer\
sity, April, '96\n\n");
   printf("\n\nIn order to run, you must have the following on your \
A: floppy\n\n");
   printf("\tGEN_S19.EXE - This file\n");
   printf("\tAS11.EXE - Motorola Assembler.  Used by MAKE_S19\n");
   printf("\tPROM.DTA - Your ASCII source data file\n");
   printf("\n\nThis program will take your PROM.DTA file and prepare a\n");
   printf("TEMP.ASM file on your floppy.\n\n");
   printf("This program will then invoke the Motorola AS11 assember.  The\n");
   printf("result will be a file PROM.S19 which you may then download to \n");
   printf("the Prom Blower.");
   printf("\n\nHit any Key to Continue:");
   getch();
   clrscr();
}

void make_org_string(char *s_w, char *s_r)
{
   int n;
   for(n=1; n<=4; n++)
   {
	 if (ishex(s_r[n]) == FALSE)
	 {
	    printf("Non Hex character encountered in address %s\n", s_r);
	    printf("Fix this and then try again\n");
	    fcloseall();
	    exit(0);
	 }
   }
   sprintf(s_w, "\t ORG $%c%c%c%c\n", s_r[1], s_r[2], s_r[3], s_r[4]);
}

void make_data_string(char *s_w, char *s_r)
{
   char s_byte[16][10];
   char s_byte2[16][10];
   int n, bytes;
   bytes = sscanf(s_r, "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
		s_byte[0], s_byte[1], s_byte[2], s_byte[3],
		s_byte[4], s_byte[5], s_byte[6], s_byte[7],
		s_byte[8], s_byte[9], s_byte[10], s_byte[11],
		s_byte[12], s_byte[13], s_byte[14], s_byte[15]);


   for(n=0; n<bytes; n++)
   {
	 make_hex_byte(s_byte2[n], s_byte[n]);
   }
   strcpy(s_w, "\tFCB ");
   for(n=0; n<bytes; n++)
   {
	 strcat(s_w, s_byte2[n]);
   }
   remove_last_comma(s_w);

}

int ishex(char c)
{
   if(isdigit(c))
   {
	 return(TRUE);
   }
   else if ( (c >= 'A') && (c<='F') )
   {
	 return(TRUE);
   }
   else
   {
	 printf("Error!  Hex Digit Expected. %c\n", c);
	 return(FALSE);
   }
}

void str_toupper(char *s1, char *s2)
{
  int n;
  for(n=0; n<strlen(s2); n++)
  {
	s1[n] = toupper(s2[n]);
  }
}

void make_hex_byte(char *result, char *in)
{
   char tmp[80];
   if ( (ishex(in[0])) && (ishex(in[1]))  )
   {
	 sprintf(tmp, "$%s, ", in);
	 strcpy(result, tmp);
   }
   else
   {
	 printf("Hex Character Expected\n");
	 fcloseall();
	 exit(0);
   }
}

void remove_last_comma(char *s)
{
   int n;
   for(n=strlen(s)-1; n>=0; n--)
   {
	 if (s[n]==',')
	 {
	    s[n] = '\0';
	    break;
	 }
   }
}