1 /* FCE Ultra - NES/Famicom Emulator
3 * Copyright notice for this file:
4 * Copyright (C) 2002 Ben Parnell
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.
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.
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
23 #include "../../driver.h"
25 static void GetString(char *s)
38 /* Get unsigned 16-bit integer from stdin in hex. */
39 static uint32 GetH16(unsigned int def)
47 sscanf(buf+1,"%04x",&def);
49 sscanf(buf,"%04x",&def);
53 /* Get unsigned 8-bit integer from stdin in decimal. */
54 static uint8 Get8(unsigned int def)
61 sscanf(buf,"%d",&def);
65 static int GetYN(int def)
68 printf("(Y/N)[%s]: ",def?"Y":"N");
70 if(buf[0]=='y' || buf[0]=='Y')
72 if(buf[0]=='n' || buf[0]=='N')
82 static int listids[16];
86 void BeginListShow(void)
93 /* Hmm =0 for in list choices, hmm=1 for end of list choices. */
94 /* Return equals 0 to continue, -1 to stop, otherwise a number. */
95 int ListChoice(int hmm)
104 printf(" <'Enter' to continue, (S)top, or enter a number.> ");
106 if(buf[0]=='s' || buf[0]=='S') return(-1);
107 if(buf[0]=='\n') return(0);
108 if(!sscanf(buf,"%d",&num))
110 if(num<1) goto tryagain;
118 printf(" <'Enter' to make no selection or enter a number.> ");
120 if(buf[0]=='\n') return(0);
121 if(!sscanf(buf,"%d",&num))
123 if(num<1) goto tryagain2;
128 int EndListShow(void)
133 if(r>0 && r<=listcount)
134 listsel=listids[r-1];
139 /* Returns 0 to stop listing, 1 to continue. */
140 int AddToList(char *text, uint32 id)
146 if(t==-1) return(0); // Stop listing.
149 listsel=listids[t-1];
155 listids[listcount]=id;
156 printf("%2d) %s\n",listcount+1,text);
166 typedef struct MENU {
169 int type; // 0 for menu, 1 for function.
172 static void SetOC(void)
174 FCEUI_CheatSearchSetCurrentAsOriginal();
177 static void UnhideEx(void)
179 FCEUI_CheatSearchShowExcluded();
182 static void ToggleCheat(int num)
187 FCEUI_GetCheat(num,0,&A,0,&s);
189 FCEUI_SetCheat(num,0,-1,-1,s);
190 printf("Cheat for address $%04x %sabled.\n",(unsigned int)A,s?"en":"dis");
193 static void ModifyCheat(int num)
202 FCEUI_GetCheat(num, &name, &A, &V, &s);
204 printf("Name [%s]: ",name);
207 /* This obviously doesn't allow for cheats with no names. Bah. Who wants
208 nameless cheats anyway...
212 name=buf; // Change name when FCEUI_SetCheat() is called.
214 name=0; // Don't change name when FCEUI_SetCheat() is called.
216 printf("Address [$%04x]: ",(unsigned int)A);
219 printf("Value [%03d]: ",(unsigned int)V);
222 printf("Enable [%s]: ",s?"Y":"N");
224 if(t=='Y' || t=='y') s=1;
225 else if(t=='N' || t=='n') s=0;
227 FCEUI_SetCheat(num,name,A,V,s);
230 static void AddCheatParam(uint32 A, uint8 V)
236 printf("Address [$%04x]: ",(unsigned int)A);
238 printf("Value [%03d]: ",(unsigned int)V);
240 printf("Add cheat \"%s\" for address $%04x with value %03d?",name,(unsigned int)A,(unsigned int)V);
243 if(FCEUI_AddCheat(name,A,V))
244 puts("Cheat added.");
246 puts("Error adding cheat.");
250 static void AddCheat(void)
256 static int clistcallb(char *name, uint32 a, uint8 v, int s)
261 sprintf(tmp,"%s $%04x:%03d - %s",s?"*":" ",(unsigned int)a,(unsigned int)v,name);
262 ret=AddToList(tmp,lid);
267 static void ListCheats(void)
273 FCEUI_ListCheats(clistcallb);
278 printf(" <(T)oggle status, (M)odify, or (D)elete this cheat.> ");
280 switch(tolower(tmp[0]))
282 case 't':ToggleCheat(which);
284 case 'd':if(!FCEUI_DelCheat(which))
285 puts("Error deleting cheat!");
287 puts("Cheat has been deleted.");
289 case 'm':ModifyCheat(which);
295 static void ResetSearch(void)
297 FCEUI_CheatSearchBegin();
301 static int srescallb(uint32 a, uint8 last, uint8 current)
304 sprintf(tmp, "$%04x:%03d:%03d",(unsigned int)a,(unsigned int)last,(unsigned int)current);
305 return(AddToList(tmp,a));
308 static void ShowRes(void)
310 int n=FCEUI_CheatSearchGetCount();
311 printf(" %d results:\n",n);
316 FCEUI_CheatSearchGet(srescallb);
319 AddCheatParam(which,0);
323 static int ShowShortList(char *moe[], int n, int def)
331 printf("%d) %s\n",x+1,moe[x]);
332 puts("D) Display List");
335 printf("\nSelection [%d]> ",def+1);
336 fgets(tmp,256,stdin);
348 puts("Invalid selection.");
353 static void DoSearch(void)
355 static int v1=0,v2=0;
357 char *m[4]={"O==V1 && C==V2","O==V1 && |O-C|==V2","|O-C|==V2","O!=C"};
358 printf("\nSearch Filter:\n");
360 method=ShowShortList(m,4,method);
363 printf("V1 [%03d]: ",v1);
368 printf("V2 [%03d]: ",v2);
371 FCEUI_CheatSearchEnd(method,v1,v2);
372 puts("Search completed.\n");
376 static MENU NewCheatsMenu[7]={
377 {"Add Cheat",AddCheat,1},
378 {"Reset Search",ResetSearch,1},
379 {"Do Search",DoSearch,1},
380 {"Set Original to Current",SetOC,1},
381 {"Unhide Excluded",UnhideEx,1},
382 {"Show Results",ShowRes,1},
386 static MENU MainMenu[3]={
387 {"List Cheats",ListCheats,1},
388 {"New Cheats...",NewCheatsMenu,0},
392 static void DoMenu(MENU *men)
401 printf("%d) %s\n",x+1,men[x].text);
404 puts("D) Display Menu\nX) Return to Previous\n");
421 else if(sscanf(buf,"%d",&c))
423 if(c>x) goto invalid;
426 void (*func)(void)=men[c-1].action;
430 DoMenu(men[c-1].action); /* Mmm...recursivey goodness. */
436 puts("Invalid command.\n");
443 void DoConsoleCheatConfig(void)
445 MENU *curmenu=MainMenu;