#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "main.h"
#include "ym_conv.h"

int convert_ym3(unsigned char **out_buf, unsigned char *in_buf, int len)
{
 int pos=4;
 int todo=((len-4)/14);
 int vbl_len=todo;
 int out_counter = 0;
 printf("VBL found: %i\n",todo);
 strcpy(song_name, file_name);

 while( todo )
 {
  int i;
  for (i=0;i<11;i++)
  {
   unsigned char *out = out_buf[i];
   unsigned char poker = in_buf[pos+(i*((len-4)/14))];

    if ((i==1)||(i==3)||(i==5))
     poker &= 1+2+4+8;

    if ((i==6) )//||(i==8)||(i==9)||(i==10))
     poker &= 1+2+4+8+16;

    if (i==7)
     poker &= 1+2+4+8+16+32;

// for vectrex
    if ((i==8)||(i==9)||(i==10))
     poker &= 1+2+4+8;
    out[out_counter] = poker;
  }
  out_counter++;
  pos++;
  todo--;
 }
 return vbl_len;
}

int convert_ym3b(unsigned char **out_buf, unsigned char *in_buf, int len)
{
 return convert_ym3(out_buf, in_buf, len);
}

int convert_ym4(unsigned char **out_buf, unsigned char *in_buf)
{
 unsigned char c1;
 unsigned char c2;
 unsigned char c3;
 unsigned char c4;
 int pos=12;

 c1 = in_buf[pos++];
 c2 = in_buf[pos++];
 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 int todo=c1*256*256*256+c2*256*256+c3*256+c4;
 int vbl_len=todo;
 int out_counter = 0;
 printf("VBL found: %i\n",todo);
 int interleave_length=todo;

 c1 = in_buf[pos++];
 c2 = in_buf[pos++];
 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 long attribut=c1*256*256*256+c2*256*256+c3*256+c4;
 printf("Attributs found: %08X\n",attribut);

 c1 = in_buf[pos++];
 c2 = in_buf[pos++];
 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 int samples=c1*256*256*256+c2*256*256+c3*256+c4;
 if (samples)
  printf("Samples found: %i (not converted!)\n",samples);

 c1 = in_buf[pos++];
 c2 = in_buf[pos++];
 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 printf("Frame loop start: %i\n",c1*256*256*256+c2*256*256+c3*256+c4);

 // skip samples!
 while (samples)
 {
  long sample_length;
  c1 = in_buf[pos++];
  c2 = in_buf[pos++];
  c3 = in_buf[pos++];
  c4 = in_buf[pos++];
  sample_length=c1*256*256*256+c2*256*256+c3*256+c4;
  pos+=sample_length;
  samples--;
 }
 printf("Name of song: %s\n", (char *)in_buf+pos);
 strcpy(song_name, (char *)in_buf+pos);

 pos+=strlen((char *)in_buf+pos)+1;

 printf("Name of author: %s\n", (char *)in_buf+pos);
 pos+=strlen((char *)in_buf+pos)+1;

 printf("Comment: %s\n", (char *)in_buf+pos);
 pos+=strlen((char *)in_buf+pos)+1;

 int interleave=attribut&1;
 if (interleave)
 {
  printf("Using interleave format!\n");
  while( todo )
  {
   int i;
   for (i=0;i<11;i++)
   {
    unsigned char poker = in_buf[pos+i*interleave_length];
    unsigned char *out = out_buf[i];
    if ((i==1)||(i==3)||(i==5))
     poker &= 1+2+4+8;

    if ((i==6) )//||(i==8)||(i==9)||(i==10))
     poker &= 1+2+4+8+16;

    if (i==7)
     poker &= 1+2+4+8+16+32;
// for vectrex
    if ((i==8)||(i==9)||(i==10))
     poker &= 1+2+4+8;
    out[out_counter] = poker;
   }
   out_counter++;
   pos++;
   todo--;
  }
 }
 else
 {
  printf("Using non interleave format!\n");
  printf("Not supported yet!\n");
  return 0;
 }
 return vbl_len;
}

int convert_ym5(unsigned char *out_buf[], unsigned char *in_buf)
{
 unsigned char c1;
 unsigned char c2;
 unsigned char c3;
 unsigned char c4;
 int pos=12;
 int out_counter = 0;

 c1 = in_buf[pos++];
 c2 = in_buf[pos++];
 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 int todo=c1*256*256*256+c2*256*256+c3*256+c4;
 int vbl_len=todo;
 printf("VBL found: %i\n",todo);
 int interleave_length=todo;

 c1 = in_buf[pos++];
 c2 = in_buf[pos++];
 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 long attribut=c1*256*256*256+c2*256*256+c3*256+c4;
 printf("Attributs found: %08X\n",attribut);

 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 int samples=c3*256+c4;
 if (samples)
  printf("Samples found: %i (not converted!)\n",samples);

 c1 = in_buf[pos++];
 c2 = in_buf[pos++];
 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 printf("YM2149 External frequency in Hz: %i\n",c1*256*256*256+c2*256*256+c3*256+c4);

 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 printf("Player frequency in Hz: %i\n",c3*256+c4);

 c1 = in_buf[pos++];
 c2 = in_buf[pos++];
 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 printf("Vbl number to loop the song: %i (unused)\n",c1*256*256*256+c2*256*256+c3*256+c4);

 c3 = in_buf[pos++];
 c4 = in_buf[pos++];
 printf("Size (in bytes) of future data: %i\n",c3*256+c4);

 // skip samples!
 while (samples)
 {
  long sample_length;
  c1 = in_buf[pos++];
  c2 = in_buf[pos++];
  c3 = in_buf[pos++];
  c4 = in_buf[pos++];
  sample_length=c1*256*256*256+c2*256*256+c3*256+c4;
  pos+=sample_length;
  samples--;
 }
 printf("Name of song: %s\n", (char *)in_buf+pos);
 strcpy(song_name, (char *)in_buf+pos);
 pos+=strlen((char *)in_buf+pos)+1;

 printf("Name of author: %s\n", (char *)in_buf+pos);
 pos+=strlen((char *)in_buf+pos)+1;

 printf("Comment: %s\n", (char *)in_buf+pos);
 pos+=strlen((char *)in_buf+pos)+1;

 int interleave=attribut&1;
 if (interleave)
 {
  printf("Using interleave format!\n");
  while( todo )
  {
   int i;
   for (i=0;i<11;i++)
   {
    unsigned char poker = in_buf[pos+i*interleave_length];
    unsigned char *out = out_buf[i];
    if ((i==1)||(i==3)||(i==5))
     poker &= 1+2+4+8;
    if ((i==6) )//||(i==8)||(i==9)||(i==10))
     poker &= 1+2+4+8+16;

    if (i==7)
     poker &= 1+2+4+8+16+32;
// for vectrex
    if ((i==8)||(i==9)||(i==10))
     poker &= 1+2+4+8;
    out[out_counter] = poker;
   }
   out_counter++;
   pos++;
   todo--;
  }
 }
 else
 {
  printf("Using non interleave format!\n");
  printf("Not supported yet!\n");
  return 0;
 }
 return vbl_len;
}
