32x: some missed code from MAME, minor tweaks
[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 "../../pico_int.h"
10 #include "compiler.h"
11 #if defined(__linux__) && defined(ARM)
12 #include <sys/mman.h>
13 #endif
14
15 svp_t *svp = NULL;
16 int PicoSVPCycles = 850; // 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 #ifndef PSP
42         if ((PicoOpt&POPT_EN_SVP_DRC) && svp_dyn_ready)
43                 ssp1601_dyn_reset(&svp->ssp1601);
44 #endif
45 }
46
47
48 static void PicoSVPLine(void)
49 {
50         int count = 1;
51 #if defined(ARM) || defined(PSP)
52         // performance hack
53         static int delay_lines = 0;
54         delay_lines++;
55         if ((Pico.m.scanline&0xf) != 0xf && Pico.m.scanline != 261 && Pico.m.scanline != 311)
56                 return;
57         count = delay_lines;
58         delay_lines = 0;
59 #endif
60
61 #ifndef PSP
62         if ((PicoOpt&POPT_EN_SVP_DRC) && svp_dyn_ready)
63                 ssp1601_dyn_run(PicoSVPCycles * count);
64         else
65 #endif
66         {
67                 ssp1601_run(PicoSVPCycles * count);
68                 svp_dyn_ready = 0; // just in case
69         }
70
71         // test mode
72         //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff;
73 }
74
75
76 static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp)
77 {
78         if (source < Pico.romsize) // Rom
79         {
80                 source -= 2;
81                 *srcp = (unsigned short *)(Pico.rom + (source&~1));
82                 *limitp = (unsigned short *)(Pico.rom + Pico.romsize);
83                 return 1;
84         }
85         else if ((source & 0xfe0000) == 0x300000)
86         {
87                 elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len);
88                 source &= 0x1fffe;
89                 source -= 2;
90                 *srcp = (unsigned short *)(svp->dram + source);
91                 *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram));
92                 return 1;
93         }
94         else
95                 elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len);
96
97         return 0;
98 }
99
100
101 void PicoSVPInit(void)
102 {
103 #if defined(__linux__) && defined(ARM)
104         int ret;
105         ret = munmap(tcache, SSP_DRC_SIZE);
106         printf("munmap tcache: %i\n", ret);
107 #endif
108 }
109
110
111 static void PicoSVPShutdown(void)
112 {
113 #if defined(__linux__) && defined(ARM)
114         // also unmap tcache
115         PicoSVPInit();
116 #endif
117 }
118
119
120 void PicoSVPStartup(void)
121 {
122         void *tmp;
123
124         elprintf(EL_STATUS, "SVP startup");
125
126         tmp = realloc(Pico.rom, 0x200000 + sizeof(*svp));
127         if (tmp == NULL)
128         {
129                 elprintf(EL_STATUS|EL_SVP, "OOM for SVP data");
130                 return;
131         }
132
133         //PicoOpt &= ~0x20000;
134         Pico.rom = tmp;
135         svp = (void *) ((char *)tmp + 0x200000);
136         memset(svp, 0, sizeof(*svp));
137
138 #if defined(__linux__) && defined(ARM)
139         tmp = mmap(tcache, SSP_DRC_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
140         printf("mmap tcache: %p, asked %p\n", tmp, tcache);
141 #endif
142
143         // init SVP compiler
144         svp_dyn_ready = 0;
145 #ifndef PSP
146         if (PicoOpt&POPT_EN_SVP_DRC) {
147                 if (ssp1601_dyn_startup()) return;
148                 svp_dyn_ready = 1;
149         }
150 #endif
151
152         // init ok, setup hooks..
153         PicoCartMemSetup = PicoSVPMemSetup;
154         PicoDmaHook = PicoSVPDma;
155         PicoResetHook = PicoSVPReset;
156         PicoLineHook = PicoSVPLine;
157         PicoCartUnloadHook = PicoSVPShutdown;
158
159         // save state stuff
160         svp_states[0].ptr = svp->iram_rom;
161         svp_states[1].ptr = svp->dram;
162         svp_states[2].ptr = &svp->ssp1601;
163         carthw_chunks = svp_states;
164         PicoAHW |= PAHW_SVP;
165 }
166
167