/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
*/
/*
 * $Source: f:/miner/source/main/rcs/coindev.c $
 * $Revision: 2.0 $
 * $Author: john $
 * $Date: 1995/02/27 11:26:39 $
 * 
 * Routines to read the coin counter.
 * 
 * $Log: coindev.c $
 * Revision 2.0  1995/02/27  11:26:39  john
 * New version 2.0, which has no anonymous unions, builds with
 * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
 * 
 * Revision 1.2  1994/09/16  16:15:05  john
 * Added acrade sequencing.
 * 
 * Revision 1.1  1994/09/16  12:56:09  john
 * Initial revision
 * 
 * 
 */


#pragma off (unreferenced)
static char rcsid[] = "$Id: coindev.c 2.0 1995/02/27 11:26:39 john Exp $";
#pragma on (unreferenced)

#include <stdio.h>
#include <dos.h>
#include <conio.h>

#include "coindev.h"

int CoinMechPort[3]  =  { COINMECH1_PORT,
                          COINMECH2_PORT,
                          COINMECH3_PORT };

int CoinMechCtrl[3]  =  { COINMECH1_CTRLMASK,
                          COINMECH2_CTRLMASK,
                          COINMECH3_CTRLMASK };

unsigned int CoinMechLastCnt[3];


int coindev_init(CoinMechNumber)
{
    int x;

    int CoinMechAdj[3]   =  { COINMECH1_ADJMASK,
                              COINMECH2_ADJMASK,
                              COINMECH3_ADJMASK };

    /* set up IO port for our special use */
    outp(COINMECH_CMDPORT, CoinMechCtrl[CoinMechNumber]);
    outp(CoinMechPort[CoinMechNumber], 0);
    outp(CoinMechPort[CoinMechNumber], 0);

    /* write to the IO board so that the counter eventually gets cleared */
    for( x = 10; x > 0; x-- )
    {
       outp(COINMECH_ADJPORT, CoinMechAdj[CoinMechNumber]);
       outp(COINMECH_ADJPORT, 0);
       if( coindev_read(CoinMechNumber) == 0 )
       {
          break;
       }
    }

    CoinMechLastCnt[CoinMechNumber] = 0;

    return(x);  /* TRUE == CoinMech is cleared;  FALSE == CoinMech error! */
}


/* Do a raw read of the specified CoinMech */
unsigned int coindev_read(CoinMechNumber)
{
    unsigned int x;

    /* read lower byte, then upper byte */
    x  = (inp(CoinMechPort[CoinMechNumber]));
    x += (inp(CoinMechPort[CoinMechNumber]) * 256);

    /* conver to usable number */
    if( x )
    {
        x = (65536L - x);
    }

    return(x);
}


/* read the specified CoinMech and return the */
/* ...number of clicks since the last read    */
unsigned int coindev_count(CoinMechNumber)
{
    unsigned int x, y;

    x = coindev_read(CoinMechNumber);
    y = x - CoinMechLastCnt[CoinMechNumber];

    CoinMechLastCnt[CoinMechNumber] = x;

    return(y);
}



