summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/memory.h
blob: 44afa1ef3409cd8cefb139e47b098814d00fb4ab (plain) (blame)
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
/***************************************************************************

    memory.h

    Functions which handle the CPU memory accesses.

    Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team.
    Visit http://mamedev.org for licensing and usage restrictions.

***************************************************************************/

#pragma once

#ifndef __MEMORY_H__
#define __MEMORY_H__

#include "mamecore.h"



/***************************************************************************
    PARAMETERS
***************************************************************************/

#ifdef MAME_DEBUG
#define CPUREADOP_SAFETY_NONE		0
#define CPUREADOP_SAFETY_PARTIAL	0
#define CPUREADOP_SAFETY_FULL		1
#elif defined(MESS)
#define CPUREADOP_SAFETY_NONE		0
#define CPUREADOP_SAFETY_PARTIAL	1
#define CPUREADOP_SAFETY_FULL		0
#else
#define CPUREADOP_SAFETY_NONE		1
#define CPUREADOP_SAFETY_PARTIAL	0
#define CPUREADOP_SAFETY_FULL		0
#endif



/***************************************************************************
    BASIC TYPE DEFINITIONS
***************************************************************************/

/* ----- typedefs for data and offset types ----- */
typedef UINT32			offs_t;

/* ----- typedefs for the various common data access handlers ----- */
typedef UINT8			(*read8_handler)  (ATTR_UNUSED offs_t offset);
typedef void			(*write8_handler) (ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT8 data);
typedef UINT16			(*read16_handler) (ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT16 mem_mask);
typedef void			(*write16_handler)(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT16 data, ATTR_UNUSED UINT16 mem_mask);
typedef UINT32			(*read32_handler) (ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT32 mem_mask);
typedef void			(*write32_handler)(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT32 data, ATTR_UNUSED UINT32 mem_mask);
typedef UINT64			(*read64_handler) (ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT64 mem_mask);
typedef void			(*write64_handler)(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT64 data, ATTR_UNUSED UINT64 mem_mask);
typedef offs_t			(*opbase_handler) (ATTR_UNUSED offs_t address);

/* ----- this struct contains pointers to the live read/write routines ----- */
struct _data_accessors
{
	UINT8			(*read_byte)(offs_t offset);
	UINT16			(*read_word)(offs_t offset);
	UINT32			(*read_dword)(offs_t offset);
	UINT64			(*read_qword)(offs_t offset);

	void			(*write_byte)(offs_t offset, UINT8 data);
	void			(*write_word)(offs_t offset, UINT16 data);
	void			(*write_dword)(offs_t offset, UINT32 data);
	void			(*write_qword)(offs_t offset, UINT64 data);
};
typedef struct _data_accessors data_accessors;



/***************************************************************************
    BASIC MACROS
***************************************************************************/

/* ----- macros for declaring the various common data access handlers ----- */
#define READ8_HANDLER(name) 	UINT8  name(ATTR_UNUSED offs_t offset)
#define WRITE8_HANDLER(name) 	void   name(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT8 data)
#define READ16_HANDLER(name)	UINT16 name(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT16 mem_mask)
#define WRITE16_HANDLER(name)	void   name(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT16 data, ATTR_UNUSED UINT16 mem_mask)
#define READ32_HANDLER(name)	UINT32 name(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT32 mem_mask)
#define WRITE32_HANDLER(name)	void   name(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT32 data, ATTR_UNUSED UINT32 mem_mask)
#define READ64_HANDLER(name)	UINT64 name(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT64 mem_mask)
#define WRITE64_HANDLER(name)	void   name(ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT64 data, ATTR_UNUSED UINT64 mem_mask)
#define OPBASE_HANDLER(name)	offs_t name(ATTR_UNUSED offs_t address)

/* ----- macros for accessing bytes and words within larger chunks ----- */
#ifdef LSB_FIRST
	#define BYTE_XOR_BE(a)  	((a) ^ 1)				/* read/write a byte to a 16-bit space */
	#define BYTE_XOR_LE(a)  	(a)
	#define BYTE4_XOR_BE(a) 	((a) ^ 3)				/* read/write a byte to a 32-bit space */
	#define BYTE4_XOR_LE(a) 	(a)
	#define WORD_XOR_BE(a)  	((a) ^ 2)				/* read/write a word to a 32-bit space */
	#define WORD_XOR_LE(a)  	(a)
	#define BYTE8_XOR_BE(a) 	((a) ^ 7)				/* read/write a byte to a 64-bit space */
	#define BYTE8_XOR_LE(a) 	(a)
	#define WORD2_XOR_BE(a)  	((a) ^ 6)				/* read/write a word to a 64-bit space */
	#define WORD2_XOR_LE(a)  	(a)
	#define DWORD_XOR_BE(a)  	((a) ^ 4)				/* read/write a dword to a 64-bit space */
	#define DWORD_XOR_LE(a)  	(a)
#else
	#define BYTE_XOR_BE(a)  	(a)
	#define BYTE_XOR_LE(a)  	((a) ^ 1)				/* read/write a byte to a 16-bit space */
	#define BYTE4_XOR_BE(a) 	(a)
	#define BYTE4_XOR_LE(a) 	((a) ^ 3)				/* read/write a byte to a 32-bit space */
	#define WORD_XOR_BE(a)  	(a)
	#define WORD_XOR_LE(a)  	((a) ^ 2)				/* read/write a word to a 32-bit space */
	#define BYTE8_XOR_BE(a) 	(a)
	#define BYTE8_XOR_LE(a) 	((a) ^ 7)				/* read/write a byte to a 64-bit space */
	#define WORD2_XOR_BE(a)  	(a)
	#define WORD2_XOR_LE(a)  	((a) ^ 6)				/* read/write a word to a 64-bit space */
	#define DWORD_XOR_BE(a)  	(a)
	#define DWORD_XOR_LE(a)  	((a) ^ 4)				/* read/write a dword to a 64-bit space */
#endif



/***************************************************************************
    ADDRESS ARRAY CONSTANTS
****************************************************************************
    These apply to values in the array of read/write handlers that is
    declared within each driver.
***************************************************************************/

/* ----- definitions for the flags in the address maps ----- */
#define AM_FLAGS_EXTENDED		0x01					/* this is an extended entry with the below flags in the start field */
#define AM_FLAGS_MATCH_MASK		0x02					/* this entry should have the start/end pair treated as a match/mask pair */
#define AM_FLAGS_END			0x04					/* this is the terminating entry in the array */

/* ----- definitions for the extended flags in the address maps ----- */
#define AMEF_SPECIFIES_SPACE	0x00000001				/* set if the address space is specified */
#define AMEF_SPECIFIES_ABITS	0x00000002				/* set if the number of address space bits is specified */
#define AMEF_SPECIFIES_DBITS	0x00000004				/* set if the databus width is specified */
#define AMEF_SPECIFIES_UNMAP	0x00000008				/* set if the unmap value is specified */

/* ----- definitions for specifying the address space in the extended flags ----- */
#define AMEF_SPACE_SHIFT		8						/* shift to get at the address space */
#define AMEF_SPACE_MASK			(0x0f << AMEF_SPACE_SHIFT) /* mask to get at the address space */
#define AMEF_SPACE(x)			(((x) << AMEF_SPACE_SHIFT) | AMEF_SPECIFIES_SPACE) /* specifies a given address space */

/* ----- definitions for specifying the address bus width in the extended flags ----- */
#define AMEF_ABITS_SHIFT		12						/* shift to get the address bits count */
#define AMEF_ABITS_MASK			(0x3f << AMEF_ABITS_SHIFT) /* mask to get at the address bits count */
#define AMEF_ABITS(n)			(((n) << AMEF_ABITS_SHIFT) | AMEF_SPECIFIES_ABITS) /* specifies a given number of address  */

/* ----- definitions for specifying the data bus width in the extended flags ----- */
#define AMEF_DBITS_SHIFT		18						/* shift to get the data bits count */
#define AMEF_DBITS_MASK			(0x07 << AMEF_DBITS_SHIFT) /* mask to get at the data bits count */
#define AMEF_DBITS(n)			((((n)/8-1) << AMEF_DBITS_SHIFT) | AMEF_SPECIFIES_DBITS) /* specifies a given data bus width */

/* ----- definitions for specifying the unmap value in the extended flags ----- */
#define AMEF_UNMAP_SHIFT		21						/* shift to get the unmap value */
#define AMEF_UNMAP_MASK			(1 << AMEF_UNMAP_SHIFT)	/* mask to get at the unmap value */
#define AMEF_UNMAP(x)			(((x) << AMEF_UNMAP_SHIFT) | AMEF_SPECIFIES_UNMAP) /* specifies a given unmap value */

/* ----- static data access handler constants ----- */
enum
{
	STATIC_INVALID = 0,	/* invalid - should never be used */
	STATIC_BANK1,		/* banked memory */
	STATIC_BANK2,
	STATIC_BANK3,
	STATIC_BANK4,
	STATIC_BANK5,
	STATIC_BANK6,
	STATIC_BANK7,
	STATIC_BANK8,
	STATIC_BANK9,
	STATIC_BANK10,
	STATIC_BANK11,
	STATIC_BANK12,
	STATIC_BANK13,
	STATIC_BANK14,
	STATIC_BANK15,
	STATIC_BANK16,
	STATIC_BANK17,
	STATIC_BANK18,
	STATIC_BANK19,
	STATIC_BANK20,
	STATIC_BANK21,
	STATIC_BANK22,
	STATIC_BANK23,
	STATIC_BANK24,
	STATIC_BANK25,
	STATIC_BANK26,
	STATIC_BANK27,
	STATIC_BANK28,
	STATIC_BANK29,
	STATIC_BANK30,
	STATIC_BANK31,
	STATIC_BANK32,
/* entries 33-67 are reserved for dynamically allocated internal banks */
	STATIC_RAM = 68,	/* RAM - standard reads/writes */
	STATIC_ROM,		/* ROM - standard reads, no writes */
	STATIC_NOP,
	STATIC_UNMAP,		/* unmapped - all unmapped memory goes here */
	STATIC_COUNT		/* total number of static handlers */
};

/* ----- banking constants ----- */
#define MAX_BANKS				66						/* maximum number of banks */
#define MAX_BANK_ENTRIES		256						/* maximum number of possible bank values */
#define MAX_EXPLICIT_BANKS		32						/* maximum number of explicitly-defined banks */
#define STATIC_BANKMAX			(STATIC_RAM - 1)		/* handler constant of last bank */



/***************************************************************************
    STATIC ENTRY CONSTANTS
****************************************************************************
    The first 32 entries in the address lookup table are reserved for
    "static" handlers. These are internal handlers for RAM, ROM, banks,
    and unmapped areas in the address space. The following definitions
    are the properly-casted versions of the STATIC_ constants above.
***************************************************************************/

/* 8-bit reads */
#define MRA8_BANK1				((read8_handler)STATIC_BANK1)
#define MRA8_BANK2				((read8_handler)STATIC_BANK2)
#define MRA8_BANK3				((read8_handler)STATIC_BANK3)
#define MRA8_BANK4				((read8_handler)STATIC_BANK4)
#define MRA8_BANK5				((read8_handler)STATIC_BANK5)
#define MRA8_BANK6				((read8_handler)STATIC_BANK6)
#define MRA8_BANK7				((read8_handler)STATIC_BANK7)
#define MRA8_BANK8				((read8_handler)STATIC_BANK8)
#define MRA8_BANK9				((read8_handler)STATIC_BANK9)
#define MRA8_BANK10				((read8_handler)STATIC_BANK10)
#define MRA8_BANK11				((read8_handler)STATIC_BANK11)
#define MRA8_BANK12				((read8_handler)STATIC_BANK12)
#define MRA8_BANK13				((read8_handler)STATIC_BANK13)
#define MRA8_BANK14				((read8_handler)STATIC_BANK14)
#define MRA8_BANK15				((read8_handler)STATIC_BANK15)
#define MRA8_BANK16				((read8_handler)STATIC_BANK16)
#define MRA8_BANK17				((read8_handler)STATIC_BANK17)
#define MRA8_BANK18				((read8_handler)STATIC_BANK18)
#define MRA8_BANK19				((read8_handler)STATIC_BANK19)
#define MRA8_BANK20				((read8_handler)STATIC_BANK20)
#define MRA8_BANK21				((read8_handler)STATIC_BANK21)
#define MRA8_BANK22				((read8_handler)STATIC_BANK22)
#define MRA8_BANK23				((read8_handler)STATIC_BANK23)
#define MRA8_BANK24				((read8_handler)STATIC_BANK24)
#define MRA8_BANK25				((read8_handler)STATIC_BANK25)
#define MRA8_BANK26				((read8_handler)STATIC_BANK26)
#define MRA8_BANK27				((read8_handler)STATIC_BANK27)
#define MRA8_BANK28				((read8_handler)STATIC_BANK28)
#define MRA8_BANK29				((read8_handler)STATIC_BANK29)
#define MRA8_BANK30				((read8_handler)STATIC_BANK30)
#define MRA8_BANK31				((read8_handler)STATIC_BANK31)
#define MRA8_BANK32				((read8_handler)STATIC_BANK32)
#define MRA8_RAM				((read8_handler)STATIC_RAM)
#define MRA8_ROM				((read8_handler)STATIC_ROM)
#define MRA8_NOP				((read8_handler)STATIC_NOP)
#define MRA8_UNMAP				((read8_handler)STATIC_UNMAP)

/* 8-bit writes */
#define MWA8_BANK1				((write8_handler)STATIC_BANK1)
#define MWA8_BANK2				((write8_handler)STATIC_BANK2)
#define MWA8_BANK3				((write8_handler)STATIC_BANK3)
#define MWA8_BANK4				((write8_handler)STATIC_BANK4)
#define MWA8_BANK5				((write8_handler)STATIC_BANK5)
#define MWA8_BANK6				((write8_handler)STATIC_BANK6)
#define MWA8_BANK7				((write8_handler)STATIC_BANK7)
#define MWA8_BANK8				((write8_handler)STATIC_BANK8)
#define MWA8_BANK9				((write8_handler)STATIC_BANK9)
#define MWA8_BANK10				((write8_handler)STATIC_BANK10)
#define MWA8_BANK11				((write8_handler)STATIC_BANK11)
#define MWA8_BANK12				((write8_handler)STATIC_BANK12)
#define MWA8_BANK13				((write8_handler)STATIC_BANK13)
#define MWA8_BANK14				((write8_handler)STATIC_BANK14)
#define MWA8_BANK15				((write8_handler)STATIC_BANK15)
#define MWA8_BANK16				((write8_handler)STATIC_BANK16)
#define MWA8_BANK17				((write8_handler)STATIC_BANK17)
#define MWA8_BANK18				((write8_handler)STATIC_BANK18)
#define MWA8_BANK19				((write8_handler)STATIC_BANK19)
#define MWA8_BANK20				((write8_handler)STATIC_BANK20)
#define MWA8_BANK21				((write8_handler)STATIC_BANK21)
#define MWA8_BANK22				((write8_handler)STATIC_BANK22)
#define MWA8_BANK23				((write8_handler)STATIC_BANK23)
#define MWA8_BANK24				((write8_handler)STATIC_BANK24)
#define MWA8_BANK25				((write8_handler)STATIC_BANK25)
#define MWA8_BANK26				((write8_handler)STATIC_BANK26)
#define MWA8_BANK27				((write8_handler)STATIC_BANK27)
#define MWA8_BANK28				((write8_handler)STATIC_BANK28)
#define MWA8_BANK29				((write8_handler)STATIC_BANK29)
#define MWA8_BANK30				((write8_handler)STATIC_BANK30)
#define MWA8_BANK31				((write8_handler)STATIC_BANK31)
#define MWA8_BANK32				((write8_handler)STATIC_BANK32)
#define MWA8_RAM				((write8_handler)STATIC_RAM)
#define MWA8_ROM				((write8_handler)STATIC_ROM)
#define MWA8_NOP				((write8_handler)STATIC_NOP)
#define MWA8_UNMAP				((write8_handler)STATIC_UNMAP)

/* 16-bit reads */
#define MRA16_BANK1				((read16_handler)STATIC_BANK1)
#define MRA16_BANK2				((read16_handler)STATIC_BANK2)
#define MRA16_BANK3				((read16_handler)STATIC_BANK3)
#define MRA16_BANK4				((read16_handler)STATIC_BANK4)
#define MRA16_BANK5				((read16_handler)STATIC_BANK5)
#define MRA16_BANK6				((read16_handler)STATIC_BANK6)
#define MRA16_BANK7				((read16_handler)STATIC_BANK7)
#define MRA16_BANK8				((read16_handler)STATIC_BANK8)
#define MRA16_BANK9				((read16_handler)STATIC_BANK9)
#define MRA16_BANK10			((read16_handler)STATIC_BANK10)
#define MRA16_BANK11			((read16_handler)STATIC_BANK11)
#define MRA16_BANK12			((read16_handler)STATIC_BANK12)
#define MRA16_BANK13			((read16_handler)STATIC_BANK13)
#define MRA16_BANK14			((read16_handler)STATIC_BANK14)
#define MRA16_BANK15			((read16_handler)STATIC_BANK15)
#define MRA16_BANK16			((read16_handler)STATIC_BANK16)
#define MRA16_BANK17			((read16_handler)STATIC_BANK17)
#define MRA16_BANK18			((read16_handler)STATIC_BANK18)
#define MRA16_BANK19			((read16_handler)STATIC_BANK19)
#define MRA16_BANK20			((read16_handler)STATIC_BANK20)
#define MRA16_BANK21			((read16_handler)STATIC_BANK21)
#define MRA16_BANK22			((read16_handler)STATIC_BANK22)
#define MRA16_BANK23			((read16_handler)STATIC_BANK23)
#define MRA16_BANK24			((read16_handler)STATIC_BANK24)
#define MRA16_BANK25			((read16_handler)STATIC_BANK25)
#define MRA16_BANK26			((read16_handler)STATIC_BANK26)
#define MRA16_BANK27			((read16_handler)STATIC_BANK27)
#define MRA16_BANK28			((read16_handler)STATIC_BANK28)
#define MRA16_BANK29			((read16_handler)STATIC_BANK29)
#define MRA16_BANK30			((read16_handler)STATIC_BANK30)
#define MRA16_BANK31			((read16_handler)STATIC_BANK31)
#define MRA16_BANK32			((read16_handler)STATIC_BANK32)
#define MRA16_RAM				((read16_handler)STATIC_RAM)
#define MRA16_ROM				((read16_handler)STATIC_ROM)
#define MRA16_NOP				((read16_handler)STATIC_NOP)
#define MRA16_UNMAP				((read16_handler)STATIC_UNMAP)

/* 16-bit writes */
#define MWA16_BANK1				((write16_handler)STATIC_BANK1)
#define MWA16_BANK2				((write16_handler)STATIC_BANK2)
#define MWA16_BANK3				((write16_handler)STATIC_BANK3)
#define MWA16_BANK4				((write16_handler)STATIC_BANK4)
#define MWA16_BANK5				((write16_handler)STATIC_BANK5)
#define MWA16_BANK6				((write16_handler)STATIC_BANK6)
#define MWA16_BANK7				((write16_handler)STATIC_BANK7)
#define MWA16_BANK8				((write16_handler)STATIC_BANK8)
#define MWA16_BANK9				((write16_handler)STATIC_BANK9)
#define MWA16_BANK10			((write16_handler)STATIC_BANK10)
#define MWA16_BANK11			((write16_handler)STATIC_BANK11)
#define MWA16_BANK12			((write16_handler)STATIC_BANK12)
#define MWA16_BANK13			((write16_handler)STATIC_BANK13)
#define MWA16_BANK14			((write16_handler)STATIC_BANK14)
#define MWA16_BANK15			((write16_handler)STATIC_BANK15)
#define MWA16_BANK16			((write16_handler)STATIC_BANK16)
#define MWA16_BANK17			((write16_handler)STATIC_BANK17)
#define MWA16_BANK18			((write16_handler)STATIC_BANK18)
#define MWA16_BANK19			((write16_handler)STATIC_BANK19)
#define MWA16_BANK20			((write16_handler)STATIC_BANK20)
#define MWA16_BANK21			((write16_handler)STATIC_BANK21)
#define MWA16_BANK22			((write16_handler)STATIC_BANK22)
#define MWA16_BANK23			((write16_handler)STATIC_BANK23)
#define MWA16_BANK24			((write16_handler)STATIC_BANK24)
#define MWA16_BANK25			((write16_handler)STATIC_BANK25)
#define MWA16_BANK26			((write16_handler)STATIC_BANK26)
#define MWA16_BANK27			((write16_handler)STATIC_BANK27)
#define MWA16_BANK28			((write16_handler)STATIC_BANK28)
#define MWA16_BANK29			((write16_handler)STATIC_BANK29)
#define MWA16_BANK30			((write16_handler)STATIC_BANK30)
#define MWA16_BANK31			((write16_handler)STATIC_BANK31)
#define MWA16_BANK32			((write16_handler)STATIC_BANK32)
#define MWA16_RAM				((write16_handler)STATIC_RAM)
#define MWA16_ROM				((write16_handler)STATIC_ROM)
#define MWA16_NOP				((write16_handler)STATIC_NOP)
#define MWA16_UNMAP				((write16_handler)STATIC_UNMAP)

/* 32-bit reads */
#define MRA32_BANK1				((read32_handler)STATIC_BANK1)
#define MRA32_BANK2				((read32_handler)STATIC_BANK2)
#define MRA32_BANK3				((read32_handler)STATIC_BANK3)
#define MRA32_BANK4				((read32_handler)STATIC_BANK4)
#define MRA32_BANK5				((read32_handler)STATIC_BANK5)
#define MRA32_BANK6				((read32_handler)STATIC_BANK6)
#define MRA32_BANK7				((read32_handler)STATIC_BANK7)
#define MRA32_BANK8				((read32_handler)STATIC_BANK8)
#define MRA32_BANK9				((read32_handler)STATIC_BANK9)
#define MRA32_BANK10			((read32_handler)STATIC_BANK10)
#define MRA32_BANK11			((read32_handler)STATIC_BANK11)
#define MRA32_BANK12			((read32_handler)STATIC_BANK12)
#define MRA32_BANK13			((read32_handler)STATIC_BANK13)
#define MRA32_BANK14			((read32_handler)STATIC_BANK14)
#define MRA32_BANK15			((read32_handler)STATIC_BANK15)
#define MRA32_BANK16			((read32_handler)STATIC_BANK16)
#define MRA32_BANK17			((read32_handler)STATIC_BANK17)
#define MRA32_BANK18			((read32_handler)STATIC_BANK18)
#define MRA32_BANK19			((read32_handler)STATIC_BANK19)
#define MRA32_BANK20			((read32_handler)STATIC_BANK20)
#define MRA32_BANK21			((read32_handler)STATIC_BANK21)
#define MRA32_BANK22			((read32_handler)STATIC_BANK22)
#define MRA32_BANK23			((read32_handler)STATIC_BANK23)
#define MRA32_BANK24			((read32_handler)STATIC_BANK24)
#define MRA32_BANK25			((read32_handler)STATIC_BANK25)
#define MRA32_BANK26			((read32_handler)STATIC_BANK26)
#define MRA32_BANK27			((read32_handler)STATIC_BANK27)
#define MRA32_BANK28			((read32_handler)STATIC_BANK28)
#define MRA32_BANK29			((read32_handler)STATIC_BANK29)
#define MRA32_BANK30			((read32_handler)STATIC_BANK30)
#define MRA32_BANK31			((read32_handler)STATIC_BANK31)
#define MRA32_BANK32			((read32_handler)STATIC_BANK32)
#define MRA32_RAM				((read32_handler)STATIC_RAM)
#define MRA32_ROM				((read32_handler)STATIC_ROM)
#define MRA32_NOP				((read32_handler)STATIC_NOP)
#define MRA32_UNMAP				((read32_handler)STATIC_UNMAP)

/* 32-bit writes */
#define MWA32_BANK1				((write32_handler)STATIC_BANK1)
#define MWA32_BANK2				((write32_handler)STATIC_BANK2)
#define MWA32_BANK3				((write32_handler)STATIC_BANK3)
#define MWA32_BANK4				((write32_handler)STATIC_BANK4)
#define MWA32_BANK5				((write32_handler)STATIC_BANK5)
#define MWA32_BANK6				((write32_handler)STATIC_BANK6)
#define MWA32_BANK7				((write32_handler)STATIC_BANK7)
#define MWA32_BANK8				((write32_handler)STATIC_BANK8)
#define MWA32_BANK9				((write32_handler)STATIC_BANK9)
#define MWA32_BANK10			((write32_handler)STATIC_BANK10)
#define MWA32_BANK11			((write32_handler)STATIC_BANK11)
#define MWA32_BANK12			((write32_handler)STATIC_BANK12)
#define MWA32_BANK13			((write32_handler)STATIC_BANK13)
#define MWA32_BANK14			((write32_handler)STATIC_BANK14)
#define MWA32_BANK15			((write32_handler)STATIC_BANK15)
#define MWA32_BANK16			((write32_handler)STATIC_BANK16)
#define MWA32_BANK17			((write32_handler)STATIC_BANK17)
#define MWA32_BANK18			((write32_handler)STATIC_BANK18)
#define MWA32_BANK19			((write32_handler)STATIC_BANK19)
#define MWA32_BANK20			((write32_handler)STATIC_BANK20)
#define MWA32_BANK21			((write32_handler)STATIC_BANK21)
#define MWA32_BANK22			((write32_handler)STATIC_BANK22)
#define MWA32_BANK23			((write32_handler)STATIC_BANK23)
#define MWA32_BANK24			((write32_handler)STATIC_BANK24)
#define MWA32_BANK25			((write32_handler)STATIC_BANK25)
#define MWA32_BANK26			((write32_handler)STATIC_BANK26)
#define MWA32_BANK27			((write32_handler)STATIC_BANK27)
#define MWA32_BANK28			((write32_handler)STATIC_BANK28)
#define MWA32_BANK29			((write32_handler)STATIC_BANK29)
#define MWA32_BANK30			((write32_handler)STATIC_BANK30)
#define MWA32_BANK31			((write32_handler)STATIC_BANK31)
#define MWA32_BANK32			((write32_handler)STATIC_BANK32)
#define MWA32_RAM				((write32_handler)STATIC_RAM)
#define MWA32_ROM				((write32_handler)STATIC_ROM)
#define MWA32_NOP				((write32_handler)STATIC_NOP)
#define MWA32_UNMAP				((write32_handler)STATIC_UNMAP)

/* 64-bit reads */
#define MRA64_BANK1				((read64_handler)STATIC_BANK1)
#define MRA64_BANK2				((read64_handler)STATIC_BANK2)
#define MRA64_BANK3				((read64_handler)STATIC_BANK3)
#define MRA64_BANK4				((read64_handler)STATIC_BANK4)
#define MRA64_BANK5				((read64_handler)STATIC_BANK5)
#define MRA64_BANK6				((read64_handler)STATIC_BANK6)
#define MRA64_BANK7				((read64_handler)STATIC_BANK7)
#define MRA64_BANK8				((read64_handler)STATIC_BANK8)
#define MRA64_BANK9				((read64_handler)STATIC_BANK9)
#define MRA64_BANK10			((read64_handler)STATIC_BANK10)
#define MRA64_BANK11			((read64_handler)STATIC_BANK11)
#define MRA64_BANK12			((read64_handler)STATIC_BANK12)
#define MRA64_BANK13			((read64_handler)STATIC_BANK13)
#define MRA64_BANK14			((read64_handler)STATIC_BANK14)
#define MRA64_BANK15			((read64_handler)STATIC_BANK15)
#define MRA64_BANK16			((read64_handler)STATIC_BANK16)
#define MRA64_BANK17			((read64_handler)STATIC_BANK17)
#define MRA64_BANK18			((read64_handler)STATIC_BANK18)
#define MRA64_BANK19			((read64_handler)STATIC_BANK19)
#define MRA64_BANK20			((read64_handler)STATIC_BANK20)
#define MRA64_BANK21			((read64_handler)STATIC_BANK21)
#define MRA64_BANK22			((read64_handler)STATIC_BANK22)
#define MRA64_BANK23			((read64_handler)STATIC_BANK23)
#define MRA64_BANK24			((read64_handler)STATIC_BANK24)
#define MRA64_BANK25			((read64_handler)STATIC_BANK25)
#define MRA64_BANK26			((read64_handler)STATIC_BANK26)
#define MRA64_BANK27			((read64_handler)STATIC_BANK27)
#define MRA64_BANK28			((read64_handler)STATIC_BANK28)
#define MRA64_BANK29			((read64_handler)STATIC_BANK29)
#define MRA64_BANK30			((read64_handler)STATIC_BANK30)
#define MRA64_BANK31			((read64_handler)STATIC_BANK31)
#define MRA64_BANK32			((read64_handler)STATIC_BANK32)
#define MRA64_RAM				((read64_handler)STATIC_RAM)
#define MRA64_ROM				((read64_handler)STATIC_ROM)
#define MRA64_NOP				((read64_handler)STATIC_NOP)
#define MRA64_UNMAP				((read64_handler)STATIC_UNMAP)

/* 64-bit writes */
#define MWA64_BANK1				((write64_handler)STATIC_BANK1)
#define MWA64_BANK2				((write64_handler)STATIC_BANK2)
#define MWA64_BANK3				((write64_handler)STATIC_BANK3)
#define MWA64_BANK4				((write64_handler)STATIC_BANK4)
#define MWA64_BANK5				((write64_handler)STATIC_BANK5)
#define MWA64_BANK6				((write64_handler)STATIC_BANK6)
#define MWA64_BANK7				((write64_handler)STATIC_BANK7)
#define MWA64_BANK8				((write64_handler)STATIC_BANK8)
#define MWA64_BANK9				((write64_handler)STATIC_BANK9)
#define MWA64_BANK10			((write64_handler)STATIC_BANK10)
#define MWA64_BANK11			((write64_handler)STATIC_BANK11)
#define MWA64_BANK12			((write64_handler)STATIC_BANK12)
#define MWA64_BANK13			((write64_handler)STATIC_BANK13)
#define MWA64_BANK14			((write64_handler)STATIC_BANK14)
#define MWA64_BANK15			((write64_handler)STATIC_BANK15)
#define MWA64_BANK16			((write64_handler)STATIC_BANK16)
#define MWA64_BANK17			((write64_handler)STATIC_BANK17)
#define MWA64_BANK18			((write64_handler)STATIC_BANK18)
#define MWA64_BANK19			((write64_handler)STATIC_BANK19)
#define MWA64_BANK20			((write64_handler)STATIC_BANK20)
#define MWA64_BANK21			((write64_handler)STATIC_BANK21)
#define MWA64_BANK22			((write64_handler)STATIC_BANK22)
#define MWA64_BANK23			((write64_handler)STATIC_BANK23)
#define MWA64_BANK24			((write64_handler)STATIC_BANK24)
#define MWA64_BANK25			((write64_handler)STATIC_BANK25)
#define MWA64_BANK26			((write64_handler)STATIC_BANK26)
#define MWA64_BANK27			((write64_handler)STATIC_BANK27)
#define MWA64_BANK28			((write64_handler)STATIC_BANK28)
#define MWA64_BANK29			((write64_handler)STATIC_BANK29)
#define MWA64_BANK30			((write64_handler)STATIC_BANK30)
#define MWA64_BANK31			((write64_handler)STATIC_BANK31)
#define MWA64_BANK32			((write64_handler)STATIC_BANK32)
#define MWA64_RAM				((write64_handler)STATIC_RAM)
#define MWA64_ROM				((write64_handler)STATIC_ROM)
#define MWA64_NOP				((write64_handler)STATIC_NOP)
#define MWA64_UNMAP				((write64_handler)STATIC_UNMAP)



/***************************************************************************
    ADDRESS SPACE ARRAY TYPE DEFINITIONS
****************************************************************************
    Note that the data access handlers are not passed the actual address
    where the operation takes place, but the offset from the beginning
    of the block they are assigned to. This makes handling of mirror
    addresses easier, and makes the handlers a bit more "object oriented".
    If you handler needs to read/write the main memory area, provide a
    "base" pointer: it will be initialized by the main engine to point to
    the beginning of the memory block assigned to the handler. You may
    also provide a pointer to "size": it will be set to the length of
    the memory area processed by the handler.
***************************************************************************/

typedef struct _handler_data handler_data;

/* ----- a union of all the different read handler types ----- */
typedef union _read_handlers read_handlers;
union _read_handlers
{
	genf *				handler;
	read8_handler		handler8;
	read16_handler		handler16;
	read32_handler		handler32;
	read64_handler		handler64;
};

/* ----- a union of all the different write handler types ----- */
typedef union _write_handlers write_handlers;
union _write_handlers
{
	genf *				handler;
	write8_handler		handler8;
	write16_handler		handler16;
	write32_handler		handler32;
	write64_handler		handler64;
};

/* ----- a generic address map type ----- */
typedef struct _address_map address_map;
struct _address_map
{
	UINT32				flags;				/* flags and additional info about this entry */
	offs_t				start, end;			/* start/end (or mask/match) values */
	offs_t				mirror;				/* mirror bits */
	offs_t				mask;				/* mask bits */
	read_handlers 		read;				/* read handler callback */
	const char *		read_name;			/* read handler callback name */
	write_handlers 		write;				/* write handler callback */
	const char *		write_name;			/* write handler callback name */
	void *				memory;				/* pointer to memory backing this entry */
	UINT32				share;				/* index of a shared memory block */
	void **				base;				/* receives pointer to memory (optional) */
	size_t *			size;				/* receives size of area in bytes (optional) */
	UINT32				region;				/* region containing the memory backing this entry */
	offs_t				region_offs;		/* offset within the region */
};

/* ----- structs to contain internal data ----- */
typedef struct _address_space address_space;
struct _address_space
{
	offs_t				addrmask;			/* address mask */
	UINT8 *				readlookup;			/* read table lookup */
	UINT8 *				writelookup;		/* write table lookup */
	handler_data *		readhandlers;		/* read handlers */
	handler_data *		writehandlers;		/* write handlers */
	const data_accessors *	accessors;			/* pointers to the data access handlers */
};



/***************************************************************************
    ADDRESS MAP ARRAY CONSTRUCTORS
***************************************************************************/

void construct_address_map(address_map *map, const machine_config *drv, int cpunum, int spacenum);

/* ----- a typedef for pointers to these functions ----- */
typedef address_map *(*construct_map_t)(address_map *map);

/* use this to declare external references to a machine driver */
#define ADDRESS_MAP_EXTERN(_name)										\
address_map *construct_map_##_name(address_map *map)					\

/* ----- macros for starting, ending, and setting map flags ----- */
#define ADDRESS_MAP_START(_name,_space,_bits)							\
address_map *construct_map_##_name(address_map *map)					\
{																		\
	extern read##_bits##_handler port_tag_to_handler##_bits(const char *); \
	typedef read##_bits##_handler _rh_t;								\
	typedef write##_bits##_handler _wh_t;								\
	_rh_t read;															\
	_wh_t write;														\
	_rh_t (*port_tag_to_handler)(const char *) = port_tag_to_handler##_bits; \
	UINT##_bits **base;													\
																		\
	(void)read; (void)write; (void)base;								\
	(void)port_tag_to_handler; \
	map->flags = AM_FLAGS_EXTENDED;										\
	map->start = AMEF_DBITS(_bits) | AMEF_SPACE(_space);				\

#define ADDRESS_MAP_FLAGS(_flags)										\
	map++;																\
	map->flags = AM_FLAGS_EXTENDED;										\
	map->start = (_flags);												\

#define ADDRESS_MAP_END													\
	map++;																\
	map->flags = AM_FLAGS_END;											\
	return map;															\
}																		\

/* ----- each map entry begins with one of these ----- */
#define AM_RANGE(_start,_end)											\
	map++;																\
	map->flags = 0;														\
	map->start = (_start);												\
	map->end = (_end);													\

#define AM_SPACE(_match,_mask)											\
	map++;																\
	map->flags = AM_FLAGS_MATCH_MASK;									\
	map->start = (_match);												\
	map->end = (_mask);													\

/* ----- these are optional entries after each map entry ----- */
#define AM_MASK(_mask)													\
	map->mask = (_mask);												\

#define AM_MIRROR(_mirror)												\
	map->mirror = (_mirror);											\

#define AM_READ(_handler)												\
	map->read.handler = (genf *)(read = _handler);						\
	map->read_name = #_handler;											\

#define AM_READ_PORT(_tag) \
	AM_READ((*port_tag_to_handler)(_tag))

#define AM_WRITE(_handler)												\
	map->write.handler = (genf *)(write = _handler);					\
	map->write_name = #_handler;										\

#define AM_REGION(_region, _offs)										\
	map->region = (_region);											\
	map->region_offs = (_offs);											\

#define AM_SHARE(_index)												\
	map->share = _index;												\

#define AM_BASE(_base)													\
	map->base = (void **)(base = _base);								\

#define AM_BASE_MEMBER(_struct, _member)								\
	if (Machine != NULL && Machine->driver_data != NULL)				\
		map->base = (void **)(base = &((_struct *)Machine->driver_data)->_member);\

#define AM_SIZE(_size)													\
	map->size = _size;													\

#define AM_SIZE_MEMBER(_struct, _member)								\
	if (Machine != NULL && Machine->driver_data != NULL)				\
		map->size = &((_struct *)Machine->driver_data)->(_member);		\

/* ----- common shortcuts ----- */
#define AM_READWRITE(_read,_write)			AM_READ(_read) AM_WRITE(_write)
#define AM_ROM								AM_READ((_rh_t)STATIC_ROM)
#define AM_RAM								AM_READWRITE((_rh_t)STATIC_RAM, (_wh_t)STATIC_RAM)
#define AM_UNMAP							AM_READWRITE((_rh_t)STATIC_UNMAP, (_wh_t)STATIC_UNMAP)
#define AM_ROMBANK(_bank)					AM_READ((_rh_t)(STATIC_BANK1 + (_bank) - 1))
#define AM_RAMBANK(_bank)					AM_READWRITE((_rh_t)(STATIC_BANK1 + (_bank) - 1), (_wh_t)(STATIC_BANK1 + (_bank) - 1))
#define AM_NOP								AM_READWRITE((_rh_t)STATIC_NOP, (_wh_t)STATIC_NOP)
#define AM_READNOP							AM_READ((_rh_t)STATIC_NOP)
#define AM_WRITENOP							AM_WRITE((_wh_t)STATIC_NOP)



/***************************************************************************
    ADDRESS MAP ARRAY HELPER MACROS
***************************************************************************/

/* ----- macros for identifying address map struct markers ----- */
#define IS_AMENTRY_EXTENDED(ma)				(((ma)->flags & AM_FLAGS_EXTENDED) != 0)
#define IS_AMENTRY_MATCH_MASK(ma)			(((ma)->flags & AM_FLAGS_MATCH_MASK) != 0)
#define IS_AMENTRY_END(ma)					(((ma)->flags & AM_FLAGS_END) != 0)

#define AM_EXTENDED_FLAGS(ma)				((ma)->start)



/***************************************************************************
    ADDRESS MAP LOOKUP CONSTANTS
***************************************************************************/

/* ----- address spaces ----- */
enum
{
	ADDRESS_SPACE_PROGRAM = 0,			/* program address space */
	ADDRESS_SPACE_DATA,				/* data address space */
	ADDRESS_SPACE_IO,				/* I/O address space */
	ADDRESS_SPACES					/* maximum number of address spaces */
};

extern const char *const address_space_names[ADDRESS_SPACES];

/* ----- address map lookup table definitions ----- */
#define SUBTABLE_COUNT			64						/* number of slots reserved for subtables */
#define SUBTABLE_BASE			(256-SUBTABLE_COUNT)	/* first index of a subtable */
#define ENTRY_COUNT				(SUBTABLE_BASE)			/* number of legitimate (non-subtable) entries */
#define SUBTABLE_ALLOC			8						/* number of subtables to allocate at a time */

/* ----- bit counts ----- */
#define LEVEL1_BITS				18						/* number of address bits in the level 1 table */
#define LEVEL2_BITS				(32 - LEVEL1_BITS)		/* number of address bits in the level 2 table */

/* ----- other address map constants ----- */
#define MAX_ADDRESS_MAP_SIZE	256						/* maximum entries in an address map */
#define MAX_MEMORY_BLOCKS		1024					/* maximum memory blocks we can track */
#define MAX_SHARED_POINTERS		256						/* maximum number of shared pointers in memory maps */
#define MEMORY_BLOCK_SIZE		65536					/* size of allocated memory blocks */



/***************************************************************************
    ADDRESS MAP LOOKUP MACROS
***************************************************************************/

/* ----- table lookup helpers ----- */
#define LEVEL1_INDEX(a)			((a) >> LEVEL2_BITS)
#define LEVEL2_INDEX(e,a)		((1 << LEVEL1_BITS) + (((e) - SUBTABLE_BASE) << LEVEL2_BITS) + ((a) & ((1 << LEVEL2_BITS) - 1)))



/***************************************************************************
    FUNCTION PROTOTYPES FOR CORE READ/WRITE ROUTINES
***************************************************************************/

/* ----- declare program address space handlers ----- */
UINT8 program_read_byte_8(offs_t address);
void program_write_byte_8(offs_t address, UINT8 data);

UINT8 program_read_byte_16be(offs_t address);
UINT16 program_read_word_16be(offs_t address);
void program_write_byte_16be(offs_t address, UINT8 data);
void program_write_word_16be(offs_t address, UINT16 data);

UINT8 program_read_byte_16le(offs_t address);
UINT16 program_read_word_16le(offs_t address);
void program_write_byte_16le(offs_t address, UINT8 data);
void program_write_word_16le(offs_t address, UINT16 data);

UINT8 program_read_byte_32be(offs_t address);
UINT16 program_read_word_32be(offs_t address);
UINT32 program_read_dword_32be(offs_t address);
UINT32 program_read_masked_32be(offs_t address, UINT32 mem_mask);
void program_write_byte_32be(offs_t address, UINT8 data);
void program_write_word_32be(offs_t address, UINT16 data);
void program_write_dword_32be(offs_t address, UINT32 data);
void program_write_masked_32be(offs_t address, UINT32 data, UINT32 mem_mask);

UINT8 program_read_byte_32le(offs_t address);
UINT16 program_read_word_32le(offs_t address);
UINT32 program_read_dword_32le(offs_t address);
UINT32 program_read_masked_32le(offs_t address, UINT32 mem_mask);
void program_write_byte_32le(offs_t address, UINT8 data);
void program_write_word_32le(offs_t address, UINT16 data);
void program_write_dword_32le(offs_t address, UINT32 data);
void program_write_masked_32le(offs_t address, UINT32 data, UINT32 mem_mask);

UINT8 program_read_byte_64be(offs_t address);
UINT16 program_read_word_64be(offs_t address);
UINT32 program_read_dword_64be(offs_t address);
UINT64 program_read_qword_64be(offs_t address);
UINT64 program_read_masked_64be(offs_t address, UINT64 mem_mask);
void program_write_byte_64be(offs_t address, UINT8 data);
void program_write_word_64be(offs_t address, UINT16 data);
void program_write_dword_64be(offs_t address, UINT32 data);
void program_write_qword_64be(offs_t address, UINT64 data);
void program_write_masked_64be(offs_t address, UINT64 data, UINT64 mem_mask);

UINT8 program_read_byte_64le(offs_t address);
UINT16 program_read_word_64le(offs_t address);
UINT32 program_read_dword_64le(offs_t address);
UINT64 program_read_qword_64le(offs_t address);
UINT64 program_read_masked_64le(offs_t address, UINT64 mem_mask);
void program_write_byte_64le(offs_t address, UINT8 data);
void program_write_word_64le(offs_t address, UINT16 data);
void program_write_dword_64le(offs_t address, UINT32 data);
void program_write_qword_64le(offs_t address, UINT64 data);
void program_write_masked_64le(offs_t address, UINT64 data, UINT64 mem_mask);

/* ----- declare data address space handlers ----- */
UINT8 data_read_byte_8(offs_t address);
void data_write_byte_8(offs_t address, UINT8 data);

UINT8 data_read_byte_16be(offs_t address);
UINT16 data_read_word_16be(offs_t address);
void data_write_byte_16be(offs_t address, UINT8 data);
void data_write_word_16be(offs_t address, UINT16 data);

UINT8 data_read_byte_16le(offs_t address);
UINT16 data_read_word_16le(offs_t address);
void data_write_byte_16le(offs_t address, UINT8 data);
void data_write_word_16le(offs_t address, UINT16 data);

UINT8 data_read_byte_32be(offs_t address);
UINT16 data_read_word_32be(offs_t address);
UINT32 data_read_dword_32be(offs_t address);
UINT32 data_read_masked_32be(offs_t address, UINT32 mem_mask);
void data_write_byte_32be(offs_t address, UINT8 data);
void data_write_word_32be(offs_t address, UINT16 data);
void data_write_dword_32be(offs_t address, UINT32 data);
void data_write_masked_32be(offs_t address, UINT32 data, UINT32 mem_mask);

UINT8 data_read_byte_32le(offs_t address);
UINT16 data_read_word_32le(offs_t address);
UINT32 data_read_dword_32le(offs_t address);
UINT32 data_read_masked_32le(offs_t address, UINT32 mem_mask);
void data_write_byte_32le(offs_t address, UINT8 data);
void data_write_word_32le(offs_t address, UINT16 data);
void data_write_dword_32le(offs_t address, UINT32 data);
void data_write_masked_32le(offs_t address, UINT32 data, UINT32 mem_mask);

UINT8 data_read_byte_64be(offs_t address);
UINT16 data_read_word_64be(offs_t address);
UINT32 data_read_dword_64be(offs_t address);
UINT64 data_read_qword_64be(offs_t address);
UINT64 data_read_masked_64be(offs_t address, UINT64 mem_mask);
void data_write_byte_64be(offs_t address, UINT8 data);
void data_write_word_64be(offs_t address, UINT16 data);
void data_write_dword_64be(offs_t address, UINT32 data);
void data_write_qword_64be(offs_t address, UINT64 data);
void data_write_masked_64be(offs_t address, UINT64 data, UINT64 mem_mask);

UINT8 data_read_byte_64le(offs_t address);
UINT16 data_read_word_64le(offs_t address);
UINT32 data_read_dword_64le(offs_t address);
UINT64 data_read_qword_64le(offs_t address);
UINT64 data_read_masked_64le(offs_t address, UINT64 mem_mask);
void data_write_byte_64le(offs_t address, UINT8 data);
void data_write_word_64le(offs_t address, UINT16 data);
void data_write_dword_64le(offs_t address, UINT32 data);
void data_write_qword_64le(offs_t address, UINT64 data);
void data_write_masked_64le(offs_t address, UINT64 data, UINT64 mem_mask);

/* ----- declare I/O address space handlers ----- */
UINT8 io_read_byte_8(offs_t address);
void io_write_byte_8(offs_t address, UINT8 data);

UINT8 io_read_byte_16be(offs_t address);
UINT16 io_read_word_16be(offs_t address);
void io_write_byte_16be(offs_t address, UINT8 data);
void io_write_word_16be(offs_t address, UINT16 data);

UINT8 io_read_byte_16le(offs_t address);
UINT16 io_read_word_16le(offs_t address);
void io_write_byte_16le(offs_t address, UINT8 data);
void io_write_word_16le(offs_t address, UINT16 data);

UINT8 io_read_byte_32be(offs_t address);
UINT16 io_read_word_32be(offs_t address);
UINT32 io_read_dword_32be(offs_t address);
UINT32 io_read_masked_32be(offs_t address, UINT32 mem_mask);
void io_write_byte_32be(offs_t address, UINT8 data);
void io_write_word_32be(offs_t address, UINT16 data);
void io_write_dword_32be(offs_t address, UINT32 data);
void io_write_masked_32be(offs_t address, UINT32 data, UINT32 mem_mask);

UINT8 io_read_byte_32le(offs_t address);
UINT16 io_read_word_32le(offs_t address);
UINT32 io_read_dword_32le(offs_t address);
UINT32 io_read_masked_32le(offs_t address, UINT32 mem_mask);
void io_write_byte_32le(offs_t address, UINT8 data);
void io_write_word_32le(offs_t address, UINT16 data);
void io_write_dword_32le(offs_t address, UINT32 data);
void io_write_masked_32le(offs_t address, UINT32 data, UINT32 mem_mask);

UINT8 io_read_byte_64be(offs_t address);
UINT16 io_read_word_64be(offs_t address);
UINT32 io_read_dword_64be(offs_t address);
UINT64 io_read_qword_64be(offs_t address);
UINT64 io_read_masked_64be(offs_t address, UINT64 mem_mask);
void io_write_byte_64be(offs_t address, UINT8 data);
void io_write_word_64be(offs_t address, UINT16 data);
void io_write_dword_64be(offs_t address, UINT32 data);
void io_write_qword_64be(offs_t address, UINT64 data);
void io_write_masked_64be(offs_t address, UINT64 data, UINT64 mem_mask);

UINT8 io_read_byte_64le(offs_t address);
UINT16 io_read_word_64le(offs_t address);
UINT32 io_read_dword_64le(offs_t address);
UINT64 io_read_qword_64le(offs_t address);
UINT64 io_read_masked_64le(offs_t address, UINT64 mem_mask);
void io_write_byte_64le(offs_t address, UINT8 data);
void io_write_word_64le(offs_t address, UINT16 data);
void io_write_dword_64le(offs_t address, UINT32 data);
void io_write_qword_64le(offs_t address, UINT64 data);
void io_write_masked_64le(offs_t address, UINT64 data, UINT64 mem_mask);



/***************************************************************************
    FUNCTION PROTOTYPES FOR CORE MEMORY FUNCTIONS
***************************************************************************/

/* ----- memory setup function ----- */
void		memory_init(running_machine *machine);
void		memory_set_context(int activecpu);

/* ----- address map functions ----- */
const address_map *memory_get_map(int cpunum, int spacenum);

/* ----- opcode base control ---- */
opbase_handler memory_set_opbase_handler(int cpunum, opbase_handler function);
void		memory_set_opbase(offs_t offset);

/* ----- separate opcode/data encryption helper ---- */
void 		memory_set_decrypted_region(int cpunum, offs_t start, offs_t end, void *base);

/* ----- return a base pointer to memory ---- */
void *		memory_get_read_ptr(int cpunum, int spacenum, offs_t offset);
void *		memory_get_write_ptr(int cpunum, int spacenum, offs_t offset);
void *		memory_get_op_ptr(int cpunum, offs_t offset, int arg);

/* ----- memory banking ----- */
void		memory_configure_bank(int banknum, int startentry, int numentries, void *base, offs_t stride);
void		memory_configure_bank_decrypted(int banknum, int startentry, int numentries, void *base, offs_t stride);
void		memory_set_bank(int banknum, int entrynum);
int 		memory_get_bank(int banknum);
void		memory_set_bankptr(int banknum, void *base);

/* ----- debugging ----- */
void		memory_set_debugger_access(int debugger);
void		memory_set_log_unmap(int spacenum, int log);
int			memory_get_log_unmap(int spacenum);

/* ----- dynamic address space mapping ----- */
void *		_memory_install_read_handler   (int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, FPTR handler, const char *handler_name);
UINT8 *		_memory_install_read8_handler  (int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, read8_handler handler, const char *handler_name);
UINT16 *	_memory_install_read16_handler (int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, read16_handler handler, const char *handler_name);
UINT32 *	_memory_install_read32_handler (int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, read32_handler handler, const char *handler_name);
UINT64 *	_memory_install_read64_handler (int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, read64_handler handler, const char *handler_name);
void *		_memory_install_write_handler  (int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, FPTR handler, const char *handler_name);
UINT8 *		_memory_install_write8_handler (int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, write8_handler handler, const char *handler_name);
UINT16 *	_memory_install_write16_handler(int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, write16_handler handler, const char *handler_name);
UINT32 *	_memory_install_write32_handler(int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, write32_handler handler, const char *handler_name);
UINT64 *	_memory_install_write64_handler(int cpunum, int spacenum, offs_t start, offs_t end, offs_t mask, offs_t mirror, write64_handler handler, const char *handler_name);

void *		_memory_install_read_matchmask_handler   (int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, FPTR handler, const char *handler_name);
UINT8 *		_memory_install_read8_matchmask_handler  (int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, read8_handler handler, const char *handler_name);
UINT16 *	_memory_install_read16_matchmask_handler (int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, read16_handler handler, const char *handler_name);
UINT32 *	_memory_install_read32_matchmask_handler (int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, read32_handler handler, const char *handler_name);
UINT64 *	_memory_install_read64_matchmask_handler (int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, read64_handler handler, const char *handler_name);
void *		_memory_install_write_matchmask_handler  (int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, FPTR handler, const char *handler_name);
UINT8 *		_memory_install_write8_matchmask_handler (int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, write8_handler handler, const char *handler_name);
UINT16 *	_memory_install_write16_matchmask_handler(int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, write16_handler handler, const char *handler_name);
UINT32 *	_memory_install_write32_matchmask_handler(int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, write32_handler handler, const char *handler_name);
UINT64 *	_memory_install_write64_matchmask_handler(int cpunum, int spacenum, offs_t matchval, offs_t maskval, offs_t mask, offs_t mirror, write64_handler handler, const char *handler_name);

/* ----- memory debugging ----- */
void 		memory_dump(FILE *file);
const char *memory_get_handler_string(int read0_or_write1, int cpunum, int spacenum, offs_t offset);



/***************************************************************************
    GLOBAL VARIABLES
***************************************************************************/

extern UINT8 			opcode_entry;				/* current entry for opcode fetching */
extern UINT8 *			opcode_base;				/* opcode ROM base */
extern UINT8 *			opcode_arg_base;			/* opcode RAM base */
extern offs_t			opcode_mask;				/* mask to apply to the opcode address */
extern offs_t			opcode_memory_min;			/* opcode memory minimum */
extern offs_t			opcode_memory_max;			/* opcode memory maximum */
extern address_space	active_address_space[];		/* address spaces */
#define construct_map_0 NULL



/***************************************************************************
    HELPER MACROS AND INLINES
***************************************************************************/

/* ----- 16/32-bit memory accessing ----- */
#define COMBINE_DATA(varptr)		(*(varptr) = (*(varptr) & mem_mask) | (data & ~mem_mask))

/* ----- 16-bit memory accessing ----- */
#define ACCESSING_LSB16				((mem_mask & 0x00ff) == 0)
#define ACCESSING_MSB16				((mem_mask & 0xff00) == 0)
#define ACCESSING_LSB				ACCESSING_LSB16
#define ACCESSING_MSB				ACCESSING_MSB16

/* ----- 32-bit memory accessing ----- */
#define ACCESSING_LSW32				((mem_mask & 0x0000ffff) == 0)
#define ACCESSING_MSW32				((mem_mask & 0xffff0000) == 0)
#define ACCESSING_LSB32				((mem_mask & 0x000000ff) == 0)
#define ACCESSING_MSB32				((mem_mask & 0xff000000) == 0)

/* ----- opcode range safety checks ----- */
#if CPUREADOP_SAFETY_NONE
#define address_is_unsafe(A)		(0)
#elif CPUREADOP_SAFETY_PARTIAL
#define address_is_unsafe(A)		(UNEXPECTED((A) > opcode_memory_max))
#elif CPUREADOP_SAFETY_FULL
#define address_is_unsafe(A)		((UNEXPECTED((A) < opcode_memory_min) || UNEXPECTED((A) > opcode_memory_max)))
#else
#error Must set either CPUREADOP_SAFETY_NONE, CPUREADOP_SAFETY_PARTIAL or CPUREADOP_SAFETY_FULL
#endif

/* ----- dynamic memory installation ----- */
#define memory_install_read_handler(cpu, space, start, end, mask, mirror, handler)				\
	_memory_install_read_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_read8_handler(cpu, space, start, end, mask, mirror, handler)				\
	_memory_install_read8_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_read16_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_read16_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_read32_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_read32_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_read64_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_read64_handler(cpu, space, start, end, mask, mirror, handler, #handler)

#define memory_install_write_handler(cpu, space, start, end, mask, mirror, handler)				\
	_memory_install_write_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_write8_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_write8_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_write16_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_write16_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_write32_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_write32_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_write64_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_write64_handler(cpu, space, start, end, mask, mirror, handler, #handler)

#define memory_install_readwrite_handler(cpu, space, start, end, mask, mirror, rhandler, whandler) \
	_memory_install_read_handler(cpu, space, start, end, mask, mirror, rhandler, #rhandler);	\
	_memory_install_write_handler(cpu, space, start, end, mask, mirror, whandler, #whandler)
#define memory_install_readwrite8_handler(cpu, space, start, end, mask, mirror, rhandler, whandler) \
	_memory_install_read8_handler(cpu, space, start, end, mask, mirror, rhandler, #rhandler);	\
	_memory_install_write8_handler(cpu, space, start, end, mask, mirror, whandler, #whandler)
#define memory_install_readwrite16_handler(cpu, space, start, end, mask, mirror, rhandler, whandler) \
	_memory_install_read16_handler(cpu, space, start, end, mask, mirror, rhandler, #rhandler);	\
	_memory_install_write16_handler(cpu, space, start, end, mask, mirror, whandler, #whandler)
#define memory_install_readwrite32_handler(cpu, space, start, end, mask, mirror, rhandler, whandler) \
	_memory_install_read32_handler(cpu, space, start, end, mask, mirror, rhandler, #rhandler);	\
	_memory_install_write32_handler(cpu, space, start, end, mask, mirror, whandler, #whandler)
#define memory_install_readwrite64_handler(cpu, space, start, end, mask, mirror, rhandler, whandler) \
	_memory_install_read64_handler(cpu, space, start, end, mask, mirror, rhandler, #rhandler);	\
	_memory_install_write64_handler(cpu, space, start, end, mask, mirror, whandler, #whandler)

#define memory_install_read_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_read_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_read8_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_read8_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_read16_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_read16_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_read32_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_read32_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_read64_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_read64_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)

#define memory_install_write_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_write_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_write8_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_write8_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_write16_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_write16_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_write32_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_write32_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)
#define memory_install_write64_matchmask_handler(cpu, space, start, end, mask, mirror, handler)			\
	_memory_install_write64_matchmask_handler(cpu, space, start, end, mask, mirror, handler, #handler)

/* ----- generic memory access ----- */
INLINE UINT8  program_read_byte (offs_t offset) { return (*active_address_space[ADDRESS_SPACE_PROGRAM].accessors->read_byte)(offset); }
INLINE UINT16 program_read_word (offs_t offset) { return (*active_address_space[ADDRESS_SPACE_PROGRAM].accessors->read_word)(offset); }
INLINE UINT32 program_read_dword(offs_t offset) { return (*active_address_space[ADDRESS_SPACE_PROGRAM].accessors->read_dword)(offset); }
INLINE UINT64 program_read_qword(offs_t offset) { return (*active_address_space[ADDRESS_SPACE_PROGRAM].accessors->read_qword)(offset); }

INLINE void	program_write_byte (offs_t offset, UINT8  data) { (*active_address_space[ADDRESS_SPACE_PROGRAM].accessors->write_byte)(offset, data); }
INLINE void	program_write_word (offs_t offset, UINT16 data) { (*active_address_space[ADDRESS_SPACE_PROGRAM].accessors->write_word)(offset, data); }
INLINE void	program_write_dword(offs_t offset, UINT32 data) { (*active_address_space[ADDRESS_SPACE_PROGRAM].accessors->write_dword)(offset, data); }
INLINE void	program_write_qword(offs_t offset, UINT64 data) { (*active_address_space[ADDRESS_SPACE_PROGRAM].accessors->write_qword)(offset, data); }

INLINE UINT8  data_read_byte (offs_t offset) { return (*active_address_space[ADDRESS_SPACE_DATA].accessors->read_byte)(offset); }
INLINE UINT16 data_read_word (offs_t offset) { return (*active_address_space[ADDRESS_SPACE_DATA].accessors->read_word)(offset); }
INLINE UINT32 data_read_dword(offs_t offset) { return (*active_address_space[ADDRESS_SPACE_DATA].accessors->read_dword)(offset); }
INLINE UINT64 data_read_qword(offs_t offset) { return (*active_address_space[ADDRESS_SPACE_DATA].accessors->read_qword)(offset); }

INLINE void	data_write_byte (offs_t offset, UINT8  data) { (*active_address_space[ADDRESS_SPACE_DATA].accessors->write_byte)(offset, data); }
INLINE void	data_write_word (offs_t offset, UINT16 data) { (*active_address_space[ADDRESS_SPACE_DATA].accessors->write_word)(offset, data); }
INLINE void	data_write_dword(offs_t offset, UINT32 data) { (*active_address_space[ADDRESS_SPACE_DATA].accessors->write_dword)(offset, data); }
INLINE void	data_write_qword(offs_t offset, UINT64 data) { (*active_address_space[ADDRESS_SPACE_DATA].accessors->write_qword)(offset, data); }

INLINE UINT8  io_read_byte (offs_t offset) { return (*active_address_space[ADDRESS_SPACE_IO].accessors->read_byte)(offset); }
INLINE UINT16 io_read_word (offs_t offset) { return (*active_address_space[ADDRESS_SPACE_IO].accessors->read_word)(offset); }
INLINE UINT32 io_read_dword(offs_t offset) { return (*active_address_space[ADDRESS_SPACE_IO].accessors->read_dword)(offset); }
INLINE UINT64 io_read_qword(offs_t offset) { return (*active_address_space[ADDRESS_SPACE_IO].accessors->read_qword)(offset); }

INLINE void	io_write_byte (offs_t offset, UINT8  data) { (*active_address_space[ADDRESS_SPACE_IO].accessors->write_byte)(offset, data); }
INLINE void	io_write_word (offs_t offset, UINT16 data) { (*active_address_space[ADDRESS_SPACE_IO].accessors->write_word)(offset, data); }
INLINE void	io_write_dword(offs_t offset, UINT32 data) { (*active_address_space[ADDRESS_SPACE_IO].accessors->write_dword)(offset, data); }
INLINE void	io_write_qword(offs_t offset, UINT64 data) { (*active_address_space[ADDRESS_SPACE_IO].accessors->write_qword)(offset, data); }

/* ----- safe opcode and opcode argument reading ----- */
UINT8	cpu_readop_safe(offs_t offset);
UINT16	cpu_readop16_safe(offs_t offset);
UINT32	cpu_readop32_safe(offs_t offset);
UINT64	cpu_readop64_safe(offs_t offset);
UINT8	cpu_readop_arg_safe(offs_t offset);
UINT16	cpu_readop_arg16_safe(offs_t offset);
UINT32	cpu_readop_arg32_safe(offs_t offset);
UINT64	cpu_readop_arg64_safe(offs_t offset);

/* ----- unsafe opcode and opcode argument reading ----- */
#define cpu_opptr_unsafe(A)			((void *)&opcode_base[(A) & opcode_mask])
#define cpu_readop_unsafe(A)		(opcode_base[(A) & opcode_mask])
#define cpu_readop16_unsafe(A)		(*(UINT16 *)&opcode_base[(A) & opcode_mask])
#define cpu_readop32_unsafe(A)		(*(UINT32 *)&opcode_base[(A) & opcode_mask])
#define cpu_readop64_unsafe(A)		(*(UINT64 *)&opcode_base[(A) & opcode_mask])
#define cpu_readop_arg_unsafe(A)	(opcode_arg_base[(A) & opcode_mask])
#define cpu_readop_arg16_unsafe(A)	(*(UINT16 *)&opcode_arg_base[(A) & opcode_mask])
#define cpu_readop_arg32_unsafe(A)	(*(UINT32 *)&opcode_arg_base[(A) & opcode_mask])
#define cpu_readop_arg64_unsafe(A)	(*(UINT64 *)&opcode_arg_base[(A) & opcode_mask])

/* ----- opcode and opcode argument reading ----- */
INLINE void * cpu_opptr(offs_t A)			{ if (address_is_unsafe(A)) { memory_set_opbase(A); } return cpu_opptr_unsafe(A); }
INLINE UINT8  cpu_readop(offs_t A)			{ if (address_is_unsafe(A)) { memory_set_opbase(A); } return cpu_readop_unsafe(A); }
INLINE UINT16 cpu_readop16(offs_t A)		{ if (address_is_unsafe(A)) { memory_set_opbase(A); } return cpu_readop16_unsafe(A); }
INLINE UINT32 cpu_readop32(offs_t A)		{ if (address_is_unsafe(A)) { memory_set_opbase(A); } return cpu_readop32_unsafe(A); }
INLINE UINT64 cpu_readop64(offs_t A)		{ if (address_is_unsafe(A)) { memory_set_opbase(A); } return cpu_readop64_unsafe(A); }
INLINE UINT8  cpu_readop_arg(offs_t A)		{ if (address_is_unsafe(A)) { memory_set_opbase(A); } return cpu_readop_arg_unsafe(A); }
INLINE UINT16 cpu_readop_arg16(offs_t A)	{ if (address_is_unsafe(A)) { memory_set_opbase(A); } return cpu_readop_arg16_unsafe(A); }
INLINE UINT32 cpu_readop_arg32(offs_t A)	{ if (address_is_unsafe(A)) { memory_set_opbase(A); } return cpu_readop_arg32_unsafe(A); }
INLINE UINT64 cpu_readop_arg64(offs_t A)	{ if (address_is_unsafe(A)) { memory_set_opbase(A); } return cpu_readop_arg64_unsafe(A); }

/* ----- bank switching for CPU cores ----- */
#define change_pc(pc)				memory_set_opbase(pc);

/* ----- forces the next branch to generate a call to the opbase handler ----- */
#define catch_nextBranch()			(opcode_entry = 0xff)


#endif	/* __MEMORY_H__ */