/*
 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>
#include <bios.h>
/* starting address of xt rom  f000:6000 */
/* add 100h for data, 300h for address */
unsigned int seg=0xF000;
unsigned int offst=0x6000;
void senddt();
void ch0();
void ch1();
void ch2();
void ch0v();
void ch1v();
void ch2v();
void chkbd();
void silence();
int ct0,ct1,ct2;       /* tone values for registers */
int cv1,cv2;       /* volume values for registers */
int cth,ctl;
int reg,data,ofst,gch,gchs,gch2; /* variables for functions */
int n=0,c,ct02,delta=10;
float cv0c=.08,cv0;

        main() {
                do{
                printf("\nWhat is the offset for your synthesizer (hex)? ");
                scanf("%4x",&offst); }
                while(offst<=0);
                clrscr();
                printf("Keyboard -- Ver 1.10b    by Larry Battraw  (c) 1989");
                reg=7;
                data=254; /* initialize for channel zero tone only */
                senddt();
                silence();    /* initialize channels for zero volume */
                printf("\n\n\n\nPress <up> for more slurring");
                printf("\nPress <down> for less");


   do {

                
                if(ct02>ct0) {++ct0;}
                if(ct02<ct0) {--ct0;}
                ch0();
                if(cv0>0) {cv0=cv0-cv0c; ch0v(); }
                gch=bioskey(1);
                if(gch!=0) {chkbd();}

      }
    while(gchs!=0x1b);

  silence();

}





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

void chkbd() {
gch2=bioskey(1);
gch=getch();
if(gch2==0x4800) {++delta;
        gotoxy(30,15); printf("Slurring: %d   ",delta);}
if(gch2==0x5000) {if(delta!=0) --delta;
        gotoxy(30,15); printf("Slurring: %d   ",delta);}

if(gch=='a') {ct02=855; cv0=15;}
if(gch=='w') {ct02=807; cv0=15;}
if(gch=='s') {ct02=762; cv0=15;}
if(gch=='e') {ct02=719; cv0=15;}
if(gch=='d') {ct02=679; cv0=15;}
if(gch=='f') {ct02=641; cv0=15;}
if(gch=='t') {ct02=605; cv0=15;}
if(gch=='g') {ct02=571; cv0=15;}
if(gch=='y') {ct02=539; cv0=15;}
if(gch=='h') {ct02=508; cv0=15;}
if(gch=='u') {ct02=480; cv0=15;} /*b flat*/
if(gch=='j') {ct02=453; cv0=15;} /* b    */
if(gch=='k') {ct02=428; cv0=15;} /* c    */
if(gch=='o') {ct02=404; cv0=15;} /* c#   */
if(gch=='l') {ct02=381; cv0=15;} /* d    */
if(gch=='p') {ct02=360; cv0=15;} /* d#   */
if(gch==';') {ct02=339; cv0=15;} /* e    */
if(gch==39)  {ct02=320; cv0=15;} /* f    */
if(gch==']') {ct02=302; cv0=15;} /* f#   */
if(gch==13)  {ct02=285; cv0=15;} /* g    */
ch0();
                if(ct02>ct0 && cv0==15) {ct0=ct02-delta;}
                if(ct02<ct0 && cv0==15) {ct0=ct02+delta;}

gchs=gch;
gch=0;

}

 void senddt() {

       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 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 ch0v() {
        ofst=offst;
       /* send register code */
       ofst=ofst+0x308;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100+cv0;
       c=peekb(seg,ofst);
       }
void ch1v() {
        ofst=offst;
       /* send register code */
       ofst=ofst+0x309;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100+cv1;
       c=peekb(seg,ofst);
       }
void ch2v() {
        ofst=offst;
       /* send register code */
       ofst=ofst+0x30A;
       c=peekb(seg,ofst);
       ofst=offst;
       /* send data */
       ofst=ofst+0x100+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);
       }
