| 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 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "../mednafen-types.h" |
| 24 | #include "wswan-memory.h" |
| 25 | |
| 26 | static uint8 tiles[256][256][2][8]; |
| 27 | uint8 wsTCache[512*64]; |
| 28 | uint8 wsTCache2[512*64]; |
| 29 | uint8 wsTCacheFlipped[512*64]; |
| 30 | uint8 wsTCacheFlipped2[512*64]; |
| 31 | uint8 wsTCacheUpdate[512]; |
| 32 | uint8 wsTCacheUpdate2[512]; |
| 33 | uint8 wsTileRow[8]; |
| 34 | int wsVMode; |
| 35 | |
| 36 | void WSWan_TCacheInvalidByAddr(uint32 ws_offset) |
| 37 | { |
| 38 | if(wsVMode && (ws_offset>=0x4000)&&(ws_offset<0x8000)) |
| 39 | { |
| 40 | wsTCacheUpdate[(ws_offset-0x4000)>>5]=false; /*invalidate tile*/ |
| 41 | return; |
| 42 | } |
| 43 | else if((ws_offset>=0x2000)&&(ws_offset<0x4000)) |
| 44 | { |
| 45 | wsTCacheUpdate[(ws_offset-0x2000)>>4]=false; /*invalidate tile*/ |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if(wsVMode && (ws_offset>=0x8000)&&(ws_offset<0xc000)) |
| 50 | { |
| 51 | wsTCacheUpdate2[(ws_offset-0x8000)>>5]=false; /*invalidate tile*/ |
| 52 | return; |
| 53 | } |
| 54 | else if((ws_offset>=0x4000)&&(ws_offset<0x6000)) |
| 55 | { |
| 56 | wsTCacheUpdate2[(ws_offset-0x4000)>>4]=false; /*invalidate tile*/ |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void wsSetVideo(int number,bool force) |
| 62 | { |
| 63 | if((number!=wsVMode)||(force)) |
| 64 | { |
| 65 | wsVMode=number; |
| 66 | memset(wsTCacheUpdate,0,512); |
| 67 | memset(wsTCacheUpdate2,0,512); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void wsMakeTiles(void) |
| 72 | { |
| 73 | int x,y,b0,b1,b2,b3,b4,b5,b6,b7; |
| 74 | for(x=0;x<256;x++) |
| 75 | for(y=0;y<256;y++) |
| 76 | { |
| 77 | b0=(x&128)>>7;b1=(x&64)>>6;b2=(x&32)>>5;b3=(x&16)>>4;b4=(x&8)>>3;b5=(x&4)>>2;b6=(x&2)>>1;b7=(x&1); |
| 78 | b0|=(y&128)>>6;b1|=(y&64)>>5;b2|=(y&32)>>4;b3|=(y&16)>>3;b4|=(y&8)>>2;b5|=(y&4)>>1;b6|=(y&2);b7|=(y&1)<<1; |
| 79 | tiles[x][y][0][0]=b0; |
| 80 | tiles[x][y][0][1]=b1; |
| 81 | tiles[x][y][0][2]=b2; |
| 82 | tiles[x][y][0][3]=b3; |
| 83 | tiles[x][y][0][4]=b4; |
| 84 | tiles[x][y][0][5]=b5; |
| 85 | tiles[x][y][0][6]=b6; |
| 86 | tiles[x][y][0][7]=b7; |
| 87 | tiles[x][y][1][0]=b7; |
| 88 | tiles[x][y][1][1]=b6; |
| 89 | tiles[x][y][1][2]=b5; |
| 90 | tiles[x][y][1][3]=b4; |
| 91 | tiles[x][y][1][4]=b3; |
| 92 | tiles[x][y][1][5]=b2; |
| 93 | tiles[x][y][1][6]=b1; |
| 94 | tiles[x][y][1][7]=b0; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void wsGetTile(uint32 number,uint32 line,int flipv,int fliph,int bank) |
| 99 | { |
| 100 | uint32 t_adr,t_index,i; |
| 101 | uint8 byte0,byte1,byte2,byte3; |
| 102 | |
| 103 | if((!bank)||(!(wsVMode &0x07))) |
| 104 | { |
| 105 | #ifdef TCACHE_OFF |
| 106 | wsTCacheUpdate[number]=false; |
| 107 | #endif |
| 108 | |
| 109 | if(!wsTCacheUpdate[number]) |
| 110 | { |
| 111 | wsTCacheUpdate[number]=true; |
| 112 | switch(wsVMode) |
| 113 | { |
| 114 | case 7: |
| 115 | t_adr=0x4000+(number<<5); |
| 116 | t_index=number<<6; |
| 117 | for(i=0;i<8;i++) |
| 118 | { |
| 119 | byte0=wsRAM[t_adr++]; |
| 120 | byte1=wsRAM[t_adr++]; |
| 121 | byte2=wsRAM[t_adr++]; |
| 122 | byte3=wsRAM[t_adr++]; |
| 123 | wsTCache[t_index]=byte0>>4; |
| 124 | wsTCacheFlipped[t_index++]=byte3&15; |
| 125 | wsTCache[t_index]=byte0&15; |
| 126 | wsTCacheFlipped[t_index++]=byte3>>4; |
| 127 | wsTCache[t_index]=byte1>>4; |
| 128 | wsTCacheFlipped[t_index++]=byte2&15; |
| 129 | wsTCache[t_index]=byte1&15; |
| 130 | wsTCacheFlipped[t_index++]=byte2>>4; |
| 131 | wsTCache[t_index]=byte2>>4; |
| 132 | wsTCacheFlipped[t_index++]=byte1&15; |
| 133 | wsTCache[t_index]=byte2&15; |
| 134 | wsTCacheFlipped[t_index++]=byte1>>4; |
| 135 | wsTCache[t_index]=byte3>>4; |
| 136 | wsTCacheFlipped[t_index++]=byte0&15; |
| 137 | wsTCache[t_index]=byte3&15; |
| 138 | wsTCacheFlipped[t_index++]=byte0>>4; |
| 139 | } |
| 140 | break; |
| 141 | |
| 142 | case 6: |
| 143 | t_adr=0x4000+(number<<5); |
| 144 | t_index=number<<6; |
| 145 | for(i=0;i<8;i++) |
| 146 | { |
| 147 | byte0=wsRAM[t_adr++]; |
| 148 | byte1=wsRAM[t_adr++]; |
| 149 | byte2=wsRAM[t_adr++]; |
| 150 | byte3=wsRAM[t_adr++]; |
| 151 | wsTCache[t_index]=((byte0>>7)&1)|(((byte1>>7)&1)<<1)|(((byte2>>7)&1)<<2)|(((byte3>>7)&1)<<3); |
| 152 | wsTCacheFlipped[t_index++]=((byte0)&1)|(((byte1)&1)<<1)|(((byte2)&1)<<2)|(((byte3)&1)<<3); |
| 153 | wsTCache[t_index]=((byte0>>6)&1)|(((byte1>>6)&1)<<1)|(((byte2>>6)&1)<<2)|(((byte3>>6)&1)<<3); |
| 154 | wsTCacheFlipped[t_index++]=((byte0>>1)&1)|(((byte1>>1)&1)<<1)|(((byte2>>1)&1)<<2)|(((byte3>>1)&1)<<3); |
| 155 | wsTCache[t_index]=((byte0>>5)&1)|(((byte1>>5)&1)<<1)|(((byte2>>5)&1)<<2)|(((byte3>>5)&1)<<3); |
| 156 | wsTCacheFlipped[t_index++]=((byte0>>2)&1)|(((byte1>>2)&1)<<1)|(((byte2>>2)&1)<<2)|(((byte3>>2)&1)<<3); |
| 157 | wsTCache[t_index]=((byte0>>4)&1)|(((byte1>>4)&1)<<1)|(((byte2>>4)&1)<<2)|(((byte3>>4)&1)<<3); |
| 158 | wsTCacheFlipped[t_index++]=((byte0>>3)&1)|(((byte1>>3)&1)<<1)|(((byte2>>3)&1)<<2)|(((byte3>>3)&1)<<3); |
| 159 | wsTCache[t_index]=((byte0>>3)&1)|(((byte1>>3)&1)<<1)|(((byte2>>3)&1)<<2)|(((byte3>>3)&1)<<3); |
| 160 | wsTCacheFlipped[t_index++]=((byte0>>4)&1)|(((byte1>>4)&1)<<1)|(((byte2>>4)&1)<<2)|(((byte3>>4)&1)<<3); |
| 161 | wsTCache[t_index]=((byte0>>2)&1)|(((byte1>>2)&1)<<1)|(((byte2>>2)&1)<<2)|(((byte3>>2)&1)<<3); |
| 162 | wsTCacheFlipped[t_index++]=((byte0>>5)&1)|(((byte1>>5)&1)<<1)|(((byte2>>5)&1)<<2)|(((byte3>>5)&1)<<3); |
| 163 | wsTCache[t_index]=((byte0>>1)&1)|(((byte1>>1)&1)<<1)|(((byte2>>1)&1)<<2)|(((byte3>>1)&1)<<3); |
| 164 | wsTCacheFlipped[t_index++]=((byte0>>6)&1)|(((byte1>>6)&1)<<1)|(((byte2>>6)&1)<<2)|(((byte3>>6)&1)<<3); |
| 165 | wsTCache[t_index]=((byte0)&1)|(((byte1)&1)<<1)|(((byte2)&1)<<2)|(((byte3)&1)<<3); |
| 166 | wsTCacheFlipped[t_index++]=((byte0>>7)&1)|(((byte1>>7)&1)<<1)|(((byte2>>7)&1)<<2)|(((byte3>>7)&1)<<3); |
| 167 | } |
| 168 | break; |
| 169 | |
| 170 | default: |
| 171 | t_adr=0x2000+(number<<4); |
| 172 | t_index=number<<6; |
| 173 | for(i=0;i<8;i++) |
| 174 | { |
| 175 | byte0=wsRAM[t_adr++]; |
| 176 | byte1=wsRAM[t_adr++]; |
| 177 | wsTCache[t_index]=tiles[byte0][byte1][0][0]; |
| 178 | wsTCacheFlipped[t_index++]=tiles[byte0][byte1][1][0]; |
| 179 | wsTCache[t_index]=tiles[byte0][byte1][0][1]; |
| 180 | wsTCacheFlipped[t_index++]=tiles[byte0][byte1][1][1]; |
| 181 | wsTCache[t_index]=tiles[byte0][byte1][0][2]; |
| 182 | wsTCacheFlipped[t_index++]=tiles[byte0][byte1][1][2]; |
| 183 | wsTCache[t_index]=tiles[byte0][byte1][0][3]; |
| 184 | wsTCacheFlipped[t_index++]=tiles[byte0][byte1][1][3]; |
| 185 | wsTCache[t_index]=tiles[byte0][byte1][0][4]; |
| 186 | wsTCacheFlipped[t_index++]=tiles[byte0][byte1][1][4]; |
| 187 | wsTCache[t_index]=tiles[byte0][byte1][0][5]; |
| 188 | wsTCacheFlipped[t_index++]=tiles[byte0][byte1][1][5]; |
| 189 | wsTCache[t_index]=tiles[byte0][byte1][0][6]; |
| 190 | wsTCacheFlipped[t_index++]=tiles[byte0][byte1][1][6]; |
| 191 | wsTCache[t_index]=tiles[byte0][byte1][0][7]; |
| 192 | wsTCacheFlipped[t_index++]=tiles[byte0][byte1][1][7]; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | if(flipv) |
| 197 | line=7-line; |
| 198 | if(fliph) |
| 199 | memcpy(&wsTileRow[0],&wsTCacheFlipped[(number<<6)|(line<<3)],8); |
| 200 | else |
| 201 | memcpy(&wsTileRow[0],&wsTCache[(number<<6)|(line<<3)],8); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | |
| 206 | #ifdef TCACHE_OFF |
| 207 | wsTCacheUpdate2[number]=false; |
| 208 | #endif |
| 209 | |
| 210 | if(!wsTCacheUpdate2[number]) |
| 211 | { |
| 212 | wsTCacheUpdate2[number]=true; |
| 213 | switch(wsVMode) |
| 214 | { |
| 215 | case 7: |
| 216 | t_adr=0x8000+(number<<5); |
| 217 | t_index=number<<6; |
| 218 | for(i=0;i<8;i++) |
| 219 | { |
| 220 | byte0=wsRAM[t_adr++]; |
| 221 | byte1=wsRAM[t_adr++]; |
| 222 | byte2=wsRAM[t_adr++]; |
| 223 | byte3=wsRAM[t_adr++]; |
| 224 | wsTCache2[t_index]=byte0>>4; |
| 225 | wsTCacheFlipped2[t_index++]=byte3&15; |
| 226 | wsTCache2[t_index]=byte0&15; |
| 227 | wsTCacheFlipped2[t_index++]=byte3>>4; |
| 228 | wsTCache2[t_index]=byte1>>4; |
| 229 | wsTCacheFlipped2[t_index++]=byte2&15; |
| 230 | wsTCache2[t_index]=byte1&15; |
| 231 | wsTCacheFlipped2[t_index++]=byte2>>4; |
| 232 | wsTCache2[t_index]=byte2>>4; |
| 233 | wsTCacheFlipped2[t_index++]=byte1&15; |
| 234 | wsTCache2[t_index]=byte2&15; |
| 235 | wsTCacheFlipped2[t_index++]=byte1>>4; |
| 236 | wsTCache2[t_index]=byte3>>4; |
| 237 | wsTCacheFlipped2[t_index++]=byte0&15; |
| 238 | wsTCache2[t_index]=byte3&15; |
| 239 | wsTCacheFlipped2[t_index++]=byte0>>4; |
| 240 | } |
| 241 | break; |
| 242 | case 6: |
| 243 | t_adr=0x8000+(number<<5); |
| 244 | t_index=number<<6; |
| 245 | for(i=0;i<8;i++) |
| 246 | { |
| 247 | byte0=wsRAM[t_adr++]; |
| 248 | byte1=wsRAM[t_adr++]; |
| 249 | byte2=wsRAM[t_adr++]; |
| 250 | byte3=wsRAM[t_adr++]; |
| 251 | wsTCache2[t_index]=((byte0>>7)&1)|(((byte1>>7)&1)<<1)|(((byte2>>7)&1)<<2)|(((byte3>>7)&1)<<3); |
| 252 | wsTCacheFlipped2[t_index++]=((byte0)&1)|(((byte1)&1)<<1)|(((byte2)&1)<<2)|(((byte3)&1)<<3); |
| 253 | wsTCache2[t_index]=((byte0>>6)&1)|(((byte1>>6)&1)<<1)|(((byte2>>6)&1)<<2)|(((byte3>>6)&1)<<3); |
| 254 | wsTCacheFlipped2[t_index++]=((byte0>>1)&1)|(((byte1>>1)&1)<<1)|(((byte2>>1)&1)<<2)|(((byte3>>1)&1)<<3); |
| 255 | wsTCache2[t_index]=((byte0>>5)&1)|(((byte1>>5)&1)<<1)|(((byte2>>5)&1)<<2)|(((byte3>>5)&1)<<3); |
| 256 | wsTCacheFlipped2[t_index++]=((byte0>>2)&1)|(((byte1>>2)&1)<<1)|(((byte2>>2)&1)<<2)|(((byte3>>2)&1)<<3); |
| 257 | wsTCache2[t_index]=((byte0>>4)&1)|(((byte1>>4)&1)<<1)|(((byte2>>4)&1)<<2)|(((byte3>>4)&1)<<3); |
| 258 | wsTCacheFlipped2[t_index++]=((byte0>>3)&1)|(((byte1>>3)&1)<<1)|(((byte2>>3)&1)<<2)|(((byte3>>3)&1)<<3); |
| 259 | wsTCache2[t_index]=((byte0>>3)&1)|(((byte1>>3)&1)<<1)|(((byte2>>3)&1)<<2)|(((byte3>>3)&1)<<3); |
| 260 | wsTCacheFlipped2[t_index++]=((byte0>>4)&1)|(((byte1>>4)&1)<<1)|(((byte2>>4)&1)<<2)|(((byte3>>4)&1)<<3); |
| 261 | wsTCache2[t_index]=((byte0>>2)&1)|(((byte1>>2)&1)<<1)|(((byte2>>2)&1)<<2)|(((byte3>>2)&1)<<3); |
| 262 | wsTCacheFlipped2[t_index++]=((byte0>>5)&1)|(((byte1>>5)&1)<<1)|(((byte2>>5)&1)<<2)|(((byte3>>5)&1)<<3); |
| 263 | wsTCache2[t_index]=((byte0>>1)&1)|(((byte1>>1)&1)<<1)|(((byte2>>1)&1)<<2)|(((byte3>>1)&1)<<3); |
| 264 | wsTCacheFlipped2[t_index++]=((byte0>>6)&1)|(((byte1>>6)&1)<<1)|(((byte2>>6)&1)<<2)|(((byte3>>6)&1)<<3); |
| 265 | wsTCache2[t_index]=((byte0)&1)|(((byte1)&1)<<1)|(((byte2)&1)<<2)|(((byte3)&1)<<3); |
| 266 | wsTCacheFlipped2[t_index++]=((byte0>>7)&1)|(((byte1>>7)&1)<<1)|(((byte2>>7)&1)<<2)|(((byte3>>7)&1)<<3); |
| 267 | } |
| 268 | break; |
| 269 | default: |
| 270 | t_adr=0x4000+(number<<4); |
| 271 | t_index=number<<6; |
| 272 | for(i=0;i<8;i++) |
| 273 | { |
| 274 | byte0=wsRAM[t_adr++]; |
| 275 | byte1=wsRAM[t_adr++]; |
| 276 | wsTCache2[t_index]=tiles[byte0][byte1][0][0]; |
| 277 | wsTCacheFlipped2[t_index++]=tiles[byte0][byte1][1][0]; |
| 278 | wsTCache2[t_index]=tiles[byte0][byte1][0][1]; |
| 279 | wsTCacheFlipped2[t_index++]=tiles[byte0][byte1][1][1]; |
| 280 | wsTCache2[t_index]=tiles[byte0][byte1][0][2]; |
| 281 | wsTCacheFlipped2[t_index++]=tiles[byte0][byte1][1][2]; |
| 282 | wsTCache2[t_index]=tiles[byte0][byte1][0][3]; |
| 283 | wsTCacheFlipped2[t_index++]=tiles[byte0][byte1][1][3]; |
| 284 | wsTCache2[t_index]=tiles[byte0][byte1][0][4]; |
| 285 | wsTCacheFlipped2[t_index++]=tiles[byte0][byte1][1][4]; |
| 286 | wsTCache2[t_index]=tiles[byte0][byte1][0][5]; |
| 287 | wsTCacheFlipped2[t_index++]=tiles[byte0][byte1][1][5]; |
| 288 | wsTCache2[t_index]=tiles[byte0][byte1][0][6]; |
| 289 | wsTCacheFlipped2[t_index++]=tiles[byte0][byte1][1][6]; |
| 290 | wsTCache2[t_index]=tiles[byte0][byte1][0][7]; |
| 291 | wsTCacheFlipped2[t_index++]=tiles[byte0][byte1][1][7]; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | if(flipv) |
| 296 | line=7-line; |
| 297 | if(fliph) |
| 298 | memcpy(&wsTileRow[0],&wsTCacheFlipped2[(number<<6)|(line<<3)],8); |
| 299 | else |
| 300 | memcpy(&wsTileRow[0],&wsTCache2[(number<<6)|(line<<3)],8); |
| 301 | } |
| 302 | } |