#include <exec/memory.h>

#include "inc.h"
#include "defs.h"
#include "req_manx_5.c"
#include "zz_pointer.h"


void ExternToIntern(void);
void InternToExtern(void);
void SwapInternExtern(void);
int TestCard(void);

/**********************************************************************************/
/***                                                                            ***/
/**********************************************************************************/

void ExternToIntern(void)
{
	int chn;

	chn=WaitForK1();
	if (chn == -1)
	{
		MyReq("Error: K1 not present",NULL," Ok ");
		goto Ende;
	}

	if (!TestCard()) goto Ende;
	MyReq("Not implemented, yet",NULL,"Resume");

	Ende: CLEAR_POINTER(win);
}

	
/**********************************************************************************/
/***                                                                            ***/
/**********************************************************************************/

void InternToExtern(void)
{
	int chn;

	chn=WaitForK1();
	if (chn == -1)
	{
		MyReq("Error: K1 not present",NULL," Ok ");
		goto Ende;
	}

	if (!TestCard()) goto Ende;
	MyReq("Not implemented, yet",NULL,"Resume");

	Ende: CLEAR_POINTER(win);
}

/**********************************************************************************/
/***                                                                            ***/
/**********************************************************************************/

void SwapInternExtern(void)
{
	int chn;

	chn=WaitForK1();
	if (chn == -1)
	{
		MyReq("Error: K1 not present",NULL," Ok ");
		goto Ende;
	}

	if (!TestCard()) goto Ende;
	MyReq("Not implemented, yet",NULL,"Resume");

	Ende: CLEAR_POINTER(win);
}

/**********************************************************************************/
/***                                                                            ***/
/**********************************************************************************/

int TestCard(void)
{
	int i,fo;
	unsigned char buf[97];
	char str[100];
	FILE *fp,*fopen();

	fo=creat("ram:k1-bank",0666);
	if (fo != -1)
	{
		buf[0]=MS_SYSEX; buf[1]=0x40; buf[2]=MASTERCHANNEL; buf[3]=0x20; 
		buf[4]=0x00; buf[5]=0x03; buf[6]=0x0; buf[7]=1;

		for (i=0;i<=87;i++)	buf[8+i]=0x22;    /* Singlessound an externe Card schicken */
		buf[96]=MS_EOX;						  /* und Reaktion abwarten */
		PutMidiMsg(source,buf);
		Delay(3);

		WaitPort(dest->DestPort);
		if (packet = GetMidiPacket(dest)) 
		{
			if (write(fo,packet->MidiMsg,packet->Length) != packet->Length); 
			FreeMidiPacket(packet);
		}
		close(fo);
	}

	fp=fopen("ram:k1-bank","r");
	for (i=0;i<=7;i++) str[i]=fgetc(fp);		/* Error-Code einlesen*/
	fclose(fp);
	DeleteFile("ram:k1-bank");
	
	if (str[3]==0x43)
	{
		MyReq("Sorry, there is no external card",NULL,"Go on");
		return(FALSE);
	}

	if (str[3]==0x42)
	{
		MyReq("Sorry, memory is write protected",NULL,"Go on");
		return(FALSE);
	}

	if (str[3]==0x41)
	{
		MyReq("Sorry, write error on card",NULL,"Go on");
		return(FALSE);
	}

	return(TRUE);
}

