improved debug
[fceu.git] / general.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Ben Parnell
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
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24
25 #include "types.h"
26
27 #include "general.h"
28 #include "state.h"
29 #include "version.h"
30 #include "svga.h"
31 #include "driver.h"
32
33 char *marray[1]={"Error allocating memory!"};
34
35 static char BaseDirectory[2048];
36 static char FileBase[2048];
37 static char FileExt[2048];
38 static char FileBaseDirectory[2048];
39
40 void FCEUI_SetBaseDirectory(char *dir)
41 {
42  strncpy(BaseDirectory,dir,2047);
43  BaseDirectory[2047]=0;
44 }
45
46 static char *odirs[FCEUIOD__COUNT]={0,0,0,0,0};     // odirs, odors. ^_^
47
48 void FCEUI_SetDirOverride(int which, char *n)
49 {
50  odirs[which]=n;
51  if(which==FCEUIOD_STATE)
52   SaveStateRefresh();
53 }
54
55 /* We should probably use snprintf(), but many C libraries don't seem to
56    have this function.
57 */
58 char *FCEU_MakeFName(int type, int id1, char *cd1)
59 {
60  static uint8 ret[2048];
61
62  ret[0]=0;
63  switch(type)
64  {
65   case FCEUMKF_STATE:if(odirs[FCEUIOD_STATE])
66                       sprintf(((char*)ret),"%s"PSS"%s.fc%d",odirs[FCEUIOD_STATE],FileBase,id1);
67                      else
68                       sprintf(((char*)ret),"%s"PSS"fcs"PSS"%s.fc%d",BaseDirectory,FileBase,id1);
69                      break;
70   case FCEUMKF_SNAP:
71                     if(FSettings.SnapName)
72                     {
73                      if(odirs[FCEUIOD_SNAPS])
74                       sprintf(((char*)ret),"%s"PSS"%s-%d.%s",odirs[FCEUIOD_SNAPS],FileBase,id1,cd1);
75                      else
76                       sprintf(((char*)ret),"%s"PSS"snaps"PSS"%s-%d.%s",BaseDirectory,FileBase,id1,cd1);
77                     }
78                     else
79                     {
80                      if(odirs[FCEUIOD_SNAPS])
81                       sprintf(((char*)ret),"%s"PSS"%d.%s",odirs[FCEUIOD_SNAPS],id1,cd1);
82                      else
83                       sprintf(((char*)ret),"%s"PSS"snaps"PSS"%d.%s",BaseDirectory,id1,cd1);
84                     }
85                     break;
86   case FCEUMKF_SAV:if(odirs[FCEUIOD_NV])
87                    {
88                     sprintf(((char*)ret),"%s"PSS"%s.%s",odirs[FCEUIOD_NV],FileBase,cd1);
89                    }
90                    else
91                    {
92                     if(FSettings.SUnderBase)
93                      sprintf(((char*)ret),"%s"PSS"sav"PSS"%s.%s",BaseDirectory,FileBase,cd1);
94                     else
95                      sprintf(((char*)ret),"%s"PSS"%s.%s",FileBaseDirectory,FileBase,cd1);
96                    }
97                    break;
98   case FCEUMKF_CHEAT:
99                      if(odirs[FCEUIOD_CHEATS])
100                       sprintf(((char*)ret),"%s"PSS"%s.cht",odirs[FCEUIOD_CHEATS],FileBase);
101                      else
102                       sprintf(((char*)ret),"%s"PSS"cheats"PSS"%s.cht",BaseDirectory,FileBase);
103                      break;
104   case FCEUMKF_GGROM:sprintf(((char*)ret),"%s"PSS"gg.rom",BaseDirectory);break;
105   case FCEUMKF_FDSROM:sprintf(((char*)ret),"%s"PSS"disksys.rom",BaseDirectory);break;
106   case FCEUMKF_PALETTE:
107                        if(odirs[FCEUIOD_MISC])
108                         sprintf(((char*)ret),"%s"PSS"%s.pal",odirs[FCEUIOD_MISC],FileBase);
109                        else
110                         sprintf(((char*)ret),"%s"PSS"gameinfo"PSS"%s.pal",BaseDirectory,FileBase);
111                        break;
112  }
113  return((char *)ret);
114 }
115
116 void GetFileBase(char *f)
117 {
118         char *tp1,*tp3;
119
120  #if PSS_STYLE==4
121      tp1=((char *)strrchr(f,':'));
122  #elif PSS_STYLE==1
123      tp1=((char *)strrchr(f,'/'));
124  #else
125      tp1=((char *)strrchr(f,'\\'));
126   #if PSS_STYLE!=3
127      tp3=((char *)strrchr(f,'/'));
128      if(tp1<tp3) tp1=tp3;
129   #endif
130  #endif
131      if(!tp1)
132      {
133       tp1=f;
134       strcpy(FileBaseDirectory,".");
135      }
136      else
137      {
138       memcpy(FileBaseDirectory,f,tp1-f);
139       FileBaseDirectory[tp1-f]=0;
140       tp1++;
141      }
142
143      if((tp3=strrchr(f,'.'))!=NULL)
144      {
145       memcpy(FileBase,tp1,tp3-tp1);
146       FileBase[tp3-tp1]=0;
147       strcpy(FileExt,tp3+1);
148      }
149      else
150      {
151       strcpy(FileBase,tp1);
152       FileExt[0]=0;
153      }
154 }
155
156 uint32 uppow2(uint32 n)
157 {
158  int x;
159
160  for(x=31;x>=0;x--)
161   if(n&(1<<x))
162   {
163    if((1<<x)!=n)
164     return(1<<(x+1));
165    break;
166   }
167  return n;
168 }
169