Trying to find a more efficient way to know which byte has a cheat. It might be memory cost but it's 2020.

This commit is contained in:
owomomo 2020-02-01 01:00:34 +08:00
parent c830d2cc7d
commit c3d0d40aa6
6 changed files with 52 additions and 56 deletions

View File

@ -39,7 +39,6 @@ using namespace std;
static uint8 *CheatRPtrs[64];
vector<uint16> FrozenAddresses; //List of addresses that are currently frozen
void UpdateFrozenList(void); //Function that populates the list of frozen addresses
unsigned int FrozenAddressCount = 0; //Keeps up with the Frozen address count, necessary for using in other dialogs (such as hex editor)
void FCEU_CheatResetRAM(void)
@ -64,6 +63,7 @@ CHEATF_SUBFAST SubCheats[256] = { 0 };
uint32 numsubcheats = 0;
int globalCheatDisabled = 0;
int disableAutoLSCheats = 0;
static unsigned char cheatMap[0x10000 / 8] = { 0 };
struct CHEATF *cheats = 0, *cheatsl = 0;
@ -102,11 +102,16 @@ void RebuildSubCheats(void)
{
uint32 x;
struct CHEATF *c = cheats;
for(x = 0; x < numsubcheats; x++)
for (x = 0; x < numsubcheats; x++)
{
SetReadHandler(SubCheats[x].addr, SubCheats[x].addr, SubCheats[x].PrevRead);
SetByteCheat(SubCheats[x].addr, false);
}
numsubcheats = 0;
if (!globalCheatDisabled)
{
while(c)
{
if(c->type == 1 && c->status && GetReadHandler(c->addr) != SubCheatsRead)
@ -116,26 +121,28 @@ void RebuildSubCheats(void)
SubCheats[numsubcheats].val = c->val;
SubCheats[numsubcheats].compare = c->compare;
SetReadHandler(c->addr, c->addr, SubCheatsRead);
SetByteCheat(SubCheats[numsubcheats].addr, true);
numsubcheats++;
}
c = c->next;
}
}
FrozenAddressCount = numsubcheats; //Update the frozen address list
UpdateFrozenList();
}
void FCEU_PowerCheats()
{
numsubcheats = 0; /* Quick hack to prevent setting of ancient read addresses. */
memset(cheatMap, 0, sizeof(cheatMap));
RebuildSubCheats();
}
int FCEU_CalcCheatAffectedBytes(uint32 address, uint32 size) {
uint32 count = 0;
for (uint32 i = 0; i < numsubcheats && count < size; ++i)
if (SubCheats[i].addr >= address && SubCheats[i].addr < address + size)
for (uint32 i = 0; i < size; ++i)
if (IsByteCheat(address + i))
++count;
return count;
}
@ -195,9 +202,11 @@ void FCEU_LoadGameCheats(FILE *override, int override_existing)
int tc = 0;
char *fn;
savecheats = 0;
if (override_existing)
{
numsubcheats = 0;
memset(cheatMap, 0, sizeof(cheatMap));
}
if(override)
fp = override;
@ -870,22 +879,6 @@ void FCEU_CheatSetByte(uint32 A, uint8 V)
BWrite[A](A, V);
}
void UpdateFrozenList(void)
{
//The purpose of this function is to keep an up to date list of addresses that are currently frozen
//and make these accessible to other dialogs that deal with memory addresses such as
//memwatch, hex editor, ramfilter, etc.
uint32 x;
FrozenAddresses.clear(); //Clear vector and repopulate
for(x = 0; x < numsubcheats; x++)
{
FrozenAddresses.push_back(SubCheats[x].addr);
//FCEU_printf("Address %d: %d \n",x,FrozenAddresses[x]); //Debug
}
//FCEUI_DispMessage("FrozenCount: %d",0,FrozenAddressCount);//Debug
}
// disable all cheats
int FCEU_DisableAllCheats(){
int count = 0;
@ -902,3 +895,14 @@ int FCEU_DisableAllCheats(){
RebuildSubCheats();
return count;
}
int IsByteCheat(uint8 address)
{
return cheatMap[address / 8] >> (address % 8) & 1;
}
void SetByteCheat(uint8 address, bool cheat)
{
cheat ? cheatMap[address / 8] |= (1 << address % 8) : cheatMap[address / 8] ^= (1 << address % 8);
}

View File

@ -11,7 +11,6 @@ void FCEU_ApplyPeriodicCheats(void);
void FCEU_PowerCheats(void);
int FCEU_CalcCheatAffectedBytes(uint32 address, uint32 size);
int FCEU_CheatGetByte(uint32 A);
void FCEU_CheatSetByte(uint32 A, uint8 V);
@ -45,6 +44,11 @@ struct SEARCHPOSSIBLE {
bool update;
};
// Trying to find a more efficient way for determining if an address has a cheat
// 1 byte represents to 8 bytes in NES,
int IsByteCheat(uint8 address);
void SetByteCheat(uint8 address, bool cheat);
#define FCEU_SEARCH_SPECIFIC_CHANGE 0
#define FCEU_SEARCH_RELATIVE_CHANGE 1
#define FCEU_SEARCH_PUERLY_RELATIVE_CHANGE 2
@ -55,10 +59,4 @@ struct SEARCHPOSSIBLE {
#define FCEU_SEARCH_NEWVAL_GT_KNOWN 7
#define FCEU_SEARCH_NEWVAL_LT_KNOWN 8
#define CalcAddressRangeCheatCount(count, address, size) \
count = 0; \
for (int i = 0; i < numsubcheats && count < size; ++i) \
if (SubCheats[i].addr >= address && SubCheats[i].addr < address + size) \
++count
#endif

View File

@ -1415,24 +1415,22 @@ void UpdateCheatRelatedWindow()
// ram search
extern HWND RamSearchHWnd;
if (RamSearchHWnd)
{
// if ram search is open then update the ram list.
SendDlgItemMessage(RamSearchHWnd, IDC_RAMLIST, LVM_REDRAWITEMS,
SendDlgItemMessage(RamSearchHWnd, IDC_RAMLIST, LVM_GETTOPINDEX, 0, 0),
SendDlgItemMessage(RamSearchHWnd, IDC_RAMLIST, LVM_GETTOPINDEX, 0, 0) +
SendDlgItemMessage(RamSearchHWnd, IDC_RAMLIST, LVM_GETCOUNTPERPAGE, 0, 0) + 1);
}
// ram watch
extern void UpdateWatchCheats();
UpdateWatchCheats();
extern HWND RamWatchHWnd;
if (RamWatchHWnd)
{
// if ram watch is open then update the ram list.
SendDlgItemMessage(RamWatchHWnd, IDC_WATCHLIST, LVM_REDRAWITEMS,
SendDlgItemMessage(RamWatchHWnd, IDC_WATCHLIST, LVM_GETTOPINDEX, 0, 0),
SendDlgItemMessage(RamSearchHWnd, IDC_RAMLIST, LVM_GETTOPINDEX, 0, 0) +
SendDlgItemMessage(RamWatchHWnd, IDC_WATCHLIST, LVM_GETCOUNTPERPAGE, 0, 0) + 1);
}
}

View File

@ -26,7 +26,6 @@ static void SetCheatToolTip(HWND hwndDlg, UINT id);
char* GetCheatToolTipStr(HWND hwndDlg, UINT id);
extern unsigned int FrozenAddressCount;
extern std::vector<uint16> FrozenAddresses;
//void ConfigAddCheat(HWND wnd); //bbit edited:commented out this line
extern struct CHEATF* cheats;
extern char* GameGenieLetters;

View File

@ -315,17 +315,13 @@ void UpdateMemWatch()
MWRec& mwrec = mwrecs[i];
//Display blue if address is frozen
if (FrozenAddressCount && FrozenAddresses.size())
{
if (FrozenAddressCount)
for (unsigned int x = 0; x < FrozenAddressCount; x++)
{
if (mwrec.addr == FrozenAddresses[x])
{
//SetTextColor(hdc,RGB(0,0,255));
extern int IsByteCheat(uint8);
if (IsByteCheat(mwrec.addr))
SetTextColor(hdc,GetSysColor(COLOR_HIGHLIGHT));
}
}
}
char* text;
if(mwrec.valid && GameInfo)

View File

@ -39,6 +39,7 @@
#include <commctrl.h>
#include <list>
#include <vector>
#ifdef WIN32
#include "BaseTsd.h"
typedef INT_PTR intptr_t;
@ -1262,20 +1263,20 @@ LRESULT CustomDraw (LPARAM lParam)
int rv = CDRF_DODEFAULT;
int cheat = CALL_WITH_T_SIZE_TYPES_1(GetNumCheatsFromIndex, rs_type_size, rs_t == 's', noMisalign, lplvcd->nmcd.dwItemSpec);
switch (cheat) {
default:
case 0:
if (lplvcd->nmcd.dwItemSpec % 2)
lplvcd->clrTextBk = RGB(248, 248, 255);
break;
case 1:
lplvcd->clrTextBk = CHEAT_1BYTE_BG; break;
case 2:
lplvcd->clrTextBk = CHEAT_2BYTE_BG; break;
case 3:
lplvcd->clrTextBk = CHEAT_3BYTE_BG; break;
case 4:
lplvcd->clrTextBk = CHEAT_4BYTE_BG;
lplvcd->clrText = CHEAT_4BYTE_TEXT; break; // use a more visual color in dark background
default:
case 0:
if (lplvcd->nmcd.dwItemSpec % 2)
lplvcd->clrTextBk = RGB(248, 248, 255);
break;
case 1:
lplvcd->clrTextBk = CHEAT_1BYTE_BG; break;
case 2:
lplvcd->clrTextBk = CHEAT_2BYTE_BG; break;
case 3:
lplvcd->clrTextBk = CHEAT_3BYTE_BG; break;
case 4:
lplvcd->clrTextBk = CHEAT_4BYTE_BG;
lplvcd->clrText = CHEAT_4BYTE_TEXT; break; // use a more visual color in dark background
}
if(!IsSatisfied(lplvcd->nmcd.dwItemSpec))