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