Debian Codegraph @

mednafen/hw_cpu/v810/v810_cpu.cpp

raw symbols SCIP
1 /* V810 Emulator
2 *
3 * Copyright (C) 2006 David Tucker
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 /* Alternatively, the V810 emulator code(and all V810 emulation header files) can be used/distributed under the following license(you can adopt either
21 license exclusively for your changes by removing one of these license headers, but it's STRONGLY preferable
22 to keep your changes dual-licensed as well):
23
24 This Reality Boy emulator is copyright (C) David Tucker 1997-2008, all rights
25 reserved. You may use this code as long as you make no money from the use of
26 this code and you acknowledge the original author (Me). I reserve the right to
27 dictate who can use this code and how (Just so you don't do something stupid
28 with it).
29 Most Importantly, this code is swap ware. If you use It send along your new
30 program (with code) or some other interesting tidbits you wrote, that I might be
31 interested in.
32 This code is in beta, there are bugs! I am not responsible for any damage
33 done to your computer, reputation, ego, dog, or family life due to the use of
34 this code. All source is provided as is, I make no guaranties, and am not
35 responsible for anything you do with the code (legal or otherwise).
36 Virtual Boy is a trademark of Nintendo, and V810 is a trademark of NEC. I am
37 in no way affiliated with either party and all information contained hear was
38 found freely through public domain sources.
39 */
40
41 //////////////////////////////////////////////////////////
42 // CPU routines
43
44 #include <boolean.h>
45
46 #include "../../mednafen.h"
47 #include "../../masmem.h"
48
49 //#include "pcfx.h"
50 //#include "debug.h"
51
52 #include <string.h>
53 #include <errno.h>
54
55 #include "v810_opt.h"
56 #include "v810_cpu.h"
57 #include "v810_cpuD.h"
58
59 //#include "fpu-new/softfloat.h"
60
61 V810::V810()
62 {
63 #ifdef WANT_DEBUGGER
64 CPUHook = NULL;
65 ADDBT = NULL;
66 #endif
67
68 MemRead8 = NULL;
69 MemRead16 = NULL;
70 MemRead32 = NULL;
71
72 IORead8 = NULL;
73 IORead16 = NULL;
74 IORead32 = NULL;
75
76 MemWrite8 = NULL;
77 MemWrite16 = NULL;
78 MemWrite32 = NULL;
79
80 IOWrite8 = NULL;
81 IOWrite16 = NULL;
82 IOWrite32 = NULL;
83
84 memset(FastMap, 0, sizeof(FastMap));
85
86 memset(MemReadBus32, 0, sizeof(MemReadBus32));
87 memset(MemWriteBus32, 0, sizeof(MemWriteBus32));
88
89 v810_timestamp = 0;
90 next_event_ts = 0x7FFFFFFF;
91 }
92
93 V810::~V810()
94 {
95
96 }
97
98 INLINE void V810::RecalcIPendingCache(void)
99 {
100 IPendingCache = 0;
101
102 // Of course don't generate an interrupt if there's not one pending!
103 if(ilevel < 0)
104 return;
105
106 // If CPU is halted because of a fatal exception, don't let an interrupt
107 // take us out of this halted status.
108 if(Halted == HALT_FATAL_EXCEPTION)
109 return;
110
111 // If the NMI pending, exception pending, and/or interrupt disabled bit
112 // is set, don't accept any interrupts.
113 if(S_REG[PSW] & (PSW_NP | PSW_EP | PSW_ID))
114 return;
115
116 // If the interrupt level is lower than the interrupt enable level, don't
117 // accept it.
118 if(ilevel < (int)((S_REG[PSW] & PSW_IA) >> 16))
119 return;
120
121 IPendingCache = 0xFF;
122 }
123
124
125 // TODO: "An interrupt that occurs during restore/dump/clear operation is internally held and is accepted after the
126 // operation in progress is finished. The maskable interrupt is held internally only when the EP, NP, and ID flags
127 // of PSW are all 0."
128 //
129 // This behavior probably doesn't have any relevance on the PC-FX, unless we're sadistic
130 // and try to restore cache from an interrupt acknowledge register or dump it to a register
131 // controlling interrupt masks... I wanna be sadistic~
132
133 void V810::CacheClear(v810_timestamp_t &timestamp, uint32 start, uint32 count)
134 {
135 //printf("Cache clear: %08x %08x\n", start, count);
136 for(uint32 i = 0; i < count && (i + start) < 128; i++)
137 memset(&Cache[i + start], 0, sizeof(V810_CacheEntry_t));
138 }
139
140 INLINE void V810::CacheOpMemStore(v810_timestamp_t &timestamp, uint32 A, uint32 V)
141 {
142 if(MemWriteBus32[A >> 24])
143 {
144 timestamp += 2;
145 MemWrite32(timestamp, A, V);
146 }
147 else
148 {
149 timestamp += 2;
150 MemWrite16(timestamp, A, V & 0xFFFF);
151
152 timestamp += 2;
153 MemWrite16(timestamp, A | 2, V >> 16);
154 }
155 }
156
157 INLINE uint32 V810::CacheOpMemLoad(v810_timestamp_t &timestamp, uint32 A)
158 {
159 if(MemReadBus32[A >> 24])
160 {
161 timestamp += 2;
162 return(MemRead32(timestamp, A));
163 }
164 else
165 {
166 uint32 ret;
167
168 timestamp += 2;
169 ret = MemRead16(timestamp, A);
170
171 timestamp += 2;
172 ret |= MemRead16(timestamp, A | 2) << 16;
173 return(ret);
174 }
175 }
176
177 void V810::CacheDump(v810_timestamp_t &timestamp, const uint32 SA)
178 {
179 printf("Cache dump: %08x\n", SA);
180
181 for(int i = 0; i < 128; i++)
182 {
183 CacheOpMemStore(timestamp, SA + i * 8 + 0, Cache[i].data[0]);
184 CacheOpMemStore(timestamp, SA + i * 8 + 4, Cache[i].data[1]);
185 }
186
187 for(int i = 0; i < 128; i++)
188 {
189 uint32 icht = Cache[i].tag | ((int)Cache[i].data_valid[0] << 22) | ((int)Cache[i].data_valid[1] << 23);
190
191 CacheOpMemStore(timestamp, SA + 1024 + i * 4, icht);
192 }
193
194 }
195
196 void V810::CacheRestore(v810_timestamp_t &timestamp, const uint32 SA)
197 {
198 printf("Cache restore: %08x\n", SA);
199
200 for(int i = 0; i < 128; i++)
201 {
202 Cache[i].data[0] = CacheOpMemLoad(timestamp, SA + i * 8 + 0);
203 Cache[i].data[1] = CacheOpMemLoad(timestamp, SA + i * 8 + 4);
204 }
205
206 for(int i = 0; i < 128; i++)
207 {
208 uint32 icht;
209
210 icht = CacheOpMemLoad(timestamp, SA + 1024 + i * 4);
211
212 Cache[i].tag = icht & ((1 << 22) - 1);
213 Cache[i].data_valid[0] = (icht >> 22) & 1;
214 Cache[i].data_valid[1] = (icht >> 23) & 1;
215 }
216 }
217
218
219 INLINE uint32 V810::RDCACHE(v810_timestamp_t &timestamp, uint32 addr)
220 {
221 const int CI = (addr >> 3) & 0x7F;
222 const int SBI = (addr & 4) >> 2;
223
224 if(Cache[CI].tag == (addr >> 10))
225 {
226 if(!Cache[CI].data_valid[SBI])
227 {
228 timestamp += 2; // or higher? Penalty for cache miss seems to be higher than having cache disabled.
229 if(MemReadBus32[addr >> 24])
230 Cache[CI].data[SBI] = MemRead32(timestamp, addr & ~0x3);
231 else
232 {
233 timestamp++;
234 Cache[CI].data[SBI] = MemRead16(timestamp, addr & ~0x3) | ((MemRead16(timestamp, (addr & ~0x3) | 0x2) << 16));
235 }
236 Cache[CI].data_valid[SBI] = true;
237 }
238 }
239 else
240 {
241 Cache[CI].tag = addr >> 10;
242
243 timestamp += 2; // or higher? Penalty for cache miss seems to be higher than having cache disabled.
244 if(MemReadBus32[addr >> 24])
245 Cache[CI].data[SBI] = MemRead32(timestamp, addr & ~0x3);
246 else
247 {
248 timestamp++;
249 Cache[CI].data[SBI] = MemRead16(timestamp, addr & ~0x3) | ((MemRead16(timestamp, (addr & ~0x3) | 0x2) << 16));
250 }
251 //Cache[CI].data[SBI] = MemRead32(timestamp, addr & ~0x3);
252 Cache[CI].data_valid[SBI] = true;
253 Cache[CI].data_valid[SBI ^ 1] = false;
254 }
255
256 //{
257 // // Caution: This can mess up DRAM page change penalty timings
258 // uint32 dummy_timestamp = 0;
259 // if(Cache[CI].data[SBI] != mem_rword(addr & ~0x3, dummy_timestamp))
260 // {
261 // printf("Cache/Real Memory Mismatch: %08x %08x/%08x\n", addr & ~0x3, Cache[CI].data[SBI], mem_rword(addr & ~0x3, dummy_timestamp));
262 // }
263 //}
264
265 return(Cache[CI].data[SBI]);
266 }
267
268 INLINE uint16 V810::RDOP(v810_timestamp_t &timestamp, uint32 addr, uint32 meow)
269 {
270 uint16 ret;
271
272 if(S_REG[CHCW] & 0x2)
273 {
274 uint32 d32 = RDCACHE(timestamp, addr);
275 ret = d32 >> ((addr & 2) * 8);
276 }
277 else
278 {
279 timestamp += meow; //++;
280 ret = MemRead16(timestamp, addr);
281 }
282 return(ret);
283 }
284
285 #define BRANCH_ALIGN_CHECK(x) { if((S_REG[CHCW] & 0x2) && (x & 0x2)) { ADDCLOCK(1); } }
286
287 // Reinitialize the defaults in the CPU
288 void V810::Reset()
289 {
290 #ifdef WANT_DEBUGGER
291 if(ADDBT)
292 ADDBT(GetPC(), 0xFFFFFFF0, 0xFFF0);
293 #endif
294 memset(&Cache, 0, sizeof(Cache));
295
296 memset(P_REG, 0, sizeof(P_REG));
297 memset(S_REG, 0, sizeof(S_REG));
298 memset(Cache, 0, sizeof(Cache));
299
300 P_REG[0] = 0x00000000;
301 SetPC(0xFFFFFFF0);
302
303 S_REG[ECR] = 0x0000FFF0;
304 S_REG[PSW] = 0x00008000;
305
306 if(VBMode)
307 S_REG[PIR] = 0x00005346;
308 else
309 S_REG[PIR] = 0x00008100;
310
311 S_REG[TKCW] = 0x000000E0;
312 Halted = HALT_NONE;
313 ilevel = -1;
314
315 lastop = 0;
316
317 in_bstr = false;
318
319 RecalcIPendingCache();
320 }
321
322 bool V810::Init(V810_Emu_Mode mode, bool vb_mode)
323 {
324 EmuMode = mode;
325 VBMode = vb_mode;
326
327 in_bstr = false;
328 in_bstr_to = 0;
329
330 if(mode == V810_EMU_MODE_FAST)
331 {
332 memset(DummyRegion, 0, V810_FAST_MAP_PSIZE);
333
334 for(unsigned int i = V810_FAST_MAP_PSIZE; i < V810_FAST_MAP_PSIZE + V810_FAST_MAP_TRAMPOLINE_SIZE; i += 2)
335 {
336 DummyRegion[i + 0] = 0;
337 DummyRegion[i + 1] = 0x36 << 2;
338 }
339
340 for(uint64 A = 0; A < (1ULL << 32); A += V810_FAST_MAP_PSIZE)
341 FastMap[A / V810_FAST_MAP_PSIZE] = DummyRegion - A;
342 }
343
344 return(true);
345 }
346
347 void V810::Kill(void)
348 {
349 for(unsigned int i = 0; i < FastMapAllocList.size(); i++)
350 free(FastMapAllocList[i]);
351
352 FastMapAllocList.clear();
353 }
354
355 void V810::SetInt(int level)
356 {
357 assert(level >= -1 && level <= 15);
358
359 ilevel = level;
360 RecalcIPendingCache();
361 }
362
363 uint8 *V810::SetFastMap(uint32 addresses[], uint32 length, unsigned int num_addresses, const char *name)
364 {
365 uint8 *ret = NULL;
366
367 for(unsigned int i = 0; i < num_addresses; i++)
368 {
369 assert((addresses[i] & (V810_FAST_MAP_PSIZE - 1)) == 0);
370 }
371 assert((length & (V810_FAST_MAP_PSIZE - 1)) == 0);
372
373 if(!(ret = (uint8 *)malloc(length + V810_FAST_MAP_TRAMPOLINE_SIZE)))
374 {
375 return(NULL);
376 }
377
378 for(unsigned int i = length; i < length + V810_FAST_MAP_TRAMPOLINE_SIZE; i += 2)
379 {
380 ret[i + 0] = 0;
381 ret[i + 1] = 0x36 << 2;
382 }
383
384 for(unsigned int i = 0; i < num_addresses; i++)
385 {
386 for(uint64 addr = addresses[i]; addr != (uint64)addresses[i] + length; addr += V810_FAST_MAP_PSIZE)
387 {
388 //printf("%08x, %d, %s\n", addr, length, name);
389
390 FastMap[addr / V810_FAST_MAP_PSIZE] = ret - addresses[i];
391 }
392 }
393
394 FastMapAllocList.push_back(ret);
395
396 return(ret);
397 }
398
399
400 void V810::SetMemReadBus32(uint8 A, bool value)
401 {
402 MemReadBus32[A] = value;
403 }
404
405 void V810::SetMemWriteBus32(uint8 A, bool value)
406 {
407 MemWriteBus32[A] = value;
408 }
409
410 void V810::SetMemReadHandlers(uint8 MDFN_FASTCALL (*read8)(v810_timestamp_t &, uint32), uint16 MDFN_FASTCALL (*read16)(v810_timestamp_t &, uint32), uint32 MDFN_FASTCALL (*read32)(v810_timestamp_t &, uint32))
411 {
412 MemRead8 = read8;
413 MemRead16 = read16;
414 MemRead32 = read32;
415 }
416
417 void V810::SetMemWriteHandlers(void MDFN_FASTCALL (*write8)(v810_timestamp_t &, uint32, uint8), void MDFN_FASTCALL (*write16)(v810_timestamp_t &, uint32, uint16), void MDFN_FASTCALL (*write32)(v810_timestamp_t &, uint32, uint32))
418 {
419 MemWrite8 = write8;
420 MemWrite16 = write16;
421 MemWrite32 = write32;
422 }
423
424 void V810::SetIOReadHandlers(uint8 MDFN_FASTCALL (*read8)(v810_timestamp_t &, uint32), uint16 MDFN_FASTCALL (*read16)(v810_timestamp_t &, uint32), uint32 MDFN_FASTCALL (*read32)(v810_timestamp_t &, uint32))
425 {
426 IORead8 = read8;
427 IORead16 = read16;
428 IORead32 = read32;
429 }
430
431 void V810::SetIOWriteHandlers(void MDFN_FASTCALL (*write8)(v810_timestamp_t &, uint32, uint8), void MDFN_FASTCALL (*write16)(v810_timestamp_t &, uint32, uint16), void MDFN_FASTCALL (*write32)(v810_timestamp_t &, uint32, uint32))
432 {
433 IOWrite8 = write8;
434 IOWrite16 = write16;
435 IOWrite32 = write32;
436 }
437
438
439 INLINE void V810::SetFlag(uint32 n, bool condition)
440 {
441 S_REG[PSW] &= ~n;
442
443 if(condition)
444 S_REG[PSW] |= n;
445 }
446
447 INLINE void V810::SetSZ(uint32 value)
448 {
449 SetFlag(PSW_Z, !value);
450 SetFlag(PSW_S, value & 0x80000000);
451 }
452
453 #ifdef WANT_DEBUGGER
454 void V810::CheckBreakpoints(void (*callback)(int type, uint32 address, uint32 value, unsigned int len), uint16 MDFN_FASTCALL (*peek16)(const v810_timestamp_t, uint32), uint32 MDFN_FASTCALL (*peek32)(const v810_timestamp_t, uint32))
455 {
456 unsigned int opcode;
457 uint16 tmpop;
458 uint16 tmpop_high;
459 int32 ws_dummy = v810_timestamp;
460 uint32 tmp_PC = GetPC();
461
462 tmpop = peek16(ws_dummy, tmp_PC);
463 tmpop_high = peek16(ws_dummy, tmp_PC + 2);
464
465 opcode = tmpop >> 10;
466
467 // Uncomment this out later if necessary.
468 //if((tmpop & 0xE000) == 0x8000) // Special opcode format for
469 // opcode = (tmpop >> 9) & 0x7F; // type III instructions.
470
471 switch(opcode)
472 {
473 case CAXI: break;
474
475 default: break;
476
477 case LD_B: callback(BPOINT_READ, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFF, 0, 1); break;
478 case LD_H: callback(BPOINT_READ, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFE, 0, 2); break;
479 case LD_W: callback(BPOINT_READ, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFC, 0, 4); break;
480
481 case ST_B: callback(BPOINT_WRITE, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFF, P_REG[(tmpop >> 5) & 0x1F] & 0x00FF, 1); break;
482 case ST_H: callback(BPOINT_WRITE, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFE, P_REG[(tmpop >> 5) & 0x1F] & 0xFFFF, 2); break;
483 case ST_W: callback(BPOINT_WRITE, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFC, P_REG[(tmpop >> 5) & 0x1F], 4); break;
484
485 case IN_B: callback(BPOINT_IO_READ, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFF, 0, 1); break;
486 case IN_H: callback(BPOINT_IO_READ, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFE, 0, 2); break;
487 case IN_W: callback(BPOINT_IO_READ, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFC, 0, 4); break;
488
489 case OUT_B: callback(BPOINT_IO_WRITE, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFF, P_REG[(tmpop >> 5) & 0x1F] & 0xFF, 1); break;
490 case OUT_H: callback(BPOINT_IO_WRITE, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFE, P_REG[(tmpop >> 5) & 0x1F] & 0xFFFF, 2); break;
491 case OUT_W: callback(BPOINT_IO_WRITE, (sign_16(tmpop_high)+P_REG[tmpop & 0x1F])&0xFFFFFFFC, P_REG[(tmpop >> 5) & 0x1F], 4); break;
492 }
493
494 }
495 #endif
496
497 #define SetPREG(n, val) { P_REG[n] = val; }
498
499 INLINE void V810::SetSREG(v810_timestamp_t &timestamp, unsigned int which, uint32 value)
500 {
501 switch(which)
502 {
503 default: // Reserved
504 printf("LDSR to reserved system register: 0x%02x : 0x%08x\n", which, value);
505 break;
506
507 case ECR: // Read-only
508 break;
509
510 case PIR: // Read-only (obviously)
511 break;
512
513 case TKCW: // Read-only
514 break;
515
516 case EIPSW:
517 case FEPSW:
518 S_REG[which] = value & 0xFF3FF;
519 break;
520
521 case PSW:
522 S_REG[which] = value & 0xFF3FF;
523 RecalcIPendingCache();
524 break;
525
526 case EIPC:
527 case FEPC:
528 S_REG[which] = value & 0xFFFFFFFE;
529 break;
530
531 case ADDTRE:
532 S_REG[ADDTRE] = value & 0xFFFFFFFE;
533 printf("Address trap(unemulated): %08x\n", value);
534 break;
535
536 case CHCW:
537 S_REG[CHCW] = value & 0x2;
538
539 switch(value & 0x31)
540 {
541 default: printf("Undefined cache control bit combination: %08x\n", value);
542 break;
543
544 case 0x00: break;
545
546 case 0x01: CacheClear(timestamp, (value >> 20) & 0xFFF, (value >> 8) & 0xFFF);
547 break;
548
549 case 0x10: CacheDump(timestamp, value & ~0xFF);
550 break;
551
552 case 0x20: CacheRestore(timestamp, value & ~0xFF);
553 break;
554 }
555 break;
556 }
557 }
558
559 INLINE uint32 V810::GetSREG(unsigned int which)
560 {
561 uint32 ret;
562
563 if(which != 24 && which != 25 && which >= 8)
564 {
565 printf("STSR from reserved system register: 0x%02x", which);
566 }
567
568 ret = S_REG[which];
569
570 return(ret);
571 }
572
573 #define RB_SETPC(new_pc_raw) \
574 { \
575 const uint32 new_pc = new_pc_raw; /* So RB_SETPC(RB_GETPC()) won't mess up */ \
576 if(RB_AccurateMode) \
577 PC = new_pc; \
578 else \
579 { \
580 PC_ptr = &FastMap[(new_pc) >> V810_FAST_MAP_SHIFT][(new_pc)]; \
581 PC_base = PC_ptr - (new_pc); \
582 } \
583 }
584
585 #define RB_PCRELCHANGE(delta) { \
586 if(RB_AccurateMode) \
587 PC += (delta); \
588 else \
589 { \
590 uint32 PC_tmp = RB_GETPC(); \
591 PC_tmp += (delta); \
592 RB_SETPC(PC_tmp); \
593 } \
594 }
595
596 #define RB_INCPCBY2() { if(RB_AccurateMode) PC += 2; else PC_ptr += 2; }
597 #define RB_INCPCBY4() { if(RB_AccurateMode) PC += 4; else PC_ptr += 4; }
598
599 #define RB_DECPCBY2() { if(RB_AccurateMode) PC -= 2; else PC_ptr -= 2; }
600 #define RB_DECPCBY4() { if(RB_AccurateMode) PC -= 4; else PC_ptr -= 4; }
601
602
603 // Define accurate mode defines
604 #define RB_GETPC() PC
605 #ifdef _MSC_VER
606 #define RB_RDOP(PC_offset) RDOP(timestamp, PC + PC_offset)
607 #else
608 #define RB_RDOP(PC_offset, ...) RDOP(timestamp, PC + PC_offset, ## __VA_ARGS__)
609 #endif
610
611 void V810::Run_Accurate(int32 MDFN_FASTCALL (*event_handler)(const v810_timestamp_t timestamp))
612 {
613 const bool RB_AccurateMode = true;
614
615 #define RB_ADDBT(n,o,p)
616 #define RB_CPUHOOK(n)
617
618 #include "v810_oploop.inc"
619
620 #undef RB_CPUHOOK
621 #undef RB_ADDBT
622 }
623
624 #ifdef WANT_DEBUGGER
625
626 /* Make sure class member variable v810_timestamp is synchronized to our local copy, since we'll read it externally if a system
627 reset/power occurs when in step mode or similar.
628 */
629 #define RB_CPUHOOK_DBG(n) { if(CPUHook) { v810_timestamp = timestamp_rl; CPUHook(timestamp_rl, n); } }
630
631 void V810::Run_Accurate_Debug(int32 MDFN_FASTCALL (*event_handler)(const v810_timestamp_t timestamp))
632 {
633 const bool RB_AccurateMode = true;
634
635 #define RB_ADDBT(n,o,p) { if(ADDBT) ADDBT(n,o,p); }
636 /* Make sure class member variable v810_timestamp is synchronized to our local copy, since we'll read it externally if a system
637 reset/power occurs when in step mode or similar.
638 */
639 #define RB_CPUHOOK(n) RB_CPUHOOK_DBG(n)
640 #define RB_DEBUGMODE
641
642 #include "v810_oploop.inc"
643
644 #undef RB_DEBUGMODE
645 #undef RB_CPUHOOK
646 #undef RB_ADDBT
647 }
648 #endif
649
650 //
651 // Undefine accurate mode defines
652 //
653 #undef RB_GETPC
654 #undef RB_RDOP
655
656
657
658 //
659 // Define fast mode defines
660 //
661 #define RB_GETPC() ((uint32)(PC_ptr - PC_base))
662
663 #ifdef _MSC_VER
664 #define RB_RDOP(PC_offset, b) LoadU16_LE((uint16 *)&PC_ptr[PC_offset])
665 #else
666 #define RB_RDOP(PC_offset, ...) LoadU16_LE((uint16 *)&PC_ptr[PC_offset])
667 #endif
668
669 void V810::Run_Fast(int32 MDFN_FASTCALL (*event_handler)(const v810_timestamp_t timestamp))
670 {
671 const bool RB_AccurateMode = false;
672
673 #define RB_ADDBT(n,o,p)
674 #define RB_CPUHOOK(n)
675
676 #include "v810_oploop.inc"
677
678 #undef RB_CPUHOOK
679 #undef RB_ADDBT
680 }
681
682 #ifdef WANT_DEBUGGER
683 void V810::Run_Fast_Debug(int32 MDFN_FASTCALL (*event_handler)(const v810_timestamp_t timestamp))
684 {
685 const bool RB_AccurateMode = false;
686
687 #define RB_ADDBT(n,o,p) { if(ADDBT) ADDBT(n,o,p); }
688 #define RB_CPUHOOK(n) RB_CPUHOOK_DBG(n)
689 #define RB_DEBUGMODE
690
691 #include "v810_oploop.inc"
692
693 #undef RB_DEBUGMODE
694 #undef RB_CPUHOOK
695 #undef RB_ADDBT
696 }
697 #endif
698
699 //
700 // Undefine fast mode defines
701 //
702 #undef RB_GETPC
703 #undef RB_RDOP
704
705 v810_timestamp_t V810::Run(int32 MDFN_FASTCALL (*event_handler)(const v810_timestamp_t timestamp))
706 {
707 Running = true;
708
709 #ifdef WANT_DEBUGGER
710 if(CPUHook || ADDBT)
711 {
712 if(EmuMode == V810_EMU_MODE_FAST)
713 Run_Fast_Debug(event_handler);
714 else
715 Run_Accurate_Debug(event_handler);
716 }
717 else
718 #endif
719 {
720 if(EmuMode == V810_EMU_MODE_FAST)
721 Run_Fast(event_handler);
722 else
723 Run_Accurate(event_handler);
724 }
725 return(v810_timestamp);
726 }
727
728 void V810::Exit(void)
729 {
730 Running = false;
731 }
732
733 #ifdef WANT_DEBUGGER
734 void V810::SetCPUHook(void (*newhook)(const v810_timestamp_t timestamp, uint32 PC), void (*new_ADDBT)(uint32 old_PC, uint32 new_PC, uint32))
735 {
736 CPUHook = newhook;
737 ADDBT = new_ADDBT;
738 }
739 #endif
740
741 uint32 V810::GetPC(void)
742 {
743 if(EmuMode == V810_EMU_MODE_ACCURATE)
744 return(PC);
745 else
746 {
747 return(PC_ptr - PC_base);
748 }
749 }
750
751 void V810::SetPC(uint32 new_pc)
752 {
753 if(EmuMode == V810_EMU_MODE_ACCURATE)
754 PC = new_pc;
755 else
756 {
757 PC_ptr = &FastMap[new_pc >> V810_FAST_MAP_SHIFT][new_pc];
758 PC_base = PC_ptr - new_pc;
759 }
760 }
761
762 uint32 V810::GetPR(const unsigned int which)
763 {
764 assert(which <= 0x1F);
765
766
767 return(which ? P_REG[which] : 0);
768 }
769
770 void V810::SetPR(const unsigned int which, uint32 value)
771 {
772 assert(which <= 0x1F);
773
774 if(which)
775 P_REG[which] = value;
776 }
777
778 uint32 V810::GetSR(const unsigned int which)
779 {
780 assert(which <= 0x1F);
781
782 return(GetSREG(which));
783 }
784
785 void V810::SetSR(const unsigned int which, uint32 value)
786 {
787 assert(which <= 0x1F);
788
789 // SetSREG(timestamp, which, value);
790 }
791
792
793 #define BSTR_OP_MOV dst_cache &= ~(1 << dstoff); dst_cache |= ((src_cache >> srcoff) & 1) << dstoff;
794 #define BSTR_OP_NOT dst_cache &= ~(1 << dstoff); dst_cache |= (((src_cache >> srcoff) & 1) ^ 1) << dstoff;
795
796 #define BSTR_OP_XOR dst_cache ^= ((src_cache >> srcoff) & 1) << dstoff;
797 #define BSTR_OP_OR dst_cache |= ((src_cache >> srcoff) & 1) << dstoff;
798 #define BSTR_OP_AND dst_cache &= ~((((src_cache >> srcoff) & 1) ^ 1) << dstoff);
799
800 #define BSTR_OP_XORN dst_cache ^= (((src_cache >> srcoff) & 1) ^ 1) << dstoff;
801 #define BSTR_OP_ORN dst_cache |= (((src_cache >> srcoff) & 1) ^ 1) << dstoff;
802 #define BSTR_OP_ANDN dst_cache &= ~(((src_cache >> srcoff) & 1) << dstoff);
803
804 INLINE uint32 V810::BSTR_RWORD(v810_timestamp_t &timestamp, uint32 A)
805 {
806 if(MemReadBus32[A >> 24])
807 {
808 timestamp += 2;
809 return(MemRead32(timestamp, A));
810 }
811 else
812 {
813 uint32 ret;
814
815 timestamp += 2;
816 ret = MemRead16(timestamp, A);
817
818 timestamp += 2;
819 ret |= MemRead16(timestamp, A | 2) << 16;
820 return(ret);
821 }
822 }
823
824 INLINE void V810::BSTR_WWORD(v810_timestamp_t &timestamp, uint32 A, uint32 V)
825 {
826 if(MemWriteBus32[A >> 24])
827 {
828 timestamp += 2;
829 MemWrite32(timestamp, A, V);
830 }
831 else
832 {
833 timestamp += 2;
834 MemWrite16(timestamp, A, V & 0xFFFF);
835
836 timestamp += 2;
837 MemWrite16(timestamp, A | 2, V >> 16);
838 }
839 }
840
841 #define DO_BSTR(op) { \
842 while(len) \
843 { \
844 if(!have_src_cache) \
845 { \
846 have_src_cache = true; \
847 src_cache = BSTR_RWORD(timestamp, src); \
848 } \
849 \
850 if(!have_dst_cache) \
851 { \
852 have_dst_cache = true; \
853 dst_cache = BSTR_RWORD(timestamp, dst); \
854 } \
855 \
856 op; \
857 srcoff = (srcoff + 1) & 0x1F; \
858 dstoff = (dstoff + 1) & 0x1F; \
859 len--; \
860 \
861 if(!srcoff) \
862 { \
863 src += 4; \
864 have_src_cache = false; \
865 } \
866 \
867 if(!dstoff) \
868 { \
869 BSTR_WWORD(timestamp, dst, dst_cache); \
870 dst += 4; \
871 have_dst_cache = false; \
872 if(timestamp >= next_event_ts) \
873 break; \
874 } \
875 } \
876 if(have_dst_cache) \
877 BSTR_WWORD(timestamp, dst, dst_cache); \
878 }
879
880 INLINE bool V810::Do_BSTR_Search(v810_timestamp_t &timestamp, const int inc_mul, unsigned int bit_test)
881 {
882 uint32 srcoff = (P_REG[27] & 0x1F);
883 uint32 len = P_REG[28];
884 uint32 bits_skipped = P_REG[29];
885 uint32 src = (P_REG[30] & 0xFFFFFFFC);
886 bool found = false;
887
888 #if 0
889 // TODO: Better timing.
890 if(!in_bstr) // If we're just starting the execution of this instruction(kind of spaghetti-code), so FIXME if we change
891 // bstr handling in v810_oploop.inc
892 {
893 timestamp += 13 - 1;
894 }
895 #endif
896
897 while(len)
898 {
899 if(!have_src_cache)
900 {
901 have_src_cache = true;
902 timestamp++;
903 src_cache = BSTR_RWORD(timestamp, src);
904 }
905
906 if(((src_cache >> srcoff) & 1) == bit_test)
907 {
908 found = true;
909
910 /* Fix the bit offset and word address to "1 bit before" it was found */
911 srcoff -= inc_mul * 1;
912 if(srcoff & 0x20) /* Handles 0x1F->0x20(0x00) and 0x00->0xFFFF... */
913 {
914 src -= inc_mul * 4;
915 srcoff &= 0x1F;
916 }
917 break;
918 }
919 srcoff = (srcoff + inc_mul * 1) & 0x1F;
920 bits_skipped++;
921 len--;
922
923 if(!srcoff)
924 {
925 have_src_cache = false;
926 src += inc_mul * 4;
927 if(timestamp >= next_event_ts)
928 break;
929 }
930 }
931
932 P_REG[27] = srcoff;
933 P_REG[28] = len;
934 P_REG[29] = bits_skipped;
935 P_REG[30] = src;
936
937
938 if(found) // Set Z flag to 0 if the bit was found
939 SetFlag(PSW_Z, 0);
940 else if(!len) // ...and if the search is over, and the bit was not found, set it to 1
941 SetFlag(PSW_Z, 1);
942
943 if(found) // Bit found, so don't continue the search.
944 return(false);
945
946 return((bool)len); // Continue the search if any bits are left to search.
947 }
948
949 bool V810::bstr_subop(v810_timestamp_t &timestamp, int sub_op, int arg1)
950 {
951 if((sub_op >= 0x10) || (!(sub_op & 0x8) && sub_op >= 0x4))
952 {
953 printf("%08x\tBSR Error: %04x\n", PC,sub_op);
954
955 SetPC(GetPC() - 2);
956 Exception(INVALID_OP_HANDLER_ADDR, ECODE_INVALID_OP);
957
958 return(false);
959 }
960
961 // printf("BSTR: %02x, %02x %02x; src: %08x, dst: %08x, len: %08x\n", sub_op, P_REG[27], P_REG[26], P_REG[30], P_REG[29], P_REG[28]);
962
963 if(sub_op & 0x08)
964 {
965 uint32 dstoff = (P_REG[26] & 0x1F);
966 uint32 srcoff = (P_REG[27] & 0x1F);
967 uint32 len = P_REG[28];
968 uint32 dst = (P_REG[29] & 0xFFFFFFFC);
969 uint32 src = (P_REG[30] & 0xFFFFFFFC);
970
971 #if 0
972 // Be careful not to cause 32-bit integer overflow, and careful about not shifting by 32.
973 // TODO:
974
975 // Read src[0], src[4] into shifter.
976 // Read dest[0].
977 DO_BSTR_PROLOGUE(); // if(len) { blah blah blah masking blah }
978 src_cache = BSTR_RWORD(timestamp, src);
979
980 if((uint64)(srcoff + len) > 0x20)
981 src_cache |= (uint64)BSTR_RWORD(timestamp, src + 4) << 32;
982
983 dst_cache = BSTR_RWORD(timestamp, dst);
984
985 if(len)
986 {
987 uint32 dst_preserve_mask;
988 uint32 dst_change_mask;
989
990 dst_preserve_mask = (1U << dstoff) - 1;
991
992 if((uint64)(dstoff + len) < 0x20)
993 dst_preserve_mask |= ((1U << ((0x20 - (dstoff + len)) & 0x1F)) - 1) << (dstoff + len);
994
995 dst_change_mask = ~dst_preserve_mask;
996
997 src_cache = BSTR_RWORD(timestamp, src);
998 src_cache |= (uint64)BSTR_RWORD(timestamp, src + 4) << 32;
999 dst_cache = BSTR_RWORD(timestamp, dst);
1000
1001 dst_cache = (dst_cache & dst_preserve_mask) | ((dst_cache OP_THINGY_HERE (src_cache >> srcoff)) & dst_change_mask);
1002 BSTR_WWORD(timestamp, dst, dst_cache);
1003
1004 if((uint64)(dstoff + len) < 0x20)
1005 {
1006 srcoff += len;
1007 dstoff += len;
1008 len = 0;
1009 }
1010 else
1011 {
1012 srcoff += (0x20 - dstoff);
1013 dstoff = 0;
1014 len -= (0x20 - dstoff);
1015 dst += 4;
1016 }
1017
1018 if(srcoff >= 0x20)
1019 {
1020 srcoff &= 0x1F;
1021 src += 4;
1022
1023 if(len)
1024 {
1025 src_cache >>= 32;
1026 src_cache |= (uint64)BSTR_RWORD(timestamp, src + 4) << 32;
1027 }
1028 }
1029 }
1030
1031 DO_BSTR_PRIMARY(); // while(len >= 32) (do allow interruption; interrupt and emulator-return -
1032 // they must be handled differently!)
1033 while(len >= 32)
1034 {
1035 dst_cache = BSTR_RWORD(timestamp, dst);
1036 dst_cache = OP_THINGY_HERE(dst_cache, src_cache >> srcoff);
1037 BSTR_WWORD(timestamp, dst, dst_cache);
1038 len -= 32;
1039 dst += 4;
1040 src += 4;
1041 src_cache >>= 32;
1042 src_cache |= (uint64)BSTR_RWORD(timestamp, src + 4) << 32;
1043 }
1044
1045 DO_BSTR_EPILOGUE(); // if(len) { blah blah blah masking blah }
1046 if(len)
1047 {
1048 uint32 dst_preserve_mask;
1049 uint32 dst_change_mask;
1050
1051 dst_preserve_mask = (1U << ((0x20 - len) & 0x1F) << len;
1052 dst_change_mask = ~dst_preserve_mask;
1053
1054 dst_cache = BSTR_RWORD(timestamp, dst);
1055 dst_cache = OP_THINGY_HERE(dst_cache, src_cache >> srcoff);
1056 BSTR_WWORD(timestamp, dst, dst_cache);
1057 dstoff += len;
1058 srcoff += len;
1059
1060 if(srcoff >= 0x20)
1061 {
1062 srcoff &= 0x1F;
1063 src += 4;
1064 }
1065 len = 0;
1066 }
1067 #endif
1068
1069 switch(sub_op)
1070 {
1071 case ORBSU: DO_BSTR(BSTR_OP_OR); break;
1072
1073 case ANDBSU: DO_BSTR(BSTR_OP_AND); break;
1074
1075 case XORBSU: DO_BSTR(BSTR_OP_XOR); break;
1076
1077 case MOVBSU: DO_BSTR(BSTR_OP_MOV); break;
1078
1079 case ORNBSU: DO_BSTR(BSTR_OP_ORN); break;
1080
1081 case ANDNBSU: DO_BSTR(BSTR_OP_ANDN); break;
1082
1083 case XORNBSU: DO_BSTR(BSTR_OP_XORN); break;
1084
1085 case NOTBSU: DO_BSTR(BSTR_OP_NOT); break;
1086 }
1087
1088 P_REG[26] = dstoff;
1089 P_REG[27] = srcoff;
1090 P_REG[28] = len;
1091 P_REG[29] = dst;
1092 P_REG[30] = src;
1093
1094 return((bool)P_REG[28]);
1095 }
1096 else
1097 {
1098 printf("BSTR Search: %02x\n", sub_op);
1099 }
1100
1101 return(Do_BSTR_Search(timestamp, ((sub_op & 1) ? -1 : 1), (sub_op & 0x2) >> 1));
1102 }
1103
1104 INLINE void V810::SetFPUOPNonFPUFlags(uint32 result)
1105 {
1106 // Now, handle flag setting
1107 SetFlag(PSW_OV, 0);
1108
1109 if(!(result & 0x7FFFFFFF)) // Check to see if exponent and mantissa are 0
1110 {
1111 // If Z flag is set, S and CY should be clear, even if it's negative 0(confirmed on real thing with subf.s, at least).
1112 SetFlag(PSW_Z, 1);
1113 SetFlag(PSW_S, 0);
1114 SetFlag(PSW_CY, 0);
1115 }
1116 else
1117 {
1118 SetFlag(PSW_Z, 0);
1119 SetFlag(PSW_S, result & 0x80000000);
1120 SetFlag(PSW_CY, result & 0x80000000);
1121 }
1122 //printf("MEOW: %08x\n", S_REG[PSW] & (PSW_S | PSW_CY));
1123 }
1124
1125 INLINE bool V810::CheckFPInputException(uint32 fpval)
1126 {
1127 // Zero isn't a subnormal! (OR IS IT *DUN DUN DUNNN* ;b)
1128 if(!(fpval & 0x7FFFFFFF))
1129 return(false);
1130
1131 switch((fpval >> 23) & 0xFF)
1132 {
1133 case 0x00: // Subnormal
1134 case 0xFF: // NaN or infinity
1135 {
1136 //puts("New FPU FRO");
1137
1138 S_REG[PSW] |= PSW_FRO;
1139
1140 SetPC(GetPC() - 4);
1141 Exception(FPU_HANDLER_ADDR, ECODE_FRO);
1142 }
1143 return(true); // Yes, exception occurred
1144 }
1145 return(false); // No, no exception occurred.
1146 }
1147
1148 bool V810::FPU_DoesExceptionKillResult(void)
1149 {
1150 if(float_exception_flags & float_flag_invalid)
1151 return(true);
1152
1153 if(float_exception_flags & float_flag_divbyzero)
1154 return(true);
1155
1156
1157 // Return false here, so that the result of this calculation IS put in the output register.
1158 // (Incidentally, to get the result of operations on overflow to match a real V810, required a bit of hacking of the SoftFloat code to "wrap" the exponent
1159 // on overflow,
1160 // rather than generating an infinity. The wrapping behavior is specified in IEE 754 AFAIK, and is useful in cases where you divide a huge number
1161 // by another huge number, and fix the result afterwards based on the number of overflows that occurred. Probably requires some custom assembly code,
1162 // though. And it's the kind of thing you'd see in an engineering or physics program, not in a perverted video game :b).
1163 // Oh, and just a note to self, FPR is NOT set when an overflow occurs. Or it is in certain cases?
1164 if(float_exception_flags & float_flag_overflow)
1165 return(false);
1166
1167 return(false);
1168 }
1169
1170 void V810::FPU_DoException(void)
1171 {
1172 if(float_exception_flags & float_flag_invalid)
1173 {
1174 //puts("New FPU Invalid");
1175
1176 S_REG[PSW] |= PSW_FIV;
1177
1178 SetPC(GetPC() - 4);
1179 Exception(FPU_HANDLER_ADDR, ECODE_FIV);
1180
1181 return;
1182 }
1183
1184 if(float_exception_flags & float_flag_divbyzero)
1185 {
1186 //puts("New FPU Divide by Zero");
1187
1188 S_REG[PSW] |= PSW_FZD;
1189
1190 SetPC(GetPC() - 4);
1191 Exception(FPU_HANDLER_ADDR, ECODE_FZD);
1192
1193 return;
1194 }
1195
1196 if(float_exception_flags & float_flag_underflow)
1197 {
1198 //puts("New FPU Underflow");
1199
1200 S_REG[PSW] |= PSW_FUD;
1201 }
1202
1203 if(float_exception_flags & float_flag_inexact)
1204 {
1205 S_REG[PSW] |= PSW_FPR;
1206 //puts("New FPU Precision Degradation");
1207 }
1208
1209 // FPR can be set along with overflow, so put the overflow exception handling at the end here(for Exception() messes with PSW).
1210 if(float_exception_flags & float_flag_overflow)
1211 {
1212 //puts("New FPU Overflow");
1213
1214 S_REG[PSW] |= PSW_FOV;
1215
1216 SetPC(GetPC() - 4);
1217 Exception(FPU_HANDLER_ADDR, ECODE_FOV);
1218 }
1219 }
1220
1221 bool V810::IsSubnormal(uint32 fpval)
1222 {
1223 if( ((fpval >> 23) & 0xFF) == 0 && (fpval & ((1 << 23) - 1)) )
1224 return(true);
1225
1226 return(false);
1227 }
1228
1229 INLINE void V810::FPU_Math_Template(float32 (*func)(float32, float32), uint32 arg1, uint32 arg2)
1230 {
1231 if(CheckFPInputException(P_REG[arg1]) || CheckFPInputException(P_REG[arg2]))
1232 {
1233
1234 }
1235 else
1236 {
1237 uint32 result;
1238
1239 float_exception_flags = 0;
1240 result = func(P_REG[arg1], P_REG[arg2]);
1241
1242 if(IsSubnormal(result))
1243 {
1244 float_exception_flags |= float_flag_underflow;
1245 float_exception_flags |= float_flag_inexact;
1246 }
1247
1248 //printf("Result: %08x, %02x; %02x\n", result, (result >> 23) & 0xFF, float_exception_flags);
1249
1250 if(!FPU_DoesExceptionKillResult())
1251 {
1252 // Force it to +/- zero before setting S/Z based off of it(confirmed with subf.s on real V810, at least).
1253 if(float_exception_flags & float_flag_underflow)
1254 result &= 0x80000000;
1255
1256 SetFPUOPNonFPUFlags(result);
1257 SetPREG(arg1, result);
1258 }
1259 FPU_DoException();
1260 }
1261 }
1262
1263 void V810::fpu_subop(v810_timestamp_t &timestamp, int sub_op, int arg1, int arg2)
1264 {
1265 //printf("FPU: %02x\n", sub_op);
1266 if(VBMode)
1267 {
1268 switch(sub_op)
1269 {
1270 case XB: timestamp++; // Unknown
1271 P_REG[arg1] = (P_REG[arg1] & 0xFFFF0000) | ((P_REG[arg1] & 0xFF) << 8) | ((P_REG[arg1] & 0xFF00) >> 8);
1272 return;
1273
1274 case XH: timestamp++; // Unknown
1275 P_REG[arg1] = (P_REG[arg1] << 16) | (P_REG[arg1] >> 16);
1276 return;
1277
1278 // Does REV use arg1 or arg2 for the source register?
1279 case REV: timestamp++; // Unknown
1280 printf("Revvie bits\n");
1281 {
1282 // Public-domain code snippet from: http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
1283 uint32 v = P_REG[arg2]; // 32-bit word to reverse bit order
1284
1285 // swap odd and even bits
1286 v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
1287 // swap consecutive pairs
1288 v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);
1289 // swap nibbles ...
1290 v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
1291 // swap bytes
1292 v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);
1293 // swap 2-byte long pairs
1294 v = ( v >> 16 ) | ( v << 16);
1295
1296 P_REG[arg1] = v;
1297 }
1298 return;
1299
1300 case MPYHW: timestamp += 9 - 1; // Unknown?
1301 P_REG[arg1] = (int32)(int16)(P_REG[arg1] & 0xFFFF) * (int32)(int16)(P_REG[arg2] & 0xFFFF);
1302 return;
1303 }
1304 }
1305
1306 switch(sub_op)
1307 {
1308 // Virtual-Boy specific(probably!)
1309 default:
1310 {
1311 SetPC(GetPC() - 4);
1312 Exception(INVALID_OP_HANDLER_ADDR, ECODE_INVALID_OP);
1313 }
1314 break;
1315
1316 case CVT_WS:
1317 timestamp += 5;
1318 {
1319 uint32 result;
1320
1321 float_exception_flags = 0;
1322 result = int32_to_float32((int32)P_REG[arg2]);
1323
1324 if(!FPU_DoesExceptionKillResult())
1325 {
1326 SetPREG(arg1, result);
1327 SetFPUOPNonFPUFlags(result);
1328 }
1329 else
1330 {
1331 puts("Exception on CVT.WS?????"); // This shouldn't happen, but just in case there's a bug...
1332 }
1333 FPU_DoException();
1334 }
1335 break; // End CVT.WS
1336
1337 case CVT_SW:
1338 timestamp += 8;
1339 if(CheckFPInputException(P_REG[arg2]))
1340 {
1341
1342 }
1343 else
1344 {
1345 int32 result;
1346
1347 float_exception_flags = 0;
1348 result = float32_to_int32(P_REG[arg2]);
1349
1350 if(!FPU_DoesExceptionKillResult())
1351 {
1352 SetPREG(arg1, result);
1353 SetFlag(PSW_OV, 0);
1354 SetSZ(result);
1355 }
1356 FPU_DoException();
1357 }
1358 break; // End CVT.SW
1359
1360 case ADDF_S: timestamp += 8;
1361 FPU_Math_Template(float32_add, arg1, arg2);
1362 break;
1363
1364 case SUBF_S: timestamp += 11;
1365 FPU_Math_Template(float32_sub, arg1, arg2);
1366 break;
1367
1368 case CMPF_S: timestamp += 6;
1369 // Don't handle this like subf.s because the flags
1370 // have slightly different semantics(mostly regarding underflow/subnormal results) (confirmed on real V810).
1371 if(CheckFPInputException(P_REG[arg1]) || CheckFPInputException(P_REG[arg2]))
1372 {
1373
1374 }
1375 else
1376 {
1377 SetFlag(PSW_OV, 0);
1378
1379 if(float32_eq(P_REG[arg1], P_REG[arg2]))
1380 {
1381 SetFlag(PSW_Z, 1);
1382 SetFlag(PSW_S, 0);
1383 SetFlag(PSW_CY, 0);
1384 }
1385 else
1386 {
1387 SetFlag(PSW_Z, 0);
1388
1389 if(float32_lt(P_REG[arg1], P_REG[arg2]))
1390 {
1391 SetFlag(PSW_S, 1);
1392 SetFlag(PSW_CY, 1);
1393 }
1394 else
1395 {
1396 SetFlag(PSW_S, 0);
1397 SetFlag(PSW_CY, 0);
1398 }
1399 }
1400 } // else of if(CheckFP...
1401 break;
1402
1403 case MULF_S: timestamp += 7;
1404 FPU_Math_Template(float32_mul, arg1, arg2);
1405 break;
1406
1407 case DIVF_S: timestamp += 43;
1408 FPU_Math_Template(float32_div, arg1, arg2);
1409 break;
1410
1411 case TRNC_SW:
1412 timestamp += 7;
1413
1414 if(CheckFPInputException(P_REG[arg2]))
1415 {
1416
1417 }
1418 else
1419 {
1420 int32 result;
1421
1422 float_exception_flags = 0;
1423 result = float32_to_int32_round_to_zero(P_REG[arg2]);
1424
1425 if(!FPU_DoesExceptionKillResult())
1426 {
1427 SetPREG(arg1, result);
1428 SetFlag(PSW_OV, 0);
1429 SetSZ(result);
1430 }
1431 FPU_DoException();
1432 }
1433 break; // end TRNC.SW
1434 }
1435 }
1436
1437 // Generate exception
1438 void V810::Exception(uint32 handler, uint16 eCode)
1439 {
1440 // Exception overhead is unknown.
1441
1442 #ifdef WANT_DEBUGGER
1443 if(ADDBT)
1444 {
1445 uint32 old_PC = GetPC();
1446
1447 if((eCode & 0xFFE0) == 0xFFA0) // Trap instruction(PC is pointing to next instruction at this point)
1448 old_PC -= 2;
1449
1450 ADDBT(old_PC, handler, eCode);
1451 }
1452 #endif
1453
1454 printf("Exception: %08x %04x\n", handler, eCode);
1455
1456 // Invalidate our bitstring state(forces the instruction to be re-read, and the r/w buffers reloaded).
1457 in_bstr = false;
1458 have_src_cache = false;
1459 have_dst_cache = false;
1460
1461 if(S_REG[PSW] & PSW_NP) // Fatal exception
1462 {
1463 printf("Fatal exception; Code: %08x, ECR: %08x, PSW: %08x, PC: %08x\n", eCode, S_REG[ECR], S_REG[PSW], PC);
1464 Halted = HALT_FATAL_EXCEPTION;
1465 IPendingCache = 0;
1466 return;
1467 }
1468 else if(S_REG[PSW] & PSW_EP) //Double Exception
1469 {
1470 S_REG[FEPC] = GetPC();
1471 S_REG[FEPSW] = S_REG[PSW];
1472
1473 S_REG[ECR] = (S_REG[ECR] & 0xFFFF) | (eCode << 16);
1474 S_REG[PSW] |= PSW_NP;
1475 S_REG[PSW] |= PSW_ID;
1476 S_REG[PSW] &= ~PSW_AE;
1477
1478 SetPC(0xFFFFFFD0);
1479 IPendingCache = 0;
1480 return;
1481 }
1482 else // Regular exception
1483 {
1484 S_REG[EIPC] = GetPC();
1485 S_REG[EIPSW] = S_REG[PSW];
1486 S_REG[ECR] = (S_REG[ECR] & 0xFFFF0000) | eCode;
1487 S_REG[PSW] |= PSW_EP;
1488 S_REG[PSW] |= PSW_ID;
1489 S_REG[PSW] &= ~PSW_AE;
1490
1491 SetPC(handler);
1492 IPendingCache = 0;
1493 return;
1494 }
1495 }
1496
1497 int V810::StateAction(StateMem *sm, int load, int data_only)
1498 {
1499 uint32 *cache_tag_temp = NULL;
1500 uint32 *cache_data_temp = NULL;
1501 bool *cache_data_valid_temp = NULL;
1502 uint32 PC_tmp = GetPC();
1503
1504 if(EmuMode == V810_EMU_MODE_ACCURATE)
1505 {
1506 cache_tag_temp = (uint32 *)malloc(sizeof(uint32 *) * 128);
1507 cache_data_temp = (uint32 *)malloc(sizeof(uint32 *) * 128 * 2);
1508 cache_data_valid_temp = (bool *)malloc(sizeof(bool *) * 128 * 2);
1509
1510 if(!cache_tag_temp || !cache_data_temp || !cache_data_valid_temp)
1511 {
1512 if(cache_tag_temp)
1513 free(cache_tag_temp);
1514
1515 if(cache_data_temp)
1516 free(cache_data_temp);
1517
1518 if(cache_data_valid_temp)
1519 free(cache_data_valid_temp);
1520
1521 return(0);
1522 }
1523 if(!load)
1524 {
1525 for(int i = 0; i < 128; i++)
1526 {
1527 cache_tag_temp[i] = Cache[i].tag;
1528
1529 cache_data_temp[i * 2 + 0] = Cache[i].data[0];
1530 cache_data_temp[i * 2 + 1] = Cache[i].data[1];
1531
1532 cache_data_valid_temp[i * 2 + 0] = Cache[i].data_valid[0];
1533 cache_data_valid_temp[i * 2 + 1] = Cache[i].data_valid[1];
1534 }
1535 }
1536 else // If we're loading, go ahead and clear the cache temporaries,
1537 // in case the save state was saved while in fast mode
1538 // and the cache data isn't present and thus won't be loaded.
1539 {
1540 memset(cache_tag_temp, 0, sizeof(uint32) * 128);
1541 memset(cache_data_temp, 0, sizeof(uint32) * 128 * 2);
1542 memset(cache_data_valid_temp, 0, sizeof(bool) * 128 * 2);
1543 }
1544 }
1545
1546 int32 next_event_ts_delta = next_event_ts - v810_timestamp;
1547
1548 SFORMAT StateRegs[] =
1549 {
1550 SFARRAY32(P_REG, 32),
1551 SFARRAY32(S_REG, 32),
1552 SFVARN(PC_tmp, "PC"),
1553 SFVAR(Halted),
1554
1555 SFVAR(lastop),
1556
1557 SFARRAY32(cache_tag_temp, 128),
1558 SFARRAY32(cache_data_temp, 128 * 2),
1559 SFARRAYB(cache_data_valid_temp, 128 * 2),
1560
1561 SFVAR(ilevel), // Perhaps remove in future?
1562 SFVAR(next_event_ts_delta),
1563
1564 // Bitstring stuff:
1565 SFVAR(src_cache),
1566 SFVAR(dst_cache),
1567 SFVAR(have_src_cache),
1568 SFVAR(have_dst_cache),
1569 SFVAR(in_bstr),
1570 SFVAR(in_bstr_to),
1571
1572 SFEND
1573 };
1574
1575 int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "V810");
1576
1577 if(load)
1578 {
1579 // std::max is sanity check for a corrupted save state to not crash emulation,
1580 // std::min<int64>(0x7FF... is a sanity check and for the case where next_event_ts is set to an extremely large value to
1581 // denote that it's not happening anytime soon, which could cause an overflow if our current timestamp is larger
1582 // than what it was when the state was saved.
1583 next_event_ts = std::max<int64>(v810_timestamp, std::min<int64>(0x7FFFFFFF, (int64)v810_timestamp + next_event_ts_delta));
1584
1585 RecalcIPendingCache();
1586
1587 SetPC(PC_tmp);
1588 if(EmuMode == V810_EMU_MODE_ACCURATE)
1589 {
1590 for(int i = 0; i < 128; i++)
1591 {
1592 Cache[i].tag = cache_tag_temp[i];
1593
1594 Cache[i].data[0] = cache_data_temp[i * 2 + 0];
1595 Cache[i].data[1] = cache_data_temp[i * 2 + 1];
1596
1597 Cache[i].data_valid[0] = cache_data_valid_temp[i * 2 + 0];
1598 Cache[i].data_valid[1] = cache_data_valid_temp[i * 2 + 1];
1599
1600 //printf("%d %08x %08x %08x %d %d\n", i, Cache[i].tag << 10, Cache[i].data[0], Cache[i].data[1], Cache[i].data_valid[0], Cache[i].data_valid[1]);
1601 }
1602 }
1603 }
1604
1605 if(EmuMode == V810_EMU_MODE_ACCURATE)
1606 {
1607 free(cache_tag_temp);
1608 free(cache_data_temp);
1609 free(cache_data_valid_temp);
1610 }
1611
1612 return(ret);
1613 }