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; |
2385f273 |
16 | int PicoSVPCycles = 820; // cycles/line, just a guess |
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 }, |
e122fae6 |
29 | { CHUNK_SSP, sizeof(svp->ssp1601) - sizeof(svp->ssp1601.drc), NULL }, |
945c2fdc |
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 | { |
726bbb3e |
47 | if (PicoOpt&0x20000) |
48 | ssp1601_run(PicoSVPCycles * count); |
49 | else |
50 | ssp1601_dyn_run(PicoSVPCycles * count); |
d26dc685 |
51 | |
52 | // test mode |
53 | //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff; |
017512f2 |
54 | } |
55 | |
56 | |
5de27868 |
57 | static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp) |
017512f2 |
58 | { |
d4ca252d |
59 | if (source < Pico.romsize) // Rom |
60 | { |
50483b53 |
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) |
017512f2 |
67 | { |
5de27868 |
68 | elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len); |
017512f2 |
69 | source &= 0x1fffe; |
30752975 |
70 | source -= 2; |
5de27868 |
71 | *srcp = (unsigned short *)(svp->dram + source); |
72 | *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram)); |
017512f2 |
73 | return 1; |
74 | } |
d26dc685 |
75 | else |
76 | elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len); |
017512f2 |
77 | |
78 | return 0; |
79 | } |
80 | |
81 | |
f53f286a |
82 | void PicoSVPInit(void) |
e807ac75 |
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) |
f53f286a |
102 | { |
f8ef8ff7 |
103 | void *tmp; |
104 | |
017512f2 |
105 | elprintf(EL_SVP, "SVP init"); |
f8ef8ff7 |
106 | |
107 | tmp = realloc(Pico.rom, 0x200000 + sizeof(*svp)); |
108 | if (tmp == NULL) |
109 | { |
017512f2 |
110 | elprintf(EL_STATUS|EL_SVP, "OOM for SVP data"); |
f8ef8ff7 |
111 | return; |
112 | } |
113 | |
d5276282 |
114 | //PicoOpt |= 0x20000; |
f8ef8ff7 |
115 | Pico.rom = tmp; |
116 | svp = (void *) ((char *)tmp + 0x200000); |
117 | memset(svp, 0, sizeof(*svp)); |
118 | |
e807ac75 |
119 | #ifdef __GP2X__ |
120 | tmp = mmap(tcache, TCACHE_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED|MAP_ANONYMOUS, -1, 0); |
121 | printf("mmap tcache: %p, asked %p\n", tmp, tcache); |
122 | #endif |
123 | |
726bbb3e |
124 | // init SVP compiler |
125 | if (!(PicoOpt&0x20000)) { |
e807ac75 |
126 | if (ssp1601_dyn_startup()) return; |
726bbb3e |
127 | } |
128 | |
f8ef8ff7 |
129 | // init ok, setup hooks.. |
130 | PicoRead16Hook = PicoSVPRead16; |
131 | PicoWrite8Hook = PicoSVPWrite8; |
132 | PicoWrite16Hook = PicoSVPWrite16; |
133 | PicoDmaHook = PicoSVPDma; |
017512f2 |
134 | PicoResetHook = PicoSVPReset; |
135 | PicoLineHook = PicoSVPLine; |
e807ac75 |
136 | PicoCartUnloadHook = PicoSVPShutdown; |
945c2fdc |
137 | |
138 | // save state stuff |
139 | svp_states[0].ptr = svp->iram_rom; |
140 | svp_states[1].ptr = svp->dram; |
141 | svp_states[2].ptr = &svp->ssp1601; |
142 | carthw_chunks = svp_states; |
f53f286a |
143 | } |
144 | |
e807ac75 |
145 | |