| 1 | /* Mednafen - Multi-system Emulator |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | */ |
| 17 | |
| 18 | #include "vb.h" |
| 19 | #include "input.h" |
| 20 | |
| 21 | static bool InstantReadHack; |
| 22 | |
| 23 | static bool IntPending; |
| 24 | |
| 25 | static uint8 *data_ptr; |
| 26 | |
| 27 | static uint16 PadData; |
| 28 | static uint16 PadLatched; |
| 29 | |
| 30 | static uint8 SCR; |
| 31 | static uint16 SDR; |
| 32 | |
| 33 | #define SCR_S_ABT_DIS 0x01 |
| 34 | #define SCR_SI_STAT 0x02 |
| 35 | #define SCR_HW_SI 0x04 |
| 36 | #define SCR_SOFT_CLK 0x10 |
| 37 | |
| 38 | #define SCR_PARA_SI 0x20 |
| 39 | #define SCR_K_INT_INH 0x80 |
| 40 | |
| 41 | static uint32 ReadBitPos; |
| 42 | static int32 ReadCounter; |
| 43 | |
| 44 | static v810_timestamp_t last_ts; |
| 45 | |
| 46 | void VBINPUT_Init(void) |
| 47 | { |
| 48 | InstantReadHack = true; |
| 49 | } |
| 50 | |
| 51 | void VBINPUT_SetInstantReadHack(bool enabled) |
| 52 | { |
| 53 | InstantReadHack = enabled; |
| 54 | } |
| 55 | |
| 56 | void VBINPUT_SetInput(int port, const char *type, void *ptr) |
| 57 | { |
| 58 | data_ptr = (uint8 *)ptr; |
| 59 | } |
| 60 | |
| 61 | uint8 VBINPUT_Read(v810_timestamp_t ×tamp, uint32 A) |
| 62 | { |
| 63 | uint8_t ret = 0; |
| 64 | |
| 65 | VBINPUT_Update(timestamp); |
| 66 | |
| 67 | switch(A & 0xFF) |
| 68 | { |
| 69 | case 0x10: |
| 70 | if(InstantReadHack) |
| 71 | ret = PadData; |
| 72 | else |
| 73 | ret = SDR & 0xFF; |
| 74 | break; |
| 75 | case 0x14: |
| 76 | if(InstantReadHack) |
| 77 | ret = PadData >> 8; |
| 78 | else |
| 79 | ret = SDR >> 8; |
| 80 | break; |
| 81 | case 0x28: |
| 82 | ret = SCR | (0x40 | 0x08 | SCR_HW_SI); |
| 83 | if(ReadCounter > 0) |
| 84 | ret |= SCR_SI_STAT; |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | // printf("Input Read: %08x %02x\n", A, ret); |
| 89 | VB_SetEvent(VB_EVENT_INPUT, (ReadCounter > 0) ? (timestamp + ReadCounter) : VB_EVENT_NONONO); |
| 90 | |
| 91 | return(ret); |
| 92 | } |
| 93 | |
| 94 | void VBINPUT_Write(v810_timestamp_t ×tamp, uint32 A, uint8 V) |
| 95 | { |
| 96 | VBINPUT_Update(timestamp); |
| 97 | |
| 98 | //printf("Input write: %d, %08x %02x\n", timestamp, A, V); |
| 99 | switch(A & 0xFF) |
| 100 | { |
| 101 | case 0x28: |
| 102 | if((V & SCR_HW_SI) && !(SCR & SCR_S_ABT_DIS) && ReadCounter <= 0) |
| 103 | { |
| 104 | //printf("Start Read: %d\n", timestamp); |
| 105 | PadLatched = PadData; |
| 106 | ReadBitPos = 0; |
| 107 | ReadCounter = 640; |
| 108 | } |
| 109 | |
| 110 | if(V & SCR_S_ABT_DIS) |
| 111 | { |
| 112 | ReadCounter = 0; |
| 113 | ReadBitPos = 0; |
| 114 | } |
| 115 | |
| 116 | if(V & SCR_K_INT_INH) |
| 117 | { |
| 118 | IntPending = false; |
| 119 | VBIRQ_Assert(VBIRQ_SOURCE_INPUT, IntPending); |
| 120 | } |
| 121 | |
| 122 | SCR = V & (0x80 | 0x20 | 0x10 | 1); |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | VB_SetEvent(VB_EVENT_INPUT, (ReadCounter > 0) ? (timestamp + ReadCounter) : VB_EVENT_NONONO); |
| 127 | } |
| 128 | |
| 129 | void VBINPUT_Frame(void) |
| 130 | { |
| 131 | PadData = (MDFN_de16lsb(data_ptr) << 2) | 0x2; |
| 132 | } |
| 133 | |
| 134 | v810_timestamp_t VBINPUT_Update(const v810_timestamp_t timestamp) |
| 135 | { |
| 136 | int32 clocks = timestamp - last_ts; |
| 137 | |
| 138 | if(ReadCounter > 0) |
| 139 | { |
| 140 | ReadCounter -= clocks; |
| 141 | |
| 142 | while(ReadCounter <= 0) |
| 143 | { |
| 144 | SDR &= ~(1 << ReadBitPos); |
| 145 | SDR |= PadLatched & (1 << ReadBitPos); |
| 146 | |
| 147 | ReadBitPos++; |
| 148 | if(ReadBitPos < 16) |
| 149 | ReadCounter += 640; |
| 150 | else |
| 151 | { |
| 152 | //printf("Read End: %d\n", timestamp); |
| 153 | if(!(SCR & SCR_K_INT_INH)) |
| 154 | { |
| 155 | //printf("Input IRQ: %d\n", timestamp); |
| 156 | IntPending = true; |
| 157 | VBIRQ_Assert(VBIRQ_SOURCE_INPUT, IntPending); |
| 158 | } |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | } |
| 164 | |
| 165 | |
| 166 | last_ts = timestamp; |
| 167 | |
| 168 | return((ReadCounter > 0) ? (timestamp + ReadCounter) : VB_EVENT_NONONO); |
| 169 | } |
| 170 | |
| 171 | void VBINPUT_ResetTS(void) |
| 172 | { |
| 173 | last_ts = 0; |
| 174 | } |
| 175 | |
| 176 | void VBINPUT_Power(void) |
| 177 | { |
| 178 | last_ts = 0; |
| 179 | PadData = 0; |
| 180 | PadLatched = 0; |
| 181 | SDR = 0; |
| 182 | SCR = 0; |
| 183 | ReadBitPos = 0; |
| 184 | ReadCounter = 0; |
| 185 | IntPending = false; |
| 186 | |
| 187 | VBIRQ_Assert(VBIRQ_SOURCE_INPUT, 0); |
| 188 | } |
| 189 | |
| 190 | int VBINPUT_StateAction(StateMem *sm, int load, int data_only) |
| 191 | { |
| 192 | SFORMAT StateRegs[] = |
| 193 | { |
| 194 | SFVAR(PadData), |
| 195 | SFVAR(PadLatched), |
| 196 | SFVAR(SCR), |
| 197 | SFVAR(SDR), |
| 198 | SFVAR(ReadBitPos), |
| 199 | SFVAR(ReadCounter), |
| 200 | SFVAR(IntPending), |
| 201 | SFEND |
| 202 | }; |
| 203 | |
| 204 | int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "INPUT"); |
| 205 | |
| 206 | if(load) |
| 207 | { |
| 208 | |
| 209 | } |
| 210 | |
| 211 | return(ret); |
| 212 | } |