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