src/mame/video/ppu2c0x.c
| r14138 | r14139 | |
| 23 | 23 | |
| 24 | 24 | ******************************************************************************/ |
| 25 | 25 | |
| 26 | #define ADDRESS_MAP_MODERN |
| 26 | 27 | #include "emu.h" |
| 27 | 28 | #include "profiler.h" |
| 28 | 29 | #include "video/ppu2c0x.h" |
| r14138 | r14139 | |
| 67 | 68 | 0,29,30,31, |
| 68 | 69 | }; |
| 69 | 70 | |
| 71 | //************************************************************************** |
| 72 | // GLOBAL VARIABLES |
| 73 | //************************************************************************** |
| 70 | 74 | |
| 71 | | /*************************************************************************** |
| 72 | | TYPE DEFINITIONS |
| 73 | | ***************************************************************************/ |
| 75 | // devices |
| 76 | const device_type PPU_2C02 = &device_creator<ppu2c02_device>; |
| 77 | const device_type PPU_2C03B = &device_creator<ppu2c03b_device>; |
| 78 | const device_type PPU_2C04 = &device_creator<ppu2c04_device>; |
| 79 | const device_type PPU_2C07 = &device_creator<ppu2c07_device>; |
| 80 | const device_type PPU_2C05_01 = &device_creator<ppu2c05_01_device>; |
| 81 | const device_type PPU_2C05_02 = &device_creator<ppu2c05_02_device>; |
| 82 | const device_type PPU_2C05_03 = &device_creator<ppu2c05_03_device>; |
| 83 | const device_type PPU_2C05_04 = &device_creator<ppu2c05_04_device>; |
| 74 | 84 | |
| 75 | 85 | |
| 76 | | typedef struct _ppu2c0x_state ppu2c0x_state; |
| 77 | | struct _ppu2c0x_state |
| 86 | // default address map |
| 87 | static ADDRESS_MAP_START( ppu2c0x, AS_0, 8, ppu2c0x_device ) |
| 88 | AM_RANGE(0x0000, 0x3eff) AM_RAM |
| 89 | AM_RANGE(0x3f00, 0x3fff) AM_READWRITE(palette_read, palette_write) |
| 90 | // AM_RANGE(0x0000, 0x3fff) AM_RAM |
| 91 | ADDRESS_MAP_END |
| 92 | |
| 93 | //------------------------------------------------- |
| 94 | // memory_space_config - return a description of |
| 95 | // any address spaces owned by this device |
| 96 | //------------------------------------------------- |
| 97 | |
| 98 | const address_space_config *ppu2c0x_device::memory_space_config(address_spacenum spacenum) const |
| 78 | 99 | { |
| 79 | | address_space *space; /* memory space */ |
| 80 | | bitmap_ind16 *bitmap; /* target bitmap */ |
| 81 | | UINT8 *spriteram; /* sprite ram */ |
| 82 | | pen_t *colortable; /* color table modified at run time */ |
| 83 | | pen_t *colortable_mono; /* monochromatic color table modified at run time */ |
| 84 | | emu_timer *scanline_timer; /* scanline timer */ |
| 85 | | emu_timer *hblank_timer; /* hblank period at end of each scanline */ |
| 86 | | emu_timer *nmi_timer; /* NMI timer */ |
| 87 | | int scanline; /* scanline count */ |
| 88 | | ppu2c0x_scanline_cb scanline_callback_proc; /* optional scanline callback */ |
| 89 | | ppu2c0x_hblank_cb hblank_callback_proc; /* optional hblank callback */ |
| 90 | | ppu2c0x_vidaccess_cb vidaccess_callback_proc; /* optional video access callback */ |
| 91 | | ppu2c0x_nmi_cb nmi_callback_proc; /* nmi access callback from interface */ |
| 92 | | int regs[PPU_MAX_REG]; /* registers */ |
| 93 | | int refresh_data; /* refresh-related */ |
| 94 | | int refresh_latch; /* refresh-related */ |
| 95 | | int x_fine; /* refresh-related */ |
| 96 | | int toggle; /* used to latch hi-lo scroll */ |
| 97 | | int add; /* vram increment amount */ |
| 98 | | int videomem_addr; /* videomem address pointer */ |
| 99 | | int addr_latch; /* videomem address latch */ |
| 100 | | int data_latch; /* latched videomem data */ |
| 101 | | int buffered_data; |
| 102 | | int tile_page; /* current tile page */ |
| 103 | | int sprite_page; /* current sprite page */ |
| 104 | | int back_color; /* background color */ |
| 105 | | int color_base; |
| 106 | | UINT8 palette_ram[0x20]; /* shouldn't be in main memory! */ |
| 107 | | int scan_scale; /* scan scale */ |
| 108 | | int scanlines_per_frame; /* number of scanlines per frame */ |
| 109 | | rgb_t palette[64*4]; /* palette for this chip */ |
| 110 | | int security_value; /* 2C05 protection */ |
| 111 | | void (*latch)( device_t *device, offs_t offset ); |
| 112 | | }; |
| 100 | return (spacenum == AS_0) ? &m_space_config : NULL; |
| 101 | } |
| 113 | 102 | |
| 114 | 103 | |
| 115 | | /*************************************************************************** |
| 116 | | PROTOTYPES |
| 117 | | ***************************************************************************/ |
| 104 | //------------------------------------------------- |
| 105 | // ppu2c0x_device - constructor |
| 106 | //------------------------------------------------- |
| 118 | 107 | |
| 119 | | static void update_scanline( device_t *device ); |
| 108 | void ppu2c0x_device::device_config_complete() |
| 109 | { |
| 110 | const ppu2c0x_interface *config = reinterpret_cast<const ppu2c0x_interface *>(static_config()); |
| 111 | assert(config); |
| 112 | |
| 113 | /* reset the callbacks */ |
| 114 | m_latch = NULL; |
| 115 | m_scanline_callback_proc = NULL; |
| 116 | m_hblank_callback_proc = NULL; |
| 117 | m_vidaccess_callback_proc = NULL; |
| 120 | 118 | |
| 121 | | static TIMER_CALLBACK( scanline_callback ); |
| 122 | | static TIMER_CALLBACK( hblank_callback ); |
| 123 | | static TIMER_CALLBACK( nmi_callback ); |
| 119 | m_nmi_callback_proc = config->nmi_handler; |
| 120 | m_color_base = config->color_base; |
| 124 | 121 | |
| 122 | m_cpu_tag = config->cpu_tag; |
| 123 | m_screen_tag = config->screen_tag; |
| 124 | } |
| 125 | 125 | |
| 126 | | /* palette handlers */ |
| 127 | | static WRITE8_HANDLER( ppu2c0x_palette_write ); |
| 128 | | static READ8_HANDLER( ppu2c0x_palette_read ); |
| 126 | ppu2c0x_device::ppu2c0x_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock) |
| 127 | : device_t(mconfig, type, name, tag, owner, clock), |
| 128 | device_memory_interface(mconfig, *this), |
| 129 | m_space_config("videoram", ENDIANNESS_LITTLE, 8, 17, 0, NULL, *ADDRESS_MAP_NAME(ppu2c0x)), |
| 130 | m_scanline(0), // reset the scanline count |
| 131 | m_refresh_data(0), |
| 132 | m_refresh_latch(0), |
| 133 | m_x_fine(0), |
| 134 | m_toggle(0), |
| 135 | m_add(1), |
| 136 | m_videomem_addr(0), |
| 137 | m_data_latch(0), |
| 138 | m_buffered_data(0), |
| 139 | m_tile_page(0), |
| 140 | m_sprite_page(0), |
| 141 | m_back_color(0), |
| 142 | m_scan_scale(1) // set the scan scale (this is for dual monitor vertical setups) |
| 143 | { |
| 144 | for (int i = 0; i < PPU_MAX_REG; i++) |
| 145 | m_regs[i] = 0; |
| 146 | |
| 147 | memset(m_palette_ram, 0, ARRAY_LENGTH(m_palette_ram)); |
| 148 | |
| 149 | m_scanlines_per_frame = PPU_NTSC_SCANLINES_PER_FRAME; |
| 150 | |
| 151 | /* usually, no security value... */ |
| 152 | m_security_value = 0; |
| 153 | } |
| 129 | 154 | |
| 130 | 155 | |
| 131 | | /*************************************************************************** |
| 132 | | INLINE FUNCTIONS |
| 133 | | ***************************************************************************/ |
| 156 | // NTSC NES |
| 157 | ppu2c02_device::ppu2c02_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) : ppu2c0x_device(mconfig, PPU_2C02, "2C02 PPU", tag, owner, clock) |
| 158 | { |
| 159 | } |
| 134 | 160 | |
| 135 | | INLINE ppu2c0x_state *get_token( device_t *device ) |
| 161 | // Playchoice 10 |
| 162 | ppu2c03b_device::ppu2c03b_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) : ppu2c0x_device(mconfig, PPU_2C03B, "2C03B PPU PPU", tag, owner, clock) |
| 136 | 163 | { |
| 137 | | assert(device != NULL); |
| 138 | | assert((device->type() == PPU_2C02) || (device->type() == PPU_2C03B) |
| 139 | | || (device->type() == PPU_2C04) || (device->type() == PPU_2C05_01) |
| 140 | | || (device->type() == PPU_2C05_02) || (device->type() == PPU_2C05_03) |
| 141 | | || (device->type() == PPU_2C05_04) || (device->type() == PPU_2C07)); |
| 142 | | return (ppu2c0x_state *) downcast<legacy_device_base *>(device)->token(); |
| 143 | 164 | } |
| 144 | 165 | |
| 166 | // Vs. Unisystem |
| 167 | ppu2c04_device::ppu2c04_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) : ppu2c0x_device(mconfig, PPU_2C04, "2C04 PPU", tag, owner, clock) |
| 168 | { |
| 169 | } |
| 145 | 170 | |
| 146 | | INLINE const ppu2c0x_interface *get_interface( device_t *device ) |
| 171 | // PAL NES |
| 172 | ppu2c07_device::ppu2c07_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) : ppu2c0x_device(mconfig, PPU_2C07, "2C07 PPU", tag, owner, clock) |
| 147 | 173 | { |
| 148 | | assert(device != NULL); |
| 149 | | assert((device->type() == PPU_2C02) || (device->type() == PPU_2C03B) |
| 150 | | || (device->type() == PPU_2C04) || (device->type() == PPU_2C05_01) |
| 151 | | || (device->type() == PPU_2C05_02) || (device->type() == PPU_2C05_03) |
| 152 | | || (device->type() == PPU_2C05_04) || (device->type() == PPU_2C07)); |
| 153 | | return (const ppu2c0x_interface *) device->static_config(); |
| 174 | m_scanlines_per_frame = PPU_PAL_SCANLINES_PER_FRAME; |
| 154 | 175 | } |
| 155 | 176 | |
| 177 | // The PPU_2C05 variants have different protection value, set at DEVICE_START, but otherwise are all the same... |
| 178 | // Vs. Unisystem (Ninja Jajamaru Kun) |
| 179 | ppu2c05_01_device::ppu2c05_01_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) : ppu2c0x_device(mconfig, PPU_2C05_01, "2C05 PPU", tag, owner, clock) |
| 180 | { |
| 181 | m_security_value = 0x1b; // game (jajamaru) doesn't seem to ever actually check it |
| 182 | } |
| 183 | // Vs. Unisystem (Mighty Bomb Jack) |
| 184 | ppu2c05_02_device::ppu2c05_02_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) : ppu2c0x_device(mconfig, PPU_2C05_02, "2C05 PPU", tag, owner, clock) |
| 185 | { |
| 186 | m_security_value = 0x3d; |
| 187 | } |
| 188 | // Vs. Unisystem (Gumshoe) |
| 189 | ppu2c05_03_device::ppu2c05_03_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) : ppu2c0x_device(mconfig, PPU_2C05_03, "2C05 PPU", tag, owner, clock) |
| 190 | { |
| 191 | m_security_value = 0x1c; |
| 192 | } |
| 193 | // Vs. Unisystem (Top Gun) |
| 194 | ppu2c05_04_device::ppu2c05_04_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) : ppu2c0x_device(mconfig, PPU_2C05_04, "2C05 PPU", tag, owner, clock) |
| 195 | { |
| 196 | m_security_value = 0x1b; |
| 197 | } |
| 156 | 198 | |
| 199 | |
| 200 | //------------------------------------------------- |
| 201 | // device_start - device-specific startup |
| 202 | //------------------------------------------------- |
| 203 | |
| 204 | void ppu2c0x_device::device_start() |
| 205 | { |
| 206 | m_screen = machine().device<screen_device>( m_screen_tag ); |
| 207 | m_cpu = machine().device<cpu_device>( m_cpu_tag ); |
| 208 | |
| 209 | assert(m_screen && m_cpu); |
| 210 | |
| 211 | // allocate timers |
| 212 | m_hblank_timer = timer_alloc(TIMER_HBLANK); |
| 213 | m_nmi_timer = timer_alloc(TIMER_NMI); |
| 214 | m_scanline_timer = timer_alloc(TIMER_SCANLINE); |
| 215 | |
| 216 | /* initialize the scanline handling portion */ |
| 217 | m_scanline_timer->adjust(m_screen->time_until_pos(1)); |
| 218 | m_hblank_timer->adjust(m_cpu->cycles_to_attotime(86.67)); // ??? FIXME - hardcoding NTSC, need better calculation |
| 219 | m_nmi_timer->adjust(attotime::never); |
| 220 | |
| 221 | /* allocate a screen bitmap, videomem and spriteram, a dirtychar array and the monochromatic colortable */ |
| 222 | m_bitmap = auto_bitmap_ind16_alloc(machine(), VISIBLE_SCREEN_WIDTH, VISIBLE_SCREEN_HEIGHT); |
| 223 | m_spriteram = auto_alloc_array_clear(machine(), UINT8, SPRITERAM_SIZE); |
| 224 | m_colortable = auto_alloc_array(machine(), pen_t, ARRAY_LENGTH(default_colortable)); |
| 225 | m_colortable_mono = auto_alloc_array(machine(), pen_t, ARRAY_LENGTH(default_colortable_mono)); |
| 226 | |
| 227 | /* initialize the color tables */ |
| 228 | for (int i = 0; i < ARRAY_LENGTH(default_colortable_mono); i++) |
| 229 | { |
| 230 | /* monochromatic table */ |
| 231 | m_colortable_mono[i] = default_colortable_mono[i] + m_color_base; |
| 232 | |
| 233 | /* color table */ |
| 234 | m_colortable[i] = default_colortable[i] + m_color_base; |
| 235 | } |
| 236 | |
| 237 | // register for state saving |
| 238 | save_item(NAME(m_scanline)); |
| 239 | save_item(NAME(m_refresh_data)); |
| 240 | save_item(NAME(m_refresh_latch)); |
| 241 | save_item(NAME(m_x_fine)); |
| 242 | save_item(NAME(m_toggle)); |
| 243 | save_item(NAME(m_add)); |
| 244 | save_item(NAME(m_videomem_addr)); |
| 245 | save_item(NAME(m_data_latch)); |
| 246 | save_item(NAME(m_buffered_data)); |
| 247 | save_item(NAME(m_tile_page)); |
| 248 | save_item(NAME(m_sprite_page)); |
| 249 | save_item(NAME(m_back_color)); |
| 250 | save_item(NAME(m_scan_scale)); |
| 251 | save_item(NAME(m_scanlines_per_frame)); |
| 252 | save_item(NAME(m_regs)); |
| 253 | save_item(NAME(m_palette_ram)); |
| 254 | save_pointer(NAME(m_spriteram), SPRITERAM_SIZE); |
| 255 | save_pointer(NAME(m_colortable), ARRAY_LENGTH(default_colortable)); |
| 256 | save_pointer(NAME(m_colortable_mono), ARRAY_LENGTH(default_colortable_mono)); |
| 257 | save_item(NAME(*m_bitmap)); |
| 258 | } |
| 259 | |
| 260 | //************************************************************************** |
| 261 | // INLINE HELPERS |
| 262 | //************************************************************************** |
| 263 | |
| 264 | //------------------------------------------------- |
| 265 | // readbyte - read a byte at the given address |
| 266 | //------------------------------------------------- |
| 267 | |
| 268 | inline UINT8 ppu2c0x_device::readbyte(offs_t address) |
| 269 | { |
| 270 | return space()->read_byte(address); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | //------------------------------------------------- |
| 275 | // writebyte - write a byte at the given address |
| 276 | //------------------------------------------------- |
| 277 | |
| 278 | inline void ppu2c0x_device::writebyte(offs_t address, UINT8 data) |
| 279 | { |
| 280 | space()->write_byte(address, data); |
| 281 | } |
| 282 | |
| 283 | |
| 157 | 284 | /*************************************************************************** |
| 158 | 285 | IMPLEMENTATION |
| 159 | 286 | ***************************************************************************/ |
| r14138 | r14139 | |
| 164 | 291 | * |
| 165 | 292 | *************************************/ |
| 166 | 293 | |
| 167 | | /* default address map */ |
| 168 | | // make this INTERNAL, default should just be enough to avoid compile errors, print error messages! |
| 169 | | static ADDRESS_MAP_START( ppu2c0x, AS_0, 8 ) |
| 170 | | AM_RANGE(0x3f00, 0x3fff) AM_READWRITE(ppu2c0x_palette_read, ppu2c0x_palette_write) |
| 171 | | ADDRESS_MAP_END |
| 172 | | |
| 173 | | void ppu2c0x_init_palette( running_machine &machine, int first_entry ) |
| 294 | void ppu2c0x_device::init_palette( running_machine &machine, int first_entry ) |
| 174 | 295 | { |
| 175 | 296 | |
| 176 | 297 | /* This routine builds a palette using a transformation from */ |
| r14138 | r14139 | |
| 285 | 406 | /* color tables are modified at run-time, and are initialized on 'ppu2c0x_reset' */ |
| 286 | 407 | } |
| 287 | 408 | |
| 288 | | void ppu2c0x_init_palette_rgb( running_machine &machine, int first_entry ) |
| 409 | void ppu2c0x_device::init_palette_rgb( running_machine &machine, int first_entry ) |
| 289 | 410 | { |
| 290 | 411 | int color_emphasis, color_num; |
| 291 | 412 | |
| r14138 | r14139 | |
| 327 | 448 | * |
| 328 | 449 | *************************************/ |
| 329 | 450 | |
| 330 | | void ppu2c0x_set_latch( device_t *device, void (*latch)(device_t *device, offs_t offset)) |
| 451 | void ppu2c0x_device::set_latch( void (*latch)(device_t *device, offs_t offset) ) |
| 331 | 452 | { |
| 332 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 333 | | ppu2c0x->latch = latch; |
| 453 | if (latch) |
| 454 | m_latch = latch; |
| 334 | 455 | } |
| 335 | 456 | |
| 336 | | static TIMER_CALLBACK( hblank_callback ) |
| 337 | | { |
| 338 | | device_t *device = (device_t *)ptr; |
| 339 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 340 | | int *ppu_regs = &ppu2c0x->regs[0]; |
| 457 | //------------------------------------------------- |
| 458 | // device_timer - handle timer events |
| 459 | //------------------------------------------------- |
| 341 | 460 | |
| 342 | | int blanked = (ppu_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) == 0; |
| 343 | | int vblank = ((ppu2c0x->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (ppu2c0x->scanline < ppu2c0x->scanlines_per_frame - 1)) ? 1 : 0; |
| 344 | | |
| 345 | | // update_scanline(device); |
| 346 | | |
| 347 | | if (ppu2c0x->hblank_callback_proc) |
| 348 | | (*ppu2c0x->hblank_callback_proc) (device, ppu2c0x->scanline, vblank, blanked); |
| 349 | | |
| 350 | | ppu2c0x->hblank_timer->adjust(attotime::never); |
| 351 | | } |
| 352 | | |
| 353 | | static TIMER_CALLBACK( nmi_callback ) |
| 461 | void ppu2c0x_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) |
| 354 | 462 | { |
| 355 | | device_t *device = (device_t *)ptr; |
| 356 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 357 | | int *ppu_regs = &ppu2c0x->regs[0]; |
| 463 | int blanked, vblank; |
| 464 | int *regs = &m_regs[0]; |
| 358 | 465 | |
| 359 | | // Actually fire the VMI |
| 360 | | if (ppu2c0x->nmi_callback_proc != NULL) |
| 361 | | (*ppu2c0x->nmi_callback_proc) (device, ppu_regs); |
| 362 | | |
| 363 | | ppu2c0x->nmi_timer->adjust(attotime::never); |
| 466 | switch (id) |
| 467 | { |
| 468 | case TIMER_HBLANK: |
| 469 | blanked = (m_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) == 0; |
| 470 | vblank = ((m_scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (m_scanline < m_scanlines_per_frame - 1)) ? 1 : 0; |
| 471 | |
| 472 | //update_scanline(); |
| 473 | |
| 474 | if (m_hblank_callback_proc) |
| 475 | (*m_hblank_callback_proc) (this, m_scanline, vblank, blanked); |
| 476 | |
| 477 | m_hblank_timer->adjust(attotime::never); |
| 478 | break; |
| 479 | |
| 480 | case TIMER_NMI: |
| 481 | // Actually fire the VMI |
| 482 | if (m_nmi_callback_proc) |
| 483 | (*m_nmi_callback_proc) (this, regs); |
| 484 | |
| 485 | m_nmi_timer->adjust(attotime::never); |
| 486 | break; |
| 487 | |
| 488 | case TIMER_SCANLINE: |
| 489 | blanked = (m_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) == 0; |
| 490 | vblank = ((m_scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (m_scanline < m_scanlines_per_frame - 1)) ? 1 : 0; |
| 491 | int next_scanline; |
| 492 | |
| 493 | /* if a callback is available, call it */ |
| 494 | if (m_scanline_callback_proc) |
| 495 | (*m_scanline_callback_proc)(this, m_scanline, vblank, blanked); |
| 496 | |
| 497 | /* update the scanline that just went by */ |
| 498 | update_scanline(); |
| 499 | |
| 500 | /* increment our scanline count */ |
| 501 | m_scanline++; |
| 502 | |
| 503 | // logerror("starting scanline %d (MAME %d, beam %d)\n", m_scanline, device->machine().primary_screen->vpos(), device->machine().primary_screen->hpos()); |
| 504 | |
| 505 | /* Note: this is called at the _end_ of each scanline */ |
| 506 | if (m_scanline == PPU_VBLANK_FIRST_SCANLINE) |
| 507 | { |
| 508 | // logerror("vblank starting\n"); |
| 509 | /* We just entered VBLANK */ |
| 510 | m_regs[PPU_STATUS] |= PPU_STATUS_VBLANK; |
| 511 | |
| 512 | /* If NMI's are set to be triggered, go for it */ |
| 513 | if (m_regs[PPU_CONTROL0] & PPU_CONTROL0_NMI) |
| 514 | { |
| 515 | // We need an ever-so-slight delay between entering vblank and firing an NMI - enough so that |
| 516 | // a game can read the high bit of $2002 before the NMI is called (potentially resetting the bit |
| 517 | // via a read from $2002 in the NMI handler). |
| 518 | // B-Wings is an example game that needs this. |
| 519 | m_nmi_timer->adjust(m_cpu->cycles_to_attotime(4)); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if (m_scanline == m_scanlines_per_frame - 1) |
| 524 | { |
| 525 | // logerror("vblank ending\n"); |
| 526 | /* clear the vblank & sprite hit flag */ |
| 527 | m_regs[PPU_STATUS] &= ~(PPU_STATUS_VBLANK | PPU_STATUS_SPRITE0_HIT | PPU_STATUS_8SPRITES); |
| 528 | } |
| 529 | |
| 530 | /* see if we rolled */ |
| 531 | else if (m_scanline == m_scanlines_per_frame) |
| 532 | { |
| 533 | /* if background or sprites are enabled, copy the ppu address latch */ |
| 534 | if (!blanked) |
| 535 | m_refresh_data = m_refresh_latch; |
| 536 | |
| 537 | /* reset the scanline count */ |
| 538 | m_scanline = 0; |
| 539 | //logerror("sprite 0 x: %d y: %d num: %d\n", m_spriteram[3], m_spriteram[0] + 1, m_spriteram[1]); |
| 540 | } |
| 541 | |
| 542 | next_scanline = m_scanline + 1; |
| 543 | if (next_scanline == m_scanlines_per_frame) |
| 544 | next_scanline = 0; |
| 545 | |
| 546 | // Call us back when the hblank starts for this scanline |
| 547 | m_hblank_timer->adjust(m_cpu->cycles_to_attotime(86.67)); // ??? FIXME - hardcoding NTSC, need better calculation |
| 548 | |
| 549 | // trigger again at the start of the next scanline |
| 550 | m_scanline_timer->adjust(m_screen->time_until_pos(next_scanline * m_scan_scale)); |
| 551 | break; |
| 552 | } |
| 364 | 553 | } |
| 365 | 554 | |
| 366 | | static void draw_background( device_t *device, UINT8 *line_priority ) |
| 555 | void ppu2c0x_device::draw_background( UINT8 *line_priority ) |
| 367 | 556 | { |
| 368 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 369 | | |
| 370 | | /* cache some values locally */ |
| 371 | | bitmap_ind16 &bitmap = *ppu2c0x->bitmap; |
| 372 | | const int *ppu_regs = &ppu2c0x->regs[0]; |
| 373 | | const int scanline = ppu2c0x->scanline; |
| 374 | | const int refresh_data = ppu2c0x->refresh_data; |
| 375 | | const int tile_page = ppu2c0x->tile_page; |
| 376 | | |
| 377 | | int start_x = (ppu2c0x->x_fine ^ 0x07) - 7; |
| 557 | bitmap_ind16 &bitmap = *m_bitmap; |
| 558 | int start_x = (m_x_fine ^ 0x07) - 7; |
| 378 | 559 | UINT16 back_pen; |
| 379 | 560 | UINT16 *dest; |
| 380 | 561 | |
| r14138 | r14139 | |
| 387 | 568 | int tilecount = 0; |
| 388 | 569 | |
| 389 | 570 | /* setup the color mask and colortable to use */ |
| 390 | | if (ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 571 | if (m_regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 391 | 572 | { |
| 392 | 573 | color_mask = 0xf0; |
| 393 | | color_table = ppu2c0x->colortable_mono; |
| 574 | color_table = m_colortable_mono; |
| 394 | 575 | } |
| 395 | 576 | else |
| 396 | 577 | { |
| 397 | 578 | color_mask = 0xff; |
| 398 | | color_table = ppu2c0x->colortable; |
| 579 | color_table = m_colortable; |
| 399 | 580 | } |
| 400 | 581 | |
| 401 | 582 | /* cache the background pen */ |
| 402 | | back_pen = (ppu2c0x->back_color & color_mask) + ppu2c0x->color_base; |
| 583 | back_pen = (m_back_color & color_mask) + m_color_base; |
| 403 | 584 | |
| 404 | 585 | /* determine where in the nametable to start drawing from */ |
| 405 | 586 | /* based on the current scanline and scroll regs */ |
| 406 | | scroll_x_coarse = refresh_data & 0x1f; |
| 407 | | scroll_y_coarse = (refresh_data & 0x3e0) >> 5; |
| 408 | | scroll_y_fine = (refresh_data & 0x7000) >> 12; |
| 587 | scroll_x_coarse = m_refresh_data & 0x1f; |
| 588 | scroll_y_coarse = (m_refresh_data & 0x3e0) >> 5; |
| 589 | scroll_y_fine = (m_refresh_data & 0x7000) >> 12; |
| 409 | 590 | |
| 410 | 591 | x = scroll_x_coarse; |
| 411 | 592 | |
| 412 | 593 | /* get the tile index */ |
| 413 | | tile_index = ((refresh_data & 0xc00) | 0x2000) + scroll_y_coarse * 32; |
| 594 | tile_index = ((m_refresh_data & 0xc00) | 0x2000) + scroll_y_coarse * 32; |
| 414 | 595 | |
| 415 | 596 | /* set up dest */ |
| 416 | | dest = &bitmap.pix16(scanline, start_x); |
| 597 | dest = &bitmap.pix16(m_scanline, start_x); |
| 417 | 598 | |
| 418 | 599 | /* draw the 32 or 33 tiles that make up a line */ |
| 419 | 600 | while (tilecount < 34) |
| r14138 | r14139 | |
| 432 | 613 | pos = ((index1 & 0x380) >> 4) | ((index1 & 0x1f) >> 2); |
| 433 | 614 | page = (index1 & 0x0c00) >> 10; |
| 434 | 615 | address = 0x3c0 + pos; |
| 435 | | color_byte = ppu2c0x->space->read_byte((((page * 0x400) + address) & 0xfff) + 0x2000); |
| 616 | color_byte = readbyte((((page * 0x400) + address) & 0xfff) + 0x2000); |
| 436 | 617 | |
| 437 | 618 | /* figure out which bits in the color table to use */ |
| 438 | 619 | color_bits = ((index1 & 0x40) >> 4) + (index1 & 0x02); |
| 439 | 620 | |
| 440 | 621 | // page2 is the output of the nametable read (this section is the FIRST read per tile!) |
| 441 | 622 | address = index1 & 0x3ff; |
| 442 | | page2 = ppu2c0x->space->read_byte(index1); |
| 623 | page2 = readbyte(index1); |
| 443 | 624 | |
| 444 | 625 | // 27/12/2002 |
| 445 | | if (ppu2c0x->latch) |
| 446 | | { |
| 447 | | (*ppu2c0x->latch)(device, (tile_page << 10) | (page2 << 4)); |
| 448 | | } |
| 626 | if (m_latch) |
| 627 | (*m_latch)(this, (m_tile_page << 10) | (page2 << 4)); |
| 449 | 628 | |
| 450 | 629 | if (start_x < VISIBLE_SCREEN_WIDTH) |
| 451 | 630 | { |
| r14138 | r14139 | |
| 453 | 632 | paldata = &color_table[4 * (((color_byte >> color_bits) & 0x03))]; |
| 454 | 633 | |
| 455 | 634 | // need to read 0x0000 or 0x1000 + 16*nametable data |
| 456 | | address = ((ppu2c0x->tile_page) ? 0x1000 : 0) + (page2 * 16); |
| 635 | address = ((m_tile_page) ? 0x1000 : 0) + (page2 * 16); |
| 457 | 636 | // plus something that accounts for y |
| 458 | 637 | address += scroll_y_fine; |
| 459 | 638 | |
| 460 | | plane1 = ppu2c0x->space->read_byte((address & 0x1fff)); |
| 461 | | plane2 = ppu2c0x->space->read_byte((address + 8) & 0x1fff); |
| 639 | plane1 = readbyte((address & 0x1fff)); |
| 640 | plane2 = readbyte((address + 8) & 0x1fff); |
| 462 | 641 | |
| 463 | 642 | /* render the pixel */ |
| 464 | 643 | for (i = 0; i < 8; i++) |
| r14138 | r14139 | |
| 498 | 677 | } |
| 499 | 678 | |
| 500 | 679 | /* if the left 8 pixels for the background are off, blank 'em */ |
| 501 | | if (!(ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_BACKGROUND_L8)) |
| 680 | if (!(m_regs[PPU_CONTROL1] & PPU_CONTROL1_BACKGROUND_L8)) |
| 502 | 681 | { |
| 503 | | dest = &bitmap.pix16(scanline); |
| 682 | dest = &bitmap.pix16(m_scanline); |
| 504 | 683 | for (i = 0; i < 8; i++) |
| 505 | 684 | { |
| 506 | 685 | *(dest++) = back_pen; |
| r14138 | r14139 | |
| 509 | 688 | } |
| 510 | 689 | } |
| 511 | 690 | |
| 512 | | static void draw_sprites( device_t *device, UINT8 *line_priority ) |
| 691 | void ppu2c0x_device::draw_sprites( UINT8 *line_priority ) |
| 513 | 692 | { |
| 514 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 693 | bitmap_ind16 &bitmap = *m_bitmap; |
| 515 | 694 | |
| 516 | | /* cache some values locally */ |
| 517 | | bitmap_ind16 &bitmap = *ppu2c0x->bitmap; |
| 518 | | const int scanline = ppu2c0x->scanline; |
| 519 | | const int sprite_page = ppu2c0x->sprite_page; |
| 520 | | const UINT8 *sprite_ram = ppu2c0x->spriteram; |
| 521 | | pen_t *color_table = ppu2c0x->colortable; |
| 522 | | int *ppu_regs = &ppu2c0x->regs[0]; |
| 523 | | |
| 524 | 695 | int sprite_xpos, sprite_ypos, sprite_index; |
| 525 | 696 | int tile, index1; |
| 526 | 697 | int pri; |
| r14138 | r14139 | |
| 536 | 707 | int pixel; |
| 537 | 708 | |
| 538 | 709 | /* determine if the sprites are 8x8 or 8x16 */ |
| 539 | | size = (ppu_regs[PPU_CONTROL0] & PPU_CONTROL0_SPRITE_SIZE) ? 16 : 8; |
| 710 | size = (m_regs[PPU_CONTROL0] & PPU_CONTROL0_SPRITE_SIZE) ? 16 : 8; |
| 540 | 711 | |
| 541 | | first_pixel = (ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_SPRITES_L8)? 0: 8; |
| 712 | first_pixel = (m_regs[PPU_CONTROL1] & PPU_CONTROL1_SPRITES_L8)? 0: 8; |
| 542 | 713 | |
| 543 | 714 | for (sprite_index = 0; sprite_index < SPRITERAM_SIZE; sprite_index += 4) |
| 544 | 715 | { |
| 545 | 716 | UINT8 plane1; |
| 546 | 717 | UINT8 plane2; |
| 547 | 718 | |
| 548 | | sprite_ypos = sprite_ram[sprite_index] + 1; |
| 549 | | sprite_xpos = sprite_ram[sprite_index + 3]; |
| 719 | sprite_ypos = m_spriteram[sprite_index] + 1; |
| 720 | sprite_xpos = m_spriteram[sprite_index + 3]; |
| 550 | 721 | |
| 551 | 722 | // The sprite collision acts funny on the last pixel of a scanline. |
| 552 | 723 | // The various scanline latches update while the last few pixels |
| r14138 | r14139 | |
| 560 | 731 | sprite_ypos--; |
| 561 | 732 | /* set the "sprite 0 hit" flag if appropriate */ |
| 562 | 733 | if (line_priority[sprite_xpos] & 0x02) |
| 563 | | ppu_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT; |
| 734 | m_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT; |
| 564 | 735 | } |
| 565 | 736 | |
| 566 | 737 | /* if the sprite isn't visible, skip it */ |
| 567 | | if ((sprite_ypos + size <= scanline) || (sprite_ypos > scanline)) |
| 738 | if ((sprite_ypos + size <= m_scanline) || (sprite_ypos > m_scanline)) |
| 568 | 739 | continue; |
| 569 | 740 | |
| 570 | | tile = sprite_ram[sprite_index + 1]; |
| 571 | | color = (sprite_ram[sprite_index + 2] & 0x03) + 4; |
| 572 | | pri = sprite_ram[sprite_index + 2] & 0x20; |
| 573 | | flipx = sprite_ram[sprite_index + 2] & 0x40; |
| 574 | | flipy = sprite_ram[sprite_index + 2] & 0x80; |
| 741 | tile = m_spriteram[sprite_index + 1]; |
| 742 | color = (m_spriteram[sprite_index + 2] & 0x03) + 4; |
| 743 | pri = m_spriteram[sprite_index + 2] & 0x20; |
| 744 | flipx = m_spriteram[sprite_index + 2] & 0x40; |
| 745 | flipy = m_spriteram[sprite_index + 2] & 0x80; |
| 575 | 746 | |
| 576 | 747 | if (size == 16) |
| 577 | 748 | { |
| r14138 | r14139 | |
| 583 | 754 | } |
| 584 | 755 | } |
| 585 | 756 | |
| 586 | | if (ppu2c0x->latch) |
| 587 | | (*ppu2c0x->latch)(device, (sprite_page << 10) | ((tile & 0xff) << 4)); |
| 757 | if (m_latch) |
| 758 | (*m_latch)(this, (m_sprite_page << 10) | ((tile & 0xff) << 4)); |
| 588 | 759 | |
| 589 | 760 | /* compute the character's line to draw */ |
| 590 | | sprite_line = scanline - sprite_ypos; |
| 761 | sprite_line = m_scanline - sprite_ypos; |
| 591 | 762 | |
| 592 | 763 | if (flipy) |
| 593 | 764 | sprite_line = (size - 1) - sprite_line; |
| 594 | 765 | |
| 595 | | paldata = &color_table[4 * color]; |
| 766 | paldata = &m_colortable[4 * color]; |
| 596 | 767 | |
| 597 | 768 | if (size == 16 && sprite_line > 7) |
| 598 | 769 | { |
| r14138 | r14139 | |
| 602 | 773 | |
| 603 | 774 | index1 = tile * 16; |
| 604 | 775 | if (size == 8) |
| 605 | | index1 += ((sprite_page == 0) ? 0 : 0x1000); |
| 776 | index1 += ((m_sprite_page == 0) ? 0 : 0x1000); |
| 606 | 777 | |
| 607 | | plane1 = ppu2c0x->space->read_byte((index1 + sprite_line + 0) & 0x1fff); |
| 608 | | plane2 = ppu2c0x->space->read_byte((index1 + sprite_line + 8) & 0x1fff); |
| 778 | plane1 = readbyte((index1 + sprite_line + 0) & 0x1fff); |
| 779 | plane2 = readbyte((index1 + sprite_line + 8) & 0x1fff); |
| 609 | 780 | |
| 610 | 781 | /* if there are more than 8 sprites on this line, set the flag */ |
| 611 | 782 | if (sprite_count == 8) |
| 612 | 783 | { |
| 613 | | ppu_regs[PPU_STATUS] |= PPU_STATUS_8SPRITES; |
| 614 | | // logerror ("> 8 sprites, scanline: %d\n", scanline); |
| 784 | m_regs[PPU_STATUS] |= PPU_STATUS_8SPRITES; |
| 785 | // logerror ("> 8 sprites, scanline: %d\n", m_scanline); |
| 615 | 786 | |
| 616 | 787 | /* the real NES only draws up to 8 sprites - the rest should be invisible */ |
| 617 | 788 | break; |
| r14138 | r14139 | |
| 620 | 791 | sprite_count++; |
| 621 | 792 | |
| 622 | 793 | /* abort drawing if sprites aren't rendered */ |
| 623 | | if (!(ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_SPRITES)) |
| 794 | if (!(m_regs[PPU_CONTROL1] & PPU_CONTROL1_SPRITES)) |
| 624 | 795 | continue; |
| 625 | 796 | |
| 626 | 797 | if (pri) |
| r14138 | r14139 | |
| 652 | 823 | { |
| 653 | 824 | /* no, draw */ |
| 654 | 825 | if ((sprite_xpos + pixel) < VISIBLE_SCREEN_WIDTH) |
| 655 | | bitmap.pix16(scanline, sprite_xpos + pixel) = paldata[pixel_data]; |
| 826 | bitmap.pix16(m_scanline, sprite_xpos + pixel) = paldata[pixel_data]; |
| 656 | 827 | } |
| 657 | 828 | /* indicate that a sprite was drawn at this location, even if it's not seen */ |
| 658 | 829 | if ((sprite_xpos + pixel) < VISIBLE_SCREEN_WIDTH) |
| r14138 | r14139 | |
| 661 | 832 | |
| 662 | 833 | /* set the "sprite 0 hit" flag if appropriate */ |
| 663 | 834 | if (sprite_index == 0 && (pixel_data & 0x03) && ((sprite_xpos + pixel) < 255) && (line_priority[sprite_xpos + pixel] & 0x02)) |
| 664 | | ppu_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT; |
| 835 | m_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT; |
| 665 | 836 | } |
| 666 | 837 | } |
| 667 | 838 | } |
| r14138 | r14139 | |
| 695 | 866 | /* no, draw */ |
| 696 | 867 | if ((sprite_xpos + pixel) < VISIBLE_SCREEN_WIDTH) |
| 697 | 868 | { |
| 698 | | bitmap.pix16(scanline, sprite_xpos + pixel) = paldata[pixel_data]; |
| 869 | bitmap.pix16(m_scanline, sprite_xpos + pixel) = paldata[pixel_data]; |
| 699 | 870 | line_priority[sprite_xpos + pixel] |= 0x01; |
| 700 | 871 | } |
| 701 | 872 | } |
| r14138 | r14139 | |
| 703 | 874 | |
| 704 | 875 | /* set the "sprite 0 hit" flag if appropriate */ |
| 705 | 876 | if (sprite_index == 0 && (pixel_data & 0x03) && ((sprite_xpos + pixel) < 255) && (line_priority[sprite_xpos + pixel] & 0x02)) |
| 706 | | ppu_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT; |
| 877 | m_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT; |
| 707 | 878 | } |
| 708 | 879 | } |
| 709 | 880 | } |
| r14138 | r14139 | |
| 716 | 887 | * |
| 717 | 888 | *************************************/ |
| 718 | 889 | |
| 719 | | static void render_scanline( device_t *device ) |
| 890 | void ppu2c0x_device::render_scanline( void ) |
| 720 | 891 | { |
| 721 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 722 | 892 | UINT8 line_priority[VISIBLE_SCREEN_WIDTH]; |
| 723 | | int *ppu_regs = &ppu2c0x->regs[0]; |
| 724 | 893 | |
| 725 | 894 | /* lets see how long it takes */ |
| 726 | 895 | g_profiler.start(PROFILER_USER1); |
| r14138 | r14139 | |
| 729 | 898 | memset(line_priority, 0, VISIBLE_SCREEN_WIDTH); |
| 730 | 899 | |
| 731 | 900 | /* see if we need to render the background */ |
| 732 | | if (ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_BACKGROUND) |
| 733 | | draw_background(device, line_priority); |
| 901 | if (m_regs[PPU_CONTROL1] & PPU_CONTROL1_BACKGROUND) |
| 902 | draw_background(line_priority); |
| 734 | 903 | else |
| 735 | 904 | { |
| 736 | | bitmap_ind16 &bitmap = *ppu2c0x->bitmap; |
| 737 | | const int scanline = ppu2c0x->scanline; |
| 905 | bitmap_ind16 &bitmap = *m_bitmap; |
| 738 | 906 | UINT8 color_mask; |
| 739 | 907 | UINT16 back_pen; |
| 740 | 908 | int i; |
| 741 | 909 | |
| 742 | 910 | /* setup the color mask and colortable to use */ |
| 743 | | if (ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 911 | if (m_regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 744 | 912 | color_mask = 0xf0; |
| 745 | 913 | else |
| 746 | 914 | color_mask = 0xff; |
| 747 | 915 | |
| 748 | 916 | /* cache the background pen */ |
| 749 | | back_pen = (ppu2c0x->back_color & color_mask) + ppu2c0x->color_base; |
| 917 | back_pen = (m_back_color & color_mask) + m_color_base; |
| 750 | 918 | |
| 751 | 919 | // Fill this scanline with the background pen. |
| 752 | 920 | for (i = 0; i < bitmap.width(); i++) |
| 753 | | bitmap.pix16(scanline, i) = back_pen; |
| 921 | bitmap.pix16(m_scanline, i) = back_pen; |
| 754 | 922 | } |
| 755 | 923 | |
| 756 | 924 | /* if sprites are on, draw them, but we call always to process them */ |
| 757 | | draw_sprites(device, line_priority); |
| 925 | draw_sprites(line_priority); |
| 758 | 926 | |
| 759 | 927 | /* done updating, whew */ |
| 760 | 928 | g_profiler.stop(); |
| 761 | 929 | } |
| 762 | 930 | |
| 763 | | static void update_scanline( device_t *device ) |
| 931 | void ppu2c0x_device::update_scanline( void ) |
| 764 | 932 | { |
| 765 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 766 | | const int scanline = ppu2c0x->scanline; |
| 767 | | int *ppu_regs = &ppu2c0x->regs[0]; |
| 768 | | |
| 769 | | if (scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 933 | if (m_scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 770 | 934 | { |
| 771 | 935 | /* Render this scanline if appropriate */ |
| 772 | | if (ppu_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) |
| 936 | if (m_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) |
| 773 | 937 | { |
| 774 | 938 | /* If background or sprites are enabled, copy the ppu address latch */ |
| 775 | 939 | /* Copy only the scroll x-coarse and the x-overflow bit */ |
| 776 | | ppu2c0x->refresh_data &= ~0x041f; |
| 777 | | ppu2c0x->refresh_data |= (ppu2c0x->refresh_latch & 0x041f); |
| 940 | m_refresh_data &= ~0x041f; |
| 941 | m_refresh_data |= (m_refresh_latch & 0x041f); |
| 778 | 942 | |
| 779 | | // logerror("updating refresh_data: %04x (scanline: %d)\n", ppu2c0x->refresh_data, ppu2c0x->scanline); |
| 780 | | render_scanline(device); |
| 943 | // logerror("updating refresh_data: %04x (scanline: %d)\n", m_refresh_data, m_scanline); |
| 944 | render_scanline(); |
| 781 | 945 | } |
| 782 | 946 | else |
| 783 | 947 | { |
| 784 | | bitmap_ind16 &bitmap = *ppu2c0x->bitmap; |
| 948 | bitmap_ind16 &bitmap = *m_bitmap; |
| 785 | 949 | UINT8 color_mask; |
| 786 | 950 | UINT16 back_pen; |
| 787 | 951 | int i; |
| 788 | 952 | |
| 789 | 953 | /* setup the color mask and colortable to use */ |
| 790 | | if (ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 954 | if (m_regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 791 | 955 | color_mask = 0xf0; |
| 792 | 956 | else |
| 793 | 957 | color_mask = 0xff; |
| 794 | 958 | |
| 795 | 959 | /* cache the background pen */ |
| 796 | | if (ppu2c0x->videomem_addr >= 0x3f00) |
| 960 | if (m_videomem_addr >= 0x3f00) |
| 797 | 961 | { |
| 798 | 962 | // If the PPU's VRAM address happens to point into palette ram space while |
| 799 | 963 | // both the sprites and background are disabled, the PPU paints the scanline |
| r14138 | r14139 | |
| 801 | 965 | // pen. Micro Machines makes use of this feature. |
| 802 | 966 | int pen_num; |
| 803 | 967 | |
| 804 | | if (ppu2c0x->videomem_addr & 0x03) |
| 805 | | pen_num = ppu2c0x->palette_ram[ppu2c0x->videomem_addr & 0x1f]; |
| 968 | if (m_videomem_addr & 0x03) |
| 969 | pen_num = m_palette_ram[m_videomem_addr & 0x1f]; |
| 806 | 970 | else |
| 807 | | pen_num = ppu2c0x->palette_ram[0]; |
| 971 | pen_num = m_palette_ram[0]; |
| 808 | 972 | |
| 809 | | back_pen = pen_num + ppu2c0x->color_base; |
| 973 | back_pen = pen_num + m_color_base; |
| 810 | 974 | } |
| 811 | 975 | else |
| 812 | | back_pen = (ppu2c0x->back_color & color_mask) + ppu2c0x->color_base; |
| 976 | back_pen = (m_back_color & color_mask) + m_color_base; |
| 813 | 977 | |
| 814 | 978 | // Fill this scanline with the background pen. |
| 815 | 979 | for (i = 0; i < bitmap.width(); i++) |
| 816 | | bitmap.pix16(scanline, i) = back_pen; |
| 980 | bitmap.pix16(m_scanline, i) = back_pen; |
| 817 | 981 | } |
| 818 | 982 | |
| 819 | 983 | /* increment the fine y-scroll */ |
| 820 | | ppu2c0x->refresh_data += 0x1000; |
| 984 | m_refresh_data += 0x1000; |
| 821 | 985 | |
| 822 | 986 | /* if it's rolled, increment the coarse y-scroll */ |
| 823 | | if (ppu2c0x->refresh_data & 0x8000) |
| 987 | if (m_refresh_data & 0x8000) |
| 824 | 988 | { |
| 825 | 989 | UINT16 tmp; |
| 826 | | tmp = (ppu2c0x->refresh_data & 0x03e0) + 0x20; |
| 827 | | ppu2c0x->refresh_data &= 0x7c1f; |
| 990 | tmp = (m_refresh_data & 0x03e0) + 0x20; |
| 991 | m_refresh_data &= 0x7c1f; |
| 828 | 992 | |
| 829 | 993 | /* handle bizarro scrolling rollover at the 30th (not 32nd) vertical tile */ |
| 830 | 994 | if (tmp == 0x03c0) |
| 831 | | ppu2c0x->refresh_data ^= 0x0800; |
| 995 | m_refresh_data ^= 0x0800; |
| 832 | 996 | else |
| 833 | | ppu2c0x->refresh_data |= (tmp & 0x03e0); |
| 997 | m_refresh_data |= (tmp & 0x03e0); |
| 834 | 998 | |
| 835 | | // logerror("updating refresh_data: %04x\n", ppu2c0x->refresh_data); |
| 999 | // logerror("updating refresh_data: %04x\n", m_refresh_data); |
| 836 | 1000 | } |
| 837 | 1001 | } |
| 838 | 1002 | |
| 839 | 1003 | } |
| 840 | 1004 | |
| 841 | | static TIMER_CALLBACK( scanline_callback ) |
| 842 | | { |
| 843 | | device_t *device = (device_t *)ptr; |
| 844 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 845 | | int *ppu_regs = &ppu2c0x->regs[0]; |
| 846 | | int blanked = (ppu_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) == 0; |
| 847 | | int vblank = ((ppu2c0x->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (ppu2c0x->scanline < ppu2c0x->scanlines_per_frame - 1)) ? 1 : 0; |
| 848 | | int next_scanline; |
| 849 | | |
| 850 | | /* if a callback is available, call it */ |
| 851 | | if (ppu2c0x->scanline_callback_proc != NULL) |
| 852 | | (*ppu2c0x->scanline_callback_proc)(device, ppu2c0x->scanline, vblank, blanked); |
| 853 | | |
| 854 | | /* update the scanline that just went by */ |
| 855 | | update_scanline(device); |
| 856 | | |
| 857 | | /* increment our scanline count */ |
| 858 | | ppu2c0x->scanline++; |
| 859 | | |
| 860 | | // logerror("starting scanline %d (MAME %d, beam %d)\n", ppu2c0x->scanline, device->machine().primary_screen->vpos(), device->machine().primary_screen->hpos()); |
| 861 | | |
| 862 | | /* Note: this is called at the _end_ of each scanline */ |
| 863 | | if (ppu2c0x->scanline == PPU_VBLANK_FIRST_SCANLINE) |
| 864 | | { |
| 865 | | // logerror("vblank starting\n"); |
| 866 | | /* We just entered VBLANK */ |
| 867 | | ppu_regs[PPU_STATUS] |= PPU_STATUS_VBLANK; |
| 868 | | |
| 869 | | /* If NMI's are set to be triggered, go for it */ |
| 870 | | if (ppu_regs[PPU_CONTROL0] & PPU_CONTROL0_NMI) |
| 871 | | { |
| 872 | | // We need an ever-so-slight delay between entering vblank and firing an NMI - enough so that |
| 873 | | // a game can read the high bit of $2002 before the NMI is called (potentially resetting the bit |
| 874 | | // via a read from $2002 in the NMI handler). |
| 875 | | // B-Wings is an example game that needs this. |
| 876 | | ppu2c0x->nmi_timer->adjust(device->machine().device<cpu_device>("maincpu")->cycles_to_attotime(4)); |
| 877 | | } |
| 878 | | } |
| 879 | | |
| 880 | | if (ppu2c0x->scanline == ppu2c0x->scanlines_per_frame - 1) |
| 881 | | { |
| 882 | | // logerror("vblank ending\n"); |
| 883 | | /* clear the vblank & sprite hit flag */ |
| 884 | | ppu_regs[PPU_STATUS] &= ~(PPU_STATUS_VBLANK | PPU_STATUS_SPRITE0_HIT | PPU_STATUS_8SPRITES); |
| 885 | | } |
| 886 | | |
| 887 | | /* see if we rolled */ |
| 888 | | else if (ppu2c0x->scanline == ppu2c0x->scanlines_per_frame) |
| 889 | | { |
| 890 | | /* if background or sprites are enabled, copy the ppu address latch */ |
| 891 | | if (!blanked) |
| 892 | | ppu2c0x->refresh_data = ppu2c0x->refresh_latch; |
| 893 | | |
| 894 | | /* reset the scanline count */ |
| 895 | | ppu2c0x->scanline = 0; |
| 896 | | // logerror("sprite 0 x: %d y: %d num: %d\n", ppu2c0x->spriteram[3], ppu2c0x->spriteram[0] + 1, ppu2c0x->spriteram[1]); |
| 897 | | } |
| 898 | | |
| 899 | | next_scanline = ppu2c0x->scanline + 1; |
| 900 | | if (next_scanline == ppu2c0x->scanlines_per_frame) |
| 901 | | next_scanline = 0; |
| 902 | | |
| 903 | | // Call us back when the hblank starts for this scanline |
| 904 | | ppu2c0x->hblank_timer->adjust(device->machine().device<cpu_device>("maincpu")->cycles_to_attotime(86.67)); // ??? FIXME - hardcoding NTSC, need better calculation |
| 905 | | |
| 906 | | // trigger again at the start of the next scanline |
| 907 | | ppu2c0x->scanline_timer->adjust(device->machine().primary_screen->time_until_pos(next_scanline * ppu2c0x->scan_scale)); |
| 908 | | } |
| 909 | | |
| 910 | 1005 | /************************************* |
| 911 | 1006 | * |
| 912 | 1007 | * PPU Memory functions |
| 913 | 1008 | * |
| 914 | 1009 | *************************************/ |
| 915 | 1010 | |
| 916 | | static WRITE8_HANDLER( ppu2c0x_palette_write ) |
| 1011 | WRITE8_MEMBER( ppu2c0x_device::palette_write ) |
| 917 | 1012 | { |
| 918 | | ppu2c0x_state *ppu2c0x = get_token(&space->device()); |
| 919 | | int color_base = ppu2c0x->color_base; |
| 920 | | int color_emphasis = (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS) * 2; |
| 1013 | int color_base = m_color_base; |
| 1014 | int color_emphasis = (m_regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS) * 2; |
| 921 | 1015 | |
| 922 | 1016 | // palette RAM is only 6 bits wide |
| 923 | 1017 | data &= 0x3f; |
| r14138 | r14139 | |
| 925 | 1019 | // transparent pens are mirrored! |
| 926 | 1020 | if (offset & 0x3) |
| 927 | 1021 | { |
| 928 | | ppu2c0x->palette_ram[offset & 0x1f] = data; |
| 929 | | ppu2c0x->colortable[offset & 0x1f] = color_base + data + color_emphasis; |
| 930 | | ppu2c0x->colortable_mono[offset & 0x1f] = color_base + (data & 0xf0) + color_emphasis; |
| 1022 | m_palette_ram[offset & 0x1f] = data; |
| 1023 | m_colortable[offset & 0x1f] = color_base + data + color_emphasis; |
| 1024 | m_colortable_mono[offset & 0x1f] = color_base + (data & 0xf0) + color_emphasis; |
| 931 | 1025 | } |
| 932 | 1026 | else |
| 933 | 1027 | { |
| 934 | 1028 | int i; |
| 935 | 1029 | if (0 == (offset & 0xf)) |
| 936 | 1030 | { |
| 937 | | ppu2c0x->back_color = data; |
| 1031 | m_back_color = data; |
| 938 | 1032 | for (i = 0; i < 32; i += 4) |
| 939 | 1033 | { |
| 940 | | ppu2c0x->colortable[i] = color_base + data + color_emphasis; |
| 941 | | ppu2c0x->colortable_mono[i] = color_base + (data & 0xf0) + color_emphasis; |
| 1034 | m_colortable[i] = color_base + data + color_emphasis; |
| 1035 | m_colortable_mono[i] = color_base + (data & 0xf0) + color_emphasis; |
| 942 | 1036 | } |
| 943 | 1037 | } |
| 944 | | ppu2c0x->palette_ram[offset & 0xf] = ppu2c0x->palette_ram[(offset & 0xf) + 0x10] = data; |
| 1038 | m_palette_ram[offset & 0xf] = m_palette_ram[(offset & 0xf) + 0x10] = data; |
| 945 | 1039 | } |
| 946 | 1040 | } |
| 947 | 1041 | |
| 948 | | static READ8_HANDLER( ppu2c0x_palette_read ) |
| 1042 | READ8_MEMBER( ppu2c0x_device::palette_read ) |
| 949 | 1043 | { |
| 950 | | ppu2c0x_state *ppu2c0x = get_token(&space->device()); |
| 951 | | { |
| 952 | | if (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 953 | | return (ppu2c0x->palette_ram[offset & 0x1f] & 0x30); |
| 954 | | |
| 955 | | else |
| 956 | | return (ppu2c0x->palette_ram[offset & 0x1f]); |
| 957 | | } |
| 1044 | if (m_regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO) |
| 1045 | return (m_palette_ram[offset & 0x1f] & 0x30); |
| 1046 | |
| 1047 | else |
| 1048 | return (m_palette_ram[offset & 0x1f]); |
| 958 | 1049 | } |
| 959 | 1050 | |
| 960 | 1051 | /************************************* |
| r14138 | r14139 | |
| 963 | 1054 | * |
| 964 | 1055 | *************************************/ |
| 965 | 1056 | |
| 966 | | READ8_DEVICE_HANDLER( ppu2c0x_r ) |
| 1057 | READ8_MEMBER( ppu2c0x_device::read ) |
| 967 | 1058 | { |
| 968 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 969 | 1059 | |
| 970 | 1060 | if (offset >= PPU_MAX_REG) |
| 971 | 1061 | { |
| 972 | | logerror("PPU %s: Attempting to read past the chip: offset %x\n", device->tag(), offset); |
| 1062 | logerror("PPU %s: Attempting to read past the chip: offset %x\n", this->tag(), offset); |
| 973 | 1063 | offset &= PPU_MAX_REG - 1; |
| 974 | 1064 | } |
| 975 | 1065 | |
| r14138 | r14139 | |
| 979 | 1069 | case PPU_STATUS: /* 2 */ |
| 980 | 1070 | // The top 3 bits of the status register are the only ones that report data. The |
| 981 | 1071 | // remainder contain whatever was last in the PPU data latch, except on the RC2C05 (protection) |
| 982 | | if (ppu2c0x->security_value) |
| 983 | | ppu2c0x->data_latch = (ppu2c0x->regs[PPU_STATUS] & 0xc0) | ppu2c0x->security_value; |
| 1072 | if (m_security_value) |
| 1073 | m_data_latch = (m_regs[PPU_STATUS] & 0xc0) | m_security_value; |
| 984 | 1074 | else |
| 985 | | ppu2c0x->data_latch = ppu2c0x->regs[PPU_STATUS] | (ppu2c0x->data_latch & 0x1f); |
| 1075 | m_data_latch = m_regs[PPU_STATUS] | (m_data_latch & 0x1f); |
| 986 | 1076 | |
| 987 | 1077 | // Reset hi/lo scroll toggle |
| 988 | | ppu2c0x->toggle = 0; |
| 1078 | m_toggle = 0; |
| 989 | 1079 | |
| 990 | 1080 | // If the vblank bit is set, clear all status bits but the 2 sprite flags |
| 991 | | if (ppu2c0x->data_latch & PPU_STATUS_VBLANK) |
| 992 | | ppu2c0x->regs[PPU_STATUS] &= 0x60; |
| 1081 | if (m_data_latch & PPU_STATUS_VBLANK) |
| 1082 | m_regs[PPU_STATUS] &= 0x60; |
| 993 | 1083 | break; |
| 994 | 1084 | |
| 995 | 1085 | case PPU_SPRITE_DATA: /* 4 */ |
| 996 | | ppu2c0x->data_latch = ppu2c0x->spriteram[ppu2c0x->regs[PPU_SPRITE_ADDRESS]]; |
| 1086 | m_data_latch = m_spriteram[m_regs[PPU_SPRITE_ADDRESS]]; |
| 997 | 1087 | break; |
| 998 | 1088 | |
| 999 | 1089 | case PPU_DATA: /* 7 */ |
| 1000 | | if (ppu2c0x->latch) |
| 1001 | | (*ppu2c0x->latch)(device, ppu2c0x->videomem_addr & 0x3fff); |
| 1090 | if (m_latch) |
| 1091 | (*m_latch)(this, m_videomem_addr & 0x3fff); |
| 1002 | 1092 | |
| 1003 | | if (ppu2c0x->videomem_addr >= 0x3f00) |
| 1093 | if (m_videomem_addr >= 0x3f00) |
| 1004 | 1094 | { |
| 1005 | | ppu2c0x->data_latch = ppu2c0x->space->read_byte(ppu2c0x->videomem_addr); |
| 1095 | m_data_latch = readbyte(m_videomem_addr); |
| 1006 | 1096 | // buffer the mirrored NT data |
| 1007 | | ppu2c0x->buffered_data = ppu2c0x->space->read_byte(ppu2c0x->videomem_addr & 0x2fff); |
| 1097 | m_buffered_data = readbyte(m_videomem_addr & 0x2fff); |
| 1008 | 1098 | } |
| 1009 | 1099 | else |
| 1010 | 1100 | { |
| 1011 | | ppu2c0x->data_latch = ppu2c0x->buffered_data; |
| 1012 | | ppu2c0x->buffered_data = ppu2c0x->space->read_byte(ppu2c0x->videomem_addr); |
| 1101 | m_data_latch = m_buffered_data; |
| 1102 | m_buffered_data = readbyte(m_videomem_addr); |
| 1013 | 1103 | } |
| 1014 | 1104 | |
| 1015 | | ppu2c0x->videomem_addr += ppu2c0x->add; |
| 1105 | m_videomem_addr += m_add; |
| 1016 | 1106 | break; |
| 1017 | 1107 | |
| 1018 | 1108 | default: |
| 1019 | 1109 | break; |
| 1020 | 1110 | } |
| 1021 | 1111 | |
| 1022 | | return ppu2c0x->data_latch; |
| 1112 | return m_data_latch; |
| 1023 | 1113 | } |
| 1024 | 1114 | |
| 1025 | 1115 | |
| r14138 | r14139 | |
| 1029 | 1119 | * |
| 1030 | 1120 | *************************************/ |
| 1031 | 1121 | |
| 1032 | | WRITE8_DEVICE_HANDLER( ppu2c0x_w ) |
| 1122 | WRITE8_MEMBER( ppu2c0x_device::write ) |
| 1033 | 1123 | { |
| 1034 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1035 | 1124 | int color_base; |
| 1036 | 1125 | |
| 1037 | | color_base = ppu2c0x->color_base; |
| 1126 | color_base = m_color_base; |
| 1038 | 1127 | |
| 1039 | 1128 | if (offset >= PPU_MAX_REG) |
| 1040 | 1129 | { |
| 1041 | | logerror("PPU %s: Attempting to write past the chip: offset %x, data %x\n", device->tag(), offset, data); |
| 1130 | logerror("PPU %s: Attempting to write past the chip: offset %x, data %x\n", this->tag(), offset, data); |
| 1042 | 1131 | offset &= PPU_MAX_REG - 1; |
| 1043 | 1132 | } |
| 1044 | 1133 | |
| 1045 | 1134 | #ifdef MAME_DEBUG |
| 1046 | | if (ppu2c0x->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 1135 | if (m_scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 1047 | 1136 | { |
| 1048 | 1137 | screen_device *screen = device->machine().primary_screen; |
| 1049 | | logerror("PPU register %d write %02x during non-vblank scanline %d (MAME %d, beam pos: %d)\n", offset, data, ppu2c0x->scanline, screen->vpos(), screen->hpos()); |
| 1138 | logerror("PPU register %d write %02x during non-vblank scanline %d (MAME %d, beam pos: %d)\n", offset, data, m_scanline, m_screen->vpos(), m_screen->hpos()); |
| 1050 | 1139 | } |
| 1051 | 1140 | #endif |
| 1052 | 1141 | |
| 1053 | 1142 | /* on the RC2C05, PPU_CONTROL0 and PPU_CONTROL1 are swapped (protection) */ |
| 1054 | | if ((ppu2c0x->security_value) && !(offset & 6)) |
| 1143 | if ((m_security_value) && !(offset & 6)) |
| 1055 | 1144 | offset ^= 1; |
| 1056 | 1145 | |
| 1057 | 1146 | switch (offset & 7) |
| 1058 | 1147 | { |
| 1059 | 1148 | case PPU_CONTROL0: /* 0 */ |
| 1060 | | ppu2c0x->regs[PPU_CONTROL0] = data; |
| 1149 | m_regs[PPU_CONTROL0] = data; |
| 1061 | 1150 | |
| 1062 | 1151 | /* update the name table number on our refresh latches */ |
| 1063 | | ppu2c0x->refresh_latch &= 0x73ff; |
| 1064 | | ppu2c0x->refresh_latch |= (data & 3) << 10; |
| 1152 | m_refresh_latch &= 0x73ff; |
| 1153 | m_refresh_latch |= (data & 3) << 10; |
| 1065 | 1154 | |
| 1066 | 1155 | /* the char ram bank points either 0x0000 or 0x1000 (page 0 or page 4) */ |
| 1067 | | ppu2c0x->tile_page = (data & PPU_CONTROL0_CHR_SELECT) >> 2; |
| 1068 | | ppu2c0x->sprite_page = (data & PPU_CONTROL0_SPR_SELECT) >> 1; |
| 1156 | m_tile_page = (data & PPU_CONTROL0_CHR_SELECT) >> 2; |
| 1157 | m_sprite_page = (data & PPU_CONTROL0_SPR_SELECT) >> 1; |
| 1069 | 1158 | |
| 1070 | | ppu2c0x->add = (data & PPU_CONTROL0_INC) ? 32 : 1; |
| 1071 | | // logerror("control0 write: %02x (scanline: %d)\n", data, ppu2c0x->scanline); |
| 1159 | m_add = (data & PPU_CONTROL0_INC) ? 32 : 1; |
| 1160 | // logerror("control0 write: %02x (scanline: %d)\n", data, m_scanline); |
| 1072 | 1161 | break; |
| 1073 | 1162 | |
| 1074 | 1163 | case PPU_CONTROL1: /* 1 */ |
| 1075 | 1164 | /* if color intensity has changed, change all the color tables to reflect them */ |
| 1076 | | if ((data & PPU_CONTROL1_COLOR_EMPHASIS) != (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS)) |
| 1165 | if ((data & PPU_CONTROL1_COLOR_EMPHASIS) != (m_regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS)) |
| 1077 | 1166 | { |
| 1078 | 1167 | int i; |
| 1079 | 1168 | for (i = 0; i <= 0x1f; i++) |
| 1080 | 1169 | { |
| 1081 | | UINT8 oldColor = ppu2c0x->palette_ram[i]; |
| 1170 | UINT8 oldColor = m_palette_ram[i]; |
| 1082 | 1171 | |
| 1083 | | ppu2c0x->colortable[i] = color_base + oldColor + (data & PPU_CONTROL1_COLOR_EMPHASIS) * 2; |
| 1172 | m_colortable[i] = color_base + oldColor + (data & PPU_CONTROL1_COLOR_EMPHASIS) * 2; |
| 1084 | 1173 | } |
| 1085 | 1174 | } |
| 1086 | 1175 | |
| 1087 | | // logerror("control1 write: %02x (scanline: %d)\n", data, ppu2c0x->scanline); |
| 1088 | | ppu2c0x->regs[PPU_CONTROL1] = data; |
| 1176 | // logerror("control1 write: %02x (scanline: %d)\n", data, m_scanline); |
| 1177 | m_regs[PPU_CONTROL1] = data; |
| 1089 | 1178 | break; |
| 1090 | 1179 | |
| 1091 | 1180 | case PPU_SPRITE_ADDRESS: /* 3 */ |
| 1092 | | ppu2c0x->regs[PPU_SPRITE_ADDRESS] = data; |
| 1181 | m_regs[PPU_SPRITE_ADDRESS] = data; |
| 1093 | 1182 | break; |
| 1094 | 1183 | |
| 1095 | 1184 | case PPU_SPRITE_DATA: /* 4 */ |
| 1096 | 1185 | // If the PPU is currently rendering the screen, 0xff is written instead of the desired data. |
| 1097 | | if (ppu2c0x->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 1186 | if (m_scanline <= PPU_BOTTOM_VISIBLE_SCANLINE) |
| 1098 | 1187 | data = 0xff; |
| 1099 | | ppu2c0x->spriteram[ppu2c0x->regs[PPU_SPRITE_ADDRESS]] = data; |
| 1100 | | ppu2c0x->regs[PPU_SPRITE_ADDRESS] = (ppu2c0x->regs[PPU_SPRITE_ADDRESS] + 1) & 0xff; |
| 1188 | m_spriteram[m_regs[PPU_SPRITE_ADDRESS]] = data; |
| 1189 | m_regs[PPU_SPRITE_ADDRESS] = (m_regs[PPU_SPRITE_ADDRESS] + 1) & 0xff; |
| 1101 | 1190 | break; |
| 1102 | 1191 | |
| 1103 | 1192 | case PPU_SCROLL: /* 5 */ |
| 1104 | | if (ppu2c0x->toggle) |
| 1193 | if (m_toggle) |
| 1105 | 1194 | { |
| 1106 | 1195 | /* second write */ |
| 1107 | | ppu2c0x->refresh_latch &= 0x0c1f; |
| 1108 | | ppu2c0x->refresh_latch |= (data & 0xf8) << 2; |
| 1109 | | ppu2c0x->refresh_latch |= (data & 0x07) << 12; |
| 1110 | | // logerror("scroll write 2: %d, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline); |
| 1196 | m_refresh_latch &= 0x0c1f; |
| 1197 | m_refresh_latch |= (data & 0xf8) << 2; |
| 1198 | m_refresh_latch |= (data & 0x07) << 12; |
| 1199 | // logerror("scroll write 2: %d, %04x (scanline: %d)\n", data, m_refresh_latch, m_scanline); |
| 1111 | 1200 | } |
| 1112 | 1201 | else |
| 1113 | 1202 | { |
| 1114 | 1203 | /* first write */ |
| 1115 | | ppu2c0x->refresh_latch &= 0x7fe0; |
| 1116 | | ppu2c0x->refresh_latch |= (data & 0xf8) >> 3; |
| 1204 | m_refresh_latch &= 0x7fe0; |
| 1205 | m_refresh_latch |= (data & 0xf8) >> 3; |
| 1117 | 1206 | |
| 1118 | | ppu2c0x->x_fine = data & 7; |
| 1119 | | // logerror("scroll write 1: %d, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline); |
| 1207 | m_x_fine = data & 7; |
| 1208 | // logerror("scroll write 1: %d, %04x (scanline: %d)\n", data, m_refresh_latch, m_scanline); |
| 1120 | 1209 | } |
| 1121 | 1210 | |
| 1122 | | ppu2c0x->toggle ^= 1; |
| 1211 | m_toggle ^= 1; |
| 1123 | 1212 | break; |
| 1124 | 1213 | |
| 1125 | 1214 | case PPU_ADDRESS: /* 6 */ |
| 1126 | | if (ppu2c0x->toggle) |
| 1215 | if (m_toggle) |
| 1127 | 1216 | { |
| 1128 | 1217 | /* second write */ |
| 1129 | | ppu2c0x->refresh_latch &= 0x7f00; |
| 1130 | | ppu2c0x->refresh_latch |= data; |
| 1131 | | ppu2c0x->refresh_data = ppu2c0x->refresh_latch; |
| 1218 | m_refresh_latch &= 0x7f00; |
| 1219 | m_refresh_latch |= data; |
| 1220 | m_refresh_data = m_refresh_latch; |
| 1132 | 1221 | |
| 1133 | | ppu2c0x->videomem_addr = ppu2c0x->refresh_latch; |
| 1134 | | // logerror("vram addr write 2: %02x, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline); |
| 1222 | m_videomem_addr = m_refresh_latch; |
| 1223 | // logerror("vram addr write 2: %02x, %04x (scanline: %d)\n", data, m_refresh_latch, m_scanline); |
| 1135 | 1224 | } |
| 1136 | 1225 | else |
| 1137 | 1226 | { |
| 1138 | 1227 | /* first write */ |
| 1139 | | ppu2c0x->refresh_latch &= 0x00ff; |
| 1140 | | ppu2c0x->refresh_latch |= (data & 0x3f) << 8; |
| 1141 | | // logerror("vram addr write 1: %02x, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline); |
| 1228 | m_refresh_latch &= 0x00ff; |
| 1229 | m_refresh_latch |= (data & 0x3f) << 8; |
| 1230 | // logerror("vram addr write 1: %02x, %04x (scanline: %d)\n", data, m_refresh_latch, m_scanline); |
| 1142 | 1231 | } |
| 1143 | 1232 | |
| 1144 | | ppu2c0x->toggle ^= 1; |
| 1233 | m_toggle ^= 1; |
| 1145 | 1234 | break; |
| 1146 | 1235 | |
| 1147 | 1236 | case PPU_DATA: /* 7 */ |
| 1148 | 1237 | { |
| 1149 | | int tempAddr = ppu2c0x->videomem_addr & 0x3fff; |
| 1238 | int tempAddr = m_videomem_addr & 0x3fff; |
| 1150 | 1239 | |
| 1151 | | if (ppu2c0x->latch) |
| 1152 | | (*ppu2c0x->latch)(device, tempAddr); |
| 1240 | if (m_latch) |
| 1241 | (*m_latch)(this, tempAddr); |
| 1153 | 1242 | |
| 1154 | 1243 | /* if there's a callback, call it now */ |
| 1155 | | if (ppu2c0x->vidaccess_callback_proc) |
| 1156 | | data = (*ppu2c0x->vidaccess_callback_proc)(device, tempAddr, data); |
| 1244 | if (m_vidaccess_callback_proc) |
| 1245 | data = (*m_vidaccess_callback_proc)(this, tempAddr, data); |
| 1157 | 1246 | |
| 1158 | 1247 | /* see if it's on the chargen portion */ |
| 1159 | 1248 | if (tempAddr < 0x2000) |
| 1160 | 1249 | { |
| 1161 | 1250 | /* store the data */ |
| 1162 | | ppu2c0x->space->write_byte(tempAddr, data); |
| 1251 | writebyte(tempAddr, data); |
| 1163 | 1252 | } |
| 1164 | 1253 | |
| 1165 | 1254 | else |
| 1166 | 1255 | { |
| 1167 | | ppu2c0x->space->write_byte(tempAddr, data); |
| 1256 | writebyte(tempAddr, data); |
| 1168 | 1257 | } |
| 1169 | 1258 | /* increment the address */ |
| 1170 | | ppu2c0x->videomem_addr += ppu2c0x->add; |
| 1259 | m_videomem_addr += m_add; |
| 1171 | 1260 | } |
| 1172 | 1261 | break; |
| 1173 | 1262 | |
| r14138 | r14139 | |
| 1176 | 1265 | break; |
| 1177 | 1266 | } |
| 1178 | 1267 | |
| 1179 | | ppu2c0x->data_latch = data; |
| 1268 | m_data_latch = data; |
| 1180 | 1269 | } |
| 1181 | 1270 | |
| 1182 | 1271 | /************************************* |
| r14138 | r14139 | |
| 1185 | 1274 | * |
| 1186 | 1275 | *************************************/ |
| 1187 | 1276 | |
| 1188 | | void ppu2c0x_spriteram_dma( address_space *space, device_t *device, const UINT8 page ) |
| 1277 | void ppu2c0x_device::spriteram_dma( address_space *space, const UINT8 page ) |
| 1189 | 1278 | { |
| 1190 | 1279 | int i; |
| 1191 | 1280 | int address = page << 8; |
| r14138 | r14139 | |
| 1206 | 1295 | * |
| 1207 | 1296 | *************************************/ |
| 1208 | 1297 | |
| 1209 | | void ppu2c0x_render( device_t *device, bitmap_ind16 &bitmap, int flipx, int flipy, int sx, int sy ) |
| 1298 | void ppu2c0x_device::render( bitmap_ind16 &bitmap, int flipx, int flipy, int sx, int sy ) |
| 1210 | 1299 | { |
| 1211 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1212 | | copybitmap(bitmap, *ppu2c0x->bitmap, flipx, flipy, sx, sy, bitmap.cliprect()); |
| 1300 | copybitmap(bitmap, *m_bitmap, flipx, flipy, sx, sy, bitmap.cliprect()); |
| 1213 | 1301 | } |
| 1214 | 1302 | |
| 1215 | 1303 | /************************************* |
| r14138 | r14139 | |
| 1218 | 1306 | * |
| 1219 | 1307 | *************************************/ |
| 1220 | 1308 | |
| 1221 | | int ppu2c0x_get_pixel( device_t *device, int x, int y ) |
| 1309 | int ppu2c0x_device::get_pixel( int x, int y ) |
| 1222 | 1310 | { |
| 1223 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1224 | 1311 | |
| 1225 | 1312 | if (x >= VISIBLE_SCREEN_WIDTH) |
| 1226 | 1313 | x = VISIBLE_SCREEN_WIDTH - 1; |
| r14138 | r14139 | |
| 1228 | 1315 | if (y >= VISIBLE_SCREEN_HEIGHT) |
| 1229 | 1316 | y = VISIBLE_SCREEN_HEIGHT - 1; |
| 1230 | 1317 | |
| 1231 | | return ppu2c0x->bitmap->pix16(y, x); |
| 1318 | return m_bitmap->pix16(y, x); |
| 1232 | 1319 | } |
| 1233 | 1320 | |
| 1234 | | int ppu2c0x_get_colorbase( device_t *device ) |
| 1235 | | { |
| 1236 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1237 | | return ppu2c0x->color_base; |
| 1238 | | } |
| 1239 | | |
| 1240 | | int ppu2c0x_get_current_scanline( device_t *device ) |
| 1241 | | { |
| 1242 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1243 | | return ppu2c0x->scanline; |
| 1244 | | } |
| 1245 | | |
| 1246 | | // MMC5 has to be able to check this |
| 1247 | | int ppu2c0x_is_sprite_8x16( device_t *device ) |
| 1248 | | { |
| 1249 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1250 | | return BIT(ppu2c0x->regs[0], 5); |
| 1251 | | } |
| 1252 | | |
| 1253 | | void ppu2c0x_set_scanline_callback( device_t *device, ppu2c0x_scanline_cb cb ) |
| 1254 | | { |
| 1255 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1256 | | ppu2c0x->scanline_callback_proc = cb; |
| 1257 | | } |
| 1258 | | |
| 1259 | | void ppu2c0x_set_hblank_callback( device_t *device, ppu2c0x_hblank_cb cb ) |
| 1260 | | { |
| 1261 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1262 | | ppu2c0x->hblank_callback_proc = cb; |
| 1263 | | } |
| 1264 | | |
| 1265 | | void ppu2c0x_set_vidaccess_callback( device_t *device, ppu2c0x_vidaccess_cb cb ) |
| 1266 | | { |
| 1267 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1268 | | ppu2c0x->vidaccess_callback_proc = cb; |
| 1269 | | } |
| 1270 | | |
| 1271 | | void ppu2c0x_set_scanlines_per_frame( device_t *device, int scanlines ) |
| 1272 | | { |
| 1273 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1274 | | ppu2c0x->scanlines_per_frame = scanlines; |
| 1275 | | } |
| 1276 | | |
| 1277 | | |
| 1278 | | /************************************* |
| 1279 | | * |
| 1280 | | * PPU Start |
| 1281 | | * |
| 1282 | | *************************************/ |
| 1283 | | |
| 1284 | | static DEVICE_START( ppu2c0x ) |
| 1285 | | { |
| 1286 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1287 | | const ppu2c0x_interface *intf = get_interface(device); |
| 1288 | | |
| 1289 | | memset(ppu2c0x, 0, sizeof(*ppu2c0x)); |
| 1290 | | ppu2c0x->space = device->memory().space(); |
| 1291 | | ppu2c0x->scanlines_per_frame = (device->type() != PPU_2C07) ? PPU_NTSC_SCANLINES_PER_FRAME : PPU_PAL_SCANLINES_PER_FRAME; |
| 1292 | | |
| 1293 | | /* usually, no security value... */ |
| 1294 | | ppu2c0x->security_value = 0; |
| 1295 | | |
| 1296 | | /* ...except for VS. games which specific PPU types */ |
| 1297 | | if (device->type() == PPU_2C05_01) |
| 1298 | | ppu2c0x->security_value = 0x1b; // game (jajamaru) doesn't seem to ever actually check it |
| 1299 | | |
| 1300 | | if (device->type() == PPU_2C05_02) |
| 1301 | | ppu2c0x->security_value = 0x3d; |
| 1302 | | |
| 1303 | | if (device->type() == PPU_2C05_03) |
| 1304 | | ppu2c0x->security_value = 0x1c; |
| 1305 | | |
| 1306 | | if (device->type() == PPU_2C05_04) |
| 1307 | | ppu2c0x->security_value = 0x1b; |
| 1308 | | |
| 1309 | | /* initialize the scanline handling portion */ |
| 1310 | | ppu2c0x->scanline_timer = device->machine().scheduler().timer_alloc(FUNC(scanline_callback), (void *) device); |
| 1311 | | ppu2c0x->scanline_timer->adjust(device->machine().primary_screen->time_until_pos(1)); |
| 1312 | | |
| 1313 | | ppu2c0x->hblank_timer = device->machine().scheduler().timer_alloc(FUNC(hblank_callback), (void *) device); |
| 1314 | | ppu2c0x->hblank_timer->adjust(device->machine().device<cpu_device>("maincpu")->cycles_to_attotime(86.67)); // ??? FIXME - hardcoding NTSC, need better calculation |
| 1315 | | |
| 1316 | | ppu2c0x->nmi_timer = device->machine().scheduler().timer_alloc(FUNC(nmi_callback), (void *) device); |
| 1317 | | ppu2c0x->nmi_timer->adjust(attotime::never); |
| 1318 | | |
| 1319 | | ppu2c0x->nmi_callback_proc = intf->nmi_handler; |
| 1320 | | ppu2c0x->color_base = intf->color_base; |
| 1321 | | |
| 1322 | | /* allocate a screen bitmap, videomem and spriteram, a dirtychar array and the monochromatic colortable */ |
| 1323 | | ppu2c0x->bitmap = auto_bitmap_ind16_alloc(device->machine(), VISIBLE_SCREEN_WIDTH, VISIBLE_SCREEN_HEIGHT); |
| 1324 | | ppu2c0x->spriteram = auto_alloc_array_clear(device->machine(), UINT8, SPRITERAM_SIZE); |
| 1325 | | ppu2c0x->colortable = auto_alloc_array(device->machine(), pen_t, ARRAY_LENGTH(default_colortable)); |
| 1326 | | ppu2c0x->colortable_mono = auto_alloc_array(device->machine(), pen_t, ARRAY_LENGTH(default_colortable_mono)); |
| 1327 | | |
| 1328 | | device->save_item(NAME(ppu2c0x->scanline)); |
| 1329 | | device->save_item(NAME(ppu2c0x->refresh_data)); |
| 1330 | | device->save_item(NAME(ppu2c0x->refresh_latch)); |
| 1331 | | device->save_item(NAME(ppu2c0x->x_fine)); |
| 1332 | | device->save_item(NAME(ppu2c0x->toggle)); |
| 1333 | | device->save_item(NAME(ppu2c0x->add)); |
| 1334 | | device->save_item(NAME(ppu2c0x->videomem_addr)); |
| 1335 | | device->save_item(NAME(ppu2c0x->addr_latch)); |
| 1336 | | device->save_item(NAME(ppu2c0x->data_latch)); |
| 1337 | | device->save_item(NAME(ppu2c0x->buffered_data)); |
| 1338 | | device->save_item(NAME(ppu2c0x->tile_page)); |
| 1339 | | device->save_item(NAME(ppu2c0x->sprite_page)); |
| 1340 | | device->save_item(NAME(ppu2c0x->back_color)); |
| 1341 | | device->save_item(NAME(ppu2c0x->scan_scale)); |
| 1342 | | device->save_item(NAME(ppu2c0x->scanlines_per_frame)); |
| 1343 | | device->save_item(NAME(ppu2c0x->regs)); |
| 1344 | | device->save_item(NAME(ppu2c0x->palette_ram)); |
| 1345 | | device->save_pointer(NAME(ppu2c0x->spriteram), SPRITERAM_SIZE); |
| 1346 | | device->save_pointer(NAME(ppu2c0x->colortable), ARRAY_LENGTH(default_colortable)); |
| 1347 | | device->save_pointer(NAME(ppu2c0x->colortable_mono), ARRAY_LENGTH(default_colortable_mono)); |
| 1348 | | device->save_item(NAME(*ppu2c0x->bitmap)); |
| 1349 | | } |
| 1350 | | |
| 1351 | | /************************************* |
| 1352 | | * |
| 1353 | | * PPU Reset |
| 1354 | | * |
| 1355 | | *************************************/ |
| 1356 | | |
| 1357 | | static DEVICE_RESET( ppu2c0x ) |
| 1358 | | { |
| 1359 | | ppu2c0x_state *ppu2c0x = get_token(device); |
| 1360 | | const ppu2c0x_interface *intf = get_interface(device); |
| 1361 | | int i; |
| 1362 | | |
| 1363 | | /* reset the scanline count */ |
| 1364 | | ppu2c0x->scanline = 0; |
| 1365 | | |
| 1366 | | /* set the scan scale (this is for dual monitor vertical setups) */ |
| 1367 | | ppu2c0x->scan_scale = 1; |
| 1368 | | |
| 1369 | | /* reset the callbacks */ |
| 1370 | | ppu2c0x->scanline_callback_proc = NULL; |
| 1371 | | ppu2c0x->vidaccess_callback_proc = NULL; |
| 1372 | | |
| 1373 | | for (i = 0; i < PPU_MAX_REG; i++) |
| 1374 | | ppu2c0x->regs[i] = 0; |
| 1375 | | |
| 1376 | | memset(ppu2c0x->palette_ram, 0, ARRAY_LENGTH(ppu2c0x->palette_ram)); |
| 1377 | | |
| 1378 | | /* initialize the rest of the members */ |
| 1379 | | ppu2c0x->refresh_data = 0; |
| 1380 | | ppu2c0x->refresh_latch = 0; |
| 1381 | | ppu2c0x->x_fine = 0; |
| 1382 | | ppu2c0x->toggle = 0; |
| 1383 | | ppu2c0x->add = 1; |
| 1384 | | ppu2c0x->videomem_addr = 0; |
| 1385 | | ppu2c0x->addr_latch = 0; |
| 1386 | | ppu2c0x->data_latch = 0; |
| 1387 | | ppu2c0x->tile_page = 0; |
| 1388 | | ppu2c0x->sprite_page = 0; |
| 1389 | | ppu2c0x->back_color = 0; |
| 1390 | | ppu2c0x->buffered_data = 0; |
| 1391 | | |
| 1392 | | /* initialize the color tables */ |
| 1393 | | { |
| 1394 | | int color_base = intf->color_base; |
| 1395 | | |
| 1396 | | for (i = 0; i < ARRAY_LENGTH(default_colortable_mono); i++) |
| 1397 | | { |
| 1398 | | /* monochromatic table */ |
| 1399 | | ppu2c0x->colortable_mono[i] = default_colortable_mono[i] + color_base; |
| 1400 | | |
| 1401 | | /* color table */ |
| 1402 | | ppu2c0x->colortable[i] = default_colortable[i] + color_base; |
| 1403 | | } |
| 1404 | | } |
| 1405 | | } |
| 1406 | | |
| 1407 | | /*************************************************************************** |
| 1408 | | GET INFO FUNCTIONS |
| 1409 | | ***************************************************************************/ |
| 1410 | | |
| 1411 | | DEVICE_GET_INFO(ppu2c02) |
| 1412 | | { |
| 1413 | | switch (state) |
| 1414 | | { |
| 1415 | | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 1416 | | case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(ppu2c0x_state); break; |
| 1417 | | case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = 0; break; |
| 1418 | | case DEVINFO_INT_DATABUS_WIDTH_0: info->i = 8; break; |
| 1419 | | case DEVINFO_INT_ADDRBUS_WIDTH_0: info->i = 14; break; |
| 1420 | | case DEVINFO_INT_ADDRBUS_SHIFT_0: info->i = 0; break; |
| 1421 | | |
| 1422 | | |
| 1423 | | /* --- the following bits of info are returned as pointers to data --- */ |
| 1424 | | case DEVINFO_PTR_DEFAULT_MEMORY_MAP_0: info->default_map8 = ADDRESS_MAP_NAME(ppu2c0x);break; |
| 1425 | | |
| 1426 | | |
| 1427 | | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 1428 | | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(ppu2c0x); break; |
| 1429 | | case DEVINFO_FCT_STOP: /* Nothing */ break; |
| 1430 | | case DEVINFO_FCT_RESET: info->reset = DEVICE_RESET_NAME(ppu2c0x); break; |
| 1431 | | |
| 1432 | | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 1433 | | case DEVINFO_STR_NAME: strcpy(info->s, "2C02 PPU"); break; |
| 1434 | | case DEVINFO_STR_FAMILY: strcpy(info->s, "2C0X PPU"); break; |
| 1435 | | case DEVINFO_STR_VERSION: strcpy(info->s, "1.0"); break; |
| 1436 | | case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break; |
| 1437 | | case DEVINFO_STR_CREDITS: /* Nothing */ break; |
| 1438 | | } |
| 1439 | | } |
| 1440 | | |
| 1441 | | DEVICE_GET_INFO(ppu2c03b) |
| 1442 | | { |
| 1443 | | switch (state) |
| 1444 | | { |
| 1445 | | case DEVINFO_STR_NAME: strcpy(info->s, "2C02B PPU"); break; |
| 1446 | | default: DEVICE_GET_INFO_CALL(ppu2c02); break; |
| 1447 | | } |
| 1448 | | } |
| 1449 | | |
| 1450 | | DEVICE_GET_INFO(ppu2c04) |
| 1451 | | { |
| 1452 | | switch (state) |
| 1453 | | { |
| 1454 | | case DEVINFO_STR_NAME: strcpy(info->s, "2C04 PPU"); break; |
| 1455 | | default: DEVICE_GET_INFO_CALL(ppu2c02); break; |
| 1456 | | } |
| 1457 | | } |
| 1458 | | |
| 1459 | | DEVICE_GET_INFO(ppu2c05_01) |
| 1460 | | { |
| 1461 | | switch (state) |
| 1462 | | { |
| 1463 | | case DEVINFO_STR_NAME: strcpy(info->s, "2C05 PPU"); break; |
| 1464 | | default: DEVICE_GET_INFO_CALL(ppu2c02); break; |
| 1465 | | } |
| 1466 | | } |
| 1467 | | |
| 1468 | | DEVICE_GET_INFO(ppu2c05_02) |
| 1469 | | { |
| 1470 | | DEVICE_GET_INFO_CALL(ppu2c05_01); |
| 1471 | | } |
| 1472 | | |
| 1473 | | DEVICE_GET_INFO(ppu2c05_03) |
| 1474 | | { |
| 1475 | | DEVICE_GET_INFO_CALL(ppu2c05_01); |
| 1476 | | } |
| 1477 | | |
| 1478 | | DEVICE_GET_INFO(ppu2c05_04) |
| 1479 | | { |
| 1480 | | DEVICE_GET_INFO_CALL(ppu2c05_01); |
| 1481 | | } |
| 1482 | | |
| 1483 | | DEVICE_GET_INFO(ppu2c07) |
| 1484 | | { |
| 1485 | | switch (state) |
| 1486 | | { |
| 1487 | | case DEVINFO_STR_NAME: strcpy(info->s, "2C07 PPU"); break; |
| 1488 | | default: DEVICE_GET_INFO_CALL(ppu2c02); break; |
| 1489 | | } |
| 1490 | | } |
| 1491 | | |
| 1492 | | |
| 1493 | | DEFINE_LEGACY_MEMORY_DEVICE(PPU_2C02, ppu2c02); // NTSC NES |
| 1494 | | DEFINE_LEGACY_MEMORY_DEVICE(PPU_2C03B, ppu2c03b); // Playchoice 10 |
| 1495 | | DEFINE_LEGACY_MEMORY_DEVICE(PPU_2C04, ppu2c04); // Vs. Unisystem |
| 1496 | | // The PPU_2C05 variants have different protection value, set at DEVICE_START, but otherwise are all the same... |
| 1497 | | DEFINE_LEGACY_MEMORY_DEVICE(PPU_2C05_01, ppu2c05_01); // Vs. Unisystem (Ninja Jajamaru Kun) |
| 1498 | | DEFINE_LEGACY_MEMORY_DEVICE(PPU_2C05_02, ppu2c05_02); // Vs. Unisystem (Mighty Bomb Jack) |
| 1499 | | DEFINE_LEGACY_MEMORY_DEVICE(PPU_2C05_03, ppu2c05_03); // Vs. Unisystem (Gumshoe) |
| 1500 | | DEFINE_LEGACY_MEMORY_DEVICE(PPU_2C05_04, ppu2c05_04); // Vs. Unisystem (Top Gun) |
| 1501 | | DEFINE_LEGACY_MEMORY_DEVICE(PPU_2C07, ppu2c07); // PAL NES |
src/mame/video/ppu2c0x.h
| r14138 | r14139 | |
| 7 | 7 | |
| 8 | 8 | ******************************************************************************/ |
| 9 | 9 | |
| 10 | #pragma once |
| 11 | |
| 10 | 12 | #ifndef __PPU_2C03B_H__ |
| 11 | 13 | #define __PPU_2C03B_H__ |
| 12 | 14 | |
| 13 | 15 | #include "devlegcy.h" |
| 14 | 16 | |
| 15 | 17 | |
| 16 | | /*************************************************************************** |
| 17 | | CONSTANTS |
| 18 | | ***************************************************************************/ |
| 18 | ///************************************************************************* |
| 19 | // MACROS / CONSTANTS |
| 20 | ///************************************************************************* |
| 19 | 21 | |
| 20 | | /* mirroring types */ |
| 22 | // mirroring types |
| 21 | 23 | #define PPU_MIRROR_NONE 0 |
| 22 | 24 | #define PPU_MIRROR_VERT 1 |
| 23 | 25 | #define PPU_MIRROR_HORZ 2 |
| r14138 | r14139 | |
| 25 | 27 | #define PPU_MIRROR_LOW 4 |
| 26 | 28 | #define PPU_MIRROR_4SCREEN 5 // Same effect as NONE, but signals that we should never mirror |
| 27 | 29 | |
| 28 | | /* registers definition */ |
| 30 | // registers definition |
| 29 | 31 | enum |
| 30 | 32 | { |
| 31 | 33 | PPU_CONTROL0 = 0, |
| r14138 | r14139 | |
| 39 | 41 | PPU_MAX_REG |
| 40 | 42 | }; |
| 41 | 43 | |
| 42 | | /* bit definitions for (some of) the registers */ |
| 44 | // bit definitions for (some of) the registers |
| 43 | 45 | enum |
| 44 | 46 | { |
| 45 | 47 | PPU_CONTROL0_INC = 0x04, |
| r14138 | r14139 | |
| 74 | 76 | // are non-rendering and non-vblank. |
| 75 | 77 | }; |
| 76 | 78 | |
| 77 | | /* callback datatypes */ |
| 79 | /*----------- defined in video/ppu2c0x.c -----------*/ |
| 80 | |
| 81 | ///************************************************************************* |
| 82 | // INTERFACE CONFIGURATION MACROS |
| 83 | ///************************************************************************* |
| 84 | |
| 85 | #define MCFG_PPU2C0X_ADD(_tag, _type, _intrf) \ |
| 86 | MCFG_DEVICE_ADD(_tag, _type, 0) \ |
| 87 | MCFG_DEVICE_CONFIG(_intrf) |
| 88 | |
| 89 | #define MCFG_PPU2C02_ADD(_tag, _intrf) \ |
| 90 | MCFG_PPU2C0X_ADD(_tag, PPU_2C02, _intrf) \ |
| 91 | |
| 92 | #define MCFG_PPU2C03B_ADD(_tag, _intrf) \ |
| 93 | MCFG_PPU2C0X_ADD(_tag, PPU_2C03B, _intrf) \ |
| 94 | |
| 95 | #define MCFG_PPU2C04_ADD(_tag, _intrf) \ |
| 96 | MCFG_PPU2C0X_ADD(_tag, PPU_2C04, _intrf) \ |
| 97 | |
| 98 | #define MCFG_PPU2C07_ADD(_tag, _intrf) \ |
| 99 | MCFG_PPU2C0X_ADD(_tag, PPU_2C07, _intrf) \ |
| 100 | |
| 101 | #define MCFG_PPU2C05_01_ADD(_tag, _intrf) \ |
| 102 | MCFG_PPU2C0X_ADD(_tag, PPU_2C05_01, _intrf) \ |
| 103 | |
| 104 | #define MCFG_PPU2C05_02_ADD(_tag, _intrf) \ |
| 105 | MCFG_PPU2C0X_ADD(_tag, PPU_2C05_02, _intrf) \ |
| 106 | |
| 107 | #define MCFG_PPU2C05_03_ADD(_tag, _intrf) \ |
| 108 | MCFG_PPU2C0X_ADD(_tag, PPU_2C05_03, _intrf) \ |
| 109 | |
| 110 | #define MCFG_PPU2C05_04_ADD(_tag, _intrf) \ |
| 111 | MCFG_PPU2C0X_ADD(_tag, PPU_2C05_04, _intrf) \ |
| 112 | |
| 113 | |
| 114 | ///************************************************************************* |
| 115 | // TYPE DEFINITIONS |
| 116 | ///************************************************************************* |
| 117 | |
| 118 | // callback datatypes |
| 78 | 119 | typedef void (*ppu2c0x_scanline_cb)( device_t *device, int scanline, int vblank, int blanked ); |
| 79 | 120 | typedef void (*ppu2c0x_hblank_cb)( device_t *device, int scanline, int vblank, int blanked ); |
| 80 | 121 | typedef void (*ppu2c0x_nmi_cb)( device_t *device, int *ppu_regs ); |
| 81 | 122 | typedef int (*ppu2c0x_vidaccess_cb)( device_t *device, int address, int data ); |
| 82 | 123 | |
| 83 | | typedef struct _ppu2c0x_interface ppu2c0x_interface; |
| 84 | | struct _ppu2c0x_interface |
| 124 | |
| 125 | // ======================> ppu2c0x_interface |
| 126 | |
| 127 | struct ppu2c0x_interface |
| 85 | 128 | { |
| 129 | const char *cpu_tag; |
| 130 | const char *screen_tag; |
| 86 | 131 | int gfx_layout_number; /* gfx layout number used by each chip */ |
| 87 | 132 | int color_base; /* color base to use per ppu */ |
| 88 | 133 | int mirroring; /* mirroring options (PPU_MIRROR_* flag) */ |
| r14138 | r14139 | |
| 90 | 135 | }; |
| 91 | 136 | |
| 92 | 137 | |
| 93 | | /*************************************************************************** |
| 94 | | PROTOTYPES |
| 95 | | ***************************************************************************/ |
| 138 | // ======================> ppu2c0x_device |
| 96 | 139 | |
| 97 | | DECLARE_LEGACY_MEMORY_DEVICE(PPU_2C02, ppu2c02); // NTSC NES |
| 98 | | DECLARE_LEGACY_MEMORY_DEVICE(PPU_2C03B, ppu2c03b); // Playchoice 10 |
| 99 | | DECLARE_LEGACY_MEMORY_DEVICE(PPU_2C04, ppu2c04); // Vs. Unisystem |
| 100 | | // The PPU_2C05 variants have different protection value, set at DEVICE_START, but otherwise are all the same... |
| 101 | | DECLARE_LEGACY_MEMORY_DEVICE(PPU_2C05_01, ppu2c05_01); // Vs. Unisystem (Ninja Jajamaru Kun) |
| 102 | | DECLARE_LEGACY_MEMORY_DEVICE(PPU_2C05_02, ppu2c05_02); // Vs. Unisystem (Mighty Bomb Jack) |
| 103 | | DECLARE_LEGACY_MEMORY_DEVICE(PPU_2C05_03, ppu2c05_03); // Vs. Unisystem (Gumshoe) |
| 104 | | DECLARE_LEGACY_MEMORY_DEVICE(PPU_2C05_04, ppu2c05_04); // Vs. Unisystem (Top Gun) |
| 105 | | DECLARE_LEGACY_MEMORY_DEVICE(PPU_2C07, ppu2c07); // PAL NES |
| 140 | class ppu2c0x_device : public device_t, |
| 141 | public device_memory_interface, |
| 142 | public ppu2c0x_interface |
| 143 | { |
| 144 | public: |
| 145 | // construction/destruction |
| 146 | ppu2c0x_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock); |
| 147 | |
| 148 | DECLARE_READ8_MEMBER( read ); |
| 149 | DECLARE_WRITE8_MEMBER( write ); |
| 150 | DECLARE_READ8_MEMBER( palette_read ); |
| 151 | DECLARE_WRITE8_MEMBER( palette_write ); |
| 106 | 152 | |
| 107 | | /* routines */ |
| 108 | | void ppu2c0x_init_palette(running_machine &machine, int first_entry ); |
| 109 | | void ppu2c0x_init_palette_rgb(running_machine &machine, int first_entry ); |
| 153 | virtual void device_start(); |
| 154 | virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr); |
| 155 | virtual void device_config_complete(); |
| 156 | // device_config_memory_interface overrides |
| 157 | virtual const address_space_config *memory_space_config(address_spacenum spacenum = AS_0) const; |
| 158 | // address space configurations |
| 159 | const address_space_config m_space_config; |
| 160 | |
| 161 | |
| 162 | /* routines */ |
| 163 | void init_palette( running_machine &machine, int first_entry ); |
| 164 | void init_palette_rgb( running_machine &machine, int first_entry ); |
| 165 | |
| 166 | void draw_background( UINT8 *line_priority ); |
| 167 | void draw_sprites( UINT8 *line_priority ); |
| 168 | void render_scanline(); |
| 169 | void update_scanline(); |
| 110 | 170 | |
| 111 | | void ppu2c0x_spriteram_dma(address_space *space, device_t *device, const UINT8 page ) ATTR_NONNULL(1); |
| 112 | | void ppu2c0x_render( device_t *device, bitmap_ind16 &bitmap, int flipx, int flipy, int sx, int sy ) ATTR_NONNULL(1); |
| 113 | | int ppu2c0x_get_pixel( device_t *device, int x, int y ) ATTR_NONNULL(1); |
| 114 | | int ppu2c0x_get_colorbase( device_t *device ) ATTR_NONNULL(1); |
| 115 | | int ppu2c0x_get_current_scanline( device_t *device ) ATTR_NONNULL(1); |
| 116 | | int ppu2c0x_is_sprite_8x16( device_t *device ) ATTR_NONNULL(1); |
| 117 | | void ppu2c0x_set_scanline_callback( device_t *device, ppu2c0x_scanline_cb cb ) ATTR_NONNULL(1); |
| 118 | | void ppu2c0x_set_hblank_callback( device_t *device, ppu2c0x_scanline_cb cb ) ATTR_NONNULL(1); |
| 119 | | void ppu2c0x_set_vidaccess_callback( device_t *device, ppu2c0x_vidaccess_cb cb ) ATTR_NONNULL(1); |
| 120 | | void ppu2c0x_set_scanlines_per_frame( device_t *device, int scanlines ) ATTR_NONNULL(1); |
| 171 | void spriteram_dma(address_space *space, const UINT8 page ) ATTR_NONNULL(1); |
| 172 | void render( bitmap_ind16 &bitmap, int flipx, int flipy, int sx, int sy ) ATTR_NONNULL(1); |
| 173 | int get_pixel( int x, int y ) ATTR_NONNULL(1); |
| 121 | 174 | |
| 122 | | //27/12/2002 (HACK!) |
| 123 | | void ppu2c0x_set_latch( device_t *device, void (*ppu_latch_t)( device_t *device, offs_t offset )); |
| 124 | | WRITE8_DEVICE_HANDLER( ppu2c0x_w ); |
| 125 | | READ8_DEVICE_HANDLER( ppu2c0x_r ); |
| 175 | int get_colorbase() { return m_color_base; }; |
| 176 | int get_current_scanline() { return m_scanline; }; |
| 177 | int is_sprite_8x16() { return BIT(m_regs[0], 5); }; // MMC5 has to be able to check this |
| 178 | void set_scanline_callback( ppu2c0x_scanline_cb cb ) { if (cb != NULL) m_scanline_callback_proc = cb; }; |
| 179 | void set_hblank_callback( ppu2c0x_scanline_cb cb ) { if (cb != NULL) m_hblank_callback_proc = cb; }; |
| 180 | void set_vidaccess_callback( ppu2c0x_vidaccess_cb cb ) { if (cb != NULL) m_vidaccess_callback_proc = cb; }; |
| 181 | void set_scanlines_per_frame( int scanlines ) { m_scanlines_per_frame = scanlines; }; |
| 182 | |
| 183 | //27/12/2002 (HACK!) |
| 184 | void set_latch( void (*ppu_latch_t)( device_t *device, offs_t offset ) ); |
| 185 | |
| 186 | // void update_screen(bitmap_t &bitmap, const rectangle &cliprect); |
| 187 | |
| 188 | cpu_device *m_cpu; |
| 189 | screen_device *m_screen; |
| 190 | bitmap_ind16 *m_bitmap; /* target bitmap */ |
| 191 | UINT8 *m_spriteram; /* sprite ram */ |
| 192 | pen_t *m_colortable; /* color table modified at run time */ |
| 193 | pen_t *m_colortable_mono; /* monochromatic color table modified at run time */ |
| 194 | int m_scanline; /* scanline count */ |
| 195 | ppu2c0x_scanline_cb m_scanline_callback_proc; /* optional scanline callback */ |
| 196 | ppu2c0x_hblank_cb m_hblank_callback_proc; /* optional hblank callback */ |
| 197 | ppu2c0x_vidaccess_cb m_vidaccess_callback_proc; /* optional video access callback */ |
| 198 | ppu2c0x_nmi_cb m_nmi_callback_proc; /* nmi access callback from interface */ |
| 199 | int m_regs[PPU_MAX_REG]; /* registers */ |
| 200 | int m_refresh_data; /* refresh-related */ |
| 201 | int m_refresh_latch; /* refresh-related */ |
| 202 | int m_x_fine; /* refresh-related */ |
| 203 | int m_toggle; /* used to latch hi-lo scroll */ |
| 204 | int m_add; /* vram increment amount */ |
| 205 | int m_videomem_addr; /* videomem address pointer */ |
| 206 | int m_data_latch; /* latched videomem data */ |
| 207 | int m_buffered_data; |
| 208 | int m_tile_page; /* current tile page */ |
| 209 | int m_sprite_page; /* current sprite page */ |
| 210 | int m_back_color; /* background color */ |
| 211 | int m_color_base; |
| 212 | UINT8 m_palette_ram[0x20]; /* shouldn't be in main memory! */ |
| 213 | int m_scan_scale; /* scan scale */ |
| 214 | int m_scanlines_per_frame; /* number of scanlines per frame */ |
| 215 | int m_security_value; /* 2C05 protection */ |
| 216 | void (*m_latch)( device_t *device, offs_t offset ); |
| 217 | |
| 218 | // timers |
| 219 | emu_timer *m_hblank_timer; /* hblank period at end of each scanline */ |
| 220 | emu_timer *m_nmi_timer; /* NMI timer */ |
| 221 | emu_timer *m_scanline_timer; /* scanline timer */ |
| 126 | 222 | |
| 223 | const char *m_cpu_tag; |
| 224 | const char *m_screen_tag; |
| 127 | 225 | |
| 128 | | /*************************************************************************** |
| 129 | | DEVICE CONFIGURATION MACROS |
| 130 | | ***************************************************************************/ |
| 226 | private: |
| 227 | static const device_timer_id TIMER_HBLANK = 0; |
| 228 | static const device_timer_id TIMER_NMI = 1; |
| 229 | static const device_timer_id TIMER_SCANLINE = 2; |
| 131 | 230 | |
| 132 | | #define MCFG_PPU2C02_ADD(_tag, _intrf) \ |
| 133 | | MCFG_DEVICE_ADD(_tag, PPU_2C02, 0) \ |
| 134 | | MCFG_DEVICE_CONFIG(_intrf) |
| 231 | inline UINT8 readbyte(offs_t address); |
| 232 | inline void writebyte(offs_t address, UINT8 data); |
| 233 | }; |
| 135 | 234 | |
| 136 | | #define MCFG_PPU2C03B_ADD(_tag, _intrf) \ |
| 137 | | MCFG_DEVICE_ADD(_tag, PPU_2C03B, 0) \ |
| 138 | | MCFG_DEVICE_CONFIG(_intrf) |
| 235 | class ppu2c02_device : public ppu2c0x_device { |
| 236 | public: |
| 237 | ppu2c02_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); |
| 238 | }; |
| 139 | 239 | |
| 140 | | #define MCFG_PPU2C04_ADD(_tag, _intrf) \ |
| 141 | | MCFG_DEVICE_ADD(_tag, PPU_2C04, 0) \ |
| 142 | | MCFG_DEVICE_CONFIG(_intrf) |
| 240 | class ppu2c03b_device : public ppu2c0x_device { |
| 241 | public: |
| 242 | ppu2c03b_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); |
| 243 | }; |
| 143 | 244 | |
| 144 | | #define MCFG_PPU2C05_01_ADD(_tag, _intrf) \ |
| 145 | | MCFG_DEVICE_ADD(_tag, PPU_2C05_01, 0) \ |
| 146 | | MCFG_DEVICE_CONFIG(_intrf) |
| 245 | class ppu2c04_device : public ppu2c0x_device { |
| 246 | public: |
| 247 | ppu2c04_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); |
| 248 | }; |
| 147 | 249 | |
| 148 | | #define MCFG_PPU2C05_02_ADD(_tag, _intrf) \ |
| 149 | | MCFG_DEVICE_ADD(_tag, PPU_2C05_02, 0) \ |
| 150 | | MCFG_DEVICE_CONFIG(_intrf) |
| 250 | class ppu2c07_device : public ppu2c0x_device { |
| 251 | public: |
| 252 | ppu2c07_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); |
| 253 | }; |
| 151 | 254 | |
| 152 | | #define MCFG_PPU2C05_03_ADD(_tag, _intrf) \ |
| 153 | | MCFG_DEVICE_ADD(_tag, PPU_2C05_03, 0) \ |
| 154 | | MCFG_DEVICE_CONFIG(_intrf) |
| 255 | class ppu2c05_01_device : public ppu2c0x_device { |
| 256 | public: |
| 257 | ppu2c05_01_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); |
| 258 | }; |
| 155 | 259 | |
| 156 | | #define MCFG_PPU2C05_04_ADD(_tag, _intrf) \ |
| 157 | | MCFG_DEVICE_ADD(_tag, PPU_2C05_04, 0) \ |
| 158 | | MCFG_DEVICE_CONFIG(_intrf) |
| 260 | class ppu2c05_02_device : public ppu2c0x_device { |
| 261 | public: |
| 262 | ppu2c05_02_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); |
| 263 | }; |
| 159 | 264 | |
| 160 | | #define MCFG_PPU2C07_ADD(_tag, _intrf) \ |
| 161 | | MCFG_DEVICE_ADD(_tag, PPU_2C07, 0) \ |
| 162 | | MCFG_DEVICE_CONFIG(_intrf) |
| 265 | class ppu2c05_03_device : public ppu2c0x_device { |
| 266 | public: |
| 267 | ppu2c05_03_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); |
| 268 | }; |
| 163 | 269 | |
| 164 | | #endif /* __PPU_2C0X_H__ */ |
| 270 | class ppu2c05_04_device : public ppu2c0x_device { |
| 271 | public: |
| 272 | ppu2c05_04_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock); |
| 273 | }; |
| 274 | |
| 275 | |
| 276 | // device type definition |
| 277 | //extern const device_type PPU_2C0X; |
| 278 | extern const device_type PPU_2C02; // NTSC NES |
| 279 | extern const device_type PPU_2C03B; // Playchoice 10 |
| 280 | extern const device_type PPU_2C04; // Vs. Unisystem |
| 281 | extern const device_type PPU_2C07; // PAL NES |
| 282 | extern const device_type PPU_2C05_01; // Vs. Unisystem (Ninja Jajamaru Kun) |
| 283 | extern const device_type PPU_2C05_02; // Vs. Unisystem (Mighty Bomb Jack) |
| 284 | extern const device_type PPU_2C05_03; // Vs. Unisystem (Gumshoe) |
| 285 | extern const device_type PPU_2C05_04; // Vs. Unisystem (Top Gun) |
| 286 | |
| 287 | |
| 288 | #endif |