summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/saturn/sat_slot.cpp
blob: 13f69c8ba92ff437ebb124f355a3b82805043a21 (plain) (blame)
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
263
264
265
266
267
268
269
270
271
272
273
// license:BSD-3-Clause
// copyright-holders:Fabio Priuli
/***********************************************************************************************************


    Saturn cart emulation
    (through slot devices)

    Despite the system having a single cart slot, 3 different kinds of cart can be inserted and
    different memory areas are exposed to each of them
    * ROM carts are accessed in range 0x02000000-0x023fffff and 0x22000000-0x24ffffff of both CPUs
    * Data RAM carts are accessed in range 0x02400000-0x027fffff of both CPUs (each DRAM chip is
      mapped independently, the 1st at 0x2400000, the second at 0x2600000)
    * Battery RAM carts are accessed in range 0x04000000-0x047fffff of both CPUs

    It is not clear what happens to accesses beyond the cart size (open bus? mirror of cart data?),
    e.g. if you have a 16Mbit battery cart inserted and the system tries to read/write above 0x04400000,
    so for the moment the whole range is mapped and an error message is printed for out-of-bounds
    accesses


 ***********************************************************************************************************/


#include "emu.h"
#include "sat_slot.h"

//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

DEFINE_DEVICE_TYPE(SATURN_CART_SLOT, sat_cart_slot_device, "sat_cart_slot", "Saturn Cartridge Slot")


//-------------------------------------------------
//  device_sat_cart_interface - constructor
//-------------------------------------------------

device_sat_cart_interface::device_sat_cart_interface(const machine_config &mconfig, device_t &device, int cart_type) :
	device_slot_card_interface(mconfig, device),
	m_cart_type(cart_type),
	m_rom(nullptr),
	m_rom_size(0)
{
}


//-------------------------------------------------
//  ~device_sat_cart_interface - destructor
//-------------------------------------------------

device_sat_cart_interface::~device_sat_cart_interface()
{
}

//-------------------------------------------------
//  rom_alloc - alloc the space for the cart
//-------------------------------------------------

void device_sat_cart_interface::rom_alloc(uint32_t size, const char *tag)
{
	if (m_rom == nullptr)
	{
		m_rom = (uint32_t *)device().machine().memory().region_alloc(std::string(tag).append(SATSLOT_ROM_REGION_TAG).c_str(), size, 4, ENDIANNESS_LITTLE)->base();
		m_rom_size = size;
	}
}


//-------------------------------------------------
//  bram_alloc - alloc the space for the Backup RAM
//-------------------------------------------------

void device_sat_cart_interface::bram_alloc(uint32_t size)
{
	m_ext_bram.resize(size);
	device().save_item(NAME(m_ext_bram));
}


//-------------------------------------------------
//  dram*_alloc - alloc the space for the DRAM
//-------------------------------------------------

void device_sat_cart_interface::dram0_alloc(uint32_t size)
{
	m_ext_dram0.resize(size/sizeof(uint32_t));
	device().save_item(NAME(m_ext_dram0));
}

void device_sat_cart_interface::dram1_alloc(uint32_t size)
{
	m_ext_dram1.resize(size/sizeof(uint32_t));
	device().save_item(NAME(m_ext_dram1));
}


//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  sat_cart_slot_device - constructor
//-------------------------------------------------
sat_cart_slot_device::sat_cart_slot_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	device_t(mconfig, SATURN_CART_SLOT, tag, owner, clock),
	device_image_interface(mconfig, *this),
	device_slot_interface(mconfig, *this), m_cart(nullptr)
{
}


//-------------------------------------------------
//  sat_cart_slot_device - destructor
//-------------------------------------------------

sat_cart_slot_device::~sat_cart_slot_device()
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void sat_cart_slot_device::device_start()
{
	m_cart = dynamic_cast<device_sat_cart_interface *>(get_card_device());
}



/*-------------------------------------------------
 call load
 -------------------------------------------------*/


image_init_result sat_cart_slot_device::call_load()
{
	if (m_cart)
	{
		bool is_rom = (!loaded_through_softlist() || (loaded_through_softlist() && get_software_region("rom")));

		if (is_rom)
		{
			// from fullpath, only ROM carts
			uint32_t len = loaded_through_softlist() ? get_software_region_length("rom") : length();
			uint32_t *ROM;

			m_cart->rom_alloc(len, tag());
			ROM = m_cart->get_rom_base();

			if (loaded_through_softlist())
				memcpy(ROM, get_software_region("rom"), len);
			else
				fread(ROM, len);

			// fix endianness....
			for (int i = 0; i < len/4; i ++)
				ROM[i] = BITSWAP32(ROM[i],7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8,23,22,21,20,19,18,17,16,31,30,29,28,27,26,25,24);
//          {
//              uint8_t tempa = ROM[i+0];
//              uint8_t tempb = ROM[i+1];
//              ROM[i+1] = ROM[i+2];
//              ROM[i+0] = ROM[i+3];
//              ROM[i+3] = tempa;
//              ROM[i+2] = tempb;
//          }
		}
		else
		{
			// DRAM or BRAM carts from softlist
			if (get_software_region("bram"))
				m_cart->bram_alloc(get_software_region_length("bram"));
			if (get_software_region("dram0"))
				m_cart->dram0_alloc(get_software_region_length("dram0"));
			if (get_software_region("dram1"))
				m_cart->dram1_alloc(get_software_region_length("dram1"));
		}
		return image_init_result::PASS;
	}

	return image_init_result::PASS;
}


/*-------------------------------------------------
 call_unload
 -------------------------------------------------*/

void sat_cart_slot_device::call_unload()
{
}



/*-------------------------------------------------
 get default card software
 -------------------------------------------------*/

std::string sat_cart_slot_device::get_default_card_software(get_default_card_software_hook &hook) const
{
	return software_get_default_slot("rom");
}



int sat_cart_slot_device::get_cart_type()
{
	if (m_cart)
		return m_cart->get_cart_type();

	return 0xff;
}



/*-------------------------------------------------
 read
 -------------------------------------------------*/

READ32_MEMBER(sat_cart_slot_device::read_rom)
{
	if (m_cart)
		return m_cart->read_rom(space, offset, mem_mask);
	else
		return 0xffffffff;
}

READ32_MEMBER(sat_cart_slot_device::read_ext_dram0)
{
	if (m_cart)
		return m_cart->read_ext_dram0(space, offset, mem_mask);
	else
		return 0xffffffff;
}

READ32_MEMBER(sat_cart_slot_device::read_ext_dram1)
{
	if (m_cart)
		return m_cart->read_ext_dram1(space, offset, mem_mask);
	else
		return 0xffffffff;
}

READ32_MEMBER(sat_cart_slot_device::read_ext_bram)
{
	if (m_cart)
		return m_cart->read_ext_bram(space, offset, mem_mask);
	else
		return 0xffffffff;
}

/*-------------------------------------------------
 write
 -------------------------------------------------*/

WRITE32_MEMBER(sat_cart_slot_device::write_ext_dram0)
{
	if (m_cart)
		m_cart->write_ext_dram0(space, offset, data, mem_mask);
}

WRITE32_MEMBER(sat_cart_slot_device::write_ext_dram1)
{
	if (m_cart)
		m_cart->write_ext_dram1(space, offset, data, mem_mask);
}

WRITE32_MEMBER(sat_cart_slot_device::write_ext_bram)
{
	if (m_cart)
		m_cart->write_ext_bram(space, offset, data, mem_mask);
}