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