1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
// license:BSD-3-Clause
// copyright-holders:Wilbert Pol
/**********************************************************************************
When backing up the SRAM from an FM-PAC the file seems to be prefixed
with: PAC2 BACKUP DATA. We only store the raw sram contents.
**********************************************************************************/
#include "emu.h"
#include "fmpac.h"
#include "sound/ymopl.h"
#include "speaker.h"
namespace {
class msx_cart_fmpac_device : public device_t, public msx_cart_interface
{
public:
msx_cart_fmpac_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
: device_t(mconfig, MSX_CART_FMPAC, tag, owner, clock)
, msx_cart_interface(mconfig, *this)
, m_ym2413(*this, "ym2413")
, m_rombank(*this, "rombank")
, m_view(*this, "view")
, m_sram_active(false)
, m_opll_active(false)
, m_sram_unlock{0, 0}
, m_control(0)
{ }
virtual std::error_condition initialize_cartridge(std::string &message) override;
protected:
virtual void device_start() override ATTR_COLD;
virtual void device_reset() override ATTR_COLD;
// device_t implementation
virtual void device_add_mconfig(machine_config &config) override ATTR_COLD;
private:
void write_ym2413(offs_t offset, u8 data);
void sram_unlock(offs_t offset, u8 data);
u8 control_r();
void control_w(u8 data);
u8 bank_r();
void bank_w(u8 data);
required_device<ym2413_device> m_ym2413;
memory_bank_creator m_rombank;
memory_view m_view;
bool m_sram_active;
bool m_opll_active;
u8 m_sram_unlock[2];
u8 m_control;
};
void msx_cart_fmpac_device::device_add_mconfig(machine_config &config)
{
YM2413(config, m_ym2413, DERIVED_CLOCK(1, 1));
if (parent_slot())
m_ym2413->add_route(ALL_OUTPUTS, soundin(), 0.8);
}
void msx_cart_fmpac_device::device_start()
{
save_item(NAME(m_sram_active));
save_item(NAME(m_opll_active));
save_item(NAME(m_sram_unlock));
save_item(NAME(m_control));
// Install IO read/write handlers
io_space().install_write_handler(0x7c, 0x7d, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::write_ym2413)));
}
void msx_cart_fmpac_device::device_reset()
{
m_sram_active = false;
m_opll_active = false;
m_sram_unlock[0] = 0;
m_sram_unlock[1] = 0;
m_control = 0;
m_view.select(0);
m_rombank->set_entry(0);
}
std::error_condition msx_cart_fmpac_device::initialize_cartridge(std::string &message)
{
if (!cart_rom_region())
{
message = "msx_cart_fmpac_device: Required region 'rom' was not found.";
return image_error::INTERNAL;
}
if (!cart_sram_region())
{
message = "msx_cart_fmpac_device: Required region 'sram' was not found.";
return image_error::INTERNAL;
}
if (cart_rom_region()->bytes() != 0x10000)
{
message = "msx_cart_fmpac_device: Region 'rom' has unsupported size.";
return image_error::INVALIDLENGTH;
}
if (cart_sram_region()->bytes() < 0x2000)
{
message = "msx_cart_fmpac_device: Region 'sram' has unsupported size.";
return image_error::BADSOFTWARE;
}
m_rombank->configure_entries(0, 4, cart_rom_region()->base(), 0x4000);
page(1)->install_view(0x4000, 0x7fff, m_view);
m_view[0].install_read_bank(0x4000, 0x7fff, m_rombank);
m_view[0].install_write_handler(0x5ffe, 0x5fff, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::sram_unlock)));
m_view[0].install_write_handler(0x7ff4, 0x7ff5, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::write_ym2413)));
m_view[0].install_read_handler(0x7ff6, 0x7ff6, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::control_r)));
m_view[0].install_write_handler(0x7ff6, 0x7ff6, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::control_w)));
m_view[0].install_read_handler(0x7ff7, 0x7ff7, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::bank_r)));
m_view[0].install_write_handler(0x7ff7, 0x7ff7, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::bank_w)));
m_view[1].install_ram(0x4000, 0x5fff, cart_sram_region()->base());
m_view[1].install_write_handler(0x5ffe, 0x5fff, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::sram_unlock)));
m_view[1].install_write_handler(0x7ff4, 0x7ff5, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::write_ym2413)));
m_view[1].install_read_handler(0x7ff6, 0x7ff6, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::control_r)));
m_view[1].install_write_handler(0x7ff6, 0x7ff6, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::control_w)));
m_view[1].install_read_handler(0x7ff7, 0x7ff7, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::bank_r)));
m_view[1].install_write_handler(0x7ff7, 0x7ff7, emu::rw_delegate(*this, FUNC(msx_cart_fmpac_device::bank_w)));
return std::error_condition();
}
void msx_cart_fmpac_device::sram_unlock(offs_t offset, u8 data)
{
m_sram_unlock[offset] = data;
m_sram_active = m_sram_unlock[0] == 0x4d && m_sram_unlock[1] == 0x69;
m_view.select(m_sram_active ? 1 : 0);
}
u8 msx_cart_fmpac_device::control_r()
{
return m_control;
}
void msx_cart_fmpac_device::control_w(u8 data)
{
m_control = data & 0x11;
m_opll_active = BIT(data, 0);
}
u8 msx_cart_fmpac_device::bank_r()
{
return m_rombank->entry();
}
void msx_cart_fmpac_device::bank_w(u8 data)
{
m_rombank->set_entry(data);
}
void msx_cart_fmpac_device::write_ym2413(offs_t offset, u8 data)
{
if (m_opll_active)
{
m_ym2413->write(offset & 1, data);
}
}
} // anonymous namespace
DEFINE_DEVICE_TYPE_PRIVATE(MSX_CART_FMPAC, msx_cart_interface, msx_cart_fmpac_device, "msx_cart_fmpac", "MSX Cartridge - FM-PAC")
|