| 1 | /* Cygne |
| 2 | * |
| 3 | * Copyright notice for this file: |
| 4 | * Copyright (C) 2002 Dox [email protected] |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include <ctype.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "wswan.h" |
| 24 | #include "eeprom.h" |
| 25 | #include "wswan-memory.h" |
| 26 | #include "../state_inline.h" |
| 27 | |
| 28 | uint8_t wsEEPROM[2048]; |
| 29 | static uint8_t iEEPROM[0x400]; |
| 30 | static const uint8_t iEEPROM_Init[0x400] = |
| 31 | { |
| 32 | 255,255,255,255,255,255,192,255,0,0,0,0, |
| 33 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 34 | 0,0,127,0,255,255,255,255,255,255,255,255, |
| 35 | 255,255,255,255,255,255,0,0,0,0,0,0, |
| 36 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 37 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 38 | 0,0,0,0,0,0,255,255,255,255,255,255, |
| 39 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 40 | 0,252,255,1,255,253,255,253,255,253,255,253, |
| 41 | 255,253,255,253,255,255,255,255,255,255,255,255, |
| 42 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 43 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 44 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 45 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 46 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 47 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 48 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 49 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 50 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 51 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 52 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 53 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 54 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 55 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 56 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 57 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 58 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 59 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 60 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 61 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 62 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 63 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 64 | 0,0,3,3,0,0,0,64,128,0,0,0, |
| 65 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 66 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 67 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 68 | 135,5,140,9,5,12,139,12,144,0,0,2, |
| 69 | 0,76,165,0,128,0,0,0,255,127,255,127, |
| 70 | 255,127,255,127,255,127,255,127,255,127,255,127, |
| 71 | 255,127,255,127,255,127,255,127,255,127,255,127, |
| 72 | 255,127,255,127,255,127,255,127,255,127,255,127, |
| 73 | 255,127,255,127,255,127,255,127,255,127,255,127, |
| 74 | 255,127,255,127,255,127,255,127,255,255,255,255, |
| 75 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 76 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 77 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 78 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 79 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 80 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 81 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 82 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 83 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 84 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 85 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 86 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 87 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 88 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 89 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 90 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 91 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 92 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 93 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 94 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 95 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 96 | 0,0,6,6,6,6,6,0,0,0,0,0, |
| 97 | 1,128,15,0,1,1,1,15,0,0,0,0, |
| 98 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 99 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 100 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 101 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 102 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 103 | 0,0,0,0,0,0,0,0,0,0,0,0, |
| 104 | 'C'-54,'Y'-54,'G'-54,'N'-54,'E'-54,0,0,0,0,0,0,0,0,0,0, |
| 105 | 0,32,1,1,33,1,4,0,1, |
| 106 | 0,152,60,127,74,1,53,1,255,255,255,255, |
| 107 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 108 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 109 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 110 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 111 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 112 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 113 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 114 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 115 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 116 | 255,255,255,255,255,255,255,255,255,255,255,255, |
| 117 | 255,255,255,255 |
| 118 | }; |
| 119 | |
| 120 | static uint8_t iEEPROM_Command, EEPROM_Command; |
| 121 | static uint16_t iEEPROM_Address, EEPROM_Address; |
| 122 | |
| 123 | uint8_t WSwan_EEPROMRead(uint32_t A) |
| 124 | { |
| 125 | switch(A) |
| 126 | { |
| 127 | default: |
| 128 | break; |
| 129 | case 0xBA: |
| 130 | return(iEEPROM[(iEEPROM_Address << 1) & 0x3FF]); |
| 131 | case 0xBB: |
| 132 | return(iEEPROM[((iEEPROM_Address << 1) | 1) & 0x3FF]); |
| 133 | case 0xBC: |
| 134 | return(iEEPROM_Address >> 0); |
| 135 | case 0xBD: |
| 136 | return(iEEPROM_Address >> 8); |
| 137 | case 0xBE: |
| 138 | if(iEEPROM_Command & 0x20) |
| 139 | return iEEPROM_Command|2; |
| 140 | if(iEEPROM_Command & 0x10) |
| 141 | return iEEPROM_Command|1; |
| 142 | return iEEPROM_Command | 3; |
| 143 | case 0xC4: |
| 144 | return(wsEEPROM[(EEPROM_Address << 1) & (eeprom_size - 1)]); |
| 145 | case 0xC5: |
| 146 | return(wsEEPROM[((EEPROM_Address << 1) | 1) & (eeprom_size - 1)]); |
| 147 | case 0xC6: |
| 148 | return(EEPROM_Address >> 0); |
| 149 | case 0xC7: |
| 150 | return(EEPROM_Address >> 8); |
| 151 | case 0xC8: |
| 152 | if(EEPROM_Command & 0x20) |
| 153 | return EEPROM_Command|2; |
| 154 | if(EEPROM_Command & 0x10) |
| 155 | return EEPROM_Command|1; |
| 156 | return EEPROM_Command | 3; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void WSwan_EEPROMWrite(uint32_t A, uint8_t V) |
| 164 | { |
| 165 | switch(A) |
| 166 | { |
| 167 | case 0xBA: |
| 168 | iEEPROM[(iEEPROM_Address << 1) & 0x3FF] = V; |
| 169 | break; |
| 170 | case 0xBB: |
| 171 | iEEPROM[((iEEPROM_Address << 1) | 1) & 0x3FF] = V; |
| 172 | break; |
| 173 | case 0xBC: |
| 174 | iEEPROM_Address &= 0xFF00; iEEPROM_Address |= (V << 0); |
| 175 | break; |
| 176 | case 0xBD: |
| 177 | iEEPROM_Address &= 0x00FF; iEEPROM_Address |= (V << 8); |
| 178 | break; |
| 179 | case 0xBE: |
| 180 | iEEPROM_Command = V; |
| 181 | break; |
| 182 | case 0xC4: |
| 183 | wsEEPROM[(EEPROM_Address << 1) & (eeprom_size - 1)] = V; |
| 184 | break; |
| 185 | case 0xC5: |
| 186 | wsEEPROM[((EEPROM_Address << 1) | 1) & (eeprom_size - 1)] = V; |
| 187 | break; |
| 188 | case 0xC6: |
| 189 | EEPROM_Address &= 0xFF00; EEPROM_Address |= (V << 0); |
| 190 | break; |
| 191 | case 0xC7: |
| 192 | EEPROM_Address &= 0x00FF; EEPROM_Address |= (V << 8); |
| 193 | break; |
| 194 | case 0xC8: |
| 195 | EEPROM_Command = V; |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void WSwan_EEPROMReset(void) |
| 201 | { |
| 202 | iEEPROM_Command = EEPROM_Command = 0; |
| 203 | iEEPROM_Address = EEPROM_Address = 0; |
| 204 | } |
| 205 | |
| 206 | #define mBCD16(value) ( (((((value)%100) / 10) <<4)|((value)%10)) | ((((((value / 100)%100) / 10) <<4)|((value / 100)%10))<<8) ) |
| 207 | #define INT16_TO_BCD(A) ((((((A) % 100) / 10) * 16 + ((A) % 10))) | (((((((A) / 100) % 100) / 10) * 16 + (((A) / 100) % 10))) << 8)) |
| 208 | |
| 209 | void WSwan_EEPROMInit(const char *Name, const uint16_t BYear, const uint8_t BMonth, const uint8_t BDay, const uint8_t Sex, const uint8_t Blood) |
| 210 | { |
| 211 | unsigned x; |
| 212 | uint16_t bcd_BYear; |
| 213 | |
| 214 | memset(wsEEPROM, 0, 2048); |
| 215 | memcpy(iEEPROM, iEEPROM_Init, 0x400); |
| 216 | |
| 217 | for(x = 0; x < 16; x++) |
| 218 | { |
| 219 | uint8_t zechar = 0; |
| 220 | |
| 221 | if(x < strlen(Name)) |
| 222 | { |
| 223 | char tc = toupper(Name[x]); |
| 224 | if(tc == ' ') zechar = 0; |
| 225 | else if(tc >= '0' && tc <= '9') zechar = tc - '0' + 0x1; |
| 226 | else if(tc >= 'A' && tc <= 'Z') zechar = tc - 'A' + 0xB; |
| 227 | else if(tc >= 'a' && tc <= 'z') zechar = tc - 'a' + 0xB + 26; |
| 228 | } |
| 229 | iEEPROM[0x360 + x] = zechar; |
| 230 | } |
| 231 | |
| 232 | bcd_BYear = INT16_TO_BCD(BYear); |
| 233 | |
| 234 | iEEPROM[0x370] = (bcd_BYear >> 8) & 0xFF; |
| 235 | iEEPROM[0x371] = (bcd_BYear >> 0) & 0xFF; |
| 236 | iEEPROM[0x372] = mBCD(BMonth); |
| 237 | iEEPROM[0x373] = mBCD(BDay); |
| 238 | iEEPROM[0x374] = Sex; |
| 239 | iEEPROM[0x375] = Blood; |
| 240 | } |
| 241 | |
| 242 | int WSwan_EEPROMStateAction(StateMem *sm, int load, int data_only) |
| 243 | { |
| 244 | SFORMAT StateRegs[] = |
| 245 | { |
| 246 | SFVARN(iEEPROM_Command, "iEEPROM_Command"), |
| 247 | SFVARN(iEEPROM_Address, "iEEPROM_Address"), |
| 248 | SFVARN(EEPROM_Command, "EEPROM_Command"), |
| 249 | SFVARN(EEPROM_Address, "EEPROM_Address"), |
| 250 | SFARRAYN((iEEPROM), (sizeof(iEEPROM)), "iEEPROM"), |
| 251 | SFARRAYN(eeprom_size ? wsEEPROM : NULL, eeprom_size, "EEPROM"), |
| 252 | { 0, 0, 0, 0 } |
| 253 | }; |
| 254 | |
| 255 | if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "EEPR", false)) |
| 256 | return 0; |
| 257 | |
| 258 | return 1; |
| 259 | } |