/*

*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <dos.h>
#include <synth.h>
#include <conio.h>
/* starting address of xt rom  f000:6000 */
/* add 100h for data, 300h for address */
unsigned int seg=0xF000;
unsigned int offst=0x6000;

void chkbd(void);
void updtt0(void);
void updtt1(void);
void updtt2(void);
void updtv0(void);
void updtv1(void);
void updtv2(void);

int gch,gchs,gch2;
int n=0,c,cv,ct;
int csv0,csv1,csv2;
int c0,c1,c2,c3,c4,c5;
int c0t,c1t,c2t,c3t,c4t,c5t;
int cv0,cv1,cv2,cv3,cv4,cv5;
int ca0=1,ca1=1,ca2=1;
int dvu,dv,vib,rt;

        main() {offst=readfil();
                clrscr();
                printf("Keyboard -- Ver 1.85b    by Larry Battraw  (c) 1990");
                senddt0(7,248);
                senddt1(7,248);
                silence();    /* initialize channels for zero volume */
                printf("\n\n\nDecay value (larger increases time)? ");
                scanf("%d",&dv);
                dvu=dv*8;
/*                printf("\nVRise time? ");
                scanf("%d",&rt);
                */
                rt=dv*3/2;
                printf("\nVibrato value? ");
                scanf("%d",&vib);
                clrscr();
                printf("Keyboard -- Ver 1.85b    by Larry Battraw  (c) 1990");

   do {


                if(csv0>0) {csv0=csv0-1; cv0=csv0/dv; ch0v(cv0); ch3v(cv0);
                c0t=c0t+ca0;
                if(c0t>c0+vib || c0t<c0-vib) ca0=ca0*-1;
                updtt0();
                gch=bioskey(1);
                if(gch!=0) {chkbd();}
                }

                if(csv1>0) {csv1=csv1-1; cv1=csv1/dv; ch1v(cv1); ch4v(cv1);
                c1t=c1t+ca1;
                if(c1t>c1+vib || c1t<c1-vib) ca1=ca1*-1;
                updtt1();
                gch=bioskey(1);
                if(gch!=0) {chkbd();}
                }

                if(csv2>0) {csv2=csv2-1; cv2=csv2/dv; ch2v(cv2); ch5v(cv2);
                c2t=c2t+ca2;
                if(c2t>c2+vib || c2t<c2-vib) ca2=ca2*-1;
                updtt2();
                gch=bioskey(1);
                if(gch!=0) {chkbd();}
                }

                gch=bioskey(1);
                if(gch!=0) {chkbd();}

      }
    while(gchs!=0x1b);

  silence();

}





/*   ------functions on registers -------------------- */

void chkbd() {

st:
gch=getch();


if(gch=='a') {ct=855; goto arb;}
if(gch=='w') {ct=807; goto arb;}
if(gch=='s') {ct=762; goto arb;}
if(gch=='e') {ct=719; goto arb;}
if(gch=='d') {ct=679; goto arb;}
if(gch=='f') {ct=641; goto arb;}
if(gch=='t') {ct=605; goto arb;}
if(gch=='g') {ct=571; goto arb;}
if(gch=='y') {ct=539; goto arb;}
if(gch=='h') {ct=508; goto arb;}
if(gch=='u') {ct=480; goto arb;} /*b flat*/
if(gch=='j') {ct=453; goto arb;} /* b    */
if(gch=='k') {ct=428; goto arb;} /* c    */
if(gch=='o') {ct=404; goto arb;} /* c#   */
if(gch=='l') {ct=381; goto arb;} /* d    */
if(gch=='p') {ct=360; goto arb;} /* d#   */
if(gch==';') {ct=339; goto arb;} /* e    */
if(gch==39)  {ct=320; goto arb;} /* f    */
if(gch==']') {ct=302; goto arb;} /* f#   */
if(gch==13)  {ct=285; goto arb;} /* g    */
goto end;
arb:
if(ct==c0) {if(csv0<dvu) {csv0=csv0+rt; cv0=csv0/dv; updtv0(); } goto end; }
if(ct==c1) {if(csv1<dvu) {csv1=csv1+rt; cv1=csv1/dv; updtv1(); } goto end; }
if(ct==c2) {if(csv2<dvu) {csv2=csv2+rt; cv2=csv2/dv; updtv2(); } goto end; }


if(cv0>0) {if(cv1>0)

{if(cv2>0) {c0t=ct; updtt0();
            if(csv0<dv) {csv0=csv0+rt; cv0=csv0/dv; updtv0();}
                                                              }


else {c2t=ct; updtt2(); if(csv2<dvu)
{csv2=csv2+rt; updtv2();}
}}
else {c1t=ct; updtt1(); if(csv1<dvu)
{csv1=csv1+rt; updtv1();}
}}
else {c0t=ct; updtt0(); if(csv0<dvu)
{csv0=csv0+rt; updtv0();}
}


/*
Error b:\kbd3d.c 127: Undefined label 'end' in function chkbd
Error b:\kbd3d.c 128: Declaration syntax error
Error b:\kbd3d.c 173: Compound statement missing } in function chkbd
*/





c0=c0t;
c1=c1t;
c2=c2t;

end:
gchs=gch;
gch=bioskey(1);
if(gch!=0) goto st;

}
void updtt0(void) {
ch0(c0t); ch3(c0t-3);
}
void updtt1(void) {
ch1(c1t); ch4(c1t-3);
}
void updtt2(void) {
ch2(c2t); ch5(c2t-3);
}
void updtv0(void) {
ch0v(cv0); ch3v(cv0);
}
void updtv1(void) {
ch1v(cv1); ch4v(cv1);
}
void updtv2(void) {
ch2v(cv2); ch5v(cv2);
}

