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
|
#include "../copyright"
#include "fxemu.h"
#include "fxinst.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* The FxChip Emulator's internal variables */
struct FxRegs_s GSU; // This will be initialized when loading a ROM
void FxCacheWriteAccess(uint16_t vAddress)
{
if ((vAddress & 0x00f) == 0x00f)
GSU.vCacheFlags |= 1 << ((vAddress & 0x1f0) >> 4);
}
void FxFlushCache()
{
GSU.vCacheFlags = 0;
GSU.vCacheBaseReg = 0;
GSU.bCacheActive = false;
}
void fx_flushCache()
{
GSU.vCacheFlags = 0;
GSU.bCacheActive = false;
}
void fx_updateRamBank(uint8_t Byte)
{
// Update BankReg and Bank pointer
GSU.vRamBankReg = (uint32_t) Byte & (FX_RAM_BANKS - 1);
GSU.pvRamBank = GSU.apvRamBank[Byte & 0x3];
}
static void fx_readRegisterSpaceForCheck()
{
R15 = GSU.pvRegisters[30];
R15 |= ((uint32_t) GSU.pvRegisters[31]) << 8;
GSU.vStatusReg = (uint32_t) GSU.pvRegisters[GSU_SFR];
GSU.vStatusReg |= ((uint32_t) GSU.pvRegisters[GSU_SFR + 1]) << 8;
GSU.vPrgBankReg = (uint32_t) GSU.pvRegisters[GSU_PBR];
}
static void fx_readRegisterSpaceForUse()
{
static uint32_t avHeight[] = { 128, 160, 192, 256 };
static uint32_t avMult[] = { 16, 32, 32, 64 };
int32_t i;
uint8_t* p = GSU.pvRegisters;
GSU.vErrorCode = 0;
/* Update R0 - R14 */
for (i = 0; i < 15; i++)
{
GSU.avReg[i] = *p++;
GSU.avReg[i] += ((uint32_t)(*p++)) << 8;
}
/* Update other registers */
p = GSU.pvRegisters;
GSU.vRomBankReg = (uint32_t)p[GSU_ROMBR];
GSU.vRamBankReg = ((uint32_t)p[GSU_RAMBR]) & (FX_RAM_BANKS - 1);
GSU.vCacheBaseReg = (uint32_t)p[GSU_CBR];
GSU.vCacheBaseReg |= ((uint32_t)p[GSU_CBR + 1]) << 8;
/* Update status register variables */
GSU.vZero = !(GSU.vStatusReg & FLG_Z);
GSU.vSign = (GSU.vStatusReg & FLG_S) << 12;
GSU.vOverflow = (GSU.vStatusReg & FLG_OV) << 16;
GSU.vCarry = (GSU.vStatusReg & FLG_CY) >> 2;
/* Set bank pointers */
GSU.pvRamBank = GSU.apvRamBank[GSU.vRamBankReg & 0x3];
GSU.pvRomBank = GSU.apvRomBank[GSU.vRomBankReg];
GSU.pvPrgBank = GSU.apvRomBank[GSU.vPrgBankReg];
/* Set screen pointers */
GSU.pvScreenBase = &GSU.pvRam[ USEX8(p[GSU_SCBR]) << 10 ];
i = (int32_t)(!!(p[GSU_SCMR] & 0x04));
i |= ((int32_t)(!!(p[GSU_SCMR] & 0x20))) << 1;
GSU.vScreenHeight = GSU.vScreenRealHeight = avHeight[i];
GSU.vMode = p[GSU_SCMR] & 0x03;
if (i == 3)
GSU.vScreenSize = (256 / 8) * (256 / 8) * 32;
else
GSU.vScreenSize = (GSU.vScreenHeight / 8) * (256 / 8) * avMult[GSU.vMode];
if (GSU.vPlotOptionReg & 0x10)
{
/* OBJ Mode (for drawing into sprites) */
GSU.vScreenHeight = 256;
}
if (GSU.pvScreenBase + GSU.vScreenSize > GSU.pvRam + (GSU.nRamBanks * 65536))
GSU.pvScreenBase = GSU.pvRam + (GSU.nRamBanks * 65536) - GSU.vScreenSize;
GSU.pfPlot = fx_apfPlotTable[GSU.vMode];
GSU.pfRpix = fx_apfPlotTable[GSU.vMode + 5];
fx_apfOpcodeTable[0x04c] = GSU.pfPlot;
fx_apfOpcodeTable[0x14c] = GSU.pfRpix;
fx_apfOpcodeTable[0x24c] = GSU.pfPlot;
fx_apfOpcodeTable[0x34c] = GSU.pfRpix;
fx_computeScreenPointers();
}
void fx_dirtySCBR()
{
GSU.vSCBRDirty = true;
}
void fx_computeScreenPointers()
{
if (GSU.vMode != GSU.vPrevMode || GSU.vPrevScreenHeight != GSU.vScreenHeight || GSU.vSCBRDirty)
{
int32_t i;
GSU.vSCBRDirty = false;
/* Make a list of pointers to the start of each screen column */
switch (GSU.vScreenHeight)
{
case 128:
switch (GSU.vMode)
{
case 0:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + (i << 4);
GSU.x[i] = i << 8;
}
break;
case 1:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + (i << 5);
GSU.x[i] = i << 9;
}
break;
case 2:
case 3:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + (i << 6);
GSU.x[i] = i << 10;
}
break;
}
break;
case 160:
switch (GSU.vMode)
{
case 0:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + (i << 4);
GSU.x[i] = (i << 8) + (i << 6);
}
break;
case 1:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + (i << 5);
GSU.x[i] = (i << 9) + (i << 7);
}
break;
case 2:
case 3:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + (i << 6);
GSU.x[i] = (i << 10) + (i << 8);
}
break;
}
break;
case 192:
switch (GSU.vMode)
{
case 0:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + (i << 4);
GSU.x[i] = (i << 8) + (i << 7);
}
break;
case 1:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + (i << 5);
GSU.x[i] = (i << 9) + (i << 8);
}
break;
case 2:
case 3:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + (i << 6);
GSU.x[i] = (i << 10) + (i << 9);
}
break;
}
break;
case 256:
switch (GSU.vMode)
{
case 0:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + ((i & 0x10) << 9) + ((i & 0xf) << 8);
GSU.x[i] = ((i & 0x10) << 8) + ((i & 0xf) << 4);
}
break;
case 1:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + ((i & 0x10) << 10) + ((i & 0xf) << 9);
GSU.x[i] = ((i & 0x10) << 9) + ((i & 0xf) << 5);
}
break;
case 2:
case 3:
for (i = 0; i < 32; i++)
{
GSU.apvScreen[i] = GSU.pvScreenBase + ((i & 0x10) << 11) + ((i & 0xf) << 10);
GSU.x[i] = ((i & 0x10) << 10) + ((i & 0xf) << 6);
}
break;
}
break;
}
GSU.vPrevMode = GSU.vMode;
GSU.vPrevScreenHeight = GSU.vScreenHeight;
}
}
static void fx_writeRegisterSpaceAfterCheck()
{
GSU.pvRegisters[30] = (uint8_t) R15;
GSU.pvRegisters[31] = (uint8_t) (R15 >> 8);
GSU.pvRegisters[GSU_SFR] = (uint8_t) GSU.vStatusReg;
GSU.pvRegisters[GSU_SFR + 1] = (uint8_t) (GSU.vStatusReg >> 8);
GSU.pvRegisters[GSU_PBR] = (uint8_t) GSU.vPrgBankReg;
}
static void fx_writeRegisterSpaceAfterUse()
{
int32_t i;
uint8_t* p = GSU.pvRegisters;
for (i = 0; i < 15; i++)
{
*p++ = (uint8_t)GSU.avReg[i];
*p++ = (uint8_t)(GSU.avReg[i] >> 8);
}
/* Update status register */
if (USEX16(GSU.vZero) == 0)
SF(Z);
else
CF(Z);
if (GSU.vSign & 0x8000)
SF(S);
else
CF(S);
if (GSU.vOverflow >= 0x8000 || GSU.vOverflow < -0x8000)
SF(OV);
else
CF(OV);
if (GSU.vCarry)
SF(CY);
else
CF(CY);
p = GSU.pvRegisters;
p[GSU_ROMBR] = (uint8_t)GSU.vRomBankReg;
p[GSU_RAMBR] = (uint8_t)GSU.vRamBankReg;
p[GSU_CBR] = (uint8_t)GSU.vCacheBaseReg;
p[GSU_CBR + 1] = (uint8_t)(GSU.vCacheBaseReg >> 8);
}
/* Reset the FxChip */
void FxReset(struct FxInit_s* psFxInfo)
{
/* Clear all internal variables */
memset((uint8_t*) &GSU, 0, sizeof(struct FxRegs_s));
/* Set default registers */
GSU.pvSreg = GSU.pvDreg = &R0;
/* Set RAM and ROM pointers */
GSU.pvRegisters = psFxInfo->pvRegisters;
GSU.nRamBanks = psFxInfo->nRamBanks;
GSU.pvRam = psFxInfo->pvRam;
GSU.nRomBanks = psFxInfo->nRomBanks;
GSU.pvRom = psFxInfo->pvRom;
GSU.vPrevScreenHeight = ~0;
GSU.vPrevMode = ~0;
/* The GSU can't access more than 2mb (16mbits) */
if (GSU.nRomBanks > 0x20)
GSU.nRomBanks = 0x20;
/* Clear FxChip register space */
memset(GSU.pvRegisters, 0, 0x300);
/* Set FxChip version Number */
GSU.pvRegisters[0x3b] = 0;
/* Make ROM bank table */
int32_t i;
for (i = 0; i < 256; i++)
{
uint32_t b = i & 0x7f;
if (b >= 0x40)
{
if (GSU.nRomBanks > 2)
b %= GSU.nRomBanks;
else
b &= 1;
GSU.apvRomBank[i] = &GSU.pvRom[b << 16];
}
else
{
b %= GSU.nRomBanks * 2;
GSU.apvRomBank[i] = &GSU.pvRom[(b << 16) + 0x200000];
}
}
/* Make RAM bank table */
for (i = 0; i < 4; i++)
{
GSU.apvRamBank[i] = &GSU.pvRam[(i % GSU.nRamBanks) << 16];
GSU.apvRomBank[0x70 + i] = GSU.apvRamBank[i];
}
/* Start with a nop in the pipe */
GSU.vPipe = 0x01;
/* Set pointer to GSU cache */
GSU.pvCache = &GSU.pvRegisters[0x100];
fx_readRegisterSpaceForCheck();
fx_readRegisterSpaceForUse();
}
static bool fx_checkStartAddress()
{
/* Check if we start inside the cache */
if (GSU.bCacheActive && R15 >= GSU.vCacheBaseReg && R15 < (GSU.vCacheBaseReg + 512))
return true;
/* Check if we're in an unused area */
if (GSU.vPrgBankReg >= 0x60 && GSU.vPrgBankReg <= 0x6f)
return false;
if (GSU.vPrgBankReg >= 0x74)
return false;
/* Check if we're in RAM and the RAN flag is not set */
if (GSU.vPrgBankReg >= 0x70 && GSU.vPrgBankReg <= 0x73 && !(SCMR & (1 << 3)))
return false;
/* If not, we're in ROM, so check if the RON flag is set */
if (!(SCMR & (1 << 4)))
return false;
return true;
}
/* Execute until the next stop instruction */
int32_t FxEmulate(uint32_t nInstructions)
{
uint32_t vCount;
/* Read registers and initialize GSU session */
fx_readRegisterSpaceForCheck();
/* Check if the start address is valid */
if (!fx_checkStartAddress())
{
CF(G);
fx_writeRegisterSpaceAfterCheck();
return 0;
}
fx_readRegisterSpaceForUse();
/* Execute GSU session */
CF(IRQ);
vCount = fx_run(nInstructions);
/* Store GSU registers */
fx_writeRegisterSpaceAfterCheck();
fx_writeRegisterSpaceAfterUse();
/* Check for error code */
if (GSU.vErrorCode)
return GSU.vErrorCode;
else
return vCount;
}
/* Errors */
int32_t FxGetErrorCode()
{
return GSU.vErrorCode;
}
int32_t FxGetIllegalAddress()
{
return GSU.vIllegalAddress;
}
/* Access to internal registers */
uint32_t FxGetColorRegister()
{
return GSU.vColorReg & 0xff;
}
uint32_t FxGetPlotOptionRegister()
{
return GSU.vPlotOptionReg & 0x1f;
}
uint32_t FxGetSourceRegisterIndex()
{
return GSU.pvSreg - GSU.avReg;
}
uint32_t FxGetDestinationRegisterIndex()
{
return GSU.pvDreg - GSU.avReg;
}
uint8_t FxPipe()
{
return GSU.vPipe;
}
|