/***************************************************************************************1* Genesis Plus2* Sega Mouse support3*4* Copyright (C) 2007-2011 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;44uint8 Wait;45uint8 Port;46} mouse;4748void mouse_reset(int port)49{50input.analog[port][0] = 0;51input.analog[port][1] = 0;52mouse.State = 0x60;53mouse.Counter = 0;54mouse.Wait = 0;55mouse.Port = port;56}5758unsigned char mouse_read()59{60unsigned int temp = 0x00;61int x = input.analog[mouse.Port][0];62int y = input.analog[mouse.Port][1];6364switch (mouse.Counter)65{66case 0: /* initial */67temp = 0x00;68break;6970case 1: /* xxxx1011 */71temp = 0x0B;72break;7374case 2: /* xxxx1111 */75temp = 0x0F;76break;7778case 3: /* xxxx1111 */79temp = 0x0F;80break;8182case 4: /* Axis sign & overflow (not emulated) bits */83temp |= (x < 0);84temp |= (y < 0) << 1;85/*86temp |= (abs(x) > 255) << 2;87temp |= (abs(y) > 255) << 3;88*/89break;9091case 5: /* START, A, B, C buttons state (active high) */92temp = (input.pad[mouse.Port] >> 4) & 0x0F;93break;9495case 6: /* X Axis MSB */96temp = (x >> 4) & 0x0F;97break;9899case 7: /* X Axis LSB */100temp = (x & 0x0F);101break;102103case 8: /* Y Axis MSB */104temp = (y >> 4) & 0x0F;105break;106107case 9: /* Y Axis LSB */108temp = (y & 0x0F);109break;110}111112/* TL = busy status */113if (mouse.Wait)114{115/* wait before ACK, fix some buggy mouse routine (Cannon Fodder, Shangai 2, Wack World,...) */116mouse.Wait = 0;117118/* TL = !TR */119temp |= (~mouse.State & 0x20) >> 1;120}121else122{123/* TL = TR (data is ready) */124temp |= (mouse.State & 0x20) >> 1;125}126127return temp;128}129130void mouse_write(unsigned char data, unsigned char mask)131{132/* update bits set as output only */133data = (mouse.State & ~mask) | (data & mask);134135/* TH transition */136if ((mouse.State ^ data) & 0x40)137{138/* start (TH=0) or stop (TH=1) acquisition */139mouse.Counter = 1 - ((data & 0x40) >> 6);140}141142/* TR transition */143if ((mouse.State ^ data) & 0x20)144{145/* acquisition in progress */146if ((mouse.Counter > 0) && (mouse.Counter < 10))147{148/* increment phase */149mouse.Counter++;150}151152/* TL handshake latency */153mouse.Wait = 1;154}155156/* update internal state */157mouse.State = data;158}159160161