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