src/mess/machine/sms.c
| r4402 | r4403 | |
| 13 | 13 | #define CF_KOREAN_MAPPER 0x02 |
| 14 | 14 | #define CF_93C46_EEPROM 0x04 |
| 15 | 15 | #define CF_ONCART_RAM 0x08 |
| 16 | #define CF_GG_SMS_MODE 0x10 |
| 16 | 17 | |
| 17 | 18 | #define MAX_CARTRIDGES 16 |
| 18 | 19 | |
| r4402 | r4403 | |
| 57 | 58 | static UINT8 sports_pad_2_x; |
| 58 | 59 | static UINT8 sports_pad_2_y; |
| 59 | 60 | |
| 60 | | static struct { |
| 61 | static struct |
| 62 | { |
| 61 | 63 | UINT8 *ROM; /* Pointer to ROM image data */ |
| 62 | 64 | UINT32 size; /* Size of the ROM image */ |
| 63 | 65 | UINT8 features; /* on-cartridge special hardware */ |
| r4402 | r4403 | |
| 71 | 73 | |
| 72 | 74 | static void setup_rom(const address_space *space); |
| 73 | 75 | |
| 74 | | static TIMER_CALLBACK( rapid_fire_callback ) { |
| 76 | |
| 77 | static TIMER_CALLBACK( rapid_fire_callback ) |
| 78 | { |
| 75 | 79 | rapid_fire_state_1 ^= 0xFF; |
| 76 | 80 | rapid_fire_state_2 ^= 0xFF; |
| 77 | 81 | } |
| 78 | 82 | |
| 79 | | static WRITE8_HANDLER( sms_input_write ) { |
| 80 | | switch( offset ) { |
| 83 | |
| 84 | static WRITE8_HANDLER( sms_input_write ) |
| 85 | { |
| 86 | switch( offset ) |
| 87 | { |
| 81 | 88 | case 0: |
| 82 | | switch( input_port_read(space->machine, "CTRLSEL") & 0x0F ) { |
| 89 | switch( input_port_read(space->machine, "CTRLSEL") & 0x0F ) |
| 90 | { |
| 83 | 91 | case 0x03: /* Sports Pad */ |
| 84 | | if ( data != sports_pad_last_data_1 ) { |
| 92 | if ( data != sports_pad_last_data_1 ) |
| 93 | { |
| 85 | 94 | UINT32 cpu_cycles = cpu_get_total_cycles(space->cpu); |
| 86 | 95 | |
| 87 | 96 | sports_pad_last_data_1 = data; |
| 88 | | if ( cpu_cycles - last_sports_pad_time_1 > 512 ) { |
| 97 | if ( cpu_cycles - last_sports_pad_time_1 > 512 ) |
| 98 | { |
| 89 | 99 | sports_pad_state_1 = 3; |
| 90 | 100 | sports_pad_1_x = input_port_read(space->machine, "SPORT0"); |
| 91 | 101 | sports_pad_1_y = input_port_read(space->machine, "SPORT1"); |
| r4402 | r4403 | |
| 96 | 106 | break; |
| 97 | 107 | } |
| 98 | 108 | break; |
| 109 | |
| 99 | 110 | case 1: |
| 100 | | switch( input_port_read(space->machine, "CTRLSEL") >> 4 ) { |
| 111 | switch( input_port_read(space->machine, "CTRLSEL") >> 4 ) |
| 112 | { |
| 101 | 113 | case 0x03: /* Sports Pad */ |
| 102 | | if ( data != sports_pad_last_data_2 ) { |
| 114 | if ( data != sports_pad_last_data_2 ) |
| 115 | { |
| 103 | 116 | UINT32 cpu_cycles = cpu_get_total_cycles(space->cpu); |
| 104 | 117 | |
| 105 | 118 | sports_pad_last_data_2 = data; |
| 106 | | if ( cpu_cycles - last_sports_pad_time_2 > 2048 ) { |
| 119 | if ( cpu_cycles - last_sports_pad_time_2 > 2048 ) |
| 120 | { |
| 107 | 121 | sports_pad_state_2 = 3; |
| 108 | 122 | sports_pad_2_x = input_port_read(space->machine, "SPORT2"); |
| 109 | 123 | sports_pad_2_y = input_port_read(space->machine, "SPORT3"); |
| r4402 | r4403 | |
| 117 | 131 | } |
| 118 | 132 | } |
| 119 | 133 | |
| 120 | | static void sms_get_inputs(const address_space *space) { |
| 134 | static void sms_get_inputs(const address_space *space) |
| 135 | { |
| 121 | 136 | UINT8 data = 0x00; |
| 122 | 137 | UINT32 cpu_cycles = cpu_get_total_cycles(space->cpu); |
| 123 | 138 | running_machine *machine = space->machine; |
| r4402 | r4403 | |
| 131 | 146 | } |
| 132 | 147 | |
| 133 | 148 | /* Player 1 */ |
| 134 | | switch( input_port_read(machine, "CTRLSEL") & 0x0F ) { |
| 149 | switch( input_port_read(machine, "CTRLSEL") & 0x0F ) |
| 150 | { |
| 135 | 151 | case 0x00: /* Joystick */ |
| 136 | 152 | data = input_port_read(machine, "PORT_DC"); |
| 137 | 153 | /* Rapid Fire setting for Button A */ |
| 138 | | if ( input_port_read(machine, "RFU") & 0x01 ) { |
| 154 | if ( input_port_read(machine, "RFU") & 0x01 ) |
| 155 | { |
| 139 | 156 | data = ( data & 0xEF ) | ( rapid_fire_state_1 & 0x10 ); |
| 140 | 157 | } |
| 141 | 158 | /* Check Rapid Fire setting for Button B */ |
| 142 | | if ( input_port_read(machine, "RFU") & 0x02 ) { |
| 159 | if ( input_port_read(machine, "RFU") & 0x02 ) |
| 160 | { |
| 143 | 161 | data = ( data & 0xDF ) | ( rapid_fire_state_1 & 0x20 ); |
| 144 | 162 | } |
| 145 | 163 | sms_input_port0 = ( sms_input_port0 & 0xC0 ) | ( data & 0x3F ); |
| 146 | 164 | break; |
| 165 | |
| 147 | 166 | case 0x01: /* Light Phaser */ |
| 148 | 167 | break; |
| 168 | |
| 149 | 169 | case 0x02: /* Paddle Control */ |
| 150 | 170 | /* Get button A state */ |
| 151 | 171 | data = input_port_read(machine, "PADDLE0"); |
| 152 | | if ( paddle_read_state ) { |
| 172 | if ( paddle_read_state ) |
| 173 | { |
| 153 | 174 | data = data >> 4; |
| 154 | 175 | } |
| 155 | 176 | sms_input_port0 = ( sms_input_port0 & 0xC0 ) | ( data & 0x0F ) | ( paddle_read_state & 0x20 ) |
| 156 | 177 | | ( ( input_port_read(machine, "CTRLIPT") & 0x02 ) << 3 ); |
| 157 | 178 | break; |
| 179 | |
| 158 | 180 | case 0x03: /* Sega Sports Pad */ |
| 159 | | switch( sports_pad_state_1 ) { |
| 181 | switch( sports_pad_state_1 ) |
| 182 | { |
| 160 | 183 | case 0: |
| 161 | 184 | data = ( sports_pad_1_x >> 4 ) & 0x0F; |
| 162 | 185 | break; |
| r4402 | r4403 | |
| 175 | 198 | } |
| 176 | 199 | |
| 177 | 200 | /* Player 2 */ |
| 178 | | switch( input_port_read(machine, "CTRLSEL") >> 4 ) { |
| 201 | switch( input_port_read(machine, "CTRLSEL") >> 4 ) |
| 202 | { |
| 179 | 203 | case 0x00: /* Joystick */ |
| 180 | 204 | data = input_port_read(machine, "PORT_DC"); |
| 181 | 205 | sms_input_port0 = ( sms_input_port0 & 0x3F ) | ( data & 0xC0 ); |
| 182 | 206 | data = input_port_read(machine, "PORT_DD"); |
| 183 | | if ( input_port_read(machine, "RFU") & 0x04 ) { |
| 207 | if ( input_port_read(machine, "RFU") & 0x04 ) |
| 208 | { |
| 184 | 209 | data = ( data & 0xFB ) | ( rapid_fire_state_2 & 0x04 ); |
| 185 | 210 | } |
| 186 | | if ( input_port_read(machine, "RFU") & 0x08 ) { |
| 211 | if ( input_port_read(machine, "RFU") & 0x08 ) |
| 212 | { |
| 187 | 213 | data = ( data & 0xF7 ) | ( rapid_fire_state_2 & 0x08 ); |
| 188 | 214 | } |
| 189 | 215 | sms_input_port1 = ( sms_input_port1 & 0xF0 ) | ( data & 0x0F ); |
| 190 | 216 | break; |
| 217 | |
| 191 | 218 | case 0x01: /* Light Phaser */ |
| 192 | 219 | break; |
| 220 | |
| 193 | 221 | case 0x02: /* Paddle Control */ |
| 194 | 222 | /* Get button A state */ |
| 195 | 223 | data = input_port_read(machine, "PADDLE1"); |
| 196 | | if ( paddle_read_state ) { |
| 224 | if ( paddle_read_state ) |
| 225 | { |
| 197 | 226 | data = data >> 4; |
| 198 | 227 | } |
| 199 | 228 | sms_input_port0 = ( sms_input_port0 & 0x3F ) | ( ( data & 0x03 ) << 6 ); |
| 200 | 229 | sms_input_port1 = ( sms_input_port1 & 0xF0 ) | ( ( data & 0x0C ) >> 2 ) | ( paddle_read_state & 0x08 ) |
| 201 | 230 | | ( ( input_port_read(machine, "CTRLIPT") & 0x20 ) >> 3 ); |
| 202 | 231 | break; |
| 232 | |
| 203 | 233 | case 0x03: /* Sega Sports Pad */ |
| 204 | | switch( sports_pad_state_2 ) { |
| 234 | switch( sports_pad_state_2 ) |
| 235 | { |
| 205 | 236 | case 0: |
| 206 | 237 | data = sports_pad_2_x & 0x0F; |
| 207 | 238 | break; |
| r4402 | r4403 | |
| 220 | 251 | } |
| 221 | 252 | } |
| 222 | 253 | |
| 223 | | WRITE8_HANDLER(sms_fm_detect_w) { |
| 224 | | if ( HAS_FM ) { |
| 254 | |
| 255 | WRITE8_HANDLER(sms_fm_detect_w) |
| 256 | { |
| 257 | if ( HAS_FM ) |
| 258 | { |
| 225 | 259 | smsFMDetect = (data & 0x01); |
| 226 | 260 | } |
| 227 | 261 | } |
| 228 | 262 | |
| 229 | | READ8_HANDLER(sms_fm_detect_r) { |
| 230 | | if ( HAS_FM ) { |
| 263 | |
| 264 | READ8_HANDLER(sms_fm_detect_r) |
| 265 | { |
| 266 | if ( HAS_FM ) |
| 267 | { |
| 231 | 268 | return smsFMDetect; |
| 232 | | } else { |
| 233 | | if ( biosPort & IO_CHIP ) { |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | if ( biosPort & IO_CHIP ) |
| 273 | { |
| 234 | 274 | return 0xFF; |
| 235 | | } else { |
| 275 | } |
| 276 | else |
| 277 | { |
| 236 | 278 | sms_get_inputs(space); |
| 237 | 279 | return sms_input_port0; |
| 238 | 280 | } |
| 239 | 281 | } |
| 240 | 282 | } |
| 241 | 283 | |
| 242 | | WRITE8_HANDLER(sms_version_w) { |
| 243 | | if ((data & 0x01) && (data & 0x04)) { |
| 284 | |
| 285 | WRITE8_HANDLER(sms_version_w) |
| 286 | { |
| 287 | if ((data & 0x01) && (data & 0x04)) |
| 288 | { |
| 244 | 289 | smsVersion = (data & 0xA0); |
| 245 | 290 | } |
| 246 | | if ( data & 0x08 ) { |
| 291 | if ( data & 0x08 ) |
| 292 | { |
| 247 | 293 | sms_input_write( space, 0, ( data & 0x20 ) >> 5 ); |
| 248 | 294 | } |
| 249 | | if ( data & 0x02 ) { |
| 295 | if ( data & 0x02 ) |
| 296 | { |
| 250 | 297 | sms_input_write( space, 1, ( data & 0x80 ) >> 7 ); |
| 251 | 298 | } |
| 252 | 299 | } |
| 253 | 300 | |
| 254 | | READ8_HANDLER(sms_version_r) { |
| 301 | |
| 302 | READ8_HANDLER(sms_version_r) |
| 303 | { |
| 255 | 304 | UINT8 temp; |
| 256 | 305 | |
| 257 | | if (biosPort & IO_CHIP) { |
| 306 | if (biosPort & IO_CHIP) |
| 307 | { |
| 258 | 308 | return (0xFF); |
| 259 | 309 | } |
| 260 | 310 | |
| r4402 | r4403 | |
| 262 | 312 | temp = (smsVersion & 0x80) | (smsVersion & 0x20) << 1; |
| 263 | 313 | |
| 264 | 314 | /* Inverse version detect value for Japanese machines */ |
| 265 | | if ( IS_REGION_JAPAN ) { |
| 315 | if ( IS_REGION_JAPAN ) |
| 316 | { |
| 266 | 317 | temp ^= 0xC0; |
| 267 | 318 | } |
| 268 | 319 | |
| r4402 | r4403 | |
| 273 | 324 | return (temp); |
| 274 | 325 | } |
| 275 | 326 | |
| 276 | | READ8_HANDLER(sms_count_r) { |
| 277 | | if ( offset & 0x01 ) { |
| 327 | |
| 328 | READ8_HANDLER(sms_count_r) |
| 329 | { |
| 330 | if ( offset & 0x01 ) |
| 331 | { |
| 278 | 332 | return sms_vdp_hcount_r(space, offset); |
| 279 | | } else { |
| 333 | } |
| 334 | else |
| 335 | { |
| 280 | 336 | /* VCount read */ |
| 281 | 337 | return sms_vdp_vcount_r(space, offset); |
| 282 | 338 | } |
| 283 | 339 | } |
| 284 | 340 | |
| 285 | | void sms_check_pause_button(const address_space *space) { |
| 286 | | if ( ! IS_GAMEGEAR ) { |
| 287 | | if ( ! (input_port_read(space->machine, "PAUSE") & 0x80) ) { |
| 288 | | if ( ! smsPaused ) { |
| 289 | | cpu_set_input_line(space->machine->cpu[0], INPUT_LINE_NMI, ASSERT_LINE ); |
| 290 | | cpu_set_input_line(space->machine->cpu[0], INPUT_LINE_NMI, CLEAR_LINE ); |
| 291 | | } |
| 292 | | smsPaused = 1; |
| 293 | | } else { |
| 294 | | smsPaused = 0; |
| 341 | |
| 342 | /* |
| 343 | Check if the pause button is pressed. |
| 344 | If the gamegear is in sms mode, check if the start button is pressed. |
| 345 | */ |
| 346 | void sms_check_pause_button(const address_space *space) |
| 347 | { |
| 348 | if ( IS_GAMEGEAR && ! ( sms_cartridge[sms_current_cartridge].features & CF_GG_SMS_MODE ) ) |
| 349 | { |
| 350 | return; |
| 351 | } |
| 352 | if ( ! (input_port_read(space->machine, IS_GAMEGEAR ? "START" : "PAUSE") & 0x80) ) { |
| 353 | if ( ! smsPaused ) { |
| 354 | cpu_set_input_line(space->machine->cpu[0], INPUT_LINE_NMI, PULSE_LINE ); |
| 295 | 355 | } |
| 356 | smsPaused = 1; |
| 296 | 357 | } |
| 358 | else |
| 359 | { |
| 360 | smsPaused = 0; |
| 361 | } |
| 297 | 362 | } |
| 298 | 363 | |
| 299 | | READ8_HANDLER(sms_input_port_0_r) { |
| 300 | | if (biosPort & IO_CHIP) { |
| 364 | |
| 365 | READ8_HANDLER(sms_input_port_0_r) |
| 366 | { |
| 367 | if (biosPort & IO_CHIP) |
| 368 | { |
| 301 | 369 | return (0xFF); |
| 302 | | } else { |
| 370 | } |
| 371 | else |
| 372 | { |
| 303 | 373 | sms_get_inputs(space); |
| 304 | 374 | return sms_input_port0; |
| 305 | 375 | } |
| 306 | 376 | } |
| 307 | 377 | |
| 378 | |
| 308 | 379 | WRITE8_HANDLER(sms_ym2413_register_port_0_w) |
| 309 | 380 | { |
| 310 | 381 | if ( HAS_FM ) |
| r4402 | r4403 | |
| 314 | 385 | } |
| 315 | 386 | } |
| 316 | 387 | |
| 388 | |
| 317 | 389 | WRITE8_HANDLER(sms_ym2413_data_port_0_w) |
| 318 | 390 | { |
| 319 | 391 | if ( HAS_FM ) |
| r4402 | r4403 | |
| 324 | 396 | } |
| 325 | 397 | } |
| 326 | 398 | |
| 327 | | READ8_HANDLER(gg_input_port_2_r) { |
| 399 | |
| 400 | READ8_HANDLER(gg_input_port_2_r) |
| 401 | { |
| 328 | 402 | //logerror("joy 2 read, val: %02x, pc: %04x\n", (( IS_REGION_JAPAN ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc()); |
| 329 | 403 | return (( IS_REGION_JAPAN ? 0x00 : 0x40 ) | (input_port_read(space->machine, "START") & 0x80)); |
| 330 | 404 | } |
| 331 | 405 | |
| 406 | |
| 332 | 407 | READ8_HANDLER(sms_mapper_r) |
| 333 | 408 | { |
| 334 | 409 | return sms_mapper[offset]; |
| 335 | 410 | } |
| 336 | 411 | |
| 412 | |
| 337 | 413 | WRITE8_HANDLER(sms_mapper_w) |
| 338 | 414 | { |
| 339 | 415 | int page; |
| r4402 | r4403 | |
| 347 | 423 | sms_mapper[offset] = data; |
| 348 | 424 | sms_mapper_ram[offset] = data; |
| 349 | 425 | |
| 350 | | if ( sms_cartridge[sms_current_cartridge].ROM ) { |
| 426 | if ( sms_cartridge[sms_current_cartridge].ROM ) |
| 427 | { |
| 351 | 428 | SOURCE_CART = sms_cartridge[sms_current_cartridge].ROM + ( ( rom_page_count > 0) ? data % rom_page_count : 0 ) * 0x4000; |
| 352 | | } else { |
| 429 | } |
| 430 | else |
| 431 | { |
| 353 | 432 | SOURCE_CART = sms_banking_none[1]; |
| 354 | 433 | } |
| 355 | | if ( BIOS ) { |
| 434 | if ( BIOS ) |
| 435 | { |
| 356 | 436 | SOURCE_BIOS = BIOS + ( (smsBiosPageCount > 0) ? data % smsBiosPageCount : 0 ) * 0x4000; |
| 357 | | } else { |
| 437 | } |
| 438 | else |
| 439 | { |
| 358 | 440 | SOURCE_BIOS = sms_banking_none[1]; |
| 359 | 441 | } |
| 360 | 442 | |
| r4402 | r4403 | |
| 381 | 463 | SOURCE = SOURCE_BIOS; |
| 382 | 464 | } |
| 383 | 465 | |
| 384 | | switch(offset) { |
| 466 | switch(offset) |
| 467 | { |
| 385 | 468 | case 0: /* Control */ |
| 386 | 469 | /* Is it ram or rom? */ |
| 387 | | if (data & 0x08) { /* it's ram */ |
| 470 | if (data & 0x08) /* it's ram */ |
| 471 | { |
| 388 | 472 | sms_cartridge[sms_current_cartridge].sram_save = 1; /* SRAM should be saved on exit. */ |
| 389 | | if (data & 0x04) { |
| 473 | if (data & 0x04) |
| 474 | { |
| 390 | 475 | LOG(("ram 1 paged.\n")); |
| 391 | 476 | SOURCE = sms_cartridge[sms_current_cartridge].cartSRAM + 0x4000; |
| 392 | | } else { |
| 477 | } |
| 478 | else |
| 479 | { |
| 393 | 480 | LOG(("ram 0 paged.\n")); |
| 394 | 481 | SOURCE = sms_cartridge[sms_current_cartridge].cartSRAM; |
| 395 | 482 | } |
| 396 | 483 | memory_set_bankptr(space->machine, 4, SOURCE ); |
| 397 | 484 | memory_set_bankptr(space->machine, 5, SOURCE + 0x2000 ); |
| 398 | | } else { /* it's rom */ |
| 399 | | if ( biosPort & IO_BIOS_ROM || ! HAS_BIOS ) { |
| 485 | } |
| 486 | else /* it's rom */ |
| 487 | { |
| 488 | if ( biosPort & IO_BIOS_ROM || ! HAS_BIOS ) |
| 489 | { |
| 400 | 490 | page = ( rom_page_count > 0) ? sms_mapper[3] % rom_page_count : 0; |
| 401 | 491 | SOURCE = sms_banking_cart[4]; |
| 402 | | } else { |
| 492 | } |
| 493 | else |
| 494 | { |
| 403 | 495 | page = (smsBiosPageCount > 0) ? sms_mapper[3] % smsBiosPageCount : 0; |
| 404 | 496 | SOURCE = sms_banking_bios[4]; |
| 405 | 497 | } |
| r4402 | r4403 | |
| 408 | 500 | memory_set_bankptr(space->machine, 5, SOURCE + 0x2000 ); |
| 409 | 501 | } |
| 410 | 502 | break; |
| 503 | |
| 411 | 504 | case 1: /* Select 16k ROM bank for 0400-3FFF */ |
| 412 | 505 | LOG(("rom 0 paged in %x.\n", page)); |
| 413 | 506 | sms_banking_bios[2] = SOURCE_BIOS + 0x0400; |
| 414 | 507 | sms_banking_cart[2] = SOURCE_CART + 0x0400; |
| 415 | | if ( IS_GAMEGEAR ) { |
| 508 | if ( IS_GAMEGEAR ) |
| 509 | { |
| 416 | 510 | SOURCE = SOURCE_CART; |
| 417 | 511 | } |
| 418 | 512 | memory_set_bankptr(space->machine, 2, SOURCE + 0x0400 ); |
| 419 | 513 | break; |
| 514 | |
| 420 | 515 | case 2: /* Select 16k ROM bank for 4000-7FFF */ |
| 421 | 516 | LOG(("rom 1 paged in %x.\n", page)); |
| 422 | 517 | sms_banking_bios[3] = SOURCE_BIOS; |
| 423 | 518 | sms_banking_cart[3] = SOURCE_CART; |
| 424 | | if ( IS_GAMEGEAR ) { |
| 519 | if ( IS_GAMEGEAR ) |
| 520 | { |
| 425 | 521 | SOURCE = SOURCE_CART; |
| 426 | 522 | } |
| 427 | 523 | memory_set_bankptr(space->machine, 3, SOURCE ); |
| 428 | 524 | break; |
| 525 | |
| 429 | 526 | case 3: /* Select 16k ROM bank for 8000-BFFF */ |
| 430 | 527 | sms_banking_bios[4] = SOURCE_BIOS; |
| 431 | | if ( IS_GAMEGEAR ) { |
| 528 | if ( IS_GAMEGEAR ) |
| 529 | { |
| 432 | 530 | SOURCE = SOURCE_CART; |
| 433 | 531 | } |
| 434 | | if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) { |
| 435 | | if ( SOURCE == SOURCE_CART ) { |
| 532 | if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) |
| 533 | { |
| 534 | if ( SOURCE == SOURCE_CART ) |
| 535 | { |
| 436 | 536 | SOURCE = sms_banking_cart[4]; |
| 437 | 537 | } |
| 438 | | } else { |
| 538 | } |
| 539 | else |
| 540 | { |
| 439 | 541 | sms_banking_cart[4] = SOURCE_CART; |
| 440 | 542 | } |
| 441 | | if ( ! ( sms_mapper[0] & 0x08 ) ) { /* is RAM disabled? */ |
| 543 | if ( ! ( sms_mapper[0] & 0x08 ) ) /* is RAM disabled? */ |
| 544 | { |
| 442 | 545 | LOG(("rom 2 paged in %x.\n", page)); |
| 443 | 546 | memory_set_bankptr(space->machine, 4, SOURCE ); |
| 444 | 547 | memory_set_bankptr(space->machine, 5, SOURCE + 0x2000 ); |
| r4402 | r4403 | |
| 447 | 550 | } |
| 448 | 551 | } |
| 449 | 552 | |
| 450 | | static WRITE8_HANDLER(sms_codemasters_page0_w) { |
| 451 | | if ( sms_cartridge[sms_current_cartridge].ROM && sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) { |
| 553 | |
| 554 | static WRITE8_HANDLER(sms_codemasters_page0_w) |
| 555 | { |
| 556 | if ( sms_cartridge[sms_current_cartridge].ROM && sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) |
| 557 | { |
| 452 | 558 | UINT8 rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000; |
| 453 | 559 | sms_banking_cart[1] = sms_cartridge[sms_current_cartridge].ROM + ( ( rom_page_count > 0) ? data % rom_page_count : 0 ) * 0x4000; |
| 454 | 560 | sms_banking_cart[2] = sms_banking_cart[1] + 0x0400; |
| r4402 | r4403 | |
| 457 | 563 | } |
| 458 | 564 | } |
| 459 | 565 | |
| 460 | | static WRITE8_HANDLER(sms_codemasters_page1_w) { |
| 461 | | if ( sms_cartridge[sms_current_cartridge].ROM && sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) { |
| 566 | |
| 567 | static WRITE8_HANDLER(sms_codemasters_page1_w) |
| 568 | { |
| 569 | if ( sms_cartridge[sms_current_cartridge].ROM && sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) |
| 570 | { |
| 462 | 571 | /* Check if we need to switch in some RAM */ |
| 463 | | if ( data & 0x80 ) { |
| 572 | if ( data & 0x80 ) |
| 573 | { |
| 464 | 574 | sms_cartridge[sms_current_cartridge].ram_page = data & 0x07; |
| 465 | 575 | memory_set_bankptr(space->machine, 5, sms_cartridge[sms_current_cartridge].cartRAM + sms_cartridge[sms_current_cartridge].ram_page * 0x2000 ); |
| 466 | | } else { |
| 576 | } |
| 577 | else |
| 578 | { |
| 467 | 579 | UINT8 rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000; |
| 468 | 580 | sms_banking_cart[3] = sms_cartridge[sms_current_cartridge].ROM + ( ( rom_page_count > 0) ? data % rom_page_count : 0 ) * 0x4000; |
| 469 | 581 | memory_set_bankptr(space->machine, 3, sms_banking_cart[3] ); |
| r4402 | r4403 | |
| 472 | 584 | } |
| 473 | 585 | } |
| 474 | 586 | |
| 475 | | WRITE8_HANDLER(sms_bios_w) { |
| 587 | |
| 588 | WRITE8_HANDLER(sms_bios_w) |
| 589 | { |
| 476 | 590 | biosPort = data; |
| 477 | 591 | |
| 478 | 592 | logerror("bios write %02x, pc: %04x\n", data, cpu_get_pc(space->cpu)); |
| r4402 | r4403 | |
| 480 | 594 | setup_rom(space); |
| 481 | 595 | } |
| 482 | 596 | |
| 483 | | WRITE8_HANDLER(sms_cartram2_w) { |
| 484 | | if (sms_mapper[0] & 0x08) { |
| 597 | |
| 598 | WRITE8_HANDLER(sms_cartram2_w) |
| 599 | { |
| 600 | if (sms_mapper[0] & 0x08) |
| 601 | { |
| 485 | 602 | logerror("write %02X to cartram at offset #%04X\n", data, offset + 0x2000); |
| 486 | | if (sms_mapper[0] & 0x04) { |
| 603 | if (sms_mapper[0] & 0x04) |
| 604 | { |
| 487 | 605 | sms_cartridge[sms_current_cartridge].cartSRAM[offset + 0x6000] = data; |
| 488 | | } else { |
| 606 | } |
| 607 | else |
| 608 | { |
| 489 | 609 | sms_cartridge[sms_current_cartridge].cartSRAM[offset + 0x2000] = data; |
| 490 | 610 | } |
| 491 | 611 | } |
| 492 | | if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) { |
| 612 | if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) |
| 613 | { |
| 493 | 614 | sms_cartridge[sms_current_cartridge].cartRAM[sms_cartridge[sms_current_cartridge].ram_page * 0x2000 + offset] = data; |
| 494 | 615 | } |
| 495 | | if ( sms_cartridge[sms_current_cartridge].features & CF_KOREAN_MAPPER && offset == 0 ) { /* Dodgeball King mapper */ |
| 616 | if ( sms_cartridge[sms_current_cartridge].features & CF_KOREAN_MAPPER && offset == 0 ) /* Dodgeball King mapper */ |
| 617 | { |
| 496 | 618 | UINT8 rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000; |
| 497 | 619 | int page = (rom_page_count > 0) ? data % rom_page_count : 0; |
| 498 | 620 | if ( ! sms_cartridge[sms_current_cartridge].ROM ) |
| r4402 | r4403 | |
| 504 | 626 | } |
| 505 | 627 | } |
| 506 | 628 | |
| 507 | | WRITE8_HANDLER(sms_cartram_w) { |
| 629 | |
| 630 | WRITE8_HANDLER(sms_cartram_w) |
| 631 | { |
| 508 | 632 | int page; |
| 509 | 633 | |
| 510 | | if (sms_mapper[0] & 0x08) { |
| 634 | if (sms_mapper[0] & 0x08) |
| 635 | { |
| 511 | 636 | logerror("write %02X to cartram at offset #%04X\n", data, offset); |
| 512 | | if (sms_mapper[0] & 0x04) { |
| 637 | if (sms_mapper[0] & 0x04) |
| 638 | { |
| 513 | 639 | sms_cartridge[sms_current_cartridge].cartSRAM[offset + 0x4000] = data; |
| 514 | | } else { |
| 640 | } |
| 641 | else |
| 642 | { |
| 515 | 643 | sms_cartridge[sms_current_cartridge].cartSRAM[offset] = data; |
| 516 | 644 | } |
| 517 | | } else { |
| 518 | | if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0 ) { /* Codemasters mapper */ |
| 645 | } |
| 646 | else |
| 647 | { |
| 648 | if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0 ) /* Codemasters mapper */ |
| 649 | { |
| 519 | 650 | UINT8 rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000; |
| 520 | 651 | page = (rom_page_count > 0) ? data % rom_page_count : 0; |
| 521 | 652 | if ( ! sms_cartridge[sms_current_cartridge].ROM ) |
| r4402 | r4403 | |
| 524 | 655 | memory_set_bankptr(space->machine, 4, sms_banking_cart[4] ); |
| 525 | 656 | memory_set_bankptr(space->machine, 5, sms_banking_cart[4] + 0x2000 ); |
| 526 | 657 | LOG(("rom 2 paged in %x codemasters.\n", page)); |
| 527 | | } else if ( sms_cartridge[sms_current_cartridge].features & CF_ONCART_RAM ) { |
| 658 | } |
| 659 | else if ( sms_cartridge[sms_current_cartridge].features & CF_ONCART_RAM ) |
| 660 | { |
| 528 | 661 | sms_cartridge[sms_current_cartridge].cartRAM[offset & ( sms_cartridge[sms_current_cartridge].ram_size - 1 ) ] = data; |
| 529 | | } else { |
| 662 | } |
| 663 | else |
| 664 | { |
| 530 | 665 | logerror("INVALID write %02X to cartram at offset #%04X\n", data, offset); |
| 531 | 666 | } |
| 532 | 667 | } |
| 533 | 668 | } |
| 534 | 669 | |
| 535 | | WRITE8_HANDLER(gg_sio_w) { |
| 670 | |
| 671 | WRITE8_HANDLER(gg_sio_w) |
| 672 | { |
| 536 | 673 | logerror("*** write %02X to SIO register #%d\n", data, offset); |
| 537 | 674 | |
| 538 | 675 | ggSIO[offset & 0x07] = data; |
| 539 | | switch(offset & 7) { |
| 676 | switch(offset & 7) |
| 677 | { |
| 540 | 678 | case 0x00: /* Parallel Data */ |
| 541 | 679 | break; |
| 680 | |
| 542 | 681 | case 0x01: /* Data Direction/ NMI Enable */ |
| 543 | 682 | break; |
| 683 | |
| 544 | 684 | case 0x02: /* Serial Output */ |
| 545 | 685 | break; |
| 686 | |
| 546 | 687 | case 0x03: /* Serial Input */ |
| 547 | 688 | break; |
| 689 | |
| 548 | 690 | case 0x04: /* Serial Control / Status */ |
| 549 | 691 | break; |
| 550 | 692 | } |
| 551 | 693 | } |
| 552 | 694 | |
| 553 | | READ8_HANDLER(gg_sio_r) { |
| 695 | |
| 696 | READ8_HANDLER(gg_sio_r) |
| 697 | { |
| 554 | 698 | logerror("*** read SIO register #%d\n", offset); |
| 555 | 699 | |
| 556 | | switch(offset & 7) { |
| 700 | switch(offset & 7) |
| 701 | { |
| 557 | 702 | case 0x00: /* Parallel Data */ |
| 558 | 703 | break; |
| 704 | |
| 559 | 705 | case 0x01: /* Data Direction/ NMI Enable */ |
| 560 | 706 | break; |
| 707 | |
| 561 | 708 | case 0x02: /* Serial Output */ |
| 562 | 709 | break; |
| 710 | |
| 563 | 711 | case 0x03: /* Serial Input */ |
| 564 | 712 | break; |
| 713 | |
| 565 | 714 | case 0x04: /* Serial Control / Status */ |
| 566 | 715 | break; |
| 567 | 716 | } |
| r4402 | r4403 | |
| 569 | 718 | return ggSIO[offset]; |
| 570 | 719 | } |
| 571 | 720 | |
| 572 | | READ8_HANDLER(gg_psg_r) { |
| 721 | |
| 722 | READ8_HANDLER(gg_psg_r) |
| 723 | { |
| 573 | 724 | return 0xFF; |
| 574 | 725 | } |
| 575 | 726 | |
| 576 | | WRITE8_HANDLER(gg_psg_w) { |
| 727 | |
| 728 | WRITE8_HANDLER(gg_psg_w) |
| 729 | { |
| 577 | 730 | logerror("write %02X to psg at offset #%d.\n",data , offset); |
| 578 | 731 | |
| 579 | 732 | /* D7 = Noise Left */ |
| r4402 | r4403 | |
| 587 | 740 | /* D0 = Tone1 Right */ |
| 588 | 741 | } |
| 589 | 742 | |
| 590 | | static void sms_machine_stop(running_machine *machine) { |
| 743 | |
| 744 | static void sms_machine_stop(running_machine *machine) |
| 745 | { |
| 591 | 746 | /* Does the cartridge have SRAM that should be saved? */ |
| 592 | 747 | if ( sms_cartridge[sms_current_cartridge].sram_save ) |
| 593 | 748 | image_battery_save(devtag_get_device(machine, CARTSLOT, "cart1"), sms_cartridge[sms_current_cartridge].cartSRAM, sizeof(UINT8) * NVRAM_SIZE ); |
| 594 | 749 | } |
| 595 | 750 | |
| 751 | |
| 596 | 752 | static void setup_rom(const address_space *space) |
| 597 | 753 | { |
| 598 | 754 | running_machine *machine = space->machine; |
| r4402 | r4403 | |
| 605 | 761 | memory_set_bankptr(machine, 5, sms_banking_none[4] + 0x2000 ); |
| 606 | 762 | |
| 607 | 763 | /* 2. check and set up expansion port */ |
| 608 | | if (!(biosPort & IO_EXPANSION) && (biosPort & IO_CARTRIDGE) && (biosPort & IO_CARD)) { |
| 764 | if (!(biosPort & IO_EXPANSION) && (biosPort & IO_CARTRIDGE) && (biosPort & IO_CARD)) |
| 765 | { |
| 609 | 766 | /* TODO: Implement me */ |
| 610 | 767 | logerror( "Switching to unsupported expansion port.\n" ); |
| 611 | 768 | } |
| 612 | 769 | |
| 613 | 770 | /* 3. check and set up card rom */ |
| 614 | | if (!(biosPort & IO_CARD) && (biosPort & IO_CARTRIDGE) && (biosPort & IO_EXPANSION)) { |
| 771 | if (!(biosPort & IO_CARD) && (biosPort & IO_CARTRIDGE) && (biosPort & IO_EXPANSION)) |
| 772 | { |
| 615 | 773 | /* TODO: Implement me */ |
| 616 | 774 | logerror( "Switching to unsupported card rom port.\n" ); |
| 617 | 775 | } |
| r4402 | r4403 | |
| 619 | 777 | /* 4. check and set up cartridge rom */ |
| 620 | 778 | /* if ( ( !(biosPort & IO_CARTRIDGE) && (biosPort & IO_EXPANSION) && (biosPort & IO_CARD) ) || IS_GAMEGEAR ) { */ |
| 621 | 779 | /* Out Run Europa initially writes a value to port 3E where IO_CARTRIDGE, IO_EXPANSION and IO_CARD are reset */ |
| 622 | | if ( ( ! ( biosPort & IO_CARTRIDGE ) ) || IS_GAMEGEAR ) { |
| 780 | if ( ( ! ( biosPort & IO_CARTRIDGE ) ) || IS_GAMEGEAR ) |
| 781 | { |
| 623 | 782 | memory_set_bankptr(machine, 1, sms_banking_cart[1] ); |
| 624 | 783 | memory_set_bankptr(machine, 2, sms_banking_cart[2] ); |
| 625 | 784 | memory_set_bankptr(machine, 3, sms_banking_cart[3] ); |
| r4402 | r4403 | |
| 629 | 788 | } |
| 630 | 789 | |
| 631 | 790 | /* 5. check and set up bios rom */ |
| 632 | | if ( !(biosPort & IO_BIOS_ROM) ) { |
| 791 | if ( !(biosPort & IO_BIOS_ROM) ) |
| 792 | { |
| 633 | 793 | /* 0x0400 bioses */ |
| 634 | | if ( HAS_BIOS_0400 ) { |
| 794 | if ( HAS_BIOS_0400 ) |
| 795 | { |
| 635 | 796 | memory_set_bankptr(machine, 1, sms_banking_bios[1] ); |
| 636 | 797 | logerror( "Switched in 0x0400 bios.\n" ); |
| 637 | 798 | } |
| 638 | 799 | /* 0x2000 bioses */ |
| 639 | | if ( HAS_BIOS_2000 ) { |
| 800 | if ( HAS_BIOS_2000 ) |
| 801 | { |
| 640 | 802 | memory_set_bankptr(machine, 1, sms_banking_bios[1] ); |
| 641 | 803 | memory_set_bankptr(machine, 2, sms_banking_bios[2] ); |
| 642 | 804 | logerror( "Switched in 0x2000 bios.\n" ); |
| 643 | 805 | } |
| 644 | | if ( HAS_BIOS_FULL ) { |
| 806 | if ( HAS_BIOS_FULL ) |
| 807 | { |
| 645 | 808 | memory_set_bankptr(machine, 1, sms_banking_bios[1] ); |
| 646 | 809 | memory_set_bankptr(machine, 2, sms_banking_bios[2] ); |
| 647 | 810 | memory_set_bankptr(machine, 3, sms_banking_bios[3] ); |
| r4402 | r4403 | |
| 651 | 814 | } |
| 652 | 815 | } |
| 653 | 816 | |
| 654 | | if ( sms_cartridge[sms_current_cartridge].features & CF_ONCART_RAM ) { |
| 817 | if ( sms_cartridge[sms_current_cartridge].features & CF_ONCART_RAM ) |
| 818 | { |
| 655 | 819 | memory_set_bankptr(machine, 4, sms_cartridge[sms_current_cartridge].cartRAM ); |
| 656 | 820 | memory_set_bankptr(machine, 5, sms_cartridge[sms_current_cartridge].cartRAM ); |
| 657 | 821 | } |
| 658 | 822 | } |
| 659 | 823 | |
| 660 | | static int sms_verify_cart(UINT8 *magic, int size) { |
| 824 | |
| 825 | static int sms_verify_cart(UINT8 *magic, int size) |
| 826 | { |
| 661 | 827 | int retval; |
| 662 | 828 | |
| 663 | 829 | retval = IMAGE_VERIFY_FAIL; |
| 664 | 830 | |
| 665 | 831 | /* Verify the file is a valid image - check $7ff0 for "TMR SEGA" */ |
| 666 | | if (size >= 0x8000) { |
| 667 | | if (!strncmp((char*)&magic[0x7FF0], "TMR SEGA", 8)) { |
| 832 | if (size >= 0x8000) |
| 833 | { |
| 834 | if (!strncmp((char*)&magic[0x7FF0], "TMR SEGA", 8)) |
| 835 | { |
| 668 | 836 | /* Technically, it should be this, but remove for now until verified: |
| 669 | | if (!strcmp(sysname, "gamegear")) { |
| 837 | if (!strcmp(sysname, "gamegear")) |
| 838 | { |
| 670 | 839 | if ((unsigned char)magic[0x7FFD] < 0x50) |
| 671 | 840 | retval = IMAGE_VERIFY_PASS; |
| 672 | 841 | } |
| 673 | | if (!strcmp(sysname, "sms")) { |
| 842 | if (!strcmp(sysname, "sms")) |
| 843 | { |
| 674 | 844 | if ((unsigned char)magic[0x7FFD] >= 0x50) |
| 675 | 845 | retval = IMAGE_VERIFY_PASS; |
| 676 | 846 | } |
| r4402 | r4403 | |
| 715 | 885 | |
| 716 | 886 | The Korean game Jang Pung II also seems to use a codemasters style mapper. |
| 717 | 887 | */ |
| 718 | | static int detect_codemasters_mapper( UINT8 *rom ) { |
| 888 | static int detect_codemasters_mapper( UINT8 *rom ) |
| 889 | { |
| 719 | 890 | static const UINT8 jang_pung2[16] = { 0x00, 0xBA, 0x38, 0x0D, 0x00, 0xB8, 0x38, 0x0C, 0x00, 0xB6, 0x38, 0x0B, 0x00, 0xB4, 0x38, 0x0A }; |
| 720 | 891 | |
| 721 | 892 | if ( ( ( rom[0x7fe0] & 0x0F ) <= 9 ) && |
| 722 | 893 | ( rom[0x7fe3] == 0x93 || rom[0x7fe3] == 0x94 || rom[0x7fe3] == 0x95 ) && |
| 723 | | rom[0x7fef] == 0x00 ) { |
| 894 | rom[0x7fef] == 0x00 ) |
| 895 | { |
| 724 | 896 | return 1; |
| 725 | 897 | } |
| 726 | 898 | |
| 727 | | if ( ! memcmp( &rom[0x7ff0], jang_pung2, 16 ) ) { |
| 899 | if ( ! memcmp( &rom[0x7ff0], jang_pung2, 16 ) ) |
| 900 | { |
| 728 | 901 | return 1; |
| 729 | 902 | } |
| 730 | 903 | |
| 731 | 904 | return 0; |
| 732 | 905 | } |
| 733 | 906 | |
| 734 | | static int detect_korean_mapper( UINT8 *rom ) { |
| 735 | | static const UINT8 signatures[2][16] = { |
| 907 | |
| 908 | static int detect_korean_mapper( UINT8 *rom ) |
| 909 | { |
| 910 | static const UINT8 signatures[2][16] = |
| 911 | { |
| 736 | 912 | { 0x3E, 0x11, 0x32, 0x00, 0xA0, 0x78, 0xCD, 0x84, 0x85, 0x3E, 0x02, 0x32, 0x00, 0xA0, 0xC9, 0xFF }, /* Dodgeball King */ |
| 737 | 913 | { 0x41, 0x48, 0x37, 0x37, 0x44, 0x37, 0x4E, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20 }, /* Sangokushi 3 */ |
| 738 | 914 | }; |
| 739 | 915 | int i; |
| 740 | 916 | |
| 741 | | for( i = 0; i < 2; i++ ) { |
| 742 | | if ( !memcmp( &rom[0x7ff0], signatures[i], 16 ) ) { |
| 917 | for( i = 0; i < 2; i++ ) |
| 918 | { |
| 919 | if ( !memcmp( &rom[0x7ff0], signatures[i], 16 ) ) |
| 920 | { |
| 743 | 921 | return 1; |
| 744 | 922 | } |
| 745 | 923 | } |
| 746 | 924 | return 0; |
| 747 | 925 | } |
| 748 | 926 | |
| 749 | | DEVICE_START( sms_cart ) { |
| 927 | |
| 928 | DEVICE_START( sms_cart ) |
| 929 | { |
| 750 | 930 | running_machine *machine = device->machine; |
| 751 | 931 | const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM); |
| 752 | 932 | int i; |
| 753 | 933 | |
| 754 | | for ( i = 0; i < MAX_CARTRIDGES; i++ ) { |
| 934 | for ( i = 0; i < MAX_CARTRIDGES; i++ ) |
| 935 | { |
| 755 | 936 | sms_cartridge[i].ROM = NULL; |
| 756 | 937 | sms_cartridge[i].size = 0; |
| 757 | 938 | sms_cartridge[i].features = 0; |
| r4402 | r4403 | |
| 764 | 945 | sms_current_cartridge = 0; |
| 765 | 946 | |
| 766 | 947 | biosPort = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD); |
| 767 | | if ( ! IS_GAMEGEAR && ! HAS_BIOS ) { |
| 948 | if ( ! IS_GAMEGEAR && ! HAS_BIOS ) |
| 949 | { |
| 768 | 950 | biosPort &= ~(IO_CARTRIDGE); |
| 769 | 951 | biosPort |= IO_BIOS_ROM; |
| 770 | 952 | } |
| 771 | | |
| 772 | | /* Initially set the vdp to not use sms compatibility mode for GG */ |
| 773 | | sms_set_ggsmsmode( 0 ); |
| 774 | 953 | } |
| 775 | 954 | |
| 955 | |
| 776 | 956 | DEVICE_IMAGE_LOAD( sms_cart ) |
| 777 | 957 | { |
| 778 | 958 | running_machine *machine = image->machine; |
| r4402 | r4403 | |
| 783 | 963 | const char *extrainfo = image_extrainfo( image ); |
| 784 | 964 | const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM); |
| 785 | 965 | |
| 786 | | if (strcmp(image->tag,"cart1")==0) { |
| 966 | if (strcmp(image->tag,"cart1")==0) |
| 967 | { |
| 787 | 968 | index = 0; |
| 788 | 969 | } |
| 789 | | if (strcmp(image->tag,"cart2")==0) { |
| 970 | if (strcmp(image->tag,"cart2")==0) |
| 971 | { |
| 790 | 972 | index = 1; |
| 791 | 973 | } |
| 792 | | if (strcmp(image->tag,"cart3")==0) { |
| 974 | if (strcmp(image->tag,"cart3")==0) |
| 975 | { |
| 793 | 976 | index = 2; |
| 794 | 977 | } |
| 795 | | if (strcmp(image->tag,"cart4")==0) { |
| 978 | if (strcmp(image->tag,"cart4")==0) |
| 979 | { |
| 796 | 980 | index = 3; |
| 797 | 981 | } |
| 798 | | if (strcmp(image->tag,"cart5")==0) { |
| 982 | if (strcmp(image->tag,"cart5")==0) |
| 983 | { |
| 799 | 984 | index = 4; |
| 800 | 985 | } |
| 801 | 986 | /* Check for 512-byte header */ |
| r4402 | r4403 | |
| 805 | 990 | size -= 512; |
| 806 | 991 | } |
| 807 | 992 | |
| 808 | | if ( ! size ) { |
| 993 | if ( ! size ) |
| 994 | { |
| 809 | 995 | image_seterror( image, IMAGE_ERROR_UNSPECIFIED, "Invalid ROM image: ROM image is too small" ); |
| 810 | 996 | return INIT_FAIL; |
| 811 | 997 | } |
| r4402 | r4403 | |
| 821 | 1007 | size = image_fread(image, sms_cartridge[index].ROM, size); |
| 822 | 1008 | |
| 823 | 1009 | /* check the image */ |
| 824 | | if ( ! HAS_BIOS ) { |
| 825 | | if (sms_verify_cart(sms_cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL) { |
| 1010 | if ( ! HAS_BIOS ) |
| 1011 | { |
| 1012 | if (sms_verify_cart(sms_cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL) |
| 1013 | { |
| 826 | 1014 | logerror("Warning loading image: sms_verify_cart failed\n"); |
| 827 | 1015 | } |
| 828 | 1016 | } |
| r4402 | r4403 | |
| 830 | 1018 | sms_cartridge[index].features = 0; |
| 831 | 1019 | |
| 832 | 1020 | /* Detect special features from the extrainfo field */ |
| 833 | | if ( extrainfo ) { |
| 1021 | if ( extrainfo ) |
| 1022 | { |
| 834 | 1023 | /* Check for codemasters mapper */ |
| 835 | | if ( strstr( extrainfo, "CODEMASTERS" ) ) { |
| 1024 | if ( strstr( extrainfo, "CODEMASTERS" ) ) |
| 1025 | { |
| 836 | 1026 | sms_cartridge[index].features |= CF_CODEMASTERS_MAPPER; |
| 837 | 1027 | } |
| 838 | 1028 | |
| 839 | 1029 | /* Check for korean mapper */ |
| 840 | | if ( strstr( extrainfo, "KOREAN" ) ) { |
| 1030 | if ( strstr( extrainfo, "KOREAN" ) ) |
| 1031 | { |
| 841 | 1032 | sms_cartridge[index].features |= CF_KOREAN_MAPPER; |
| 842 | 1033 | } |
| 843 | 1034 | |
| 844 | 1035 | /* Check for special SMS Compatibility mode gamegear cartridges */ |
| 845 | | if ( IS_GAMEGEAR && strstr( extrainfo, "GGSMS" ) ) { |
| 846 | | sms_set_ggsmsmode( 1 ); |
| 1036 | if ( IS_GAMEGEAR && strstr( extrainfo, "GGSMS" ) ) |
| 1037 | { |
| 1038 | sms_cartridge[index].features |= CF_GG_SMS_MODE; |
| 847 | 1039 | } |
| 848 | 1040 | |
| 849 | 1041 | /* Check for 93C46 eeprom */ |
| 850 | | if ( strstr( extrainfo, "93C46" ) ) { |
| 1042 | if ( strstr( extrainfo, "93C46" ) ) |
| 1043 | { |
| 851 | 1044 | sms_cartridge[index].features |= CF_93C46_EEPROM; |
| 852 | 1045 | } |
| 853 | 1046 | |
| 854 | 1047 | /* Check for 8KB on-cart RAM */ |
| 855 | | if ( strstr( extrainfo, "8KB_CART_RAM" ) ) { |
| 1048 | if ( strstr( extrainfo, "8KB_CART_RAM" ) ) |
| 1049 | { |
| 856 | 1050 | sms_cartridge[index].features |= CF_ONCART_RAM; |
| 857 | 1051 | sms_cartridge[index].ram_size = 0x2000; |
| 858 | 1052 | sms_cartridge[index].cartRAM = auto_malloc( sms_cartridge[index].ram_size ); |
| 859 | 1053 | } |
| 860 | | } else { |
| 1054 | } |
| 1055 | else |
| 1056 | { |
| 861 | 1057 | /* If no extrainfo information is available try to find special information out on our own */ |
| 862 | 1058 | /* Check for special cartridge features */ |
| 863 | | if ( size >= 0x8000 ) { |
| 1059 | if ( size >= 0x8000 ) |
| 1060 | { |
| 864 | 1061 | /* Check for special mappers */ |
| 865 | | if ( detect_codemasters_mapper( sms_cartridge[index].ROM ) ) { |
| 1062 | if ( detect_codemasters_mapper( sms_cartridge[index].ROM ) ) |
| 1063 | { |
| 866 | 1064 | sms_cartridge[index].features |= CF_CODEMASTERS_MAPPER; |
| 867 | 1065 | } |
| 868 | | if ( detect_korean_mapper( sms_cartridge[index].ROM ) ) { |
| 1066 | if ( detect_korean_mapper( sms_cartridge[index].ROM ) ) |
| 1067 | { |
| 869 | 1068 | sms_cartridge[index].features |= CF_KOREAN_MAPPER; |
| 870 | 1069 | } |
| 871 | 1070 | } |
| 872 | 1071 | |
| 873 | 1072 | /* Check for special SMS Compatibility mode gamegear cartridges */ |
| 874 | | if ( IS_GAMEGEAR ) { |
| 1073 | if ( IS_GAMEGEAR ) |
| 1074 | { |
| 875 | 1075 | /* Just in case someone passes us an sms file */ |
| 876 | | if ( fname_len > 3 && ! mame_stricmp( fname + fname_len - 3, "sms" ) ) { |
| 877 | | sms_set_ggsmsmode( 1 ); |
| 1076 | if ( fname_len > 3 && ! mame_stricmp( fname + fname_len - 3, "sms" ) ) |
| 1077 | { |
| 1078 | sms_cartridge[index].features |= CF_GG_SMS_MODE; |
| 878 | 1079 | } |
| 879 | 1080 | } |
| 880 | 1081 | } |
| 881 | 1082 | |
| 882 | | if ( sms_cartridge[index].features & CF_CODEMASTERS_MAPPER ) { |
| 1083 | if ( sms_cartridge[index].features & CF_CODEMASTERS_MAPPER ) |
| 1084 | { |
| 883 | 1085 | sms_cartridge[index].ram_size = 0x10000; |
| 884 | 1086 | sms_cartridge[index].cartRAM = auto_malloc( sms_cartridge[index].ram_size ); |
| 885 | 1087 | sms_cartridge[index].ram_page = 0; |
| r4402 | r4403 | |
| 891 | 1093 | return INIT_PASS; |
| 892 | 1094 | } |
| 893 | 1095 | |
| 894 | | static void setup_cart_banks( void ) { |
| 895 | | if ( sms_cartridge[sms_current_cartridge].ROM ) { |
| 1096 | |
| 1097 | static void setup_cart_banks( void ) |
| 1098 | { |
| 1099 | if ( sms_cartridge[sms_current_cartridge].ROM ) |
| 1100 | { |
| 896 | 1101 | UINT8 rom_page_count = sms_cartridge[sms_current_cartridge].size / 0x4000; |
| 897 | 1102 | sms_banking_cart[1] = sms_cartridge[sms_current_cartridge].ROM; |
| 898 | 1103 | sms_banking_cart[2] = sms_cartridge[sms_current_cartridge].ROM + 0x0400; |
| r4402 | r4403 | |
| 902 | 1107 | if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) { |
| 903 | 1108 | sms_banking_cart[4] = sms_cartridge[sms_current_cartridge].ROM; |
| 904 | 1109 | } |
| 905 | | } else { |
| 1110 | } |
| 1111 | else |
| 1112 | { |
| 906 | 1113 | sms_banking_cart[1] = sms_banking_none[1]; |
| 907 | 1114 | sms_banking_cart[2] = sms_banking_none[2]; |
| 908 | 1115 | sms_banking_cart[3] = sms_banking_none[3]; |
| r4402 | r4403 | |
| 910 | 1117 | } |
| 911 | 1118 | } |
| 912 | 1119 | |
| 913 | | static void setup_banks( running_machine *machine ) { |
| 1120 | |
| 1121 | static void setup_banks( running_machine *machine ) |
| 1122 | { |
| 914 | 1123 | UINT8 *mem = memory_region(machine, "maincpu"); |
| 915 | 1124 | sms_banking_bios[1] = sms_banking_cart[1] = sms_banking_none[1] = mem; |
| 916 | 1125 | sms_banking_bios[2] = sms_banking_cart[2] = sms_banking_none[2] = mem; |
| r4402 | r4403 | |
| 923 | 1132 | |
| 924 | 1133 | setup_cart_banks(); |
| 925 | 1134 | |
| 926 | | if ( BIOS == NULL || BIOS[0] == 0x00 ) { |
| 1135 | if ( BIOS == NULL || BIOS[0] == 0x00 ) |
| 1136 | { |
| 927 | 1137 | BIOS = NULL; |
| 928 | 1138 | biosPort |= IO_BIOS_ROM; |
| 929 | 1139 | } |
| 930 | 1140 | |
| 931 | | if ( BIOS ) { |
| 1141 | if ( BIOS ) |
| 1142 | { |
| 932 | 1143 | sms_banking_bios[1] = BIOS; |
| 933 | 1144 | sms_banking_bios[2] = BIOS + 0x0400; |
| 934 | 1145 | sms_banking_bios[3] = BIOS + ( ( 1 < smsBiosPageCount) ? 0x4000 : 0 ); |
| r4402 | r4403 | |
| 936 | 1147 | } |
| 937 | 1148 | } |
| 938 | 1149 | |
| 939 | | MACHINE_START(sms) { |
| 1150 | |
| 1151 | MACHINE_START(sms) |
| 1152 | { |
| 940 | 1153 | add_exit_callback(machine, sms_machine_stop); |
| 941 | 1154 | } |
| 942 | 1155 | |
| 1156 | |
| 943 | 1157 | MACHINE_RESET(sms) |
| 944 | 1158 | { |
| 945 | 1159 | const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM); |
| 946 | 1160 | |
| 947 | 1161 | smsVersion = 0x00; |
| 948 | | if ( HAS_FM ) { |
| 1162 | if ( HAS_FM ) |
| 1163 | { |
| 949 | 1164 | smsFMDetect = 0x01; |
| 950 | 1165 | } |
| 951 | 1166 | |
| 952 | 1167 | sms_mapper_ram = memory_get_write_ptr( space, 0xDFFC ); |
| 953 | 1168 | |
| 954 | | if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) { |
| 1169 | if ( sms_cartridge[sms_current_cartridge].features & CF_CODEMASTERS_MAPPER ) |
| 1170 | { |
| 955 | 1171 | /* Install special memory handlers */ |
| 956 | 1172 | memory_install_write8_handler(space, 0x0000, 0x0000, 0, 0, sms_codemasters_page0_w ); |
| 957 | 1173 | memory_install_write8_handler(space, 0x4000, 0x4000, 0, 0, sms_codemasters_page1_w ); |
| 958 | 1174 | } |
| 959 | 1175 | |
| 1176 | if ( sms_cartridge[sms_current_cartridge].features & CF_GG_SMS_MODE ) |
| 1177 | { |
| 1178 | sms_set_ggsmsmode( 1 ); |
| 1179 | } |
| 1180 | |
| 960 | 1181 | /* Initialize SIO stuff for GG */ |
| 961 | 1182 | ggSIO[0] = 0x7F; |
| 962 | 1183 | ggSIO[1] = 0xFF; |
| r4402 | r4403 | |
| 990 | 1211 | sports_pad_2_y = 0; |
| 991 | 1212 | } |
| 992 | 1213 | |
| 993 | | READ8_HANDLER(sms_store_cart_select_r) { |
| 1214 | |
| 1215 | READ8_HANDLER(sms_store_cart_select_r) |
| 1216 | { |
| 994 | 1217 | return 0xFF; |
| 995 | 1218 | } |
| 996 | 1219 | |
| 997 | | WRITE8_HANDLER(sms_store_cart_select_w) { |
| 1220 | |
| 1221 | WRITE8_HANDLER(sms_store_cart_select_w) |
| 1222 | { |
| 998 | 1223 | UINT8 slot = data >> 4; |
| 999 | 1224 | UINT8 slottype = data & 0x08; |
| 1000 | 1225 | |
| 1001 | 1226 | logerror("switching in part of %s slot #%d\n", slottype ? "card" : "cartridge", slot ); |
| 1002 | 1227 | /* cartridge? slot #0 */ |
| 1003 | | if ( slottype == 0 ) { |
| 1228 | if ( slottype == 0 ) |
| 1229 | { |
| 1004 | 1230 | sms_current_cartridge = slot; |
| 1005 | 1231 | } |
| 1006 | 1232 | setup_cart_banks(); |
| r4402 | r4403 | |
| 1008 | 1234 | setup_rom(space); |
| 1009 | 1235 | } |
| 1010 | 1236 | |
| 1011 | | READ8_HANDLER(sms_store_select1) { |
| 1237 | |
| 1238 | READ8_HANDLER(sms_store_select1) |
| 1239 | { |
| 1012 | 1240 | return 0xFF; |
| 1013 | 1241 | } |
| 1014 | 1242 | |
| 1015 | | READ8_HANDLER(sms_store_select2) { |
| 1243 | |
| 1244 | READ8_HANDLER(sms_store_select2) |
| 1245 | { |
| 1016 | 1246 | return 0xFF; |
| 1017 | 1247 | } |
| 1018 | 1248 | |
| 1019 | | READ8_HANDLER(sms_store_control_r) { |
| 1249 | |
| 1250 | READ8_HANDLER(sms_store_control_r) |
| 1251 | { |
| 1020 | 1252 | return sms_store_control; |
| 1021 | 1253 | } |
| 1022 | 1254 | |
| 1023 | | WRITE8_HANDLER(sms_store_control_w) { |
| 1255 | |
| 1256 | WRITE8_HANDLER(sms_store_control_w) |
| 1257 | { |
| 1024 | 1258 | logerror( "0x%04X: sms_store_control write 0x%02X\n", cpu_get_pc(space->cpu), data ); |
| 1025 | | if ( data & 0x02 ) { |
| 1259 | if ( data & 0x02 ) |
| 1260 | { |
| 1026 | 1261 | cpu_resume( space->machine->cpu[0], SUSPEND_REASON_HALT ); |
| 1027 | | } else { |
| 1262 | } |
| 1263 | else |
| 1264 | { |
| 1028 | 1265 | /* Pull reset line of CPU #0 low */ |
| 1029 | 1266 | cpu_suspend( space->machine->cpu[0], SUSPEND_REASON_HALT, 1 ); |
| 1030 | 1267 | device_reset(space->machine->cpu[0]); |
| r4402 | r4403 | |
| 1032 | 1269 | sms_store_control = data; |
| 1033 | 1270 | } |
| 1034 | 1271 | |
| 1035 | | void sms_int_callback( running_machine *machine, int state ) { |
| 1272 | |
| 1273 | void sms_int_callback( running_machine *machine, int state ) |
| 1274 | { |
| 1036 | 1275 | cpu_set_input_line(machine->cpu[0], 0, state ); |
| 1037 | 1276 | } |
| 1038 | 1277 | |
| 1039 | | void sms_store_int_callback( running_machine *machine, int state ) { |
| 1278 | |
| 1279 | void sms_store_int_callback( running_machine *machine, int state ) |
| 1280 | { |
| 1040 | 1281 | cpu_set_input_line(machine->cpu[0], sms_store_control & 0x01 ? 1 : 0, state ); |
| 1041 | 1282 | } |
| 1042 | 1283 | |