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