aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/Hardware/EmRegs.cpp
blob: bd1c50aee57514f9eb57a29c9bfb3330f5e88e3b (plain)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 2000-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#include "EmCommon.h"
#include "EmRegs.h"

#include "Byteswapping.h"		// Canonical
#include "EmBankRegs.h"			// EmBankRegs::InvalidAccess
#include "EmMemory.h"			// Memory::InitializeBanks, EmMemBankIndex

/*
	EmRegs is a base class for subclasses that manage the accessing of
	emulated memory at a byte, word, or long level.  See comments in
	EmRegsBank.cpp for a description on how EmRegs objects are managed,
	and EmMemory for how memory in general is emulated.

	EmRegs subclasses are suited for managing hardware registers, such
	as the Dragonball [EZ | VZ] hardware registers, the USB registers
	in the Handspring Visor, or the special PLD registers in some of the
	Palm VII devices.

	To create a new EmRegs subclass that can handle accesses to a special
	range of memory, do the following:

	* Create a subclass of EmRegs

	* Provide implementations for the pure virtual functions:

		* SetSubBankHandlers: this method needs to call EmRegs::SetHandler
		  to install a read and write function for each memory location
		  that can be accessed.  It should also first call the base
		  class SetSubBankHandlers in order to install default handlers
		  for all memory locations in the memory range the subclass manages.

		* GetRealAddress: given an address in emulated space, this function
		  returns the real address from the point of view of the emulator.
		  For instance, if your EmRegs subclass manages the memory range
		  0x17000000 to 0x17001000, and you have created a 4K buffer to
		  represent this memory range, then GetRealAddress would be like:

		  	return &fMyBuffer[address - 0x17000000];

		* GetAddressStart: returns the base address of the memory range
		  managed by this class.  In the example above, this method would
		  return 0x17000000.

		* GetAddressRange: returns the range of memory managed by this
		  class.  In the example above, this method would return 0x1000.

	* Define read and write function handlers for each memory location
	  in the managed range.  These functions handlers are installed in
	  your SetSubBankHandlers override.  EmBankRegs will call these
	  handlers as appropriate.

	* Optionally implement Initialize, Reset, Save, Load, and Dispose
	  overrides.  You will usually override Initialize to create any
	  buffers required to hold your data, and correspondingly dispose
	  of those resources in a Dispose override.  Most subclasses will
	  need to override Reset in order to initialize their emulated
	  registers.  And override Save and Load if you want to save and
	  restore your registers to the session (.psf) file.

	* In the appropriate case statement in EmDevice::CreateRegs, create
	  an instance of your EmRegs class and install it by calling
	  EmBankRegs::AddSubBank.
*/


// ---------------------------------------------------------------------------
//		¥ EmRegs::EmRegs
// ---------------------------------------------------------------------------

EmRegs::EmRegs (void) :
	fReadFunctions (),
	fWriteFunctions ()
{
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::~EmRegs
// ---------------------------------------------------------------------------

EmRegs::~EmRegs (void)
{
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::Initialize
// ---------------------------------------------------------------------------

void EmRegs::Initialize (void)
{
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::Reset
// ---------------------------------------------------------------------------

void EmRegs::Reset (Bool /*hardwareReset*/)
{
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::Save
// ---------------------------------------------------------------------------

void EmRegs::Save (SessionFile&)
{
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::Load
// ---------------------------------------------------------------------------

void EmRegs::Load (SessionFile&)
{
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::Dispose
// ---------------------------------------------------------------------------

void EmRegs::Dispose (void)
{
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::SetBankHandlers
// ---------------------------------------------------------------------------

void EmRegs::SetBankHandlers (EmAddressBank& bank)
{
	emuptr	address		= this->GetAddressStart ();
	uint32	range		= this->GetAddressRange ();
	uint32	numBanks	= EmMemBankIndex (address + range - 1) - EmMemBankIndex (address) + 1;

	Memory::InitializeBanks (bank, EmMemBankIndex (address), numBanks);

	this->SetSubBankHandlers ();
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::SetSubBankHandlers
// ---------------------------------------------------------------------------

void EmRegs::SetSubBankHandlers (void)
{
	this->SetHandler (&EmRegs::UnsupportedRead, &EmRegs::UnsupportedWrite,
						this->GetAddressStart (), this->GetAddressRange ());
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::GetLong
// ---------------------------------------------------------------------------

uint32 EmRegs::GetLong (emuptr address)
{
//	EmAssert (this->ValidAddress (address, 4));

	long			offset	= address - this->GetAddressStart ();
	ReadFunction	fn		= fReadFunctions [offset];
	EmAssert (fn);

	return (this->*fn) (address, 4);
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::GetWord
// ---------------------------------------------------------------------------

uint32 EmRegs::GetWord (emuptr address)
{
//	EmAssert (this->ValidAddress (address, 2));

	long			offset	= address - this->GetAddressStart ();
	ReadFunction	fn		= fReadFunctions [offset];
	EmAssert (fn);

	return (this->*fn) (address, 2);
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::GetByte
// ---------------------------------------------------------------------------

uint32 EmRegs::GetByte (emuptr address)
{
//	EmAssert (this->ValidAddress (address, 1));

	long			offset	= address - this->GetAddressStart ();
	ReadFunction	fn		= fReadFunctions [offset];
	EmAssert (fn);

	return (this->*fn) (address, 1);
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::SetLong
// ---------------------------------------------------------------------------

void EmRegs::SetLong (emuptr address, uint32 value)
{
//	EmAssert (this->ValidAddress (address, 4));

	long			offset	= address - this->GetAddressStart ();
	WriteFunction	fn		= fWriteFunctions [offset];
	EmAssert (fn);

	(this->*fn) (address, 4, value);
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::SetWord
// ---------------------------------------------------------------------------

void EmRegs::SetWord (emuptr address, uint32 value)
{
//	EmAssert (this->ValidAddress (address, 2));

	long			offset	= address - this->GetAddressStart ();
	WriteFunction	fn		= fWriteFunctions [offset];
	EmAssert (fn);

	(this->*fn) (address, 2, value);
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::SetByte
// ---------------------------------------------------------------------------

void EmRegs::SetByte (emuptr address, uint32 value)
{
//	EmAssert (this->ValidAddress (address, 1));

	long			offset	= address - this->GetAddressStart ();
	WriteFunction	fn		= fWriteFunctions [offset];
	EmAssert (fn);

	(this->*fn) (address, 1, value);
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::ValidAddress
// ---------------------------------------------------------------------------

int EmRegs::ValidAddress (emuptr address, uint32 size)
{
	UNUSED_PARAM (size);

	int				result	= false;
	unsigned long	offset	= address - this->GetAddressStart ();

	if (offset < fReadFunctions.size ())
	{
		ReadFunction	fn		= fReadFunctions [offset];
		result	= (fn != &EmRegs::UnsupportedRead);
	}

	return result;
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::GetRealAddress
// ---------------------------------------------------------------------------

uint8* EmRegs::GetRealAddress (emuptr address)
{
	UNUSED_PARAM (address);

	// Sub-class's responsibility.

	return NULL;
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::SetHandler
// ---------------------------------------------------------------------------

void EmRegs::SetHandler (ReadFunction read, WriteFunction write,
							uint32 start, int count)
{
	if (fReadFunctions.size () == 0)
	{
		uint32	range	= this->GetAddressRange ();

		fReadFunctions.resize (range, &EmRegs::UnsupportedRead);
		fWriteFunctions.resize (range, &EmRegs::UnsupportedWrite);
	}

	int index = start - this->GetAddressStart ();

	EmAssert (index >= 0);
	EmAssert (index < (long) fReadFunctions.size ());

	for (int ii = 0; ii < count; ++ii, ++index)
	{
		fReadFunctions[index] = read;
		fWriteFunctions[index] = write;
	}
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::UnsupportedRead
// ---------------------------------------------------------------------------

uint32 EmRegs::UnsupportedRead (emuptr address, int size)
{
	if (!CEnableFullAccess::AccessOK ())
	{
		EmBankRegs::PreventedAccess (address, size, true);
	}

	return ~0;
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::UnsupportedWrite
// ---------------------------------------------------------------------------

void EmRegs::UnsupportedWrite (emuptr address, int size, uint32 value)
{
	UNUSED_PARAM(value)

	if (!CEnableFullAccess::AccessOK ())
	{
		EmBankRegs::PreventedAccess (address, size, false);
	}
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::StdReadBE
// ---------------------------------------------------------------------------
// Read registers in a strict Big Endian fashion.

uint32 EmRegs::StdReadBE (emuptr address, int size)
{
	uint8*	realAddr = this->GetRealAddress (address);

	if (size == 1)
		return *(uint8*) realAddr;

	if (size == 2)
	{
		uint16	result16 = *(uint16*) realAddr;
		Canonical (result16);
		return result16;
	}

#if UNALIGNED_LONG_ACCESS

	uint32	result32 = *(uint32*) realAddr;
	Canonical (result32);
	return result32;

#else

	return	(realAddr[0] << 24) |
			(realAddr[1] << 16) |
			(realAddr[2] <<  8) |
			(realAddr[3] <<  0);

#endif
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::StdWriteBE
// ---------------------------------------------------------------------------
// Write registers in a strict Big Endian fashion.

void EmRegs::StdWriteBE (emuptr address, int size, uint32 value)
{
	uint8*	realAddr = this->GetRealAddress (address);

	if (size == 1)
	{
		*(uint8*) realAddr = value;
	}

	else if (size == 2)
	{
		uint16	value16 = value;
		Canonical (value16);
		*(uint16*) realAddr = value16;
	}

	else if (size == 4)
	{
#if UNALIGNED_LONG_ACCESS

		Canonical (value);
		*(uint32*) realAddr = value;

#else

		realAddr[0] = (uint8) (value >> 24);
		realAddr[1] = (uint8) (value >> 16);
		realAddr[2] = (uint8) (value >>  8);
		realAddr[3] = (uint8) (value >>  0);

#endif
	}
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::StdRead
// ---------------------------------------------------------------------------
// Read registers using the routines in maccess.h

uint32 EmRegs::StdRead (emuptr address, int size)
{
	uint8*	realAddr = this->GetRealAddress (address);

	if (size == 1)
		return EmMemDoGet8 (realAddr);

	if (size == 2)
		return EmMemDoGet16 (realAddr);

	return EmMemDoGet32 (realAddr);
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::StdWrite
// ---------------------------------------------------------------------------
// Write registers using the routines in maccess.h

void EmRegs::StdWrite (emuptr address, int size, uint32 value)
{
	uint8*	realAddr = this->GetRealAddress (address);

	if (size == 1)
		EmMemDoPut8 (realAddr, value);

	else if (size == 2)
		EmMemDoPut16 (realAddr, value);

	else
		EmMemDoPut32 (realAddr, value);
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::ZeroRead
// ---------------------------------------------------------------------------

uint32 EmRegs::ZeroRead (emuptr address, int size)
{
	UNUSED_PARAM(address)
	UNUSED_PARAM(size)

	return 0;
}


// ---------------------------------------------------------------------------
//		¥ EmRegs::NullWrite
// ---------------------------------------------------------------------------

void EmRegs::NullWrite (emuptr address, int size, uint32 value)
{
	UNUSED_PARAM(address)
	UNUSED_PARAM(size)
	UNUSED_PARAM(value)

	// Do nothing (for read-only registers).
}