bugfixes, new config system and messed code for it
[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
53         // test mode
54         //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff;
55 }
56
57
58 static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp)
59 {
60         if (source < Pico.romsize) // Rom
61         {
62                 source -= 2;
63                 *srcp = (unsigned short *)(Pico.rom + (source&~1));
64                 *limitp = (unsigned short *)(Pico.rom + Pico.romsize);
65                 return 1;
66         }
67         else if ((source & 0xfe0000) == 0x300000)
68         {
69                 elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len);
70                 source &= 0x1fffe;
71                 source -= 2;
72                 *srcp = (unsigned short *)(svp->dram + source);
73                 *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram));
74                 return 1;
75         }
76         else
77                 elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len);
78
79         return 0;
80 }
81
82
83 void PicoSVPInit(void)
84 {
85 #ifdef __GP2X__
86         int ret;
87         ret = munmap(tcache, SSP_DRC_SIZE);
88         printf("munmap tcache: %i\n", ret);
89 #endif
90 }
91
92
93 static void PicoSVPShutdown(void)
94 {
95 #ifdef __GP2X__
96         // also unmap tcache
97         PicoSVPInit();
98 #endif
99 }
100
101
102 void PicoSVPStartup(void)
103 {
104         void *tmp;
105
106         elprintf(EL_SVP, "SVP init");
107
108         tmp = realloc(Pico.rom, 0x200000 + sizeof(*svp));
109         if (tmp == NULL)
110         {
111                 elprintf(EL_STATUS|EL_SVP, "OOM for SVP data");
112                 return;
113         }
114
115         //PicoOpt &= ~0x20000;
116         Pico.rom = tmp;
117         svp = (void *) ((char *)tmp + 0x200000);
118         memset(svp, 0, sizeof(*svp));
119
120 #ifdef __GP2X__
121         tmp = mmap(tcache, SSP_DRC_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
122         printf("mmap tcache: %p, asked %p\n", tmp, tcache);
123 #endif
124
125         // init SVP compiler
126         svp_dyn_ready = 0;
127         if (PicoOpt&0x20000) {
128                 if (ssp1601_dyn_startup()) return;
129                 svp_dyn_ready = 1;
130         }
131
132         // init ok, setup hooks..
133         PicoRead16Hook = PicoSVPRead16;
134         PicoWrite8Hook = PicoSVPWrite8;
135         PicoWrite16Hook = PicoSVPWrite16;
136         PicoDmaHook = PicoSVPDma;
137         PicoResetHook = PicoSVPReset;
138         PicoLineHook = PicoSVPLine;
139         PicoCartUnloadHook = PicoSVPShutdown;
140
141         // save state stuff
142         svp_states[0].ptr = svp->iram_rom;
143         svp_states[1].ptr = svp->dram;
144         svp_states[2].ptr = &svp->ssp1601;
145         carthw_chunks = svp_states;
146 }
147
148