some warnings fixed, nsf fixed, palettes, more code backported
[fceu.git] / general.c
CommitLineData
c62d2810 1/* FCE Ultra - NES/Famicom Emulator
2 *
3 * Copyright notice for this file:
92764e62 4 * Copyright (C) 2002 Xodnizel
c62d2810 5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
92764e62 21#include <stdio.h>
c62d2810 22#include <stdlib.h>
23#include <string.h>
92764e62 24#include <stdarg.h>
25
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <unistd.h>
c62d2810 29
30#include "types.h"
92764e62 31#include "fce.h"
c62d2810 32
33#include "general.h"
34#include "state.h"
92764e62 35#include "movie.h"
36
c62d2810 37#include "driver.h"
38
92764e62 39#include "md5.h"
40#include "svga.h"
c62d2810 41
42static char BaseDirectory[2048];
92764e62 43char FileBase[2048];
44static char FileExt[2048]; /* Includes the . character, as in ".nes" */
45
c62d2810 46static char FileBaseDirectory[2048];
47
48void FCEUI_SetBaseDirectory(char *dir)
49{
50 strncpy(BaseDirectory,dir,2047);
51 BaseDirectory[2047]=0;
52}
53
54static char *odirs[FCEUIOD__COUNT]={0,0,0,0,0}; // odirs, odors. ^_^
55
56void FCEUI_SetDirOverride(int which, char *n)
57{
92764e62 58// FCEU_PrintError("odirs[%d]=%s->%s", which, odirs[which], n);
59if(which < FCEUIOD__COUNT)
c62d2810 60 odirs[which]=n;
92764e62 61
62#if 0
63 if(FCEUGameInfo) /* Rebuild cache of present states/movies. */
64 {
65 if(which==FCEUIOD_STATE)
66 FCEUSS_CheckStates();
67 else if(which == FCEUIOD_MISC)
68 FCEUMOV_CheckMovies();
69 }
70#endif
71}
72
73#ifndef HAVE_ASPRINTF
74static int asprintf(char **strp, const char *fmt, ...)
75{
76 va_list ap;
77 int ret;
78
79 va_start(ap,fmt);
80 if(!(*strp=malloc(2048)))
81 return(0);
82 ret=vsnprintf(*strp,2048,fmt,ap);
83 va_end(ap);
84 return(ret);
85}
86#endif
87
88char* FCEU_GetPath(int type)
89{
90 char *ret=0;
91 switch(type)
92 {
93 case FCEUMKF_STATE:if(odirs[FCEUIOD_STATE])
94 ret=strdup(odirs[FCEUIOD_STATE]);
95 else
96 asprintf(&ret,"%s"PSS"movie",BaseDirectory);
97 break;
98 case FCEUMKF_MOVIE:if(odirs[FCEUIOD_MISC])
99 ret=strdup(odirs[FCEUIOD_MISC]);
100 else
101 asprintf(&ret,"%s"PSS"movie",BaseDirectory);
102 break;
103 }
104 return(ret);
105}
106
107char *FCEU_MakePath(int type, const char* filebase)
108{
109 char *ret=0;
110
111 switch(type)
112 {
113 case FCEUMKF_MOVIE:if(odirs[FCEUIOD_MISC])
114 asprintf(&ret,"%s"PSS"%s",odirs[FCEUIOD_MISC],filebase);
115 else
116 asprintf(&ret,"%s"PSS"movie"PSS"%s",BaseDirectory,filebase);
117 break;
118 }
119 return(ret);
c62d2810 120}
121
c62d2810 122char *FCEU_MakeFName(int type, int id1, char *cd1)
123{
92764e62 124 char *ret=0;
125 struct stat tmpstat;
c62d2810 126
c62d2810 127 switch(type)
128 {
92764e62 129 case FCEUMKF_NPTEMP: asprintf(&ret,"%s"PSS"m590plqd94fo.tmp",BaseDirectory);break;
130 case FCEUMKF_MOVIE:if(id1>=0)
131 {
132 if(odirs[FCEUIOD_MISC])
133 asprintf(&ret,"%s"PSS"%s.%d.fcm",odirs[FCEUIOD_MISC],FileBase,id1);
134 else
135 asprintf(&ret,"%s"PSS"movie"PSS"%s.%d.fcm",BaseDirectory,FileBase,id1);
136 if(stat(ret,&tmpstat)==-1)
137 {
138 if(odirs[FCEUIOD_MISC])
139 asprintf(&ret,"%s"PSS"%s.%d.fcm",odirs[FCEUIOD_MISC],FileBase,id1);
140 else
141 asprintf(&ret,"%s"PSS"movie"PSS"%s.%d.fcm",BaseDirectory,FileBase,id1);
142 }
143 }
144 else
145 {
146 if(odirs[FCEUIOD_MISC])
147 asprintf(&ret,"%s"PSS"%s.fcm",odirs[FCEUIOD_MISC],FileBase);
148 else
149 asprintf(&ret,"%s"PSS"movie"PSS"%s.fcm",BaseDirectory,FileBase);
150 }
151 break;
c62d2810 152 case FCEUMKF_STATE:if(odirs[FCEUIOD_STATE])
92764e62 153 {
154 asprintf(&ret,"%s"PSS"%s.fc%d",odirs[FCEUIOD_STATE],FileBase,id1);
155// FCEU_PrintError("A");
156 }
c62d2810 157 else
92764e62 158 {
159 asprintf(&ret,"%s"PSS"fcs"PSS"%s.fc%d",BaseDirectory,FileBase,id1);
160// FCEU_PrintError("B");
161 }
162 if(stat(ret,&tmpstat)==-1)
163 {
164 if(odirs[FCEUIOD_STATE])
165 {
166 asprintf(&ret,"%s"PSS"%s.fc%d",odirs[FCEUIOD_STATE],FileBase,id1);
167// FCEU_PrintError("C");
168 }
169 else
170 {
171 asprintf(&ret,"%s"PSS"fcs"PSS"%s.fc%d",BaseDirectory,FileBase,id1);
172// FCEU_PrintError("D");
173 }
174 }
c62d2810 175 break;
176 case FCEUMKF_SNAP:
177 if(FSettings.SnapName)
178 {
179 if(odirs[FCEUIOD_SNAPS])
92764e62 180 asprintf(&ret,"%s"PSS"%s-%d.%s",odirs[FCEUIOD_SNAPS],FileBase,id1,cd1);
c62d2810 181 else
92764e62 182 asprintf(&ret,"%s"PSS"snaps"PSS"%s-%d.%s",BaseDirectory,FileBase,id1,cd1);
c62d2810 183 }
184 else
185 {
186 if(odirs[FCEUIOD_SNAPS])
92764e62 187 asprintf(&ret,"%s"PSS"%d.%s",odirs[FCEUIOD_SNAPS],id1,cd1);
c62d2810 188 else
92764e62 189 asprintf(&ret,"%s"PSS"snaps"PSS"%d.%s",BaseDirectory,id1,cd1);
c62d2810 190 }
191 break;
d97315ac 192 case FCEUMKF_FDS:if(odirs[FCEUIOD_NV])
92764e62 193 asprintf(&ret,"%s"PSS"%s.fds",odirs[FCEUIOD_NV],FileBase);
d97315ac 194 else
92764e62 195 asprintf(&ret,"%s"PSS"sav"PSS"%s.fds",BaseDirectory,FileBase);
d97315ac 196 break;
c62d2810 197 case FCEUMKF_SAV:if(odirs[FCEUIOD_NV])
92764e62 198 asprintf(&ret,"%s"PSS"%s.%s",odirs[FCEUIOD_NV],FileBase,cd1);
c62d2810 199 else
92764e62 200 asprintf(&ret,"%s"PSS"sav"PSS"%s.%s",BaseDirectory,FileBase,cd1);
201 if(stat(ret,&tmpstat)==-1)
c62d2810 202 {
92764e62 203 if(odirs[FCEUIOD_NV])
204 asprintf(&ret,"%s"PSS"%s.%s",odirs[FCEUIOD_NV],FileBase,cd1);
c62d2810 205 else
92764e62 206 asprintf(&ret,"%s"PSS"sav"PSS"%s.%s",BaseDirectory,FileBase,cd1);
c62d2810 207 }
208 break;
209 case FCEUMKF_CHEAT:
210 if(odirs[FCEUIOD_CHEATS])
92764e62 211 asprintf(&ret,"%s"PSS"%s.cht",odirs[FCEUIOD_CHEATS],FileBase);
c62d2810 212 else
92764e62 213 asprintf(&ret,"%s"PSS"cheats"PSS"%s.cht",BaseDirectory,FileBase);
214 break;
215 case FCEUMKF_IPS: asprintf(&ret,"%s"PSS"%s%s.ips",FileBaseDirectory,FileBase,FileExt);
c62d2810 216 break;
92764e62 217 case FCEUMKF_GGROM:asprintf(&ret,"%s"PSS"gg.rom",BaseDirectory);break;
218 case FCEUMKF_FDSROM:asprintf(&ret,"%s"PSS"disksys.rom",BaseDirectory);break;
c62d2810 219 case FCEUMKF_PALETTE:
220 if(odirs[FCEUIOD_MISC])
92764e62 221 asprintf(&ret,"%s"PSS"%s.pal",odirs[FCEUIOD_MISC],FileBase);
c62d2810 222 else
92764e62 223 asprintf(&ret,"%s"PSS"%s.pal",BaseDirectory,FileBase);
c62d2810 224 break;
92764e62 225 case FCEUMKF_MOVIEGLOB:
226 if(odirs[FCEUIOD_MISC])
227// asprintf(&ret,"%s"PSS"%s*.fcm",odirs[FCEUIOD_MISC],FileBase);
228 asprintf(&ret,"%s"PSS"*.???",odirs[FCEUIOD_MISC]);
229 else
230// asprintf(&ret,"%s"PSS"fcs"PSS"%s*.fcm",BaseDirectory,FileBase);
231 asprintf(&ret,"%s"PSS"movie"PSS"*.???",BaseDirectory);
232 break;
233 case FCEUMKF_MOVIEGLOB2:
234 asprintf(&ret,"%s"PSS"*.???",BaseDirectory);
235 break;
236 case FCEUMKF_STATEGLOB:
237 if(odirs[FCEUIOD_STATE])
238 asprintf(&ret,"%s"PSS"%s*.fc?",odirs[FCEUIOD_STATE],FileBase);
239 else
240 asprintf(&ret,"%s"PSS"fcs"PSS"%s*.fc?",BaseDirectory,FileBase);
241 break;
c62d2810 242 }
92764e62 243 return(ret);
c62d2810 244}
245
92764e62 246void GetFileBase(const char *f)
c62d2810 247{
92764e62 248 const char *tp1,*tp3;
c62d2810 249
250 #if PSS_STYLE==4
251 tp1=((char *)strrchr(f,':'));
252 #elif PSS_STYLE==1
253 tp1=((char *)strrchr(f,'/'));
254 #else
255 tp1=((char *)strrchr(f,'\\'));
256 #if PSS_STYLE!=3
257 tp3=((char *)strrchr(f,'/'));
258 if(tp1<tp3) tp1=tp3;
259 #endif
260 #endif
261 if(!tp1)
262 {
263 tp1=f;
264 strcpy(FileBaseDirectory,".");
265 }
266 else
267 {
268 memcpy(FileBaseDirectory,f,tp1-f);
269 FileBaseDirectory[tp1-f]=0;
270 tp1++;
271 }
272
92764e62 273 if(((tp3=strrchr(f,'.'))!=NULL) && (tp3>tp1))
c62d2810 274 {
275 memcpy(FileBase,tp1,tp3-tp1);
276 FileBase[tp3-tp1]=0;
92764e62 277 strcpy(FileExt,tp3);
c62d2810 278 }
279 else
280 {
281 strcpy(FileBase,tp1);
282 FileExt[0]=0;
283 }
284}
285
286uint32 uppow2(uint32 n)
287{
288 int x;
289
290 for(x=31;x>=0;x--)
291 if(n&(1<<x))
292 {
293 if((1<<x)!=n)
294 return(1<<(x+1));
295 break;
296 }
297 return n;
298}
299