src/mess/machine/nes.c
| r1343 | r1344 | |
| 11 | 11 | |
| 12 | 12 | |
| 13 | 13 | |
| 14 | | /* Uncomment this to dump info about the inputs to the errorlog */ |
| 15 | | //#define LOG_JOY |
| 14 | /*************************************************************************** |
| 15 | CONSTANTS |
| 16 | ***************************************************************************/ |
| 16 | 17 | |
| 17 | | /* Uncomment this to generate prg chunk files when the cart is loaded */ |
| 18 | | //#define SPLIT_PRG |
| 18 | /* Set to dump info about the inputs to the errorlog */ |
| 19 | #define LOG_JOY 0 |
| 19 | 20 | |
| 21 | /* Set to generate prg chunk files when the cart is loaded */ |
| 22 | #define SPLIT_PRG 0 |
| 23 | |
| 20 | 24 | #define BATTERY_SIZE 0x2000 |
| 25 | |
| 26 | |
| 27 | |
| 28 | /*************************************************************************** |
| 29 | TYPE DEFINITIONS |
| 30 | ***************************************************************************/ |
| 31 | |
| 32 | typedef struct _nes_input nes_input; |
| 33 | struct _nes_input |
| 34 | { |
| 35 | UINT32 shift; |
| 36 | UINT32 i0, i1, i2; |
| 37 | }; |
| 38 | |
| 39 | |
| 40 | |
| 41 | /*************************************************************************** |
| 42 | GLOBAL VARIABLES |
| 43 | ***************************************************************************/ |
| 44 | |
| 21 | 45 | UINT8 battery_data[BATTERY_SIZE]; |
| 22 | 46 | |
| 23 | 47 | struct nes_struct nes; |
| 24 | 48 | struct fds_struct nes_fds; |
| 25 | 49 | |
| 26 | | static UINT32 in_0[3]; |
| 27 | | static UINT32 in_1[3]; |
| 28 | | static UINT32 in_0_shift; |
| 29 | | static UINT32 in_1_shift; |
| 50 | static nes_input in_0; |
| 51 | static nes_input in_1; |
| 30 | 52 | |
| 31 | | /* local prototypes */ |
| 53 | |
| 54 | |
| 55 | /*************************************************************************** |
| 56 | FUNCTION PROTOTYPES |
| 57 | ***************************************************************************/ |
| 58 | |
| 32 | 59 | static void init_nes_core(void); |
| 33 | 60 | static void nes_machine_stop(running_machine *machine); |
| 34 | 61 | |
| 62 | |
| 63 | |
| 64 | /*************************************************************************** |
| 65 | FUNCTIONS |
| 66 | ***************************************************************************/ |
| 67 | |
| 35 | 68 | static mess_image *cartslot_image(void) |
| 36 | 69 | { |
| 37 | 70 | return image_from_devtype_and_index(IO_CARTSLOT, 0); |
| r1343 | r1344 | |
| 144 | 177 | mapper_reset (nes.mapper); |
| 145 | 178 | |
| 146 | 179 | /* Reset the serial input ports */ |
| 147 | | in_0_shift = 0; |
| 148 | | in_1_shift = 0; |
| 180 | in_0.shift = 0; |
| 181 | in_1.shift = 0; |
| 149 | 182 | } |
| 150 | 183 | |
| 151 | 184 | MACHINE_START( nes ) |
| r1343 | r1344 | |
| 173 | 206 | |
| 174 | 207 | |
| 175 | 208 | |
| 176 | | static int zapper_hit_pixel(const UINT32 *input) |
| 209 | static int zapper_hit_pixel(const nes_input *input) |
| 177 | 210 | { |
| 178 | 211 | UINT16 pix = 0; |
| 179 | 212 | rgb_t col; |
| r1343 | r1344 | |
| 181 | 214 | extern mame_bitmap *nes_zapper_hack; |
| 182 | 215 | |
| 183 | 216 | if (nes_zapper_hack) |
| 184 | | pix = *BITMAP_ADDR16(nes_zapper_hack, input[2], input[1]); |
| 217 | pix = *BITMAP_ADDR16(nes_zapper_hack, input->i2, input->i1); |
| 185 | 218 | |
| 186 | 219 | col = palette_get_color(Machine, pix); |
| 187 | 220 | r = (UINT8) (col >> 16); |
| r1343 | r1344 | |
| 202 | 235 | /* in the unused upper 3 bits, so typically a read from $4016 leaves 0x40 there. */ |
| 203 | 236 | retVal = 0x40; |
| 204 | 237 | |
| 205 | | retVal |= ((in_0[0] >> in_0_shift) & 0x01); |
| 238 | retVal |= ((in_0.i0 >> in_0.shift) & 0x01); |
| 206 | 239 | |
| 207 | 240 | /* Check the configuration to see what's connected */ |
| 208 | 241 | cfg = readinputport(PORT_CONFIG1); |
| r1343 | r1344 | |
| 210 | 243 | if (((cfg & 0x000f) == 0x0002) || ((cfg & 0x000f) == 0x0003)) |
| 211 | 244 | { |
| 212 | 245 | /* If button 1 is pressed, indicate the light gun trigger is pressed */ |
| 213 | | retVal |= ((in_0[0] & 0x01) << 4); |
| 246 | retVal |= ((in_0.i0 & 0x01) << 4); |
| 214 | 247 | |
| 215 | 248 | /* Look at the screen and see if the cursor is over a bright pixel */ |
| 216 | | if (zapper_hit_pixel(in_0)) |
| 249 | if (zapper_hit_pixel(&in_0)) |
| 217 | 250 | retVal &= ~0x08; /* sprite hit */ |
| 218 | 251 | else |
| 219 | 252 | retVal |= 0x08; /* no sprite hit */ |
| 220 | 253 | } |
| 221 | 254 | |
| 222 | | #ifdef LOG_JOY |
| 223 | | logerror ("joy 0 read, val: %02x, pc: %04x, bits read: %d, chan0: %08x\n", retVal, activecpu_get_pc(), in_0_shift, in_0[0]); |
| 224 | | #endif |
| 255 | if (LOG_JOY) |
| 256 | logerror ("joy 0 read, val: %02x, pc: %04x, bits read: %d, chan0: %08x\n", retVal, activecpu_get_pc(), in_0.shift, in_0.i0); |
| 225 | 257 | |
| 226 | | in_0_shift ++; |
| 258 | in_0.shift ++; |
| 227 | 259 | return retVal; |
| 228 | 260 | } |
| 229 | 261 | |
| r1343 | r1344 | |
| 237 | 269 | retVal = 0x40; |
| 238 | 270 | |
| 239 | 271 | /* Handle data line 0's serial output */ |
| 240 | | retVal |= ((in_1[0] >> in_1_shift) & 0x01); |
| 272 | retVal |= ((in_1.i0 >> in_1.shift) & 0x01); |
| 241 | 273 | |
| 242 | 274 | /* Check the fake dip to see what's connected */ |
| 243 | 275 | cfg = readinputport(PORT_CONFIG1); |
| r1343 | r1344 | |
| 246 | 278 | { |
| 247 | 279 | /* zapper */ |
| 248 | 280 | /* If button 1 is pressed, indicate the light gun trigger is pressed */ |
| 249 | | retVal |= ((in_1[0] & 0x01) << 4); |
| 281 | retVal |= ((in_1.i0 & 0x01) << 4); |
| 250 | 282 | |
| 251 | 283 | /* Look at the screen and see if the cursor is over a bright pixel */ |
| 252 | | if (zapper_hit_pixel(in_1)) |
| 284 | if (zapper_hit_pixel(&in_1)) |
| 253 | 285 | retVal &= ~0x08; /* sprite hit */ |
| 254 | 286 | else |
| 255 | 287 | retVal |= 0x08; /* no sprite hit */ |
| r1343 | r1344 | |
| 258 | 290 | { |
| 259 | 291 | /* arkanoid dial */ |
| 260 | 292 | /* Handle data line 2's serial output */ |
| 261 | | retVal |= ((in_1[2] >> in_1_shift) & 0x01) << 3; |
| 293 | retVal |= ((in_1.i2 >> in_1.shift) & 0x01) << 3; |
| 262 | 294 | |
| 263 | 295 | /* Handle data line 3's serial output - bits are reversed */ |
| 264 | | // retVal |= ((in_1[3] >> in_1_shift) & 0x01) << 4; |
| 265 | | retVal |= ((in_1[3] << in_1_shift) & 0x80) >> 3; |
| 296 | /* NPW 27-Nov-2007 - there is no third subscript! commenting out */ |
| 297 | /* retVal |= ((in_1[3] >> in_1.shift) & 0x01) << 4; */ |
| 298 | /* retVal |= ((in_1[3] << in_1.shift) & 0x80) >> 3; */ |
| 266 | 299 | } |
| 267 | 300 | |
| 268 | | #ifdef LOG_JOY |
| 269 | | logerror ("joy 1 read, val: %02x, pc: %04x, bits read: %d, chan0: %08x\n", retVal, activecpu_get_pc(), in_1_shift, in_1[0]); |
| 270 | | #endif |
| 301 | if (LOG_JOY) |
| 302 | logerror ("joy 1 read, val: %02x, pc: %04x, bits read: %d, chan0: %08x\n", retVal, activecpu_get_pc(), in_1.shift, in_1.i0); |
| 271 | 303 | |
| 272 | | in_1_shift ++; |
| 304 | in_1.shift ++; |
| 273 | 305 | return retVal; |
| 274 | 306 | } |
| 275 | 307 | |
| 276 | 308 | |
| 277 | 309 | |
| 278 | | static void nes_read_input_device(int cfg, UINT32 *vals, int pad_port, |
| 310 | static void nes_read_input_device(int cfg, nes_input *vals, int pad_port, |
| 279 | 311 | int supports_zapper, int paddle_port) |
| 280 | 312 | { |
| 281 | | vals[0] = 0; |
| 282 | | vals[1] = 0; |
| 283 | | vals[2] = 0; |
| 313 | vals->i0 = 0; |
| 314 | vals->i1 = 0; |
| 315 | vals->i2 = 0; |
| 284 | 316 | |
| 285 | 317 | switch(cfg & 0x0f) |
| 286 | 318 | { |
| 287 | 319 | case 0x01: /* gamepad */ |
| 288 | 320 | if (pad_port >= 0) |
| 289 | | vals[0] = readinputport(pad_port); |
| 321 | vals->i0 = readinputport(pad_port); |
| 290 | 322 | break; |
| 291 | 323 | |
| 292 | 324 | case 0x02: /* zapper 1 */ |
| 293 | 325 | if (supports_zapper) |
| 294 | 326 | { |
| 295 | | vals[0] = readinputport(PORT_ZAPPER0_T); |
| 296 | | vals[1] = readinputport(PORT_ZAPPER0_X); |
| 297 | | vals[2] = readinputport(PORT_ZAPPER0_Y); |
| 327 | vals->i0 = readinputport(PORT_ZAPPER0_T); |
| 328 | vals->i1 = readinputport(PORT_ZAPPER0_X); |
| 329 | vals->i2 = readinputport(PORT_ZAPPER0_Y); |
| 298 | 330 | } |
| 299 | 331 | break; |
| 300 | 332 | |
| 301 | 333 | case 0x03: /* zapper 2 */ |
| 302 | 334 | if (supports_zapper) |
| 303 | 335 | { |
| 304 | | vals[0] = readinputport(PORT_ZAPPER1_T); |
| 305 | | vals[1] = readinputport(PORT_ZAPPER1_X); |
| 306 | | vals[2] = readinputport(PORT_ZAPPER1_Y); |
| 336 | vals->i0 = readinputport(PORT_ZAPPER1_T); |
| 337 | vals->i1 = readinputport(PORT_ZAPPER1_X); |
| 338 | vals->i2 = readinputport(PORT_ZAPPER1_Y); |
| 307 | 339 | } |
| 308 | 340 | break; |
| 309 | 341 | |
| 310 | 342 | case 0x04: /* arkanoid paddle */ |
| 311 | 343 | if (paddle_port >= 0) |
| 312 | | vals[0] = (UINT8) ((UINT8) readinputport (paddle_port) + (UINT8)0x52) ^ 0xff; |
| 344 | vals->i0 = (UINT8) ((UINT8) readinputport (paddle_port) + (UINT8)0x52) ^ 0xff; |
| 313 | 345 | break; |
| 314 | 346 | } |
| 315 | 347 | } |
| r1343 | r1344 | |
| 319 | 351 | WRITE8_HANDLER ( nes_IN0_w ) |
| 320 | 352 | { |
| 321 | 353 | int cfg; |
| 322 | | UINT32 in_2[3]; |
| 323 | | UINT32 in_3[3]; |
| 354 | nes_input in_2; |
| 355 | nes_input in_3; |
| 324 | 356 | |
| 325 | 357 | if (data & 0x01) return; |
| 326 | | #ifdef LOG_JOY |
| 327 | | logerror ("joy 0 bits read: %d\n", in_0_shift); |
| 328 | | #endif |
| 329 | 358 | |
| 359 | if (LOG_JOY) |
| 360 | logerror ("joy 0 bits read: %d\n", in_0.shift); |
| 361 | |
| 330 | 362 | /* Toggling bit 0 high then low resets both controllers */ |
| 331 | | in_0_shift = 0; |
| 332 | | in_1_shift = 0; |
| 363 | in_0.shift = 0; |
| 364 | in_1.shift = 0; |
| 333 | 365 | |
| 334 | 366 | /* Check the configuration to see what's connected */ |
| 335 | 367 | cfg = readinputport(PORT_CONFIG1); |
| 336 | 368 | |
| 337 | 369 | /* Read the input devices */ |
| 338 | | nes_read_input_device(cfg >> 0, in_0, PORT_PAD0, TRUE, -1); |
| 339 | | nes_read_input_device(cfg >> 4, in_1, PORT_PAD1, TRUE, PORT_PADDLE1); |
| 340 | | nes_read_input_device(cfg >> 8, in_2, PORT_PAD2, FALSE, -1); |
| 341 | | nes_read_input_device(cfg >> 12, in_3, PORT_PAD3, FALSE, -1); |
| 370 | nes_read_input_device(cfg >> 0, &in_0, PORT_PAD0, TRUE, -1); |
| 371 | nes_read_input_device(cfg >> 4, &in_1, PORT_PAD1, TRUE, PORT_PADDLE1); |
| 372 | nes_read_input_device(cfg >> 8, &in_2, PORT_PAD2, FALSE, -1); |
| 373 | nes_read_input_device(cfg >> 12, &in_3, PORT_PAD3, FALSE, -1); |
| 342 | 374 | |
| 343 | 375 | if (cfg & 0x0f00) |
| 344 | | in_0[0] |= (in_2[0] << 8) | (0x08 << 16); |
| 376 | in_0.i0 |= (in_2.i0 << 8) | (0x08 << 16); |
| 345 | 377 | if (cfg & 0xf000) |
| 346 | | in_1[0] |= (in_3[0] << 8) | (0x04 << 16); |
| 378 | in_1.i0 |= (in_3.i0 << 8) | (0x04 << 16); |
| 347 | 379 | } |
| 348 | 380 | |
| 349 | 381 | |
| r1343 | r1344 | |
| 472 | 504 | else |
| 473 | 505 | image_fread (image, &nes.rom[0x10000], 0x4000 * nes.prg_chunks); |
| 474 | 506 | |
| 475 | | #ifdef SPLIT_PRG |
| 476 | | { |
| 477 | | FILE *prgout; |
| 478 | | char outname[255]; |
| 507 | #if SPLIT_PRG |
| 508 | { |
| 509 | FILE *prgout; |
| 510 | char outname[255]; |
| 479 | 511 | |
| 480 | | for (i = 0; i < nes.prg_chunks; i ++) |
| 481 | | { |
| 482 | | sprintf (outname, "%s.p%d", battery_name, i); |
| 483 | | prgout = fopen (outname, "wb"); |
| 484 | | if (prgout) |
| 512 | for (i = 0; i < nes.prg_chunks; i ++) |
| 485 | 513 | { |
| 486 | | fwrite (&nes.rom[0x10000 + 0x4000 * i], 1, 0x4000, prgout); |
| 487 | | fclose (prgout); |
| 514 | sprintf (outname, "%s.p%d", battery_name, i); |
| 515 | prgout = fopen (outname, "wb"); |
| 516 | if (prgout) |
| 517 | { |
| 518 | fwrite (&nes.rom[0x10000 + 0x4000 * i], 1, 0x4000, prgout); |
| 519 | fclose (prgout); |
| 520 | } |
| 488 | 521 | } |
| 489 | 522 | } |
| 490 | | } |
| 491 | | #endif |
| 523 | #endif /* SPLIT_PRG */ |
| 492 | 524 | |
| 493 | 525 | logerror("**\n"); |
| 494 | 526 | logerror("Mapper: %d\n", nes.mapper); |