summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/dirom.cpp
blob: 6440fa2cfc6d9f71a06bd0e09fecfe142474ed6d (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
274
275
276
277
278
279
280
281
282
283
284
285
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert

#include "emu.h"


device_rom_interface::device_rom_interface(const machine_config &mconfig, device_t &device, u8 addrwidth, endianness_t endian, u8 datawidth) :
	device_memory_interface(mconfig, device),
	m_rom_tag(device.basetag()),
	m_rom_config("rom", endian, datawidth, addrwidth),
	m_bank(nullptr),
	m_cur_bank(-1)
{
}

device_rom_interface::~device_rom_interface()
{
}

device_memory_interface::space_config_vector device_rom_interface::memory_space_config() const
{
	return space_config_vector {
		std::make_pair(0, &m_rom_config)
	};
}

void device_rom_interface::rom_bank_updated()
{
}

void device_rom_interface::set_rom_bank(int bank)
{
	if(!m_bank)
		emu_fatalerror("%s: device_rom_interface::set_rom_bank called without banking setup", device().tag());

	if(bank >= m_bank_count) {
		device().logerror("Warning: requested bank %x higher than actual bank count %x\n", bank, m_bank_count);
		bank = bank % m_bank_count;
	}

	if (m_cur_bank != bank) {
		m_cur_bank = bank;
		m_bank->set_entry(bank);
		rom_bank_updated();
	}
}

void device_rom_interface::interface_post_load()
{
	if(m_bank)
		m_bank->set_entry(m_cur_bank);
}

void device_rom_interface::set_rom(const void *base, u32 size)
{
	u32 mend = m_rom_config.addr_width() == 32 ? 0xffffffff : (1 << m_rom_config.addr_width()) - 1;
	u32 rend = size-1;
	m_bank_count = mend == 0xffffffff ? 1 : (rend+1) / (mend+1);
	if(m_bank_count < 1)
		m_bank_count = 1;

	if(rend >= mend) {
		space().install_read_bank(0, mend, device().tag());
		m_bank = device().machine().memory().banks().find(device().tag())->second.get();
		m_bank->configure_entries(0, m_bank_count, const_cast<void *>(base), mend+1);
		m_cur_bank = 0;

	} else {
		// Round up to the nearest power-of-two-minus-one
		u32 rmask = rend;
		rmask |= rmask >> 1;
		rmask |= rmask >> 2;
		rmask |= rmask >> 4;
		rmask |= rmask >> 8;
		rmask |= rmask >> 16;
		if(rmask != rend)
			space().unmap_read(0, mend);
		// Mirror over the high bits.  mend and rmask are both
		// powers-of-two-minus-one, so the xor works
		space().install_rom(0, rend, mend ^ rmask, const_cast<void *>(base));
	}
}

void device_rom_interface::interface_pre_start()
{
	if(!has_space(0))
		return;

	switch(space().data_width()) {
	case  8:
		if(space().endianness() == ENDIANNESS_LITTLE) {
			auto cache = space().cache<0, 0, ENDIANNESS_LITTLE>();
			m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
			m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
			m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
			m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
		} else {
			auto cache = space().cache<0, 0, ENDIANNESS_BIG>();
			m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
			m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
			m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
			m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
		}
		break;

	case 16:
		switch(space().addr_shift()) {
		case  3:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<1, 3, ENDIANNESS_LITTLE>();
				m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<1, 3, ENDIANNESS_BIG>();
				m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		case  0:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<1, 0, ENDIANNESS_LITTLE>();
				m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<1, 0, ENDIANNESS_BIG>();
				m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		case -1:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<1, -1, ENDIANNESS_LITTLE>();
				m_r8  = nullptr;
				m_r16 = nullptr;
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<1, -1, ENDIANNESS_BIG>();
				m_r8  = nullptr;
				m_r16 = nullptr;
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		}
		break;

	case 32:
		switch(space().addr_shift()) {
		case  0:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<2, 0, ENDIANNESS_LITTLE>();
				m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<2, 0, ENDIANNESS_BIG>();
				m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		case -1:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<2, -1, ENDIANNESS_LITTLE>();
				m_r8  = nullptr;
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<2, -1, ENDIANNESS_BIG>();
				m_r8  = nullptr;
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		case -2:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<2, -2, ENDIANNESS_LITTLE>();
				m_r8  = nullptr;
				m_r16 = nullptr;
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<2, -2, ENDIANNESS_BIG>();
				m_r8  = nullptr;
				m_r16 = nullptr;
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		}
		break;

	case 64:
		switch(space().addr_shift()) {
		case  0:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<3, 0, ENDIANNESS_LITTLE>();
				m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<3, 0, ENDIANNESS_BIG>();
				m_r8  = [cache] (offs_t byteaddress) -> u8  { return cache->read_byte(byteaddress); };
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		case -1:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<3, -1, ENDIANNESS_LITTLE>();
				m_r8  = nullptr;
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<3, -1, ENDIANNESS_BIG>();
				m_r8  = nullptr;
				m_r16 = [cache] (offs_t byteaddress) -> u16 { return cache->read_word(byteaddress); };
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		case -2:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<3, -2, ENDIANNESS_LITTLE>();
				m_r8  = nullptr;
				m_r16 = nullptr;
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<3, -2, ENDIANNESS_BIG>();
				m_r8  = nullptr;
				m_r16 = nullptr;
				m_r32 = [cache] (offs_t byteaddress) -> u32 { return cache->read_dword(byteaddress); };
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		case -3:
			if(space().endianness() == ENDIANNESS_LITTLE) {
				auto cache = space().cache<3, -3, ENDIANNESS_LITTLE>();
				m_r8  = nullptr;
				m_r16 = nullptr;
				m_r32 = nullptr;
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			} else {
				auto cache = space().cache<3, -3, ENDIANNESS_BIG>();
				m_r8  = nullptr;
				m_r16 = nullptr;
				m_r32 = nullptr;
				m_r64 = [cache] (offs_t byteaddress) -> u64 { return cache->read_qword(byteaddress); };
			}
			break;
		}
		break;
	}

	device().save_item(NAME(m_cur_bank));
	device().save_item(NAME(m_bank_count));

	if(!has_configured_map(0)) {
		memory_region *reg = device().owner()->memregion(m_rom_tag);
		if(reg)
			set_rom(reg->base(), reg->bytes());
		else {
			device().logerror("ROM region '%s' not found\n", m_rom_tag);
			u32 end = m_rom_config.addr_width() == 32 ? 0xffffffff : (1 << m_rom_config.addr_width()) - 1;
			space().unmap_read(0, end);
		}
	}
}