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