src/mame/video/ppu2c0x.c
| r7669 | r7670 | |
| 33 | 33 | ***************************************************************************/ |
| 34 | 34 | |
| 35 | 35 | /* constant definitions */ |
| 36 | | #define VISIBLE_SCREEN_WIDTH (32*8) /* Visible screen width */ |
| 37 | | #define VISIBLE_SCREEN_HEIGHT (30*8) /* Visible screen height */ |
| 36 | #define VISIBLE_SCREEN_WIDTH (32*8) /* Visible screen width */ |
| 37 | #define VISIBLE_SCREEN_HEIGHT (30*8) /* Visible screen height */ |
| 38 | 38 | #define VIDEOMEM_SIZE 0x1000 /* videomem size */ |
| 39 | 39 | #define VIDEOMEM_PAGE_SIZE 0x400 /* videomem page size */ |
| 40 | 40 | #define SPRITERAM_SIZE 0x100 /* spriteram size */ |
| r7669 | r7670 | |
| 77 | 77 | TYPE DEFINITIONS |
| 78 | 78 | ***************************************************************************/ |
| 79 | 79 | |
| 80 | | /* our chip state */ |
| 81 | | typedef struct |
| 80 | |
| 81 | typedef struct _ppu2c0x_state ppu2c0x_state; |
| 82 | struct _ppu2c0x_state |
| 82 | 83 | { |
| 83 | | bitmap_t *bitmap; /* target bitmap */ |
| 84 | | UINT8 *spriteram; /* sprite ram */ |
| 85 | | pen_t *colortable; /* color table modified at run time */ |
| 86 | | pen_t *colortable_mono; /* monochromatic color table modified at run time */ |
| 87 | | emu_timer *scanline_timer; /* scanline timer */ |
| 88 | | emu_timer *hblank_timer; /* hblank period at end of each scanline */ |
| 89 | | emu_timer *nmi_timer; /* NMI timer */ |
| 90 | | int scanline; /* scanline count */ |
| 91 | | ppu2c0x_scanline_cb scanline_callback_proc; /* optional scanline callback */ |
| 92 | | ppu2c0x_hblank_cb hblank_callback_proc; /* optional hblank callback */ |
| 93 | | ppu2c0x_vidaccess_cb vidaccess_callback_proc;/* optional video access callback */ |
| 94 | | int regs[PPU_MAX_REG]; /* registers */ |
| 95 | | int refresh_data; /* refresh-related */ |
| 96 | | int refresh_latch; /* refresh-related */ |
| 97 | | int x_fine; /* refresh-related */ |
| 98 | | int toggle; /* used to latch hi-lo scroll */ |
| 99 | | int add; /* vram increment amount */ |
| 100 | | int videomem_addr; /* videomem address pointer */ |
| 101 | | int addr_latch; /* videomem address latch */ |
| 102 | | int data_latch; /* latched videomem data */ |
| 103 | | int buffered_data; |
| 104 | | int tile_page; /* current tile page */ |
| 105 | | int sprite_page; /* current sprite page */ |
| 106 | | int back_color; /* background color */ |
| 107 | | UINT8 palette_ram[0x20]; /* shouldn't be in main memory! */ |
| 108 | | int scan_scale; /* scan scale */ |
| 109 | | int scanlines_per_frame; /* number of scanlines per frame */ |
| 110 | | int mirror_state; |
| 111 | | rgb_t palette[64*4]; /* palette for this chip */ |
| 112 | | } ppu2c0x_chip; |
| 84 | bitmap_t *bitmap; /* target bitmap */ |
| 85 | UINT8 *spriteram; /* sprite ram */ |
| 86 | pen_t *colortable; /* color table modified at run time */ |
| 87 | pen_t *colortable_mono; /* monochromatic color table modified at run time */ |
| 88 | emu_timer *scanline_timer; /* scanline timer */ |
| 89 | emu_timer *hblank_timer; /* hblank period at end of each scanline */ |
| 90 | emu_timer *nmi_timer; /* NMI timer */ |
| 91 | int scanline; /* scanline count */ |
| 92 | ppu2c0x_scanline_cb scanline_callback_proc; /* optional scanline callback */ |
| 93 | ppu2c0x_hblank_cb hblank_callback_proc; /* optional hblank callback */ |
| 94 | ppu2c0x_vidaccess_cb vidaccess_callback_proc; /* optional video access callback */ |
| 95 | ppu2c0x_nmi_cb nmi_callback_proc; /* nmi access callback from interface */ |
| 96 | int regs[PPU_MAX_REG]; /* registers */ |
| 97 | int refresh_data; /* refresh-related */ |
| 98 | int refresh_latch; /* refresh-related */ |
| 99 | int x_fine; /* refresh-related */ |
| 100 | int toggle; /* used to latch hi-lo scroll */ |
| 101 | int add; /* vram increment amount */ |
| 102 | int videomem_addr; /* videomem address pointer */ |
| 103 | int addr_latch; /* videomem address latch */ |
| 104 | int data_latch; /* latched videomem data */ |
| 105 | int buffered_data; |
| 106 | int tile_page; /* current tile page */ |
| 107 | int sprite_page; /* current sprite page */ |
| 108 | int back_color; /* background color */ |
| 109 | int color_base; |
| 110 | UINT8 palette_ram[0x20]; /* shouldn't be in main memory! */ |
| 111 | int scan_scale; /* scan scale */ |
| 112 | int scanlines_per_frame; /* number of scanlines per frame */ |
| 113 | rgb_t palette[64*4]; /* palette for this chip */ |
| 114 | }; |
| 113 | 115 | |
| 114 | | |
| 115 | 116 | /*************************************************************************** |
| 116 | 117 | PROTOTYPES |
| 117 | 118 | ***************************************************************************/ |
| r7669 | r7670 | |
| 133 | 134 | INLINE FUNCTIONS |
| 134 | 135 | ***************************************************************************/ |
| 135 | 136 | |
| 136 | | INLINE ppu2c0x_chip *get_token( running_device *device ) |
| 137 | INLINE ppu2c0x_state *get_token( running_device *device ) |
| 137 | 138 | { |
| 138 | 139 | assert(device != NULL); |
| 139 | 140 | assert((device->type == PPU_2C02) || (device->type == PPU_2C03B) |
| 140 | 141 | || (device->type == PPU_2C04) || (device->type == PPU_2C05) |
| 141 | 142 | || (device->type == PPU_2C07)); |
| 142 | | return (ppu2c0x_chip *) device->token; |
| 143 | return (ppu2c0x_state *) device->token; |
| 143 | 144 | } |
| 144 | 145 | |
| 145 | 146 | |
| r7669 | r7670 | |
| 179 | 180 | /* 16 colors, with 4 luminance levels for each color */ |
| 180 | 181 | /* The 16 colors circle around the YUV color space, */ |
| 181 | 182 | |
| 182 | | int colorIntensity, colorNum, colorEmphasis; |
| 183 | int color_intensity, color_num, color_emphasis; |
| 183 | 184 | |
| 184 | 185 | double R, G, B; |
| 185 | 186 | |
| r7669 | r7670 | |
| 199 | 200 | }; |
| 200 | 201 | |
| 201 | 202 | /* Loop through the emphasis modes (8 total) */ |
| 202 | | for (colorEmphasis = 0; colorEmphasis < 8; colorEmphasis ++) |
| 203 | for (color_emphasis = 0; color_emphasis < 8; color_emphasis ++) |
| 203 | 204 | { |
| 204 | 205 | /* |
| 205 | 206 | double r_mod = 0.0; |
| 206 | 207 | double g_mod = 0.0; |
| 207 | 208 | double b_mod = 0.0; |
| 208 | 209 | |
| 209 | | switch (colorEmphasis) |
| 210 | switch (color_emphasis) |
| 210 | 211 | { |
| 211 | | case 0: r_mod = 1.0; g_mod = 1.0; b_mod = 1.0; break; |
| 212 | case 0: r_mod = 1.0; g_mod = 1.0; b_mod = 1.0; break; |
| 212 | 213 | case 1: r_mod = 1.24; g_mod = .915; b_mod = .743; break; |
| 213 | 214 | case 2: r_mod = .794; g_mod = 1.09; b_mod = .882; break; |
| 214 | 215 | case 3: r_mod = .905; g_mod = 1.03; b_mod = 1.28; break; |
| 215 | | case 4: r_mod = .741; g_mod = .987; b_mod = 1.0; break; |
| 216 | case 4: r_mod = .741; g_mod = .987; b_mod = 1.0; break; |
| 216 | 217 | case 5: r_mod = 1.02; g_mod = .908; b_mod = .979; break; |
| 217 | | case 6: r_mod = 1.02; g_mod = .98; b_mod = .653; break; |
| 218 | | case 7: r_mod = .75; g_mod = .75; b_mod = .75; break; |
| 218 | case 6: r_mod = 1.02; g_mod = .98; b_mod = .653; break; |
| 219 | case 7: r_mod = .75; g_mod = .75; b_mod = .75; break; |
| 219 | 220 | } |
| 220 | 221 | */ |
| 221 | 222 | |
| 222 | 223 | /* loop through the 4 intensities */ |
| 223 | | for (colorIntensity = 0; colorIntensity < 4; colorIntensity++) |
| 224 | for (color_intensity = 0; color_intensity < 4; color_intensity++) |
| 224 | 225 | { |
| 225 | 226 | /* loop through the 16 colors */ |
| 226 | | for (colorNum = 0; colorNum < 16; colorNum++) |
| 227 | for (color_num = 0; color_num < 16; color_num++) |
| 227 | 228 | { |
| 228 | 229 | double sat; |
| 229 | 230 | double y, u, v; |
| 230 | 231 | double rad; |
| 231 | 232 | |
| 232 | | switch (colorNum) |
| 233 | switch (color_num) |
| 233 | 234 | { |
| 234 | 235 | case 0: |
| 235 | 236 | sat = 0; rad = 0; |
| 236 | | y = brightness[0][colorIntensity]; |
| 237 | y = brightness[0][color_intensity]; |
| 237 | 238 | break; |
| 238 | 239 | |
| 239 | 240 | case 13: |
| 240 | 241 | sat = 0; rad = 0; |
| 241 | | y = brightness[2][colorIntensity]; |
| 242 | y = brightness[2][color_intensity]; |
| 242 | 243 | break; |
| 243 | 244 | |
| 244 | 245 | case 14: |
| r7669 | r7670 | |
| 248 | 249 | |
| 249 | 250 | default: |
| 250 | 251 | sat = tint; |
| 251 | | rad = M_PI * ((colorNum * 30 + hue) / 180.0); |
| 252 | | y = brightness[1][colorIntensity]; |
| 252 | rad = M_PI * ((color_num * 30 + hue) / 180.0); |
| 253 | y = brightness[1][color_intensity]; |
| 253 | 254 | break; |
| 254 | 255 | } |
| 255 | 256 | |
| r7669 | r7670 | |
| 302 | 303 | * |
| 303 | 304 | *************************************/ |
| 304 | 305 | |
| 305 | | static DEVICE_START( ppu2c0x ) |
| 306 | | { |
| 307 | | ppu2c0x_chip *chip = get_token(device); |
| 308 | | |
| 309 | | memset(chip, 0, sizeof(*chip)); |
| 310 | | chip->scanlines_per_frame = (int) device->get_config_int(PPU2C0XINFO_INT_SCANLINES_PER_FRAME); |
| 311 | | |
| 312 | | /* initialize the scanline handling portion */ |
| 313 | | chip->scanline_timer = timer_alloc(device->machine, scanline_callback, (void *) device); |
| 314 | | chip->hblank_timer = timer_alloc(device->machine, hblank_callback, (void *) device); |
| 315 | | chip->nmi_timer = timer_alloc(device->machine, nmi_callback, (void *) device); |
| 316 | | chip->scanline = 0; |
| 317 | | chip->scan_scale = 1; |
| 318 | | |
| 319 | | /* allocate a screen bitmap, videomem and spriteram, a dirtychar array and the monochromatic colortable */ |
| 320 | | chip->bitmap = auto_bitmap_alloc(device->machine, VISIBLE_SCREEN_WIDTH, VISIBLE_SCREEN_HEIGHT, video_screen_get_format(device->machine->primary_screen)); |
| 321 | | chip->spriteram = auto_alloc_array_clear(device->machine, UINT8, SPRITERAM_SIZE); |
| 322 | | chip->colortable = auto_alloc_array(device->machine, pen_t, ARRAY_LENGTH(default_colortable)); |
| 323 | | chip->colortable_mono = auto_alloc_array(device->machine, pen_t, ARRAY_LENGTH(default_colortable_mono)); |
| 324 | | } |
| 325 | | |
| 326 | 306 | static TIMER_CALLBACK( hblank_callback ) |
| 327 | 307 | { |
| 328 | 308 | running_device *device = (running_device *)ptr; |
| 329 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 330 | | int *ppu_regs = &this_ppu->regs[0]; |
| 309 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 310 | int *ppu_regs = &ppu2c0x->regs[0]; |
| 331 | 311 | |
| 332 | 312 | int blanked = (ppu_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) == 0; |
| 333 | | int vblank = ((this_ppu->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (this_ppu->scanline < this_ppu->scanlines_per_frame - 1)) ? 1 : 0; |
| 313 | int vblank = ((ppu2c0x->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (ppu2c0x->scanline < ppu2c0x->scanlines_per_frame - 1)) ? 1 : 0; |
| 334 | 314 | |
| 335 | 315 | // update_scanline(device); |
| 336 | 316 | |
| 337 | | if (this_ppu->hblank_callback_proc) |
| 338 | | (*this_ppu->hblank_callback_proc) (device, this_ppu->scanline, vblank, blanked); |
| 317 | if (ppu2c0x->hblank_callback_proc) |
| 318 | (*ppu2c0x->hblank_callback_proc) (device, ppu2c0x->scanline, vblank, blanked); |
| 339 | 319 | |
| 340 | | timer_adjust_oneshot(this_ppu->hblank_timer, attotime_never, 0); |
| 320 | timer_adjust_oneshot(ppu2c0x->hblank_timer, attotime_never, 0); |
| 341 | 321 | } |
| 342 | 322 | |
| 343 | 323 | static TIMER_CALLBACK( nmi_callback ) |
| 344 | 324 | { |
| 345 | 325 | running_device *device = (running_device *)ptr; |
| 346 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 347 | | const ppu2c0x_interface *intf = get_interface(device); |
| 348 | | int *ppu_regs = &this_ppu->regs[0]; |
| 326 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 327 | int *ppu_regs = &ppu2c0x->regs[0]; |
| 349 | 328 | |
| 350 | 329 | // Actually fire the VMI |
| 351 | | if (intf->nmi_handler != NULL) |
| 352 | | (*intf->nmi_handler) (device, ppu_regs); |
| 330 | if (ppu2c0x->nmi_callback_proc != NULL) |
| 331 | (*ppu2c0x->nmi_callback_proc) (device, ppu_regs); |
| 353 | 332 | |
| 354 | | timer_adjust_oneshot(this_ppu->nmi_timer, attotime_never, 0); |
| 333 | timer_adjust_oneshot(ppu2c0x->nmi_timer, attotime_never, 0); |
| 355 | 334 | } |
| 356 | 335 | |
| 357 | 336 | static void draw_background( running_device *device, UINT8 *line_priority ) |
| 358 | 337 | { |
| 359 | | const ppu2c0x_interface *intf = get_interface(device); |
| 360 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 338 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 361 | 339 | |
| 362 | 340 | /* cache some values locally */ |
| 363 | | bitmap_t *bitmap = this_ppu->bitmap; |
| 364 | | const int *ppu_regs = &this_ppu->regs[0]; |
| 365 | | const int scanline = this_ppu->scanline; |
| 366 | | const int refresh_data = this_ppu->refresh_data; |
| 367 | | const int tile_page = this_ppu->tile_page; |
| 341 | bitmap_t *bitmap = ppu2c0x->bitmap; |
| 342 | const int *ppu_regs = &ppu2c0x->regs[0]; |
| 343 | const int scanline = ppu2c0x->scanline; |
| 344 | const int refresh_data = ppu2c0x->refresh_data; |
| 345 | const int tile_page = ppu2c0x->tile_page; |
| 368 | 346 | |
| 369 | | int start_x = (this_ppu->x_fine ^ 0x07) - 7; |
| 347 | int start_x = (ppu2c0x->x_fine ^ 0x07) - 7; |
| 370 | 348 | UINT16 back_pen; |
| 371 | 349 | UINT16 *dest; |
| 372 | 350 | |
| r7669 | r7670 | |
| 382 | 360 | if (ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 383 | 361 | { |
| 384 | 362 | color_mask = 0xf0; |
| 385 | | color_table = this_ppu->colortable_mono; |
| 363 | color_table = ppu2c0x->colortable_mono; |
| 386 | 364 | } |
| 387 | 365 | else |
| 388 | 366 | { |
| 389 | 367 | color_mask = 0xff; |
| 390 | | color_table = this_ppu->colortable; |
| 368 | color_table = ppu2c0x->colortable; |
| 391 | 369 | } |
| 392 | 370 | |
| 393 | 371 | /* cache the background pen */ |
| 394 | | back_pen = (this_ppu->back_color & color_mask) + intf->color_base; |
| 372 | back_pen = (ppu2c0x->back_color & color_mask) + ppu2c0x->color_base; |
| 395 | 373 | |
| 396 | 374 | /* determine where in the nametable to start drawing from */ |
| 397 | 375 | /* based on the current scanline and scroll regs */ |
| r7669 | r7670 | |
| 445 | 423 | paldata = &color_table[4 * (((color_byte >> color_bits) & 0x03))]; |
| 446 | 424 | |
| 447 | 425 | // need to read 0x0000 or 0x1000 + 16*nametable data |
| 448 | | address = ((this_ppu->tile_page) ? 0x1000 : 0) + (page2 * 16); |
| 426 | address = ((ppu2c0x->tile_page) ? 0x1000 : 0) + (page2 * 16); |
| 449 | 427 | // plus something that accounts for y |
| 450 | 428 | address += scroll_y_fine; |
| 451 | 429 | |
| r7669 | r7670 | |
| 503 | 481 | |
| 504 | 482 | static void draw_sprites( running_device *device, UINT8 *line_priority ) |
| 505 | 483 | { |
| 506 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 484 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 507 | 485 | |
| 508 | 486 | /* cache some values locally */ |
| 509 | | bitmap_t *bitmap = this_ppu->bitmap; |
| 510 | | const int scanline = this_ppu->scanline; |
| 511 | | const int sprite_page = this_ppu->sprite_page; |
| 512 | | const UINT8 *sprite_ram = this_ppu->spriteram; |
| 513 | | pen_t *color_table = this_ppu->colortable; |
| 514 | | int *ppu_regs = &this_ppu->regs[0]; |
| 487 | bitmap_t *bitmap = ppu2c0x->bitmap; |
| 488 | const int scanline = ppu2c0x->scanline; |
| 489 | const int sprite_page = ppu2c0x->sprite_page; |
| 490 | const UINT8 *sprite_ram = ppu2c0x->spriteram; |
| 491 | pen_t *color_table = ppu2c0x->colortable; |
| 492 | int *ppu_regs = &ppu2c0x->regs[0]; |
| 515 | 493 | |
| 516 | | int spriteXPos, spriteYPos, spriteIndex; |
| 494 | int sprite_xpos, sprite_ypos, sprite_index; |
| 517 | 495 | int tile, index1, page; |
| 518 | 496 | int pri; |
| 519 | 497 | |
| 520 | 498 | int flipx, flipy, color; |
| 521 | 499 | int size; |
| 522 | | int spriteCount = 0; |
| 500 | int sprite_count = 0; |
| 523 | 501 | int sprite_line; |
| 524 | 502 | |
| 525 | 503 | int first_pixel; |
| r7669 | r7670 | |
| 532 | 510 | |
| 533 | 511 | first_pixel = (ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_SPRITES_L8)? 0: 8; |
| 534 | 512 | |
| 535 | | for (spriteIndex = 0; spriteIndex < SPRITERAM_SIZE; spriteIndex += 4) |
| 513 | for (sprite_index = 0; sprite_index < SPRITERAM_SIZE; sprite_index += 4) |
| 536 | 514 | { |
| 537 | 515 | UINT8 plane1; |
| 538 | 516 | UINT8 plane2; |
| 539 | 517 | |
| 540 | | spriteYPos = sprite_ram[spriteIndex] + 1; |
| 541 | | spriteXPos = sprite_ram[spriteIndex + 3]; |
| 518 | sprite_ypos = sprite_ram[sprite_index] + 1; |
| 519 | sprite_xpos = sprite_ram[sprite_index + 3]; |
| 542 | 520 | |
| 543 | 521 | // The sprite collision acts funny on the last pixel of a scanline. |
| 544 | 522 | // The various scanline latches update while the last few pixels |
| r7669 | r7670 | |
| 547 | 525 | // when, e.g., sprite x is 254, sprite y is 29 and we're rendering |
| 548 | 526 | // at the end of scanline 28. |
| 549 | 527 | // Battletoads needs this level of precision to be playable. |
| 550 | | if ((spriteIndex == 0) && (spriteXPos == 254)) |
| 528 | if ((sprite_index == 0) && (sprite_xpos == 254)) |
| 551 | 529 | { |
| 552 | | spriteYPos --; |
| 530 | sprite_ypos--; |
| 553 | 531 | /* set the "sprite 0 hit" flag if appropriate */ |
| 554 | | if (line_priority[spriteXPos] & 0x02) |
| 532 | if (line_priority[sprite_xpos] & 0x02) |
| 555 | 533 | ppu_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT; |
| 556 | 534 | } |
| 557 | 535 | |
| 558 | 536 | /* if the sprite isn't visible, skip it */ |
| 559 | | if ((spriteYPos + size <= scanline) || (spriteYPos > scanline)) |
| 537 | if ((sprite_ypos + size <= scanline) || (sprite_ypos > scanline)) |
| 560 | 538 | continue; |
| 561 | 539 | |
| 562 | | tile = sprite_ram[spriteIndex + 1]; |
| 563 | | color = (sprite_ram[spriteIndex + 2] & 0x03) + 4; |
| 564 | | pri = sprite_ram[spriteIndex + 2] & 0x20; |
| 565 | | flipx = sprite_ram[spriteIndex + 2] & 0x40; |
| 566 | | flipy = sprite_ram[spriteIndex + 2] & 0x80; |
| 540 | tile = sprite_ram[sprite_index + 1]; |
| 541 | color = (sprite_ram[sprite_index + 2] & 0x03) + 4; |
| 542 | pri = sprite_ram[sprite_index + 2] & 0x20; |
| 543 | flipx = sprite_ram[sprite_index + 2] & 0x40; |
| 544 | flipy = sprite_ram[sprite_index + 2] & 0x80; |
| 567 | 545 | |
| 568 | 546 | if (size == 16) |
| 569 | 547 | { |
| r7669 | r7670 | |
| 583 | 561 | (*ppu_latch)(device, (sprite_page << 10) | ((tile & 0xff) << 4)); |
| 584 | 562 | |
| 585 | 563 | /* compute the character's line to draw */ |
| 586 | | sprite_line = scanline - spriteYPos; |
| 564 | sprite_line = scanline - sprite_ypos; |
| 587 | 565 | |
| 588 | 566 | if (flipy) |
| 589 | 567 | sprite_line = (size - 1) - sprite_line; |
| r7669 | r7670 | |
| 600 | 578 | if (size == 8) |
| 601 | 579 | index1 += ((sprite_page == 0) ? 0 : 0x1000); |
| 602 | 580 | |
| 603 | | plane1 = memory_read_byte(device->space(), (index1 + sprite_line) & 0x1fff); |
| 581 | plane1 = memory_read_byte(device->space(), (index1 + sprite_line + 0) & 0x1fff); |
| 604 | 582 | plane2 = memory_read_byte(device->space(), (index1 + sprite_line + 8) & 0x1fff); |
| 605 | 583 | |
| 606 | 584 | /* if there are more than 8 sprites on this line, set the flag */ |
| 607 | | if (spriteCount == 8) |
| 585 | if (sprite_count == 8) |
| 608 | 586 | { |
| 609 | 587 | ppu_regs[PPU_STATUS] |= PPU_STATUS_8SPRITES; |
| 610 | 588 | // logerror ("> 8 sprites, scanline: %d\n", scanline); |
| r7669 | r7670 | |
| 613 | 591 | break; |
| 614 | 592 | } |
| 615 | 593 | |
| 616 | | spriteCount++; |
| 594 | sprite_count++; |
| 617 | 595 | |
| 618 | 596 | /* abort drawing if sprites aren't rendered */ |
| 619 | 597 | if (!(ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_SPRITES)) |
| r7669 | r7670 | |
| 624 | 602 | /* draw the low-priority sprites */ |
| 625 | 603 | for (pixel = 0; pixel < 8; pixel++) |
| 626 | 604 | { |
| 627 | | UINT8 pixelData; |
| 605 | UINT8 pixel_data; |
| 628 | 606 | if (flipx) |
| 629 | 607 | { |
| 630 | | pixelData = (plane1 & 1) + ((plane2 & 1) << 1); |
| 608 | pixel_data = (plane1 & 1) + ((plane2 & 1) << 1); |
| 631 | 609 | plane1 = plane1 >> 1; |
| 632 | 610 | plane2 = plane2 >> 1; |
| 633 | 611 | } |
| 634 | 612 | else |
| 635 | 613 | { |
| 636 | | pixelData = ((plane1 >> 7) & 1) | (((plane2 >> 7) & 1) << 1); |
| 614 | pixel_data = ((plane1 >> 7) & 1) | (((plane2 >> 7) & 1) << 1); |
| 637 | 615 | plane1 = plane1 << 1; |
| 638 | 616 | plane2 = plane2 << 1; |
| 639 | 617 | } |
| 640 | 618 | |
| 641 | 619 | /* is this pixel non-transparent? */ |
| 642 | | if (spriteXPos + pixel >= first_pixel) |
| 620 | if (sprite_xpos + pixel >= first_pixel) |
| 643 | 621 | { |
| 644 | | if (pixelData) |
| 622 | if (pixel_data) |
| 645 | 623 | { |
| 646 | 624 | /* has the background (or another sprite) already been drawn here? */ |
| 647 | | if (!line_priority[spriteXPos + pixel]) |
| 625 | if (!line_priority[sprite_xpos + pixel]) |
| 648 | 626 | { |
| 649 | 627 | /* no, draw */ |
| 650 | | if ((spriteXPos + pixel) < VISIBLE_SCREEN_WIDTH) |
| 651 | | *BITMAP_ADDR16(bitmap, scanline, spriteXPos + pixel) = paldata[pixelData]; |
| 628 | if ((sprite_xpos + pixel) < VISIBLE_SCREEN_WIDTH) |
| 629 | *BITMAP_ADDR16(bitmap, scanline, sprite_xpos + pixel) = paldata[pixel_data]; |
| 652 | 630 | } |
| 653 | 631 | /* indicate that a sprite was drawn at this location, even if it's not seen */ |
| 654 | | if ((spriteXPos + pixel) < VISIBLE_SCREEN_WIDTH) |
| 655 | | line_priority[spriteXPos + pixel] |= 0x01; |
| 632 | if ((sprite_xpos + pixel) < VISIBLE_SCREEN_WIDTH) |
| 633 | line_priority[sprite_xpos + pixel] |= 0x01; |
| 656 | 634 | } |
| 657 | 635 | |
| 658 | 636 | /* set the "sprite 0 hit" flag if appropriate */ |
| 659 | | if (spriteIndex == 0 && (pixelData & 0x03) && ((spriteXPos + pixel) < 255) && (line_priority[spriteXPos + pixel] & 0x02)) |
| 637 | if (sprite_index == 0 && (pixel_data & 0x03) && ((sprite_xpos + pixel) < 255) && (line_priority[sprite_xpos + pixel] & 0x02)) |
| 660 | 638 | ppu_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT; |
| 661 | 639 | } |
| 662 | 640 | } |
| r7669 | r7670 | |
| 666 | 644 | /* draw the high-priority sprites */ |
| 667 | 645 | for (pixel = 0; pixel < 8; pixel++) |
| 668 | 646 | { |
| 669 | | UINT8 pixelData; |
| 647 | UINT8 pixel_data; |
| 670 | 648 | if (flipx) |
| 671 | 649 | { |
| 672 | | pixelData = (plane1 & 1) + ((plane2 & 1) << 1); |
| 650 | pixel_data = (plane1 & 1) + ((plane2 & 1) << 1); |
| 673 | 651 | plane1 = plane1 >> 1; |
| 674 | 652 | plane2 = plane2 >> 1; |
| 675 | 653 | } |
| 676 | 654 | else |
| 677 | 655 | { |
| 678 | | pixelData = ((plane1 >> 7) & 1) | (((plane2 >> 7) & 1) << 1); |
| 656 | pixel_data = ((plane1 >> 7) & 1) | (((plane2 >> 7) & 1) << 1); |
| 679 | 657 | plane1 = plane1 << 1; |
| 680 | 658 | plane2 = plane2 << 1; |
| 681 | 659 | } |
| 682 | 660 | |
| 683 | 661 | /* is this pixel non-transparent? */ |
| 684 | | if (spriteXPos + pixel >= first_pixel) |
| 662 | if (sprite_xpos + pixel >= first_pixel) |
| 685 | 663 | { |
| 686 | | if (pixelData) |
| 664 | if (pixel_data) |
| 687 | 665 | { |
| 688 | 666 | /* has another sprite been drawn here? */ |
| 689 | | if (!(line_priority[spriteXPos + pixel] & 0x01)) |
| 667 | if (!(line_priority[sprite_xpos + pixel] & 0x01)) |
| 690 | 668 | { |
| 691 | 669 | /* no, draw */ |
| 692 | | if ((spriteXPos + pixel) < VISIBLE_SCREEN_WIDTH) |
| 670 | if ((sprite_xpos + pixel) < VISIBLE_SCREEN_WIDTH) |
| 693 | 671 | { |
| 694 | | *BITMAP_ADDR16(bitmap, scanline, spriteXPos + pixel) = paldata[pixelData]; |
| 695 | | line_priority[spriteXPos + pixel] |= 0x01; |
| 672 | *BITMAP_ADDR16(bitmap, scanline, sprite_xpos + pixel) = paldata[pixel_data]; |
| 673 | line_priority[sprite_xpos + pixel] |= 0x01; |
| 696 | 674 | } |
| 697 | 675 | } |
| 698 | 676 | } |
| 699 | 677 | |
| 700 | 678 | /* set the "sprite 0 hit" flag if appropriate */ |
| 701 | | if (spriteIndex == 0 && (pixelData & 0x03) && ((spriteXPos + pixel) < 255) && (line_priority[spriteXPos + pixel] & 0x02)) |
| 679 | if (sprite_index == 0 && (pixel_data & 0x03) && ((sprite_xpos + pixel) < 255) && (line_priority[sprite_xpos + pixel] & 0x02)) |
| 702 | 680 | ppu_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT; |
| 703 | 681 | } |
| 704 | 682 | } |
| r7669 | r7670 | |
| 714 | 692 | |
| 715 | 693 | static void render_scanline( running_device *device ) |
| 716 | 694 | { |
| 717 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 718 | | const ppu2c0x_interface *intf = get_interface(device); |
| 695 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 719 | 696 | UINT8 line_priority[VISIBLE_SCREEN_WIDTH]; |
| 720 | | int *ppu_regs = &this_ppu->regs[0]; |
| 697 | int *ppu_regs = &ppu2c0x->regs[0]; |
| 721 | 698 | |
| 722 | 699 | /* lets see how long it takes */ |
| 723 | 700 | profiler_mark_start(PROFILER_USER1); |
| r7669 | r7670 | |
| 730 | 707 | draw_background(device, line_priority); |
| 731 | 708 | else |
| 732 | 709 | { |
| 733 | | bitmap_t *bitmap = this_ppu->bitmap; |
| 734 | | const int scanline = this_ppu->scanline; |
| 710 | bitmap_t *bitmap = ppu2c0x->bitmap; |
| 711 | const int scanline = ppu2c0x->scanline; |
| 735 | 712 | UINT8 color_mask; |
| 736 | 713 | UINT16 back_pen; |
| 737 | 714 | int i; |
| r7669 | r7670 | |
| 743 | 720 | color_mask = 0xff; |
| 744 | 721 | |
| 745 | 722 | /* cache the background pen */ |
| 746 | | back_pen = (this_ppu->back_color & color_mask) + intf->color_base; |
| 723 | back_pen = (ppu2c0x->back_color & color_mask) + ppu2c0x->color_base; |
| 747 | 724 | |
| 748 | 725 | // Fill this scanline with the background pen. |
| 749 | 726 | for (i = 0; i < bitmap->width; i++) |
| r7669 | r7670 | |
| 759 | 736 | |
| 760 | 737 | static void update_scanline( running_device *device ) |
| 761 | 738 | { |
| 762 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 763 | | const ppu2c0x_interface *intf = get_interface(device); |
| 764 | | int scanline = this_ppu->scanline; |
| 765 | | int *ppu_regs = &this_ppu->regs[0]; |
| 739 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 740 | int scanline = ppu2c0x->scanline; |
| 741 | int *ppu_regs = &ppu2c0x->regs[0]; |
| 766 | 742 | |
| 767 | 743 | if (scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 768 | 744 | { |
| r7669 | r7670 | |
| 771 | 747 | { |
| 772 | 748 | /* If background or sprites are enabled, copy the ppu address latch */ |
| 773 | 749 | /* Copy only the scroll x-coarse and the x-overflow bit */ |
| 774 | | this_ppu->refresh_data &= ~0x041f; |
| 775 | | this_ppu->refresh_data |= (this_ppu->refresh_latch & 0x041f); |
| 750 | ppu2c0x->refresh_data &= ~0x041f; |
| 751 | ppu2c0x->refresh_data |= (ppu2c0x->refresh_latch & 0x041f); |
| 776 | 752 | |
| 777 | | // logerror("updating refresh_data: %04x (scanline: %d)\n", this_ppu->refresh_data, this_ppu->scanline); |
| 753 | // logerror("updating refresh_data: %04x (scanline: %d)\n", ppu2c0x->refresh_data, ppu2c0x->scanline); |
| 778 | 754 | render_scanline(device); |
| 779 | 755 | } |
| 780 | 756 | else |
| 781 | 757 | { |
| 782 | | bitmap_t *bitmap = this_ppu->bitmap; |
| 783 | | const int scanline = this_ppu->scanline; |
| 758 | bitmap_t *bitmap = ppu2c0x->bitmap; |
| 759 | const int scanline = ppu2c0x->scanline; |
| 784 | 760 | UINT8 color_mask; |
| 785 | 761 | UINT16 back_pen; |
| 786 | 762 | int i; |
| r7669 | r7670 | |
| 792 | 768 | color_mask = 0xff; |
| 793 | 769 | |
| 794 | 770 | /* cache the background pen */ |
| 795 | | if (this_ppu->videomem_addr >= 0x3f00) |
| 771 | if (ppu2c0x->videomem_addr >= 0x3f00) |
| 796 | 772 | { |
| 797 | 773 | // If the PPU's VRAM address happens to point into palette ram space while |
| 798 | 774 | // both the sprites and background are disabled, the PPU paints the scanline |
| 799 | 775 | // with the palette entry at the VRAM address instead of the usual background |
| 800 | 776 | // pen. Micro Machines makes use of this feature. |
| 801 | | int penNum; |
| 777 | int pen_num; |
| 802 | 778 | |
| 803 | | if (this_ppu->videomem_addr & 0x03) |
| 804 | | penNum = this_ppu->palette_ram[this_ppu->videomem_addr & 0x1f]; |
| 779 | if (ppu2c0x->videomem_addr & 0x03) |
| 780 | pen_num = ppu2c0x->palette_ram[ppu2c0x->videomem_addr & 0x1f]; |
| 805 | 781 | else |
| 806 | | penNum = this_ppu->palette_ram[0]; |
| 782 | pen_num = ppu2c0x->palette_ram[0]; |
| 807 | 783 | |
| 808 | | back_pen = penNum + intf->color_base; |
| 784 | back_pen = pen_num + ppu2c0x->color_base; |
| 809 | 785 | } |
| 810 | 786 | else |
| 811 | | back_pen = (this_ppu->back_color & color_mask) + intf->color_base; |
| 787 | back_pen = (ppu2c0x->back_color & color_mask) + ppu2c0x->color_base; |
| 812 | 788 | |
| 813 | 789 | // Fill this scanline with the background pen. |
| 814 | 790 | for (i = 0; i < bitmap->width; i++) |
| r7669 | r7670 | |
| 816 | 792 | } |
| 817 | 793 | |
| 818 | 794 | /* increment the fine y-scroll */ |
| 819 | | this_ppu->refresh_data += 0x1000; |
| 795 | ppu2c0x->refresh_data += 0x1000; |
| 820 | 796 | |
| 821 | 797 | /* if it's rolled, increment the coarse y-scroll */ |
| 822 | | if (this_ppu->refresh_data & 0x8000) |
| 798 | if (ppu2c0x->refresh_data & 0x8000) |
| 823 | 799 | { |
| 824 | 800 | UINT16 tmp; |
| 825 | | tmp = (this_ppu->refresh_data & 0x03e0) + 0x20; |
| 826 | | this_ppu->refresh_data &= 0x7c1f; |
| 801 | tmp = (ppu2c0x->refresh_data & 0x03e0) + 0x20; |
| 802 | ppu2c0x->refresh_data &= 0x7c1f; |
| 827 | 803 | |
| 828 | 804 | /* handle bizarro scrolling rollover at the 30th (not 32nd) vertical tile */ |
| 829 | 805 | if (tmp == 0x03c0) |
| 830 | | this_ppu->refresh_data ^= 0x0800; |
| 806 | ppu2c0x->refresh_data ^= 0x0800; |
| 831 | 807 | else |
| 832 | | this_ppu->refresh_data |= (tmp & 0x03e0); |
| 808 | ppu2c0x->refresh_data |= (tmp & 0x03e0); |
| 833 | 809 | |
| 834 | | // logerror("updating refresh_data: %04x\n", this_ppu->refresh_data); |
| 810 | // logerror("updating refresh_data: %04x\n", ppu2c0x->refresh_data); |
| 835 | 811 | } |
| 836 | 812 | } |
| 837 | 813 | |
| r7669 | r7670 | |
| 840 | 816 | static TIMER_CALLBACK( scanline_callback ) |
| 841 | 817 | { |
| 842 | 818 | running_device *device = (running_device *)ptr; |
| 843 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 844 | | int *ppu_regs = &this_ppu->regs[0]; |
| 819 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 820 | int *ppu_regs = &ppu2c0x->regs[0]; |
| 845 | 821 | int blanked = (ppu_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) == 0; |
| 846 | | int vblank = ((this_ppu->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (this_ppu->scanline < this_ppu->scanlines_per_frame - 1)) ? 1 : 0; |
| 822 | int vblank = ((ppu2c0x->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (ppu2c0x->scanline < ppu2c0x->scanlines_per_frame - 1)) ? 1 : 0; |
| 847 | 823 | int next_scanline; |
| 848 | 824 | |
| 849 | 825 | /* if a callback is available, call it */ |
| 850 | | if (this_ppu->scanline_callback_proc != NULL) |
| 851 | | (*this_ppu->scanline_callback_proc)(device, this_ppu->scanline, vblank, blanked); |
| 826 | if (ppu2c0x->scanline_callback_proc != NULL) |
| 827 | (*ppu2c0x->scanline_callback_proc)(device, ppu2c0x->scanline, vblank, blanked); |
| 852 | 828 | |
| 853 | 829 | /* update the scanline that just went by */ |
| 854 | 830 | update_scanline(device); |
| 855 | 831 | |
| 856 | 832 | /* increment our scanline count */ |
| 857 | | this_ppu->scanline++; |
| 833 | ppu2c0x->scanline++; |
| 858 | 834 | |
| 859 | | // logerror("starting scanline %d (MAME %d, beam %d)\n", this_ppu->scanline, video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen)); |
| 835 | // logerror("starting scanline %d (MAME %d, beam %d)\n", ppu2c0x->scanline, video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen)); |
| 860 | 836 | |
| 861 | 837 | /* Note: this is called at the _end_ of each scanline */ |
| 862 | | if (this_ppu->scanline == PPU_VBLANK_FIRST_SCANLINE) |
| 838 | if (ppu2c0x->scanline == PPU_VBLANK_FIRST_SCANLINE) |
| 863 | 839 | { |
| 864 | 840 | // logerror("vblank starting\n"); |
| 865 | 841 | /* We just entered VBLANK */ |
| r7669 | r7670 | |
| 872 | 848 | // a game can read the high bit of $2002 before the NMI is called (potentially resetting the bit |
| 873 | 849 | // via a read from $2002 in the NMI handler). |
| 874 | 850 | // B-Wings is an example game that needs this. |
| 875 | | timer_adjust_oneshot(this_ppu->nmi_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 4), 0); |
| 851 | timer_adjust_oneshot(ppu2c0x->nmi_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 4), 0); |
| 876 | 852 | } |
| 877 | 853 | } |
| 878 | 854 | |
| 879 | | if (this_ppu->scanline == this_ppu->scanlines_per_frame - 1) |
| 855 | if (ppu2c0x->scanline == ppu2c0x->scanlines_per_frame - 1) |
| 880 | 856 | { |
| 881 | 857 | // logerror("vblank ending\n"); |
| 882 | 858 | /* clear the vblank & sprite hit flag */ |
| r7669 | r7670 | |
| 884 | 860 | } |
| 885 | 861 | |
| 886 | 862 | /* see if we rolled */ |
| 887 | | else if (this_ppu->scanline == this_ppu->scanlines_per_frame) |
| 863 | else if (ppu2c0x->scanline == ppu2c0x->scanlines_per_frame) |
| 888 | 864 | { |
| 889 | 865 | /* if background or sprites are enabled, copy the ppu address latch */ |
| 890 | 866 | if (!blanked) |
| 891 | | this_ppu->refresh_data = this_ppu->refresh_latch; |
| 867 | ppu2c0x->refresh_data = ppu2c0x->refresh_latch; |
| 892 | 868 | |
| 893 | 869 | /* reset the scanline count */ |
| 894 | | this_ppu->scanline = 0; |
| 895 | | // logerror("sprite 0 x: %d y: %d num: %d\n", this_ppu->spriteram[3], this_ppu->spriteram[0] + 1, this_ppu->spriteram[1]); |
| 870 | ppu2c0x->scanline = 0; |
| 871 | // logerror("sprite 0 x: %d y: %d num: %d\n", ppu2c0x->spriteram[3], ppu2c0x->spriteram[0] + 1, ppu2c0x->spriteram[1]); |
| 896 | 872 | } |
| 897 | 873 | |
| 898 | | next_scanline = this_ppu->scanline + 1; |
| 899 | | if (next_scanline == this_ppu->scanlines_per_frame) |
| 874 | next_scanline = ppu2c0x->scanline + 1; |
| 875 | if (next_scanline == ppu2c0x->scanlines_per_frame) |
| 900 | 876 | next_scanline = 0; |
| 901 | 877 | |
| 902 | 878 | // Call us back when the hblank starts for this scanline |
| 903 | | timer_adjust_oneshot(this_ppu->hblank_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 86.67), 0); // ??? FIXME - hardcoding NTSC, need better calculation |
| 879 | timer_adjust_oneshot(ppu2c0x->hblank_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 86.67), 0); // ??? FIXME - hardcoding NTSC, need better calculation |
| 904 | 880 | |
| 905 | 881 | // trigger again at the start of the next scanline |
| 906 | | timer_adjust_oneshot(this_ppu->scanline_timer, video_screen_get_time_until_pos(device->machine->primary_screen, next_scanline * this_ppu->scan_scale, 0), 0); |
| 882 | timer_adjust_oneshot(ppu2c0x->scanline_timer, video_screen_get_time_until_pos(device->machine->primary_screen, next_scanline * ppu2c0x->scan_scale, 0), 0); |
| 907 | 883 | } |
| 908 | 884 | |
| 909 | 885 | /************************************* |
| 910 | | * |
| 911 | | * PPU Reset |
| 912 | | * |
| 913 | | *************************************/ |
| 914 | | |
| 915 | | static DEVICE_RESET( ppu2c0x ) |
| 916 | | { |
| 917 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 918 | | const ppu2c0x_interface *intf = get_interface(device); |
| 919 | | int scan_scale = 1; |
| 920 | | int i; |
| 921 | | |
| 922 | | /* reset the scanline count */ |
| 923 | | this_ppu->scanline = 0; |
| 924 | | |
| 925 | | /* set the scan scale (this is for dual monitor vertical setups) */ |
| 926 | | this_ppu->scan_scale = scan_scale; |
| 927 | | |
| 928 | | timer_adjust_oneshot(this_ppu->nmi_timer, attotime_never, 0); |
| 929 | | |
| 930 | | // Call us back when the hblank starts for this scanline |
| 931 | | timer_adjust_oneshot(this_ppu->hblank_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 86.67), 0); // ??? FIXME - hardcoding NTSC, need better calculation |
| 932 | | |
| 933 | | // Call us back at the start of the next scanline |
| 934 | | timer_adjust_oneshot(this_ppu->scanline_timer, video_screen_get_time_until_pos(device->machine->primary_screen, 1, 0), 0); |
| 935 | | |
| 936 | | /* reset the callbacks */ |
| 937 | | this_ppu->scanline_callback_proc = 0; |
| 938 | | this_ppu->vidaccess_callback_proc = 0; |
| 939 | | |
| 940 | | for (i = 0; i < PPU_MAX_REG; i++) |
| 941 | | this_ppu->regs[i] = 0; |
| 942 | | |
| 943 | | /* initialize the rest of the members */ |
| 944 | | this_ppu->refresh_data = 0; |
| 945 | | this_ppu->refresh_latch = 0; |
| 946 | | this_ppu->x_fine = 0; |
| 947 | | this_ppu->toggle = 0; |
| 948 | | this_ppu->add = 1; |
| 949 | | this_ppu->videomem_addr = 0; |
| 950 | | this_ppu->addr_latch = 0; |
| 951 | | this_ppu->data_latch = 0; |
| 952 | | this_ppu->tile_page = 0; |
| 953 | | this_ppu->sprite_page = 0; |
| 954 | | this_ppu->back_color = 0; |
| 955 | | |
| 956 | | /* initialize the color tables */ |
| 957 | | { |
| 958 | | int color_base = intf->color_base; |
| 959 | | |
| 960 | | for (i = 0; i < ARRAY_LENGTH(default_colortable_mono); i++) |
| 961 | | { |
| 962 | | /* monochromatic table */ |
| 963 | | this_ppu->colortable_mono[i] = default_colortable_mono[i] + color_base; |
| 964 | | |
| 965 | | /* color table */ |
| 966 | | this_ppu->colortable[i] = default_colortable[i] + color_base; |
| 967 | | } |
| 968 | | } |
| 969 | | } |
| 970 | | |
| 971 | | /************************************* |
| 972 | 886 | * |
| 973 | 887 | * PPU Memory functions |
| 974 | 888 | * |
| r7669 | r7670 | |
| 976 | 890 | |
| 977 | 891 | static WRITE8_HANDLER( ppu2c0x_palette_write ) |
| 978 | 892 | { |
| 979 | | ppu2c0x_chip *this_ppu = get_token(space->cpu); |
| 980 | | const ppu2c0x_interface *intf = get_interface(space->cpu); |
| 981 | | int color_base = intf->color_base; |
| 893 | ppu2c0x_state *ppu2c0x = get_token(space->cpu); |
| 894 | int color_base = ppu2c0x->color_base; |
| 982 | 895 | |
| 983 | | int colorEmphasis = (this_ppu->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS) * 2; |
| 896 | int color_emphasis = (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS) * 2; |
| 984 | 897 | // it's a palette |
| 985 | 898 | // transparent pens are mirrored! |
| 986 | 899 | if (offset & 0x3) |
| 987 | 900 | { |
| 988 | | this_ppu->palette_ram[offset & 0x1f] = data; |
| 989 | | this_ppu->colortable[offset & 0x1f] = color_base + data + colorEmphasis; |
| 990 | | this_ppu->colortable_mono[offset & 0x1f] = color_base + (data & 0xf0) + colorEmphasis; |
| 901 | ppu2c0x->palette_ram[offset & 0x1f] = data; |
| 902 | ppu2c0x->colortable[offset & 0x1f] = color_base + data + color_emphasis; |
| 903 | ppu2c0x->colortable_mono[offset & 0x1f] = color_base + (data & 0xf0) + color_emphasis; |
| 991 | 904 | } |
| 992 | 905 | else |
| 993 | 906 | { |
| 994 | 907 | int i; |
| 995 | 908 | if (0 == (offset & 0xf)) |
| 996 | 909 | { |
| 997 | | this_ppu->back_color = data; |
| 910 | ppu2c0x->back_color = data; |
| 998 | 911 | for (i = 0; i < 32; i += 4) |
| 999 | 912 | { |
| 1000 | | this_ppu->colortable[i] = color_base + data + colorEmphasis; |
| 1001 | | this_ppu->colortable_mono[i] = color_base + (data & 0xf0) + colorEmphasis; |
| 913 | ppu2c0x->colortable[i] = color_base + data + color_emphasis; |
| 914 | ppu2c0x->colortable_mono[i] = color_base + (data & 0xf0) + color_emphasis; |
| 1002 | 915 | } |
| 1003 | 916 | } |
| 1004 | | this_ppu->palette_ram[offset & 0xf] = this_ppu->palette_ram[(offset & 0xf) + 0x10] = data; |
| 917 | ppu2c0x->palette_ram[offset & 0xf] = ppu2c0x->palette_ram[(offset & 0xf) + 0x10] = data; |
| 1005 | 918 | } |
| 1006 | 919 | } |
| 1007 | 920 | |
| 1008 | 921 | static READ8_HANDLER( ppu2c0x_palette_read ) |
| 1009 | 922 | { |
| 1010 | | ppu2c0x_chip *this_ppu = get_token(space->cpu); |
| 923 | ppu2c0x_state *ppu2c0x = get_token(space->cpu); |
| 1011 | 924 | { |
| 1012 | | if (this_ppu->regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 1013 | | return (this_ppu->palette_ram[offset & 0x1f] & 0x30); |
| 925 | if (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 926 | return (ppu2c0x->palette_ram[offset & 0x1f] & 0x30); |
| 1014 | 927 | |
| 1015 | 928 | else |
| 1016 | | return (this_ppu->palette_ram[offset & 0x1f] & 0x3f); |
| 929 | return (ppu2c0x->palette_ram[offset & 0x1f] & 0x3f); |
| 1017 | 930 | } |
| 1018 | 931 | } |
| 1019 | 932 | |
| r7669 | r7670 | |
| 1025 | 938 | |
| 1026 | 939 | READ8_DEVICE_HANDLER( ppu2c0x_r ) |
| 1027 | 940 | { |
| 1028 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 941 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1029 | 942 | |
| 1030 | 943 | if (offset >= PPU_MAX_REG) |
| 1031 | 944 | { |
| r7669 | r7670 | |
| 1039 | 952 | case PPU_STATUS: |
| 1040 | 953 | // The top 3 bits of the status register are the only ones that report data. The |
| 1041 | 954 | // remainder contain whatever was last in the PPU data latch. |
| 1042 | | this_ppu->data_latch = this_ppu->regs[PPU_STATUS] | (this_ppu->data_latch & 0x1f); |
| 955 | ppu2c0x->data_latch = ppu2c0x->regs[PPU_STATUS] | (ppu2c0x->data_latch & 0x1f); |
| 1043 | 956 | |
| 1044 | 957 | // Reset hi/lo scroll toggle |
| 1045 | | this_ppu->toggle = 0; |
| 958 | ppu2c0x->toggle = 0; |
| 1046 | 959 | |
| 1047 | 960 | // If the vblank bit is set, clear all status bits but the 2 sprite flags |
| 1048 | | if (this_ppu->data_latch & PPU_STATUS_VBLANK) |
| 1049 | | this_ppu->regs[PPU_STATUS] &= 0x60; |
| 961 | if (ppu2c0x->data_latch & PPU_STATUS_VBLANK) |
| 962 | ppu2c0x->regs[PPU_STATUS] &= 0x60; |
| 1050 | 963 | break; |
| 1051 | 964 | |
| 1052 | 965 | case PPU_SPRITE_DATA: |
| 1053 | | this_ppu->data_latch = this_ppu->spriteram[this_ppu->regs[PPU_SPRITE_ADDRESS]]; |
| 966 | ppu2c0x->data_latch = ppu2c0x->spriteram[ppu2c0x->regs[PPU_SPRITE_ADDRESS]]; |
| 1054 | 967 | break; |
| 1055 | 968 | |
| 1056 | 969 | case PPU_DATA: |
| 1057 | 970 | if (ppu_latch) |
| 1058 | | (*ppu_latch)(device, this_ppu->videomem_addr & 0x3fff); |
| 971 | (*ppu_latch)(device, ppu2c0x->videomem_addr & 0x3fff); |
| 1059 | 972 | |
| 1060 | | if (this_ppu->videomem_addr >= 0x3f00) |
| 973 | if (ppu2c0x->videomem_addr >= 0x3f00) |
| 1061 | 974 | { |
| 1062 | | this_ppu->data_latch = memory_read_byte(device->space(), this_ppu->videomem_addr); |
| 975 | ppu2c0x->data_latch = memory_read_byte(device->space(), ppu2c0x->videomem_addr); |
| 1063 | 976 | // buffer the mirrored NT data |
| 1064 | | this_ppu->buffered_data = memory_read_byte(device->space(), this_ppu->videomem_addr & 0x2fff); |
| 977 | ppu2c0x->buffered_data = memory_read_byte(device->space(), ppu2c0x->videomem_addr & 0x2fff); |
| 1065 | 978 | } |
| 1066 | 979 | else |
| 1067 | 980 | { |
| 1068 | | this_ppu->data_latch = this_ppu->buffered_data; |
| 1069 | | this_ppu->buffered_data = memory_read_byte(device->space(), this_ppu->videomem_addr); |
| 981 | ppu2c0x->data_latch = ppu2c0x->buffered_data; |
| 982 | ppu2c0x->buffered_data = memory_read_byte(device->space(), ppu2c0x->videomem_addr); |
| 1070 | 983 | } |
| 1071 | 984 | |
| 1072 | | this_ppu->videomem_addr += this_ppu->add; |
| 985 | ppu2c0x->videomem_addr += ppu2c0x->add; |
| 1073 | 986 | break; |
| 1074 | 987 | |
| 1075 | 988 | default: |
| 1076 | 989 | break; |
| 1077 | 990 | } |
| 1078 | 991 | |
| 1079 | | return this_ppu->data_latch; |
| 992 | return ppu2c0x->data_latch; |
| 1080 | 993 | } |
| 1081 | 994 | |
| 1082 | 995 | |
| r7669 | r7670 | |
| 1088 | 1001 | |
| 1089 | 1002 | WRITE8_DEVICE_HANDLER( ppu2c0x_w ) |
| 1090 | 1003 | { |
| 1091 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 1092 | | const ppu2c0x_interface *intf = get_interface(device); |
| 1004 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1093 | 1005 | int color_base; |
| 1094 | 1006 | |
| 1095 | | color_base = intf->color_base; |
| 1007 | color_base = ppu2c0x->color_base; |
| 1096 | 1008 | |
| 1097 | 1009 | if (offset >= PPU_MAX_REG) |
| 1098 | 1010 | { |
| r7669 | r7670 | |
| 1101 | 1013 | } |
| 1102 | 1014 | |
| 1103 | 1015 | #ifdef MAME_DEBUG |
| 1104 | | if (this_ppu->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 1016 | if (ppu2c0x->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 1105 | 1017 | { |
| 1106 | 1018 | running_device *screen = device->machine->primary_screen; |
| 1107 | | logerror("PPU register %d write %02x during non-vblank scanline %d (MAME %d, beam pos: %d)\n", offset, data, this_ppu->scanline, video_screen_get_vpos(screen), video_screen_get_hpos(screen)); |
| 1019 | logerror("PPU register %d write %02x during non-vblank scanline %d (MAME %d, beam pos: %d)\n", offset, data, ppu2c0x->scanline, video_screen_get_vpos(screen), video_screen_get_hpos(screen)); |
| 1108 | 1020 | } |
| 1109 | 1021 | #endif |
| 1110 | 1022 | |
| 1111 | 1023 | switch (offset & 7) |
| 1112 | 1024 | { |
| 1113 | 1025 | case PPU_CONTROL0: |
| 1114 | | this_ppu->regs[PPU_CONTROL0] = data; |
| 1026 | ppu2c0x->regs[PPU_CONTROL0] = data; |
| 1115 | 1027 | |
| 1116 | 1028 | /* update the name table number on our refresh latches */ |
| 1117 | | this_ppu->refresh_latch &= 0x73ff; |
| 1118 | | this_ppu->refresh_latch |= (data & 3) << 10; |
| 1029 | ppu2c0x->refresh_latch &= 0x73ff; |
| 1030 | ppu2c0x->refresh_latch |= (data & 3) << 10; |
| 1119 | 1031 | |
| 1120 | 1032 | /* the char ram bank points either 0x0000 or 0x1000 (page 0 or page 4) */ |
| 1121 | | this_ppu->tile_page = (data & PPU_CONTROL0_CHR_SELECT) >> 2; |
| 1122 | | this_ppu->sprite_page = (data & PPU_CONTROL0_SPR_SELECT) >> 1; |
| 1033 | ppu2c0x->tile_page = (data & PPU_CONTROL0_CHR_SELECT) >> 2; |
| 1034 | ppu2c0x->sprite_page = (data & PPU_CONTROL0_SPR_SELECT) >> 1; |
| 1123 | 1035 | |
| 1124 | | this_ppu->add = (data & PPU_CONTROL0_INC) ? 32 : 1; |
| 1125 | | // logerror("control0 write: %02x (scanline: %d)\n", data, this_ppu->scanline); |
| 1036 | ppu2c0x->add = (data & PPU_CONTROL0_INC) ? 32 : 1; |
| 1037 | // logerror("control0 write: %02x (scanline: %d)\n", data, ppu2c0x->scanline); |
| 1126 | 1038 | break; |
| 1127 | 1039 | |
| 1128 | 1040 | case PPU_CONTROL1: |
| 1129 | 1041 | /* if color intensity has changed, change all the color tables to reflect them */ |
| 1130 | | if ((data & PPU_CONTROL1_COLOR_EMPHASIS) != (this_ppu->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS)) |
| 1042 | if ((data & PPU_CONTROL1_COLOR_EMPHASIS) != (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS)) |
| 1131 | 1043 | { |
| 1132 | 1044 | int i; |
| 1133 | 1045 | for (i = 0; i <= 0x1f; i ++) |
| 1134 | 1046 | { |
| 1135 | | UINT8 oldColor = this_ppu->palette_ram[i]; |
| 1047 | UINT8 oldColor = ppu2c0x->palette_ram[i]; |
| 1136 | 1048 | |
| 1137 | | this_ppu->colortable[i] = color_base + oldColor + (data & PPU_CONTROL1_COLOR_EMPHASIS) * 2; |
| 1049 | ppu2c0x->colortable[i] = color_base + oldColor + (data & PPU_CONTROL1_COLOR_EMPHASIS) * 2; |
| 1138 | 1050 | } |
| 1139 | 1051 | } |
| 1140 | 1052 | |
| 1141 | | // logerror("control1 write: %02x (scanline: %d)\n", data, this_ppu->scanline); |
| 1142 | | this_ppu->regs[PPU_CONTROL1] = data; |
| 1053 | // logerror("control1 write: %02x (scanline: %d)\n", data, ppu2c0x->scanline); |
| 1054 | ppu2c0x->regs[PPU_CONTROL1] = data; |
| 1143 | 1055 | break; |
| 1144 | 1056 | |
| 1145 | 1057 | case PPU_SPRITE_ADDRESS: |
| 1146 | | this_ppu->regs[PPU_SPRITE_ADDRESS] = data; |
| 1058 | ppu2c0x->regs[PPU_SPRITE_ADDRESS] = data; |
| 1147 | 1059 | break; |
| 1148 | 1060 | |
| 1149 | 1061 | case PPU_SPRITE_DATA: |
| 1150 | 1062 | // If the PPU is currently rendering the screen, 0xff is written instead of the desired data. |
| 1151 | | if (this_ppu->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 1063 | if (ppu2c0x->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 1152 | 1064 | data = 0xff; |
| 1153 | | this_ppu->spriteram[this_ppu->regs[PPU_SPRITE_ADDRESS]] = data; |
| 1154 | | this_ppu->regs[PPU_SPRITE_ADDRESS] = (this_ppu->regs[PPU_SPRITE_ADDRESS] + 1) & 0xff; |
| 1065 | ppu2c0x->spriteram[ppu2c0x->regs[PPU_SPRITE_ADDRESS]] = data; |
| 1066 | ppu2c0x->regs[PPU_SPRITE_ADDRESS] = (ppu2c0x->regs[PPU_SPRITE_ADDRESS] + 1) & 0xff; |
| 1155 | 1067 | break; |
| 1156 | 1068 | |
| 1157 | 1069 | case PPU_SCROLL: |
| 1158 | | if (this_ppu->toggle) |
| 1070 | if (ppu2c0x->toggle) |
| 1159 | 1071 | { |
| 1160 | 1072 | /* second write */ |
| 1161 | | this_ppu->refresh_latch &= 0x0c1f; |
| 1162 | | this_ppu->refresh_latch |= (data & 0xf8) << 2; |
| 1163 | | this_ppu->refresh_latch |= (data & 0x07) << 12; |
| 1164 | | // logerror("scroll write 2: %d, %04x (scanline: %d)\n", data, this_ppu->refresh_latch, this_ppu->scanline); |
| 1073 | ppu2c0x->refresh_latch &= 0x0c1f; |
| 1074 | ppu2c0x->refresh_latch |= (data & 0xf8) << 2; |
| 1075 | ppu2c0x->refresh_latch |= (data & 0x07) << 12; |
| 1076 | // logerror("scroll write 2: %d, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline); |
| 1165 | 1077 | } |
| 1166 | 1078 | else |
| 1167 | 1079 | { |
| 1168 | 1080 | /* first write */ |
| 1169 | | this_ppu->refresh_latch &= 0x7fe0; |
| 1170 | | this_ppu->refresh_latch |= (data & 0xf8) >> 3; |
| 1081 | ppu2c0x->refresh_latch &= 0x7fe0; |
| 1082 | ppu2c0x->refresh_latch |= (data & 0xf8) >> 3; |
| 1171 | 1083 | |
| 1172 | | this_ppu->x_fine = data & 7; |
| 1173 | | // logerror("scroll write 1: %d, %04x (scanline: %d)\n", data, this_ppu->refresh_latch, this_ppu->scanline); |
| 1084 | ppu2c0x->x_fine = data & 7; |
| 1085 | // logerror("scroll write 1: %d, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline); |
| 1174 | 1086 | } |
| 1175 | 1087 | |
| 1176 | | this_ppu->toggle ^= 1; |
| 1088 | ppu2c0x->toggle ^= 1; |
| 1177 | 1089 | break; |
| 1178 | 1090 | |
| 1179 | 1091 | case PPU_ADDRESS: |
| 1180 | | if (this_ppu->toggle) |
| 1092 | if (ppu2c0x->toggle) |
| 1181 | 1093 | { |
| 1182 | 1094 | /* second write */ |
| 1183 | | this_ppu->refresh_latch &= 0x7f00; |
| 1184 | | this_ppu->refresh_latch |= data; |
| 1185 | | this_ppu->refresh_data = this_ppu->refresh_latch; |
| 1095 | ppu2c0x->refresh_latch &= 0x7f00; |
| 1096 | ppu2c0x->refresh_latch |= data; |
| 1097 | ppu2c0x->refresh_data = ppu2c0x->refresh_latch; |
| 1186 | 1098 | |
| 1187 | | this_ppu->videomem_addr = this_ppu->refresh_latch; |
| 1188 | | // logerror("vram addr write 2: %02x, %04x (scanline: %d)\n", data, this_ppu->refresh_latch, this_ppu->scanline); |
| 1099 | ppu2c0x->videomem_addr = ppu2c0x->refresh_latch; |
| 1100 | // logerror("vram addr write 2: %02x, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline); |
| 1189 | 1101 | } |
| 1190 | 1102 | else |
| 1191 | 1103 | { |
| 1192 | 1104 | /* first write */ |
| 1193 | | this_ppu->refresh_latch &= 0x00ff; |
| 1194 | | this_ppu->refresh_latch |= (data & 0x3f) << 8; |
| 1195 | | // logerror("vram addr write 1: %02x, %04x (scanline: %d)\n", data, this_ppu->refresh_latch, this_ppu->scanline); |
| 1105 | ppu2c0x->refresh_latch &= 0x00ff; |
| 1106 | ppu2c0x->refresh_latch |= (data & 0x3f) << 8; |
| 1107 | // logerror("vram addr write 1: %02x, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline); |
| 1196 | 1108 | } |
| 1197 | 1109 | |
| 1198 | | this_ppu->toggle ^= 1; |
| 1110 | ppu2c0x->toggle ^= 1; |
| 1199 | 1111 | break; |
| 1200 | 1112 | |
| 1201 | 1113 | case PPU_DATA: |
| 1202 | 1114 | { |
| 1203 | | int tempAddr = this_ppu->videomem_addr & 0x3fff; |
| 1115 | int tempAddr = ppu2c0x->videomem_addr & 0x3fff; |
| 1204 | 1116 | |
| 1205 | 1117 | if (ppu_latch) |
| 1206 | 1118 | (*ppu_latch)(device, tempAddr); |
| 1207 | 1119 | |
| 1208 | 1120 | /* if there's a callback, call it now */ |
| 1209 | | if (this_ppu->vidaccess_callback_proc) |
| 1210 | | data = (*this_ppu->vidaccess_callback_proc)(device, tempAddr, data); |
| 1121 | if (ppu2c0x->vidaccess_callback_proc) |
| 1122 | data = (*ppu2c0x->vidaccess_callback_proc)(device, tempAddr, data); |
| 1211 | 1123 | |
| 1212 | 1124 | /* see if it's on the chargen portion */ |
| 1213 | 1125 | if (tempAddr < 0x2000) |
| r7669 | r7670 | |
| 1221 | 1133 | memory_write_byte(device->space(), tempAddr, data); |
| 1222 | 1134 | } |
| 1223 | 1135 | /* increment the address */ |
| 1224 | | this_ppu->videomem_addr += this_ppu->add; |
| 1136 | ppu2c0x->videomem_addr += ppu2c0x->add; |
| 1225 | 1137 | } |
| 1226 | 1138 | break; |
| 1227 | 1139 | |
| r7669 | r7670 | |
| 1260 | 1172 | |
| 1261 | 1173 | void ppu2c0x_render( running_device *device, bitmap_t *bitmap, int flipx, int flipy, int sx, int sy ) |
| 1262 | 1174 | { |
| 1263 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 1264 | | copybitmap(bitmap, this_ppu->bitmap, flipx, flipy, sx, sy, 0); |
| 1175 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1176 | copybitmap(bitmap, ppu2c0x->bitmap, flipx, flipy, sx, sy, 0); |
| 1265 | 1177 | } |
| 1266 | 1178 | |
| 1267 | 1179 | /************************************* |
| r7669 | r7670 | |
| 1272 | 1184 | |
| 1273 | 1185 | int ppu2c0x_get_pixel( running_device *device, int x, int y ) |
| 1274 | 1186 | { |
| 1275 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 1187 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1276 | 1188 | |
| 1277 | 1189 | if (x >= VISIBLE_SCREEN_WIDTH) |
| 1278 | 1190 | x = VISIBLE_SCREEN_WIDTH - 1; |
| r7669 | r7670 | |
| 1280 | 1192 | if (y >= VISIBLE_SCREEN_HEIGHT) |
| 1281 | 1193 | y = VISIBLE_SCREEN_HEIGHT - 1; |
| 1282 | 1194 | |
| 1283 | | return *BITMAP_ADDR16(this_ppu->bitmap, y, x); |
| 1195 | return *BITMAP_ADDR16(ppu2c0x->bitmap, y, x); |
| 1284 | 1196 | } |
| 1285 | 1197 | |
| 1286 | 1198 | int ppu2c0x_get_colorbase( running_device *device ) |
| 1287 | 1199 | { |
| 1288 | | const ppu2c0x_interface *intf = get_interface(device); |
| 1289 | | return intf->color_base; |
| 1200 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1201 | return ppu2c0x->color_base; |
| 1290 | 1202 | } |
| 1291 | 1203 | |
| 1292 | 1204 | int ppu2c0x_get_current_scanline( running_device *device ) |
| 1293 | 1205 | { |
| 1294 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 1295 | | return this_ppu->scanline; |
| 1206 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1207 | return ppu2c0x->scanline; |
| 1296 | 1208 | } |
| 1297 | 1209 | |
| 1298 | 1210 | void ppu2c0x_set_scanline_callback( running_device *device, ppu2c0x_scanline_cb cb ) |
| 1299 | 1211 | { |
| 1300 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 1301 | | this_ppu->scanline_callback_proc = cb; |
| 1212 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1213 | ppu2c0x->scanline_callback_proc = cb; |
| 1302 | 1214 | } |
| 1303 | 1215 | |
| 1304 | 1216 | void ppu2c0x_set_hblank_callback( running_device *device, ppu2c0x_hblank_cb cb ) |
| 1305 | 1217 | { |
| 1306 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 1307 | | this_ppu->hblank_callback_proc = cb; |
| 1218 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1219 | ppu2c0x->hblank_callback_proc = cb; |
| 1308 | 1220 | } |
| 1309 | 1221 | |
| 1310 | 1222 | void ppu2c0x_set_vidaccess_callback( running_device *device, ppu2c0x_vidaccess_cb cb ) |
| 1311 | 1223 | { |
| 1312 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 1313 | | this_ppu->vidaccess_callback_proc = cb; |
| 1224 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1225 | ppu2c0x->vidaccess_callback_proc = cb; |
| 1314 | 1226 | } |
| 1315 | 1227 | |
| 1316 | 1228 | void ppu2c0x_set_scanlines_per_frame( running_device *device, int scanlines ) |
| 1317 | 1229 | { |
| 1318 | | ppu2c0x_chip *this_ppu = get_token(device); |
| 1319 | | this_ppu->scanlines_per_frame = scanlines; |
| 1230 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1231 | ppu2c0x->scanlines_per_frame = scanlines; |
| 1320 | 1232 | } |
| 1321 | 1233 | |
| 1322 | 1234 | |
| 1235 | /************************************* |
| 1236 | * |
| 1237 | * PPU Start |
| 1238 | * |
| 1239 | *************************************/ |
| 1240 | |
| 1241 | static DEVICE_START( ppu2c0x ) |
| 1242 | { |
| 1243 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1244 | const ppu2c0x_interface *intf = get_interface(device); |
| 1245 | |
| 1246 | memset(ppu2c0x, 0, sizeof(*ppu2c0x)); |
| 1247 | ppu2c0x->scanlines_per_frame = (int) device->get_config_int(PPU2C0XINFO_INT_SCANLINES_PER_FRAME); |
| 1248 | |
| 1249 | /* initialize the scanline handling portion */ |
| 1250 | ppu2c0x->scanline_timer = timer_alloc(device->machine, scanline_callback, (void *) device); |
| 1251 | timer_adjust_oneshot(ppu2c0x->scanline_timer, video_screen_get_time_until_pos(device->machine->primary_screen, 1, 0), 0); |
| 1252 | |
| 1253 | ppu2c0x->hblank_timer = timer_alloc(device->machine, hblank_callback, (void *) device); |
| 1254 | timer_adjust_oneshot(ppu2c0x->hblank_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 86.67), 0); // ??? FIXME - hardcoding NTSC, need better calculation |
| 1255 | |
| 1256 | ppu2c0x->nmi_timer = timer_alloc(device->machine, nmi_callback, (void *) device); |
| 1257 | timer_adjust_oneshot(ppu2c0x->nmi_timer, attotime_never, 0); |
| 1258 | |
| 1259 | ppu2c0x->nmi_callback_proc = intf->nmi_handler; |
| 1260 | ppu2c0x->color_base = intf->color_base; |
| 1261 | |
| 1262 | /* allocate a screen bitmap, videomem and spriteram, a dirtychar array and the monochromatic colortable */ |
| 1263 | ppu2c0x->bitmap = auto_bitmap_alloc(device->machine, VISIBLE_SCREEN_WIDTH, VISIBLE_SCREEN_HEIGHT, video_screen_get_format(device->machine->primary_screen)); |
| 1264 | ppu2c0x->spriteram = auto_alloc_array_clear(device->machine, UINT8, SPRITERAM_SIZE); |
| 1265 | ppu2c0x->colortable = auto_alloc_array(device->machine, pen_t, ARRAY_LENGTH(default_colortable)); |
| 1266 | ppu2c0x->colortable_mono = auto_alloc_array(device->machine, pen_t, ARRAY_LENGTH(default_colortable_mono)); |
| 1267 | |
| 1268 | state_save_register_device_item(device, 0, ppu2c0x->scanline); |
| 1269 | state_save_register_device_item(device, 0, ppu2c0x->refresh_data); |
| 1270 | state_save_register_device_item(device, 0, ppu2c0x->refresh_latch); |
| 1271 | state_save_register_device_item(device, 0, ppu2c0x->x_fine); |
| 1272 | state_save_register_device_item(device, 0, ppu2c0x->toggle); |
| 1273 | state_save_register_device_item(device, 0, ppu2c0x->add); |
| 1274 | state_save_register_device_item(device, 0, ppu2c0x->videomem_addr); |
| 1275 | state_save_register_device_item(device, 0, ppu2c0x->addr_latch); |
| 1276 | state_save_register_device_item(device, 0, ppu2c0x->data_latch); |
| 1277 | state_save_register_device_item(device, 0, ppu2c0x->buffered_data); |
| 1278 | state_save_register_device_item(device, 0, ppu2c0x->tile_page); |
| 1279 | state_save_register_device_item(device, 0, ppu2c0x->sprite_page); |
| 1280 | state_save_register_device_item(device, 0, ppu2c0x->back_color); |
| 1281 | state_save_register_device_item(device, 0, ppu2c0x->scan_scale); |
| 1282 | state_save_register_device_item(device, 0, ppu2c0x->scanlines_per_frame); |
| 1283 | state_save_register_device_item_array(device, 0, ppu2c0x->regs); |
| 1284 | state_save_register_device_item_array(device, 0, ppu2c0x->palette_ram); |
| 1285 | state_save_register_device_item_pointer(device, 0, ppu2c0x->spriteram, SPRITERAM_SIZE); |
| 1286 | state_save_register_device_item_pointer(device, 0, ppu2c0x->colortable, ARRAY_LENGTH(default_colortable)); |
| 1287 | state_save_register_device_item_pointer(device, 0, ppu2c0x->colortable_mono, ARRAY_LENGTH(default_colortable_mono)); |
| 1288 | state_save_register_device_item_bitmap(device, 0, ppu2c0x->bitmap); |
| 1289 | } |
| 1290 | |
| 1291 | /************************************* |
| 1292 | * |
| 1293 | * PPU Reset |
| 1294 | * |
| 1295 | *************************************/ |
| 1296 | |
| 1297 | static DEVICE_RESET( ppu2c0x ) |
| 1298 | { |
| 1299 | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1300 | const ppu2c0x_interface *intf = get_interface(device); |
| 1301 | int i; |
| 1302 | |
| 1303 | /* reset the scanline count */ |
| 1304 | ppu2c0x->scanline = 0; |
| 1305 | |
| 1306 | /* set the scan scale (this is for dual monitor vertical setups) */ |
| 1307 | ppu2c0x->scan_scale = 1; |
| 1308 | |
| 1309 | /* reset the callbacks */ |
| 1310 | ppu2c0x->scanline_callback_proc = NULL; |
| 1311 | ppu2c0x->vidaccess_callback_proc = NULL; |
| 1312 | |
| 1313 | for (i = 0; i < PPU_MAX_REG; i++) |
| 1314 | ppu2c0x->regs[i] = 0; |
| 1315 | |
| 1316 | memset(ppu2c0x->palette_ram, 0, ARRAY_LENGTH(ppu2c0x->palette_ram)); |
| 1317 | |
| 1318 | /* initialize the rest of the members */ |
| 1319 | ppu2c0x->refresh_data = 0; |
| 1320 | ppu2c0x->refresh_latch = 0; |
| 1321 | ppu2c0x->x_fine = 0; |
| 1322 | ppu2c0x->toggle = 0; |
| 1323 | ppu2c0x->add = 1; |
| 1324 | ppu2c0x->videomem_addr = 0; |
| 1325 | ppu2c0x->addr_latch = 0; |
| 1326 | ppu2c0x->data_latch = 0; |
| 1327 | ppu2c0x->tile_page = 0; |
| 1328 | ppu2c0x->sprite_page = 0; |
| 1329 | ppu2c0x->back_color = 0; |
| 1330 | ppu2c0x->buffered_data = 0; |
| 1331 | |
| 1332 | /* initialize the color tables */ |
| 1333 | { |
| 1334 | int color_base = intf->color_base; |
| 1335 | |
| 1336 | for (i = 0; i < ARRAY_LENGTH(default_colortable_mono); i++) |
| 1337 | { |
| 1338 | /* monochromatic table */ |
| 1339 | ppu2c0x->colortable_mono[i] = default_colortable_mono[i] + color_base; |
| 1340 | |
| 1341 | /* color table */ |
| 1342 | ppu2c0x->colortable[i] = default_colortable[i] + color_base; |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1323 | 1347 | /*************************************************************************** |
| 1324 | 1348 | GET INFO FUNCTIONS |
| 1325 | 1349 | ***************************************************************************/ |
| r7669 | r7670 | |
| 1329 | 1353 | switch (state) |
| 1330 | 1354 | { |
| 1331 | 1355 | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 1332 | | case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(ppu2c0x_chip); break; |
| 1356 | case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(ppu2c0x_state); break; |
| 1333 | 1357 | case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = 0; break; |
| 1334 | 1358 | case DEVINFO_INT_CLASS: info->i = DEVICE_CLASS_PERIPHERAL; break; |
| 1335 | 1359 | case PPU2C0XINFO_INT_SCANLINES_PER_FRAME: info->i = PPU_NTSC_SCANLINES_PER_FRAME; break; |