-konami/simpsons.cpp: Cleaned up code. (#13312)

* Reduced literal tags and run-time tag lookups, use more descriptive tags.

-konami/konami_helper.cpp: Made layer sorting functions templates, reduced dependence on preprocessor.
This commit is contained in:
cam900 2025-02-08 06:07:12 +09:00 committed by GitHub
parent b128c1f9b6
commit 6f1fce3399
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 112 additions and 132 deletions

View File

@ -14,65 +14,3 @@ void konami_decode_gfx(device_gfx_interface &gfxdecode, int gfx_index, uint8_t *
gl.total = total;
gfxdecode.set_gfx(gfx_index, std::make_unique<gfx_element>(&palette, gl, data, 0, palette.entries() >> bpp, 0));
}
/* useful function to sort three tile layers by priority order */
void konami_sortlayers3( int *layer, int *pri )
{
#define SWAP(a,b) \
if (pri[a] < pri[b]) \
{ \
int t; \
t = pri[a]; pri[a] = pri[b]; pri[b] = t; \
t = layer[a]; layer[a] = layer[b]; layer[b] = t; \
}
SWAP(0,1)
SWAP(0,2)
SWAP(1,2)
#undef SWAP
}
/* useful function to sort four tile layers by priority order */
void konami_sortlayers4( int *layer, int *pri )
{
#define SWAP(a,b) \
if (pri[a] <= pri[b]) \
{ \
int t; \
t = pri[a]; pri[a] = pri[b]; pri[b] = t; \
t = layer[a]; layer[a] = layer[b]; layer[b] = t; \
}
SWAP(0, 1)
SWAP(0, 2)
SWAP(0, 3)
SWAP(1, 2)
SWAP(1, 3)
SWAP(2, 3)
#undef SWAP
}
/* useful function to sort five tile layers by priority order */
void konami_sortlayers5( int *layer, int *pri )
{
#define SWAP(a,b) \
if (pri[a] <= pri[b]) \
{ \
int t; \
t = pri[a]; pri[a] = pri[b]; pri[b] = t; \
t = layer[a]; layer[a] = layer[b]; layer[b] = t; \
}
SWAP(0, 1)
SWAP(0, 2)
SWAP(0, 3)
SWAP(0, 4)
SWAP(1, 2)
SWAP(1, 3)
SWAP(1, 4)
SWAP(2, 3)
SWAP(2, 4)
SWAP(3, 4)
#undef SWAP
}

View File

@ -6,13 +6,47 @@
#pragma once
#include <algorithm>
#include <functional>
void konami_decode_gfx(device_gfx_interface &gfxdecode, int gfx_index, uint8_t *data, uint32_t total, const gfx_layout *layout, int bpp);
// unrolled bubble sort helper
template <unsigned N, unsigned A = N - 2, unsigned B = N - 1, typename C, typename T, typename U>
inline void konami_sortlayers(C cmp, T *layer, U *pri)
{
if constexpr (B > (A + 1))
konami_sortlayers<N, A, B - 1>(cmp, layer, pri);
else if constexpr (A > 0)
konami_sortlayers<N, A - 1, N - 1>(cmp, layer, pri);
if (cmp(pri[A], pri[B]))
{
using std::swap;
swap(pri[A], pri[B]);
swap(layer[A], layer[B]);
}
}
// helper function to sort three tile layers by priority order
void konami_sortlayers3(int *layer, int *pri);
template <typename T, typename U>
inline void konami_sortlayers3(T *layer, U *pri)
{
konami_sortlayers<3>(std::less<U>(), layer, pri);
}
// helper function to sort four tile layers by priority order
void konami_sortlayers4(int *layer, int *pri);
template <typename T, typename U>
inline void konami_sortlayers4(T *layer, U *pri)
{
konami_sortlayers<4>(std::less_equal<U>(), layer, pri);
}
// helper function to sort five tile layers by priority order
void konami_sortlayers5(int *layer, int *pri);
template <typename T, typename U>
inline void konami_sortlayers5(T *layer, U *pri)
{
konami_sortlayers<5>(std::less_equal<U>(), layer, pri);
}
#endif // MAME_KONAMI_KONAMI_HELPER_H

View File

@ -113,6 +113,8 @@ Notes:
#include "emupal.h"
#include "speaker.h"
#include <algorithm>
namespace {
@ -121,13 +123,16 @@ class simpsons_state : public driver_device
public:
simpsons_state(const machine_config &mconfig, device_type type, const char *tag) :
driver_device(mconfig, type, tag),
m_palette_view(*this, "palette_view"),
m_video_view(*this, "video_view"),
m_maincpu(*this, "maincpu"),
m_audiocpu(*this, "audiocpu"),
m_k052109(*this, "k052109"),
m_k053246(*this, "k053246"),
m_k053251(*this, "k053251")
m_k053251(*this, "k053251"),
m_mainbank(*this, "mainbank"),
m_audiobank(*this, "audiobank"),
m_io_eepromout(*this, "EEPROMOUT"),
m_palette_view(*this, "palette_view"),
m_video_view(*this, "video_view")
{ }
void simpsons(machine_config &config);
@ -137,38 +142,34 @@ protected:
virtual void machine_reset() override ATTR_COLD;
private:
void bank0000_map(address_map &map) ATTR_COLD;
void bank2000_map(address_map &map) ATTR_COLD;
void main_map(address_map &map) ATTR_COLD;
void z80_map(address_map &map) ATTR_COLD;
TIMER_CALLBACK_MEMBER(dma_start);
TIMER_CALLBACK_MEMBER(dma_end);
/* memory pointers */
std::unique_ptr<uint16_t[]> m_spriteram;
/* video-related */
int m_sprite_colorbase = 0;
int m_layer_colorbase[3]{};
int m_layerpri[3]{};
emu_timer *m_dma_start_timer;
emu_timer *m_dma_end_timer;
/* misc */
int m_firq_enabled = 0;
emu_timer *m_nmi_blocked;
/* views */
memory_view m_palette_view;
memory_view m_video_view;
/* devices */
required_device<konami_cpu_device> m_maincpu;
required_device<cpu_device> m_audiocpu;
required_device<k052109_device> m_k052109;
required_device<k053247_device> m_k053246;
required_device<k053251_device> m_k053251;
required_memory_bank m_mainbank;
required_memory_bank m_audiobank;
required_ioport m_io_eepromout;
/* memory pointers */
std::unique_ptr<uint16_t[]> m_spriteram;
/* video-related */
int32_t m_sprite_colorbase = 0;
int32_t m_layer_colorbase[3]{};
int32_t m_layerpri[3]{};
emu_timer *m_dma_start_timer;
emu_timer *m_dma_end_timer;
/* misc */
bool m_firq_enabled = false;
emu_timer *m_nmi_blocked;
/* views */
memory_view m_palette_view;
memory_view m_video_view;
void z80_bankswitch_w(uint8_t data);
void z80_arm_nmi_w(uint8_t data);
void eeprom_w(uint8_t data);
@ -186,6 +187,12 @@ private:
K052109_CB_MEMBER(tile_callback);
void banking_callback(u8 data);
K053246_CB_MEMBER(sprite_callback);
TIMER_CALLBACK_MEMBER(dma_start);
TIMER_CALLBACK_MEMBER(dma_end);
void main_map(address_map &map) ATTR_COLD;
void z80_map(address_map &map) ATTR_COLD;
};
@ -211,7 +218,7 @@ K052109_CB_MEMBER(simpsons_state::tile_callback)
K053246_CB_MEMBER(simpsons_state::sprite_callback)
{
int pri = (*color & 0x0f80) >> 6; /* ??????? */
int const pri = (*color & 0x0f80) >> 6; /* ??????? */
if (pri <= m_layerpri[2])
*priority_mask = 0;
@ -244,9 +251,9 @@ void simpsons_state::k052109_w(offs_t offset, uint8_t data)
uint8_t simpsons_state::k053247_r(offs_t offset)
{
int offs = offset >> 1;
int const offs = offset >> 1;
if (offset & 1)
if (BIT(offset, 0))
return(m_spriteram[offs] & 0xff);
else
return(m_spriteram[offs] >> 8);
@ -254,21 +261,21 @@ uint8_t simpsons_state::k053247_r(offs_t offset)
void simpsons_state::k053247_w(offs_t offset, uint8_t data)
{
int offs = offset >> 1;
int const offs = offset >> 1;
if (offset & 1)
if (BIT(offset, 0))
m_spriteram[offs] = (m_spriteram[offs] & 0xff00) | data;
else
m_spriteram[offs] = (m_spriteram[offs] & 0x00ff) | (data << 8);
}
void simpsons_state::video_bank_select( int bank )
void simpsons_state::video_bank_select(int bank)
{
if(bank & 1)
if (BIT(bank, 0))
m_palette_view.select(0);
else
m_palette_view.disable();
m_video_view.select((bank >> 1) & 1);
m_video_view.select(BIT(bank, 1));
}
@ -283,12 +290,12 @@ uint32_t simpsons_state::screen_update(screen_device &screen, bitmap_ind16 &bitm
{
// update color info and refresh tilemaps
static const int K053251_CI[3] = { k053251_device::CI2, k053251_device::CI3, k053251_device::CI4 };
int bg_colorbase = m_k053251->get_palette_index(k053251_device::CI0);
int const bg_colorbase = m_k053251->get_palette_index(k053251_device::CI0);
m_sprite_colorbase = m_k053251->get_palette_index(k053251_device::CI1);
for (int i = 0; i < 3; i++)
{
int prev_colorbase = m_layer_colorbase[i];
int const prev_colorbase = m_layer_colorbase[i];
m_layer_colorbase[i] = m_k053251->get_palette_index(K053251_CI[i]);
if (m_layer_colorbase[i] != prev_colorbase)
@ -298,7 +305,7 @@ uint32_t simpsons_state::screen_update(screen_device &screen, bitmap_ind16 &bitm
m_k052109->tilemap_update();
// sort layers and draw
int layer[3];
int layer[3]{};
for (int i = 0; i < 3; i++)
{
layer[i] = i;
@ -330,11 +337,11 @@ void simpsons_state::eeprom_w(uint8_t data)
if (data == 0xff)
return;
ioport("EEPROMOUT")->write(data, 0xff);
m_io_eepromout->write(data, 0xff);
video_bank_select(data & 0x03);
m_firq_enabled = data & 0x04;
m_firq_enabled = BIT(data, 2);
if (!m_firq_enabled)
m_maincpu->set_input_line(KONAMI_FIRQ_LINE, CLEAR_LINE);
}
@ -348,14 +355,14 @@ void simpsons_state::eeprom_w(uint8_t data)
void simpsons_state::coin_counter_w(uint8_t data)
{
/* bit 0,1 coin counters */
machine().bookkeeping().coin_counter_w(0, data & 0x01);
machine().bookkeeping().coin_counter_w(1, data & 0x02);
machine().bookkeeping().coin_counter_w(0, BIT(data, 0));
machine().bookkeeping().coin_counter_w(1, BIT(data, 1));
/* bit 2 selects mono or stereo sound */
/* bit 3 = enable char ROM reading through the video RAM */
m_k052109->set_rmrd_line((data & 0x08) ? ASSERT_LINE : CLEAR_LINE);
m_k052109->set_rmrd_line(BIT(data, 3));
/* bit 4 = INIT (unknown) */
/* bit 5 = enable sprite ROM reading */
m_k053246->k053246_set_objcha_line((~data & 0x20) ? ASSERT_LINE : CLEAR_LINE);
m_k053246->k053246_set_objcha_line(BIT(~data, 5));
}
uint8_t simpsons_state::sound_interrupt_r()
@ -375,17 +382,17 @@ uint8_t simpsons_state::sound_interrupt_r()
void simpsons_state::banking_callback(u8 data)
{
membank("bank1")->set_entry(data & 0x3f);
m_mainbank->set_entry(data & 0x3f);
}
void simpsons_state::machine_start()
{
m_spriteram = make_unique_clear<uint16_t[]>(0x1000 / 2);
membank("bank1")->configure_entries(0, 64, memregion("maincpu")->base(), 0x2000);
m_mainbank->configure_entries(0, 64, memregion("maincpu")->base(), 0x2000);
membank("bank2")->configure_entries(0, 2, memregion("audiocpu")->base() + 0x10000, 0);
membank("bank2")->configure_entries(2, 6, memregion("audiocpu")->base() + 0x10000, 0x4000);
m_audiobank->configure_entries(0, 2, memregion("audiocpu")->base() + 0x10000, 0);
m_audiobank->configure_entries(2, 6, memregion("audiocpu")->base() + 0x10000, 0x4000);
save_item(NAME(m_firq_enabled));
save_item(NAME(m_sprite_colorbase));
@ -407,11 +414,11 @@ void simpsons_state::machine_reset()
}
m_sprite_colorbase = 0;
m_firq_enabled = 0;
m_firq_enabled = false;
/* init the default banks */
membank("bank1")->set_entry(0);
membank("bank2")->set_entry(0);
m_mainbank->set_entry(0);
m_audiobank->set_entry(0);
video_bank_select(0);
m_dma_start_timer->adjust(attotime::never);
@ -454,13 +461,13 @@ void simpsons_state::main_map(address_map &map)
m_video_view[1](0x2000, 0x2fff).rw(FUNC(simpsons_state::k053247_r), FUNC(simpsons_state::k053247_w));
m_video_view[1](0x3000, 0x3fff).ram();
map(0x4000, 0x5fff).ram();
map(0x6000, 0x7fff).bankr("bank1");
map(0x6000, 0x7fff).bankr(m_mainbank);
map(0x8000, 0xffff).rom().region("maincpu", 0x78000);
}
void simpsons_state::z80_bankswitch_w(uint8_t data)
{
membank("bank2")->set_entry(data & 7);
m_audiobank->set_entry(data & 7);
}
@ -499,7 +506,7 @@ void simpsons_state::z80_nmi_w(int state)
void simpsons_state::z80_map(address_map &map)
{
map(0x0000, 0x7fff).rom();
map(0x8000, 0xbfff).bankr("bank2");
map(0x8000, 0xbfff).bankr(m_audiobank);
map(0xf000, 0xf7ff).ram();
map(0xf800, 0xf801).rw("ymsnd", FUNC(ym2151_device::read), FUNC(ym2151_device::write));
map(0xfa00, 0xfa00).w(FUNC(simpsons_state::z80_arm_nmi_w));
@ -593,26 +600,27 @@ INPUT_PORTS_END
void simpsons_state::object_dma()
{
int counter, num_inactive;
uint16_t *src, *dst;
uint16_t *dst;
m_k053246->k053247_get_ram(&dst);
src = m_spriteram.get();
num_inactive = counter = 256;
uint16_t const *src = m_spriteram.get();
int num_inactive = 256;
do {
if ((*src & 0x8000) && (*src & 0xff))
for (int counter = 256; counter; --counter)
{
if (BIT(*src, 15) && (*src & 0xff))
{
memcpy(dst, src, 0x10);
dst += 8;
dst = std::copy_n(src, 8, dst);
num_inactive--;
}
src += 8;
}
while (--counter);
if (num_inactive) do { *dst = 0; dst += 8; } while (--num_inactive);
while (num_inactive--)
{
*dst = 0;
dst += 8;
}
}
INTERRUPT_GEN_MEMBER(simpsons_state::periodic_irq)