

#include "stdafx.h"
#include "ioTest.h"

#include "../include/mc8041p.h"
#include "MMCControl.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <conio.h>


HINSTANCE g_pDll = NULL;


F1 CMMCControl::_OutW_N = NULL;
F2 CMMCControl::_InW_N = NULL;
F3 CMMCControl::_CloseCard_all = NULL;
F4 CMMCControl::_OpenCard_N = NULL;
F5 CMMCControl::_ValidString = NULL;

int CMMCControl::int_flg[MAX_CARD] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int CMMCControl::BoardOk[MAX_CARD] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int CMMCControl::index[MAX_CARD] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
USHORT CMMCControl::m_rr[4][8] = { 0, };
int CMMCControl::m_no = 0;



void isr_sub( int no )
{
	CMMCControl::int_flg[ no ] = 1;
	CMMCControl::command_N( no, 0x1, 0x0f );
	CMMCControl::_InW_N( no, RR3 );
	CMMCControl::command_N( no, 0x1, 0x0f );
	CMMCControl::_OutW_N( no, WR3, 0x0100 );
}

void WINAPI isr0(void){		isr_sub( 0 );}
void WINAPI isr1(void){		isr_sub( 1 );}
void WINAPI isr2(void){		isr_sub( 2 );}
void WINAPI isr3(void){		isr_sub( 3 );}
void WINAPI isr4(void){		isr_sub( 4 );}
void WINAPI isr5(void){		isr_sub( 5 );}
void WINAPI isr6(void){		isr_sub( 6 );}
void WINAPI isr7(void){		isr_sub( 7 );}
void WINAPI isr8(void){		isr_sub( 8 );}
void WINAPI isr9(void){		isr_sub( 9 );}
void WINAPI isr10(void){	isr_sub( 10 );}
void WINAPI isr11(void){	isr_sub( 11 );}
void WINAPI isr12(void){	isr_sub( 12 );}
void WINAPI isr13(void){	isr_sub( 13 );}
void WINAPI isr14(void){	isr_sub( 14 );}
void WINAPI isr15(void){	isr_sub( 15 );}


///////////////////

CMMCControl::CMMCControl()
{
	_OutW_N = NULL;
	_InW_N = NULL;
	_CloseCard_all = NULL;
	_OpenCard_N = NULL;
	_ValidString = NULL;

	g_pDll = NULL;
}

CMMCControl::~CMMCControl()
{
	close();
}

void CMMCControl::readRR(int no, int axis) // axis=x:0,y:1,z:2,u:3
{
	int t = (0x01 << axis) & 0x0f;
	int addr[8] = {RR0, RR1, RR2, RR3, RR4, RR5, RR6, RR7};
	_OutW_N( no, WR0,( t << 8 )+ 0xf );
	for(int i=0;i<8;i++)
		m_rr[axis][i] = _InW_N( no, addr[i] );
}

void CMMCControl::wreg1_N( int no, int axis, int wdata )
{
	axis &= 0x0f;
	_OutW_N( no, WR0,( axis << 8 )+ 0xf );
	_OutW_N( no, WR1, wdata );
}

void CMMCControl::wreg2_N( int no, int axis, int wdata )
{
	axis &= 0x0f;
	_OutW_N( no, WR0,( axis << 8 )+ 0xf );
	_OutW_N( no, WR2, wdata );
}

void CMMCControl::wreg3_N( int no, int axis, int wdata )
{
	axis &= 0x0f;
	_OutW_N( no, WR0,( axis << 8 )+ 0xf );
	_OutW_N( no, WR3, wdata );
}

void CMMCControl::command_N( int no, int axis, int cmd )
{
	axis &= 0x0f;
	_OutW_N( no, WR0,( axis << 8 )+ cmd );
}

void CMMCControl::range_N( int no, int axis, long wdata )
{
	axis &= 0x0f;
	if( wdata < 1 )
		wdata = 1;
	if( wdata > 8000000L )
		wdata = 8000000L;

	_OutW_N( no, WR7,( wdata >> 16 )& 0x0ffff );
	_OutW_N( no, WR6, wdata & 0x0ffff );
	_OutW_N( no, WR0,( axis << 8 )+ 0x00 );
}

void CMMCControl::acac_N( int no, int axis, int wdata )
{
	axis &= 0x0f;
	if( wdata > 65535L )
		wdata = 65535L;
	if( wdata < 1 )
		wdata = 1;

	_OutW_N( no, WR6, wdata );
	_OutW_N( no, WR0,( axis << 8 )+ 0x01 );
}

void CMMCControl::acc_N( int no, int axis, int wdata )
{
	axis &= 0x0f;
	if( wdata > 8000 )
		wdata = 8000;
	if( wdata < 1 )
		wdata = 1;

	_OutW_N( no, WR6, wdata );
	_OutW_N( no, WR0,( axis << 8 )+ 0x02 );
}

void CMMCControl::dec_N( int no, int axis, int wdata )
{
	axis &= 0x0f;
	if( wdata > 8000 )
		wdata = 8000;
	if( wdata < 1 )
		wdata = 1;

	_OutW_N( no, WR6, wdata );
	_OutW_N( no, WR0,( axis << 8 )+ 0x03 );
}

void CMMCControl::startv_N( int no, int axis, int wdata )
{
	axis &= 0x0f;
	if( wdata > 8000 )
		wdata = 8000;
	if( wdata < 1 )
		wdata = 1;

	_OutW_N( no, WR6, wdata );
	_OutW_N( no, WR0,( axis << 8 )+ 0x04 );
}

void CMMCControl::speed_N( int no, int axis, int wdata )
{
	axis &= 0x0f;
	if( wdata > 8000 )
		wdata = 8000;
	if( wdata < 1 )
		wdata = 1;

	_OutW_N( no, WR6, wdata );
	_OutW_N( no, WR0,( axis << 8 )+ 0x05 );
}

void CMMCControl::pulse_N( int no, int axis, long wdata )
{
	axis &= 0x0f;
	if( wdata < -8388608L )
		wdata = -8388608L;
	if( wdata > 268435455L )
		wdata = 268435455L;

	_OutW_N( no, WR7,( wdata >> 16 )& 0x0ffff );
	_OutW_N( no, WR6, wdata & 0x0ffff );
	_OutW_N( no, WR0,( axis << 8 )+ 0x06 );
}

void CMMCControl::decp_N( int no, int axis, long wdata )
{
	axis &= 0x0f;

	_OutW_N( no, WR7,( wdata >> 16 )& 0x0ffff );
	_OutW_N( no, WR6, wdata & 0x0ffff );
	_OutW_N( no, WR0,( axis <<  8 )+ 0x7 );
}

void CMMCControl::center_N( int no, int axis, long wdata )
{
	axis &= 0x0f;
	if( wdata < -8388608L )
		wdata = -8388608L;
	if( wdata > 8388607L )
		wdata = 8388607L;

	_OutW_N( no, WR7,( wdata >> 16 )& 0x0ffff );
	_OutW_N( no, WR6, wdata & 0x0ffff );
	_OutW_N( no, WR0,( axis <<  8 )+ 0x8 );
}

void CMMCControl::lp_N( int no, int axis, long wdata )
{
	axis &= 0x0f;

	_OutW_N( no, WR7,( wdata >> 16 )& 0x0ffff );
	_OutW_N( no, WR6, wdata & 0x0ffff );
	_OutW_N( no, WR0,( axis <<  8 )+ 0x9 );
}

void CMMCControl::ep_N( int no, int axis, long wdata )
{
	axis &= 0x0f;

	_OutW_N( no, WR7,( wdata >> 16 )& 0x0ffff );
	_OutW_N( no, WR6, wdata & 0x0ffff );
	_OutW_N( no, WR0,( axis <<  8 )+ 0xa );
}

void CMMCControl::compp_N( int no, int axis, long wdata )
{
	axis &= 0x0f;

	_OutW_N( no, WR7,( wdata >> 16 )& 0x0ffff );
	_OutW_N( no, WR6, wdata & 0x0ffff );
	_OutW_N( no, WR0,( axis <<  8 )+ 0xb );
}

void CMMCControl::compm_N( int no, int axis, long wdata )
{
	axis &= 0x0f;

	_OutW_N( no, WR7,( wdata >> 16 )& 0x0ffff );
	_OutW_N( no, WR6, wdata & 0x0ffff );
	_OutW_N( no, WR0,( axis <<  8 )+ 0xc );
}

void CMMCControl::accofst_N( int no, int axis, long wdata )
{
	axis &= 0x0f;

	_OutW_N( no, WR7,( wdata >> 16 )& 0x0ffff );
	_OutW_N( no, WR6, wdata & 0x0ffff );
	_OutW_N( no, WR0,( axis <<  8 )+ 0xd );
}

long CMMCControl::readlp_N( int no, int axis )
{
	long	a, d6, d7;

	axis &= 0x0f;
	_OutW_N( no, WR0,( axis << 8 )+ 0x10 );
	d6 = _InW_N( no, RR6 );
	d7 = _InW_N( no, RR7 );
	a = d6 +( d7 << 16 );
	return( a );
}

long CMMCControl::readep_N( int no, int axis )
{
	long	a, d6, d7;

	axis &= 0x0f;

	_OutW_N( no, WR0,( axis << 8 )+ 0x11 );
	d6 = _InW_N( no, RR6 );
	d7 = _InW_N( no, RR7 );
	a = d6 +( d7 << 16 );
	return( a );
}

void CMMCControl::wait_N( int no, int axis )
{
	axis &= 0x0f;

	while( _InW_N( no, RR0 )& axis );
}

void CMMCControl::next_wait_N( int no )
{
	while(( _InW_N( no, RR0 )& 0x0200 )== 0 );
}

void CMMCControl::bp_wait_N( int no )
{
	while(( _InW_N( no, RR0 )& 0x6000 )== 0x6000 );
}

int CMMCControl::init()
{
	close();

	g_pDll = LoadLibrary("./mc8041p.dll");

	if(!g_pDll)
	{
#ifdef _DEBUG
		g_pDll = LoadLibrary("../../../lib_src/mc8041p/Debug/mc8041p.dll");
#else
		g_pDll = LoadLibrary("../../../lib_src/mc8041p/Release/mc8041p.dll");
#endif
	}

	if(!g_pDll)
	{
		g_pDll = LoadLibrary("../../../install/mc8041p/mc8041p.dll");
	}

	if(!g_pDll) return 0;

	_OutW_N = (F1)GetProcAddress(g_pDll, "OutW_N");
	_InW_N = (F2)GetProcAddress(g_pDll, "InW_N");
	_CloseCard_all = (F3)GetProcAddress(g_pDll, "CloseCard_all");
	_OpenCard_N = (F4)GetProcAddress(g_pDll, "OpenCard_N");
	_ValidString = (F5)GetProcAddress(g_pDll, "ValidString");

	if(!_OutW_N || !_InW_N || !_CloseCard_all || !_OpenCard_N)
	{
		_OutW_N=NULL;
		_InW_N=NULL;
		_CloseCard_all=NULL;
		_OpenCard_N=NULL;
		_ValidString=NULL;

		FreeLibrary(g_pDll);
		g_pDll = NULL;

		return 0;
	}

	HANDLE		ists;
	int			no;
	int			cnt;

	m_no = 0;

	for( no = 0; no < MAX_CARD; no++ ){
		int_flg[ no ]= 0;
		ists = NULL;
		switch( no ){
			case 0:		ists = _OpenCard_N( no, isr0, 0, 0, 0 );	break;
			case 1:		ists = _OpenCard_N( no, isr1, 0, 0, 0  );	break;
			case 2:		ists = _OpenCard_N( no, isr2, 0, 0, 0  );	break;
			case 3:		ists = _OpenCard_N( no, isr3, 0, 0, 0  );	break;
			case 4:		ists = _OpenCard_N( no, isr4, 0, 0, 0  );	break;
			case 5:		ists = _OpenCard_N( no, isr5, 0, 0, 0  );	break;
			case 6:		ists = _OpenCard_N( no, isr6, 0, 0, 0  );	break;
			case 7:		ists = _OpenCard_N( no, isr7, 0, 0, 0  );	break;
			case 8:		ists = _OpenCard_N( no, isr8, 0, 0, 0  );	break;
			case 9:		ists = _OpenCard_N( no, isr9, 0, 0, 0  );	break;
			case 10:	ists = _OpenCard_N( no, isr10, 0, 0, 0  );	break;
			case 11:	ists = _OpenCard_N( no, isr11, 0, 0, 0  );	break;
			case 12:	ists = _OpenCard_N( no, isr12, 0, 0, 0  );	break;
			case 13:	ists = _OpenCard_N( no, isr13, 0, 0, 0  );	break;
			case 14:	ists = _OpenCard_N( no, isr14, 0, 0, 0  );	break;
			case 15:	ists = _OpenCard_N( no, isr15, 0, 0, 0  );	break;
		}
		if( ists == NULL ){
			BoardOk[ no ] = 0;
		} else {
			BoardOk[ no ] = 1;
			index[ m_no ] = no;
			m_no++;

			_OutW_N( no, WR0, 0x8000);

			for(cnt =0; cnt < 10000; ++cnt);

			wreg1_N( no, 0xf, 0x0000 );
			wreg2_N( no, 0xf, 0x0000 );
			wreg3_N( no, 0xf, 0x0000 );

			accofst_N( no, 0xf, 0 );
			range_N( no, 0xf, 800000L );
			acac_N( no, 0xf, 1010 );
			acc_N( no, 0xf, 400 );
			dec_N( no, 0xf, 400 );
			startv_N( no, 0xf, 100 );
			speed_N( no, 0xf, 4000 );
			pulse_N( no, 0xf, 32000 );
			lp_N( no, 0xf, 0 );
			ep_N( no, 0xf, 0 );
		}
	}

	return m_no;
}

void CMMCControl::close()
{
	if(g_pDll)
	{
		_CloseCard_all();
		FreeLibrary(g_pDll);
	}
	g_pDll = NULL;
}

WORD CMMCControl::inwN(int no, WORD iocc)
{
	return _InW_N(no, iocc);
}

void CMMCControl::outwN(int no, WORD iocc, WORD data)
{
	_OutW_N(no, iocc, data);
}
