aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/Patches/EmPatchModule.cpp
blob: a4ad8a425931d0bb8090b5a30f215699918ca1fb (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
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1998-2001 Palm, Inc. or its subsidiaries.
	Copyright (c) 2001 PocketPyro, Inc.
	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 "EmPatchModule.h"

#include "EmPatchModuleMap.h"
#include "EmStructs.h"
#include "EmPalmFunction.h"		// SysTrapIndex, IsLibraryTrap


/***********************************************************************
 *
 * FUNCTION:	EmPatchModule::EmPatchModule
 *
 * DESCRIPTION: Base EmPatchModule constructor
 *
 * PARAMETERS:	mapIP			Interface to the EmPatchModule map this module will be added to.  
 *				nameCSP			Unique name given to this EmPatchModule.
 *				loadTables		boolean should ProtoPatchTables be loaded immediately?
 *				protoTable1P	Optional Protopatch table 1 - AddProtoPatchTable can be used subsequently
 *				protoTable2P	Optional Protopatch table 2 - AddProtoPatchTable can be used subsequently
 *
 * RETURNED:	nothing
 *
 ***********************************************************************/

EmPatchModule::EmPatchModule(const char *nameCSP, ProtoPatchTableEntry *protoTable1P, ProtoPatchTableEntry *protoTable2P)
{
	fContainerIP = NULL;

	fProtoTableCount = 0;
	fLoadedTableCount = 0;

	memset(fProtoTables, 0, kMaxProtoTables * sizeof(ProtoPatchTableEntry *));

	fName = nameCSP;

	if (protoTable1P != NULL)
		fProtoTables[fProtoTableCount++] = protoTable1P;

	if (protoTable2P != NULL)
		fProtoTables[fProtoTableCount++] = protoTable2P;
}



/***********************************************************************
 *
 * FUNCTION:	EmPatchModule::PrvLoadProtoPatchTable
 *
 * DESCRIPTION: Loads the given ProtoPatch Table
 *
 * PARAMETERS:	tableNbr - index of the internally referenced ProtoPatch tables to load
 *
 * RETURNED:	nothing
 *
 ***********************************************************************/

void EmPatchModule::PrvLoadProtoPatchTable (uint16 tableNbr)
{
	// Create a fast dispatch table for the managed module.  A "fast
	// dispatch table" is a table with a headpatch and tailpatch entry
	// for each possible function in the module.  If the function is
	// not head or tailpatched, the corresponding entry is NULL.  When
	// a patch function is needed, the trap dispatch number is used as
	// an index into the table in order to get the right patch function.
	//
	// For simplicity, "fast patch tables" are created from "proto patch
	// tables".  A proto patch table is a compact table containing the
	// information needed to create a fast patch table.  Each entry in
	// the proto patch table is a trap-number/headpatch/tailpatch tupple.
	// Each tuple is examined in turn.	If there is a head or tail patch
	// function for the indicated module function, that patch function
	// is entered in the fast dispatch table, using the trap number as
	// the index.
	
	ProtoPatchTableEntry *protoPatchTable = fProtoTables[tableNbr];

	for (long ii = 0; protoPatchTable[ii].fTrapWord; ++ii)
	{
		// If there is a headpatch function...

		if (protoPatchTable[ii].fHeadpatch)
		{
			// Get the trap number.

			uint16 index = ::SysTrapIndex (protoPatchTable[ii].fTrapWord);

			// If the trap number is 0xA800-based, make it zero based.

			if (::IsLibraryTrap (index))
				index -= SysTrapIndex (sysLibTrapBase);

			// Resize the fast patch table, if necessary.

			if (index >= fHeadpatches.size ())
			{
				fHeadpatches.resize (index + 1);
			}

			// Add the headpatch function.

			fHeadpatches[index] = protoPatchTable[ii].fHeadpatch;
		}

		// If there is a tailpatch function...

		if (protoPatchTable[ii].fTailpatch)
		{
			// Get the trap number.

			uint16 index = SysTrapIndex (protoPatchTable[ii].fTrapWord);

			// If the trap number is 0xA800-based, make it zero based.

			if (IsLibraryTrap (index))
				index -= SysTrapIndex (sysLibTrapBase);

			// Resize the fast patch table, if necessary.

			if (index >= fTailpatches.size ())
			{
				fTailpatches.resize (index + 1);
			}

			// Add the tailpatch function.

			fTailpatches[index] = protoPatchTable[ii].fTailpatch;
		}
	}
}

// ==============================================================================
// *  interface implementations
// ==============================================================================
// ==============================================================================
// *  IEmPatchModule
//
// *  Interface exposed by all PatchModules
// ==============================================================================
		

/***********************************************************************
 *
 * FUNCTION:	EmPatchModule::Initialize
 *
 * DESCRIPTION: Initializes the EmPatchModule component
 *
 * PARAMETERS:	none
 *
 * RETURNED:	patchErrNone
 *
 ***********************************************************************/

Err EmPatchModule::Initialize(IEmPatchContainer &containerI)
{
	fContainerIP = &containerI;

	Clear();
	Load();

	return kPatchErrNone;
}

/***********************************************************************
 *
 * FUNCTION:	EmPatchModule::Reset
 *
 * DESCRIPTION: Resets the EmPatchModule component
 *
 * PARAMETERS:	none
 *
 * RETURNED:	patchErrNone
 *
 ***********************************************************************/

Err EmPatchModule::Reset()
{
	return kPatchErrNone;
}

/***********************************************************************
 *
 * FUNCTION:	EmPatchModule::Dispose
 *
 * DESCRIPTION: Disposes the EmPatchModule component
 *
 * PARAMETERS:	none
 *
 * RETURNED:	patchErrNone
 *
 ***********************************************************************/

Err EmPatchModule::Dispose()
{
	Clear();
	return kPatchErrNone;
}


/***********************************************************************
 *
 * FUNCTION:	EmPatchModule::Clear
 *
 * DESCRIPTION: Clears the EmPatchModule component
 *
 * PARAMETERS:	none
 *
 * RETURNED:	patchErrNone
 *
 ***********************************************************************/

Err EmPatchModule::Clear()
{
	fHeadpatches.clear ();
	fTailpatches.clear ();

	fLoadedTableCount = 0;

	return kPatchErrNone;
}


/***********************************************************************
 *
 * FUNCTION:	EmPatchModule::Load
 *
 * DESCRIPTION: Loads the EmPatchModule component
 *
 * PARAMETERS:	none
 *
 * RETURNED:	patchErrNone
 *
 ***********************************************************************/

Err EmPatchModule::Load()
{
	while (fLoadedTableCount < fProtoTableCount)
	{
		PrvLoadProtoPatchTable(fLoadedTableCount++);
	}

	return kPatchErrNone;
}


/***********************************************************************
 *
 * FUNCTION:	EmPatchModule::GetName
 *
 * DESCRIPTION: Gets a reference to the unique string name of the EmPatchModule component
 *
 * PARAMETERS:	none
 *
 * RETURNED:	reference to the unique string name
 *
 ***********************************************************************/

const string &EmPatchModule::GetName()
{
	return fName;
}


// Return the patch function for the given module function   The given
// module function *must* be given as a zero-based index.  If there is
// no patch function for the modeule function, procP == NULL.
//
Err EmPatchModule::GetHeadpatch (uint16 index, HeadpatchProc& procP)
{
	Err err = kPatchErrInvalidIndex;
	procP = NULL;

	if (index < fHeadpatches.size ())
	{
		procP = fHeadpatches[index];
		err = kPatchErrNone;
	}

	return err;
}


// Return the patch function for the given module function   The given
// module function *must* be given as a zero-based index.  If there is
// no patch function for the modeule function, procP == NULL.
//
Err EmPatchModule::GetTailpatch (uint16 index, TailpatchProc& procP)
{
	Err err = kPatchErrInvalidIndex;
	procP = NULL;

	if (index < fTailpatches.size ())
	{
		procP = fTailpatches[index];
		err = kPatchErrNone;
	}

	return err;
}


// ==============================================================================
// *  END IEmPatchModule
// ==============================================================================