1 /*
2 
3 Copyright 2021, dettus@dettus.net
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright notice,
12    this list of conditions and the following disclaimer in the documentation
13    and/or other materials provided with the distribution.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 
27 */
28 
29 #ifndef	VM68K_MACROS_H
30 #define	VM68K_MACROS_H
31 #include "vm68k_datatypes.h"
32 // the gfx2 format introduced MIXED ENDIAN.
33 #define READ_INT32ME(ptr,idx)   (\
34         (((tVM68k_ulong)((ptr)[((idx)+1)])&0xff)<<24)   |\
35         (((tVM68k_ulong)((ptr)[((idx)+0)])&0xff)<<16)   |\
36         (((tVM68k_ulong)((ptr)[((idx)+3)])&0xff)<< 8)   |\
37         (((tVM68k_ulong)((ptr)[((idx)+2)])&0xff)<< 0)   |\
38         0)
39 
40 
41 #define	READ_INT32BE(ptr,idx)	(\
42 	(((tVM68k_ulong)((ptr)[((idx)+0)])&0xff)<<24)	|\
43 	(((tVM68k_ulong)((ptr)[((idx)+1)])&0xff)<<16)	|\
44 	(((tVM68k_ulong)((ptr)[((idx)+2)])&0xff)<< 8)	|\
45 	(((tVM68k_ulong)((ptr)[((idx)+3)])&0xff)<< 0)	|\
46 	0)
47 #define	READ_INT16BE(ptr,idx)	(\
48 	(((tVM68k_ulong)((ptr)[((idx)+0)])&0xff)<< 8)	|\
49 	(((tVM68k_ulong)((ptr)[((idx)+1)])&0xff)<< 0)	|\
50 	0)
51 #define	READ_INT8BE(ptr,idx)	(\
52 	(((tVM68k_ulong)((ptr)[((idx)+0)])&0xff)<< 0)	|\
53 	0)
54 
55 #define	READ_INT32LE(ptr,idx)	(\
56 	(((unsigned int)((ptr)[((idx)+3)])&0xff)<<24)	|\
57 	(((unsigned int)((ptr)[((idx)+2)])&0xff)<<16)	|\
58 	(((unsigned int)((ptr)[((idx)+1)])&0xff)<< 8)	|\
59 	(((unsigned int)((ptr)[((idx)+0)])&0xff)<< 0)	|\
60 	0)
61 #define	READ_INT16LE(ptr,idx)	(\
62 	(((unsigned int)((ptr)[((idx)+1)])&0xff)<< 8)	|\
63 	(((unsigned int)((ptr)[((idx)+0)])&0xff)<< 0)	|\
64 	0)
65 #define	READ_INT8LE(ptr,idx)	(\
66 	(((unsigned int)((ptr)[((idx)+0)])&0xff)<< 0)	|\
67 	0)
68 
69 
70 #define	WRITE_INT32BE(ptr,idx,val) {\
71 	(ptr)[(idx)+3]=((tVM68k_ubyte)((val)>> 0)&0xff);	\
72 	(ptr)[(idx)+2]=((tVM68k_ubyte)((val)>> 8)&0xff);	\
73 	(ptr)[(idx)+1]=((tVM68k_ubyte)((val)>>16)&0xff);	\
74 	(ptr)[(idx)+0]=((tVM68k_ubyte)((val)>>24)&0xff);	\
75 	}
76 #define	WRITE_INT16BE(ptr,idx,val) {\
77 	(ptr)[(idx)+1]=((tVM68k_ubyte)((val)>> 0)&0xff);	\
78 	(ptr)[(idx)+0]=((tVM68k_ubyte)((val)>> 8)&0xff);	\
79 	}
80 #define	WRITE_INT8BE(ptr,idx,val) {\
81 	(ptr)[(idx)+0]=((tVM68k_ubyte)((val)>> 0)&0xff);	\
82 	}
83 
84 #define	WRITE_INT32LE(ptr,idx,val) {\
85 	(ptr)[(idx)+0]=((unsigned char)((val)>> 0)&0xff);	\
86 	(ptr)[(idx)+1]=((unsigned char)((val)>> 8)&0xff);	\
87 	(ptr)[(idx)+2]=((unsigned char)((val)>>16)&0xff);	\
88 	(ptr)[(idx)+3]=((unsigned char)((val)>>24)&0xff);	\
89 	}
90 #define	WRITE_INT16LE(ptr,idx,val) {\
91 	(ptr)[(idx)+0]=((unsigned char)((val)>> 0)&0xff);	\
92 	(ptr)[(idx)+1]=((unsigned char)((val)>> 8)&0xff);	\
93 	}
94 #define	WRITE_INT8LE(ptr,idx,val) {\
95 	(ptr)[(idx)+0]=((unsigned char)((val)>> 0)&0xff);	\
96 	}
97 
98 
99 #define	INITNEXT(pVM68k,next)	\
100 	(next).pcr=(pVM68k)->pcr;	\
101 	(next).a[0]=(pVM68k)->a[0];	\
102 	(next).a[1]=(pVM68k)->a[1];	\
103 	(next).a[2]=(pVM68k)->a[2];	\
104 	(next).a[3]=(pVM68k)->a[3];	\
105 	(next).a[4]=(pVM68k)->a[4];	\
106 	(next).a[5]=(pVM68k)->a[5];	\
107 	(next).a[6]=(pVM68k)->a[6];	\
108 	(next).a[7]=(pVM68k)->a[7];	\
109 	(next).d[0]=(pVM68k)->d[0];	\
110 	(next).d[1]=(pVM68k)->d[1];	\
111 	(next).d[2]=(pVM68k)->d[2];	\
112 	(next).d[3]=(pVM68k)->d[3];	\
113 	(next).d[4]=(pVM68k)->d[4];	\
114 	(next).d[5]=(pVM68k)->d[5];	\
115 	(next).d[6]=(pVM68k)->d[6];	\
116 	(next).d[7]=(pVM68k)->d[7];	\
117 	(next).override_sr=0;		\
118 	(next).sr=((pVM68k)->sr);	\
119 	(next).cflag=((pVM68k)->sr>>0)&1;	\
120 	(next).vflag=((pVM68k)->sr>>1)&1;	\
121 	(next).zflag=((pVM68k)->sr>>2)&1;	\
122 	(next).nflag=((pVM68k)->sr>>3)&1;	\
123 	(next).xflag=((pVM68k)->sr>>4)&1;	\
124 	(next).mem_we=0;	\
125 	(next).mem_addr[0]=0;	\
126 	(next).mem_size=0;	\
127 	(next).mem_value[0]=0;
128 
129 
130 #define	WRITEFLAGS(pVM68k,transaction)	\
131 	(pVM68k)->sr|=(tVM68k_uword)((transaction).cflag)<<0;	\
132 	(pVM68k)->sr|=(tVM68k_uword)((transaction).vflag)<<1;	\
133 	(pVM68k)->sr|=(tVM68k_uword)((transaction).zflag)<<2;	\
134 	(pVM68k)->sr|=(tVM68k_uword)((transaction).nflag)<<3;	\
135 	(pVM68k)->sr|=(tVM68k_uword)((transaction).xflag)<<4;
136 
137 #define	READEXTENSIONBYTE(pVM68k,pNext)	READ_INT8BE((pVM68k)->pMem,(pNext)->pcr+1);(pNext)->pcr+=2;
138 #define	READEXTENSIONWORD(pVM68k,pNext)	READ_INT16BE((pVM68k)->pMem,(pNext)->pcr);(pNext)->pcr+=2;
139 #define	READEXTENSIONLONG(pVM68k,pNext)	READ_INT32BE((pVM68k)->pMem,(pNext)->pcr);(pNext)->pcr+=4;
140 
141 #define	READEXTENSION(pVM68k,pNext,datatype,operand)	\
142 	switch (datatype)	\
143 	{	\
144 		case VM68K_BYTE:	operand=READEXTENSIONBYTE(pVM68k,pNext);break;	\
145 		case VM68K_WORD:	operand=READEXTENSIONWORD(pVM68k,pNext);break;	\
146 		case VM68K_LONG:	operand=READEXTENSIONLONG(pVM68k,pNext);break;	\
147 		default:		operand=0;break;	\
148 	}
149 
150 #define	READSIGNEDEXTENSION(pVM68k,pNext,datatype,operand)	\
151 	switch (datatype)	\
152 	{	\
153 		case VM68K_BYTE:	operand=READEXTENSIONBYTE(pVM68k,pNext);operand=(tVM68k_slong)((tVM68k_sbyte)((operand)&  0xff));break;	\
154 		case VM68K_WORD:	operand=READEXTENSIONBYTE(pVM68k,pNext);operand=(tVM68k_slong)((tVM68k_sword)((operand)&0xffff));break;	\
155 		case VM68K_LONG:	operand=READEXTENSIONLONG(pVM68k,pNext);break;	\
156 	}
157 
158 #define	PUSHWORDTOSTACK(pVM68k,pNext,x)	{(pNext)->a[7]-=2;(pNext)->mem_addr[(pNext)->mem_we]=(pNext)->a[7];(pNext)->mem_size=VM68K_WORD;(pNext)->mem_value[(pNext)->mem_we]=x;(pNext)->mem_we++;}
159 #define	PUSHLONGTOSTACK(pVM68k,pNext,x)	{(pNext)->a[7]-=4;(pNext)->mem_addr[(pNext)->mem_we]=(pNext)->a[7];(pNext)->mem_size=VM68K_LONG;(pNext)->mem_value[(pNext)->mem_we]=x;(pNext)->mem_we++;}
160 
161 #define	POPWORDFROMSTACK(pVM68k,pNext,x)	{tVM68k_uword y;y=READ_INT16BE((pVM68k)->pMem,(pNext)->a[7]);(pNext)->a[7]+=2;x=((x)&0xffff0000)|(y&0xffff);}
162 #define	POPLONGFROMSTACK(pVM68k,pNext,x)	{x=READ_INT32BE((pVM68k)->pMem,(pNext)->a[7]);(pNext)->a[7]+=4;}
163 
164 
165 #endif
166