src/mess/video/gb.c
| r1406 | r1407 | |
| 85 | 85 | int end_x; /* Pixel to end drawing (exclusive) */ |
| 86 | 86 | int mode; /* Keep track of internal STAT mode */ |
| 87 | 87 | int lcd_irq_line; |
| 88 | int triggering_line_irq; |
| 88 | 89 | int line_irq; |
| 89 | 90 | int mode_irq; |
| 90 | 91 | int delayed_line_irq; |
| 92 | int scrollx_adjust; |
| 91 | 93 | struct layer_struct layer[2]; |
| 92 | 94 | emu_timer *lcd_timer; |
| 93 | 95 | } gb_lcd; |
| r1406 | r1407 | |
| 1140 | 1142 | |
| 1141 | 1143 | enum { |
| 1142 | 1144 | GB_LCD_STATE_LYXX_M3=1, |
| 1145 | GB_LCD_STATE_LYXX_PRE_M0, |
| 1143 | 1146 | GB_LCD_STATE_LYXX_M0, |
| 1144 | | GB_LCD_STATE_LYXX_M0_2, |
| 1145 | 1147 | GB_LCD_STATE_LYXX_M0_PRE_INC, |
| 1146 | 1148 | GB_LCD_STATE_LYXX_M0_INC, |
| 1147 | 1149 | GB_LCD_STATE_LY00_M2, |
| r1406 | r1407 | |
| 1159 | 1161 | int mode = param; |
| 1160 | 1162 | if ( LCDCONT & 0x80 ) { |
| 1161 | 1163 | switch( mode ) { |
| 1164 | case GB_LCD_STATE_LYXX_PRE_M0: /* Just before switching to mode 0 */ |
| 1165 | gb_lcd.mode = 0; |
| 1166 | if ( LCDSTAT & 0x08 ) { |
| 1167 | if ( ! gb_lcd.mode_irq ) { |
| 1168 | if ( ! gb_lcd.line_irq && ! gb_lcd.delayed_line_irq ) { |
| 1169 | gb_lcd.mode_irq = 1; |
| 1170 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1171 | } |
| 1172 | } else { |
| 1173 | gb_lcd.mode_irq = 0; |
| 1174 | } |
| 1175 | } |
| 1176 | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(4,0), GB_LCD_STATE_LYXX_M0, attotime_never ); |
| 1177 | break; |
| 1162 | 1178 | case GB_LCD_STATE_LYXX_M0: /* Switch to mode 0 */ |
| 1163 | 1179 | /* update current scanline */ |
| 1164 | 1180 | update_scanline(); |
| r1406 | r1407 | |
| 1178 | 1194 | /* Check for HBLANK DMA */ |
| 1179 | 1195 | if( gbc_hdma_enabled ) |
| 1180 | 1196 | gbc_hdma(0x10); |
| 1181 | | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(196 - 10 * gb_lcd.sprCount,0), GB_LCD_STATE_LYXX_M0_PRE_INC, attotime_never ); |
| 1197 | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(196 - gb_lcd.scrollx_adjust - 10 * gb_lcd.sprCount,0), GB_LCD_STATE_LYXX_M0_PRE_INC, attotime_never ); |
| 1182 | 1198 | break; |
| 1183 | 1199 | case GB_LCD_STATE_LYXX_M0_PRE_INC: /* Just before incrementing the line counter go to mode 2 internally */ |
| 1184 | 1200 | if ( CURLINE < 143 ) { |
| 1185 | 1201 | gb_lcd.mode = 2; |
| 1186 | | if ( ! gb_lcd.mode_irq ) { |
| 1187 | | if ( ( LCDSTAT & 0x20 ) && ( LCDSTAT & 0x40 ) && ( CMPLINE == CURLINE + 1 ) ) { |
| 1188 | | gb_lcd.mode_irq = 1; |
| 1189 | | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1202 | if ( LCDSTAT & 0x20 ) { |
| 1203 | if ( ! gb_lcd.mode_irq ) { |
| 1204 | if ( ! gb_lcd.line_irq && ! gb_lcd.delayed_line_irq ) { |
| 1205 | gb_lcd.mode_irq = 1; |
| 1206 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1207 | } |
| 1208 | } else { |
| 1209 | gb_lcd.mode_irq = 0; |
| 1190 | 1210 | } |
| 1191 | 1211 | } |
| 1192 | 1212 | } |
| r1406 | r1407 | |
| 1195 | 1215 | case GB_LCD_STATE_LYXX_M0_INC: /* Increment LY, stay in M0 for 4 more cycles */ |
| 1196 | 1216 | gb_increment_scanline(); |
| 1197 | 1217 | gb_lcd.delayed_line_irq = gb_lcd.line_irq; |
| 1198 | | gb_lcd.line_irq = ( ( CMPLINE == CURLINE ) && ( LCDSTAT & 0x40 ) ) ? 1 : 0; |
| 1199 | | if ( ! gb_lcd.delayed_line_irq && gb_lcd.line_irq && ! ( LCDSTAT & 0x20 ) ) { |
| 1218 | gb_lcd.triggering_line_irq = ( ( CMPLINE == CURLINE ) && ( LCDSTAT & 0x40 ) ) ? 1 : 0; |
| 1219 | gb_lcd.line_irq = 0; |
| 1220 | if ( ! gb_lcd.mode_irq && ! gb_lcd.delayed_line_irq && gb_lcd.triggering_line_irq && ! ( LCDSTAT & 0x20 ) ) { |
| 1221 | gb_lcd.line_irq = gb_lcd.triggering_line_irq; |
| 1200 | 1222 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1201 | 1223 | } |
| 1202 | 1224 | /* Reset LY==LYC STAT bit */ |
| r1406 | r1407 | |
| 1209 | 1231 | gb_lcd.mode = 2; |
| 1210 | 1232 | /* Generate lcd interrupt if requested */ |
| 1211 | 1233 | if ( ! gb_lcd.mode_irq && ( LCDSTAT & 0x20 ) && |
| 1212 | | ( ( ! gb_lcd.line_irq && ! gb_lcd.delayed_line_irq ) || ! ( LCDSTAT & 0x40 ) ) ) { |
| 1234 | ( ( ! gb_lcd.triggering_line_irq && ! gb_lcd.delayed_line_irq ) || ! ( LCDSTAT & 0x40 ) ) ) { |
| 1213 | 1235 | gb_lcd.mode_irq = 1; |
| 1214 | 1236 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1215 | | } else { |
| 1216 | | gb_lcd.mode_irq = 0; |
| 1217 | 1237 | } |
| 1218 | 1238 | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(4,0), GB_LCD_STATE_LYXX_M2, attotime_never ); |
| 1219 | 1239 | } |
| r1406 | r1407 | |
| 1223 | 1243 | gb_lcd.mode = 2; |
| 1224 | 1244 | LCDSTAT = ( LCDSTAT & 0xFC ) | 0x02; |
| 1225 | 1245 | /* Generate lcd interrupt if requested */ |
| 1226 | | if ( ( LCDSTAT & 0x20 ) && ! gb_lcd.line_irq ) { |
| 1246 | if ( ( LCDSTAT & 0x20 ) && ! gb_lcd.line_irq && ! gb_lcd.line_irq ) { |
| 1227 | 1247 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1228 | 1248 | } |
| 1229 | 1249 | /* Mode 2 lasts approximately 80 clock cycles */ |
| r1406 | r1407 | |
| 1233 | 1253 | /* Update STAT register to the correct state */ |
| 1234 | 1254 | LCDSTAT = (LCDSTAT & 0xFC) | 0x02; |
| 1235 | 1255 | /* Generate lcd interrupt if requested */ |
| 1236 | | if ( ( gb_lcd.delayed_line_irq && gb_lcd.line_irq && ! ( LCDSTAT & 0x20 ) ) || |
| 1256 | if ( ( gb_lcd.delayed_line_irq && gb_lcd.triggering_line_irq && ! ( LCDSTAT & 0x20 ) ) || |
| 1237 | 1257 | ( !gb_lcd.mode_irq && ! gb_lcd.line_irq && ! gb_lcd.delayed_line_irq && ( LCDSTAT & 0x20 ) ) ) { |
| 1238 | 1258 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1239 | 1259 | } |
| 1260 | gb_lcd.line_irq = gb_lcd.triggering_line_irq; |
| 1240 | 1261 | /* Check if LY==LYC STAT bit should be set */ |
| 1241 | 1262 | if ( CURLINE == CMPLINE ) { |
| 1242 | 1263 | LCDSTAT |= 0x04; |
| r1406 | r1407 | |
| 1249 | 1270 | /* Set Mode 3 lcdstate */ |
| 1250 | 1271 | gb_lcd.mode = 3; |
| 1251 | 1272 | LCDSTAT = (LCDSTAT & 0xFC) | 0x03; |
| 1273 | /* Check for compensations of x-scroll register */ |
| 1274 | if ( ( SCROLLX & 0x07 ) > 2 ) { |
| 1275 | gb_lcd.scrollx_adjust = 4; |
| 1276 | } else { |
| 1277 | gb_lcd.scrollx_adjust = 0; |
| 1278 | } |
| 1252 | 1279 | /* Mode 3 lasts for approximately 172+#sprites*10 clock cycles */ |
| 1253 | | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(172 + 10 * gb_lcd.sprCount,0), GB_LCD_STATE_LYXX_M0, attotime_never ); |
| 1280 | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(168 + gb_lcd.scrollx_adjust + 10 * gb_lcd.sprCount,0), GB_LCD_STATE_LYXX_PRE_M0, attotime_never ); |
| 1254 | 1281 | gb_lcd.start_x = 0; |
| 1255 | 1282 | break; |
| 1256 | 1283 | case GB_LCD_STATE_LY9X_M1: /* Switch to or stay in mode 1 */ |
| r1406 | r1407 | |
| 1292 | 1319 | if ( gb_lcd.line_irq ) { |
| 1293 | 1320 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1294 | 1321 | } |
| 1295 | | } else { |
| 1296 | | gb_lcd.delayed_line_irq = gb_lcd.line_irq; |
| 1297 | 1322 | } |
| 1323 | gb_lcd.delayed_line_irq = gb_lcd.delayed_line_irq | gb_lcd.line_irq; |
| 1298 | 1324 | if ( CURLINE == CMPLINE ) { |
| 1299 | 1325 | LCDSTAT |= 0x04; |
| 1300 | 1326 | } |
| 1301 | 1327 | gb_increment_scanline(); |
| 1302 | | gb_lcd.line_irq = ( ( CMPLINE == CURLINE ) && ( LCDSTAT & 0x40 ) ) ? 1 : 0; |
| 1303 | | // if ( ! gb_lcd.delayed_line_irq && gb_lcd.line_irq ) { |
| 1304 | | // cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1305 | | // } |
| 1328 | gb_lcd.triggering_line_irq = ( ( CMPLINE == CURLINE ) && ( LCDSTAT & 0x40 ) ) ? 1 : 0; |
| 1329 | gb_lcd.line_irq = 0; |
| 1306 | 1330 | LCDSTAT &= 0xFB; |
| 1307 | 1331 | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(4/*8*/,0), GB_LCD_STATE_LY00_M1_1, attotime_never ); |
| 1308 | 1332 | break; |
| 1309 | 1333 | case GB_LCD_STATE_LY00_M1_1: |
| 1310 | | if ( ! gb_lcd.delayed_line_irq && gb_lcd.line_irq ) { |
| 1334 | if ( ! gb_lcd.delayed_line_irq && gb_lcd.triggering_line_irq ) { |
| 1335 | gb_lcd.line_irq = gb_lcd.triggering_line_irq; |
| 1311 | 1336 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1312 | 1337 | } |
| 1313 | 1338 | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(4,0), GB_LCD_STATE_LY00_M1_2, attotime_never ); |
| 1314 | 1339 | break; |
| 1315 | 1340 | case GB_LCD_STATE_LY00_M1_2: /* Rest of line #0 during VBlank */ |
| 1316 | | if ( gb_lcd.delayed_line_irq && gb_lcd.line_irq ) { |
| 1341 | if ( gb_lcd.delayed_line_irq && gb_lcd.triggering_line_irq ) { |
| 1342 | gb_lcd.line_irq = gb_lcd.triggering_line_irq; |
| 1317 | 1343 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1318 | 1344 | } |
| 1319 | 1345 | if ( CURLINE == CMPLINE ) { |
| r1406 | r1407 | |
| 1387 | 1413 | } |
| 1388 | 1414 | |
| 1389 | 1415 | WRITE8_HANDLER ( gb_video_w ) { |
| 1416 | gb_video_up_to_date(); |
| 1390 | 1417 | switch (offset) { |
| 1391 | 1418 | case 0x00: /* LCDC - LCD Control */ |
| 1392 | 1419 | gb_chrgen = gb_vram + ((data & 0x10) ? 0x0000 : 0x0800); |
| r1406 | r1407 | |
| 1411 | 1438 | interrupt to be triggered. |
| 1412 | 1439 | */ |
| 1413 | 1440 | if ( LCDCONT & 0x80 ) { |
| 1414 | | /* Are these triggers correct? */ |
| 1415 | | if ( ( gb_lcd.mode != 2 ) || ( ! ( data & 0x40 ) && ! ( LCDSTAT & 0x40 ) ) ) { |
| 1441 | /* Triggers seen so far: |
| 1442 | - 0x40 -> 0x00 - trigger |
| 1443 | - 0x00 -> 0x08 - trigger |
| 1444 | - 0x08 -> 0x00 - don't trigger |
| 1445 | - 0x00 -> 0x20 (mode 3) - trigger |
| 1446 | - 0x00 -> 0x60 (mode 2) - don't trigger |
| 1447 | - 0x20 -> 0x60 (mode 3) - trigger |
| 1448 | - 0x20 -> 0x40 (mode 3) - trigger |
| 1449 | - 0x40 -> 0x20 (mode 2) - don't trigger |
| 1450 | - 0x00 -> 0x40 - trigger only if LY==LYC |
| 1451 | */ |
| 1452 | if ( ! gb_lcd.mode_irq && ( ( gb_lcd.mode == 1 ) || |
| 1453 | ( ( LCDSTAT & 0x40 ) && ! ( data & 0x60 ) ) || |
| 1454 | ( ! ( LCDSTAT & 0x40 ) && ( data & 0x40 ) && ( LCDSTAT & 0x04 ) ) || |
| 1455 | ( ! ( LCDSTAT & 0x08 ) && ( data & 0x08 ) ) || |
| 1456 | ( ( LCDSTAT & 0x60 ) == 0x00 && ( data & 0x60 ) == 0x20 ) || |
| 1457 | ( ( LCDSTAT & 0x60 ) == 0x20 && ( data & 0x40 ) ) |
| 1458 | ) ) { |
| 1416 | 1459 | cpunum_set_input_line(0, LCD_INT, HOLD_LINE); |
| 1417 | 1460 | } |
| 1418 | | if ( ! ( LCDSTAT & 0x40 ) && ( data & 0x40 ) ) { |
| 1419 | | if ( LCDSTAT & 0x04 ) { |
| 1420 | | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| 1421 | | } |
| 1422 | | } |
| 1423 | 1461 | } |
| 1424 | 1462 | break; |
| 1425 | 1463 | case 0x04: /* LY - LCD Y-coordinate */ |
| r1406 | r1407 | |
| 1434 | 1472 | } |
| 1435 | 1473 | } else { |
| 1436 | 1474 | LCDSTAT &= 0xFB; |
| 1475 | gb_lcd.triggering_line_irq = 0; |
| 1437 | 1476 | } |
| 1438 | 1477 | } |
| 1439 | 1478 | break; |
| r1406 | r1407 | |
| 1482 | 1521 | static const UINT16 gbc_to_gb_pal[4] = {32767, 21140, 10570, 0}; |
| 1483 | 1522 | static UINT16 BP = 0, OP = 0; |
| 1484 | 1523 | |
| 1524 | gb_video_up_to_date(); |
| 1485 | 1525 | switch( offset ) { |
| 1486 | 1526 | case 0x00: /* LCDC - LCD Control */ |
| 1487 | 1527 | gb_chrgen = GBC_VRAMMap[0] + ((data & 0x10) ? 0x0000 : 0x0800); |
| r1406 | r1407 | |
| 1496 | 1536 | LCDSTAT &= ~0x03; |
| 1497 | 1537 | CURLINE = 0; |
| 1498 | 1538 | } |
| 1499 | | /* If LCD is being switched on */ |
| 1500 | | if ( !( LCDCONT & 0x80 ) && ( data & 0x80 ) ) { |
| 1539 | /* If LCD is being switched on */ |
| 1540 | if ( !( LCDCONT & 0x80 ) && ( data & 0x80 ) ) { |
| 1501 | 1541 | gb_lcd_switch_on(); |
| 1502 | | } |
| 1542 | } |
| 1503 | 1543 | break; |
| 1504 | 1544 | case 0x01: /* STAT - LCD Status */ |
| 1505 | 1545 | data = 0x80 | (data & 0x78) | (LCDSTAT & 0x07); |