1 /*  Copyright 2003-2005 Guillaume Duhamel
2     Copyright 2004-2006, 2013 Theo Berkau
3 
4     This file is part of Yabause.
5 
6     Yabause is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     Yabause is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with Yabause; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19 */
20 
21 #ifndef SH2CORE_H
22 #define SH2CORE_H
23 
24 #include "core.h"
25 #include "memory.h"
26 #include "sh2cache.h"
27 
28 #define SH2CORE_DEFAULT     -1
29 #define MAX_INTERRUPTS 50
30 
31 #ifdef MACH
32 #undef MACH
33 #endif
34 
35 // UBC Flags
36 #define BBR_CPA_NONE			(0 << 6)
37 #define BBR_CPA_CPU				(1 << 6)
38 #define BBR_CPA_PER				(2 << 6)
39 
40 #define BBR_IDA_NONE			(0 << 4)
41 #define BBR_IDA_INST	 		(1 << 4)
42 #define BBR_IDA_DATA			(2 << 4)
43 
44 #define BBR_RWA_NONE			(0 << 2)
45 #define BBR_RWA_READ	 		(1 << 2)
46 #define BBR_RWA_WRITE			(2 << 2)
47 
48 #define BBR_SZA_NONE			(0 << 0)
49 #define BBR_SZA_BYTE	 		(1 << 0)
50 #define BBR_SZA_WORD			(2 << 0)
51 #define BBR_SZA_LONGWORD	 	(3 << 0)
52 
53 #define BRCR_CMFCA				(1 << 15)
54 #define BRCR_CMFPA				(1 << 14)
55 #define BRCR_EBBA				(1 << 13)
56 #define BRCR_UMD				(1 << 12)
57 #define BRCR_PCBA				(1 << 10)
58 
59 #define BRCR_CMFCB				(1 << 7)
60 #define BRCR_CMFPB				(1 << 6)
61 #define BRCR_SEQ				(1 << 4)
62 #define BRCR_DBEB				(1 << 3)
63 #define BRCR_PCBB				(1 << 2)
64 
65 typedef struct
66 {
67    u32 R[16];
68 
69 #ifdef WORDS_BIGENDIAN
70   union {
71     struct {
72       u32 reserved1:22;
73       u32 M:1;
74       u32 Q:1;
75       u32 I:4;
76       u32 reserved0:2;
77       u32 S:1;
78       u32 T:1;
79     } part;
80     u32 all;
81   } SR;
82 #else
83   union {
84     struct {
85       u32 T:1;
86       u32 S:1;
87       u32 reserved0:2;
88       u32 I:4;
89       u32 Q:1;
90       u32 M:1;
91       u32 reserved1:22;
92     } part;
93     u32 all;
94   } SR;
95 #endif
96 
97    u32 GBR;
98    u32 VBR;
99    u32 MACH;
100    u32 MACL;
101    u32 PR;
102    u32 PC;
103 } sh2regs_struct;
104 
105 typedef struct
106 {
107    u8 SMR;     // 0xFFFFFE00
108    u8 BRR;     // 0xFFFFFE01
109    u8 SCR;     // 0xFFFFFE02
110    u8 TDR;     // 0xFFFFFE03
111    u8 SSR;     // 0xFFFFFE04
112    u8 RDR;     // 0xFFFFFE05
113    u8 TIER;    // 0xFFFFFE10
114    u8 FTCSR;   // 0xFFFFFE11
115 
116 #ifdef WORDS_BIGENDIAN
117   union {
118     struct {
119       u16 H:8; // 0xFFFFFE12
120       u16 L:8; // 0xFFFFFE13
121     } part;
122     u16 all;
123   } FRC;
124 #else
125   union {
126     struct {
127       u16 L:8; // 0xFFFFFE13
128       u16 H:8; // 0xFFFFFE12
129     } part;
130     u16 all;
131   } FRC;
132 #endif
133    u16 OCRA;   // 0xFFFFFE14/0xFFFFFE15
134    u16 OCRB;   // 0xFFFFFE14/0xFFFFFE15
135    u8 TCR;     // 0xFFFFFE16
136    u8 TOCR;    // 0xFFFFFE17
137    u16 FICR;   // 0xFFFFFE18
138                // 0xFFFFFE19
139    u16 IPRB;   // 0xFFFFFE60
140    u16 VCRA;   // 0xFFFFFE62
141    u16 VCRB;   // 0xFFFFFE64
142    u16 VCRC;   // 0xFFFFFE66
143    u16 VCRD;   // 0xFFFFFE68
144    u8 DRCR0;   // 0xFFFFFE71
145    u8 DRCR1;   // 0xFFFFFE72
146    u8 WTCSR;   // 0xFFFFFE80
147    u8 WTCNT;   // 0xFFFFFE81
148    u8 RSTCSR;  // 0xFFFFFE83
149    u8 SBYCR;   // 0xFFFFFE91
150    u8 CCR;     // 0xFFFFFE92
151    u16 ICR;    // 0xFFFFFEE0
152    u16 IPRA;   // 0xFFFFFEE2
153    u16 VCRWDT; // 0xFFFFFEE4
154    u32 DVSR;   // 0xFFFFFF00
155    u32 DVDNT;  // 0xFFFFFF04
156    u32 DVCR;   // 0xFFFFFF08
157    u32 VCRDIV; // 0xFFFFFF0C
158    u32 DVDNTH; // 0xFFFFFF10
159    u32 DVDNTL; // 0xFFFFFF14
160    u32 DVDNTUH; // 0xFFFFFF18
161    u32 DVDNTUL; // 0xFFFFFF1C
162 #ifdef WORDS_BIGENDIAN
163   union {
164     struct {
165       u32 H:16; // 0xFFFFFF40
166       u32 L:16; // 0xFFFFFF42
167     } part;
168     u16 all;
169   } BARA;
170 
171   union {
172     struct {
173       u32 H:16; // 0xFFFFFF44
174       u32 L:16; // 0xFFFFFF46
175     } part;
176     u16 all;
177   } BAMRA;
178 #else
179   union {
180     struct {
181       u32 L:16; // 0xFFFFFF42
182       u32 H:16; // 0xFFFFFF40
183     } part;
184     u16 all;
185   } BARA;
186 
187   union {
188     struct {
189       u32 L:16; // 0xFFFFFF46
190       u32 H:16; // 0xFFFFFF44
191     } part;
192     u16 all;
193   } BAMRA;
194 #endif
195    u32 BBRA;   // 0xFFFFFF48
196 #ifdef WORDS_BIGENDIAN
197   union {
198     struct {
199       u32 H:16; // 0xFFFFFF60
200       u32 L:16; // 0xFFFFFF62
201     } part;
202     u16 all;
203   } BARB;
204 
205   union {
206     struct {
207       u32 H:16; // 0xFFFFFF64
208       u32 L:16; // 0xFFFFFF66
209     } part;
210     u16 all;
211   } BAMRB;
212 #else
213   union {
214     struct {
215       u32 L:16; // 0xFFFFFF62
216       u32 H:16; // 0xFFFFFF60
217     } part;
218     u16 all;
219   } BARB;
220 
221   union {
222     struct {
223       u32 L:16; // 0xFFFFFF66
224       u32 H:16; // 0xFFFFFF64
225     } part;
226     u16 all;
227   } BAMRB;
228 #endif
229    u32 BBRB;   // 0xFFFFFF68
230 #ifdef WORDS_BIGENDIAN
231   union {
232     struct {
233       u32 H:16; // 0xFFFFFF70
234       u32 L:16; // 0xFFFFFF72
235     } part;
236     u16 all;
237   } BDRB;
238 
239   union {
240     struct {
241       u32 H:16; // 0xFFFFFF74
242       u32 L:16; // 0xFFFFFF76
243     } part;
244     u16 all;
245   } BDMRB;
246 #else
247   union {
248     struct {
249       u32 L:16; // 0xFFFFFF72
250       u32 H:16; // 0xFFFFFF70
251     } part;
252     u16 all;
253   } BDRB;
254 
255   union {
256     struct {
257       u32 L:16; // 0xFFFFFF76
258       u32 H:16; // 0xFFFFFF74
259     } part;
260     u16 all;
261   } BDMRB;
262 #endif
263    u32 BRCR;   // 0xFFFFFF78
264    u32 SAR0;   // 0xFFFFFF80
265    u32 DAR0;   // 0xFFFFFF84
266    u32 TCR0;   // 0xFFFFFF88
267    u32 CHCR0;  // 0xFFFFFF8C
268    u32 SAR1;   // 0xFFFFFF90
269    u32 DAR1;   // 0xFFFFFF94
270    u32 TCR1;   // 0xFFFFFF98
271    u32 CHCR1;  // 0xFFFFFF9C
272    u32 VCRDMA0;// 0xFFFFFFA0
273    u32 VCRDMA1;// 0xFFFFFFA8
274    u32 DMAOR;  // 0xFFFFFFB0
275    u16 BCR1;   // 0xFFFFFFE0
276    u16 BCR2;   // 0xFFFFFFE4
277    u16 WCR;    // 0xFFFFFFE8
278    u16 MCR;    // 0xFFFFFFEC
279    u16 RTCSR;  // 0xFFFFFFF0
280    u16 RTCNT;  // 0xFFFFFFF4
281    u16 RTCOR;  // 0xFFFFFFF8
282    cache_enty cache;
283    int dma0_active;
284    int dma1_active;
285    int dma_robin;
286 } Onchip_struct;
287 
288 typedef struct
289 {
290    u8 vector;
291    u8 level;
292 } interrupt_struct;
293 
294 typedef struct
295 {
296   u32 addr;
297 } codebreakpoint_struct;
298 
299 typedef struct
300 {
301   u32 addr;
302   u32 flags;
303   readbytefunc oldreadbyte;
304   readwordfunc oldreadword;
305   readlongfunc oldreadlong;
306   writebytefunc oldwritebyte;
307   writewordfunc oldwriteword;
308   writelongfunc oldwritelong;
309 } memorybreakpoint_struct;
310 
311 #define MAX_BREAKPOINTS 10
312 
313 typedef struct
314 {
315    codebreakpoint_struct codebreakpoint[MAX_BREAKPOINTS];
316    int numcodebreakpoints;
317    memorybreakpoint_struct memorybreakpoint[MAX_BREAKPOINTS];
318    int nummemorybreakpoints;
319    void (*BreakpointCallBack)(void *, u32, void *);
320    void *BreakpointUserData;
321    int inbreakpoint;
322    int breaknow;
323 } breakpoint_struct;
324 
325 typedef struct
326 {
327    u32 addr[256];
328    int numbacktrace;
329 } backtrace_struct;
330 
331 
332 #define BREAK_BYTEREAD  0x1
333 #define BREAK_WORDREAD  0x2
334 #define BREAK_LONGREAD  0x4
335 #define BREAK_BYTEWRITE 0x8
336 #define BREAK_WORDWRITE 0x10
337 #define BREAK_LONGWRITE 0x20
338 
339 enum SH2STEPTYPE
340 {
341    SH2ST_STEPOVER,
342    SH2ST_STEPOUT
343 };
344 
345 enum SHMODELTYPE
346 {
347    SHMT_SH1,
348    SHMT_SH2
349 };
350 
351 typedef struct
352 {
353    u32 addr;
354    u64 count;
355 } tilInfo_struct;
356 
357 typedef struct SH2Interface_struct SH2Interface_struct;
358 
359 #include "sh2_jit.h"
360 
361 struct SH2_struct
362 {
363    struct SH2Interface_struct *core;
364    enum SHMODELTYPE model;
365 
366    void *opcodes[0x10000];
367    void *fetchlist[0x100];
368 
369    writebytefunc WriteByteList[0x1000];
370    writewordfunc WriteWordList[0x1000];
371    writelongfunc WriteLongList[0x1000];
372 
373    readbytefunc ReadByteList[0x1000];
374    readwordfunc ReadWordList[0x1000];
375    readlongfunc ReadLongList[0x1000];
376 
377    writebytefunc MappedMemoryWriteByte;
378    writewordfunc MappedMemoryWriteWord;
379    writelongfunc MappedMemoryWriteLong;
380 
381    readbytefunc MappedMemoryReadByte;
382    readwordfunc MappedMemoryReadWord;
383    readlongfunc MappedMemoryReadLong;
384 
385    sh2regs_struct regs;
386    Onchip_struct onchip;
387 
388    struct Sh2JitContext jit;
389 
390    struct
391    {
392       u32 leftover;
393       u32 shift;
394    } frc;
395 
396    struct
397    {
398         int isenable;
399         int isinterval;
400         u32 leftover;
401         u32 shift;
402    } wdt;
403 
404    interrupt_struct interrupts[MAX_INTERRUPTS];
405    u32 NumberOfInterrupts;
406    u32 AddressArray[0x100];
407    u8 DataArray[0x1000];
408    u32 delay;
409    u32 cycles;
410    u8 isslave;
411    u8 isIdle;
412    u8 isSleeping;
413    u16 instruction;
414    u8 breakpointEnabled;
415    breakpoint_struct bp;
416    u8 backtraceEnabled;
417    backtrace_struct bt;
418    struct
419    {
420       u8 enabled;
421       void (*callBack)(void *, u32, void *);
422       enum SH2STEPTYPE type;
423       union
424       {
425          s32 levels;
426          u32 address;
427       };
428    } stepOverOut;
429    struct
430    {
431       u8 enabled;
432       tilInfo_struct *match;
433       int num;
434       int maxNum;
435    } trackInfLoop;
436 
437 };
438 
439 struct SH2Interface_struct
440 {
441    int id;
442    const char *Name;
443 
444    int (*Init)(enum SHMODELTYPE model, SH2_struct *msh, SH2_struct *ssh);
445    void (*DeInit)();
446    void (*Reset)(SH2_struct *context);
447    void FASTCALL (*Exec)(SH2_struct *context, u32 cycles);
448 
449    void (*GetRegisters)(SH2_struct *context, sh2regs_struct *regs);
450    u32 (*GetGPR)(SH2_struct *context, int num);
451    u32 (*GetSR)(SH2_struct *context);
452    u32 (*GetGBR)(SH2_struct *context);
453    u32 (*GetVBR)(SH2_struct *context);
454    u32 (*GetMACH)(SH2_struct *context);
455    u32 (*GetMACL)(SH2_struct *context);
456    u32 (*GetPR)(SH2_struct *context);
457    u32 (*GetPC)(SH2_struct *context);
458 
459    void (*SetRegisters)(SH2_struct *context, const sh2regs_struct *regs);
460    void (*SetGPR)(SH2_struct *context, int num, u32 value);
461    void (*SetSR)(SH2_struct *context, u32 value);
462    void (*SetGBR)(SH2_struct *context, u32 value);
463    void (*SetVBR)(SH2_struct *context, u32 value);
464    void (*SetMACH)(SH2_struct *context, u32 value);
465    void (*SetMACL)(SH2_struct *context, u32 value);
466    void (*SetPR)(SH2_struct *context, u32 value);
467    void (*SetPC)(SH2_struct *context, u32 value);
468 
469    void (*SendInterrupt)(SH2_struct *context, u8 vector, u8 level);
470    int (*GetInterrupts)(SH2_struct *context,
471                         interrupt_struct interrupts[MAX_INTERRUPTS]);
472    void (*SetInterrupts)(SH2_struct *context, int num_interrupts,
473                          const interrupt_struct interrupts[MAX_INTERRUPTS]);
474 
475    void (*WriteNotify)(u32 start, u32 length);
476 };
477 
478 extern SH2_struct *SH1;
479 extern SH2_struct *MSH2;
480 extern SH2_struct *SSH2;
481 extern SH2Interface_struct *SH2Core;
482 
483 int SH1Init(int coreid);
484 int SH2Init(int coreid);
485 void SH1DeInit(void);
486 void SH2DeInit(void);
487 void SH2Reset(SH2_struct *context);
488 void SH2PowerOn(SH2_struct *context);
489 void FASTCALL SH2Exec(SH2_struct *context, u32 cycles);
490 void SH2SendInterrupt(SH2_struct *context, u8 vector, u8 level);
491 void SH2NMI(SH2_struct *context);
492 void SH2Step(SH2_struct *context);
493 int SH2StepOver(SH2_struct *context, void (*func)(void *, u32, void *));
494 void SH2StepOut(SH2_struct *context, void (*func)(void *, u32, void *));
495 
496 int SH2TrackInfLoopInit(SH2_struct *context);
497 void SH2TrackInfLoopDeInit(SH2_struct *context);
498 void SH2TrackInfLoopStart(SH2_struct *context);
499 void SH2TrackInfLoopStop(SH2_struct *context);
500 void SH2TrackInfLoopClear(SH2_struct *context);
501 
502 void SH2GetRegisters(SH2_struct *context, sh2regs_struct * r);
503 void SH2SetRegisters(SH2_struct *context, sh2regs_struct * r);
504 void SH2WriteNotify(u32 start, u32 length);
505 
506 void SH2SetBreakpointCallBack(SH2_struct *context, void (*func)(void *, u32, void *), void *userdata);
507 int SH2AddCodeBreakpoint(SH2_struct *context, u32 addr);
508 int SH2DelCodeBreakpoint(SH2_struct *context, u32 addr);
509 codebreakpoint_struct *SH2GetBreakpointList(SH2_struct *context);
510 void SH2ClearCodeBreakpoints(SH2_struct *context);
511 
SH2HandleBreakpoints(SH2_struct * context)512 static INLINE void SH2HandleBreakpoints(SH2_struct *context)
513 {
514    int i;
515 
516    for (i=0; i < context->bp.numcodebreakpoints; i++) {
517 
518       if ((context->regs.PC == context->bp.codebreakpoint[i].addr) && context->bp.inbreakpoint == 0) {
519          context->bp.inbreakpoint = 1;
520          if (context->bp.BreakpointCallBack)
521              context->bp.BreakpointCallBack(context, context->bp.codebreakpoint[i].addr, context->bp.BreakpointUserData);
522          context->bp.inbreakpoint = 0;
523       }
524    }
525 
526    if (context->bp.breaknow) {
527       context->bp.breaknow = 0;
528       context->bp.BreakpointCallBack(context, context->regs.PC, context->bp.BreakpointUserData);
529    }
530 }
531 
SH2BreakNow(SH2_struct * context)532 static void SH2BreakNow(SH2_struct *context)
533 {
534    context->bp.breaknow = 1;
535 }
536 
537 int SH2AddMemoryBreakpoint(SH2_struct *context, u32 addr, u32 flags);
538 int SH2DelMemoryBreakpoint(SH2_struct *context, u32 addr);
539 memorybreakpoint_struct *SH2GetMemoryBreakpointList(SH2_struct *context);
540 void SH2ClearMemoryBreakpoints(SH2_struct *context);
541 void SH2HandleBackTrace(SH2_struct *context);
542 u32 *SH2GetBacktraceList(SH2_struct *context, int *size);
543 void SH2HandleStepOverOut(SH2_struct *context);
544 void SH2HandleTrackInfLoop(SH2_struct *context);
545 
546 void DMAExec(SH2_struct *sh);
547 void DMATransfer(SH2_struct *sh, u32 *CHCR, u32 *SAR, u32 *DAR, u32 *TCR, u32 *VCRDMA);
548 void sh2_dma_exec(SH2_struct *sh, u32 cycles);
549 
550 u8 FASTCALL OnchipReadByte(SH2_struct *sh, u32 addr);
551 u16 FASTCALL OnchipReadWord(SH2_struct *sh, u32 addr);
552 u32 FASTCALL OnchipReadLong(SH2_struct *sh, u32 addr);
553 void FASTCALL OnchipWriteByte(SH2_struct *sh, u32 addr, u8 val);
554 void FASTCALL OnchipWriteWord(SH2_struct *sh, u32 addr, u16 val);
555 void FASTCALL OnchipWriteLong(SH2_struct *sh, u32 addr, u32 val);
556 
557 u32 FASTCALL AddressArrayReadLong(SH2_struct *sh, u32 addr);
558 void FASTCALL AddressArrayWriteLong(SH2_struct *sh, u32 addr, u32 val);
559 
560 u8 FASTCALL DataArrayReadByte(SH2_struct *sh, u32 addr);
561 u16 FASTCALL DataArrayReadWord(SH2_struct *sh, u32 addr);
562 u32 FASTCALL DataArrayReadLong(SH2_struct *sh, u32 addr);
563 void FASTCALL DataArrayWriteByte(SH2_struct *sh, u32 addr, u8 val);
564 void FASTCALL DataArrayWriteWord(SH2_struct *sh, u32 addr, u16 val);
565 void FASTCALL DataArrayWriteLong(SH2_struct *sh, u32 addr, u32 val);
566 
567 void FASTCALL MSH2InputCaptureWriteWord(SH2_struct *sh, u32 addr, u16 data);
568 void FASTCALL SSH2InputCaptureWriteWord(SH2_struct *sh, u32 addr, u16 data);
569 
570 int SH2SaveState(SH2_struct *context, FILE *fp);
571 int SH2LoadState(SH2_struct *context, FILE *fp, int version, int size);
572 
573 u32 sh2_dma_access(u32 addr, u32 data, int is_read, int size);
574 
575 #if defined(SH2_DYNAREC)
576 extern SH2Interface_struct SH2Dynarec;
577 #endif
578 
579 #endif
580