/*----------------------------------
  se-tester ver.0.00  2001.1.31
  (c) won-wan [ Tsutsumi Hiroyuki]
----------------------------------*/

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<sys/bios.h>

#define	uchar	unsigned char

int	main();
void	v_init();
void	v_cls();
void	v_spr_prt(int c,int x,int y,int p,int f);
void	v_spr_flush();
void	v_prt();
void	v_prt_win(char *t,int x,int y,int w,int h,int f);
void	a_init();
void	a_play();
void	a_stop();

int	v_sprs=0;
int	v_fonts=4;
char	v_fontdata[]={
		0x00,0x3c,0x7e,0x7e,0x7e,0x7e,0x3c,0x00,	/* graph */
		0x00,0x00,0x00,0x07,0x0f,0x1c,0x18,0x18,	/* corner */
		0x00,0x00,0x00,0xff,0xff,0x00,0x00,0x00,	/* h-wall */
		0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,	/* v-wall */
		0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
	};
enum{P_GRAPH,P_CORNER,P_HWALL,P_VWALL};
uchar	a_sin[]={
		0xa8,0xdc,0xee,0xef,0xde,0xbc,0x9a,0x89,
		0x67,0x56,0x34,0x12,0x01,0x11,0x32,0x75
	};
uchar	a_pulse[]={
		0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
	};
uchar	a_noko[]={
		0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,
		0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00
	};
int	key,key_old=KEY_START;

int	menu,smenu,edit;
int	mode,wave,pitch,sweepd,sweept,noise,volume;
char	wd[32];

char	s[256];
int	i,j;


int main()
{
	v_init();		/* init screen */
	a_init();		/* init sound */
	menu=0;smenu=0;
	edit=0;
	mode=0;wave=0;pitch=0;sweepd=0;sweept=0;noise=0;volume=0;
	while(1){
		key=key_press_check();
		if((key&KEY_START)&&(key_old==0))break;
		if((key&KEY_LEFT1)&&(key_old==0)){menu--;if(menu<0)menu=4;}
		if((key&KEY_RIGHT1)&&(key_old==0)){menu++;if(menu>4)menu=0;}
		if((key&KEY_UP1)&&(key_old==0)){
			if(menu==0){
				wave--;if(wave<0)wave=2;
				if(wave==0)for(i=0;i<16;i++){wd[i*2]=a_sin[i]&0x0f;wd[i*2+1]=a_sin[i]>>4;}
				if(wave==1)for(i=0;i<16;i++){wd[i*2]=a_pulse[i]&0x0f;wd[i*2+1]=a_pulse[i]>>4;}
				if(wave==2)for(i=0;i<16;i++){wd[i*2]=a_noko[i]&0x0f;wd[i*2+1]=a_noko[i]>>4;}
			}
			if(menu==1){mode--;if(mode<0)mode=3;}
			if(menu==2){pitch-=256;if(pitch<0)pitch=2047;}
			if((menu==3)&&(smenu==0)&&(mode==2)){sweepd-=16;if(sweepd<0)sweepd=255;}
			if((menu==3)&&(smenu==1)&&(mode==2)){sweept--;if(sweept<0)sweept=31;}
			if((menu==3)&&(mode==3)){noise--;if(noise<0)noise=7;}
			if(menu==4){volume-=16;if(volume<0)volume=255;}
		}
		if((key&KEY_DOWN1)&&(key_old==0)){
			if(menu==0){
				wave++;if(wave>2)wave=0;
				if(wave==0)for(i=0;i<16;i++){wd[i*2]=a_sin[i]&0x0f;wd[i*2+1]=a_sin[i]>>4;}
				if(wave==1)for(i=0;i<16;i++){wd[i*2]=a_pulse[i]&0x0f;wd[i*2+1]=a_pulse[i]>>4;}
				if(wave==2)for(i=0;i<16;i++){wd[i*2]=a_noko[i]&0x0f;wd[i*2+1]=a_noko[i]>>4;}
			}
			if(menu==1){mode++;if(mode>3)mode=0;}
			if(menu==2){pitch+=256;if(pitch>2047)pitch=0;}
			if((menu==3)&&(smenu==0)&&(mode==2)){sweepd+=16;if(sweepd>255)sweepd=0;}
			if((menu==3)&&(smenu==1)&&(mode==2)){sweept++;if(sweept>31)sweept=0;}
			if((menu==3)&&(mode==3)){noise++;if(noise>7)noise=0;}
			if(menu==4){volume+=16;if(volume>255)volume=0;}
		}
		if((key&KEY_B)&&(key_old==0)&&(menu==3))smenu^=1;
		if((key&KEY_A)&&(key_old==0))a_play();
		if(!(key&KEY_A))a_stop();
		v_prt();
		key_old=key;
	}
	return(0);
}

void v_prt()
{
	v_prt_win("wave",0,0,18,12,(menu==0)&&(edit==0));
	for(i=0;i<32;i++)v_spr_prt(P_GRAPH,i*4+8,80-wd[i]*4,4,0);
	v_prt_win("wave data",0,12,18,6,(menu==0)&&(edit>0));
	for(i=0;i<4;i++){
		sprintf(s,"%02x %02x %02x %02x",wd[i*8]|wd[i*8+1]<<4,wd[i*8+2]|wd[i*8+3]<<4,wd[i*8+4]|wd[i*8+5]<<4,wd[i*8+6]|wd[i*8+7]<<4);
		text_put_string(1,13+i,s);
	}
	v_prt_win("mode",18,0,10,3,menu==1);
	if(mode==0)text_put_string(20,1,"normal");
	if(mode==1)text_put_string(20,1," voice");
	if(mode==2)text_put_string(20,1," sweep");
	if(mode==3)text_put_string(20,1," noise");
	v_prt_win("pitch",18,3,10,3,menu==2);
	sprintf(s,"%04x",pitch);
	text_put_string(21,4,s);
	if((mode==0)||(mode==1)||(mode==4)){
		text_put_string(19,7,"      ");
		text_put_string(19,8,"      ");
	}
	if(mode==2){
		v_prt_win("sweep",18,6,10,3,menu==3);
		sprintf(s,"%c d=%02x",smenu==0?'>':' ',sweepd);
		text_put_string(19,7,s);
		sprintf(s,"%c t=%02x",smenu==1?'>':' ',sweept);
		text_put_string(19,8,s);
	}
	if(mode==3){
		v_prt_win("noise",18,6,10,3,menu==3);
		sprintf(s,"no=%02x",noise);
		text_put_string(20,7,s);
	}
	v_prt_win("volume",18,11,10,3,menu==4);
	sprintf(s,"%02x",volume);
	text_put_string(23,12,s);
	v_spr_flush();
}

void v_prt_win(char *t,int x,int y,int w,int h,int f)
{
	int	l=strlen(t);
	if(f){
		v_spr_prt(P_CORNER,(x++)*8,y*8,4,0);
		x+=l;
		for(i=l;i<w-2;i++)v_spr_prt(P_HWALL,(x++)*8,y*8,4,0);
		v_spr_prt(P_CORNER,x*8,(y++)*8,4,CFM_FLIP_H);
		for(i=0;i<h-2;i++)v_spr_prt(P_VWALL,x*8,(y++)*8,4,0);
		v_spr_prt(P_CORNER,(x--)*8,y*8,4,CFM_FLIP_H|CFM_FLIP_V);
		for(i=0;i<w-2;i++)v_spr_prt(P_HWALL,(x--)*8,y*8,4,0);
		v_spr_prt(P_CORNER,x*8,(y--)*8,4,CFM_FLIP_V);
		for(i=0;i<h-2;i++)v_spr_prt(P_VWALL,x*8,(y--)*8,4,0);
	}
	text_put_string(x+1,y,t);
}

void v_init()
{
	text_set_screen(SCREEN1);
	text_screen_init();
	sprite_set_range(0,0);
	font_set_monodata(0,v_fonts,v_fontdata);
	display_control(DCM_SCR1|DCM_SPR);
}

void v_cls()
{
	text_screen_init();
}

void v_title()
{
	text_put_string(20,16,"ver.0.00");
	text_put_string(20,17,"01.01.31");
	text_put_string(20,16,"(c)2001 ");
	text_put_string(20,17," won-wan");
}

void v_spr_prt(int c,int x,int y,int p,int f)
{
	if(v_sprs>127)return;
	p=(p&7)<<9;
	sprite_set_char_location(v_sprs++,c|p|f,x,y);
}

void v_spr_flush()
{
	sprite_set_range(0,v_sprs);
	v_sprs=0;
}

void a_init()
{
	sound_init();
	sound_set_output(0x0f);
	sound_set_channel(0xef);
}

void a_play()
{
	/* f=3.072*106/((2048-pitch)*32) Hz */
	/* ch0.normal ch1.voice ch2.sweep ch3.noise */
	for(i=0;i<16;i++)s[i]=wd[i*2]|wd[i*2+1]<<4;
	sound_set_wave(mode,s);
	sound_set_pitch(mode,pitch);
	sound_set_sweep(sweepd,sweept);
	sound_set_noise(noise|8);
	sound_set_noise(noise|16);
	sound_set_volume(mode,volume);
}

void a_stop()
{
	for(i=0;i<4;i++)sound_set_volume(i,0);
}
