Finish migrating to new mem handling. Make carthw db external.
[picodrive.git] / pico / carthw / svp / svp.c
CommitLineData
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
efcba75f 9#include "../../pico_int.h"
e807ac75 10#include "compiler.h"
27701801 11#if defined(__linux__) && defined(ARM)
e807ac75 12#include <sys/mman.h>
13#endif
f53f286a 14
f8ef8ff7 15svp_t *svp = NULL;
3d48ce71 16int PicoSVPCycles = 850; // cycles/line, just a guess
1ca2ea4f 17static int svp_dyn_ready = 0;
f8ef8ff7 18
945c2fdc 19/* save state stuff */
20typedef enum {
21 CHUNK_IRAM = CHUNK_CARTHW,
22 CHUNK_DRAM,
23 CHUNK_SSP
24} chunk_name_e;
25
26static 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 35static 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
e7aac062 48static void PicoSVPLine(void)
017512f2 49{
e7aac062 50 int count = 1;
51#if defined(ARM) || defined(PSP)
52 // performance hack
53 static int delay_lines = 0;
54 delay_lines++;
55 if ((Pico.m.scanline&0xf) != 0xf && Pico.m.scanline != 261 && Pico.m.scanline != 311)
56 return;
57 count = delay_lines;
58 delay_lines = 0;
59#endif
60
6fc57144 61#ifndef PSP
602133e1 62 if ((PicoOpt&POPT_EN_SVP_DRC) && svp_dyn_ready)
726bbb3e 63 ssp1601_dyn_run(PicoSVPCycles * count);
6fc57144 64 else
65#endif
66 {
1ca2ea4f 67 ssp1601_run(PicoSVPCycles * count);
1b13dae0 68 svp_dyn_ready = 0; // just in case
69 }
d26dc685 70
71 // test mode
72 //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff;
017512f2 73}
74
75
5de27868 76static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp)
017512f2 77{
d4ca252d 78 if (source < Pico.romsize) // Rom
79 {
50483b53 80 source -= 2;
81 *srcp = (unsigned short *)(Pico.rom + (source&~1));
82 *limitp = (unsigned short *)(Pico.rom + Pico.romsize);
83 return 1;
84 }
85 else if ((source & 0xfe0000) == 0x300000)
017512f2 86 {
5de27868 87 elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len);
017512f2 88 source &= 0x1fffe;
30752975 89 source -= 2;
5de27868 90 *srcp = (unsigned short *)(svp->dram + source);
91 *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram));
017512f2 92 return 1;
93 }
d26dc685 94 else
95 elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len);
017512f2 96
97 return 0;
98}
99
100
f53f286a 101void PicoSVPInit(void)
e807ac75 102{
27701801 103#if defined(__linux__) && defined(ARM)
e807ac75 104 int ret;
1ca2ea4f 105 ret = munmap(tcache, SSP_DRC_SIZE);
e807ac75 106 printf("munmap tcache: %i\n", ret);
107#endif
108}
109
110
111static void PicoSVPShutdown(void)
112{
27701801 113#if defined(__linux__) && defined(ARM)
e807ac75 114 // also unmap tcache
115 PicoSVPInit();
116#endif
117}
118
119
120void PicoSVPStartup(void)
f53f286a 121{
f8ef8ff7 122 void *tmp;
123
45f2f245 124 elprintf(EL_STATUS, "SVP startup");
f8ef8ff7 125
126 tmp = realloc(Pico.rom, 0x200000 + sizeof(*svp));
127 if (tmp == NULL)
128 {
017512f2 129 elprintf(EL_STATUS|EL_SVP, "OOM for SVP data");
f8ef8ff7 130 return;
131 }
132
1ca2ea4f 133 //PicoOpt &= ~0x20000;
f8ef8ff7 134 Pico.rom = tmp;
135 svp = (void *) ((char *)tmp + 0x200000);
136 memset(svp, 0, sizeof(*svp));
137
27701801 138#if defined(__linux__) && defined(ARM)
1ca2ea4f 139 tmp = mmap(tcache, SSP_DRC_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
e807ac75 140 printf("mmap tcache: %p, asked %p\n", tmp, tcache);
141#endif
142
726bbb3e 143 // init SVP compiler
1ca2ea4f 144 svp_dyn_ready = 0;
6fc57144 145#ifndef PSP
602133e1 146 if (PicoOpt&POPT_EN_SVP_DRC) {
e807ac75 147 if (ssp1601_dyn_startup()) return;
1ca2ea4f 148 svp_dyn_ready = 1;
726bbb3e 149 }
6fc57144 150#endif
726bbb3e 151
f8ef8ff7 152 // init ok, setup hooks..
45f2f245 153 PicoCartMemSetup = PicoSVPMemSetup;
f8ef8ff7 154 PicoDmaHook = PicoSVPDma;
017512f2 155 PicoResetHook = PicoSVPReset;
156 PicoLineHook = PicoSVPLine;
e807ac75 157 PicoCartUnloadHook = PicoSVPShutdown;
945c2fdc 158
159 // save state stuff
160 svp_states[0].ptr = svp->iram_rom;
161 svp_states[1].ptr = svp->dram;
162 svp_states[2].ptr = &svp->ssp1601;
163 carthw_chunks = svp_states;
602133e1 164 PicoAHW |= PAHW_SVP;
f53f286a 165}
166
e807ac75 167