1/******************************************************************************/ 2/* Mednafen NEC PC-FX Emulation Module */ 3/******************************************************************************/ 4/* king-bgfast.inc: 5** Copyright (C) 2006-2016 Mednafen Team 6** 7** This program is free software; you can redistribute it and/or 8** modify it under the terms of the GNU General Public License 9** as published by the Free Software Foundation; either version 2 10** of the License, or (at your option) any later version. 11** 12** This program is distributed in the hope that it will be useful, 13** but WITHOUT ANY WARRANTY; without even the implied warranty of 14** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15** GNU General Public License for more details. 16** 17** You should have received a copy of the GNU General Public License 18** along with this program; if not, write to the Free Software Foundation, Inc., 19** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20*/ 21 22// Loop prefix non-endless 23#define DRAWBG8x1_LPRE() if(bat_x < bat_width) { 24 25// Loop postfix non-endless 26#define DRAWBG8x1_LPOST() } bat_x = (bat_x + 1) & bat_width_mask; 27 28#define CDBG_REASON(format, ...) 29//printf("BG%d Reason: " format "\n", n, ## __VA_ARGS__); 30static bool CanDrawBG_Fast(int n) 31{ 32 static const int cg_per_mode[0x8] = 33 { 34 0, // Invalid mode 35 1, // 2-bit mode 36 2, // 4-bit mode 37 4, // 8-bit mode 38 8, // 16-bit mode 39 8, // 16-bit mode 40 8, // 16-bit mode 41 8, // 16-bit mode 42 }; 43 44 const unsigned int bgmode = (king->bgmode >> (n * 4)) & 0xF; 45 const uint32 bat_offset = king->BGBATAddr[n] * 1024; 46 const uint32 cg_offset = king->BGCGAddr[n] * 1024; 47 const uint32 bg_width = (king->BGSize[n] & 0xF0) >> 4; 48 const uint32 bg_height = king->BGSize[n] & 0x0F; 49 50 // If the bgmode is 0, or 6/7(EXT DOT modes), abort. 51 if(!(bgmode & 0x7) || ((bgmode & 0x7) >= 6)) 52 { 53 CDBG_REASON("Mode %02x out of range", bgmode); 54 return(FALSE); 55 } 56 57 // BG width out of range? 58 if(bg_width < 0x3 || bg_width > 0xA) 59 { 60 CDBG_REASON("Width %02x out of range", bg_width); 61 return(FALSE); 62 } 63 64 // BG height out of range? 65 if(bg_height < 0x3 || bg_height > 0xA) 66 { 67 CDBG_REASON("Height %02x out of range", bg_height); 68 return(FALSE); 69 } 70 71 // Time for very stringent checks for BG0! Don't draw if the sub-screen is a different size than the main screen, 72 // or the subscreen CG base != the main screen CG base, or the subscreen BAT base != main screen BAT base(when bgmode & 0x8 is TRUE) 73 if(!n) 74 { 75 if(king->priority & 0x1000) 76 { 77 CDBG_REASON("Affine transform enabled"); 78 return(FALSE); 79 } 80 81 if((king->BGSize[0] & 0xFF) != (king->BGSize[0] >> 8)) 82 { 83 CDBG_REASON("Main Screen/Sub Screen Size Mismatch"); 84 return(FALSE); 85 } 86 87 // Since we already made sure the main screen/sub screen sizes match, we only 88 // care if BG base or CG base don't match if endless/repeat scrolling is enabled. 89 // Otherwise, the subscreen won't show at all. 90 if(king->BGScrollMode & 0x1) 91 { 92 if(king->BGCGAddr[0] != king->BG0SubCGAddr) 93 { 94 CDBG_REASON("Main Screen/Sub Screen CG base mismatch"); 95 return(FALSE); 96 } 97 98 if(bgmode & 0x8) 99 { 100 if(king->BGBATAddr[0] != king->BG0SubBATAddr) 101 { 102 CDBG_REASON("Main Screen/Sub Screen BAT base mismatch"); 103 return(FALSE); 104 } 105 } 106 } 107 } 108 109 110 // If microprogram fetching is disabled, abort. 111 if(!(king->MPROGControl & 0x1)) 112 { 113 CDBG_REASON("Microprogram disabled"); 114 return(FALSE); 115 } 116 else 117 { 118 int remap_thing = 0; 119 bool bat_fetch = FALSE; 120 121 for(int x = 0; x < 16; x++) 122 { 123 uint16 mpd; 124 125 // Forcing CG and BAT to 0 if the affine bit != rotate_mode is not technically correct. 126 // If there is a mismatch, it's more likely the effective CG and BAT address for the pixel/segment 127 // being drawn won't be calculated correctly, likely being just the initial offsets. 128 129 mpd = king->MPROGData[x]; 130 131 // Fetching for this BG number? 132 if(((mpd >> 6) & 0x3) != n) 133 continue; 134 135 // NOP 136 if(mpd & 0x100) 137 continue; 138 139 // Affine bit. 140 if(mpd & 0x20) 141 { 142 CDBG_REASON("Affine bit set"); 143 return(FALSE); 144 } 145 146 // BAT fetch 147 if(mpd & 0x10) 148 { 149 if(((bat_offset & 0x20000) >> 14) != (x & 8)) 150 { 151 CDBG_REASON("BAT MPROG/base bank mismatch"); 152 return(FALSE); 153 } 154 155 bat_fetch = TRUE; 156 } 157 else // CG fetch: 158 { 159 // CG offset/bank mismatch... 160 if(((cg_offset & 0x20000) >> 14) != (x & 8)) 161 { 162 CDBG_REASON("CG MPROG/base bank mismatch"); 163 return(FALSE); 164 } 165 166 // Mismatch between CG fetch type and BG mode! 167 if((mpd & 0x8) != (bgmode & 0x8)) 168 { 169 CDBG_REASON("Mismatch between CG fetch type and bg mode"); 170 return(FALSE); 171 } 172 173 // Skewed CG fetch order 174 if((mpd & 0x7) != remap_thing) 175 { 176 CDBG_REASON("Skewed CG fetch order"); 177 return(FALSE); 178 } 179 remap_thing++; 180 } 181 } 182 183 // CG fetch count mismatch 184 if(remap_thing != cg_per_mode[bgmode & 0x7]) 185 { 186 CDBG_REASON("CG fetch count mismatch"); 187 return(FALSE); 188 } 189 190 // BG mode demands a BAT fetch, but we don't have one! 191 if((bgmode & 0x8) && !bat_fetch) 192 { 193 CDBG_REASON("Missing BAT fetch"); 194 return(FALSE); 195 } 196 } 197 198 199 return(TRUE); 200} 201 202static void DrawBG_Fast(uint32 *target, int n) 203{ 204 const uint16 bgmode = (king->bgmode >> (n * 4)) & 0xF; 205 const bool endless = (king->BGScrollMode >> n) & 0x1; 206 const uint32 XScroll = sign_x_to_s32((n ? 10 : 11), king->BGXScroll[n]); 207 const uint32 YScroll = sign_x_to_s32((n ? 10 : 11), king->BGYScroll[n]); 208 const uint32 bat_offset = king->BGBATAddr[n] * 1024; 209 const uint32 cg_offset = king->BGCGAddr[n] * 1024; 210 const uint32 bat_and_cg_page = (king->PageSetting & 0x0010) ? 1 : 0; 211 212 const uint32 YOffset = (YScroll + (fx_vce.raster_counter - 22)) & 0xFFFF; 213 const uint32 layer_or = (LAYER_BG0 + n) << 28; 214 const int ysmall = YOffset & 0x7; 215 216 const unsigned int bat_width_shift = (king->BGSize[n] & 0xF0) >> 4; 217 const unsigned int bat_width = (1 << bat_width_shift) >> 3; 218 const unsigned int bat_width_mask = endless ? (bat_width - 1) : ((((1 << 0xA) * 2) / 8) - 1); 219 220 const int bat_height_shift = king->BGSize[n] & 0x0F; 221 const int bat_height = (1 << bat_height_shift) >> 3; 222 const int bat_height_mask = endless ? (bat_height - 1) : ((((1 << 0xA) * 2) / 8) - 1); 223 224 // We don't need to &= cg_offset and bat_offset with 0x1ffff after here, as the effective addresses 225 // calculated with them are anded with 0x1ffff in the rendering code already. 226 const uint16 * const king_cg_base = &king->KRAM[bat_and_cg_page][cg_offset & 0x20000]; 227 const uint16 * const king_bat_base = &king->KRAM[bat_and_cg_page][bat_offset & 0x20000]; 228 229 int bat_y = (YOffset >> 3) & bat_height_mask; 230 uint32 bat_x = (XScroll >> 3) & bat_width_mask; 231 232 target += 8 - (XScroll & 0x7); 233 234 // If we're in non-endless scroll mode, and we've scrolled past our visible area in the vertical direction, so return. 235 if(!endless && bat_y >= bat_height) // Draw this line as transparency and return? 236 { 237 return; 238 } 239 240 // Adjust/corrupt bat_y to be faster in our blitting code 241 bat_y = (bat_y << bat_width_shift) >> 3; 242 243 const uint32 palette_offset = ((vce_rendercache.palette_offset[1 + (n >> 1)] >> ((n & 1) ? 8 : 0)) << 1) & 0x1FF; 244 const uint32 * const palette_ptr = &vce_rendercache.palette_table_cache[palette_offset]; 245 246 { 247 int wmul = (1 << bat_width_shift), wmask = (1 << bat_height_shift) - 1; 248 int sexy_y_pos = (YOffset & wmask) * wmul; 249 250 #if 0 251 #define BGFAST_BATMODE (bgmode & 0x8) 252 #include "king-bgfast-blit.inc" 253 254 #else 255 if(bgmode & 0x8) 256 { 257 #define BGFAST_BATMODE 1 258 #include "king-bgfast-blit.inc" 259 #undef BGFAST_BATMODE 260 } 261 else 262 { 263 #define BGFAST_BATMODE 0 264 #include "king-bgfast-blit.inc" 265 #undef BGFAST_BATMODE 266 } 267 #endif 268 } 269} 270 271