/*
 all roms start at f000:6000 and end at f000:ffff
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <dos.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 senddt0();
void senddt1();
void ch0();
void ch1();
void ch2();
void ch3();
void ch4();
void ch5();
void ch0v();
void ch1v();
void ch2v();
void ch3v();
void ch4v();
void ch5v();
void chkbd();
void silence();
int ct0,ct1,ct2,ct3,ct4,ct5;       /* tone values for registers */
int cv0,cv1,cv2,cv3,cv4,cv5;       /* volume values for registers */
int cth,ctl;
int reg,data,ofst,gch,gchs,gch2; /* variables for functions */
int n=0,c,cv,ct,c0v,c1v,c2v,c3v,c4v,c5v;
int c0,c1,c2,c3,c4,c5;
int ca0=1,ca1=1,ca2=1,ca3=1,ca4=1,ca5=1;
int ca20=1,ca21=1,ca22=1,ca23=1,ca24=1,ca25=1;
int dvu,dv,vib,vib2;

        main() {
                do{
                printf("\nWhat is the offset for your synthesizer (hex)? ");
                scanf("%4x",&offst); }
                clrscr();
                printf("Keyboard -- Ver 1.50a    by Larry Battraw  (c) 1989");
                reg=7;
                data=248; /* initialize for channel zero tone only */
                senddt0();
                reg=7;
                data=248; /* initialize for channel zero tone only */
                senddt1();
                silence();    /* initialize channels for zero volume */
                printf("\n\n\nDecay value (larger increases time)? ");
                scanf("%d",&dv);
                dvu=dv*15;
                printf("\nMain vibrato value? ");
                scanf("%d",&vib);
                clrscr();
                printf("Keyboard -- Ver 1.50a    by Larry Battraw  (c) 1989");

   do {


                if(cv0>0) {cv0=cv0-1; ch0v(); ct0=ct0+ca0;
                if(ct0>c0+vib || ct0<c0-vib) ca0=ca0*-1; ch0();
                ct3=ct0-1;
                ch3();
                }

                if(cv1>0) {cv1=cv1-1; ch1v(); ct1=ct1+ca1;
                if(ct1>c1+vib || ct1<c1-vib) ca1=ca1*-1; ch1();
                ct4=ct1-1;
                ch4();
                }

                if(cv2>0) {cv2=cv2-1; ch2v(); ct2=ct2+ca2;
                if(ct2>c2+vib || ct2<c2-vib) ca2=ca2*-1; ch2();
                ct5=ct2-1;
                ch5();
                }

              
                
               
                
                

                gch=bioskey(1);


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

      }
    while(gchs!=0x1b);

  silence();

}





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

void chkbd() {

gch=getch();


if(gch=='a') {ct=855; cv=dvu; goto arb;}
if(gch=='w') {ct=807; cv=dvu; goto arb;}
if(gch=='s') {ct=762; cv=dvu; goto arb;}
if(gch=='e') {ct=719; cv=dvu; goto arb;}

if(gch=='d') {ct=679; cv=dvu; goto arb;}
if(gch=='f') {ct=641; cv=dvu; goto arb;}
if(gch=='t') {ct=605; cv=dvu; goto arb;}
if(gch=='g') {ct=571; cv=dvu; goto arb;}
if(gch=='y') {ct=539; cv=dvu; goto arb;}
if(gch=='h') {ct=508; cv=dvu; goto arb;}
if(gch=='u') {ct=480; cv=dvu; goto arb;} /*b flat*/
if(gch=='j') {ct=453; cv=dvu; goto arb;} /* b    */
if(gch=='k') {ct=428; cv=dvu; goto arb;} /* c    */
if(gch=='o') {ct=404; cv=dvu; goto arb;} /* c#   */
if(gch=='l') {ct=381; cv=dvu; goto arb;} /* d    */
if(gch=='p') {ct=360; cv=dvu; goto arb;} /* d#   */
if(gch==';') {ct=339; cv=dvu; goto arb;} /* e    */
if(gch==39)  {ct=320; cv=dvu; goto arb;} /* f    */
if(gch==']') {ct=302; cv=dvu; goto arb;} /* f#   */
if(gch==13)  {ct=285; cv=dvu; goto arb;} /* g    */
goto end;
arb:
if(ct==c0) {cv0=cv; goto end;}
if(ct==c1) {cv1=cv; goto end;}
if(ct==c2) {cv2=cv; goto end;}


if(cv0>0) {if(cv1>0)
             {if(cv2>0)
                {if(cv3>0)  {ct0=ct; cv0=cv;}
                else {ct2=ct; cv2=cv; }}
                else {ct1=ct; cv1=cv; }}
else {ct0=ct; cv0=cv; }

c0=ct0;
c1=ct1;
c2=ct2;

ch0();
ch1();
ch2();
ct3=ct0-1;
ct4=ct1-1;
ct5=ct2-1;
ch3();
ch4();
ch5();
end:
gchs=gch;
gch=0;

}

 void senddt0() {

       ofst=offst;
       /* send register code */
       ofst=ofst+0x300+reg;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100+data;
       c=peekb(seg,ofst);
       }
 void senddt1() {

       ofst=offst;
       /* send register code */
       ofst=ofst+0x700+reg;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x500+data;
       c=peekb(seg,ofst);
       }


void ch0() {
        cth=ct0/255;
        ctl=ct0-cth*255;
        ofst=offst;
       /* send register code1 */
       ofst=ofst+0x300;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datalow */
       ofst=ofst+0x100+ctl;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send register code2*/
       ofst=ofst+0x301;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datahigh */
       ofst=ofst+0x100+cth;
       c=peekb(seg,ofst);
       }

void ch1() {
        cth=ct1/255;
        ctl=ct1-cth*255;
        ofst=offst;
       /* send register code1 */
       ofst=ofst+0x302;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datalow */
       ofst=ofst+0x100+ctl;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send register code2*/
       ofst=ofst+0x303;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datahigh */
       ofst=ofst+0x100+cth;
       c=peekb(seg,ofst);

       }

void ch2() {
        cth=ct2/255;
        ctl=ct2-cth*255;
        ofst=offst;
       /* send register code1 */
       ofst=ofst+0x304;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datalow */
       ofst=ofst+0x100+ctl;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send register code2*/
       ofst=ofst+0x305;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datahigh */
       ofst=ofst+0x100+cth;
       c=peekb(seg,ofst);

       }

void ch3() {         /* channel 0 of chip two... */
        cth=ct3/255;
        ctl=ct3-cth*255;
        ofst=offst;
       /* send register code1 */
       ofst=ofst+0x700;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datalow */
       ofst=ofst+0x500+ctl;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send register code2*/
       ofst=ofst+0x701;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datahigh */
       ofst=ofst+0x500+cth;
       c=peekb(seg,ofst);
       }

void ch4() { /* channel 1 chip two */
        cth=ct4/255;
        ctl=ct4-cth*255;
        ofst=offst;
       /* send register code1 */
       ofst=ofst+0x702;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datalow */
       ofst=ofst+0x500+ctl;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send register code2*/
       ofst=ofst+0x703;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datahigh */
       ofst=ofst+0x500+cth;
       c=peekb(seg,ofst);

       }
void ch5() { /* channel 2 chip 2 */
        cth=ct5/255;
        ctl=ct5-cth*255;
        ofst=offst;
       /* send register code1 */
       ofst=ofst+0x704;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datalow */
       ofst=ofst+0x500+ctl;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send register code2*/
       ofst=ofst+0x705;
       c=peekb(seg,ofst);
        ofst=offst;
       /* send datahigh */
       ofst=ofst+0x500+cth;
       c=peekb(seg,ofst);

       }

void ch0v() {
        c0v=cv0/dv;
        ofst=offst;
       /* send register code */
       ofst=ofst+0x308;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100+c0v;
       c=peekb(seg,ofst);
       }
void ch1v() {
        c1v=cv1/dv;
        ofst=offst;
       /* send register code */
       ofst=ofst+0x309;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100+c1v;
       c=peekb(seg,ofst);
       }
void ch2v() {
        c2v=cv2/dv;
        ofst=offst;
       /* send register code */
       ofst=ofst+0x30A;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100+c2v;
       c=peekb(seg,ofst);
       }
void ch3v() {
        c3v=cv3/dv;
        ofst=offst;
       /* send register code */
       ofst=ofst+0x708;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x500+c3v;
       c=peekb(seg,ofst);
       }
void ch4v() {
        c4v=cv4/dv;
        ofst=offst;
       /* send register code */
       ofst=ofst+0x709;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x500+c4v;
       c=peekb(seg,ofst);
       }
void ch5v() {
        c5v=cv5/dv;
        ofst=offst;
       /* send register code */
       ofst=ofst+0x70A;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x500+c5v;
       c=peekb(seg,ofst);
       }

void silence() {
/* channel 0 */
        ofst=offst;
       /* send register code */
       ofst=ofst+0x308;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100;
       c=peekb(seg,ofst);
/* channel 1 */
        ofst=offst;
       /* send register code */
       ofst=ofst+0x309;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100;
       c=peekb(seg,ofst);
/* channel 2 */
        ofst=offst;
       /* send register code */
       ofst=ofst+0x30a;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100;
       c=peekb(seg,ofst);
/* channel 3 */
        ofst=offst;
       /* send register code */
       ofst=ofst+0x708;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x500;
       c=peekb(seg,ofst);
/* channel 4 */
        ofst=offst;
       /* send register code */
       ofst=ofst+0x709;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x500;
       c=peekb(seg,ofst);
/* channel 5 */
        ofst=offst;
       /* send register code */
       ofst=ofst+0x70a;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x500;
       c=peekb(seg,ofst);
       }

