src/mess/video/gb.c
| r1062 | r1063 | |
| 1134 | 1134 | gb_lcd.current_line = ( gb_lcd.current_line + 1 ) % 154; |
| 1135 | 1135 | if ( LCDCONT & 0x80 ) { |
| 1136 | 1136 | CURLINE = gb_lcd.current_line; |
| 1137 | | if ( CURLINE == CMPLINE ) { |
| 1138 | | LCDSTAT |= 0x04; |
| 1139 | | } else { |
| 1140 | | LCDSTAT &= 0xFB; |
| 1141 | | } |
| 1142 | | /* Generate lcd interrupt if requested */ |
| 1143 | | gb_lcd_int_check(); |
| 1144 | 1137 | } |
| 1145 | 1138 | if ( gb_lcd.current_line == 0 ) { |
| 1146 | 1139 | gb_lcd.window_lines_drawn = 0; |
| r1062 | r1063 | |
| 1185 | 1178 | } |
| 1186 | 1179 | |
| 1187 | 1180 | enum { |
| 1188 | | GB_LCD_STATE_LY00_M2=1, |
| 1189 | | GB_LCD_STATE_LYXX_M3, |
| 1181 | GB_LCD_STATE_LYXX_M3=1, |
| 1190 | 1182 | GB_LCD_STATE_LYXX_M0, |
| 1183 | GB_LCD_STATE_LYXX_M0_INC, |
| 1191 | 1184 | GB_LCD_STATE_LYXX_M2, |
| 1192 | 1185 | GB_LCD_STATE_LY9X_M1, |
| 1186 | GB_LCD_STATE_LY9X_M1_INC, |
| 1193 | 1187 | GB_LCD_STATE_LY00_M1, |
| 1188 | GB_LCD_STATE_LY00_M1_2, |
| 1194 | 1189 | GB_LCD_STATE_LY00_M0 |
| 1195 | 1190 | }; |
| 1196 | 1191 | |
| r1062 | r1063 | |
| 1214 | 1209 | /* Check for HBLANK DMA */ |
| 1215 | 1210 | if( gbc_hdma_enabled ) |
| 1216 | 1211 | gbc_hdma(0x10); |
| 1217 | | if ( CURLINE == 143 ) { |
| 1218 | | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(204 - 10 * gb_lcd.sprCount,0), GB_LCD_STATE_LY9X_M1, time_never ); |
| 1212 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(200 - 10 * gb_lcd.sprCount,0), GB_LCD_STATE_LYXX_M0_INC, time_never ); |
| 1213 | break; |
| 1214 | case GB_LCD_STATE_LYXX_M0_INC: /* Increment LY, stay in M0 for 4 more cycles */ |
| 1215 | gb_increment_scanline(); |
| 1216 | /* Reset LY==LYC STAT bit */ |
| 1217 | LCDSTAT &= 0xFB; |
| 1218 | /* Check if we're going into VBlank next */ |
| 1219 | if ( CURLINE == 144 ) { |
| 1220 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(4,0), GB_LCD_STATE_LY9X_M1, time_never ); |
| 1219 | 1221 | } else { |
| 1220 | | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(204 - 10 * gb_lcd.sprCount,0), GB_LCD_STATE_LYXX_M2, time_never ); |
| 1222 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(4,0), GB_LCD_STATE_LYXX_M2, time_never ); |
| 1221 | 1223 | } |
| 1222 | 1224 | break; |
| 1223 | 1225 | case GB_LCD_STATE_LYXX_M2: /* Switch to mode 2 */ |
| 1224 | | gb_increment_scanline(); |
| 1225 | 1226 | /* Set Mode 2 lcdstate */ |
| 1226 | 1227 | LCDSTAT = (LCDSTAT & 0xFC) | 0x02; |
| 1228 | /* Check if LY==LYC STAT bit should be set */ |
| 1229 | if ( CURLINE == CMPLINE ) |
| 1230 | LCDSTAT |= 0x04; |
| 1227 | 1231 | /* Generate lcd interrupt if requested */ |
| 1228 | 1232 | gb_lcd_int_check(); |
| 1229 | 1233 | /* Mode 2 last for approximately 80 clock cycles */ |
| 1230 | 1234 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(80,0), GB_LCD_STATE_LYXX_M3, time_never ); |
| 1231 | 1235 | break; |
| 1232 | | case GB_LCD_STATE_LY00_M2: /* on scanline 0, don't increment the current line counter */ |
| 1233 | | /* Set Mode 2 lcdstate */ |
| 1234 | | LCDSTAT = (LCDSTAT & 0xFC) | 0x02; |
| 1235 | | /* Generate lcd interrupt if requested */ |
| 1236 | | gb_lcd_int_check(); |
| 1237 | | /* Mode 2 last for approximately 80 clock cycles */ |
| 1238 | | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(76,0), GB_LCD_STATE_LYXX_M3, time_never ); |
| 1239 | | break; |
| 1240 | 1236 | case GB_LCD_STATE_LYXX_M3: /* Switch to mode 3 */ |
| 1241 | 1237 | gb_select_sprites(); |
| 1242 | 1238 | /* Set Mode 3 lcdstate */ |
| r1062 | r1063 | |
| 1247 | 1243 | gb_lcd.start_x = 0; |
| 1248 | 1244 | break; |
| 1249 | 1245 | case GB_LCD_STATE_LY9X_M1: /* Switch to or stay in mode 1 */ |
| 1250 | | gb_increment_scanline(); |
| 1251 | 1246 | if ( CURLINE == 144 ) { |
| 1252 | 1247 | /* Trigger VBlank interrupt generation */ |
| 1253 | 1248 | mame_timer_adjust( gb_lcd.vblank_delay_timer, MAME_TIME_IN_CYCLES(1,0), 0, time_never ); |
| r1062 | r1063 | |
| 1256 | 1251 | /* Trigger LCD interrupt if requested */ |
| 1257 | 1252 | gb_lcd_int_check(); |
| 1258 | 1253 | } |
| 1259 | | switch( gb_lcd.current_line ) { |
| 1260 | | case 153: |
| 1261 | | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(6,0), GB_LCD_STATE_LY00_M1, time_never ); |
| 1262 | | break; |
| 1263 | | case 152: |
| 1264 | | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(452,0), GB_LCD_STATE_LY9X_M1, time_never ); |
| 1265 | | break; |
| 1266 | | default: |
| 1267 | | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(456,0), GB_LCD_STATE_LY9X_M1, time_never ); |
| 1254 | /* Check if LY==LYC STAT bit should be set */ |
| 1255 | if ( CURLINE == CMPLINE ) |
| 1256 | LCDSTAT |= 0x04; |
| 1257 | /* Trigger LCD interrupt if requested */ |
| 1258 | gb_lcd_int_check(); |
| 1259 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(452,0), GB_LCD_STATE_LY9X_M1_INC, time_never ); |
| 1260 | break; |
| 1261 | case GB_LCD_STATE_LY9X_M1_INC: /* Increment scanline counter */ |
| 1262 | gb_increment_scanline(); |
| 1263 | /* Reset LY==LYC STAT bit */ |
| 1264 | LCDSTAT &= 0xFB; |
| 1265 | if ( gb_lcd.current_line == 153 ) { |
| 1266 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(4,0), GB_LCD_STATE_LY00_M1, time_never ); |
| 1267 | } else { |
| 1268 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(4,0), GB_LCD_STATE_LY9X_M1, time_never ); |
| 1268 | 1269 | } |
| 1269 | 1270 | break; |
| 1270 | 1271 | case GB_LCD_STATE_LY00_M1: /* we stay in VBlank but current line counter should already be incremented */ |
| 1272 | /* Check LY=LYC for line #153 */ |
| 1273 | if ( CURLINE == CMPLINE ) { |
| 1274 | LCDSTAT |= 0x04; |
| 1275 | gb_lcd_int_check(); |
| 1276 | } |
| 1271 | 1277 | gb_increment_scanline(); |
| 1272 | | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(456-(2),0), GB_LCD_STATE_LY00_M0, time_never ); |
| 1278 | LCDSTAT &= 0xFB; |
| 1279 | /* This 8 cycle period still needs to be verified on hardware */ |
| 1280 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(8,0), GB_LCD_STATE_LY00_M1_2, time_never ); |
| 1273 | 1281 | break; |
| 1282 | case GB_LCD_STATE_LY00_M1_2: /* Rest of line #0 during VBlank */ |
| 1283 | if ( CURLINE == CMPLINE ) { |
| 1284 | LCDSTAT |= 0x04; |
| 1285 | gb_lcd_int_check(); |
| 1286 | } |
| 1287 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(444,0), GB_LCD_STATE_LY00_M0, time_never ); |
| 1288 | break; |
| 1274 | 1289 | case GB_LCD_STATE_LY00_M0: /* The STAT register seems to go to 0 for about 4 cycles */ |
| 1275 | 1290 | /* Set Mode 0 lcdstat */ |
| 1276 | 1291 | LCDSTAT = ( LCDSTAT & 0xFC ); |
| 1277 | 1292 | gb_lcd_int_check(); |
| 1278 | | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(4,0), GB_LCD_STATE_LY00_M2, time_never ); |
| 1293 | mame_timer_adjust( gb_lcd.lcd_timer, MAME_TIME_IN_CYCLES(4,0), GB_LCD_STATE_LYXX_M2, time_never ); |
| 1279 | 1294 | break; |
| 1280 | | |
| 1281 | 1295 | } |
| 1282 | 1296 | } else { |
| 1283 | 1297 | gb_increment_scanline(); |
| r1062 | r1063 | |
| 1303 | 1317 | } |
| 1304 | 1318 | |
| 1305 | 1319 | READ8_HANDLER( gb_video_r ) { |
| 1306 | | if ( offset == 0x01 || offset == 0x04 ) { |
| 1307 | | if ( 0 && activecpu_get_pc() > 0x100 ) { |
| 1308 | | logerror("LCDSTAT/LY read, cycles left is %d\n", (int)MAME_TIME_TO_CYCLES(0,mame_timer_timeleft( gb_lcd.lcd_timer )) ); |
| 1309 | | } |
| 1310 | | if ( offset == 0x01 || (int)MAME_TIME_TO_CYCLES(0,mame_timer_timeleft( gb_lcd.lcd_timer)) < 0 ) |
| 1320 | // if ( 0 && activecpu_get_pc() > 0x100 ) { |
| 1321 | // logerror("LCDSTAT/LY read, cycles left is %d\n", (int)MAME_TIME_TO_CYCLES(0,mame_timer_timeleft( gb_lcd.lcd_timer )) ); |
| 1322 | // } |
| 1323 | if ( (int)MAME_TIME_TO_CYCLES(0,mame_timer_timeleft( gb_lcd.lcd_timer)) <= 0 ) |
| 1311 | 1324 | mame_timer_set_global_time(mame_timer_get_time()); |
| 1312 | | } |
| 1313 | 1325 | return gb_vid_regs[offset]; |
| 1314 | 1326 | } |
| 1315 | 1327 | |
| 1316 | 1328 | /* Ignore write when LCD is on and STAT is 02 or 03 */ |
| 1317 | 1329 | int gb_video_oam_locked( void ) { |
| 1330 | if ( (int)MAME_TIME_TO_CYCLES(0,mame_timer_timeleft( gb_lcd.lcd_timer)) <= 0 ) |
| 1331 | mame_timer_set_global_time(mame_timer_get_time()); |
| 1318 | 1332 | if ( ( LCDCONT & 0x80 ) && ( LCDSTAT & 0x02 ) ) { |
| 1319 | 1333 | return 1; |
| 1320 | 1334 | } |
| r1062 | r1063 | |
| 1323 | 1337 | |
| 1324 | 1338 | /* Ignore write when LCD is on and STAT is not 03 */ |
| 1325 | 1339 | int gb_video_vram_locked( void ) { |
| 1340 | if ( (int)MAME_TIME_TO_CYCLES(0,mame_timer_timeleft( gb_lcd.lcd_timer)) <= 0 ) |
| 1341 | mame_timer_set_global_time(mame_timer_get_time()); |
| 1326 | 1342 | if ( ( LCDCONT & 0x80 ) && ( ( LCDSTAT & 0x03 ) == 0x03 ) ) { |
| 1327 | 1343 | return 1; |
| 1328 | 1344 | } |