File: SessionFile.cpp

package info (click to toggle)
pose 3.5-1
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 13,108 kB
  • ctags: 29,485
  • sloc: cpp: 93,990; ansic: 62,838; sh: 1,970; perl: 1,891; python: 1,242; makefile: 616
file content (1142 lines) | stat: -rw-r--r-- 28,959 bytes parent folder | download | duplicates (3)
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1999-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 "SessionFile.h"

#include "Byteswapping.h"		// Canonical
#include "EmErrCodes.h"			// kError_InvalidDevice
#include "EmPalmStructs.h"		// EmProxySED1376RegsType
#include "EmStreamFile.h"		// EmStreamFile
#include "ErrorHandling.h"		// Errors::Throw
#include "Miscellaneous.h"		// StMemory, RunLengthEncode, GzipEncode, etc.
#include "UAE.h"				// regstruct


/***********************************************************************
 *
 * FUNCTION:	SessionFile constructor
 *
 * DESCRIPTION:	Initialize the SessionFile object.  Sets the fFile data
 *				member to refer to the given ChunkFile (which must
 *				exist for the life of the SessionFile).
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

SessionFile::SessionFile (ChunkFile& f) :
	fFile (f),
	fCanReload (false),
	fCfg (),
	fReadBugFixes (false),
	fChangedBugFixes (false),
	fBugFixes (0)
{
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile destructor
 *
 * DESCRIPTION:	Releases SessionFile resources.  Currently does nothing.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

SessionFile::~SessionFile (void)
{
	if (fChangedBugFixes)
	{
		this->WriteBugFixes (fBugFixes);
	}
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadROMFileReference
 *
 * DESCRIPTION:	Read a reference to a paired ROM file from the session
 *				file.  For robustness, several approaches are used to
 *				find a ROM file.  Because these approaches are platform-
 *				specific, most of the work is off-loaded to the Platform
 *				sub-system.  If the file reference can be read, it's
 *				recorded in the configuration record to be used the next
 *				time a new session is created.
 *
 * PARAMETERS:	f - reference to EmFileRef to receive the read value.
 *
 * RETURNED:	True if a value could be found, false otherwise.
 *
 ***********************************************************************/

Bool SessionFile::ReadROMFileReference (EmFileRef& f)
{
	// Look for a ROM reference using a platform-specific method.

	if (Platform::ReadROMFileReference (fFile, f))
		return true;

	// If a path can't be found, look for a simple ROM name.

	string	name;
	if (fFile.ReadString (SessionFile::kROMNameTag, name))
	{
		// First, look in the same directory as the session file.

		try
		{
			// Get the stream this ChunkFile is using and see if it's
			// a file-based stream.

			EmStream&		stream = fFile.GetStream ();
			EmStreamFile&	fileStream = dynamic_cast<EmStreamFile&> (stream);

			// If so, get the directory this file is in.

			EmFileRef		sessionRef = fileStream.GetFileRef ();
			EmDirRef		sessionParent = sessionRef.GetParent ();

			f = EmFileRef (sessionParent, name);

			if (f.Exists ())
				return true;
		}
		catch (...)	// Exception thrown if dynamic_cast fails.
		{
		}

		// If not there, look in the same directory as Poser itself.

		EmDirRef	emulatorDir = EmDirRef::GetEmulatorDirectory ();

		// Return this reference, even if the file doesn't exist.
		// That way, we can at least form error messages.

		f = EmFileRef (emulatorDir, name);
		return true;
	}

	return false;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadRAMImage
 *
 * DESCRIPTION:	Read the RAM image from the session file.  Attempts to
 *				read a number of versions of the image, including
 *				compressed and uncompressed versions.  If the image can
 *				be read, its size is recorded in the configuration
 *				record to be used the next time a new session is created.
 *
 * PARAMETERS:	image - reference to the pointer to receive the address
 *					of the RAM image.
 *
 *				size - reference to the integer to receive the size of
 *					the RAM image.
 *
 * RETURNED:	True if the image was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadRAMImage (void* image)
{
	Bool	result = this->ReadChunk (kRAMDataTag, image, kGzipCompression);

	if (!result)
		result = this->ReadChunk (kRLERAMDataTag, image, kRLECompression);

	if (!result)
		result = this->ReadChunk (kUncompRAMDataTag, image, kNoCompression);

	return result;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadMetaRAMImage
 *
 * DESCRIPTION:	Read the MetaRAM image from the session file.  Attempts
 *				to read a number of versions of the image, including
 *				compressed and uncompressed versions.
 *
 * PARAMETERS:	image - reference to the pointer to receive the address
 *					of the MetaRAM image.
 *
 *				size - reference to the integer to receive the size of
 *					the MetaRAM image.
 *
 * RETURNED:	True if the image was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadMetaRAMImage (void* image)
{
	Bool	result = this->ReadChunk (kMetaRAMDataTag, image, kGzipCompression);

	if (!result)
		result = this->ReadChunk (kRLEMetaRAMDataTag, image, kRLECompression);

	return result;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadMetaROMImage
 *
 * DESCRIPTION:	Read the MetaROM image from the session file.  Attempts
 *				to read a number of versions of the image, including
 *				compressed and uncompressed versions.
 *
 * PARAMETERS:	image - reference to the pointer to receive the address
 *					of the MetaROM image.
 *
 *				size - reference to the integer to receive the size of
 *					the MetaROM image.
 *
 * RETURNED:	True if the image was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadMetaROMImage (void* image)
{
	Bool	result = this->ReadChunk (kMetaROMDataTag, image, kGzipCompression);

	return result;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadHwrDBallType
 *
 * DESCRIPTION:	Read the Dragonball hardware registers from the
 *				session file.
 *
 * PARAMETERS:	hwRegs - reference to the struct to receive the register
 *					data.
 *
 * RETURNED:	True if the data was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadHwrDBallType (HwrM68328Type& hwRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kDBRegs, chunk))
	{
		memcpy (&hwRegs, chunk.GetPointer (), sizeof (hwRegs));
		return true;
	}

	return false;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadHwrDBallEZType
 *
 * DESCRIPTION:	Read the DragonballEZ hardware registers from the
 *				session file.
 *
 * PARAMETERS:	hwRegs - reference to the struct to receive the register
 *					data.
 *
 * RETURNED:	True if the data was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadHwrDBallEZType (HwrM68EZ328Type& hwRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kDBEZRegs, chunk))
	{
		memcpy (&hwRegs, chunk.GetPointer (), sizeof (hwRegs));
		return true;
	}

	return false;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadHwrDBallVZType
 *
 * DESCRIPTION:	Read the DragonballVZ hardware registers from the
 *				session file.
 *
 * PARAMETERS:	hwRegs - reference to the struct to receive the register
 *					data.
 *
 * RETURNED:	True if the data was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadHwrDBallVZType (HwrM68VZ328Type& hwRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kDBVZRegs, chunk))
	{
		memcpy (&hwRegs, chunk.GetPointer (), sizeof (hwRegs));
		return true;
	}

	return false;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadHwrDBallSZType
 *
 * DESCRIPTION:	Read the DragonballSZ hardware registers from the
 *				session file.
 *
 * PARAMETERS:	hwRegs - reference to the struct to receive the register
 *					data.
 *
 * RETURNED:	True if the data was found and could be read in.
 *
 ***********************************************************************/



/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadDBallRegs
 *
 * DESCRIPTION:	Read the Dragonball CPU registers structure from the
 *				session file.
 *
 * PARAMETERS:	cpuRegs - reference to the struct to receive the register
 *					data.
 *
 * RETURNED:	True if the data was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadDBallRegs (regstruct& cpuRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kCPURegs, chunk))
	{
		memcpy (&cpuRegs, chunk.GetPointer (), sizeof (cpuRegs));
		return true;
	}

	return false;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadDeviceType
 *
 * DESCRIPTION:	Read the id of the device to emulate.  If the id can be
 *				read, it's recorded in the configuration record to be
 *				used the next time a new session is created.
 *
 * PARAMETERS:	v - reference to the EmDevice to receive the read value.
 *
 * RETURNED:	True if the value was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadDevice (EmDevice& v)
{
	v = EmDevice ();

	// Look up the device by the string.

	string	deviceTypeString;

	if (fFile.ReadString (kDeviceTypeString, deviceTypeString))
	{
		v = EmDevice (deviceTypeString);
	}

	return v.Supported ();
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadBugFixes
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	v - reference to the BugFixes to receive the read value.
 *
 * RETURNED:	True if the value was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadBugFixes (BugFixes& v)
{
	uint32	bits;
	Bool	result = fFile.ReadInt (kBugsTag, bits);

	if (result)
	{
		v = (BugFixes) bits;
	}

	return result;
}


Bool SessionFile::ReadSED1375RegsType (SED1375RegsType& sedRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kSED1375Regs, chunk))
	{
		memcpy (&sedRegs, chunk.GetPointer (), sizeof (sedRegs));
		return true;
	}

	return false;
}


Bool SessionFile::ReadSED1375Image (void* image)
{
	Bool	result = this->ReadChunk (kSED1375Image, image, kGzipCompression);

	return result;
}


Bool SessionFile::ReadSED1375Palette (uint16 palette[256])
{
	Chunk	chunk;
	if (fFile.ReadChunk (kSED1375Palette, chunk))
	{
		// Note: "sizeof (palette)" gives 4, not 512.
		int	size = 256 * sizeof (palette[0]);
		memcpy (palette, chunk.GetPointer (), size);
		return true;
	}

	return false;
}


Bool SessionFile::ReadSED1376RegsType (SED1376RegsType& sedRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kSED1376Regs, chunk))
	{
		memcpy (&sedRegs, chunk.GetPointer (), EmProxySED1376RegsType::GetSize ());
		return true;
	}

	return false;
}


Bool SessionFile::ReadSED1376Palette (RGBType palette[256])
{
	Chunk	chunk;
	if (fFile.ReadChunk (kSED1376Palette, chunk))
	{
		// Note: "sizeof (palette)" gives 4, not 512.
		int	size = 256 * sizeof (palette[0]);
		memcpy (palette, chunk.GetPointer (), size);
		return true;
	}

	return false;
}


// SessionFile::ReadMediaQRegsType is defined in SessionFile.h


Bool SessionFile::ReadMediaQImage (void* image)
{
	Bool	result = this->ReadChunk (kMediaQImage, image, kGzipCompression);

	return result;
}


Bool SessionFile::ReadMediaQPalette (RGBType palette[256])
{
	Chunk	chunk;
	if (fFile.ReadChunk (kMediaQPalette, chunk))
	{
		// Note: "sizeof (palette)" gives 4, not 512.
		int size = 256 * sizeof (palette[0]);
		memcpy (palette, chunk.GetPointer (), size);
		return true;
	}

	return false;
}


Bool SessionFile::ReadPLDRegsType (HwrJerryPLDType& pldRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kPLDRegs, chunk))
	{
		memcpy (&pldRegs, chunk.GetPointer (), sizeof (pldRegs));
		return true;
	}

	return false;
}


Bool SessionFile::ReadConfiguration (Configuration& cfg)
{
	if (!this->ReadDevice (cfg.fDevice))
		return false;

	if (!this->ReadROMFileReference (cfg.fROMFile))
		return false;

	cfg.fRAMSize = this->GetRAMImageSize ();
	if (cfg.fRAMSize == ChunkFile::kChunkNotFound)
		return false;

	cfg.fRAMSize /= 1024;

	return true;
}


long SessionFile::GetRAMImageSize (void)
{
	long	numBytes;

	Chunk	chunk;
	if (fFile.ReadChunk (kRAMDataTag, chunk) || fFile.ReadChunk (kRLERAMDataTag, chunk))
	{
		EmStreamChunk	s (chunk);
		s >> numBytes;
	}
	else
	{
		numBytes = fFile.FindChunk (kUncompRAMDataTag);
	}

	return numBytes;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteROMFileReference
 *
 * DESCRIPTION:	Write a reference to the ROM file to use for this
 *				session.  For robustness, the reference is written out
 *				in several different ways.  Because the way the file is
 *				later looked up is platform-dependent, most of the work
 *				is off-loaded to the Platform sub-system.
 *
 * PARAMETERS:	f - reference to the EmFileRef for the ROM file.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteROMFileReference (const EmFileRef& f)
{
	// Save a ROM reference using a platform-specific method.

	Platform::WriteROMFileReference (fFile, f);

	// Save the name of the ROM file.

	string	romFileName = f.GetName ();
	fFile.WriteString (SessionFile::kROMNameTag, romFileName);

	// Remember that we were using this ROM file most recently.

	fCfg.fROMFile = f;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteRAMImage
 *
 * DESCRIPTION:	Write the given data as the RAM image for the session
 *				file.  The data is written using the default compression.
 *
 * PARAMETERS:	image - pointer to the data to be written.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 *				size - number of bytes in the image.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteRAMImage (const void* image, uint32 size)
{
	this->WriteChunk (kRAMDataTag, size, image, kGzipCompression);
	fCfg.fRAMSize = size / 1024;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteMetaRAMImage
 *
 * DESCRIPTION:	Write the given data as the MetaRAM image for the session
 *				file.  The data is written using the default compression.
 *
 * PARAMETERS:	image - pointer to the data to be written.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 *				size - number of bytes in the image.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteMetaRAMImage (const void* image, uint32 size)
{
	this->WriteChunk (kMetaRAMDataTag, size, image, kGzipCompression);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteMetaROMImage
 *
 * DESCRIPTION:	Write the given data as the MetaROM image for the session
 *				file.  The data is written using the default compression.
 *
 * PARAMETERS:	image - pointer to the data to be written.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 *				size - number of bytes in the image.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteMetaROMImage (const void* image, uint32 size)
{
	this->WriteChunk (kMetaROMDataTag, size, image, kGzipCompression);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteHwrDBallType
 *
 * DESCRIPTION:	Write the Dragonball hardware registers to the session
 *				file.
 *
 * PARAMETERS:	hwRegs - the DB registers to write to disk.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteHwrDBallType (const HwrM68328Type& hwRegs)
{
	fFile.WriteChunk (kDBRegs, sizeof (hwRegs), &hwRegs);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteHwrDBallEZType
 *
 * DESCRIPTION:	Write the Dragonball EZ hardware registers to the session
 *				file.
 *
 * PARAMETERS:	hwRegs - the DB registers to write to disk.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteHwrDBallEZType (const HwrM68EZ328Type& hwRegs)
{
	fFile.WriteChunk (kDBEZRegs, sizeof (hwRegs), &hwRegs);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteHwrDBallVZType
 *
 * DESCRIPTION:	Write the Dragonball VZ hardware registers to the session
 *				file.
 *
 * PARAMETERS:	hwRegs - the DB registers to write to disk.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteHwrDBallVZType (const HwrM68VZ328Type& hwRegs)
{
	fFile.WriteChunk (kDBVZRegs, sizeof (hwRegs), &hwRegs);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteHwrDBallSZType
 *
 * DESCRIPTION:	Write the Dragonball SZ hardware registers to the session
 *				file.
 *
 * PARAMETERS:	hwRegs - the DB registers to write to disk.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/



/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteDBallRegs
 *
 * DESCRIPTION:	Write the Dragonball CPU registers to the session file.
 *
 * PARAMETERS:	cpuRegs - the registers to write to disk. No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteDBallRegs (const regstruct& cpuRegs)
{
	fFile.WriteChunk (kCPURegs, sizeof (cpuRegs), &cpuRegs);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteDeviceType
 *
 * DESCRIPTION:	Write the id of the device to emulate.
 *
 * PARAMETERS:	v - id of the device to emulate.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteDevice (const EmDevice& v)
{
	fFile.WriteString (kDeviceTypeString, v.GetIDString ());

	// Remember that we were using this device most recently.

	fCfg.fDevice = v;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteBugFixes
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	v - flags of fixed bugs.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteBugFixes (const BugFixes& v)
{
	fFile.WriteInt (kBugsTag, (uint32) v);
}


void SessionFile::WriteSED1375RegsType (const SED1375RegsType& sedRegs)
{
	fFile.WriteChunk (kSED1375Regs, sizeof (sedRegs), &sedRegs);
}

void SessionFile::WriteSED1375Image (const void* image, uint32 size)
{
	this->WriteChunk (kSED1375Image, size, image, kGzipCompression);
}

void SessionFile::WriteSED1375Palette (const uint16 palette[256])
{
	// Note: "sizeof (palette)" gives 4, not 512.
	int	size = 256 * sizeof (palette[0]);
	fFile.WriteChunk (kSED1375Palette, size, palette);
}

void SessionFile::WriteSED1376RegsType (const SED1376RegsType& sedRegs)
{
	fFile.WriteChunk (kSED1376Regs, EmProxySED1376RegsType::GetSize (), &sedRegs);
}

void SessionFile::WriteSED1376Palette (const RGBType palette[256])
{
	// Note: "sizeof (palette)" gives 4, not 512.
	int	size = 256 * sizeof (palette[0]);
	fFile.WriteChunk (kSED1376Palette, size, palette);
}

// SessionFile::WriteMediaQRegsType is defined in SessionFile.h

void SessionFile::WriteMediaQImage (const void* image, uint32 size)
{
	this->WriteChunk (kMediaQImage, size, image, kGzipCompression);
}

void SessionFile::WriteMediaQPalette (const RGBType palette[256])
{
	// Note: "sizeof (palette)" gives 4, not 512.
	int size = 256 * sizeof (palette[0]);
	fFile.WriteChunk (kMediaQPalette, size, palette);
}

void SessionFile::WritePLDRegsType (const HwrJerryPLDType& pldRegs)
{
	fFile.WriteChunk (kPLDRegs, sizeof (pldRegs), &pldRegs);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::SetCanReload
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void SessionFile::SetCanReload (Bool val)
{
	fCanReload = val;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::GetCanReload
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

Bool SessionFile::GetCanReload (void)
{
	return fCanReload;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::FixBug
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void SessionFile::FixBug (BugFix val)
{
	fBugFixes |= val;
	fChangedBugFixes = true;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::IncludesBugFix
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

Bool SessionFile::IncludesBugFix (BugFix val)
{
	if (!fReadBugFixes)
	{
		this->ReadBugFixes (fBugFixes);
		fReadBugFixes = true;
	}

	return (fBugFixes & val) != 0;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadChunk
 *
 * DESCRIPTION:	Read an arbitrary chunk of data, utilizing the given
 *				type of compression, returning a pointer to the read
 *				data and the size of the data.
 *
 * PARAMETERS:	tag - marker identifying the data to be read.
 *
 *				size - reference to the integer to receive the number
 *					of bytes in the data.  This size may not be the same
 *					as the size of the chunk if the chunk is compressed.
 *
 *				image - reference to the pointer to receive the address
 *					of the read data.
 *
 *				compType - type of compression used for this chunk.  The
 *					caller is expected to know what kind of compression
 *					is being used; the compression type is not stored
 *					along with the chunk.
 *
 * RETURNED:	True if the image was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadChunk (ChunkFile::Tag tag, void* image,
							 CompressionType compType)
{
	// Get the size of the chunk.

	long	chunkSize = fFile.FindChunk (tag);
	if (chunkSize == ChunkFile::kChunkNotFound)
	{
		return false;
	}

	if (chunkSize)
	{
		// If no compression is being used, just read the data.

		if (compType == kNoCompression)
		{
			fFile.ReadChunk (chunkSize, image);
		}

		// The data is compressed.

		else
		{
			// Use the chunk size to create a buffer.

			StMemory	packedImage (chunkSize);

			// Read the chunk into memory.

			fFile.ReadChunk (chunkSize, packedImage.Get ());

			// The size of the unpacked image is stored as the first 4 bytes
			// of the chunk.

			long		unpackedSize = *(long*) packedImage.Get ();
			Canonical (unpackedSize);

			// Get pointers to the source (packed) data and
			// destination (unpacked) data.

			void*	src = packedImage.Get () + sizeof (long);
			void*	dest = image;

			// Decompress the data into the dest buffer.

			if (compType == kGzipCompression)
				::GzipDecode (&src, &dest, chunkSize - sizeof (long), unpackedSize);
			else
				::RunLengthDecode (&src, &dest, chunkSize - sizeof (long), unpackedSize);
		}
	}
	
	return true;
}


Bool SessionFile::ReadChunk (ChunkFile::Tag tag, Chunk& chunk,
							 CompressionType compType)
{
	// Get the size of the chunk.

	long	chunkSize = fFile.FindChunk (tag);
	if (chunkSize == ChunkFile::kChunkNotFound)
	{
		return false;
	}

	if (chunkSize)
	{
		// If no compression is being used, just read the data.

		if (compType == kNoCompression)
		{
			chunk.SetLength (chunkSize);
			fFile.ReadChunk (chunkSize, chunk.GetPointer ());
		}

		// The data is compressed.

		else
		{
			// Use the chunk size to create a buffer.

			StMemory	packedImage (chunkSize);

			// Read the chunk into memory.

			fFile.ReadChunk (chunkSize, packedImage.Get ());

			// The size of the unpacked image is stored as the first 4 bytes
			// of the chunk.

			long		unpackedSize = *(long*) packedImage.Get ();
			Canonical (unpackedSize);

			chunk.SetLength (unpackedSize);

			// Get pointers to the source (packed) data and
			// destination (unpacked) data.

			void*	src = packedImage.Get () + sizeof (long);
			void*	dest = chunk.GetPointer ();

			// Decompress the data into the dest buffer.

			if (compType == kGzipCompression)
				::GzipDecode (&src, &dest, chunkSize - sizeof (long), unpackedSize);
			else
				::RunLengthDecode (&src, &dest, chunkSize - sizeof (long), unpackedSize);
		}
	}

	return true;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteChunk
 *
 * DESCRIPTION:	Write an arbitrary chunk of data, utilizing the given
 *				type of compression.
 *
 * PARAMETERS:	tag - marker used to later retrieve the data.
 *
 *				size - number of bytes in the image to write.
 *
 *				image - pointer to the image to write.
 *
 *				compType - type of compression to use.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteChunk (ChunkFile::Tag tag, uint32 size,
				const void* image, CompressionType compType)
{
	// No compression to be used; just write the data out as-is.

	if (compType == kNoCompression)
	{
		fFile.WriteChunk (tag, size, image);
	}

	// Use some form of compression.

	else
	{
		// Get the worst-case size for the compressed data.

		long		worstPackedSize = sizeof (long) +
						((compType == kGzipCompression)
							? ::GzipWorstSize (size)
							: ::RunLengthWorstSize (size));

		// Create a new buffer to hold the compressed data.

		StMemory	packedImage (worstPackedSize);

		// Write out the uncompressed size of the data as the first
		// 4 bytes of the chunk.

		Canonical (size);
		*(long*) packedImage.Get () = size;
		Canonical (size);

		// Get pointers to the source (unpacked) data and
		// destination (packed) data.

		void*	src = (void*) image;
		void*	dest = packedImage.Get () + sizeof (long);

		// Compress the data.

		if (compType == kGzipCompression)
			::GzipEncode (&src, &dest, size, worstPackedSize);
		else
			::RunLengthEncode (&src, &dest, size, worstPackedSize);

		// Calculate the compressed size of the data.

		long	packedSize = (char*) dest - (char*) packedImage;

		// Write the compressed data to the file.

		fFile.WriteChunk (tag, packedSize, packedImage.Get ());
	}
}

void SessionFile::WriteChunk (ChunkFile::Tag tag, const Chunk& chunk, CompressionType compType)
{
	this->WriteChunk (tag, chunk.GetLength (), chunk.GetPointer (), compType);
}