#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <dir.h>
#include <io.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include "tcu.h"
#define MAX(a,b) ((a > b) ? a : b)
#define CTW     160
#define CTR     161
#define RTS     2
#define DTR     1
#define TX      64
#define CTS     16

#define EIN 128
#define IN      64
#define VPP     4
#define VDD     8
#define CLK     2
#define OUT     1

#define LDCONF  0
#define LDPROG  2
#define RDPROG  4
#define INCADD  6
#define BEGPRG  8
#define LDDATA  3
#define RDDATA  5
#define ENDPROG 14

#define VPPDLY  100    /* these are in milliseconds */
#define PRGDLY  12

int TSET= 500;      /* the units for these are CPU dependent */
int THLD= 500;     /* I doubt if they are necessary even    */
int TDLY= 500;     /* on the fastest PC, but ...            */

#define PROGRAM 0
#define DATA    1
#define PSIZE   1024
#define DSIZE   64

#define CP      16
#define PWRTE   8
#define WDTE    4
#define RC      3
#define HS      2
#define XT      1
#define LP      0

#define INHX16  16
#define INHX8M  8
#define NWORDS  8
#define SWAB(w) (((w)>>8) + (((w)&0xFF)<<8))

#define cinbit          (inportb(cport+6)&CTS)
#define inbit           (inportb(ipoort)&EIN)
#define Dbit             (inportb(ipoort)&IN)
#define D6bit            (~inportb(ipoort)&IN)
#define ppon         (d_bits |= VPP)
#define ppoff          (d_bits &= ~VPP)
#define vddoff           (d_bits |= VDD)
#define vddon          (d_bits &= ~VDD)
#define Dppon           (d_bits |= VDD)
#define Dppoff          (d_bits &= ~VDD)
#define Dvddon           (d_bits |= VPP)
#define Dvddoff          (d_bits &= ~VPP)
#define clkhi           (d_bits |= CLK)
#define clklo           (d_bits &= ~CLK)
#define outhi           (d_bits |= OUT)
#define outlo           (d_bits &= ~OUT)

#define clkchi           (cbit|= RTS)
#define clkclo           (cbit &= ~RTS)
#define outchi           (cbit |= DTR)
#define outclo           (cbit&= ~DTR)


#define D6ppoff           (d_bits |= VDD)
#define D6ppon          (d_bits &= ~VDD)
#define D6vddoff           (d_bits |= VPP)
#define D6vddon          (d_bits &= ~VPP)

#define comsert         (outportb(cport+4,cbit))
#define assert          (outportb(poort,d_bits))


unsigned long image_highwater=0;
unsigned char inp_line[80];
unsigned bytes[80];
static int check;
int picbuf[20000];
int fuses = XT;
int cbit=0;
int d_bits = 0;
int d_reg;
int s_reg;

int iport = 0;
int osc=1;int cp=0;int pt=0;int wd=0;
int w7=0;
int F84=0;

char bufile[20];
unsigned char stringbuf[100];
unsigned char *stringcha="................";
int bit=0;int pin7=0;
int outbit=0;
void tiny_delay(int);
int picl=0;
int PIC=1;int David=0;int Ludi=0;int C50=0;int C60=0;
int cport=0x3f8;
int hond,vijf,hvijf,tien,zes;
int binl=1;int hexl=1;
//char *hulp[];
int poort=0x378;int ipoort=0x379;int spoort=0x37a;
unsigned long  st=0;
   char statbuf[13]="empty";char statdev[8]="16C84";
char statmet[15]="Henk Schaer";
char statport[5]="LPT1";


  TCU_WINDOW  window1,window2,window3,window4,
  window5,window6,window7,window8,window9;
  int length;

  char  *Willem="*Willem's PicProgrammer*";
  char *Progver="*26-12-99  1.3 freeware!*";

  char  input_buffer[20];char filename[20];
  FILE *fp;
  int pe=0,ep=1,burn=30,eep=0;
  unsigned long grot;
unsigned  char huge rombuf[35000];

  unsigned long aantal=19999;
  void getoff();
  void stop();
  void start();
  char *headings[] = { "FFile", "DDevice", "PPICsettings","AAction","OOptions", NULL };

   TCU_MENU file_m,device_m,settings_m,action_m,option_m,*mlist[6],test,bin,
   M24,PICS;
   int  r1,r2;
   char                msg[26];
   TCU_PULLDOWN        pd;


   char               *file_options[] =
	     { "BLoadBin","HLoadHex","SSavebufBin",
	     "XSavebufHeX","VViewbuffer","AAbout","EExit",NULL };
    char               *filen[] =
      {"             ","             ","             ","             ",
       "             ","             ","             ","             ","             ","             ",
       "             ","             ","             ","             ",
       "             ","             ","             ","             ","             ","             ",NULL };
    char               *filbin[] =
      {"             ","             ","             ","             ",
       "             ","             ","             ","             ","             ","             ",
       "             ","             ","             ","             ",
       "             ","             ","             ","             ","             ","             ",NULL };

 //char *filbin[20];
   char               *device_options[] =
	{"24C02","24C04","24C08","24C16","24C32","24C65","24C128","24C256","16C84","16F84","12C508"
	,NULL};

   char               *settings_options[] =
	     {"CCodeProtect","PPowerOnTimer","WWatchDogTimer","OOscType",NULL};
   char               *action_options[] =
{ "RRead", "PProgram", "MCoMpare","IErasePIC","BBustPic","BClearBuffer",NULL };

   char               *option_options[] =
	     { "HHenk Schaer","7David Tait 07","6David Tait 06",
	     "1Lpt1", "2lpt2","LLudiPipo"," COM1"," COM2"," COM3"," COM4","TTesthardware",NULL };
message(){
tcu_open_window(&window8, 20,12, 36,8,
		    "message",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (YELLOW, BLUE),
		    TCU_BOX_SINGLE);
tcu_warnbeep();
tcu_wprintf(&window8,"\n");
tcu_wprintf(&window8,"       Not Implemented Yet\n");
tcu_wprintf(&window8," See My HomePage at:\n");
tcu_wprintf(&window8," http://www.bos.nl/homes/waklos/\n");
tcu_wprintf(&window8," for the latest update\n");
tcu_wprintf(&window8," E-Mail: waklos@bos.nl\n");
getch();tcu_close_window(&window8);
}

about(){
tcu_open_window(&window8, 20,10, 36,9,
		    "about",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (YELLOW, BLUE),
		    TCU_BOX_SINGLE);

tcu_wprintf(&window8,"\n");
tcu_wprintf(&window8," version 1.3 26/12/99 Freeware\n");
//tcu_wprintf(&window8,"   Windows98 Version\n");
tcu_wprintf(&window8,"    for the latest update:\n");
tcu_wprintf(&window8,"    http:\\www.willem.org\n");
tcu_wprintf(&window8,"    E-Mail:willem@willem.org\n\n");
tcu_wprintf(&window8," a Program by Willem Kloosterhuis\n");
getch();tcu_close_window(&window8);
}

viewbuffer()
{unsigned char *hbuf;int v;
int p=0,m,y=0;
unsigned char C;
//tcu_remove_pulldown(&pd);
 tcu_open_window (&window5,
		    1, 1,
		    80, 25,
		    statbuf,
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		     tcu_colour_attrib (CYAN, BLUE),
		     TCU_BOX_DOUBLE);

if(PIC){hbuf=picbuf;grot=20000;}else hbuf=rombuf;

do{
for(m=p;m<336+p;m=m+16){

for(v=0;v<16;v++){
y=v+m;
C=*(hbuf+y);
sprintf((stringbuf+(v*3))," %0.2X",*(hbuf+y));if(C<40||C>126)C=46;

sprintf((stringcha+v),"%c",C);}
tcu_wprintf(&window5,"%0.8X:",m);tcu_wprintf(&window5,stringbuf);
tcu_wprintf(&window5," |");tcu_wprintf(&window5,stringcha);tcu_wprintf(&window5,"\n"); }
if(getch()==27)break;p=p+500;}while(p<grot);
tcu_close_window(&window5);

}



void versie(){
int i,t,k;
for(i=0;i<12;i++){
tcu_wprintf(&window1," ");for(t=0;t<3;t++)tcu_wprintf(&window1,Willem);tcu_wprintf(&window1,"\n");
tcu_wprintf(&window1," ");for(k=0;k<3;k++)tcu_wprintf(&window1,Progver);tcu_wprintf(&window1,"\n");
}
}

void setup()
{if(Ludi)
{outportb(cport+3,0);outclo;clkclo;comsert;
delay(15);outportb(cport+3,TX);
outchi;clkchi;comsert;
outportb(cport+3,0);
outclo;clkclo;comsert;
delay(5);outportb(cport+3,TX);
outchi;clkchi;comsert;
return;}
if(David){if(David==6){D6ppoff,D6vddoff,clklo,outlo,assert;
delay(5);D6vddon;assert;}else{Dppoff,Dvddoff,clkhi,outhi,assert;
delay(5);Dvddon;assert;}}
else{ppoff,vddoff,clkhi,outhi,assert;
delay(5);vddon;assert;delay(200);}

}

t_delay(int te){
int i;
for(i=0;i<te;i++){;}
}



void tiny_delay(ticks)
int ticks;
{ setup_timer(0xb0);
ms_delay(1);
if(!C50)reset_timer();

}

void idle_mode()
{   if(Ludi){outportb(cport+3,0);}
  // clkclo;outclo;comsert;return;}
   if(David){if(David==6){D6ppoff, clkhi, outhi, assert;
   delay(VPPDLY);
   D6vddoff, assert;}else{Dppoff, clklo, outlo, assert;
   delay(VPPDLY);
   Dvddoff, assert;}}else{ppoff, clklo, outlo, assert;
   delay(VPPDLY);
   vddoff, assert;}

}

void p5_mode()
{   if(Ludi){outportb(cport+3,0);}
  // clkclo;outclo;comsert;return;}
   if(David){if(David==6){D6ppoff, clkhi, outhi, assert;
   delay(5);
   D6vddoff, assert;}else{Dppoff, clklo, outlo, assert;
   delay(4);
   Dvddoff, assert;}}else{ppoff, clklo, outlo, assert;
   delay(4);
   vddoff, assert;}

}

void prog_mode()
{
   if(David){if(David==6){D6ppoff, D6vddon, clkhi, outhi, assert;
   delay(VPPDLY);
   D6ppon, assert;}else{Dppoff, Dvddon, clklo, outlo, assert;
   delay(VPPDLY);
   Dppon, assert;}}else{ppoff, vddon, clklo, outlo, assert;
   delay(5);
      ppon, assert;}
   if(Ludi){outclo;clkclo;comsert;delay(5);outportb(cport+3,TX);}
}


void pclock_out(bit)
int bit;
{   if(Ludi){ bit? outchi: outclo, clkchi, comsert;
   tiny_delay(TSET);
   clkclo, comsert;
   tiny_delay(THLD);
   outclo, comsert;
   tiny_delay(TSET);return;}
   if(David==6){
   bit? outlo: outhi, clklo, assert;
   tiny_delay(TSET);
   clkhi, assert;
   tiny_delay(THLD);
   outhi, assert;
   tiny_delay(10);return;}

   bit? outhi: outlo, clkhi, assert;
   tiny_delay(TSET);
   clklo, assert;
   tiny_delay(THLD);
   outlo, assert;
   tiny_delay(TSET);

}

int pclock_in()
{
   int b;
    if(Ludi){
   outchi, clkchi, comsert;
   tiny_delay(TSET);
   clkclo, comsert;
   tiny_delay(TSET);
   b=cinbit? 1: 0;
   tiny_delay(THLD);return b;}

   if(David==6){
    outlo, clklo, assert;
   tiny_delay(TSET);
   clkhi, assert;
   tiny_delay(TSET);
   b=D6bit? 1: 0;
   tiny_delay(THLD);return b;}

   outhi, clkhi, assert;
   tiny_delay(TSET);
   clklo, assert;
   tiny_delay(TSET);
   if(David){b=Dbit? 1: 0;}else {b = inbit? 0: 1;}
   tiny_delay(THLD);


   return b;
}

void pout_word(w)
int w;
{
   int b;

   pclock_out(0);
   for ( b=0; b<14; ++b )
     pclock_out(w&(1<<b));
   pclock_out(0);
}

int pin_word()
{
   int b, w;

   (void) pclock_in();
   for ( w=0, b=0; b<14; ++b )
     w += pclock_in()<<b;
   (void) pclock_in();
   return w;
}



void command(cmd)
int cmd;
{
   int b;
    if(Ludi){
    outclo, comsert;
   tiny_delay(TDLY);
   for ( b=0; b<6; ++b )
      pclock_out(cmd&(1<<b));
   outchi, comsert;
   tiny_delay(TDLY);return;}
   if(David==6){
   outhi, assert;
   tiny_delay(TDLY);
   for ( b=0; b<6; ++b )
      pclock_out(cmd&(1<<b));
   outlo, assert;
   tiny_delay(TDLY);return;}

   outlo, assert;
   tiny_delay(TDLY);
   for ( b=0; b<6; ++b )
      pclock_out(cmd&(1<<b));
   outhi, assert;
   tiny_delay(TDLY);



}


void P_erase()
{
   int i;

   prog_mode();
   command(LDCONF);
   pout_word(0x3FFF);
   for ( i=0; i<7; ++i )
      command(INCADD);
   command(1);
   command(7);
   command(BEGPRG);
   delay(PRGDLY);
   command(1);
   command(7);
   idle_mode();

}
void verify(which)
int which;
{int i, n, w, mask, ldcmd, rdcmd, *buf;
   long e=0; long t=PSIZE/52;
   tcu_open_window(&window4, 14,10, 55,3,
		    "verifying PIC program area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (GREEN, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);


      mask = 0x3FFF;
      ldcmd = LDPROG;
      rdcmd = RDPROG;
       prog_mode();
   for ( i=0; i<PSIZE; ++i ) {

      command(rdcmd);
      w = pin_word() & mask;
      if ( w != picbuf[i]) {
      tcu_warnbeep();tcu_wprintf(&window4,"error");getch();break;}

      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;
   }
   tcu_close_window(&window4);

      mask = 0xFF;
      ldcmd = LDDATA;
      rdcmd = RDDATA;
       prog_mode();
       tcu_open_window(&window4, 14,10, 55,3,
		    "verifying PIC data area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (GREEN, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
      t=DSIZE/52;e=0;
   for ( i=0; i<DSIZE; ++i ) {
      command(rdcmd);
      w = pin_word() & mask;
      if ( w != picbuf[i+0x2100] ) {
       tcu_warnbeep();tcu_wprintf(&window4,"error");break;}


      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;
   }
  tcu_close_window(&window4);
  idle_mode();

}
/*void Fconf()
{
   int i;
   fuses=19;
   prog_mode();
   command(LDCONF);
   pout_word(fuses);
   for ( i=0; i<7; ++i )
     command(INCADD);
    command(1);
   command(7);
   //pout_word(fuses);
   command(BEGPRG);
   delay(PRGDLY);
     command(1);
   command(7);
   command(RDPROG);
  if ( fuses != (pin_word()&0x1F) ){
  tcu_warnbeep();tcu_wprintf(&window4,"error");getch();}

}   */

void R_fuses(){

cp=(fuses&CP)? CP: 0;
wd=(fuses&WDTE)? WDTE: 0;
pt=(fuses&PWRTE)? PWRTE: 0;
if(F84)pt=(fuses&PWRTE)? 0: PWRTE;
osc=(fuses&3);
if(!w7){tcu_open_window(&window7, 13,15, 21,7,
		    "PIC conf status",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLUE, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, RED),
		    TCU_BOX_SINGLE);
		    w7=1;}
 tcu_clear_window(&window7);
 tcu_wprintf(&window7,"  CodeProtect: ");if(!cp)tcu_wprintf(&window7,"YES\n");
 else tcu_wprintf(&window7,"NO\n");
  tcu_wprintf(&window7," PowerUpTimer: ");if(pt)tcu_wprintf(&window7,"YES\n");
 else tcu_wprintf(&window7,"NO\n");
 tcu_wprintf(&window7,"WatchDogTimer: ");if(wd)tcu_wprintf(&window7,"YES\n");
 else tcu_wprintf(&window7,"NO\n");
 tcu_wprintf(&window7,"OscillatorTyp: ");
 if(osc==0)tcu_wprintf(&window7,"LP\n");
 else if(osc==1)tcu_wprintf(&window7,"XT\n");
 else if(osc==2)tcu_wprintf(&window7,"HS\n");
 else if(osc==3)tcu_wprintf(&window7,"RS\n");

}
void R_50fuses(){

cp=(fuses&CP)? CP: 0;
wd=(fuses&WDTE)? WDTE: 0;
pt=(fuses&PWRTE)? PWRTE: 0;
if(F84)pt=(fuses&PWRTE)? 0: PWRTE;
osc=(fuses&3);
if(!w7){tcu_open_window(&window7, 14,16, 21,7,
		    "PIC conf status",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLUE, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, RED),
		    TCU_BOX_SINGLE);
		    w7=1;}
 tcu_clear_window(&window7);
 tcu_wprintf(&window7,"  CodeProtect: ");if(!pt)tcu_wprintf(&window7,"YES\n");
 else tcu_wprintf(&window7,"NO\n");
  tcu_wprintf(&window7," Master Clear: ");if(cp)tcu_wprintf(&window7,"YES\n");
 else tcu_wprintf(&window7,"NO\n");

 tcu_wprintf(&window7,"WatchDogTimer: ");if(wd)tcu_wprintf(&window7,"YES\n");
 else tcu_wprintf(&window7,"NO\n");
 tcu_wprintf(&window7,"OscillatorTyp: ");
 if(osc==0)tcu_wprintf(&window7,"LP\n");
 else if(osc==1)tcu_wprintf(&window7,"XT\n");
 else if(osc==2)tcu_wprintf(&window7,"iRC\n");
 else if(osc==3)tcu_wprintf(&window7,"eRC\n");

}

void config()
{
   int i;
   fuses=*(picbuf+0x2007);
   fuses&=0x1F;
   prog_mode();
   command(LDCONF);
   pout_word(fuses);
   for ( i=0; i<7; ++i )
     command(INCADD);
   command(LDPROG);
   pout_word(fuses);
   command(BEGPRG);
   setup_timer(0xb0);
       ms_delay(20000);
       reset_timer();
   //delay(PRGDLY);
   command(RDPROG);
  if ( fuses != (pin_word()&0x1F) ){
  tcu_warnbeep();tcu_wprintf(&window4,"error");getch();}

}

void bust()
{
   int i,b;
    tcu_open_window(&window4, 14,10, 55,3,
		    "BustMode Press Bustbutton",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
   fuses=*(picbuf+0x2007);
 cp=CP;
   fuses=cp+pt+wd+osc;
   R_fuses();
   delay(500);
   for(b=0;b<4;b++  ){
  tcu_warnbeep();tcu_wprintf(&window4,"   Busting!");
  prog_mode();
   command(LDCONF);
   pout_word(fuses);
   for ( i=0; i<7; ++i )
     command(INCADD);
   command(LDPROG);
   pout_word(fuses);
   command(BEGPRG);
   delay(PRGDLY);
   command(RDPROG);
  delay(500);idle_mode();}
    tcu_close_window(&window4);idle_mode();


}

int verify50(which)
int which;
{int i, n, w, mask, ldcmd, rdcmd, *buf;
   long e=0; long t=512/52;
   tcu_open_window(&window4, 14,10, 55,3,
		    "verifying PIC program area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (GREEN, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

     setup_timer(0xb0);
      mask = 0x3FFF;
      ldcmd = LDPROG;
      rdcmd = RDPROG;
       prog_mode();
	 command(INCADD);
   for ( i=0; i<510; ++i ) {

      command(rdcmd);
      ms_delay(1200);
      w = pin_word() & mask;
      if ( w != picbuf[i]) {
      tcu_wprintf(&window4,"error");idle_mode();
      reset_timer();tcu_warnbeep();getch();
      tcu_close_window(&window4);return 0;}

      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;
   }
   tcu_close_window(&window4);

  idle_mode();
  reset_timer();
  return 1;
}
int verify60(which)
int which;
{int i, n, w, mask, ldcmd, rdcmd, *buf;
   long e=0; long t=2048/52;
   tcu_open_window(&window4, 14,10, 55,3,
		    "verifying PIC program area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (GREEN, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

     setup_timer(0xb0);
      mask = 0x3FFF;
      ldcmd = LDPROG;
      rdcmd = RDPROG;
       prog_mode();

   for ( i=0; i<2048; ++i ) {

      command(rdcmd);
      ms_delay(1200);
      w = pin_word() & mask;
      if ( w != picbuf[i]) {
      tcu_wprintf(&window4,"error");idle_mode();
      reset_timer();tcu_warnbeep();getch();
      tcu_close_window(&window4);return 0;}

      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;
   }
   tcu_close_window(&window4);

  idle_mode();
  reset_timer();
  return 1;
}

void pr622(int i)
{
command(LDPROG);
ms_delay(1200);
      pout_word(picbuf[i]);
      ms_delay(4);
      command(BEGPRG);
     ms_delay(119);
     command(ENDPROG);
}
void PIC508_pr()

{
   int i, n,x, w,te, mask, ldcmd, rdcmd, *buf;
   long e=0;
   long t=512/52;
   int q=0;

   setup_timer(0xb0);
   tcu_open_window(&window4, 14,10, 55,3,
		    "programming PIC program area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

      mask = 0x3FFF;
      ldcmd = LDPROG;
      rdcmd = RDPROG;
   //	write;assert;
       prog_mode();
      ms_delay(14);
     command(INCADD);

      for ( i=0; i<=510; ++i ) {
      W:
      pr622(i);
      ms_delay(4);
      command(rdcmd);
       ms_delay(1200);
      w = pin_word() & mask;
      if ( w != picbuf[i]) {
      q++;if(q>25)
      { reset_timer();idle_mode();
      tcu_warnbeep();tcu_wprintf(&window4,"error");
      getch();tcu_close_window(&window4);return;}
      else goto W;}
      for(x=0;x<(q*3);x++)pr622(i);
      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;q=0;
   }
   tcu_close_window(&window4);

if(verify50())
{
     tcu_open_window(&window4, 14,10, 55,3,
		    "programming PIC config area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);


   tcu_wprintf(&window4,"                    blowing fuses");
   fuses=picbuf[0x2007]&0x1F;

   p5_mode();
    delay(5);
   prog_mode();


    delay(4);

   setup_timer(0xb0);
   for ( i=0; i<100; ++i )pr622(0x2007);
   reset_timer();
    p5_mode();
    delay(5);
    prog_mode();
   command(RDPROG);
   delay(2);
   te= pin_word()&0x1F;
  if ( fuses != te) {
  reset_timer(); idle_mode();
  tcu_warnbeep();tcu_wprintf(&window4,"error");getch();
  tcu_close_window(&window4);return;}
   reset_timer();
  delay(500);
 }
    tcu_close_window(&window4);idle_mode();


}

void PIC622_pr()

{
   int i, n,x, w,te, mask, ldcmd, rdcmd, *buf;
   long e=0;
   long t=2048/52;
   int q=0;

   setup_timer(0xb0);
   tcu_open_window(&window4, 14,10, 55,3,
		    "programming PIC program area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

      mask = 0x3FFF;
      ldcmd = LDPROG;
      rdcmd = RDPROG;
   //	write;assert;
       prog_mode();
      ms_delay(14);
      for ( i=0; i<2048; ++i ) {
      W:
      pr622(i);
      ms_delay(4);
      command(rdcmd);
       ms_delay(1200);
      w = pin_word() & mask;
      if ( w != picbuf[i]) {
      q++;if(q>25)
      { reset_timer();idle_mode();
      tcu_warnbeep();tcu_wprintf(&window4,"error");
      getch();tcu_close_window(&window4);return;}
      else goto W;}
      for(x=0;x<(q*3);x++)pr622(i);
      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;q=0;
   }
   tcu_close_window(&window4);

if(verify60())
{
     tcu_open_window(&window4, 14,10, 55,3,
		    "programming PIC config area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);


   tcu_wprintf(&window4,"                    blowing fuses");
   fuses=picbuf[0x2007]&0x1F;
    delay(5);
   prog_mode();
   command(LDCONF);
   pout_word(fuses);
   for ( i=0; i<7; ++i )
    command(INCADD);
    delay(4);
    setup_timer(0xb0);
   for ( i=0; i<100; ++i )pr622(0x2007);
   reset_timer();
    p5_mode();
   command(RDPROG);
   delay(2);
   te= pin_word()&0x1F;
  if ( fuses != te) {
  reset_timer(); idle_mode();
  tcu_warnbeep();tcu_wprintf(&window4,"error");getch();
  tcu_close_window(&window4);return;}
   reset_timer();
  delay(500);
 }
    tcu_close_window(&window4);idle_mode();


}

void P50read()

{int i, n, w, mask, ldcmd, rdcmd, *buf;
   long e=0; long t=512/52;
   tcu_open_window(&window4, 14,10, 55,3,
		    "reading PIC program area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
     // write;assert;
    //  setup_timer(0xb0);
      mask = 0x3FFF;
      ldcmd = LDPROG;
      rdcmd = RDPROG;
       prog_mode();
       command(INCADD);
   for ( i=0; i<512; ++i ) {
      command(rdcmd);
      delay(1);
   picbuf[i] = pin_word() & mask;
      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;
   }
   tcu_close_window(&window4);
    p5_mode();

    prog_mode();
    delay(5);
    command(RDPROG);
   picbuf[0x2007]=pin_word()&0x1F;
   fuses=picbuf[0x2007];
   R_50fuses();
  tcu_close_window(&window4);
  p5_mode();
  strcpy(statbuf,"PicDump");winstat();
  //reset_timer();
}


void PIC_pr(which)
int which;
{
   int i, n, w, mask, ldcmd, rdcmd, *buf;
   long e=0; long t=PSIZE/52;
   P_erase();
   //if(F84)Fconf();
   tcu_open_window(&window4, 14,10, 55,3,
		    "programming PIC program area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
     if(!picl){tcu_warnbeep();tcu_wprintf(&window4,"                  reload HEX file");
     getch();tcu_close_window(&window4);return;}

      mask = 0x3FFF;
      ldcmd = LDPROG;
      rdcmd = RDPROG;
       prog_mode();
   for ( i=0; i<PSIZE; ++i ) {
      command(ldcmd);
      pout_word(picbuf[i]);
      command(BEGPRG);
       setup_timer(0xb0);
       ms_delay(12000);
       reset_timer();
     // delay(12);
      command(rdcmd);
      w = pin_word() & mask;
      if ( w != picbuf[i]) {
      tcu_warnbeep();tcu_wprintf(&window4,"error");idle_mode();
      getch();tcu_close_window(&window4);return;}

      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;
   }
   tcu_close_window(&window4);

      mask = 0xFF;
      ldcmd = LDDATA;
      rdcmd = RDDATA;
       prog_mode();
       tcu_open_window(&window4, 14,10, 55,3,
		    "programming PIC data area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
      t=DSIZE/52;e=0;
   for ( i=0; i<DSIZE; ++i ) {
      command(ldcmd);
      pout_word(picbuf[i+0x2100]);
      command(BEGPRG);
      setup_timer(0xb0);
       ms_delay(20000);
       reset_timer();
      //delay(PRGDLY);
      command(rdcmd);
      w = pin_word() & mask;
      if ( w != picbuf[i+0x2100] ) {
       tcu_warnbeep();tcu_wprintf(&window4,"error");break;}
      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;
   }tcu_close_window(&window4);
   if(Ludi){idle_mode();prog_mode();}
    verify();

     tcu_open_window(&window4, 14,10, 55,3,
		    "programming PIC config area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

   tcu_wprintf(&window4,"                    blowing fuses");
   config();
   delay(500);
    tcu_close_window(&window4);idle_mode();


}

void Pread()

{int i, n, w, mask, ldcmd, rdcmd, *buf;
   long e=0; long t=PSIZE/52;
   tcu_open_window(&window4, 14,10, 55,3,
		    "reading PIC program area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

      mask = 0x3FFF;
      ldcmd = LDPROG;
      rdcmd = RDPROG;
       prog_mode();
       delay(5);
   for ( i=0; i<PSIZE; ++i ) {
      command(rdcmd);
   picbuf[i] = pin_word() & mask;
      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;
   }
   tcu_close_window(&window4);
      mask = 0xFF;
      ldcmd = LDDATA;
      rdcmd = RDDATA;
       prog_mode();
       tcu_open_window(&window4, 14,10, 55,3,
		    "reading PIC data area",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
      t=DSIZE/52;e=0;
       for ( i=0; i<DSIZE; ++i ) {
      command(rdcmd);
 picbuf[i+0x2100]= pin_word() & mask;

      command(INCADD);
      if(e==t){tcu_wprintf(&window4,"þ");e=0;}
      e++;
   } command(LDCONF);
   pout_word(fuses);
   for ( i=0; i<7; ++i )
     command(INCADD);
    command(RDPROG);
   picbuf[0x2007]=pin_word()&0x1F;
   fuses=picbuf[0x2007];
   R_fuses();
  tcu_close_window(&window4);
  idle_mode();
  strcpy(statbuf,"PicDump");winstat();
}



void eclock_out(bit)
int bit;
{
if(Ludi){
     bit? outchi: outclo;comsert;
   tiny_delay(TSET);
   clkchi;comsert;
   tiny_delay(TSET);
   clkclo; comsert;
   tiny_delay(TSET);
   outclo;comsert;return; }

   if(David==6){
    bit? outlo:outhi;assert;
   tiny_delay(TSET);
   clklo;assert;
   tiny_delay(TSET);
   clkhi; assert;
   tiny_delay(TSET);
   outhi;assert;return;}

   bit? outhi:outlo;assert;
   tiny_delay(TSET);
   clkhi;assert;
   tiny_delay(TSET);
   clklo; assert;
   tiny_delay(TSET);
   outlo;assert;

}


int eclock_in()
{
   int b;
   if(Ludi){
   outchi;comsert;
   tiny_delay(TSET);
   clkclo; comsert;
   tiny_delay(TSET);
   clkchi;comsert;
     tiny_delay(TSET);
   b=cinbit? 1: 0;
   tiny_delay(TSET);
   clkclo;comsert;return b;}

   if(David==6){
   outlo;clkhi; assert;
   tiny_delay(TSET);
   clklo;assert;
     tiny_delay(TSET);
   b=D6bit? 1: 0;
   tiny_delay(TSET);
   clkhi;assert;return b;}

   outhi;clklo; assert;
   tiny_delay(TSET);
   clkhi;assert;
     tiny_delay(TSET);
   if(David){b=Dbit? 1: 0;}else{b = inbit? 0: 1;}
   tiny_delay(TSET);
   clklo;assert;

   return b;
}
void out_word(w)
unsigned char w;
{
   int b;


   for ( b=7; b>=0;--b )
     eclock_out(w&(1<<b));

}

int ein_word()
{
   int b, w;


   for ( w=0, b=7; b>=0; --b )
     w += eclock_in()<<b;

   return w;
}
void start()
{
if(Ludi){
outclo;comsert;tiny_delay(TSET);
clkclo;comsert;tiny_delay(TSET);return;}
if(David==6){outhi;assert;tiny_delay(TSET);
clkhi;assert;tiny_delay(TSET);return;}

outlo;assert;tiny_delay(TSET);
clklo;assert;tiny_delay(TSET);

}

void stop()
{
if(Ludi){
clkchi;comsert;tiny_delay(TSET);
outchi;comsert;tiny_delay(TSET);return;}
if(David==6)
{clklo;assert;tiny_delay(TSET);
outlo;assert;tiny_delay(TSET);return;}

clkhi;assert;tiny_delay(TSET);
outhi;assert;tiny_delay(TSET);

}

int strct()

{start(),out_word(CTW);
if(!eclock_in())return 1;else return 0; }

int strctR()
{start(),out_word(CTR);
if(!eclock_in())return 1;else return 0; }


int BYTE(out)
unsigned char out;
{out_word(out);
if(!eclock_in())return 1;else return 0; }

void error()
{
tcu_wprintf(&window4,"  error");idle_mode();
tcu_warnbeep();getch();
}


void eprogram65()

{
unsigned char adrby1;unsigned char adrby2;
unsigned int e=0;unsigned int t=aantal/52;unsigned int i;

int c=0;int w=0;
unsigned char g,k;

tcu_open_window(&window4, 14,10, 55,3,
		    "programming chip",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
if(PIC){if(picl){tcu_warnbeep();tcu_wprintf(&window4,"                   reload HEX file");
     getch();tcu_close_window(&window4);return;} }


setup();
delay(50);
for(i=0;i<grot;i++){

g=*(rombuf+i);

adrby1=i>>8;
adrby2=i;

if(strct())
{
if(BYTE(adrby1))
{if(BYTE(adrby2))
{
if(BYTE(g)){if(Ludi){outclo; comsert;}
else if(David==6)outhi;else outlo;assert;stop();}
else {error();break;}}}else {error();break;}}else {error();break;}
W:
if(strct()){
if(BYTE(adrby1)){if(BYTE(adrby2))
{start();
stop();
if(strctR())
{k=ein_word();eclock_out(1);stop();}
else {error();break;}}else {error();break;}}else {error();break;}}
else {delay(1);w++;if(w==10){error();break;}stop();goto W;}
if(k!=g){error();break;}w=0;
if(e==t)
{tcu_wprintf(&window4,"þ");e=0;}
e++;

}
tcu_close_window(&window4);
idle_mode();

 }
void prog_16(){
unsigned char adrby1;unsigned char adrby2;
unsigned int e=0;unsigned int t=aantal/52;unsigned int i;

int c=0;int w=0;
int  g,k;


tcu_open_window(&window4, 14,10, 55,3,
		    "programming chip",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
if(PIC){ if(picl){tcu_warnbeep();tcu_wprintf(&window4,"                  reload HEX file");
     getch();tcu_close_window(&window4);return;}  }


setup();
delay(150);
for(i=0;i<grot;i++){
g=*(rombuf+i);

adrby1=0;
adrby1=i>>7;
adrby1&=126;
adrby1=adrby1+CTW;
adrby2=i;

start();

if(BYTE(adrby1))
{if(BYTE(adrby2))
{
if(BYTE(g)){if(Ludi){outclo;comsert;}
else if(David==6)outhi;else outlo;assert;stop();delay(1);}
else {error();break;}}}else {error();break;}
W:
start();
if(BYTE(adrby1))
{if(BYTE(adrby2))
{start();
stop();
adrby1=adrby1+1;
start();
if(BYTE(adrby1))
{k=ein_word();eclock_out(1);stop();}
else {error();break;}}else {error();break;}}
else {delay(1);w++;if(w==10){error();break;}stop();goto W;}
if(k!=g){error();break;}
w=1;

if(e==t){tcu_wprintf(&window4,"þ");e=0;}

e++; }
tcu_close_window(&window4);
idle_mode();
 }
void read_16(){
unsigned char adrby1;unsigned char adrby2;
unsigned int e=0;unsigned int t=aantal/52;unsigned int i;

int c=0;int w=0;
unsigned char g;


tcu_open_window(&window4, 14,10, 55,3,
		    "reading chip",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);


setup();
delay(150);
for(i=0;i<=aantal;i++){
adrby1=0;
adrby1=i>>7;
adrby1&=126;
adrby1=adrby1+CTW;
adrby2=i;

start();
if(BYTE(adrby1)){if(BYTE(adrby2))
{start();
stop();
adrby1=adrby1+1;
start();
if(BYTE(adrby1))
{g=ein_word();eclock_out(1);stop();}
else {error();break;}}else {error();break;}}else {error();break;}
rombuf[i]=g;
if(e==t){tcu_wprintf(&window4,"þ");e=0;}
e++;if(kbhit()){if(getch()==27)break;}
     }
strcpy(statbuf,"Chip dump");winstat();


tcu_close_window(&window4);
idle_mode();
grot=aantal;
}
void verify_16(){
unsigned char adrby1;unsigned char adrby2;
unsigned int e=0;unsigned int t=aantal/52;unsigned int i;

int c=0;int w=0;
unsigned char g;


tcu_open_window(&window4, 14,10, 55,3,
		    "comparing chip",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (GREEN, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);


setup();
delay(150);
for(i=0;i<grot;i++){
adrby1=0;
adrby1=i>>7;
adrby1&=126;
adrby1=adrby1+CTW;
adrby2=i;

start();
if(BYTE(adrby1)){if(BYTE(adrby2))
{start();
stop();
adrby1=adrby1+1;
start();
if(BYTE(adrby1))
{g=ein_word();eclock_out(1);stop();}
else {error();break;}}else {error();break;}}else {error();break;}
if(g!=rombuf[i]){error();break;}
if(e==t){tcu_wprintf(&window4,"þ");e=0;}
e++;if(kbhit()){if(getch()==27)break;}
     }



tcu_close_window(&window4);
idle_mode();
grot=aantal;
}

void uit(){
return;}
void read_65(){
unsigned char adrby1;unsigned char adrby2;
unsigned int e=0;unsigned int t=aantal/52;unsigned int i;

int c=0;int w=0;
unsigned char g;


tcu_open_window(&window4, 14,10, 55,3,
		    "reading chip",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);


setup();
delay(50);
for(i=0;i<=aantal;i++){
adrby1=i>>8;
adrby2=i;

if(strct()){
if(BYTE(adrby1)){if(BYTE(adrby2))
{start();
stop();
if(strctR())
{g=ein_word();eclock_out(1);stop();}
else {{error();break;}break;}}else {{error();break;}break;}}else {{error();break;}break;}}
else {{error();break;}break;}
rombuf[i]=g;
if(e==t){tcu_wprintf(&window4,"þ");e=0;}
e++;
uit();

if(kbhit()){if(getch()==27)break;}
     }
strcpy(statbuf,"Chip dump");winstat();

tcu_close_window(&window4);
idle_mode();
grot=aantal;
}

void verify_65(){
unsigned char adrby1;unsigned char adrby2;
unsigned int e=0;unsigned int t=aantal/52;unsigned int i;

int c=0;int w=0;
unsigned char g;


tcu_open_window(&window4, 14,10, 55,3,
		    "comparing chip",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (GREEN, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);


setup();
delay(50);
for(i=0;i<grot;i++){
adrby1=i>>8;
adrby2=i;

if(strct()){
if(BYTE(adrby1)){if(BYTE(adrby2))
{start();
stop();
if(strctR())
{g=ein_word();eclock_out(1);stop();}
else {{error();break;}break;}}else {{error();break;}break;}}else {{error();break;}break;}}
else {{error();break;}break;}
if(g!=rombuf[i]){error();break;}
if(e==t){tcu_wprintf(&window4,"þ");e=0;}
e++;


if(kbhit()){if(getch()==27)break;}
     }


tcu_close_window(&window4);
idle_mode();
grot=aantal;
}


test_hw(){
int l,r;
tcu_open_window(&window2, 12,13, 37,6,
		    NULL,
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

setup();
delay(50);
if(David==6){outlo;assert;delay(1);}
else if(Ludi){outchi;comsert;delay(1);}
else {outhi;assert;delay(1);}
r=inbit;if(Ludi)r=cinbit;if(David){if(David==6)r=D6bit;else r=Dbit;}
if(David==6){outhi;assert;delay(1);}
else if(Ludi){outclo;comsert;delay(1);}
else {outlo;assert;delay(1);}
l=inbit;if(Ludi)l=cinbit;if(David){if(David==6)l=D6bit;else l=Dbit;}
if(Ludi||David){if(r>0&&l==0){tcu_wprintf(&window2,"\n     Hardware present ");
getch();}else{tcu_wprintf(&window2,"\n hardware error :  Check Power and\n connections");tcu_warnbeep();getch();}
tcu_close_window(&window2);}else{
if(r==0&&l==128){tcu_wprintf(&window2,"\n     Hardware present ");
getch();}else{tcu_wprintf(&window2,"\n hardware error :  Check Power and\n connections");tcu_warnbeep();getch();}
tcu_close_window(&window2);}
idle_mode();

}


unsigned Process_Line (unsigned char *inp_line) {

int      file_chksum, calc_chksum;

unsigned i, status;
unsigned count, type;
long  address, addhi, addlo;
unsigned char  *inp_ptr, *out_ptr;

if (strstr(inp_line, ":") != NULL) {
    sscanf(inp_line, ":%2X%2lX%2lX%2X", &count, &addhi, &addlo, &type);
    calc_chksum = count + addhi + addlo + type;
    inp_ptr = inp_line + 9;
    for (i=0; i<count; i++) {
	sscanf(inp_ptr, "%2X", &bytes[i]);
	calc_chksum += bytes[i];
	  inp_ptr +=2;
	}
    calc_chksum =-(calc_chksum & 0xFF) & 0xFF;

    sscanf(inp_ptr, "%2X", &file_chksum);

    if (calc_chksum == file_chksum) {
	address = (addhi << 8) + addlo;
	address=address;
       //out_ptr = &rombuf[address];
	for (i=0; i<count; i++)
	  *(rombuf+address+i)= bytes[i];
	image_highwater=MAX(image_highwater,address+i);
	status = 1;
	}
	else {
tcu_open_window(&window6, 17,3, 40,3,
		    "ERROR",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

tcu_clear_window(&window6);
tcu_wprintf(&window6," Checksum Error reading Hexfile");
tcu_warnbeep();getch();
tcu_close_window(&window6);status=0;}


}       else {
tcu_open_window(&window6, 17,3, 30,3,
		    "ERROR",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

tcu_clear_window(&window6);
tcu_wprintf(&window6," Error reading Hexfile");
tcu_warnbeep();getch();
tcu_close_window(&window6);status=0;}
return status;
}

unsigned Pic_Line (unsigned char *inp_line) {

int      file_chksum, calc_chksum;

unsigned i, w,status;
unsigned count, type;
long  address, addhi, addlo;
unsigned char  *inp_ptr, *out_ptr;

if (strstr(inp_line, ":") != NULL) {
    sscanf(inp_line, ":%2X%2lX%2lX%2X", &count, &addhi, &addlo, &type);
    calc_chksum = count + addhi + addlo + type;
    inp_ptr = inp_line + 9;
    for (i=0; i<count; i++) {
	sscanf(inp_ptr, "%2X", &bytes[i]);
	calc_chksum += bytes[i];
	  inp_ptr +=2;
	}
    calc_chksum =-(calc_chksum & 0xFF) & 0xFF;

    sscanf(inp_ptr, "%2X", &file_chksum);


    sscanf(inp_line, ":%2X%2lX%2lX%2X", &count, &addhi, &addlo, &type);

    count=count/2;
    inp_ptr = inp_line + 9;
    for (i=0; i<count;i++) {
	sscanf(inp_ptr, "%4X", &w);
	w=(((w)>>8)+(((w)&0xFF)<<8));

	inp_ptr +=4;
	bytes[i]=w;
	}


    if (file_chksum==calc_chksum) {
	address = (addhi << 8) + addlo;
	address=address/2;
       //out_ptr = &rombuf[address];
	for (i=0; i<count; i++)
	  *(picbuf+address+i)= bytes[i];
	image_highwater=MAX(image_highwater,address+i);
	status = 1;
	}
	else {
tcu_open_window(&window6, 17,3, 40,3,
		    "ERROR",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

tcu_clear_window(&window6);
tcu_wprintf(&window6," Checksum Error reading Hexfile");
tcu_warnbeep();getch();
tcu_close_window(&window6);status=0;}


 }      else {
tcu_open_window(&window6, 17,3, 30,3,
		    "ERROR",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (RED, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);

tcu_clear_window(&window6);
tcu_wprintf(&window6," Error reading Hexfile");
tcu_warnbeep();getch();
tcu_close_window(&window6);status=0;}
return status;
}

testmenu(){
int c=0;int i=0;int status=1;
struct ffblk fileinfo;
if(hexl){
if(findfirst("*.hex",&fileinfo,0)!=-1){
strcpy(filen[c],fileinfo.ff_name);
while(findnext(&fileinfo)==0){c++;
strcpy(filen[c],fileinfo.ff_name); }}
c++;filen[c]=NULL;


       tcu_define_menu (&test,
			 "hex files",
			 tcu_colour_attrib (YELLOW, CYAN),       /* Title */
			 tcu_colour_attrib (BLUE, CYAN),         /* Box */
			 tcu_colour_attrib (WHITE, MAGENTA),     /* Options */
			 tcu_colour_attrib (WHITE, BLUE),        /* Selected */
			 tcu_colour_attrib (LIGHTGRAY, MAGENTA), /* Deselected */
			 TCU_ESC_ESC | TCU_ESC_CNTL_C | TCU_ESC_PGUP | TCU_ESC_FUNC,
			 TCU_BOX_DOUBLE,
			 filen,
			 0);hexl=0;}
			 tcu_display_menu(&test,22,4);
			i=tcu_read_menu_selection (&test);
			tcu_remove_menu (&test);
if(i>0)fp=fopen(filen[i-1],"r");else return;if(fp==NULL)return;
memset (rombuf, 0, aantal);
while ((fgets(inp_line,80,fp) != NULL) && (status))
     if(PIC)status =Pic_Line(inp_line);else status=Process_Line (inp_line);
grot=image_highwater;image_highwater=0;

fclose(fp);
if(status){strcpy(statbuf,filen[i-1]);winstat();}else return;
if(PIC){fuses=picbuf[0x2007];
if(C50)R_50fuses();else R_fuses();picl=1;}else picl=0;
}


void load_file(){
long gr=0;static int c=0;int i=0;
int mr=65534;
grot=0;

tcu_open_window(&window2, 17,2, 25,3,
		    NULL,
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
A:
tcu_clear_window(&window2);
tcu_wprintf(&window2,"filename:");
tcu_wgets(&window2,12,filename,&length);
//strcpy(filename,input_buffer);
fp=fopen(filename,"rb");

if(fp==NULL&&length!=-1){tcu_warnbeep();tcu_clear_window(&window2);
tcu_wprintf(&window2,"  no such file!");delay(1000);goto A;}
memset (rombuf, 0, aantal);
if(length!=-1){strcpy(statbuf,filename);winstat();}else{ fclose(fp);
tcu_close_window(&window2); return;}
{grot=fread(rombuf,1,mr,fp);}

fclose(fp);
tcu_close_window(&window2);
if(grot>aantal)grot=aantal;

memset(input_buffer,0,20);
}

void load_bin(){
long gr=0;static int c=0;int i=0;int j;struct ffblk fileinfo;
int mr=65534;
grot=0;
picl=1;
if(binl){
if(findfirst("*.bin",&fileinfo,0)!=-1){
strcpy(filbin[c],fileinfo.ff_name);
while(findnext(&fileinfo)==0){c++;
strcpy(filbin[c],fileinfo.ff_name); }}
c++;filbin[c]=NULL;binl=0;
tcu_define_menu (&bin,
			 "bin files",
			 tcu_colour_attrib (YELLOW, CYAN),       /* Title */
			 tcu_colour_attrib (BLUE, CYAN),         /* Box */
			 tcu_colour_attrib (WHITE, MAGENTA),     /* Options */
			 tcu_colour_attrib (WHITE, BLUE),        /* Selected */
			 tcu_colour_attrib (LIGHTGRAY, MAGENTA), /* Deselected */
			 TCU_ESC_ESC | TCU_ESC_CNTL_C | TCU_ESC_PGUP | TCU_ESC_FUNC,
			 TCU_BOX_DOUBLE,
			 filbin,
			 0);  }
			 tcu_display_menu(&bin,22,3);
			i=tcu_read_menu_selection (&bin);
			tcu_remove_menu (&bin);
if(i>0)fp=fopen(filbin[i-1],"rb");else return;if(fp==NULL)return;
memset (rombuf, 0, aantal);
strcpy(statbuf,filbin[i-1]);winstat();
{if(PIC)fread(picbuf,2,10000,fp);else grot=fread(rombuf,1,mr,fp);}

fclose(fp);
tcu_close_window(&window2);
if(grot>aantal)grot=aantal;
if(PIC)fuses=picbuf[0x2007];
if(PIC)R_fuses();
memset(input_buffer,0,20);
}

void save_file(){
int mw=65534;
tcu_open_window(&window2, 17,5, 25,3,
		    NULL,
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
G:
strset(filename,0);
tcu_clear_window(&window2);
tcu_wprintf(&window2,"filename:");
tcu_wgets(&window2,11,filename,&length);
strcat(filename,".bin");
if(length!=-1){
 {fp=fopen(filename,"wb");if(fp==NULL){tcu_warnbeep();goto G;}
if(PIC)fwrite(picbuf,1,aantal+1,fp);else fwrite(rombuf,1,aantal+1,fp);
fclose(fp);
  }
tcu_close_window(&window2);
fclose(fp);}
strset(filename,0);
tcu_close_window(&window2);
}
static void d_hexbyte(FILE *fp, unsigned b)
{
    fprintf(fp,"%02X",b);
    check += b;
}


static void d_hexword(FILE *fp, unsigned w)
{
    d_hexbyte(fp,w>>8);
    d_hexbyte(fp,w&0xFF);
}


/* d_hexrec(fp, buf, address, nw)
 *
 * Dumps nw words from buf in INHX8M or INHX16 format.
 *
 */

static void d_hexrec(FILE *fp, int *buf, int address, int nw)
{
    int i, n;
    unsigned w;

    while ( nw > 0 ) {
	check = 0;
	fprintf(fp,":");
	n = (nw > NWORDS)? NWORDS: nw;

	    d_hexbyte(fp,2*n);
	    d_hexword(fp,2*address);

	d_hexbyte(fp,0);
	for ( i=0; i < n; ++i ) {
	    w = *buf++;

		w = SWAB(w);                    /* swap byte order */
	    d_hexbyte(fp,w>>8);
	    d_hexbyte(fp,w&0xFF);
	}
	d_hexbyte(fp,(-check)&0xFF);
	fprintf(fp,"\n");
	nw -= NWORDS;
	address += NWORDS;
    }
}

   static void hexrec(FILE *fp, unsigned char *buf, int address, int nw)
{
    int i, n;
    unsigned w;

    while ( nw > 0 ) {
	check = 0;
	fprintf(fp,":");
	n = (nw > 16)? 16: nw;

	    d_hexbyte(fp,n);
	    d_hexword(fp,address);

	d_hexbyte(fp,0);
	for ( i=0; i < n; ++i ) {
	    w = *buf++;


	    d_hexbyte(fp,w);

	}
	d_hexbyte(fp,(-check)&0xFF);
	fprintf(fp,"\n");
	nw -= 16;
	address += 16;
    }
}



void hex_dump(){
int mw=65534;
tcu_open_window(&window2, 17,5, 25,3,
		    NULL,
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLACK, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_SINGLE);
G:
strset(filename,0);
tcu_clear_window(&window2);
tcu_wprintf(&window2,"filename:");
tcu_wgets(&window2,11,filename,&length);
strcat(filename,".hex");
if(length!=-1){
 {fp=fopen(filename,"w");if(fp==NULL){tcu_warnbeep();goto G;}
if(PIC){d_hexrec(fp,picbuf ,0,1024);
	d_hexrec(fp,picbuf+0x2007 ,0x2007,1);
	d_hexrec(fp,picbuf+0x2100 ,0x2100,64);
	fprintf(fp,":00000001FF\n");}
else {hexrec(fp,rombuf ,0,grot);
     fprintf(fp,":00000001FF\n");}

fclose(fp);
  }
tcu_close_window(&window2);
fclose(fp);}
strset(filename,0);
tcu_close_window(&window2);
}


winstat(){

tcu_open_window(&window3, 51,15, 28,7,
		    "STATUS",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (BLUE, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, RED),
		    TCU_BOX_SINGLE);
 tcu_clear_window(&window3);
 tcu_wprintf(&window3,"\n");
 tcu_wprintf(&window3,"    buffer: ");tcu_wprintf(&window3,statbuf); tcu_wprintf(&window3,"\n");
 tcu_wprintf(&window3,"    device: ");tcu_wprintf(&window3,statdev); tcu_wprintf(&window3,"\n");
 tcu_wprintf(&window3," interface: ");tcu_wprintf(&window3,statmet);tcu_wprintf(&window3,"\n");
 tcu_wprintf(&window3,"      port: ");tcu_wprintf(&window3,statport);tcu_wprintf(&window3,"\n");

}


menu_setup()
{
     tcu_open_window (&window1,
		    1, 1,
		    80, 25,
		    "Window I",
		    tcu_colour_attrib (WHITE, LIGHTBLUE),
		    tcu_colour_attrib (WHITE, LIGHTGRAY),
		    tcu_colour_attrib (CYAN, BLUE),
		    TCU_BOX_DOUBLE);
		     tcu_wprintf(&window1," ");


   tcu_set_mouse_mode (1);

   r1 = tcu_define_menu (&file_m,
			 NULL,
			 tcu_colour_attrib (YELLOW, CYAN),       /* Title */
			 tcu_colour_attrib (BLUE, CYAN),         /* Box */
			 tcu_colour_attrib (WHITE, MAGENTA),     /* Options */
			 tcu_colour_attrib (WHITE, BLUE),        /* Selected */
			 tcu_colour_attrib (LIGHTGRAY, MAGENTA), /* Deselected */
			 TCU_ESC_ESC | TCU_ESC_CNTL_C | TCU_ESC_PGUP | TCU_ESC_FUNC,
			 TCU_BOX_DOUBLE,
			 file_options,
			 tcu_colour_attrib (LIGHTRED, MAGENTA));

   r1 = tcu_define_menu (&device_m,
			 NULL,
			 tcu_colour_attrib (YELLOW, CYAN),       /* Title */
			 tcu_colour_attrib (BLUE, CYAN),         /* Box */
			 tcu_colour_attrib (WHITE, MAGENTA),     /* Options */
			 tcu_colour_attrib (WHITE, BLUE),        /* Selected */
			 tcu_colour_attrib (LIGHTGRAY, MAGENTA), /* Deselected */
			 TCU_ESC_ESC | TCU_ESC_CNTL_C | TCU_ESC_PGUP | TCU_ESC_FUNC,
			 TCU_BOX_DOUBLE,
			 device_options,
			 0);

   r1 = tcu_define_menu (&settings_m,
			 NULL,
			 tcu_colour_attrib (YELLOW, CYAN),       /* Title */
			 tcu_colour_attrib (BLUE, CYAN),         /* Box */
			 tcu_colour_attrib (WHITE, MAGENTA),     /* Options */
			 tcu_colour_attrib (WHITE, BLUE),        /* Selected */
			 tcu_colour_attrib (LIGHTGRAY, MAGENTA), /* Deselected */
			 TCU_ESC_ESC | TCU_ESC_CNTL_C | TCU_ESC_PGUP | TCU_ESC_FUNC,
			 TCU_BOX_DOUBLE,
			 settings_options,
			 tcu_colour_attrib (LIGHTRED, MAGENTA));

   r1 = tcu_define_menu (&action_m,
			 NULL,
			 tcu_colour_attrib (YELLOW, CYAN),       /* Title */
			 tcu_colour_attrib (BLUE, CYAN),         /* Box */
			 tcu_colour_attrib (WHITE, MAGENTA),     /* Options */
			 tcu_colour_attrib (WHITE, BLUE),        /* Selected */
			 tcu_colour_attrib (LIGHTGRAY, MAGENTA), /* Deselected */
			 TCU_ESC_ESC | TCU_ESC_CNTL_C | TCU_ESC_PGUP | TCU_ESC_FUNC,
			 TCU_BOX_DOUBLE,
                         action_options,
			 tcu_colour_attrib (LIGHTRED, MAGENTA));

   r1 = tcu_define_menu (&option_m,
			 NULL,
			 tcu_colour_attrib (YELLOW, CYAN),       /* Title */
			 tcu_colour_attrib (BLUE, CYAN),         /* Box */
			 tcu_colour_attrib (WHITE, MAGENTA),     /* Options */
			 tcu_colour_attrib (WHITE, BLUE),        /* Selected */
			 tcu_colour_attrib (LIGHTGRAY, MAGENTA), /* Deselected */
			 TCU_ESC_ESC | TCU_ESC_CNTL_C | TCU_ESC_PGUP | TCU_ESC_FUNC,
			 TCU_BOX_DOUBLE,
			 option_options,
			 tcu_colour_attrib (LIGHTRED, MAGENTA));





   mlist[0] = &file_m;
   mlist[1] = &device_m;
   mlist[2] = &settings_m;
   mlist[3] = &action_m;
   mlist[4] = &option_m;
   mlist[5] = NULL;

   tcu_define_pulldown    (&pd,
			   tcu_colour_attrib (WHITE,BLUE),
			   tcu_colour_attrib (YELLOW,BLUE),
			   tcu_colour_attrib (YELLOW,RED),
			   headings,
			   tcu_colour_attrib (LIGHTGREEN, BLUE),
			   mlist);
			   }


void PICM50_r(i){

if(!PIC)return;


switch(i){

case 1:if(pt)pt=0;else pt=PWRTE;break;
case 2:if(cp)cp=0;else cp=CP;break;
case 3:if(wd)wd=0;else wd=WDTE;break;
case 4:{osc++;if(osc==4)osc=0;}break;
	}

fuses=cp+pt+wd+osc;
R_50fuses();
picbuf[0x2007]=fuses;
}



void PICM_r(int i){

if(!PIC)return;
if(C50){PICM50_r(i);return;}
switch(i){

case 1:if(cp)cp=0;else cp=CP;break;
case 2:if(pt)pt=0;else pt=PWRTE;break;
case 3:if(wd)wd=0;else wd=WDTE;break;
case 4:{osc++;if(osc==4)osc=0;}break;
	}
if(F84){if(pt)pt=0;else pt=PWRTE;}
fuses=cp+pt+wd+osc;
R_fuses();
picbuf[0x2007]=fuses;
}
int Pbreak()

{int i, n, w, mask, ldcmd, rdcmd, *buf;

      mask = 0x3FFF;
      ldcmd = LDPROG;
      rdcmd = RDPROG;
       idle_mode();
       delay(5);
   for ( i=0; i<100; ++i ) {
      command(rdcmd);
   picbuf[i] = pin_word() & mask;
      command(INCADD);

   }
}



void main (int argc,char **argv)
{
if(strcmpi(argv[1],"H2")==0)
{poort=0x278;ipoort=0x279;spoort=0x27a;strcpy(statport,"LPT2");winstat();}
else if
(strcmpi(argv[1],"D61")==0){David=6;strcpy(statmet,"David Tait 06");winstat();}
else if
(strcmpi(argv[1],"D62")==0){
poort=0x278;ipoort=0x279;spoort=0x27a;strcpy(statport,"LPT2");David=6;
strcpy(statmet,"David Tait 06");winstat();}
else if
(strcmpi(argv[1],"D1")==0){David=7;strcpy(statmet,"David Tait 07");winstat();}
else if
(strcmpi(argv[1],"D2")==0){
poort=0x278;ipoort=0x279;spoort=0x27a;strcpy(statport,"LPT2");David=7;strcpy(statmet,"David Tait 07");winstat();}
else if
(strcmpi(argv[1],"L1")==0){
Ludi=1;strcpy(statport,"COM1");strcpy(statmet,"LudiPipo");winstat();}
else if
(strcmpi(argv[1],"L2")==0){
Ludi=1;cport=0x2f8;strcpy(statport,"COM2");strcpy(statmet,"LudiPipo");winstat();}
else if
(strcmpi(argv[1],"L3")==0){
Ludi=1;cport=0x3e8;strcpy(statport,"COM3");strcpy(statmet,"LudiPipo");winstat();}
else if
(strcmpi(argv[1],"L4")==0){
Ludi=1;cport=0x2e8;strcpy(statport,"COM4");strcpy(statmet,"LudiPipo");winstat();}

 biosprint(1,0,0);biosprint(1,0,1);
   setup();idle_mode();
   if(Ludi)Pbreak();
   menu_setup();
   versie();
   winstat();
   do {
      tcu_read_pulldown_selection (&pd, &r1, &r2);
      if (r1) {

      if(r1==1&&r2==1)load_bin();
      if(r1==1&&r2==2)testmenu();
      if(r1==1&&r2==3)save_file();
      if(r1==1&&r2==4)hex_dump();
      if(r1==1&&r2==5)viewbuffer();
       if(r1==1&&r2==6)about();
      if(r1==1&&r2==7){setup();idle_mode();tcu_remove_pulldown(&pd);
      tcu_close_window(&window1);exit(0);}

      if(r1==2&&r2==2){eep=1;aantal=511;strcpy(statdev,"24C04");winstat();PIC=0;F84=0;C50=0;C60=0;}
      if(r1==2&&r2==4){eep=1;aantal=2047;strcpy(statdev,"24C16");winstat();PIC=0;F84=0;C50=0;C60=0;}
      if(r1==2&&r2==5){eep=2;aantal=4095;strcpy(statdev,"24C32");winstat();PIC=0;F84=0;C50=0;C60=0;}
      if(r1==2&&r2==3){eep=1;aantal=1023;strcpy(statdev,"24C08");winstat();PIC=0;F84=0;C50=0;C60=0;}
      if(r1==2&&r2==1){eep=1;aantal=255;strcpy(statdev,"24C02");winstat();PIC=0;F84=0;C50=0;C60=0;}
      if(r1==2&&r2==6){eep=2;aantal=8191;strcpy(statdev,"24C65");winstat();PIC=0;F84=0;C50=0;C60=0;}
      if(r1==2&&r2==7){eep=2;aantal=16383;strcpy(statdev,"24C128");winstat();PIC=0;F84=0;C50=0;C60=0;}
      if(r1==2&&r2==8){eep=2;aantal=32767;strcpy(statdev,"24C256");winstat();PIC=0;F84=0;C50=0;C60=0;}

      if(r1==2&&r2==9){strcpy(statdev,"16C84");winstat();aantal=20000;PIC=1;F84=0;C50=0;C60=0;}
      if(r1==2&&r2==10){strcpy(statdev,"16F84");winstat();aantal=20000;F84=1;PIC=1;C50=0;C60=0;}
      if(r1==2&&r2==11){strcpy(statdev,"12C508");winstat();aantal=20000;F84=0;PIC=1;C50=1;C60=0;}
       if(r1==2&&r2==12){strcpy(statdev,"16C622");winstat();aantal=20000;F84=0;PIC=1;C50=0;C60=1;}

      if(r1==3&&r2==2)PICM_r(2);
      if(r1==3&&r2==1)PICM_r(1);
      if(r1==3&&r2==3)PICM_r(3);
      if(r1==3&&r2==4)PICM_r(4);

      if(r1==4&&r2==1)if(C50)P50read();
      else if(PIC)Pread();else if(eep==2)read_65();else read_16();
      if(r1==4&&r2==2)
      if(C60)PIC622_pr();
      else if(C50)PIC508_pr();
      else if(PIC)PIC_pr();else if(eep==2)eprogram65();else prog_16();
      if(r1==4&&r2==3)
       if(C60)verify60();
      else if(C50)verify50();
      else if(PIC)verify();else if(eep==2)verify_65();else verify_16();
      if(r1==4&&r2==4)P_erase();
       if(r1==4&&r2==5)bust();
      if(r1==4&&r2==6){if(PIC)memset(picbuf,0,20000);else memset(rombuf,0,aantal);strcpy(statbuf,"empty");winstat();}
      if(r1==5&&r2==1){Ludi=0;strcpy(statmet,"Henk Schaer");poort=0x378;ipoort=0x379;spoort=0x37a;strcpy(statport,"LPT1");winstat();David=0;idle_mode();}
      if(r1==5&&r2==2){Ludi=0;strcpy(statmet,"David Tait 07");poort=0x378;ipoort=0x379;spoort=0x37a;strcpy(statport,"LPT1");winstat();David=7;idle_mode();}
      if(r1==5&&r2==3){Ludi=0;strcpy(statmet,"David Tait 06");poort=0x378;ipoort=0x379;spoort=0x37a;strcpy(statport,"LPT1");winstat();David=6;idle_mode();}
      if(r1==5&&r2==4){poort=0x378;ipoort=0x379;spoort=0x37a;strcpy(statport,"LPT1");winstat();}
      if(r1==5&&r2==5){poort=0x278;ipoort=0x279;spoort=0x27a;strcpy(statport,"LPT2");winstat();}
      if(r1==5&&r2==6){Ludi=1;strcpy(statmet,"LudiPipo");cport=0x2f8;strcpy(statport,"COM2");winstat();Pbreak();}
      if(r1==5&&r2==7){cport=0x3f8;strcpy(statport,"COM1");setup();winstat();idle_mode();}
      if(r1==5&&r2==8){cport=0x2f8;strcpy(statport,"COM2");setup();winstat();idle_mode();}
      if(r1==5&&r2==9){cport=0x3e8;strcpy(statport,"COM3");setup();winstat();idle_mode();}
      if(r1==5&&r2==10){cport=0x2e8;strcpy(statport,"COM4");setup();winstat();idle_mode();}
      if(r1==5&&r2==11)test_hw();


   }if(!PIC){tcu_close_window(&window7);w7=0;}


   } while (r1);

   tcu_close_window (&window1);

}
