/****************************************************************/
/*								*/
/* Testprogramm für GAL-Brenner					*/
/*								*/
/* Soft- and Hardware  copyright (c)1991			*/
/*	 by Christian Habermann					*/
/*	    Asamstr. 17						*/
/*	    8050 Freising (W-Germany)				*/
/*								*/
/*  cc -z4000 GALerTest.c					*/
/*  as -n -c -d port.asm					*/
/*  ln GALerTest.o port.o -lc					*/
/*								*/
/****************************************************************/



#include <exec/memory.h>
#include <exec/ports.h>
#include <exec/types.h>
#include <intuition/intuition.h>
#include <functions.h>

#include "GALer.h"


#define	 YPOS	30


void Clear();
void Print();
void WaitForAction();



struct	MsgPort		*timeport;
struct	timerequest	*timereq;
struct	IntuitionBase	*IntuitionBase;
struct	GfxBase		*GfxBase;
struct	IntuiMessage	*message;
struct	Window		*window;
struct	RastPort	*rp;
struct	TextFont	*txtfont;
struct	TextAttr	txtattr;


SHORT BorderVectors1a[] = { 0,15,73,15,73,0 };
SHORT BorderVectors1b[] = { 0,15,0,0,73,0 };

struct Border Border1b    = { -1,-1,2,0,JAM1,3,BorderVectors1b,NULL };
struct Border Border1a    = { -1,-1,1,0,JAM1,3,BorderVectors1a,&Border1b };



struct IntuiText GadIText3 = { 1,0,JAM2,4,4,&txtattr,
			       (UBYTE *)"  EXIT  ",NULL };

struct IntuiText GadIText2 = { 1,0,JAM2,12,4,&txtattr,
			       (UBYTE *)"Cancel",NULL };

struct IntuiText GadIText1 = { 1,0,JAM2,4,4,&txtattr,
			       (UBYTE *)"Continue",NULL };



struct Gadget CancelGad = { NULL, 530, 180, 72, 14,
			    NULL,
			    RELVERIFY,
			    BOOLGADGET+REQGADGET,
			    (APTR)&Border1a,NULL,
			    &GadIText2,NULL,NULL,1,NULL };

struct Gadget ContGad   = { &CancelGad, 40, 180, 72, 14,
			    NULL,
			    RELVERIFY,
			    BOOLGADGET+REQGADGET,
			    (APTR)&Border1a,NULL,
			    &GadIText1,NULL,NULL,0,NULL };




struct NewWindow MyWin =  { 0,0,640,200,
			    0,1,
			    CLOSEWINDOW|GADGETUP,
			    ACTIVATE|WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH,
			    &ContGad,NULL,
			    (UBYTE *)"GALerTest  (c) 1993 by Christian Habermann",
			    NULL,NULL,
			    0,0,0,0,
			    WBENCHSCREEN
			  };




int	prog_volt = 0;		/*Dummy für das Modul "Port"*/
int	ytxt = YPOS;

main()
{
int errorIC6, errorIC7;
int n;


 openstuff();

 if (GetTimer()) {				/*Timer holen*/
   puts("kann Timer nicht finden\n");
   exit(FALSE);    
  }


 rp = window -> RPort;

 SetAPen(rp, 1L);
 SetFont(rp, txtfont);			/*Font für Textausgabe*/




 InitParPort();				/*Datenrichtungsregister der CIAs*/

 InitGALer();

 Print("This is a program for testing the hardware V1.2 of the GALer project.");
 Print("You need this program to adjust the trimpots R40-44 too.");
 Print("IMPORTANT: There must be no GAL in the Textool socket now!");
 Print("");
 Print("If the hardware works not as the test program demands, then");
 Print("you should cancel 'GALerTest' immediately and try to remove the");
 Print("bug in your hardware.");
 Print("");
 Print("Please select the 'Continue' gadget to start the test.");
 WaitForAction(0);


 Clear();
 Print("Now the LED should be off and all pins of the Textool socket should");
 Print("be set at LOW level (about 0 volt).");
 Print("");
 Print("Please select 'Continue'!");
 EnableOutput();
 WriteByte(0x00,IC3);
 WriteByte(0x00,IC4);
 WriteByte(0x00,IC5);
 WaitForAction(0);


 Clear();
 Print("Now the LED should be on.");
 Print("");
 Print("Please select 'Continue'!");
 LED(ON);
 WaitForAction(0);


 Clear();
 Print("Now the LED should be off again.");
 Print("");
 Print("Please select 'Continue'!");
 LED(OFF);
 WaitForAction(0);


 Clear();
 Print("At pin 24 of the Textool socket should be a HIGH level (about 5 volt).");
 Print("");
 Print("Please select 'Continue'!");
 SetGAL(GAL20V8);
 EnableVcc();
 WaitForAction(0);


 Clear();
 Print("At pin 22 of the Textool socket should be a HIGH level too.");
 Print("");
 Print("Please select 'Continue'!");
 DisableVcc();
 SetGAL(GAL16V8);
 EnableVcc();
 WaitForAction(0);


 Clear();
 Print("At all pins should be a HIGH level with exception of pin 12.");
 Print("Pin 12 is GND.");
 Print("");
 Print("Please select 'Continue'!");
 InitGALer();
 EnableOutput();
 WriteByte(0xff,IC3);
 WriteByte(0xff,IC4);
 WriteByte(0xff,IC5);
 WaitForAction(0);


 Clear();
 Print("At pins 1, 3, 5, 7, 9, 12, 14, 16, 18, 20, 22, 24 should be a HIGH level.");
 Print("All other pins should be LOW.");
 Print("");
 Print("Please select 'Continue'!");
 SetGAL(GAL20V8);
 EnableVcc();
 WriteByte(0x55,IC3);
 WriteByte(0x55,IC4);
 WriteByte(0x55,IC5);
 WaitForAction(0);


 Clear();
 Print("Now at pins 2, 4, 6, 8, 10, 13, 15, 17, 19, 21, 23 should be a HIGH level.");
 Print("All other pins should be LOW.");
 Print("");
 Print("Please select 'Continue'!");
 DisableVcc();
 WriteByte(0xaa,IC3);
 WriteByte(0xaa,IC4);
 WriteByte(0xaa,IC5);
 WaitForAction(0);


 DisableVcc();
 InitGALer();
 EnableOutput();
 VeditOn();


 Clear();
 Print("***************************************************************************");
 Print("Now you have to adjust the trimpots R40-R44.");
 Print("If this does not work, check IC 9, IC 10, T3, T4.");

 Print("");
 Print("Use trimpot R40 to adjust the voltage at pin 2 to exactly 16.5V.");
 Print("Please select 'Continue'!");
 SetVolt(0);
 SetGAL(GAL20V8);
 EnableVEdit();
 WaitForAction(0);

 Print("");
 Print("Use trimpot R41 to adjust the voltage at pin 2 to exactly 15.75V.");
 Print("Please select 'Continue'!");
 SetVolt(1);
 WaitForAction(0);

 Print("");
 Print("Use trimpot R42 to adjust the voltage at pin 2 to exactly 14.5V.");
 Print("Please select 'Continue'!");
 SetVolt(2);
 WaitForAction(0);

 Print("");
 Print("Use trimpot R43 to adjust the voltage at pin 2 to exactly 14.0V.");
 Print("Please select 'Continue'!");
 SetVolt(3);
 WaitForAction(0);

 Print("");
 Print("Use trimpot R44 to adjust the voltage at pin 2 to exactly 12.0V.");
 Print("Please select 'Continue'!");
 SetVolt(4);
 WaitForAction(0);

 Print("");
 Print("O.K., now all voltages are adjusted. Let me do some other tests.");
 Print("Please select 'Continue'!");
 Print("***************************************************************************");
 WaitForAction(0);
 Clear();

 Print("Now at pin 4 should be a voltage of 12.0V and pin 2 should be");
 Print("at LOW level. If this does not work, check T1, T2.");
 Print("");
 Print("Please select 'Continue'!");
 DisableVEdit();
 SetGAL(GAL16V8);
 EnableVEdit();
 WaitForAction(0);
 Clear();




			/*ab hier wird überprüft, ob das Auslesen der*/
			/*Ausgangspegel des GALs funktioniert (IC6a, IC7)*/
 Print("Now I'm trying to read pins 14-22 of the Textool socket...");
 SetGAL(GAL20V8);
 InitGALer();
 EnableVcc();
 EnableOutput();
			/*IC6a testen*/
 errorIC6 = errorIC7 = 0;
 WriteByte(0x40,IC3);
 if (!ReadByte(IC6))
   errorIC6 = 1;

 WriteByte(0x00,IC3);
 if (ReadByte(IC6))
   errorIC6 = 1;
			/*IC7 testen*/
 WriteByte(0x00,IC5);
 if (ReadByte(IC7))
   errorIC7 = 1;

 WriteByte(0x2A,IC3);
 WriteByte(0x20,IC5);
 if (ReadByte(IC7) != 0xAA)
   errorIC7 = 1;

 WriteByte(0x15,IC3);
 WriteByte(0x10,IC5);
 if (ReadByte(IC7) != 0x55)
   errorIC7 = 1;

 DisableVcc();
 DisableOutput(); 

 if (errorIC6 || errorIC7) {
   Print("");
   Print(" ERROR!!!");
   Print("   There must be an error at the BUSY line, R27, IC8, IC6a, IC6b, IC7");
   Print("   or R13. Please check this components.");
   Print("");
   Print(" Description of error:");
   if (errorIC6 && !errorIC7) {
     Print("  I can't read pin 22 through the BUSY line of the parallel port.");
   }

   if (!errorIC6 && errorIC7) {
     Print("  I can't read pins 14-21 through the BUSY line of the parallel port.");
   }

   if (errorIC6 && errorIC7) {
     Print("  I can't read pins 14-21 and pin 22 through the BUSY line of the");
     Print("  parallel port.");
   }

 }
 else {

   Print("O.K., no error found (you have a lot going for you).");
   Print("");
   Print("If you found no errors and if you have adjusted all voltages");
   Print("(12V, 14V, 14.5V, 15.75V and 16.5V), GALer works fine.");
   Print("");
   Print("Now you can read/program GALs by using the software called GALer.");

 }


 CancelGad.GadgetText = &GadIText3;
 OffGadget(&ContGad, window, NULL);
 RefreshGList(&ContGad, window, NULL);

 WaitForAction(0);


 RestoreParPort();

 FreeTimer();

 closestuff();
}






GetTimer()				/*Timer holen*/
{

 timeport = CreatePort(0L,0L);
 if (timeport == NULL)
   return(-1);

 timereq=(struct timerequest *)AllocMem(
			(long)sizeof(struct timerequest),MEMF_PUBLIC);
 if (timereq == NULL) {
   DeletePort(timeport);
   return(-1);
  }

 timereq->tr_node.io_Message.mn_Node.ln_Type=NT_MESSAGE;
 timereq->tr_node.io_Message.mn_Node.ln_Pri=0;
 timereq->tr_node.io_Message.mn_ReplyPort=timeport;

 if (OpenDevice(TIMERNAME,UNIT_MICROHZ,timereq,0L)) {
   FreeMem(&timereq,(long)sizeof(struct timerequest));
   DeletePort(timeport);
   return(-1);
  }

 return(0);
}



void FreeTimer()			/*Timer wieder freigeben*/
{
 CloseDevice(timereq);
 FreeMem(timereq,(long)sizeof(struct timerequest));
 DeletePort(timeport);
}



/*micro: microsec. to wait
  secs: seconds to wait
*/
void WaitForTimer(micro)
ULONG micro;
{
 timereq->tr_node.io_Command=TR_ADDREQUEST;
 timereq->tr_time.tv_secs=0;
 timereq->tr_time.tv_micro=micro;
 DoIO(timereq);
}




void Clear()
{
 ytxt = YPOS;
 SetAPen(rp, 0L);
 RectFill(rp, 10L, 22L, 630L, 170L);
 SetAPen(rp, 1L);
}



void Print(text)
UBYTE	*text;
{

 if (ytxt < 160)
   ytxt += 10;
 else
   ScrollRaster(rp,0L,10L,10L,22L,630L,170L);

 Move(rp,20L,(long)ytxt);
 Text(rp,text,(long)strlen(text));
}




/* wartet auf das Anklicken eines Gadgets
*/
void WaitForAction()
{
ULONG	class;
USHORT	code,gadID;

 for(;;) {
   Wait(1L<<window->UserPort->mp_SigBit);
   if (message=(struct IntuiMessage *)GetMsg(window->UserPort)) {
     class = message -> Class;
     code  = message -> Code;
     if (class == GADGETUP)
       gadID=((struct Gadget *)message->IAddress)->GadgetID;
     ReplyMsg(message);

     switch(class) {
	case CLOSEWINDOW: closestuff();		/*Programm fluchtartig*/
			  FreeTimer();		/*verlassen*/
			  RestoreParPort();
			  exit(0);
			  break;

	case GADGETUP:    if (!gadID)
			    return;			    
			  else {		/*Abbruch angeklickt*/
			    closestuff();
			    FreeTimer();
			    RestoreParPort();
			    exit (0);
			  }
			  break;
     }
   }
 }
}





closestuff()
{
  if (window)  CloseWindow(window);
  if (GfxBase) CloseLibrary(GfxBase);
  if (IntuitionBase) CloseLibrary (IntuitionBase);
}


openstuff()
{
 if ((IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0L)) == NULL) {
   puts ("kann intuition.library nicht öffnen");
   exit(FALSE);
 }

 if ((GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L)) == NULL) {
   puts ("kann graphics.library nicht öffnen");
   closestuff();
   exit(FALSE);
 }


 txtattr.ta_Name  = (STRPTR)"topaz.font";		/*Font holen*/
 txtattr.ta_YSize = 8;
 txtattr.ta_Style = 0;
 txtattr.ta_Flags = 0;

 if (!(txtfont = (struct TextFont *)OpenFont(&txtattr))) {
   puts("kann topaz.font 8 nicht finden");
   closestuff();
   exit(FALSE);
 }


 if ((window = (struct Window *)OpenWindow(&MyWin)) == NULL) {
   puts ("kann Fenster nicht öffnen");
   closestuff();
   exit(FALSE);
 }
}




