 
//----------------------------------------------------------------------------------------------------------------------------------
//MP3D.exe
//----------------------------------------------------------------------------------------------------------------------------------
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
 
#define OUT_PORT 0x3bc
#define HIGH 0x02
#define LOW  0x00

void main( int argc, /* Number of strings in array argv */
 char *argv[],       /* Array of command-line argument strings */
 char **envp )       /* Array of environment variable strings */
{
   FILE *stream;
   int i=0, masked_inb;
   unsigned long d=0, count=0;
   char b, masked_b, buffer[256];
   //struct _finddata_t c_file;

   long hFile;
   int ch;

   //Check the command line and make sure that at least one 
   // parameter has been passed to us
   if (argc < 2)
   {
      printf("Not enough parameters used!\nTo run the program, use the following command line:\nmpegplay <filename>\n");
      exit(1);
   }

   //Copy the parameter that was passed in and check to make sure
   // that the file exists.
   sprintf(buffer, "%s", argv[1]);


      //Open the file that was chosen and start playing it!
   if ((stream = fopen(buffer, "rb")) == NULL)
   {
      printf("File [%s] Open Failed!\n", buffer);
      exit(1);
   }
   else{
      printf("Playing File: %s\n", buffer);
      printf("Press 'S' to stop playing the currently selected file.\n\n"); 
   }

   while (!feof(stream))
   {
      count++;

      //Every 100k, update the user
      if ((count % 1000) == 0) 
      {
         printf("%d kbytes \r", count/1000);
      }

      b = getc(stream);
      
      //Cycle through all 8 bits in the byte
      for (i=0; i<8; i++)
      {
         masked_b = b;
         masked_b &= 0x80;

         masked_b >>= 4;

         _outp(OUT_PORT,masked_b);
         masked_b |= 0x10;

         _outp(OUT_PORT,masked_b);
         
         masked_b &= 0x08;             
         
         _outp(OUT_PORT,masked_b);

         b <<= 1;
      }

      //Read S6 (Pin 10) ACK/NACK
      masked_inb = 0x80 & _inp(OUT_PORT + 0x01);

      
      while (masked_inb > 0)
      {
         if (_kbhit() != 0)
         {
            ch = _getch();
            ch = toupper( ch );
            if (ch == 'S')
            {
               //Close the opened file
               fclose(stream);

               //Exit the program
               exit(0);

               return;
            }
         }

         //Read S6 (Pin 10) ACK/NACK
         masked_inb = 0x80 & _inp(OUT_PORT + 0x01);
      }
   }
   //Close the opened file
   fclose(stream);

   //Exit the program
   exit(0);

   return;
}

/*
    // Display each command-line argument.
    printf( "\nCommand-line arguments:\n" );
    for( count = 0; count < argc; count++ )
        printf( "  argv[%d]   %s\n", count, argv[count] );
*/
/*
    // Display each environment variable.
    printf( "\nEnvironment variables:\n" );
    while( *envp != NULL )
        printf( "  %s\n", *(envp++) );
*/    



