/* XY2ADDR converts xy coordinates to screen address offset */
/* the input file should have the coordinates entered like  */
/* this: X,Y<space>X,Y<space>X,Y and so on.                 */

#include <stdio.h>


FILE* infile;
FILE* outfile;

main(int argc,char **argv)
{
int cntr = 0;
unsigned int xval,yval,adr,buf_off;
int ch;
unsigned char inbuf[10];

       if(argc > 2)
       {
               infile = fopen(argv[1],"rb");
               outfile = fopen(argv[2],"wt");

               fprintf(outfile,"dw ");

               while(! feof(infile))
               {
                       buf_off = 0;

                       do
                         ch = getc(infile);
                       while((ch < '0' && ch != EOF) || ch > '9');
                       if(ch == EOF) quit();
                       inbuf[buf_off++] = ch;
                       while( (ch = getc(infile)) >= '0' )
                               inbuf[buf_off++] = ch;
                       inbuf[buf_off] = 0;
                       xval = atoi(inbuf);

                       buf_off = 0;

                       do
                         ch = getc(infile);
                       while((ch < '0' && ch != EOF) || ch > '9');
                       if(ch == EOF) quit();
                       inbuf[buf_off++] = ch;
                       while((ch = getc(infile)) >= '0')
                       inbuf[buf_off++] = ch;
                       inbuf[buf_off] = 0;
                       yval = atoi(inbuf);

                       adr = (yval*320)+xval;
                       fprintf(outfile,"%05u",adr);
                       cntr++;
                       if(cntr == 14)
                       {
                               fprintf(outfile,"\ndw ");
                               cntr = 0;
                       }
                       else
                               fprintf(outfile,",");
               }
               quit();
       }
       else
       {
               printf("XY2ADDR converts xy coordinates to screen address offset\n");
               printf("usage: xy2addr <xyfile> <outfile>\n");
       }
}

quit()
{
       fprintf(outfile,"0ffffh\n");
       fclose(infile);
       fclose(outfile);
       exit(0);
}