File: ROMStubs.cpp

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (1218 lines) | stat: -rw-r--r-- 29,647 bytes parent folder | download
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
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#include "EmulatorCommon.h"
#include "ROMStubs.h"

#include "ATraps.h"				// ATrap
#include "Byteswapping.h"		// Canonical
#include "Logging.h"
#include "Miscellaneous.h"		// StMemoryMapper
#include "UAE_Utils.h"			// uae_memmove


// (I actually tried using function templates here, but they ended up adding
//  more code, instead of saving code by coalescing identical functions like
//  I'd hoped.)

#define PushParm(p)	PushParameter (theTrap, p)

inline void PushParameter (ATrap& theTrap, uae_u32 p)	{ theTrap.PushLong (p); }
inline void PushParameter (ATrap& theTrap, uae_s32 p)	{ theTrap.PushLong (p); }
inline void PushParameter (ATrap& theTrap, uae_u16 p)	{ theTrap.PushWord (p); }
inline void PushParameter (ATrap& theTrap, uae_s16 p)	{ theTrap.PushWord (p); }
inline void PushParameter (ATrap& theTrap, uae_u8 p)	{ theTrap.PushByte (p); }
inline void PushParameter (ATrap& theTrap, uae_s8 p)	{ theTrap.PushByte (p); }

template <typename T>
inline void PushParameter (ATrap& theTrap, const T* p)	{ theTrap.PushLong ((uae_u32) p); }

#define CallROM0(trapWord)				\
	ATrap theTrap;						\
	theTrap.Call (trapWord)

#define CallROM1(trapWord, p1)			\
	ATrap theTrap;						\
	PushParm(p1);						\
	theTrap.Call (trapWord)

#define CallROM2(trapWord, p1, p2)		\
	ATrap theTrap;						\
	PushParm(p2);						\
	PushParm(p1);						\
	theTrap.Call (trapWord)

#define CallROM3(trapWord, p1, p2, p3)	\
	ATrap theTrap;						\
	PushParm(p3);						\
	PushParm(p2);						\
	PushParm(p1);						\
	theTrap.Call (trapWord)

#define CallROM4(trapWord, p1, p2, p3, p4)	\
	ATrap theTrap;						\
	PushParm(p4);						\
	PushParm(p3);						\
	PushParm(p2);						\
	PushParm(p1);						\
	theTrap.Call (trapWord)

#define CallROM5(trapWord, p1, p2, p3, p4, p5)	\
	ATrap theTrap;						\
	PushParm(p5);						\
	PushParm(p4);						\
	PushParm(p3);						\
	PushParm(p2);						\
	PushParm(p1);						\
	theTrap.Call (trapWord)

#define CallROM6(trapWord, p1, p2, p3, p4, p5, p6)	\
	ATrap theTrap;						\
	PushParm(p6);						\
	PushParm(p5);						\
	PushParm(p4);						\
	PushParm(p3);						\
	PushParm(p2);						\
	PushParm(p1);						\
	theTrap.Call (trapWord)

#define CallROM7(trapWord, p1, p2, p3, p4, p5, p6, p7)	\
	ATrap theTrap;						\
	PushParm(p7);						\
	PushParm(p6);						\
	PushParm(p5);						\
	PushParm(p4);						\
	PushParm(p3);						\
	PushParm(p2);						\
	PushParm(p1);						\
	theTrap.Call (trapWord)

#define CallROM13(trapWord, p1, p2, p3, p4, p5, p6, p7, p8, p9, pA, pB, pC, pD)	\
	ATrap theTrap;						\
	PushParm(pD);						\
	PushParm(pC);						\
	PushParm(pB);						\
	PushParm(pA);						\
	PushParm(p9);						\
	PushParm(p8);						\
	PushParm(p7);						\
	PushParm(p6);						\
	PushParm(p5);						\
	PushParm(p4);						\
	PushParm(p3);						\
	PushParm(p2);						\
	PushParm(p1);						\
	theTrap.Call (trapWord)

#define CallIntl1(selector, p1)			\
	ATrap theTrap;						\
	PushParm(p1);						\
	theTrap.SetNewDReg (2, selector);	\
	theTrap.Call (sysTrapIntlDispatch)

#define CallIntl3(selector, p1, p2, p3)	\
	ATrap theTrap;						\
	PushParm(p3);						\
	PushParm(p2);						\
	PushParm(p1);						\
	theTrap.SetNewDReg (2, selector);	\
	theTrap.Call (sysTrapIntlDispatch)

#define CallIntl4(selector, p1, p2, p3, p4)	\
	ATrap theTrap;						\
	PushParm(p4);						\
	PushParm(p3);						\
	PushParm(p2);						\
	PushParm(p1);						\
	theTrap.SetNewDReg (2, selector);	\
	theTrap.Call (sysTrapIntlDispatch)


// ---------------------------------------------------------------------------
//		 Canonical_If_Not_Null
// ---------------------------------------------------------------------------
// Pointer parameters often need a little extra handling.  If they are not
// NULL, they generally point to either stack or heap buffers.  If they're on
// the stack, then on Windows they're in little-endian format and need to be
// converted into big-endian format.  Fortunately, we don't have to worry
// about the goofy word-swapping done for ROM and RAM memory, as the memory
// accessors that deal with data on the stack doesn't do any of that.
//
// For now, we don't actually check to see if data is on the stack or not.
// We just use this function on pointers that we _expect_ point to
// stack-resident data.  So far we haven't run into any problems with that
// approach.

template <class T>
inline void Canonical_If_Not_Null (T* p)
{
	if (p)
		Canonical (*p);
}


// ---------------------------------------------------------------------------
//		 Data Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

Err DlkDispatchRequest (DlkServerSessionPtr sessP)
{
	CallROM1 (sysTrapDlkDispatchRequest, sessP);

	return (Err) theTrap.GetD0 ();
}

Err DlkGetSyncInfo (ULongPtr succSyncDateP, ULongPtr lastSyncDateP,
			DlkSyncStateType* syncStateP, CharPtr nameBufP,
			CharPtr logBufP, ULongPtr logLenP)
{
	CallROM6 (sysTrapDlkGetSyncInfo, succSyncDateP, lastSyncDateP, syncStateP,
				nameBufP, logBufP, logLenP);

	Canonical_If_Not_Null (succSyncDateP);
	Canonical_If_Not_Null (lastSyncDateP);
//	Canonical_If_Not_Null (syncStateP);
	Canonical_If_Not_Null (logLenP);

	return (Err) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Data Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

Err DmCloseDatabase (DmOpenRef dbR)
{
	CallROM1 (sysTrapDmCloseDatabase, dbR);

	return (Err) theTrap.GetD0 ();
}

Err DmCreateDatabase (UInt cardNo, const Char * const nameP, 
					ULong creator, ULong type, Boolean resDB)
{
	CallROM5 (sysTrapDmCreateDatabase, cardNo, nameP, creator, type, resDB);

	return (Err) theTrap.GetD0 ();
}

Err DmDatabaseInfo (UInt cardNo, LocalID	dbID, const CharPtr nameP,
					UIntPtr attributesP, UIntPtr versionP, ULongPtr crDateP,
					ULongPtr modDateP, ULongPtr bckUpDateP,
					ULongPtr modNumP, LocalID* appInfoIDP,
					LocalID* sortInfoIDP, ULongPtr typeP,
					ULongPtr creatorP)
{
	// Put the name on the stack so that the emulation memory
	// access routines can sort of the byteswapping.

	char	tempName[dmDBNameLength];

	Canonical_If_Not_Null (attributesP);
	Canonical_If_Not_Null (versionP);
	Canonical_If_Not_Null (crDateP);
	Canonical_If_Not_Null (modDateP);
	Canonical_If_Not_Null (bckUpDateP);
	Canonical_If_Not_Null (modNumP);
	Canonical_If_Not_Null (appInfoIDP);
	Canonical_If_Not_Null (sortInfoIDP);
	Canonical_If_Not_Null (typeP);
	Canonical_If_Not_Null (creatorP);

	CallROM13 (sysTrapDmDatabaseInfo, cardNo, dbID, &tempName[0], attributesP,
				versionP, crDateP, modDateP, bckUpDateP, modNumP, appInfoIDP,
				sortInfoIDP, typeP, creatorP);

	Canonical_If_Not_Null (attributesP);
	Canonical_If_Not_Null (versionP);
	Canonical_If_Not_Null (crDateP);
	Canonical_If_Not_Null (modDateP);
	Canonical_If_Not_Null (bckUpDateP);
	Canonical_If_Not_Null (modNumP);
	Canonical_If_Not_Null (appInfoIDP);
	Canonical_If_Not_Null (sortInfoIDP);
	Canonical_If_Not_Null (typeP);
	Canonical_If_Not_Null (creatorP);

	if (nameP)
	{
		strcpy (nameP, tempName);
	}

	return (Err) theTrap.GetD0 ();
}

Err DmDeleteDatabase (UInt cardNo, LocalID dbID)
{
	CallROM2 (sysTrapDmDeleteDatabase, cardNo, dbID);

	return (Err) theTrap.GetD0 ();
}

LocalID DmFindDatabase (UInt cardNo, const CharPtr nameP)
{
	// Put the name on the stack so that the emulation memory
	// access routines can sort of the byteswapping.

	char	tempName[dmDBNameLength];
	strcpy (tempName, nameP);

	CallROM2 (sysTrapDmFindDatabase, cardNo, &tempName[0]);

	return (LocalID) theTrap.GetD0 ();
}

VoidHand DmGet1Resource (ULong type, Int id)
{
	CallROM2 (sysTrapDmGet1Resource, type, id);

	return (VoidHand) theTrap.GetA0 ();
}

LocalID DmGetDatabase (UInt cardNo, UInt index)
{
	CallROM2 (sysTrapDmGetDatabase, cardNo, index);

	return (LocalID) theTrap.GetD0 ();
}

Err DmGetLastErr (void)
{
	CallROM0 (sysTrapDmGetLastErr);

	return (Err) theTrap.GetD0 ();
}

Err	DmGetNextDatabaseByTypeCreator (Boolean newSearch, DmSearchStatePtr stateInfoP,
			 		ULong type, ULong creator, Boolean onlyLatestVers, 
			 		UIntPtr cardNoP, LocalID* dbIDP)
{
	Canonical_If_Not_Null (cardNoP);
	Canonical_If_Not_Null (dbIDP);

	CallROM7 (sysTrapDmGetNextDatabaseByTypeCreator, newSearch, stateInfoP,
				type, creator, onlyLatestVers, cardNoP, dbIDP);

	Canonical_If_Not_Null (cardNoP);
	Canonical_If_Not_Null (dbIDP);

	return (Err) theTrap.GetD0 ();
}

VoidHand DmGetResource (ULong type, Int id)
{
	CallROM2 (sysTrapDmGetResource, type, id);

	return (VoidHand) theTrap.GetA0 ();
}

VoidHand DmGetResourceIndex (DmOpenRef dbP, Int index)
{
	CallROM2 (sysTrapDmGetResourceIndex, dbP, index);

	return (VoidHand) theTrap.GetA0 ();
}

VoidHand DmNewHandle (DmOpenRef dbR, ULong size)
{
	CallROM2 (sysTrapDmNewHandle, dbR, size);

	return (VoidHand) theTrap.GetA0 ();
}

VoidHand DmNewRecord (DmOpenRef dbR, UIntPtr atP, ULong size)
{
	Canonical_If_Not_Null (atP);

	CallROM3 (sysTrapDmNewRecord, dbR, atP, size);

	Canonical_If_Not_Null (atP);

	return (VoidHand) theTrap.GetA0 ();
}

UInt DmNumDatabases (UInt cardNo)
{
	CallROM1 (sysTrapDmNumDatabases, cardNo);

	return (UInt) theTrap.GetD0 ();
}

VoidHand DmNewResource (DmOpenRef dbR, ULong resType, Int resID, ULong size)
{
	CallROM4 (sysTrapDmNewResource, dbR, resType, resID, size);

	return (VoidHand) theTrap.GetA0 ();
}

UInt DmNumRecords (DmOpenRef dbP)
{
	CallROM1 (sysTrapDmNumRecords, dbP);

	return (UInt) theTrap.GetD0 ();
}

DmOpenRef DmOpenDatabase (UInt cardNo, LocalID dbID, UInt mode)
{
	CallROM3 (sysTrapDmOpenDatabase, cardNo, dbID, mode);

	return (DmOpenRef) theTrap.GetA0 ();
}

Err DmOpenDatabaseInfo (DmOpenRef dbP, LocalID* dbIDP, 
					UIntPtr openCountP, UIntPtr modeP, UIntPtr cardNoP,
					BooleanPtr resDBP)
{
	Canonical_If_Not_Null (dbIDP);
	Canonical_If_Not_Null (openCountP);
	Canonical_If_Not_Null (modeP);
	Canonical_If_Not_Null (cardNoP);
	Canonical_If_Not_Null (resDBP);

	CallROM6 (sysTrapDmOpenDatabaseInfo, dbP, dbIDP, openCountP, modeP, cardNoP, resDBP);

	Canonical_If_Not_Null (dbIDP);
	Canonical_If_Not_Null (openCountP);
	Canonical_If_Not_Null (modeP);
	Canonical_If_Not_Null (cardNoP);
	Canonical_If_Not_Null (resDBP);

	return (Err) theTrap.GetD0 ();
}

Err DmRecordInfo (DmOpenRef dbP, UInt index,
					UIntPtr attrP, ULongPtr uniqueIDP, LocalID* chunkIDP)
{
	Canonical_If_Not_Null (attrP);
	Canonical_If_Not_Null (uniqueIDP);
	Canonical_If_Not_Null (chunkIDP);

	CallROM5 (sysTrapDmRecordInfo, dbP, index, attrP, uniqueIDP, chunkIDP);

	Canonical_If_Not_Null (attrP);
	Canonical_If_Not_Null (uniqueIDP);
	Canonical_If_Not_Null (chunkIDP);

	return (Err) theTrap.GetD0 ();
}

Err DmReleaseRecord (DmOpenRef dbR, UInt index, Boolean dirty)
{
	CallROM3 (sysTrapDmReleaseRecord, dbR, index, dirty);

	return (Err) theTrap.GetD0 ();
}

Err DmReleaseResource (VoidHand resourceH)
{
	CallROM1 (sysTrapDmReleaseResource, resourceH);

	return (Err) theTrap.GetD0 ();
}

Err DmResourceInfo (DmOpenRef dbP, Int index,
					ULongPtr resTypeP, IntPtr resIDP,  
					LocalID* chunkLocalIDP)
{
	Canonical_If_Not_Null (resTypeP);
	Canonical_If_Not_Null (resIDP);
	Canonical_If_Not_Null (chunkLocalIDP);

	CallROM5 (sysTrapDmResourceInfo, dbP, index, resTypeP, resIDP, chunkLocalIDP);

	Canonical_If_Not_Null (resTypeP);
	Canonical_If_Not_Null (resIDP);
	Canonical_If_Not_Null (chunkLocalIDP);

	return (Err) theTrap.GetD0 ();
}

VoidHand DmQueryRecord (DmOpenRef dbP, UInt index)
{
	CallROM2 (sysTrapDmQueryRecord, dbP, index);

	return (VoidHand) theTrap.GetA0 ();
}

Err DmSetDatabaseInfo (UInt cardNo, LocalID dbID, const CharPtr nameP,
					UIntPtr attributesP, UIntPtr versionP, ULongPtr crDateP,
					ULongPtr modDateP, ULongPtr bckUpDateP,
					ULongPtr modNumP, LocalID* appInfoIDP,
					LocalID* sortInfoIDP, ULongPtr typeP,
					ULongPtr creatorP)
{
	// Put the name on the stack so that the emulation memory
	// access routines can sort of the byteswapping.

	CharPtr	nameP2 = nameP;
	char	tempName[dmDBNameLength];
	if (nameP)
	{
		strcpy (tempName, nameP);
		nameP2 = tempName;
	}

	Canonical_If_Not_Null (attributesP);
	Canonical_If_Not_Null (versionP);
	Canonical_If_Not_Null (crDateP);
	Canonical_If_Not_Null (modDateP);
	Canonical_If_Not_Null (bckUpDateP);
	Canonical_If_Not_Null (modNumP);
	Canonical_If_Not_Null (appInfoIDP);
	Canonical_If_Not_Null (sortInfoIDP);
	Canonical_If_Not_Null (typeP);
	Canonical_If_Not_Null (creatorP);

	CallROM13 (sysTrapDmSetDatabaseInfo, cardNo, dbID, nameP2, attributesP, versionP,
		crDateP, modDateP, bckUpDateP, modNumP, appInfoIDP, sortInfoIDP, typeP, creatorP);

	Canonical_If_Not_Null (attributesP);
	Canonical_If_Not_Null (versionP);
	Canonical_If_Not_Null (crDateP);
	Canonical_If_Not_Null (modDateP);
	Canonical_If_Not_Null (bckUpDateP);
	Canonical_If_Not_Null (modNumP);
	Canonical_If_Not_Null (appInfoIDP);
	Canonical_If_Not_Null (sortInfoIDP);
	Canonical_If_Not_Null (typeP);
	Canonical_If_Not_Null (creatorP);

	return (Err) theTrap.GetD0 ();
}

Err DmSetRecordInfo (DmOpenRef dbR, UInt index, UIntPtr attrP, ULongPtr uniqueIDP)
{
	Canonical_If_Not_Null (attrP);
	Canonical_If_Not_Null (uniqueIDP);

	CallROM4 (sysTrapDmSetRecordInfo, dbR, index, attrP, uniqueIDP);

	Canonical_If_Not_Null (attrP);
	Canonical_If_Not_Null (uniqueIDP);

	return (Err) theTrap.GetD0 ();
}

Err DmWrite (VoidPtr recordP, ULong offset, const void * const srcP, ULong bytes)
{
	CallROM4 (sysTrapDmWrite, recordP, offset, srcP, bytes);

	return (Err) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Event Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

Err EvtEnqueueKey (UInt ascii, UInt keycode, UInt modifiers)
{
	CallROM3 (sysTrapEvtEnqueueKey, ascii, keycode, modifiers);

	return (Err) theTrap.GetD0 ();
}

Err EvtEnqueuePenPoint (PointType* ptP)
{
	// Make a copy of the point, as we may be munging it.

	PointType	pt = *ptP;

	// Enqueue the new pen position. We must "reverse" correct it because the
	// Event Manager assumes that all points enqueued are raw digitizer points.

	if (pt.x != -1 || pt.y != -1)
	{
		(void) PenScreenToRaw(&pt);
	}

	// Byteswap it so that the ROM routines get it in the right format.
	// Byteswap it _after_ PenScreenToRaw, as PenScreenToRaw will do its
	// own byteswapping.

	Canonical (pt);

	CallROM1 (sysTrapEvtEnqueuePenPoint, &pt);

	return (Err) theTrap.GetD0 ();
}

PenBtnInfoPtr	EvtGetPenBtnList(UIntPtr numButtons)
{
	Canonical_If_Not_Null (numButtons);

	CallROM1 (sysTrapEvtGetPenBtnList, numButtons);

	Canonical_If_Not_Null (numButtons);

	// This one's tricky; it's returning a pointer to ROM data.  There are
	// no functions to access the fields in the struct, so the caller needs
	// to access those fields directly.  However, not only is the address
	// returned in "emulated space" and not native space, but there are
	// word- and byte-swapping issues involved.  To help out the caller,
	// we'll address those issues here, returning a pointer to a "pre-
	// digested" copy of the struct.

	static PenBtnInfoPtr	buttonListP;

	if (!buttonListP)
	{
		long	buttonListSize = *numButtons * sizeof (PenBtnInfoType);

		buttonListP = (PenBtnInfoPtr) malloc (buttonListSize);
		// !!! Should check for NULL here, but how to handle it?

		// Copy the data, sorting out address and wordswapping issues

		uae_memmove ((void*) buttonListP, theTrap.GetA0 (), buttonListSize);

		// Now sort out the byte-swapping issues.

		for (UInt ii = 0; ii < *numButtons; ++ii)
			Canonical (buttonListP[ii]);
	}

	return buttonListP;
}

Err EvtResetAutoOffTimer (void)
{
	CallROM0 (sysTrapEvtResetAutoOffTimer);

	return (Err) theTrap.GetD0 ();
}

Err EvtWakeup (void)
{
	CallROM0 (sysTrapEvtWakeup);

	return (Err) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Feature Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

Err FtrGet (DWord creator, UInt featureNum, DWordPtr valueP)
{
	Canonical_If_Not_Null (valueP);

	CallROM3 (sysTrapFtrGet, creator, featureNum, valueP);

	Canonical_If_Not_Null (valueP);

	return (Err) theTrap.GetD0 ();
}

Err FtrSet (DWord creator, UInt featureNum, DWord newValue)
{
	CallROM3 (sysTrapFtrSet, creator, featureNum, newValue);

	return (Err) theTrap.GetD0 ();
}


Err	FtrUnregister (UInt32 creator, UInt16 featureNum)
{
	CallROM2 (sysTrapFtrUnregister, creator, featureNum);

	return (Err) theTrap.GetD0 ();
}


// ---------------------------------------------------------------------------
//		 Field Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

void FldGetAttributes (const FieldPtr fld, const FieldAttrPtr attrP)
{
	Canonical_If_Not_Null (attrP);

	CallROM2 (sysTrapFldGetAttributes, fld, attrP);

	Canonical_If_Not_Null (attrP);

//	LogAppendMsg ("attr == 0x%04X", (int) *(short*) attrP);
}

Word FldGetMaxChars (const FieldPtr fld)
{
	CallROM1 (sysTrapFldGetMaxChars, fld);

	return (Word) theTrap.GetD0 ();
}

Word FldGetTextLength (const FieldPtr fld)
{
	CallROM1 (sysTrapFldGetTextLength, fld);

	return (Word) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Font Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

SWord FntLineHeight (void)
{
	CallROM0 (sysTrapFntLineHeight);

	return (SWord) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Form Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

FormPtr FrmGetActiveForm (void)
{
	CallROM0 (sysTrapFrmGetActiveForm);

	return (FormPtr) theTrap.GetA0 ();
}

Word FrmGetFocus (const FormPtr frm)
{
	CallROM1 (sysTrapFrmGetFocus, frm);

	return (Word) theTrap.GetD0 ();
}

Word FrmGetFormId (const FormPtr frm)
{
	CallROM1 (sysTrapFrmGetFormId, frm);

	return (Word) theTrap.GetD0 ();
}

Word FrmGetNumberOfObjects (const FormPtr frm)
{
	CallROM1 (sysTrapFrmGetNumberOfObjects, frm);

	return (Word) theTrap.GetD0 ();
}

void FrmGetObjectBounds (const FormPtr frm, const Word pObjIndex, const RectanglePtr r)
{
	Canonical_If_Not_Null (r);

	CallROM3 (sysTrapFrmGetObjectBounds, frm, pObjIndex, r);

	Canonical_If_Not_Null (r);
}

Word FrmGetObjectId (const FormPtr frm, const Word objIndex)
{
	CallROM2 (sysTrapFrmGetObjectId, frm, objIndex);

	return (Word) theTrap.GetD0 ();
}

VoidPtr FrmGetObjectPtr (const FormPtr frm, const Word objIndex)
{
	CallROM2 (sysTrapFrmGetObjectPtr, frm, objIndex);

	return (VoidPtr) theTrap.GetA0 ();
}

FormObjectKind FrmGetObjectType (const FormPtr frm, const Word objIndex)
{
	CallROM2 (sysTrapFrmGetObjectType, frm, objIndex);

	// Cast to an 8-bit type first.  FormObjectKind is an 8-bit
	// value on 68K machines, but a 32-bit (int) value on other
	// platforms.  If we don't cast to an 8-bit value first, we'll
	// end up with unwanted garbage in the upper 24 bits.

	return (FormObjectKind) (uae_u8) theTrap.GetD0 ();
}

WinHandle FrmGetWindowHandle (const FormPtr frm)
{
	CallROM1 (sysTrapFrmGetWindowHandle, frm);

	return (WinHandle) theTrap.GetA0 ();
}

// ---------------------------------------------------------------------------
//		 Hardware Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

void HwrDisableDataWrites (void)
{
	CallROM0 (sysTrapHwrDisableDataWrites);
}

Boolean HwrEnableDataWrites (void)
{
	CallROM0 (sysTrapHwrEnableDataWrites);

	return (Boolean) theTrap.GetD0 ();
}

DWord HwrMemReadable (VoidPtr addr)
{
	CallROM1 (sysTrapHwrMemReadable, addr);

	return (DWord) theTrap.GetD0 ();
}

DWord HwrMemWritable (VoidPtr addr)
{
	CallROM1 (sysTrapHwrMemWritable, addr);

	return (DWord) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Keyboard Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

ULong KeyHandleInterrupt (Boolean periodic, DWord status)
{
	CallROM2 (sysTrapKeyHandleInterrupt, periodic, status);

	return (ULong) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Memory Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

Err MemChunkFree (VoidPtr chunkDataP)
{
	CallROM1 (sysTrapMemChunkFree, chunkDataP);

	return (Err) theTrap.GetD0 ();
}

UInt MemHandleHeapID (VoidHand h)
{
	CallROM1 (sysTrapMemHandleHeapID, h);

	return (UInt) theTrap.GetD0 ();
}

VoidPtr MemHandleLock (VoidHand h)
{
	CallROM1 (sysTrapMemHandleLock, h);

	return (VoidPtr) theTrap.GetA0 ();
}

ULong MemHandleSize (VoidHand h)
{
	CallROM1 (sysTrapMemHandleSize, h);

	return (ULong) theTrap.GetD0 ();
}

LocalID MemHandleToLocalID (VoidHand h)
{
	CallROM1 (sysTrapMemHandleToLocalID, h);

	return (LocalID) theTrap.GetD0 ();
}

Err MemHandleUnlock (VoidHand h)
{
	CallROM1 (sysTrapMemHandleUnlock, h);

	return (Err) theTrap.GetD0 ();
}

VoidPtr MemHeapPtr (UInt heapID)
{
	CallROM1 (sysTrapMemHeapPtr, heapID);

	return (VoidPtr) theTrap.GetA0 ();
}

LocalIDKind MemLocalIDKind (LocalID local)
{
	CallROM1 (sysTrapMemLocalIDKind, local);

	return (LocalIDKind) theTrap.GetD0 ();
}

VoidPtr MemLocalIDToGlobal (LocalID local, UInt cardNo)
{
	CallROM2 (sysTrapMemLocalIDToGlobal, local, cardNo);

	return (VoidPtr) theTrap.GetA0 ();
}

UInt MemNumCards (void)
{
	CallROM0 (sysTrapMemNumCards);

	return (UInt) theTrap.GetD0 ();
}

UInt MemNumHeaps (UInt cardNo)
{
	CallROM1 (sysTrapMemNumHeaps, cardNo);

	return (UInt) theTrap.GetD0 ();
}

UInt MemNumRAMHeaps (UInt cardNo)
{
	CallROM1 (sysTrapMemNumRAMHeaps, cardNo);

	return (UInt) theTrap.GetD0 ();
}

Err MemNVParams (Boolean set, SysNVParamsPtr paramsP)
{
	Canonical_If_Not_Null (paramsP);

	CallROM2 (sysTrapMemNVParams, set, paramsP);

	Canonical_If_Not_Null (paramsP);

	return (Err) theTrap.GetD0 ();
}

UInt MemPtrFlags (VoidPtr p)
{
	CallROM1 (sysTrapMemPtrFlags, p);

	return (UInt) theTrap.GetD0 ();
}

UInt MemPtrHeapID (VoidPtr p)
{
	CallROM1 (sysTrapMemPtrHeapID, p);

	return (UInt) theTrap.GetD0 ();
}

VoidPtr MemPtrNew (ULong size)
{
	CallROM1 (sysTrapMemPtrNew, size);

	return (VoidPtr) theTrap.GetA0 ();
}

Err MemPtrSetOwner (VoidPtr p, UInt owner)
{
	CallROM2 (sysTrapMemPtrSetOwner, p, owner);

	return (Err) theTrap.GetD0 ();
}

ULong MemPtrSize (VoidPtr p)
{
	CallROM1 (sysTrapMemPtrSize, p);

	return (ULong) theTrap.GetD0 ();
}

Err MemPtrUnlock (VoidPtr p)
{
	CallROM1 (sysTrapMemPtrUnlock, p);

	return (Err) theTrap.GetD0 ();
}

Err MemSemaphoreRelease (Boolean writeAccess)
{
	CallROM1 (sysTrapMemSemaphoreRelease, writeAccess);

	return (Err) theTrap.GetD0 ();
}

Err MemSemaphoreReserve (Boolean writeAccess)
{
	CallROM1 (sysTrapMemSemaphoreReserve, writeAccess);

	return (Err) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Net Library functions
// ---------------------------------------------------------------------------

#pragma mark -

Err NetLibConfigMakeActive (Word refNum, Word configIndex)
{
	CallROM2 (netLibConfigMakeActive, refNum, configIndex);

	return (Err) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Pen Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

Err PenCalibrate (PointType* digTopLeftP, PointType* digBotRightP,
					PointType* scrTopLeftP, PointType* scrBotRightP)
{
	Canonical_If_Not_Null (digTopLeftP);
	Canonical_If_Not_Null (digBotRightP);
	Canonical_If_Not_Null (scrTopLeftP);
	Canonical_If_Not_Null (scrBotRightP);

	CallROM4 (sysTrapPenCalibrate, digTopLeftP, digBotRightP, scrTopLeftP, scrBotRightP);

	Canonical_If_Not_Null (digTopLeftP);
	Canonical_If_Not_Null (digBotRightP);
	Canonical_If_Not_Null (scrTopLeftP);
	Canonical_If_Not_Null (scrBotRightP);

	return (Err) theTrap.GetD0 ();
}

Err	 PenRawToScreen(PointType* penP)
{
	Canonical_If_Not_Null (penP);

	CallROM1 (sysTrapPenRawToScreen, penP);

	Canonical_If_Not_Null (penP);

	return (Err) theTrap.GetD0 ();
}

Err PenScreenToRaw (PointType* penP)
{
	Canonical_If_Not_Null (penP);

	CallROM1 (sysTrapPenScreenToRaw, penP);

	Canonical_If_Not_Null (penP);

	return (Err) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Peferences Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

DmOpenRef PrefOpenPreferenceDBV10 (void)
{
	CallROM0 (sysTrapPrefOpenPreferenceDBV10);

	return (DmOpenRef) theTrap.GetA0 ();
}

DmOpenRef PrefOpenPreferenceDB (Boolean saved)
{
	CallROM1 (sysTrapPrefOpenPreferenceDB, saved);

	return (DmOpenRef) theTrap.GetA0 ();
}

void PrefSetPreference (SystemPreferencesChoice choice, DWord value)
{
	// Coerce "choice" to a 1-byte value to match the way the 68K
	// code-generator determines an enum's size.

	CallROM2 (sysTrapPrefSetPreference, (uae_u8) choice, value);
}

// ---------------------------------------------------------------------------
//		 System Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

Err	SysCurAppDatabase (UIntPtr cardNoP, LocalID* dbIDP)
{
	Canonical_If_Not_Null (cardNoP);
	Canonical_If_Not_Null (dbIDP);

	CallROM2 (sysTrapSysCurAppDatabase, cardNoP, dbIDP);

	Canonical_If_Not_Null (cardNoP);
	Canonical_If_Not_Null (dbIDP);

	return (Err) theTrap.GetD0 ();
}

SysAppInfoPtr SysCurAppInfoPV20 (void)
{
	CallROM0 (sysTrapSysCurAppInfoPV20);

	return (SysAppInfoPtr) theTrap.GetA0 ();
}

SysAppInfoPtr SysGetAppInfo (SysAppInfoPtr *uiAppPP, SysAppInfoPtr *actionCodeAppPP)
{
	Canonical_If_Not_Null ((void**) uiAppPP);
	Canonical_If_Not_Null ((void**) actionCodeAppPP);

	CallROM2 (sysTrapSysGetAppInfo, uiAppPP, actionCodeAppPP);

	Canonical_If_Not_Null ((void**) uiAppPP);
	Canonical_If_Not_Null ((void**) actionCodeAppPP);

	return (SysAppInfoPtr) theTrap.GetA0 ();
}

Err SysKernelInfo (VoidPtr p)
{
	SysKernelInfoPtr	infoP = (SysKernelInfoPtr) p;

	Canonical_If_Not_Null (infoP);

	CallROM1 (sysTrapSysKernelInfo, p);

	Canonical_If_Not_Null (infoP);

	return (Err) theTrap.GetD0 ();
}


UInt SysSetAutoOffTime (UInt seconds)
{
	CallROM1 (sysTrapSysSetAutoOffTime, seconds);

	return (UInt) theTrap.GetD0 ();
}

Err SysUIAppSwitch (UInt cardNo, LocalID dbID, Word cmd, Ptr cmdPBP)
{	
	CallROM4 (sysTrapSysUIAppSwitch, cardNo, dbID, cmd, cmdPBP);

	return (Err) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Table Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

FieldPtr TblGetCurrentField (const TablePtr table)
{
	CallROM1 (sysTrapTblGetCurrentField, table);

	return (FieldPtr) theTrap.GetA0 ();
}

// ---------------------------------------------------------------------------
//		 Text Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

Byte TxtByteAttr(Byte inByte)
{
	CallIntl1 (intlTxtByteAttr, inByte);

	return (Byte) theTrap.GetD0 ();
}

Word TxtCharBounds (ConstCharPtr inText, ULong inOffset, ULongPtr outStart, ULongPtr outEnd)
{
	Canonical_If_Not_Null (outStart);
	Canonical_If_Not_Null (outEnd);

	StMemoryMapper	mapper (inText, inOffset + 4);

	CallIntl4 (intlTxtCharBounds, inText, inOffset, outStart, outEnd);

	Canonical_If_Not_Null (outStart);
	Canonical_If_Not_Null (outEnd);

	return (Word) theTrap.GetD0 ();
}

Word TxtGetNextChar (ConstCharPtr inText, ULong inOffset, WCharPtr outChar)
{
	Canonical_If_Not_Null (outChar);

	StMemoryMapper	mapper (inText, inOffset + 4);

	CallIntl3 (intlTxtGetNextChar, inText, inOffset, outChar);

	Canonical_If_Not_Null (outChar);

	return (Word) theTrap.GetD0 ();
}

// ---------------------------------------------------------------------------
//		 Window Manager functions
// ---------------------------------------------------------------------------

#pragma mark -

WinHandle WinGetActiveWindow (void)
{
	CallROM0 (sysTrapWinGetActiveWindow);

	return (WinHandle) theTrap.GetA0 ();
}

void WinGetDisplayExtent (SWordPtr extentX, SWordPtr extentY)
{
	Canonical_If_Not_Null (extentX);
	Canonical_If_Not_Null (extentY);

	CallROM2 (sysTrapWinGetDisplayExtent, extentX, extentY);

	Canonical_If_Not_Null (extentY);
	Canonical_If_Not_Null (extentX);
}

WinHandle WinGetFirstWindow (void)
{
	CallROM0 (sysTrapWinGetFirstWindow);

	return (WinHandle) theTrap.GetA0 ();
}

void WinGetWindowBounds (RectanglePtr r)
{
	Canonical_If_Not_Null (r);

	CallROM1 (sysTrapWinGetWindowBounds, r);

	Canonical_If_Not_Null (r);
}

WinHandle WinSetDrawWindow (WinHandle winHandle)
{
	CallROM1 (sysTrapWinSetDrawWindow, winHandle);

	return (WinHandle) theTrap.GetA0 ();
}

void WinWindowToDisplayPt (SWordPtr extentX, SWordPtr extentY)
{
	Canonical_If_Not_Null (extentX);
	Canonical_If_Not_Null (extentY);

	CallROM2 (sysTrapWinWindowToDisplayPt, extentX, extentY);

	Canonical_If_Not_Null (extentX);
	Canonical_If_Not_Null (extentY);
}