| 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 "wswan.h" |
| 24 | #include "gfx.h" |
| 25 | #include "interrupt.h" |
| 26 | #include "wswan-memory.h" |
| 27 | #include "v30mz.h" |
| 28 | #include "rtc.h" |
| 29 | #include "../video.h" |
| 30 | #include "../state_inline.h" |
| 31 | |
| 32 | static uint32 wsMonoPal[16][4]; |
| 33 | static uint32 wsColors[8]; |
| 34 | static uint32 wsCols[16][16]; |
| 35 | |
| 36 | static uint32 ColorMapG[16]; |
| 37 | static uint32 ColorMap[4096]; |
| 38 | static uint32 LayerEnabled; |
| 39 | |
| 40 | static uint8 wsLine; /*current scanline*/ |
| 41 | |
| 42 | static uint8 SpriteTable[2][0x80][4]; |
| 43 | static uint32 SpriteCountCache[2]; |
| 44 | static bool FrameWhichActive; |
| 45 | static uint8 DispControl; |
| 46 | static uint8 BGColor; |
| 47 | static uint8 LineCompare; |
| 48 | static uint8 SPRBase; |
| 49 | static uint8 SpriteStart, SpriteCount; |
| 50 | static uint8 FGBGLoc; |
| 51 | static uint8 FGx0, FGy0, FGx1, FGy1; |
| 52 | static uint8 SPRx0, SPRy0, SPRx1, SPRy1; |
| 53 | |
| 54 | static uint8 BGXScroll, BGYScroll; |
| 55 | static uint8 FGXScroll, FGYScroll; |
| 56 | static uint8 LCDControl, LCDIcons; |
| 57 | static uint8 LCDVtotal; |
| 58 | |
| 59 | static uint8 BTimerControl; |
| 60 | static uint16 HBTimerPeriod; |
| 61 | static uint16 VBTimerPeriod; |
| 62 | |
| 63 | static uint16 HBCounter, VBCounter; |
| 64 | static uint8 VideoMode; |
| 65 | |
| 66 | void WSwan_GfxInit(void) |
| 67 | { |
| 68 | LayerEnabled = 7; /* BG, FG, sprites */ |
| 69 | } |
| 70 | |
| 71 | void WSwan_GfxWSCPaletteRAMWrite(uint32 ws_offset, uint8 data) |
| 72 | { |
| 73 | ws_offset=(ws_offset&0xfffe)-0xfe00; |
| 74 | wsCols[(ws_offset>>1)>>4][(ws_offset>>1)&15] = wsRAM[ws_offset+0xfe00] | ((wsRAM[ws_offset+0xfe01]&0x0f) << 8); |
| 75 | } |
| 76 | |
| 77 | void WSwan_GfxWrite(uint32 A, uint8 V) |
| 78 | { |
| 79 | if(A >= 0x1C && A <= 0x1F) |
| 80 | { |
| 81 | wsColors[(A - 0x1C) * 2 + 0] = 0xF - (V & 0xf); |
| 82 | wsColors[(A - 0x1C) * 2 + 1] = 0xF - (V >> 4); |
| 83 | } |
| 84 | else if(A >= 0x20 && A <= 0x3F) |
| 85 | { |
| 86 | wsMonoPal[(A - 0x20) >> 1][((A & 0x1) << 1) + 0] = V&7; |
| 87 | wsMonoPal[(A - 0x20) >> 1][((A & 0x1) << 1) | 1] = (V>>4)&7; |
| 88 | } |
| 89 | else switch(A) |
| 90 | { |
| 91 | case 0x00: DispControl = V; break; |
| 92 | case 0x01: BGColor = V; break; |
| 93 | case 0x03: LineCompare = V; break; |
| 94 | case 0x04: SPRBase = V & 0x3F; break; |
| 95 | case 0x05: SpriteStart = V; break; |
| 96 | case 0x06: SpriteCount = V; break; |
| 97 | case 0x07: FGBGLoc = V; break; |
| 98 | case 0x08: FGx0 = V; break; |
| 99 | case 0x09: FGy0 = V; break; |
| 100 | case 0x0A: FGx1 = V; break; |
| 101 | case 0x0B: FGy1 = V; break; |
| 102 | case 0x0C: SPRx0 = V; break; |
| 103 | case 0x0D: SPRy0 = V; break; |
| 104 | case 0x0E: SPRx1 = V; break; |
| 105 | case 0x0F: SPRy1 = V; break; |
| 106 | case 0x10: BGXScroll = V; break; |
| 107 | case 0x11: BGYScroll = V; break; |
| 108 | case 0x12: FGXScroll = V; break; |
| 109 | case 0x13: FGYScroll = V; break; |
| 110 | |
| 111 | case 0x14: LCDControl = V; break; |
| 112 | case 0x15: LCDIcons = V; break; |
| 113 | case 0x16: LCDVtotal = V; break; |
| 114 | case 0x60: VideoMode = V; wsSetVideo(V>>5, false); break; |
| 115 | |
| 116 | case 0xa2: BTimerControl = V; break; |
| 117 | case 0xa4: HBTimerPeriod &= 0xFF00; HBTimerPeriod |= (V << 0); break; |
| 118 | case 0xa5: HBTimerPeriod &= 0x00FF; HBTimerPeriod |= (V << 8); HBCounter = HBTimerPeriod; break; |
| 119 | case 0xa6: VBTimerPeriod &= 0xFF00; VBTimerPeriod |= (V << 0); break; |
| 120 | case 0xa7: VBTimerPeriod &= 0x00FF; VBTimerPeriod |= (V << 8); VBCounter = VBTimerPeriod; break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | uint8 WSwan_GfxRead(uint32 A) |
| 125 | { |
| 126 | if(A >= 0x1C && A <= 0x1F) |
| 127 | { |
| 128 | uint8 ret = 0; |
| 129 | |
| 130 | ret |= 0xF - wsColors[(A - 0x1C) * 2 + 0]; |
| 131 | ret |= (0xF - wsColors[(A - 0x1C) * 2 + 1]) << 4; |
| 132 | |
| 133 | return(ret); |
| 134 | } |
| 135 | else if(A >= 0x20 && A <= 0x3F) |
| 136 | return wsMonoPal[(A - 0x20) >> 1][((A & 0x1) << 1) + 0] | (wsMonoPal[(A - 0x20) >> 1][((A & 0x1) << 1) | 1] << 4); |
| 137 | else switch(A) |
| 138 | { |
| 139 | case 0x00: return(DispControl); |
| 140 | case 0x01: return(BGColor); |
| 141 | case 0x02: return(wsLine); |
| 142 | case 0x03: return(LineCompare); |
| 143 | case 0x04: return(SPRBase); |
| 144 | case 0x05: return(SpriteStart); |
| 145 | case 0x06: return(SpriteCount); |
| 146 | case 0x07: return(FGBGLoc); |
| 147 | case 0x08: return(FGx0); |
| 148 | case 0x09: return(FGy0); |
| 149 | case 0x0A: return(FGx1); |
| 150 | case 0x0B: return(FGy1); |
| 151 | case 0x0C: return(SPRx0); |
| 152 | case 0x0D: return(SPRy0); |
| 153 | case 0x0E: return(SPRx1); |
| 154 | case 0x0F: return(SPRy1); |
| 155 | case 0x10: return(BGXScroll); |
| 156 | case 0x11: return(BGYScroll); |
| 157 | case 0x12: return(FGXScroll); |
| 158 | case 0x13: return(FGYScroll); |
| 159 | case 0x14: return(LCDControl); |
| 160 | case 0x15: return(LCDIcons); |
| 161 | case 0x16: return(LCDVtotal); |
| 162 | case 0x60: return(VideoMode); |
| 163 | case 0xa0: return(wsc ? 0x87 : 0x86); |
| 164 | case 0xa2: return(BTimerControl); |
| 165 | case 0xa4: return((HBTimerPeriod >> 0) & 0xFF); |
| 166 | case 0xa5: return((HBTimerPeriod >> 8) & 0xFF); |
| 167 | case 0xa6: return((VBTimerPeriod >> 0) & 0xFF); |
| 168 | case 0xa7: return((VBTimerPeriod >> 8) & 0xFF); |
| 169 | case 0xa8: return((HBCounter >> 0) & 0xFF); |
| 170 | case 0xa9: return((HBCounter >> 8) & 0xFF); |
| 171 | case 0xaa: return((VBCounter >> 0) & 0xFF); |
| 172 | case 0xab: return((VBCounter >> 8) & 0xFF); |
| 173 | default: break; |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | bool wsExecuteLine(MDFN_Surface *surface, bool skip) |
| 180 | { |
| 181 | bool ret = false; |
| 182 | |
| 183 | if(wsLine < 144) |
| 184 | { |
| 185 | if(!skip) |
| 186 | { |
| 187 | switch(surface->depth) |
| 188 | { |
| 189 | case 15: |
| 190 | case 16: |
| 191 | wsScanline(surface->pixels + wsLine * surface->pitch, surface->depth); |
| 192 | break; |
| 193 | |
| 194 | case 24: |
| 195 | wsScanline(surface->pixels + wsLine * surface->pitch * 2, surface->depth); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | WSwan_CheckSoundDMA(); |
| 202 | |
| 203 | /* Update sprite data table |
| 204 | * Note: it's at 142 actually |
| 205 | * but it doesn't "update" until next frame */ |
| 206 | if(wsLine == 142) |
| 207 | { |
| 208 | SpriteCountCache[!FrameWhichActive] = SpriteCount; |
| 209 | |
| 210 | if(SpriteCountCache[!FrameWhichActive] > 0x80) |
| 211 | SpriteCountCache[!FrameWhichActive] = 0x80; |
| 212 | |
| 213 | memcpy(SpriteTable[!FrameWhichActive], &wsRAM[(SPRBase << 9) + (SpriteStart << 2)], SpriteCountCache[!FrameWhichActive] << 2); |
| 214 | } |
| 215 | |
| 216 | else if(wsLine == 144) |
| 217 | { |
| 218 | FrameWhichActive = !FrameWhichActive; |
| 219 | ret = true; |
| 220 | WSwan_Interrupt(WSINT_VBLANK); |
| 221 | |
| 222 | if(VBCounter && (BTimerControl & 0x04)) |
| 223 | { |
| 224 | VBCounter--; |
| 225 | if(!VBCounter) |
| 226 | { |
| 227 | if(BTimerControl & 0x08) /* loop */ |
| 228 | VBCounter = VBTimerPeriod; |
| 229 | WSwan_Interrupt(WSINT_VBLANK_TIMER); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | if(HBCounter && (BTimerControl & 0x01)) |
| 236 | { |
| 237 | HBCounter--; |
| 238 | if(!HBCounter) |
| 239 | { |
| 240 | /* Loop mode? */ |
| 241 | if(BTimerControl & 0x02) |
| 242 | HBCounter = HBTimerPeriod; |
| 243 | WSwan_Interrupt(WSINT_HBLANK_TIMER); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | v30mz_execute(128); |
| 248 | WSwan_CheckSoundDMA(); |
| 249 | |
| 250 | |
| 251 | #define MAX(a,b) ((a >= b) ? a : b) |
| 252 | |
| 253 | |
| 254 | v30mz_execute(96); |
| 255 | wsLine = (wsLine + 1) % (MAX(144, LCDVtotal) + 1); |
| 256 | if(wsLine == LineCompare) |
| 257 | WSwan_Interrupt(WSINT_LINE_HIT); |
| 258 | |
| 259 | v30mz_execute(32); |
| 260 | WSwan_RTCClock(256); |
| 261 | |
| 262 | return(ret); |
| 263 | } |
| 264 | |
| 265 | void WSwan_SetLayerEnableMask(uint64 mask) |
| 266 | { |
| 267 | LayerEnabled = mask; |
| 268 | } |
| 269 | |
| 270 | #define LERP_MONO_PALETTE_COLOR(start, end, offset) (uint32)(((float)(((15 - offset) * start) + (offset * end)) / 15.0f) + 0.5f) |
| 271 | |
| 272 | void WSwan_SetMonoPalette(int depth, uint32 mono_start, uint32 mono_end) |
| 273 | { |
| 274 | unsigned i; |
| 275 | |
| 276 | uint32 r_start = (mono_start >> 16) & 0xFF; |
| 277 | uint32 g_start = (mono_start >> 8) & 0xFF; |
| 278 | uint32 b_start = (mono_start ) & 0xFF; |
| 279 | |
| 280 | uint32 r_end = (mono_end >> 16) & 0xFF; |
| 281 | uint32 g_end = (mono_end >> 8) & 0xFF; |
| 282 | uint32 b_end = (mono_end ) & 0xFF; |
| 283 | |
| 284 | for(i = 0; i < 16; i++) |
| 285 | { |
| 286 | uint32 neo_r = LERP_MONO_PALETTE_COLOR(r_start, r_end, i); |
| 287 | uint32 neo_g = LERP_MONO_PALETTE_COLOR(g_start, g_end, i); |
| 288 | uint32 neo_b = LERP_MONO_PALETTE_COLOR(b_start, b_end, i); |
| 289 | |
| 290 | switch(depth) |
| 291 | { |
| 292 | case 15: ColorMapG[i] = MAKECOLOR_15(neo_r, neo_g, neo_b, 0); break; |
| 293 | #if defined(ABGR1555) |
| 294 | case 16: ColorMapG[i] = MAKECOLOR_15_1(neo_r, neo_g, neo_b, 0); break; |
| 295 | #else |
| 296 | case 16: ColorMapG[i] = MAKECOLOR_16(neo_r, neo_g, neo_b, 0); break; |
| 297 | #endif |
| 298 | case 24: ColorMapG[i] = MAKECOLOR_24(neo_r, neo_g, neo_b, 0); break; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void WSwan_SetPixelFormat(int depth, uint32 mono_start, uint32 mono_end) |
| 304 | { |
| 305 | unsigned r, g, b; |
| 306 | for(r = 0; r < 16; r++) |
| 307 | for(g = 0; g < 16; g++) |
| 308 | for(b = 0; b < 16; b++) |
| 309 | { |
| 310 | uint32 neo_r, neo_g, neo_b; |
| 311 | |
| 312 | neo_r = r * 17; |
| 313 | neo_g = g * 17; |
| 314 | neo_b = b * 17; |
| 315 | |
| 316 | switch(depth) |
| 317 | { |
| 318 | case 15: ColorMap[(r << 8) | (g << 4) | (b << 0)] = MAKECOLOR_15(neo_r, neo_g, neo_b, 0); break; |
| 319 | #if defined(ABGR1555) |
| 320 | case 16: ColorMap[(r << 8) | (g << 4) | (b << 0)] = MAKECOLOR_15_1(neo_r, neo_g, neo_b, 0); break; |
| 321 | #else |
| 322 | case 16: ColorMap[(r << 8) | (g << 4) | (b << 0)] = MAKECOLOR_16(neo_r, neo_g, neo_b, 0); break; |
| 323 | #endif |
| 324 | case 24: ColorMap[(r << 8) | (g << 4) | (b << 0)] = MAKECOLOR_24(neo_r, neo_g, neo_b, 0); break; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | WSwan_SetMonoPalette(depth, mono_start, mono_end); |
| 329 | } |
| 330 | |
| 331 | void wsScanline(uint16 *target, int depth) |
| 332 | { |
| 333 | uint32 start_tile_n,map_a,startindex,adrbuf,b1,b2,j,t,l; |
| 334 | uint8 b_bg[256]; |
| 335 | uint8 b_bg_pal[256]; |
| 336 | |
| 337 | if(!wsVMode) |
| 338 | memset(b_bg, wsColors[BGColor&0xF]&0xF, 256); |
| 339 | else |
| 340 | { |
| 341 | memset(&b_bg[0], BGColor & 0xF, 256); |
| 342 | memset(&b_bg_pal[0], (BGColor>>4) & 0xF, 256); |
| 343 | } |
| 344 | start_tile_n=(wsLine+BGYScroll)&0xff;/*First line*/ |
| 345 | map_a=(((uint32)(FGBGLoc&0xF))<<11)+((start_tile_n&0xfff8)<<3); |
| 346 | startindex = BGXScroll >> 3; /*First tile in row*/ |
| 347 | adrbuf = 7-(BGXScroll&7); /*Pixel in tile*/ |
| 348 | |
| 349 | if((DispControl & 0x01) && (LayerEnabled & 0x01)) /*BG layer*/ |
| 350 | { |
| 351 | for(t=0;t<29;t++) |
| 352 | { |
| 353 | uint32 palette; |
| 354 | b1=wsRAM[map_a+(startindex<<1)]; |
| 355 | b2=wsRAM[map_a+(startindex<<1)+1]; |
| 356 | palette=(b2>>1)&15; |
| 357 | b2=(b2<<8)|b1; |
| 358 | wsGetTile(b2&0x1ff,start_tile_n&7,b2&0x8000,b2&0x4000,b2&0x2000); |
| 359 | |
| 360 | if(wsVMode) |
| 361 | { |
| 362 | if(wsVMode & 0x2) |
| 363 | { |
| 364 | int x; |
| 365 | for(x = 0; x < 8; x++) |
| 366 | if(wsTileRow[x]) |
| 367 | { |
| 368 | b_bg[adrbuf + x] = wsTileRow[x]; |
| 369 | b_bg_pal[adrbuf + x] = palette; |
| 370 | } |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | int x; |
| 375 | for(x = 0; x < 8; x++) |
| 376 | if(wsTileRow[x] || !(palette & 0x4)) |
| 377 | { |
| 378 | b_bg[adrbuf + x] = wsTileRow[x]; |
| 379 | b_bg_pal[adrbuf + x] = palette; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | int x; |
| 386 | for(x = 0; x < 8; x++) |
| 387 | if(wsTileRow[x] || !(palette & 4)) |
| 388 | { |
| 389 | b_bg[adrbuf + x] = wsColors[wsMonoPal[palette][wsTileRow[x]]]; |
| 390 | } |
| 391 | } |
| 392 | adrbuf += 8; |
| 393 | startindex=(startindex + 1)&31; |
| 394 | } /* end for(t = 0 ... */ |
| 395 | } /* End BG layer drawing */ |
| 396 | |
| 397 | if((DispControl & 0x02) && (LayerEnabled & 0x02))/*FG layer*/ |
| 398 | { |
| 399 | uint8 windowtype = DispControl&0x30; |
| 400 | bool in_window[256 + 8*2]; |
| 401 | |
| 402 | if(windowtype) |
| 403 | { |
| 404 | memset(in_window, 0, sizeof(in_window)); |
| 405 | |
| 406 | if(windowtype == 0x20) /* Display FG only inside window */ |
| 407 | { |
| 408 | if((wsLine >= FGy0) && (wsLine <= FGy1)) |
| 409 | for(j = FGx0; j <= FGx1 && j < 224; j++) |
| 410 | in_window[7 + j] = 1; |
| 411 | } |
| 412 | else if(windowtype == 0x30) /* Display FG only outside window */ |
| 413 | { |
| 414 | for(j = 0; j < 224; j++) |
| 415 | { |
| 416 | if(!(j >= FGx0 && j <= FGx1) || !((wsLine >= FGy0) && (wsLine < FGy1))) |
| 417 | in_window[7 + j] = 1; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | else |
| 422 | memset(in_window, 1, sizeof(in_window)); |
| 423 | |
| 424 | start_tile_n=(wsLine+FGYScroll)&0xff; |
| 425 | map_a=(((uint32)((FGBGLoc>>4)&0xF))<<11)+((start_tile_n>>3)<<6); |
| 426 | startindex = FGXScroll >> 3; |
| 427 | adrbuf = 7-(FGXScroll&7); |
| 428 | |
| 429 | for(t=0; t<29; t++) |
| 430 | { |
| 431 | uint32 palette; |
| 432 | b1=wsRAM[map_a+(startindex<<1)]; |
| 433 | b2=wsRAM[map_a+(startindex<<1)+1]; |
| 434 | palette=(b2>>1)&15; |
| 435 | b2=(b2<<8)|b1; |
| 436 | wsGetTile(b2&0x1ff,start_tile_n&7,b2&0x8000,b2&0x4000,b2&0x2000); |
| 437 | |
| 438 | if(wsVMode) |
| 439 | { |
| 440 | if(wsVMode & 0x2) |
| 441 | { |
| 442 | int x; |
| 443 | for(x = 0; x < 8; x++) |
| 444 | { |
| 445 | if(wsTileRow[x] && in_window[adrbuf + x]) |
| 446 | { |
| 447 | b_bg[adrbuf + x] = wsTileRow[x] | 0x10; |
| 448 | b_bg_pal[adrbuf + x] = palette; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | int x; |
| 455 | for(x = 0; x < 8; x++) |
| 456 | { |
| 457 | if((wsTileRow[x] || !(palette & 0x4)) && in_window[adrbuf + x]) |
| 458 | { |
| 459 | b_bg[adrbuf + x] = wsTileRow[x] | 0x10; |
| 460 | b_bg_pal[adrbuf + x] = palette; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | int x; |
| 468 | for(x = 0; x < 8; x++) |
| 469 | if((wsTileRow[x] || !(palette & 4)) && in_window[adrbuf + x]) |
| 470 | { |
| 471 | b_bg[adrbuf + x] = wsColors[wsMonoPal[palette][wsTileRow[x]]] | 0x10; |
| 472 | } |
| 473 | } |
| 474 | adrbuf += 8; |
| 475 | startindex=(startindex + 1)&31; |
| 476 | } /* end for(t = 0 ... */ |
| 477 | |
| 478 | } /* end FG drawing */ |
| 479 | |
| 480 | if((DispControl & 0x04) && SpriteCountCache[FrameWhichActive] && (LayerEnabled & 0x04))/* Sprites */ |
| 481 | { |
| 482 | int xs,ts,as,ys,ysx,h; |
| 483 | bool in_window[256 + 8*2]; |
| 484 | |
| 485 | if(DispControl & 0x08) |
| 486 | { |
| 487 | memset(in_window, 0, sizeof(in_window)); |
| 488 | if((wsLine >= SPRy0) && (wsLine <= SPRy1)) |
| 489 | for(j = SPRx0; j <= SPRx1 && j < 256; j++) |
| 490 | in_window[7 + j] = 1; |
| 491 | } |
| 492 | else |
| 493 | memset(in_window, 1, sizeof(in_window)); |
| 494 | |
| 495 | for(h = SpriteCountCache[FrameWhichActive] - 1; h >= 0; h--) |
| 496 | { |
| 497 | ts = SpriteTable[FrameWhichActive][h][0]; |
| 498 | as = SpriteTable[FrameWhichActive][h][1]; |
| 499 | ysx = SpriteTable[FrameWhichActive][h][2]; |
| 500 | xs = SpriteTable[FrameWhichActive][h][3]; |
| 501 | |
| 502 | if(xs >= 249) xs -= 256; |
| 503 | |
| 504 | if(ysx > 150) |
| 505 | ys = (int8)ysx; |
| 506 | else |
| 507 | ys = ysx; |
| 508 | |
| 509 | ys = wsLine - ys; |
| 510 | |
| 511 | if(ys >= 0 && ys < 8 && xs < 224) |
| 512 | { |
| 513 | uint32 palette = ((as >> 1) & 0x7); |
| 514 | |
| 515 | ts |= (as&1) << 8; |
| 516 | wsGetTile(ts, ys, as & 0x80, as & 0x40, 0); |
| 517 | |
| 518 | if(wsVMode) |
| 519 | { |
| 520 | if(wsVMode & 0x2) |
| 521 | { |
| 522 | int x; |
| 523 | for(x = 0; x < 8; x++) |
| 524 | if(wsTileRow[x]) |
| 525 | { |
| 526 | if((as & 0x20) || !(b_bg[xs + x + 7] & 0x10)) |
| 527 | { |
| 528 | bool drawthis = 0; |
| 529 | |
| 530 | if(!(DispControl & 0x08)) |
| 531 | drawthis = true; |
| 532 | else if((as & 0x10) && !in_window[7 + xs + x]) |
| 533 | drawthis = true; |
| 534 | else if(!(as & 0x10) && in_window[7 + xs + x]) |
| 535 | drawthis = true; |
| 536 | |
| 537 | if(drawthis) |
| 538 | { |
| 539 | b_bg[xs + x + 7] = wsTileRow[x] | (b_bg[xs + x + 7] & 0x10); |
| 540 | b_bg_pal[xs + x + 7] = 8 + palette; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | int x; |
| 548 | for(x = 0; x < 8; x++) |
| 549 | if(wsTileRow[x] || !(palette & 0x4)) |
| 550 | { |
| 551 | if((as & 0x20) || !(b_bg[xs + x + 7] & 0x10)) |
| 552 | { |
| 553 | bool drawthis = 0; |
| 554 | |
| 555 | if(!(DispControl & 0x08)) |
| 556 | drawthis = true; |
| 557 | else if((as & 0x10) && !in_window[7 + xs + x]) |
| 558 | drawthis = true; |
| 559 | else if(!(as & 0x10) && in_window[7 + xs + x]) |
| 560 | drawthis = true; |
| 561 | |
| 562 | if(drawthis) |
| 563 | { |
| 564 | b_bg[xs + x + 7] = wsTileRow[x] | (b_bg[xs + x + 7] & 0x10); |
| 565 | b_bg_pal[xs + x + 7] = 8 + palette; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | } |
| 571 | |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | int x; |
| 576 | for(x = 0; x < 8; x++) |
| 577 | if(wsTileRow[x] || !(palette & 4)) |
| 578 | { |
| 579 | if((as & 0x20) || !(b_bg[xs + x + 7] & 0x10)) |
| 580 | { |
| 581 | bool drawthis = false; |
| 582 | |
| 583 | if(!(DispControl & 0x08)) |
| 584 | drawthis = true; |
| 585 | else if((as & 0x10) && !in_window[7 + xs + x]) |
| 586 | drawthis = true; |
| 587 | else if(!(as & 0x10) && in_window[7 + xs + x]) |
| 588 | drawthis = true; |
| 589 | |
| 590 | if(drawthis) |
| 591 | b_bg[xs + x + 7] = wsColors[wsMonoPal[8 + palette][wsTileRow[x]]] | (b_bg[xs + x + 7] & 0x10); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | } /* End sprite drawing */ |
| 600 | |
| 601 | if(wsVMode) |
| 602 | { |
| 603 | switch(depth) |
| 604 | { |
| 605 | case 15: |
| 606 | case 16: { |
| 607 | for(l=0;l<224;l++) |
| 608 | target[l] = ColorMap[wsCols[b_bg_pal[l+7]][b_bg[(l+7)]&0xf]]; |
| 609 | } break; |
| 610 | |
| 611 | case 24: { |
| 612 | for(l=0;l<224;l++) |
| 613 | ((uint32_t*)target)[l] = ColorMap[wsCols[b_bg_pal[l+7]][b_bg[(l+7)]&0xf]]; |
| 614 | } break; |
| 615 | } |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | switch(depth) |
| 620 | { |
| 621 | case 15: |
| 622 | case 16: { |
| 623 | for(l=0;l<224;l++) |
| 624 | target[l] = ColorMapG[(b_bg[l+7])&15]; |
| 625 | } break; |
| 626 | |
| 627 | case 24: { |
| 628 | for(l=0;l<224;l++) |
| 629 | ((uint32_t*)target)[l] = ColorMapG[(b_bg[l+7])&15]; |
| 630 | } break; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | |
| 636 | void WSwan_GfxReset(void) |
| 637 | { |
| 638 | unsigned u0, u1; |
| 639 | |
| 640 | wsLine=0; |
| 641 | wsSetVideo(0,true); |
| 642 | |
| 643 | memset(SpriteTable, 0, sizeof(SpriteTable)); |
| 644 | SpriteCountCache[0] = SpriteCountCache[1] = 0; |
| 645 | FrameWhichActive = false; |
| 646 | DispControl = 0; |
| 647 | BGColor = 0; |
| 648 | LineCompare = 0xBB; |
| 649 | SPRBase = 0; |
| 650 | |
| 651 | SpriteStart = 0; |
| 652 | SpriteCount = 0; |
| 653 | FGBGLoc = 0; |
| 654 | |
| 655 | FGx0 = 0; |
| 656 | FGy0 = 0; |
| 657 | FGx1 = 0; |
| 658 | FGy1 = 0; |
| 659 | SPRx0 = 0; |
| 660 | SPRy0 = 0; |
| 661 | SPRx1 = 0; |
| 662 | SPRy1 = 0; |
| 663 | |
| 664 | BGXScroll = BGYScroll = 0; |
| 665 | FGXScroll = FGYScroll = 0; |
| 666 | LCDControl = 0; |
| 667 | LCDIcons = 0; |
| 668 | LCDVtotal = 158; |
| 669 | |
| 670 | BTimerControl = 0; |
| 671 | HBTimerPeriod = 0; |
| 672 | VBTimerPeriod = 0; |
| 673 | |
| 674 | HBCounter = 0; |
| 675 | VBCounter = 0; |
| 676 | |
| 677 | |
| 678 | for(u0=0;u0<16;u0++) |
| 679 | for(u1=0;u1<16;u1++) |
| 680 | wsCols[u0][u1]=0; |
| 681 | |
| 682 | } |
| 683 | |
| 684 | int WSwan_GfxStateAction(StateMem *sm, int load, int data_only) |
| 685 | { |
| 686 | SFORMAT StateRegs[] = |
| 687 | { |
| 688 | SFARRAY32N(&wsMonoPal[0][0], 16 * 4, "wsMonoPal"), |
| 689 | SFARRAY32(wsColors, 8), |
| 690 | |
| 691 | SFVAR(wsLine), |
| 692 | |
| 693 | SFARRAYN(&SpriteTable[0][0][0], 2 * 0x80 * 4, "SpriteTable"), |
| 694 | SFARRAY32N(&SpriteCountCache[0], 2 * 1, "SpriteCountCache"), |
| 695 | SFVARN_BOOL(FrameWhichActive, "FrameWhichActive"), |
| 696 | SFVAR(DispControl), |
| 697 | SFVAR(BGColor), |
| 698 | SFVAR(LineCompare), |
| 699 | SFVAR(SPRBase), |
| 700 | SFVAR(SpriteStart), |
| 701 | SFVAR(SpriteCount), |
| 702 | SFVAR(FGBGLoc), |
| 703 | SFVAR(FGx0), |
| 704 | SFVAR(FGy0), |
| 705 | SFVAR(FGx1), |
| 706 | SFVAR(FGy1), |
| 707 | |
| 708 | SFVAR(SPRx0), |
| 709 | SFVAR(SPRy0), |
| 710 | |
| 711 | SFVAR(SPRx1), |
| 712 | SFVAR(SPRy1), |
| 713 | |
| 714 | SFVAR(BGXScroll), |
| 715 | SFVAR(BGYScroll), |
| 716 | SFVAR(FGXScroll), |
| 717 | SFVAR(FGYScroll), |
| 718 | SFVAR(LCDControl), |
| 719 | SFVAR(LCDIcons), |
| 720 | SFVAR(LCDVtotal), |
| 721 | |
| 722 | SFVAR(BTimerControl), |
| 723 | SFVAR(HBTimerPeriod), |
| 724 | SFVAR(VBTimerPeriod), |
| 725 | |
| 726 | SFVAR(HBCounter), |
| 727 | SFVAR(VBCounter), |
| 728 | |
| 729 | SFVAR(VideoMode), |
| 730 | SFEND |
| 731 | }; |
| 732 | |
| 733 | if(!MDFNSS_StateAction(sm, load, data_only, StateRegs, "GFX", false)) |
| 734 | return(0); |
| 735 | |
| 736 | if(load) |
| 737 | { |
| 738 | unsigned i, j; |
| 739 | for(i = 0; i < 2; i++) |
| 740 | { |
| 741 | if(SpriteCountCache[i] > 0x80) |
| 742 | SpriteCountCache[i] = 0x80; |
| 743 | } |
| 744 | |
| 745 | for(i = 0; i < 16; i++) |
| 746 | for(j = 0; j < 4; j++) |
| 747 | wsMonoPal[i][j] &= 0x7; |
| 748 | |
| 749 | wsSetVideo(VideoMode >> 5, true); |
| 750 | } |
| 751 | |
| 752 | return(1); |
| 753 | } |