/***************************************************************************************1* Genesis Plus2* Sega Activator support3*4* Copyright (C) 2011-2013 Eke-Eke (Genesis Plus GX)5*6* Redistribution and use of this code or any derivative works are permitted7* provided that the following conditions are met:8*9* - Redistributions may not be sold, nor may they be used in a commercial10* product or activity.11*12* - Redistributions that are modified from the original source must include the13* complete source code, including the source code for all components used by a14* binary built from the modified sources. However, as a special exception, the15* source code distributed need not include anything that is normally distributed16* (in either source or binary form) with the major components (compiler, kernel,17* and so on) of the operating system on which the executable runs, unless that18* component itself accompanies the executable.19*20* - Redistributions must reproduce the above copyright notice, this list of21* conditions and the following disclaimer in the documentation and/or other22* materials provided with the distribution.23*24* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"25* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE28* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR29* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF30* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN32* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)33* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE34* POSSIBILITY OF SUCH DAMAGE.35*36****************************************************************************************/3738#include "shared.h"3940struct41{42uint8 State;43uint8 Counter;44} activator[2];4546void activator_reset(int index)47{4849activator[index].State = 0x40;50activator[index].Counter = 0;51}5253INLINE unsigned char activator_read(int index)54{55/* IR sensors 1-16 data (active low) */56uint16 data = ~input.pad[index << 2];5758/* D1 = D0 (data is ready) */59uint8 temp = (activator[index].State & 0x01) << 1;6061switch (activator[index].Counter)62{63case 0: /* x x x x 0 1 0 0 */64temp |= 0x04;65break;6667case 1: /* x x l1 l2 l3 l4 1 1 */68temp |= ((data << 2) & 0x3C);69break;7071case 2: /* x x l5 l6 l7 l8 0 0 */72temp |= ((data >> 2) & 0x3C);73break;7475case 3: /* x x h1 h2 h3 h4 1 1 */76temp |= ((data >> 6) & 0x3C);77break;7879case 4: /* x x h5 h6 h7 h8 0 0 */80temp |= ((data >> 10) & 0x3C);81break;82}8384return temp;85}8687INLINE void activator_write(int index, unsigned char data, unsigned char mask)88{89/* update bits set as output only */90data = (activator[index].State & ~mask) | (data & mask);9192/* TH transitions */93if ((activator[index].State ^ data) & 0x40)94{95/* reset sequence cycle */96activator[index].Counter = 0;97}98else99{100/* D0 transitions */101if ((activator[index].State ^ data) & 0x01)102{103/* increment sequence cycle */104if (activator[index].Counter < 4)105{106activator[index].Counter++;107}108}109}110111/* update internal state */112activator[index].State = data;113}114115unsigned char activator_1_read(void)116{117return activator_read(0);118}119120unsigned char activator_2_read(void)121{122return activator_read(1);123}124125void activator_1_write(unsigned char data, unsigned char mask)126{127activator_write(0, data, mask);128}129130void activator_2_write(unsigned char data, unsigned char mask)131{132activator_write(1, data, mask);133}134135136