d4ca252d |
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 | |
f53f286a |
9 | #include "../../PicoInt.h" |
e807ac75 |
10 | #include "compiler.h" |
11 | #ifdef __GP2X__ |
12 | #include <sys/mman.h> |
13 | #endif |
f53f286a |
14 | |
f8ef8ff7 |
15 | svp_t *svp = NULL; |
3d48ce71 |
16 | int PicoSVPCycles = 850; // cycles/line, just a guess |
1ca2ea4f |
17 | static int svp_dyn_ready = 0; |
f8ef8ff7 |
18 | |
945c2fdc |
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 }, |
e122fae6 |
30 | { CHUNK_SSP, sizeof(svp->ssp1601) - sizeof(svp->ssp1601.drc), NULL }, |
945c2fdc |
31 | { 0, 0, NULL } |
32 | }; |
33 | |
34 | |
017512f2 |
35 | static void PicoSVPReset(void) |
36 | { |
37 | elprintf(EL_SVP, "SVP reset"); |
38 | |
5de27868 |
39 | memcpy(svp->iram_rom + 0x800, Pico.rom + 0x800, 0x20000 - 0x800); |
017512f2 |
40 | ssp1601_reset(&svp->ssp1601); |
6fc57144 |
41 | #ifndef PSP |
602133e1 |
42 | if ((PicoOpt&POPT_EN_SVP_DRC) && svp_dyn_ready) |
726bbb3e |
43 | ssp1601_dyn_reset(&svp->ssp1601); |
6fc57144 |
44 | #endif |
017512f2 |
45 | } |
46 | |
47 | |
0ffefdb8 |
48 | static void PicoSVPLine(int count) |
017512f2 |
49 | { |
6fc57144 |
50 | #ifndef PSP |
602133e1 |
51 | if ((PicoOpt&POPT_EN_SVP_DRC) && svp_dyn_ready) |
726bbb3e |
52 | ssp1601_dyn_run(PicoSVPCycles * count); |
6fc57144 |
53 | else |
54 | #endif |
55 | { |
1ca2ea4f |
56 | ssp1601_run(PicoSVPCycles * count); |
1b13dae0 |
57 | svp_dyn_ready = 0; // just in case |
58 | } |
d26dc685 |
59 | |
60 | // test mode |
61 | //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff; |
017512f2 |
62 | } |
63 | |
64 | |
5de27868 |
65 | static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp) |
017512f2 |
66 | { |
d4ca252d |
67 | if (source < Pico.romsize) // Rom |
68 | { |
50483b53 |
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) |
017512f2 |
75 | { |
5de27868 |
76 | elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len); |
017512f2 |
77 | source &= 0x1fffe; |
30752975 |
78 | source -= 2; |
5de27868 |
79 | *srcp = (unsigned short *)(svp->dram + source); |
80 | *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram)); |
017512f2 |
81 | return 1; |
82 | } |
d26dc685 |
83 | else |
84 | elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len); |
017512f2 |
85 | |
86 | return 0; |
87 | } |
88 | |
89 | |
f53f286a |
90 | void PicoSVPInit(void) |
e807ac75 |
91 | { |
92 | #ifdef __GP2X__ |
93 | int ret; |
1ca2ea4f |
94 | ret = munmap(tcache, SSP_DRC_SIZE); |
e807ac75 |
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) |
f53f286a |
110 | { |
f8ef8ff7 |
111 | void *tmp; |
112 | |
017512f2 |
113 | elprintf(EL_SVP, "SVP init"); |
f8ef8ff7 |
114 | |
115 | tmp = realloc(Pico.rom, 0x200000 + sizeof(*svp)); |
116 | if (tmp == NULL) |
117 | { |
017512f2 |
118 | elprintf(EL_STATUS|EL_SVP, "OOM for SVP data"); |
f8ef8ff7 |
119 | return; |
120 | } |
121 | |
1ca2ea4f |
122 | //PicoOpt &= ~0x20000; |
f8ef8ff7 |
123 | Pico.rom = tmp; |
124 | svp = (void *) ((char *)tmp + 0x200000); |
125 | memset(svp, 0, sizeof(*svp)); |
126 | |
e807ac75 |
127 | #ifdef __GP2X__ |
1ca2ea4f |
128 | tmp = mmap(tcache, SSP_DRC_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED|MAP_ANONYMOUS, -1, 0); |
e807ac75 |
129 | printf("mmap tcache: %p, asked %p\n", tmp, tcache); |
130 | #endif |
131 | |
726bbb3e |
132 | // init SVP compiler |
1ca2ea4f |
133 | svp_dyn_ready = 0; |
6fc57144 |
134 | #ifndef PSP |
602133e1 |
135 | if (PicoOpt&POPT_EN_SVP_DRC) { |
e807ac75 |
136 | if (ssp1601_dyn_startup()) return; |
1ca2ea4f |
137 | svp_dyn_ready = 1; |
726bbb3e |
138 | } |
6fc57144 |
139 | #endif |
726bbb3e |
140 | |
f8ef8ff7 |
141 | // init ok, setup hooks.. |
142 | PicoRead16Hook = PicoSVPRead16; |
143 | PicoWrite8Hook = PicoSVPWrite8; |
144 | PicoWrite16Hook = PicoSVPWrite16; |
145 | PicoDmaHook = PicoSVPDma; |
017512f2 |
146 | PicoResetHook = PicoSVPReset; |
147 | PicoLineHook = PicoSVPLine; |
e807ac75 |
148 | PicoCartUnloadHook = PicoSVPShutdown; |
945c2fdc |
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; |
602133e1 |
155 | PicoAHW |= PAHW_SVP; |
f53f286a |
156 | } |
157 | |
e807ac75 |
158 | |