#include "willepr.h"

//---------------------------------------
//             AT49Fxxx, Am29F
//---------------------------------------
unsigned long  Chip_C_adres[]    = {0x5555,0x2AAA,0x5555,0x5555,0x2AAA,0x5555};
unsigned long  Chip_C_adresF[]   = {0x555,0x2AA,0x555,0x555,0x2AA,0x555};
unsigned char  Chip_Erase_Data[] = {0xAA,0x55,0x80,0xAA,0x55,0x10};
unsigned char  Chip_enP_Data[]   = {0xAA,0x55,0xA0};
unsigned char  Chip_disP_Data[]  = {0xAA,0x55,0x80,0xAA,0x55,0x20};
unsigned char  Chip_BootB_Data[] = {0xAA,0x55,0x80,0xAA,0x55,0x40};
unsigned char  Chip_ProdI_Data[] = {0xAA,0x55,0x90};
unsigned char  Chip_ProdE_Data[] = {0xAA,0x55,0xF0};

void enSoftP_Am29F(void){
	for(int i=0 ;i<3;i++) {
	     read;assert; ms_delay(4);
	     r_adres_28C(Chip_C_adresF[i]); ms_delay(4);

	     write;assert; ms_delay(4);
	     outportb(poort,Chip_enP_Data[i]); ms_delay(4);

	     pgrlo;assert;ms_delay(2);pgrhi;assert;
	     ms_delay(10);
	}
}

void disSoftP_Am29F(void){
	for(int i=0 ;i<6;i++) {
	     read;assert; ms_delay(4);
	     r_adres_28C(Chip_C_adresF[i]); ms_delay(4);

	     write;assert; ms_delay(4);
	     outportb(poort,Chip_disP_Data[i]); ms_delay(4);

	     pgrlo;assert;ms_delay(2);pgrhi;assert;
	     ms_delay(10);
	}
}

void enSoftP_AT49F(void){
	for(int i=0 ;i<3;i++) {
	     read;assert; ms_delay(4);
	     r_adres_28C(Chip_C_adres[i]); ms_delay(4);

	     write;assert; ms_delay(4);
	     outportb(poort,Chip_enP_Data[i]); ms_delay(4);

	     pgrlo;assert;ms_delay(2);pgrhi;assert;
	     ms_delay(10);
	}
}

void prog_AT49F(void)			// DW: added void
{
	long e=0, t=aantal/52, i, y, hgrot,index,index2;

	int test,tBP = 30;
	unsigned char g,q,ch,ch2,flag_err;

	if(st!=0) getoff();
	hgrot=grot;
	tcu_open_window(&window2, 55,15,22,6,NULL,
		tcu_colour_attrib (WHITE, LIGHTBLUE),
		tcu_colour_attrib (RED, LIGHTGRAY),
		tcu_colour_attrib (CYAN, BLUE),TCU_BOX_SINGLE);

	show_action("programming chip");

	setup_28C();
	q = 1;

	flag_err = OFF;
	if(grot>=aantal)grot=aantal-1;
	for(i=0;i<=grot;i++){
		y =i; index = y+st;
		if (aantal == 0x8000) {
		    index2 = index & 0x4000;
		    index2 = index2 << 1;
		    index  = index | index2;
		}
rep_28C:
		g=*(rombuf+XMSCurrent(y,0));
		if (eep == Am29F)
			enSoftP_Am29F();
		else
			enSoftP_AT49F();
		pgrhi;assert;
		read;assert;r_adres_28C(index);
		write;assert;outportb(poort,g);
		ms_delay(5);
		pgrlo;assert;ms_delay(2);pgrhi;assert;
		ms_delay(tBP);
		read;assert;r_adres_28C(index);
		ch = in_word();

		show_inout(i,g,ch,tBP);
		if(g!=ch){
			if(q==20){
				show_error(i,st,ch);
				flag_err = ON;
				goto exit_28C;
			}
			else {
				q++;
				tBP = tBP+20;
				if(tBP > 200) tBP = 200;
				goto rep_28C;
			}
		}

		if(e==t){tcu_wprintf(&window4,"þ");e=0;	}
		e++;
		q = 1;
	}
exit_28C:
	tcu_close_window(&window2);
	tcu_close_window(&window4);
	if(!flag_err) verify_28C();
	power_down();/*assert; */           //ARW: was powoff
}

void erase_AT49F()
{
      int i;

      show_action("ERASE CHIP");
      setup_28C();
      for(i=0;i<6;i++) {
		if(eep == Am29F) {
		    read;assert;r_adres_29C(Chip_C_adresF[i]);
		}else
		    read;assert;r_adres_29C(Chip_C_adres[i]);
		    write;assert;outportb(poort,Chip_Erase_Data[i]);
		     pgrlo;assert;ms_delay(1);pgrhi;assert;
      }
      // delay 12 sec.
      tcu_wprintf(&window4,"----- delay 12 sec-----");
      reset_timer();delay(12000);setup_timer(0xb0);
      power_down();
      tcu_wprintf(&window4,"\n----- Erase completed-----");getch();
      tcu_close_window(&window4);
}

void Boot_Block_AT49F()
{
      int i;

      show_action("Boot Block Lockout");
      setup_28C();
      for(i=0;i<6;i++) {
		read;assert;r_adres_29C(Chip_C_adres[i]);
		write;assert;outportb(poort,Chip_BootB_Data[i]);
		pgrlo;assert;ms_delay(1);pgrhi;assert;
      }
      // delay 1.5 sec.
      reset_timer();delay(1500);setup_timer(0xb0);
      power_down();
      tcu_wprintf(&window4,"       -----completed-----");getch();
      tcu_close_window(&window4);
}
