aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/Hardware/EmRegsSED1375.cpp
blob: ea9831becb5f5c3d7f341a4d8c1e7b84648c9d33 (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
500
501
502
503
504
505
506
/* -*- 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 "EmRegsSED1375.h"

#include "Byteswapping.h"		// Canonical
#include "EmMemory.h"			// EmMem_memcpy
#include "EmPixMap.h"			// SetSize, SetRowBytes, etc.
#include "EmScreen.h"			// EmScreen::InvalidateAll
#include "Miscellaneous.h"		// StWordSwapper
#include "SessionFile.h"		// WriteSED1375RegsType


// Given a register (specified by its field name), return its address
// in emulated space.

#define addressof(reg)				\
	(this->GetAddressStart () + fRegs.offsetof_##reg ())


// Macro to help the installation of handlers for a register.

#define INSTALL_HANDLER(read, write, reg)			\
	this->SetHandler (	(ReadFunction) &EmRegsSED1375::read,		\
						(WriteFunction) &EmRegsSED1375::write,		\
						addressof (reg),			\
						fRegs.reg.GetSize ())


#define kCLUTColorIndexMask 	0xf000
#define kCLUTColorsMask 		0x0fff

#define kCLUTRedMask			0x0f00
#define kCLUTGreenMask			0x00f0
#define kCLUTBlueMask			0x000f

#define kCLUTIndexRed			0x4000
#define kCLUTIndexGreen 		0x2000
#define kCLUTIndexBlue			0x1000



// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::EmRegsSED1375
// ---------------------------------------------------------------------------

EmRegsSED1375::EmRegsSED1375 (emuptr baseRegsAddr, emuptr baseVideoAddr) :
	fBaseRegsAddr (baseRegsAddr),
	fBaseVideoAddr (baseVideoAddr),
	fRegs ()
{
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::~EmRegsSED1375
// ---------------------------------------------------------------------------

EmRegsSED1375::~EmRegsSED1375 (void)
{
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::Initialize
// ---------------------------------------------------------------------------

void EmRegsSED1375::Initialize (void)
{
	EmRegs::Initialize ();
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::Reset
// ---------------------------------------------------------------------------

void EmRegsSED1375::Reset (Bool hardwareReset)
{
	EmRegs::Reset (hardwareReset);

	if (hardwareReset)
	{
		memset (fRegs.GetPtr (), 0, fRegs.GetSize ());

//		EmAssert ((sed1375ProductCodeExpected | sed1375RevisionCodeExpected) == 0x24);
		fRegs.productRevisionCode = 0x24;
	}
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::Save
// ---------------------------------------------------------------------------

void EmRegsSED1375::Save (SessionFile& f)
{
	EmRegs::Save (f);

	f.WriteSED1375RegsType (*(SED1375RegsType*) fRegs.GetPtr ());
	f.FixBug (SessionFile::kBugByteswappedStructs);

	StWordSwapper	swapper1 (fClutData, sizeof (fClutData));
	f.WriteSED1375Palette (fClutData);
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::Load
// ---------------------------------------------------------------------------

void EmRegsSED1375::Load (SessionFile& f)
{
	EmRegs::Load (f);

	// Read in the SED registers.

	if (f.ReadSED1375RegsType (*(SED1375RegsType*) fRegs.GetPtr ()))
	{
		// The Windows version of Poser 2.1d29 and earlier did not write
		// out structs in the correct format.  The fields of the struct
		// were written out in Little-Endian format, not Big-Endian.  To
		// address this problem, the bug has been fixed, and a new field
		// is added to the file format indicating that the bug has been
		// fixed.  With the new field (the "bug bit"), Poser can identify
		// old files from new files and read them in accordingly.
		// 
		// With the bug fixed, the .psf files should now be interchangeable
		// across platforms (modulo other bugs...).

		if (!f.IncludesBugFix (SessionFile::kBugByteswappedStructs))
		{
			Canonical (*(SED1375RegsType*) fRegs.GetPtr ());
		}
	}
	else
	{
		f.SetCanReload (false);
	}

	// Read in the LCD palette, and then byteswap it.

	if (f.ReadSED1375Palette (fClutData))
	{
		::ByteswapWords (fClutData, sizeof (fClutData));
	}
	else
	{
		f.SetCanReload (false);
	}
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::Dispose
// ---------------------------------------------------------------------------

void EmRegsSED1375::Dispose (void)
{
	EmRegs::Dispose ();
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::SetSubBankHandlers
// ---------------------------------------------------------------------------

void EmRegsSED1375::SetSubBankHandlers (void)
{
	// Install base handlers.

	EmRegs::SetSubBankHandlers ();

	// Now add standard/specialized handers for the defined registers.

	INSTALL_HANDLER (StdReadBE,				NullWrite,					productRevisionCode);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					mode0);
	INSTALL_HANDLER (StdReadBE,				invalidateWrite,			mode1);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					mode2);
	INSTALL_HANDLER (StdReadBE,				invalidateWrite,			horizontalPanelSize);
	INSTALL_HANDLER (StdReadBE,				invalidateWrite,			verticalPanelSizeLSB);
	INSTALL_HANDLER (StdReadBE,				invalidateWrite,			verticalPanelSizeMSB);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					FPLineStartPosition);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					horizontalNonDisplayPeriod);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					FPFRAMEStartPosition);
	INSTALL_HANDLER (vertNonDisplayRead,	StdWriteBE,					verticalNonDisplayPeriod);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					MODRate);
	INSTALL_HANDLER (StdReadBE,				invalidateWrite,			screen1StartAddressLSB);
	INSTALL_HANDLER (StdReadBE,				invalidateWrite,			screen1StartAddressMSB);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					screen2StartAddressLSB);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					screen2StartAddressMSB);
	INSTALL_HANDLER (StdReadBE,				invalidateWrite,			screen1StartAddressMSBit);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					memoryAddressOffset);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					screen1VerticalSizeLSB);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					screen1VerticalSizeMSB);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					unused1);
	INSTALL_HANDLER (StdReadBE,				lookUpTableAddressWrite,	lookUpTableAddress);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					unused2);
	INSTALL_HANDLER (lookUpTableDataRead,	lookUpTableDataWrite,		lookUpTableData);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					GPIOConfigurationControl);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					GPIOStatusControl);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					scratchPad);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					portraitMode);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					lineByteCountRegister);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					unused3);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					unused4);
	INSTALL_HANDLER (StdReadBE,				StdWriteBE,					unused5);
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::GetRealAddress
// ---------------------------------------------------------------------------

uint8* EmRegsSED1375::GetRealAddress (emuptr address)
{
	return (uint8*) fRegs.GetPtr () + address - this->GetAddressStart ();
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::GetAddressStart
// ---------------------------------------------------------------------------

emuptr EmRegsSED1375::GetAddressStart (void)
{
	return fBaseRegsAddr;
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::GetAddressRange
// ---------------------------------------------------------------------------

uint32 EmRegsSED1375::GetAddressRange (void)
{
	return fRegs.GetSize ();
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::GetLCDScreenOn
// ---------------------------------------------------------------------------

Bool EmRegsSED1375::GetLCDScreenOn (void)
{
	return ((fRegs.mode1) & sed1375DisplayBlank) == 0;
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::GetLCDBacklightOn
// ---------------------------------------------------------------------------

Bool EmRegsSED1375::GetLCDBacklightOn (void)
{
	return true;			// The Backlight is always on for these units.
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::GetLCDHasFrame
// ---------------------------------------------------------------------------

Bool EmRegsSED1375::GetLCDHasFrame (void)
{
	return true;
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::GetLCDBeginEnd
// ---------------------------------------------------------------------------

void EmRegsSED1375::GetLCDBeginEnd (emuptr& begin, emuptr& end)
{
	// Get the screen metrics.

	int32	bpp			= 1 << ((fRegs.mode1 & sed1375BPPMask) >> sed1375BPPShift);
	int32	height		= ((fRegs.verticalPanelSizeMSB << 8) | fRegs.verticalPanelSizeLSB) + 1;
	int32	rowBytes	= (fRegs.horizontalPanelSize + 1) * bpp;
	uint32	offset		= (fRegs.screen1StartAddressMSBit	<< 17) |
						  (fRegs.screen1StartAddressMSB		<<  9) |
						  (fRegs.screen1StartAddressLSB		<<  1);
	emuptr	baseAddr	= fBaseVideoAddr + offset;

	begin = baseAddr;
	end = baseAddr + rowBytes * height;
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::GetLCDScanlines
// ---------------------------------------------------------------------------

void EmRegsSED1375::GetLCDScanlines (EmScreenUpdateInfo& info)
{
	// Get the screen metrics.

	int32	bpp			= 1 << ((fRegs.mode1 & sed1375BPPMask) >> sed1375BPPShift);
	int32	width		= (fRegs.horizontalPanelSize + 1) * 8;
	int32	height		= ((fRegs.verticalPanelSizeMSB << 8) | fRegs.verticalPanelSizeLSB) + 1;
	int32	rowBytes	= (fRegs.horizontalPanelSize + 1) * bpp;
	uint32	offset		= (fRegs.screen1StartAddressMSBit	<< 17) |
						  (fRegs.screen1StartAddressMSB		<<  9) |
						  (fRegs.screen1StartAddressLSB		<<  1);
	emuptr	baseAddr	= fBaseVideoAddr + offset;

	info.fLeftMargin	= 0;

	EmPixMapFormat	format	=	bpp == 1 ? kPixMapFormat1 :
								bpp == 2 ? kPixMapFormat2 :
								bpp == 4 ? kPixMapFormat4 :
								kPixMapFormat8;

	RGBList	colorTable;
	this->PrvGetPalette (colorTable);

	// Set format, size, and color table of EmPixMap.

	info.fImage.SetSize			(EmPoint (width, height));
	info.fImage.SetFormat		(format);
	info.fImage.SetRowBytes		(rowBytes);
	info.fImage.SetColorTable	(colorTable);

	// Determine first and last scanlines to fetch, and fetch them.

	info.fFirstLine		= (info.fScreenLow - baseAddr) / rowBytes;
	info.fLastLine		= (info.fScreenHigh - baseAddr - 1) / rowBytes + 1;

	long	firstLineOffset	= info.fFirstLine * rowBytes;
	long	lastLineOffset	= info.fLastLine * rowBytes;

	EmMem_memcpy (
		(void*) ((uint8*) info.fImage.GetBits () + firstLineOffset),
		baseAddr + firstLineOffset,
		lastLineOffset - firstLineOffset);
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::invalidateWrite
// ---------------------------------------------------------------------------

void EmRegsSED1375::invalidateWrite (emuptr address, int size, uint32 value)
{
	this->StdWriteBE (address, size, value);
	EmScreen::InvalidateAll ();
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::vertNonDisplayRead
// ---------------------------------------------------------------------------

uint32 EmRegsSED1375::vertNonDisplayRead (emuptr address, int size)
{
	UNUSED_PARAM(address)
	UNUSED_PARAM(size)

	// Always set the vertical non-display status high since in the real
	// hardware, the ROM will check this flag in order to write the CLUT
	// registers.

	return (fRegs.verticalNonDisplayPeriod) | sed1375VerticalNonDisplayStatus;
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::lookUpTableAddressWrite
// ---------------------------------------------------------------------------

void EmRegsSED1375::lookUpTableAddressWrite (emuptr address, int size, uint32 value)
{
	this->StdWriteBE (address, size, value);

	value &= 0x0FF;

	fClutData[value] &= kCLUTColorsMask;				// Update the rgb index
	fClutData[value] |= kCLUTIndexRed;
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::lookUpTableDataRead
// ---------------------------------------------------------------------------

uint32 EmRegsSED1375::lookUpTableDataRead (emuptr address, int size)
{
	EmAssert (size == 1);

	if (size != 1)
		return 0;			// Error case.

	uint8	clutIndex = (fRegs.lookUpTableAddress); 	// Get the LUT Addr.
	uint16	clutEntry = fClutData[clutIndex];			// Get the entry.
	uint8	colorData;

	if ((clutEntry & kCLUTIndexRed) != 0)
	{
		colorData = (uint8) ((clutEntry & kCLUTRedMask) >> 4); 	// Get the 4 bits of red.

		fClutData[clutIndex] =									// Update the next rgb index
			(fClutData[clutIndex] & kCLUTColorsMask) | kCLUTIndexGreen;
	}
	else if ((clutEntry & kCLUTIndexGreen) != 0)
	{
		colorData = (uint8) (clutEntry & kCLUTGreenMask);		// Get the 4 bits of green

		fClutData[clutIndex] =									// Update the next rgb index
			(fClutData[clutIndex] & kCLUTColorsMask) | kCLUTIndexBlue;
	}
	else
	{
		colorData = (uint8) ((clutEntry & kCLUTBlueMask) << 4);	// Get the 4 bits of blue.

		address = (emuptr) (addressof (lookUpTableAddress));
		EmRegsSED1375::lookUpTableAddressWrite (address, 1, (clutIndex + 1) & 0xFF);
	}

	return colorData;
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::lookUpTableDataWrite
// ---------------------------------------------------------------------------

void EmRegsSED1375::lookUpTableDataWrite (emuptr address, int size, uint32 value)
{
	EmAssert (size == 1);

	if (size != 1)
		return; 		// Error case.

	uint8	clutIndex = (fRegs.lookUpTableAddress); 	// Get the LUT Addr.
	uint16	clutEntry = fClutData[clutIndex];			// Get the entry.

	uint8	newColor = (uint8) (value & 0x00F0);

	if (clutEntry & kCLUTIndexRed)
	{
		fClutData[clutIndex] &= ~kCLUTRedMask;			// Clear out old red bits.
		fClutData[clutIndex] |= newColor << 4;			// Save in new red bits.

		fClutData[clutIndex] =							// Update the rgb index
			(fClutData[clutIndex] & kCLUTColorsMask) | kCLUTIndexGreen;
	}
	else if (clutEntry & kCLUTIndexGreen)
	{
		fClutData[clutIndex] &= ~kCLUTGreenMask;		// Clear out old red bits.
		fClutData[clutIndex] |= newColor;				// Save in new green bits.

		fClutData[clutIndex] =							// Update the rgb index
			(fClutData[clutIndex] & kCLUTColorsMask) | kCLUTIndexBlue;
	}
	else
	{
		fClutData[clutIndex] &= ~kCLUTBlueMask; 		// Clear out old red bits.
		fClutData[clutIndex] |= newColor >> 4;			// Save in new blue bits.

		address = (emuptr) (addressof (lookUpTableAddress));
		EmRegsSED1375::lookUpTableAddressWrite (address, 1, (clutIndex + 1) & 0xFF);
	}

	EmScreen::InvalidateAll ();
}


// ---------------------------------------------------------------------------
//		¥ EmRegsSED1375::PrvGetPalette
// ---------------------------------------------------------------------------

void EmRegsSED1375::PrvGetPalette (RGBList& thePalette)
{
	int32	bpp			= 1 << ((fRegs.mode1 & sed1375BPPMask) >> sed1375BPPShift);
	int32 	numColors	= 1 << bpp;

	thePalette.resize (numColors);

	for (int ii = 0; ii < numColors; ++ii)
	{
		uint16	curEntry = fClutData[ii];
		uint8	color;

		color = (uint8) ((curEntry & kCLUTRedMask) >> 4);
		thePalette[ii].fRed = color + (color >> 4);

		color = (uint8) ((curEntry & kCLUTGreenMask) >> 0);
		thePalette[ii].fGreen = color + (color >> 4);

		color = (uint8) ((curEntry & kCLUTBlueMask) << 4);
		thePalette[ii].fBlue = color + (color >> 4);
	}
}