#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <memory.h>
#include "cd_play.h"
#include "cd_time.h"

#define locate(x,y)     printf("\x1b[%d;%dH",y+1,x+1)

void            start(void);
unsigned char   k_buf(void);
unsigned char   k_read(unsigned char *);
unsigned char   far *tvram=(unsigned char far *)0xc8000000l;


static unsigned char    data[300],sdat[6],status[10],tdat[3];
int                     i,st,ed,sst,eed,vm;
unsigned char           rep=0,hlp=0,ps=0,dt=0,es=0;
unsigned char           rt,ge,_cl;
long                    fl,fl2;
unsigned                stt;
int                     vol=0;
char                    mt[17][17]={"\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1",
				    "\2\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1",
				    "\2\2\1\1\1\1\1\1\1\1\1\1\1\1\1\1",
				    "\2\2\2\1\1\1\1\1\1\1\1\1\1\1\1\1",
				    "\2\2\2\2\1\1\1\1\1\1\1\1\1\1\1\1",
				    "\2\2\2\2\2\1\1\1\1\1\1\1\1\1\1\1",
				    "\2\2\2\2\2\2\1\1\1\1\1\1\1\1\1\1",
				    "\2\2\2\2\2\2\2\1\1\1\1\1\1\1\1\1",
				    "\2\2\2\2\2\2\2\2\1\1\1\1\1\1\1\1",
				    "\2\2\2\2\2\2\2\2\2\1\1\1\1\1\1\1",
				    "\2\2\2\2\2\2\2\2\2\2\1\1\1\1\1\1",
				    "\2\2\2\2\2\2\2\2\2\2\2\1\1\1\1\1",
				    "\2\2\2\2\2\2\2\2\2\2\2\2\1\1\1\1",
				    "\2\2\2\2\2\2\2\2\2\2\2\2\2\1\1\1",
				    "\2\2\2\2\2\2\2\2\2\2\2\2\2\2\1\1",
				    "\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\1",
				    "\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2"};

void main(int argc,char *argv[])
{
/*  キーボード初期化  */
	_asm{
	  mov ah,0x00
	  int 0x90
	  mov ah,0x05
	  mov al,0x01
	  int 0x90
	  mov ah,0x06
	  mov al,0x00
	  int 0x90
	  }

	cd_vol_on();
	cd_vol(63);
	for(i=1;i<=argc;i++){
	  if(argv[i][1]=='?') hlp=1;
	  if((argv[i][1]=='r')||(argv[i][1]=='R')) rep=1;
	  }

	if(hlp==1){
	  printf("********************************************************************\n");
	  printf("**  ＦＭ Ｔｏｗｎｓ用ＭＳ−ＤＯＳ用ＣＤ演奏プログラム  by N.Baba  **\n");
	  printf("********************************************************************\n");
	  printf("DOS_CD [<option>]\n");
	  printf("  <option> /R デフォルトでリピートモード\n");
	  printf("           /? へるぷだよー\n");
	exit(0);
	  }
/*  ＣＤ情報の読み取り  */
	cd_pause();
	rt=toc_read(data);
	cd_restart();

/*  画面表示  */
	printf("\x1b[0m\x1b*\x1b[1v");
	printf("\x1b[0;33m1:\x1b[7;36m演奏開始");
	printf("\x1b[0;33m 2:\x1b[7;36m停止");
	printf("\x1b[0;33m 3:\x1b[7;36m一時停止");
	printf("\x1b[0;33m 4:\x1b[7;36mリピートモード");
	printf("\x1b[0;33m 5:\x1b[7;36m終了");
	locate(0,5);
	printf("\x1b[0;33m開始曲番号:\x1b[7;36m\x1b\x1f\x1b[0m   \x1b[7;36m\x1b\x1e\x1b[0m\n");
	printf("\x1b[0;33m終了曲番号:\x1b[7;36m\x1b\x1d\x1b[0m   \x1b[7;36m\x1b\x1c\x1b[0m\n");
	locate(0,8);
	printf("\x1b[7;32m 演奏曲番号 \x1b[0m    \x1b[7;36m曲目");
	locate(0,10);
	printf("\x1b[7;32m曲内演奏時間\x1b[0m    \x1b[7;36m分  秒  フレーム\x1b[0m /     \x1b[7;36m分  秒  フレーム\n");
	printf("\x1b[7;32m全体演奏時間\x1b[0m    \x1b[7;36m分  秒  フレーム\x1b[0m /     \x1b[7;36m分  秒  フレーム\n");
	printf("\x1b[0m");

/*  正常に読めた  */
      if(rt==0x00){
	  if((data[0]&1)==0){
	    rt=toc_read(data);
	    }
	  if((data[0]&2)!=0){
	    dt=1;
	    locate(0,2);
	    printf("データトラックが有るなー...          \n");
	    }
	  if((data[0]&1)==0){
	    locate(0,2);
	    printf("オーディオデータが見つからないよー...\n");
	    }
	  }
/*  データトラックが有ったら...  */
	if(dt==1){
	  for(i=0;i<(int)(data[2]);i++){
	    if((data[6+i*3]&0x80)==0) break;
	    }
	  st=i+1;
	  }
	else st=data[1];
	ed=data[2];
	sst=st;
	eed=ed;

	msf2f(data[3],data[4],data[5],&fl);
	fl--;
	f2msf(fl,&data[ed*3+6],&data[ed*3+7],&data[ed*3+8]);

/*  選ぶ  */
	locate(12,5);
	printf("%03d",st);
	locate(12,6);
	printf("%03d",ed);

/*  main  */
	locate(31,0);
	printf("\x1b[7;%dmリピートモード\x1b[0m",36-rep*5);
	locate(0,20);
	printf("\x1b[7;32mLEVEL\x1b[0;34m....｡｡｡｡ooooOOOO \x1b[0m");

	outp(0x04e8,0x00);

	for(;;){
/*  ＣＤ演奏状態表示  */
	  stt=cd_status(status);
	  if((((stt&0xff00)>>8)==0x80)){
	    locate(0,2);
	    printf("\x1b[7;31mopen...\x1b[0m                               \n");
	    while(toc_read(data)!=0x00){
	      if(k_read(&ge)!=0xff){
		if(ge==0x01){
		  es=1;
		  break;
		  }
		}
	      }
	    if(es==1){
	      printf("\x1b*");
	      break;
	      }
	    locate(0,2);
	    printf("       \n");
	    if(rt==0x00){
	      if((data[0]&1)==0){
		rt=toc_read(data);
		}
	      if((data[0]&2)!=0){
		dt=1;
		locate(0,2);
		printf("データトラックが有るなー...          \n");
		}
	      if((data[0]&1)==0){
		locate(0,2);
		printf("オーディオデータが見つからないよー...\n");
		}
	      }
	    if(dt==1){
	      for(i=0;i<(int)(data[2]);i++){
		if((data[6+i*3]&0x80)==0) break;
		}
	      st=i+1;
	      }
	    else st=data[1];
	    ed=data[2];
	    sst=st;
	    eed=ed;

	    msf2f(data[3],data[4],data[5],&fl);
	    fl--;
	    f2msf(fl,&data[ed*3+6],&data[ed*3+7],&data[ed*3+8]);

	    locate(12,5);
	    printf("%03d",st);
	    locate(12,6);
	    printf("%03d",ed);
	    }
	  if(((stt&1)==0)&&(ps==0)){
	    status[3]=0; status[4]=0; status[5]=0;
	    status[7]=0; status[8]=0; status[9]=0;
	    locate(13,8);
	    printf("---");
	    }
	  else{
	    locate(13,8);
	    printf("%03d/%03d",status[1],data[2]);
	    }
	  locate(13,10);
	  printf("%03d\x1c\x1c%02d\x1c\x1c%02d",status[3],status[4],status[5]);
	  locate(13,11);
	  printf("%03d\x1c\x1c%02d\x1c\x1c%02d",status[7],status[8],status[9]);

	  msf2f(data[status[1]*3+3],data[status[1]*3+4],data[status[1]*3+5],&fl);
	  msf2f(data[status[1]*3+6],data[status[1]*3+7],data[status[1]*3+8],&fl2);
	  fl=fl2-fl;
	  f2msf(fl,&tdat[0],&tdat[1],&tdat[2]);


	  locate(36,10);
	  printf("%03d\x1c\x1c%02d\x1c\x1c%02d",tdat[0],tdat[1],tdat[2]);
	  locate(36,11);
	  printf("%03d\x1c\x1c%02d\x1c\x1c%02d",data[3],data[4],data[5]);

	  vm=inp(0x04e7);
	  if(((inp(0x04e8)&0x01)==0x01)&&((vm&0x80)==0)){
	    vol=(0x7f-vm)/8;
	    for(i=0;i<16;i++) *(tvram+3200+11+i*2)=mt[vol][i];
	    }

	  if(k_read(&ge)!=0xff){
	    if((((ge==0x02)||(ge==0x42)||(ge==0x1d)||(ge==0x45))&&(ps==0)&&((stt&1)==0))||(ge==0x73)){
	      locate(2,0);
	      printf("\x1b[7;%dm演奏開始\x1b[0m",31);
	      cd_stop();
	      start();
	      locate(2,0);
	      printf("\x1b[7;%dm演奏開始\x1b[0m",36);
	      ps=0;
	      locate(20,0);
	      printf("\x1b[7;%dm一時停止\x1b[0m",36-ps*5);
	      }
	    if((ge==0x03)||(ge==0x43)||(ge==0x35)){
	      locate(13,0);
	      printf("\x1b[7;%dm停止\x1b[0m",31);
	      cd_stop();
	      locate(13,0);
	      printf("\x1b[7;%dm停止\x1b[0m",36);
	      ps=0;
	      locate(20,0);
	      printf("\x1b[7;%dm一時停止\x1b[0m",36-ps*5);
	      }
	    if((ge==0x04)||(ge==0x44)){
	      if((stt&1)==1){
		cd_pause();
		ps=1;
		}
	      if((stt&1)==0){
		cd_restart();
		ps=0;
		}
	      locate(20,0);
	      printf("\x1b[7;%dm一時停止\x1b[0m",36-ps*5);
	      }
	    if((ge==0x05)||(ge==0x3e)){
	      rep=(unsigned char)(rep==0);
	      locate(31,0);
	      printf("\x1b[7;%dmリピートモード\x1b[0m",36-rep*5);
	      }

	    if((ge==0x50)) if(st>sst){
	      st--;
	      }
	    if(ge==0x4d) if(st<eed){
	      st++;
	      if(st>ed) ed++;
	      }
	    if(ge==0x4f) if(ed>st) ed--;
	    if(ge==0x51) if(ed<eed) ed++;

	    locate(12,5);
	    printf("%03d",st);
	    locate(12,6);
	    printf("%03d",ed);

	    if((ge==0x01)&&(ps==0)){
	      printf("\x1b*");
	      break;
	      }
	    if(((ge==0x06)||(ge==0x3f))&&(ps==0)){
	      locate(0,20);
	      printf("                      ");
	      locate(0,12);
	      break;
	      }
	    }
	  }

/*  カーソル表示  */
	printf("\x1b[0v");
}


void start(void)
{
	memcpy(sdat,data+6+(st-1)*3,3);
	memcpy(sdat+3,data+6+ed*3,3);
	cd_play(sdat,rep,0);
}


unsigned char   k_buf(void)
{
unsigned char   rt;

	_asm{
	  mov ah,0x07
	  int 0x90
	  mov rt,dh
	  }
	return rt;
}

unsigned char   k_read(unsigned char *key)
{
unsigned char   rt,k;

	_asm{
	  mov ah,0x09
	  mov al,0x01
	  int 0x90
	  mov k,bh
	  mov rt,dh
	  }
	*key=k;
	return rt;
}

