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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
// license: BSD-3-Clause
// copyright-holders: Angelo Salese
#include "emu.h"
#include "slot.h"
#include "options.h"
DEFINE_DEVICE_TYPE(MEGADRIVE_CART_SLOT, megadrive_cart_slot_device, "megadrive_cart_slot", "MegaDrive Cartridge Slot")
device_megadrive_cart_interface::device_megadrive_cart_interface(const machine_config &mconfig, device_t &device)
: device_interface(device, "megadrive_cart")
, m_slot(dynamic_cast<megadrive_cart_slot_device *>(device.owner()))
{
}
device_megadrive_cart_interface::~device_megadrive_cart_interface()
{
}
void device_megadrive_cart_interface::interface_pre_start()
{
if (!m_slot->started())
throw device_missing_dependencies();
m_cold_reset = true;
}
void device_megadrive_cart_interface::interface_post_start()
{
}
void device_megadrive_cart_interface::interface_pre_reset()
{
// FIXME: interface_post_start is too early for loose cart
// unmapping won't avoid the "Fatal error: A memory_view can be present in only one address map." for some carts
if (m_cold_reset)
{
m_slot->m_space_mem->install_device(0x000000, 0xdfffff, *this, &device_megadrive_cart_interface::cart_map);
m_slot->m_space_io->install_device(0x00, 0xff, *this, &device_megadrive_cart_interface::time_io_map);
m_cold_reset = false;
}
}
void device_megadrive_cart_interface::cart_map(address_map &map)
{
// NOTE: is host responsibility to handle open bus access
// map(0x000000, 0x3fffff).unmaprw();
}
// $a13000 base
void device_megadrive_cart_interface::time_io_map(address_map &map)
{
// map(0x00, 0xff).unmaprw();
}
//void device_megadrive_cart_interface::rom_alloc(size_t size)
//{
// if (m_rom == nullptr)
// {
// m_rom = (uint16_t *)device().machine().memory().region_alloc(device().subtag("^cart:rom"), size, 2, ENDIANNESS_BIG)->base();
// m_rom_size = size;
// m_rom_mask = size - 1;
// }
//}
megadrive_cart_slot_device::megadrive_cart_slot_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
: device_t(mconfig, MEGADRIVE_CART_SLOT, tag, owner, clock)
, device_memory_interface(mconfig, *this)
, device_cartrom_image_interface(mconfig, *this)
, device_single_card_slot_interface<device_megadrive_cart_interface>(mconfig, *this)
, m_cart(nullptr)
, m_space_mem_config("cart_mem", ENDIANNESS_BIG, 16, 24, 0, address_map_constructor())
, m_space_io_config("time_io", ENDIANNESS_BIG, 16, 8, 0, address_map_constructor())
, m_vres_cb(*this)
{
}
megadrive_cart_slot_device::~megadrive_cart_slot_device()
{
}
device_memory_interface::space_config_vector megadrive_cart_slot_device::memory_space_config() const
{
return space_config_vector{
std::make_pair(AS_PROGRAM, &m_space_mem_config),
std::make_pair(AS_IO, &m_space_io_config)
};
}
void megadrive_cart_slot_device::device_start()
{
m_cart = get_card_device();
m_space_mem = &space(AS_PROGRAM);
m_space_io = &space(AS_IO);
// NOTE: this is (sortof) duct tape until we implement /CART pin in host drivers properly
// teradrive TMSS wants to read high on no cart presence otherwise refuses to boot PC side
m_space_mem->unmap_value_high();
//m_space_io->unmap_value_high();
}
const char *megadrive_cart_slot_device::get_cart_type(const uint8_t *ROM, uint32_t len)
{
using namespace bus::megadrive;
// int type = slotoptions::MD_STD;
// TODO: not caring about loose detection for now
return slotoptions::MD_STD;
}
//static int md_get_pcb_id(const char *slot)
//{
// for (auto & elem : slot_list)
// {
// if (!strcmp(elem.slot_option, slot))
// return elem.pcb_id;
// }
//
// return MD_STD;
//}
//static const char *md_get_slot(int type)
//{
// for (auto & elem : slot_list)
// {
// if (elem.pcb_id == type)
// return elem.slot_option;
// }
//
// return "rom";
//}
std::pair<std::error_condition, std::string> megadrive_cart_slot_device::call_load()
{
if (m_cart)
{
std::error_condition err;
if (loaded_through_softlist())
{
err = load_swlist();
}
else
{
err = load_loose(image_core_file());
}
if (err)
return std::make_pair(err ? err : std::errc::io_error, "Error loading detection");
}
return std::make_pair(std::error_condition(), std::string());
}
std::error_condition megadrive_cart_slot_device::load_swlist()
{
using namespace bus::megadrive;
const char *slot_name;
slot_name = get_feature("slot");
if (slot_name == nullptr)
slot_name = slotoptions::MD_STD;
// printf("%s\n", slot_name);
return std::error_condition(m_cart->load());
}
std::error_condition megadrive_cart_slot_device::load_loose(util::random_read &file)
{
auto const len = length();
if (len)
{
logerror("Allocating %lu byte cartridge ROM region\n", len);
memory_region *const romregion = machine().memory().region_alloc(subtag("rom"), len, 2, ENDIANNESS_BIG);
u16 *const rombase = reinterpret_cast<u16 *>(romregion->base());
auto const cnt = fread(rombase, len);
if (cnt != len)
return std::error_condition(std::errc::io_error);
if (ENDIANNESS_NATIVE != ENDIANNESS_BIG)
{
for (u32 i = 0; (len / 2) > i; ++i)
rombase[i] = swapendian_int16(rombase[i]);
}
}
return std::error_condition(m_cart->load());
}
std::string megadrive_cart_slot_device::get_default_card_software(get_default_card_software_hook &hook) const
{
using namespace bus::megadrive;
if (hook.image_file())
{
uint64_t len;
if (hook.image_file()->length(len))
{
osd_printf_warning("[%s] Error getting cartridge ROM length - defaulting to linear ROM type\n", tag());
return slotoptions::MD_STD;
}
std::vector<uint8_t> rom(len);
// FIXME: check error return or read returning short
util::read(*hook.image_file(), &rom[0], len);
uint32_t const offset = 0; // genesis_is_SMD(&rom[0x200], len - 0x200) ? 0x200 : 0;
// int const type = get_cart_type(&rom[offset], len - offset);
char const *const slot_string = get_cart_type(&rom[offset], len - offset);
return std::string(slot_string);
}
else
{
return software_get_default_slot(slotoptions::MD_STD);
}
}
void megadrive_cart_slot_device::call_unload()
{
if (m_cart)
m_cart->unload();
// if (m_cart && m_cart->get_nvram_base() && m_cart->get_nvram_size())
// battery_save(m_cart->get_nvram_base(), m_cart->get_nvram_size());
}
// 0x000000-0x3fffff
// shift << 1 here and not in memory constructor for two reasons:
// 1. cart has no A0 connected
// 2. memory_views would get confused by the shift with
// "A memory_view must be installed at its configuration address."
u16 megadrive_cart_slot_device::base_r(offs_t offset, u16 mem_mask)
{
return m_space_mem->read_word(offset << 1, mem_mask);
}
void megadrive_cart_slot_device::base_w(offs_t offset, u16 data, u16 mem_mask)
{
m_space_mem->write_word(offset << 1, data, mem_mask);
}
u16 megadrive_cart_slot_device::time_r(offs_t offset, u16 mem_mask)
{
return m_space_io->read_word(offset << 1, mem_mask);
}
void megadrive_cart_slot_device::time_w(offs_t offset, u16 data, u16 mem_mask)
{
m_space_io->write_word(offset << 1, data, mem_mask);
}
|