svp compiler direct calls
[picodrive.git] / Pico / carthw / svp / svp.c
1 // The SVP chip emulator
2
3 // (c) Copyright 2008, Grazvydas "notaz" Ignotas
4 // Free for non-commercial use.
5
6 // For commercial use, separate licencing terms must be obtained.
7
8
9 #include "../../PicoInt.h"
10 #include "compiler.h"
11 #ifdef __GP2X__
12 #include <sys/mman.h>
13 #endif
14
15 svp_t *svp = NULL;
16 int PicoSVPCycles = 1000; // cycles/line
17
18 /* save state stuff */
19 typedef enum {
20         CHUNK_IRAM = CHUNK_CARTHW,
21         CHUNK_DRAM,
22         CHUNK_SSP
23 } chunk_name_e;
24
25 static carthw_state_chunk svp_states[] =
26 {
27         { CHUNK_IRAM, 0x800,                 NULL },
28         { CHUNK_DRAM, sizeof(svp->dram),     NULL },
29         { CHUNK_SSP,  sizeof(svp->ssp1601),  NULL },
30         { 0,          0,                     NULL }
31 };
32
33
34 static void PicoSVPReset(void)
35 {
36         elprintf(EL_SVP, "SVP reset");
37
38         memcpy(svp->iram_rom + 0x800, Pico.rom + 0x800, 0x20000 - 0x800);
39         ssp1601_reset(&svp->ssp1601);
40         if (!(PicoOpt&0x20000))
41                 ssp1601_dyn_reset(&svp->ssp1601);
42 }
43
44
45 static void PicoSVPLine(int count)
46 {
47         static int inited = 0;
48         if (!(svp->ssp1601.gr[SSP_PM0].h & 2) && !inited) return;
49         inited = 1;
50
51         // ???
52         if (PicoOpt&0x20000)
53                 ssp1601_run(PicoSVPCycles * count);
54         else
55                 ssp1601_dyn_run(PicoSVPCycles * count);
56
57         // test mode
58         //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff;
59 }
60
61
62 static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp)
63 {
64         if (source < Pico.romsize) // Rom
65         {
66                 source -= 2;
67                 *srcp = (unsigned short *)(Pico.rom + (source&~1));
68                 *limitp = (unsigned short *)(Pico.rom + Pico.romsize);
69                 return 1;
70         }
71         else if ((source & 0xfe0000) == 0x300000)
72         {
73                 elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len);
74                 source &= 0x1fffe;
75                 source -= 2;
76                 *srcp = (unsigned short *)(svp->dram + source);
77                 *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram));
78                 return 1;
79         }
80         else
81                 elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len);
82
83         return 0;
84 }
85
86
87 void PicoSVPInit(void)
88 {
89 #ifdef __GP2X__
90         int ret;
91         ret = munmap(tcache, TCACHE_SIZE);
92         printf("munmap tcache: %i\n", ret);
93 #endif
94 }
95
96
97 static void PicoSVPShutdown(void)
98 {
99 #ifdef __GP2X__
100         // also unmap tcache
101         PicoSVPInit();
102 #endif
103 }
104
105
106 void PicoSVPStartup(void)
107 {
108         void *tmp;
109
110         elprintf(EL_SVP, "SVP init");
111
112         tmp = realloc(Pico.rom, 0x200000 + sizeof(*svp));
113         if (tmp == NULL)
114         {
115                 elprintf(EL_STATUS|EL_SVP, "OOM for SVP data");
116                 return;
117         }
118
119         Pico.rom = tmp;
120         svp = (void *) ((char *)tmp + 0x200000);
121         memset(svp, 0, sizeof(*svp));
122
123 #ifdef __GP2X__
124         tmp = mmap(tcache, TCACHE_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
125         printf("mmap tcache: %p, asked %p\n", tmp, tcache);
126 #endif
127
128         // init SVP compiler
129         if (!(PicoOpt&0x20000)) {
130                 if (ssp1601_dyn_startup()) return;
131         }
132
133         // init ok, setup hooks..
134         PicoRead16Hook = PicoSVPRead16;
135         PicoWrite8Hook = PicoSVPWrite8;
136         PicoWrite16Hook = PicoSVPWrite16;
137         PicoDmaHook = PicoSVPDma;
138         PicoResetHook = PicoSVPReset;
139         PicoLineHook = PicoSVPLine;
140         PicoCartUnloadHook = PicoSVPShutdown;
141
142         // save state stuff
143         svp_states[0].ptr = svp->iram_rom;
144         svp_states[1].ptr = svp->dram;
145         svp_states[2].ptr = &svp->ssp1601;
146         carthw_chunks = svp_states;
147 }
148
149