summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/divtlb.cpp
blob: df88067692731434f0b88e61d27b84a2c3b98563 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    divtlb.cpp

    Device generic virtual TLB interface.

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

#include "emu.h"
#include "divtlb.h"



//**************************************************************************
//  DEBUGGING
//**************************************************************************

#define PRINTF_TLB          (0)



//**************************************************************************
//  DEVICE VTLB INTERFACE
//**************************************************************************

//-------------------------------------------------
//  device_vtlb_interface - constructor
//-------------------------------------------------

device_vtlb_interface::device_vtlb_interface(const machine_config &mconfig, device_t &device, int space)
	: device_interface(device, "vtlb"),
		m_space(space),
		m_dynamic(0),
		m_fixed(0),
		m_dynindex(0),
		m_pageshift(0),
		m_addrwidth(0),
		m_table_base(nullptr)
{
}


//-------------------------------------------------
//  device_vtlb_interface - destructor
//-------------------------------------------------

device_vtlb_interface::~device_vtlb_interface()
{
}


//-------------------------------------------------
//  interface_validity_check - validation for a
//  device after the configuration has been
//  constructed
//-------------------------------------------------

void device_vtlb_interface::interface_validity_check(validity_checker &valid) const
{
	const device_memory_interface *intf;
	if (!device().interface(intf))
		osd_printf_error("Device does not have memory interface\n");
	else
	{
		// validate CPU information
		const address_space_config *spaceconfig = intf->space_config(m_space);
		if (spaceconfig == nullptr)
			osd_printf_error("No memory address space configuration found for space %d\n", m_space);
		else if ((1 << spaceconfig->page_shift()) <= VTLB_FLAGS_MASK || spaceconfig->logaddr_width() <= spaceconfig->page_shift())
			osd_printf_error("Invalid page shift %d for VTLB\n", spaceconfig->page_shift());
	}
}


//-------------------------------------------------
//  interface_pre_start - work to be done prior to
//  actually starting a device
//-------------------------------------------------

void device_vtlb_interface::interface_pre_start()
{
	// fill in CPU information
	const address_space_config *spaceconfig = device().memory().space_config(m_space);
	m_pageshift = spaceconfig->page_shift();
	m_addrwidth = spaceconfig->logaddr_width();

	// allocate the entry array
	m_live.resize(m_fixed + m_dynamic);
	memset(&m_live[0], 0, m_live.size()*sizeof(m_live[0]));

	// allocate the lookup table
	m_table.resize((size_t)1 << (m_addrwidth - m_pageshift));
	memset(&m_table[0], 0, m_table.size() * sizeof(m_table[0]));
	m_refcnt.resize((size_t)1 << (m_addrwidth - m_pageshift));
	memset(&m_refcnt[0], 0, m_refcnt.size() * sizeof(m_refcnt[0]));
	// pointer to first element for quick access
	m_table_base = &m_table[0];

	// allocate the fixed page count array
	if (m_fixed > 0)
	{
		m_fixedpages.resize(m_fixed);
		memset(&m_fixedpages[0], 0, m_fixed*sizeof(m_fixedpages[0]));
	}
}


//-------------------------------------------------
//  interface_post_start - work to be done after
//  actually starting a device
//-------------------------------------------------

void device_vtlb_interface::interface_register_save(save_registrar &save)
{
	save_registrar container(save, "vtlb");

	container.reg(NAME(m_live))
		.reg(NAME(m_table))
		.reg(NAME(m_refcnt));
	if (m_fixed > 0)
		container.reg(NAME(m_fixedpages));
}


//-------------------------------------------------
//  interface_pre_reset - work to be done prior to
//  actually resetting a device
//-------------------------------------------------

void device_vtlb_interface::interface_pre_reset()
{
	vtlb_flush_dynamic();
}


//**************************************************************************
//  FILLING
//**************************************************************************

//-------------------------------------------------
//  vtlb_fill - called by the CPU core in
//  response to an unmapped access
//-------------------------------------------------

bool device_vtlb_interface::vtlb_fill(offs_t address, int intention)
{
	offs_t tableindex = address >> m_pageshift;
	vtlb_entry entry = m_table[tableindex];
	offs_t taddress;

#if PRINTF_TLB
	osd_printf_debug("vtlb_fill: %08X(%X) ... ", address, intention);
#endif

	// should not be called here if the entry is in the table already
//  assert((entry & (1 << intention)) == 0);

	// if we have no dynamic entries, we always fail
	if (m_dynamic == 0)
	{
#if PRINTF_TLB
		osd_printf_debug("failed: no dynamic entries\n");
#endif
		return false;
	}

	// ask the CPU core to translate for us
	taddress = address;
	if (!device().memory().translate(m_space, intention, taddress))
	{
#if PRINTF_TLB
		osd_printf_debug("failed: no translation\n");
#endif
		return false;
	}

	// if this is the first successful translation for this address, allocate a new entry
	if ((entry & VTLB_FLAGS_MASK) == 0)
	{
		int liveindex = m_dynindex++ % m_dynamic;


		// if an entry already exists at this index, free it
		if (m_live[liveindex] != 0)
		{
			if (m_refcnt[m_live[liveindex] - 1] <= 1)
				m_table[m_live[liveindex] - 1] = 0;
			else
				m_refcnt[m_live[liveindex] - 1]--;
		}


		// claim this new entry
		m_live[liveindex] = tableindex + 1;

		// form a new blank entry
		entry = (taddress >> m_pageshift) << m_pageshift;
		entry |= VTLB_FLAG_VALID;

#if PRINTF_TLB
		osd_printf_debug("success (%08X), new entry\n", taddress);
#endif
	}

	// otherwise, ensure that different intentions do not produce different addresses
	else
	{
		assert((entry >> m_pageshift) == (taddress >> m_pageshift));
		assert(entry & VTLB_FLAG_VALID);

#if PRINTF_TLB
		osd_printf_debug("success (%08X), existing entry\n", taddress);
#endif
	}

	// add the intention to the list of valid intentions and store
	entry |= 1 << (intention & (TRANSLATE_TYPE_MASK | TRANSLATE_USER_MASK));
	m_table[tableindex] = entry;
	return true;
}


//-------------------------------------------------
//  vtlb_load - load a fixed VTLB entry
//-------------------------------------------------

void device_vtlb_interface::vtlb_load(int entrynum, int numpages, offs_t address, vtlb_entry value)
{
	offs_t tableindex = address >> m_pageshift;
	int liveindex = m_dynamic + entrynum;
	int pagenum;

	// must be in range
	assert(entrynum >= 0 && entrynum < m_fixed);

#if PRINTF_TLB
	osd_printf_debug("vtlb_load %d for %d pages at %08X == %08X\n", entrynum, numpages, address, value);
#endif

	// if an entry already exists at this index, free it
	if (m_live[liveindex] != 0)
	{
		int oldtableindex = m_live[liveindex] - 1;
		m_refcnt[oldtableindex]--;
		if (m_refcnt[oldtableindex] == 0) {
			int pagecount = m_fixedpages[entrynum];
			for (pagenum = 0; pagenum < pagecount; pagenum++) {
				m_table[oldtableindex + pagenum] = 0;
			}
		}
	}

	// claim this new entry
	m_live[liveindex] = tableindex + 1;
	m_refcnt[tableindex]++;

	// store the raw value, making sure the "fixed" flag is set
	value |= VTLB_FLAG_FIXED;
	m_fixedpages[entrynum] = numpages;
	for (pagenum = 0; pagenum < numpages; pagenum++)
		m_table[tableindex + pagenum] = value + (pagenum << m_pageshift);
}

//-------------------------------------------------
//  vtlb_dynload - load a dynamic VTLB entry
//-------------------------------------------------

void device_vtlb_interface::vtlb_dynload(u32 index, offs_t address, vtlb_entry value)
{
	vtlb_entry entry = m_table[index];

	if (m_dynamic == 0)
	{
#if PRINTF_TLB
		osd_printf_debug("failed: no dynamic entries\n");
#endif
		return;
	}

	int liveindex = m_dynindex++ % m_dynamic;
	// is entry already live?
	if (!(entry & VTLB_FLAG_VALID))
	{
		// if an entry already exists at this index, free it
		if (m_live[liveindex] != 0)
			m_table[m_live[liveindex] - 1] = 0;

		// claim this new entry
		m_live[liveindex] = index + 1;
	}
	// form a new blank entry
	entry = (address >> m_pageshift) << m_pageshift;
	entry |= VTLB_FLAG_VALID | value;

#if PRINTF_TLB
	osd_printf_debug("success (%08X), new entry\n", address);
#endif
	m_table[index] = entry;
}

//**************************************************************************
//  FLUSHING
//**************************************************************************

//-------------------------------------------------
//  vtlb_flush_dynamic - flush all knowledge
//  from the dynamic part of the VTLB
//-------------------------------------------------

void device_vtlb_interface::vtlb_flush_dynamic()
{
#if PRINTF_TLB
	osd_printf_debug("vtlb_flush_dynamic\n");
#endif

	// loop over live entries and release them from the table
	for (int liveindex = 0; liveindex < m_dynamic; liveindex++)
		if (m_live[liveindex] != 0)
		{
			offs_t tableindex = m_live[liveindex] - 1;
			m_table[tableindex] = 0;
			m_live[liveindex] = 0;
		}
}


//-------------------------------------------------
//  vtlb_flush_address - flush knowledge of a
//  particular address from the VTLB
//-------------------------------------------------

void device_vtlb_interface::vtlb_flush_address(offs_t address)
{
	offs_t tableindex = address >> m_pageshift;

#if PRINTF_TLB
	osd_printf_debug("vtlb_flush_address %08X\n", address);
#endif

	// free the entry in the table; for speed, we leave the entry in the live array
	m_table[tableindex] = 0;
}



//**************************************************************************
//  ACCESSORS
//**************************************************************************

//-------------------------------------------------
//  vtlb_table - return a pointer to the base of
//  the linear VTLB lookup table
//-------------------------------------------------

const vtlb_entry *device_vtlb_interface::vtlb_table() const
{
	return m_table_base;
}