/*
 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 dv=15;

        main() {
                do{
                printf("\nWhat is the offset for your synthesizer (hex)? ");
                scanf("%4x",&offst); }
                clrscr();
                printf("Keyboard -- Ver 1.20a    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 */



   do {


                if(cv0>0) {cv0=cv0-1;  }
                if(cv1>0) {cv1=cv1-1;  }
                if(cv2>0) {cv2=cv2-1;  }
                if(cv3>0) {cv3=cv3-1;  }
                if(cv4>0) {cv4=cv4-1;  }
                if(cv5>0) {cv5=cv5-1;  }
                ch0v();
                ch1v();
                ch2v();
                ch3v();
                ch4v();
                ch5v();

                gch=bioskey(1);


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

      }
    while(gchs!=0x1b);

  silence();

}





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

void chkbd() {

gch=getch();


if(gch=='a') {ct=855; cv=225; goto arb;}
if(gch=='w') {ct=807; cv=225; goto arb;}
if(gch=='s') {ct=762; cv=225; goto arb;}
if(gch=='e') {ct=719; cv=225; goto arb;}
if(gch=='d') {ct=679; cv=225; goto arb;}
if(gch=='f') {ct=641; cv=225; goto arb;}
if(gch=='t') {ct=605; cv=225; goto arb;}
if(gch=='g') {ct=571; cv=225; goto arb;}
if(gch=='y') {ct=539; cv=225; goto arb;}
if(gch=='h') {ct=508; cv=225; goto arb;}
if(gch=='u') {ct=480; cv=225; goto arb;} /*b flat*/
if(gch=='j') {ct=453; cv=225; goto arb;} /* b    */
if(gch=='k') {ct=428; cv=225; goto arb;} /* c    */
if(gch=='o') {ct=404; cv=225; goto arb;} /* c#   */
if(gch=='l') {ct=381; cv=225; goto arb;} /* d    */
if(gch=='p') {ct=360; cv=225; goto arb;} /* d#   */
if(gch==';') {ct=339; cv=225; goto arb;} /* e    */
if(gch==39)  {ct=320; cv=225; goto arb;} /* f    */
if(gch==']') {ct=302; cv=225; goto arb;} /* f#   */
if(gch==13)  {ct=285; cv=225; goto arb;} /* g    */
goto end;
arb:
if(cv0>0) {if(cv1>0)
             {if(cv2>0)
                {if(cv3>0)
                   {if(cv4>0)
                       {if(cv5>0) {ct0=ct; cv0=cv;}
                else {ct5=ct; cv5=cv; }}
                else {ct4=ct; cv4=cv; }}
                else {ct3=ct; cv3=cv; }}
                else {ct2=ct; cv2=cv; }}
                else {ct1=ct; cv1=cv; }}
                else {ct0=ct; cv0=cv; }



ch0();
ch1();
ch2();
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() { /* channel 0 chip 2 */
        c3v=cv3/dv;
        ofst=offst;
       /* send register code */
       ofst=ofst+0x708;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x500+cv0;
       c=peekb(seg,ofst);
       }
void ch4v() { /* channel 1 chip two */
        c4v=cv4/dv;
        ofst=offst;
       /* send register code */
       ofst=ofst+0x709;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x500+cv1;
       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+cv2;
       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);
       }

