1 #include "burnint.h"
2 #include "s2650_intf.h"
3 
4 #define ADDRESS_MAX	0x8000
5 #define ADDRESS_MASK	0x7fff
6 #define PAGE		0x0100
7 #define PAGE_MASK	0x00ff
8 
9 #define READ		0
10 #define WRITE		1
11 #define FETCH		2
12 
13 INT32 s2650Count;
14 
15 struct s2650_handler
16 {
17 	UINT8 (*s2650Read)(UINT16 address);
18 	void (*s2650Write)(UINT16 address, UINT8 data);
19 
20 	UINT8 (*s2650ReadPort)(UINT16 port);
21 	void (*s2650WritePort)(UINT16 port, UINT8 data);
22 
23 	UINT8 *mem[3][ADDRESS_MAX / PAGE];
24 };
25 
26 struct s2650_handler sHandler[MAX_S2650];
27 struct s2650_handler *sPointer;
28 
29 s2650irqcallback s2650_irqcallback[MAX_S2650];
30 
31 extern void s2650_open(INT32 num);
32 extern void s2650_close();
33 extern void s2650_init(INT32 num);
34 extern void s2650_exit();
35 extern void s2650_reset(void);
36 extern INT32 s2650_get_pc();
37 
38 cpu_core_config s2650Config =
39 {
40 	s2650Open,
41 	s2650Close,
42 	s2650ReadCheat,
43 	s2650WriteROM,
44 	s2650GetActive,
45 	s2650TotalCycles,
46 	s2650NewFrame,
47 	s2650Run,
48 	s2650RunEnd,
49 	s2650Reset,
50 	0x8000,
51 	0
52 };
53 
s2650MapMemory(UINT8 * ptr,INT32 nStart,INT32 nEnd,INT32 nType)54 void s2650MapMemory(UINT8 *ptr, INT32 nStart, INT32 nEnd, INT32 nType)
55 {
56 #if defined FBA_DEBUG
57 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650MapMemory called without init\n"));
58 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650MapMemory called when no CPU open\n"));
59 #endif
60 
61 	nStart &= ADDRESS_MASK;
62 	nEnd   &= ADDRESS_MASK;
63 
64 	for (INT32 i = nStart / PAGE; i < (nEnd / PAGE) + 1; i++)
65 	{
66 		if (nType & (1 <<  READ)) sPointer->mem[ READ][i] = ptr + ((i * PAGE) - nStart);
67 		if (nType & (1 << WRITE)) sPointer->mem[WRITE][i] = ptr + ((i * PAGE) - nStart);
68 		if (nType & (1 << FETCH)) sPointer->mem[FETCH][i] = ptr + ((i * PAGE) - nStart);
69 	}
70 }
71 
s2650SetWriteHandler(void (* write)(UINT16,UINT8))72 void s2650SetWriteHandler(void (*write)(UINT16, UINT8))
73 {
74 #if defined FBA_DEBUG
75 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650SetWriteHandler called without init\n"));
76 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650SetWriteHandler called when no CPU open\n"));
77 #endif
78 
79 	sPointer->s2650Write = write;
80 }
81 
s2650SetReadHandler(UINT8 (* read)(UINT16))82 void s2650SetReadHandler(UINT8 (*read)(UINT16))
83 {
84 #if defined FBA_DEBUG
85 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650SetReadHandler called without init\n"));
86 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650SetReadHandler called when no CPU open\n"));
87 #endif
88 
89 	sPointer->s2650Read = read;
90 }
91 
s2650SetOutHandler(void (* write)(UINT16,UINT8))92 void s2650SetOutHandler(void (*write)(UINT16, UINT8))
93 {
94 #if defined FBA_DEBUG
95 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650SetOutHandler called without init\n"));
96 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650SetOutHandler called when no CPU open\n"));
97 #endif
98 
99 	sPointer->s2650WritePort = write;
100 }
101 
s2650SetInHandler(UINT8 (* read)(UINT16))102 void s2650SetInHandler(UINT8 (*read)(UINT16))
103 {
104 #if defined FBA_DEBUG
105 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650SetInHandler called without init\n"));
106 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650SetInHandler called when no CPU open\n"));
107 #endif
108 
109 	sPointer->s2650ReadPort = read;
110 }
111 
s2650Write(UINT16 address,UINT8 data)112 void s2650Write(UINT16 address, UINT8 data)
113 {
114 	address &= ADDRESS_MASK;
115 
116 	if (sPointer->mem[WRITE][address / PAGE] != NULL) {
117 		sPointer->mem[WRITE][address / PAGE][address & PAGE_MASK] = data;
118 		return;
119 	}
120 
121 	if (sPointer->s2650Write != NULL) {
122 		sPointer->s2650Write(address, data);
123 		return;
124 	}
125 
126 	return;
127 }
128 
s2650Read(UINT16 address)129 UINT8 s2650Read(UINT16 address)
130 {
131 	address &= ADDRESS_MASK;
132 
133 	if (sPointer->mem[READ][address / PAGE] != NULL) {
134 		return sPointer->mem[READ][address / PAGE][address & PAGE_MASK];
135 	}
136 
137 	if (sPointer->s2650Read != NULL) {
138 		return sPointer->s2650Read(address);
139 	}
140 
141 	return 0;
142 }
143 
s2650Fetch(UINT16 address)144 UINT8 s2650Fetch(UINT16 address)
145 {
146 	address &= ADDRESS_MASK;
147 
148 	if (sPointer->mem[FETCH][address / PAGE] != NULL) {
149 		return sPointer->mem[FETCH][address / PAGE][address & PAGE_MASK];
150 	}
151 
152 	return s2650Read(address);
153 }
154 
s2650WritePort(UINT16 port,UINT8 data)155 void s2650WritePort(UINT16 port, UINT8 data)
156 {
157 	if (sPointer->s2650WritePort != NULL) {
158 		sPointer->s2650WritePort(port, data);
159 		return;
160 	}
161 
162 	return;
163 }
164 
s2650ReadPort(UINT16 port)165 UINT8 s2650ReadPort(UINT16 port)
166 {
167 	if (sPointer->s2650ReadPort != NULL) {
168 		return sPointer->s2650ReadPort(port);
169 	}
170 
171 	return 0;
172 }
173 
s2650WriteROM(UINT32 address,UINT8 data)174 void s2650WriteROM(UINT32 address, UINT8 data)
175 {
176 #if defined FBA_DEBUG
177 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650WriteRom called without init\n"));
178 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650WriteRom called when no CPU open\n"));
179 #endif
180 
181 	address &= ADDRESS_MASK;
182 
183 	if (sPointer->mem[READ][address / PAGE] != NULL) {
184 		sPointer->mem[READ][address / PAGE][address & PAGE_MASK] = data;
185 	}
186 
187 	if (sPointer->mem[WRITE][address / PAGE] != NULL) {
188 		sPointer->mem[WRITE][address / PAGE][address & PAGE_MASK] = data;
189 	}
190 
191 	if (sPointer->mem[FETCH][address / PAGE] != NULL) {
192 		sPointer->mem[FETCH][address / PAGE][address & PAGE_MASK] = data;
193 	}
194 
195 	if (sPointer->s2650Write != NULL) {
196 		sPointer->s2650Write(address, data);
197 		return;
198 	}
199 
200 	return;
201 }
202 
s2650SetIrqCallback(INT32 (* irqcallback)(INT32))203 void s2650SetIrqCallback(INT32 (*irqcallback)(INT32))
204 {
205 #if defined FBA_DEBUG
206 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650SetIrqCallback called without init\n"));
207 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650SetIrqCallback called when no CPU open\n"));
208 #endif
209 
210 	s2650_irqcallback[nActiveS2650] = irqcallback;
211 }
212 
s2650ReadCheat(UINT32 a)213 UINT8 s2650ReadCheat(UINT32 a)
214 {
215 	return s2650Read(a);
216 }
217 
s2650TotalCycles()218 INT32 s2650TotalCycles()
219 {
220 	return 0;		// unimplemented
221 }
222 
s2650NewFrame()223 void s2650NewFrame()
224 {
225 	// unimplemented
226 }
227 
s2650RunEnd()228 void s2650RunEnd()
229 {
230 	// unimplemented
231 }
232 
s2650Init(INT32 num)233 void s2650Init(INT32 num)
234 {
235 	DebugCPU_S2650Initted = 1;
236 
237 	s2650Count = num;
238 	memset (&sHandler, 0, sizeof (s2650_handler) * (num % MAX_S2650));
239 	s2650_init(num);
240 
241 	for (INT32 i = 0; i < num; i++)
242 		CpuCheatRegister(i, &s2650Config);
243 }
244 
s2650Exit()245 void s2650Exit()
246 {
247 #if defined FBA_DEBUG
248 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650Exit called without init\n"));
249 #endif
250 
251 	if (!DebugCPU_S2650Initted) return;
252 
253 	memset (&sHandler, 0, sizeof (sHandler));
254 	s2650Count = 0;
255 	s2650_exit();
256 
257 	DebugCPU_S2650Initted = 0;
258 }
259 
s2650Open(INT32 num)260 void s2650Open(INT32 num)
261 {
262 #if defined FBA_DEBUG
263 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650Open called without init\n"));
264 	if (num > s2650Count) bprintf(PRINT_ERROR, _T("s2650Open called with invalid index %x\n"), num);
265 	if (nActiveS2650 != -1) bprintf(PRINT_ERROR, _T("s2650Open called when CPU already open with index %x\n"), num);
266 #endif
267 
268 	sPointer = &sHandler[num % MAX_S2650];
269 	s2650_open(num);
270 }
271 
s2650Close()272 void s2650Close()
273 {
274 #if defined FBA_DEBUG
275 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650Close called without init\n"));
276 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650Close called when no CPU open\n"));
277 #endif
278 
279 	s2650_close();
280 }
281 
s2650GetPC(INT32)282 UINT32 s2650GetPC(INT32)
283 {
284 #if defined FBA_DEBUG
285 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650GetPC called without init\n"));
286 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650GetPC called when no CPU open\n"));
287 #endif
288 
289 	return s2650_get_pc();
290 }
291 
s2650GetActive()292 INT32 s2650GetActive()
293 {
294 #if defined FBA_DEBUG
295 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650GetActive called without init\n"));
296 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650GetActive called when no CPU open\n"));
297 #endif
298 
299 	return nActiveS2650;
300 }
301 
s2650Reset()302 void s2650Reset()
303 {
304 #if defined FBA_DEBUG
305 	if (!DebugCPU_S2650Initted) bprintf(PRINT_ERROR, _T("s2650Reset called without init\n"));
306 	if (nActiveS2650 == -1) bprintf(PRINT_ERROR, _T("s2650Reset called when no CPU open\n"));
307 #endif
308 
309 	 s2650_reset();
310 }
311