Skip to content

Instantly share code, notes, and snippets.

@bryc
Last active December 10, 2016 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryc/20588f955b730b981db4 to your computer and use it in GitHub Desktop.
Save bryc/20588f955b730b981db4 to your computer and use it in GitHub Desktop.
C Code Snippets Archive #1 - mostly EEPROM parsing and checksum stuff
#include <stdio.h>
main(int argc, char * argv[])
{
// ------------------------------
// Misc variables and preliminary stuff
// ------------------------------
int addr, ch, q;
unsigned char EEPROM[512]; // should be unsigned char instead of just unsigned. for fread support
FILE * stream = fopen(argv[1], "rb");
// ------------------------------
// Put the EEPROM into memory
// ------------------------------
//fread(EEPROM, 512, 1, stream); // fread method
for (addr = 0; addr < 512; addr++)
{
EEPROM[addr] = fgetc(stream);
}
// ------------------------------
// Do the rest
// ------------------------------
for (q = 0; q < 5; q++)
{
int length = (q == 4 ? 64 : 112);
int slot = q * 0x70;
int checkSumArea = (q == 4 ? 0x1E : 0x36);
int magic = (q == 4 ? 0x4849 : 0x4441);
printf("Slot %d\n", q);
int i = 0;
// ------------------------------
// This for loop goes through each copy of save data within a slot
// ------------------------------
for (i = 0; i < 2; i++)
{
int copy = i * (length / 2);
unsigned checksum = 0;
int x = 0;
if(i==0){
printf(" Primary [");
} else{
printf(" Backup [");
}
while (x < (length / 2) - 2)
{
checksum += EEPROM[slot + copy + x];
x++;
}
if ((checksum >> 8) == EEPROM[slot + copy + checkSumArea] && (checksum & 0xFF) == EEPROM[slot + copy + checkSumArea + 1])
{
printf("Checksum: OK ");
}
else
{
printf("Checksum: BAD ");
}
if ((magic >> 8) == EEPROM[slot + copy + checkSumArea -2] && (magic & 0xFF) == EEPROM[slot + copy + checkSumArea -1])
{
printf("Magic: OK ]\n");
}
else
{
printf("Magic: BAD]\n");
}
//printf(" %X %X\n", EEPROM[slot + copy + checkSumArea],checksum);
}
}
// ------------------------------
// Close the file etc
// ------------------------------
fclose(stream);
printf("Finished.\n");
getc(stdin);
}
#include <stdio.h>
#include <stdint.h>
int main(void)
{
uint64_t A1, A2, A3=0x13108B3C1, T0, T1, T2, T3, T4=0, T5, T6, T7, T8, T9, S1, V0, AT, S3;
int i;
unsigned char bytes[28] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF
};
for (i = 0; i < sizeof(bytes); i++)
{
T8 = bytes[i]; // LBU
T5 = A3 & 0xFFFFFFFF; // LW
T9 = S1 & 0x000F;
T0 = T8 << T9;
T4 = A3 >> 32 ; // LW
T7 = T0 + T5;
T2 = T0 >> 0x1F;
// After JAL
A3 = (T4 << 32 ) + T7; // LD
A2 = A3 << (0x1F + 32);
A1 = A3 << 0x1F;
A2 = A2 >> 0x1F;
A1 = A1 >> (0x0 + 32);
A3 = A3 << (0xC + 32);
A2 = A2 | A1;
A3 = A3 >> (0x0 + 32);
A2 = A2 ^ A3;
A3 = A2 >> 0x14;
A3 = A3 & 0xFFF;
A3 = A3 ^ A2;
// After JR
V0 = A3 & 0xFFFFFFFF;
S1 += 0x7;
// After BNE
S3 = S3 ^ V0;
}
for (i = sizeof(bytes)-1; i >= 0; i--)
{
T1 = bytes[i]; // LBU
T3 = A3 & 0xFFFFFFFF; // LW
T8 = S1 & 0x000F;
T9 = T1 << T8;
T2 = A3 >> 32 ; // LW
T5 = T9 + T3;
T0 = T9 >> 0x1F;
// After JAL
A3 = (T2 << 32) + T5; // LD
A2 = A3 << (0x1F + 32);
A1 = A3 << 0x1F;
A2 = A2 >> 0x1F;
A1 = A1 >> (0x0 + 32);
A3 = A3 << (0xC + 32);
A2 = A2 | A1;
A3 = A3 >> (0x0 + 32);
A2 = A2 ^ A3;
A3 = A2 >> 0x14;
A3 = A3 & 0xFFF;
A3 = A3 ^ A2;
// After JR
V0 = A3 & 0xFFFFFFFF;
S1 += 0x3;
// After BNE
S3 = S3 ^ V0;
}
printf("Checksum: %lX\n", S3);
return 0;
}
#include<stdio.h>
main()
{
unsigned char data[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
int i;
unsigned long long A3 = 0x13108B3C1, A2, S1=0, S3=0;
for (i = 0; i < sizeof(data); i++)
{
A3 += data[i] << (S1 & 0x0F);
A2 = A3<<0x3F>>0x1F | A3>>1 ^ A3<<0x2C>>0x20;
A3 = A2 >> 0x14 & 0xFFF ^ A2;
S3 ^= A3;
S1 += 7;
}
for ( i-= 1; i >= 0 ; i--)
{
A3 += data[i] << (S1 & 0x0F);
A2 = A3<<0x3F>>0x1F | A3>>1 ^ A3<<0x2C>>0x20;
A3 = A2 >> 0x14 & 0xFFF ^ A2;
S3 ^= A3;
S1 += 3;
}
printf("\n---\n%X",S3);
}
#include <stdio.h>
#include <stdint.h>
int main(void) {
uint64_t A1, A2, A3=0x13108B3C1, S1=0, S3=0;
int i;
unsigned char bytes[28] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF
};
for (i = 0; i < 28; i++) {
A3 = (A3 & 0x100000000) + (bytes[i] << (S1 & 15)) + (A3 & 0xFFFFFFFF);
A2 = A3 << 32 | A3 >> 1;
A3 = A3 << 44 >> 32;
A2 = A2 ^ A3;
A3 = A2 >> 20 & 0xFFF ^ A2;
S3 = S3 ^ A3 & 0xFFFFFFFF;
S1 += 0x7;
}
for (i = i - 1; i >= 0; i--) {
A3 = (A3 & 0x100000000) + (bytes[i] << (S1 & 15)) + (A3 & 0xFFFFFFFF);
A2 = A3 << 32 | A3 >> 1;
A3 = A3 << 44 >> 32;
A2 = A2 ^ A3;
A3 = A2 >> 20 & 0xFFF ^ A2;
S3 = S3 ^ A3 & 0xFFFFFFFF;
S1 += 0x3;
}
printf("Checksum: %lX\n", S3);
return 0;
}
#include <stdio.h>
unsigned long long value = 0x13108B3C1;
unsigned long long value2 = 0;
unsigned long long checksum = 0;
unsigned int carry = 1;
int bp = 0;
int sd = 0;
unsigned char data[] = {
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x0C
};
int algo(void)
{
value = (value & 0x1FFFFFFFF) + (data[bp] << (sd & 0x0F));
//value &= 0x1FFFFFFFF;
value2 = (value >> 1 | value << 32) ^ (value << 44) >> 32;
carry = value & 0x01;
value = value2 ^ (value2 >> 20) & 0x0FFF;
checksum = value ^ checksum;
}
int main(void)
{
for(; bp < sizeof(data); bp++, sd += 7)
algo();
bp--;
for(; bp >= 0; bp--, sd += 3)
algo();
printf("%08X", checksum);
return 0;
}
#include <stdio.h>
unsigned char arr[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
main()
{
unsigned int a = 0x3108B3C1;
unsigned int t = 1;
unsigned int b = 0;
unsigned int bt = 0;
long chk = 0;
int i = 0, c = 0;
for(i = 0; i < sizeof(arr); i++, c += 7)
{
a += arr[i] << (c & 0xF);
b = (a >> 1 | t << 31) ^ (a & 0xFFFFF) << 12;t = a & 1;
a = (b >> 20) & 0xFFF ^ b;
chk = a ^ chk;
}
bt = chk;
for(i--; i >= 0; i--, c += 3)
{
a += arr[i] << (c & 0xF);
b = (a >> 1 | t << 31) ^ (a & 0xFFFFF) << 12;t = a & 1;
a = (b >> 20) & 0xFFF ^ b;
chk = a ^ chk;
}
printf("---\n%lX\n", chk);
}
#include<stdio.h>
main()
{
unsigned char data[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
int i;
unsigned long long A3 = 0x13108B3C1, A2, S1=0, S3=0;
for (i = 0; i < sizeof(data); i++)
{
A3 += data[i] << (S1 & 0x0F);
A2 = A3<<0x3F>>0x1F | A3>>1 ^ A3<<0x2C>>0x20;
A3 = A2 >> 0x14 & 0xFFF ^ A2;
S3 ^= A3;
S1 += 7;
}
for ( i-= 1; i >= 0 ; i--)
{
A3 += data[i] << (S1 & 0x0F);
A2 = A3<<0x3F>>0x1F | A3>>1 ^ A3<<0x2C>>0x20;
A3 = A2 >> 0x14 & 0xFFF ^ A2;
S3 ^= A3;
S1 += 3;
}
printf("\n---\n%X",S3);
}
#include <stdio.h>
#include <stdint.h>
#define NUMBYTES 28
int main(void)
{
uint64_t A1, A2, A3=0x13108B3C1, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, S1=0, S3=0, V0, AT;
int i;
unsigned char bytes[NUMBYTES] = {
0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
for (i = 0; i < NUMBYTES; i++)
{
T8 = bytes[i]; // LBU
T5 = A3 & 0xFFFFFFFF; // LW
T9 = S1 & 0x000F;
T0 = T8 << T9;
T4 = A3 >> 32 ; // LW
T7 = T0 + T5;
T2 = T0 >> 0x1F;
if (T7 < T5){ AT = 1; } else { AT = 0; }
T6 = AT + T2;
T6 = T6 + T4;
// After JAL
A3 = (T6 << 32 ) + T7; // LD
A2 = A3 << (0x1F + 32);
A1 = A3 << 0x1F;
A2 = A2 >> 0x1F;
A1 = A1 >> (0x0 + 32);
A3 = A3 << (0xC + 32);
A2 = A2 | A1;
A3 = A3 >> (0x0 + 32);
A2 = A2 ^ A3;
A3 = A2 >> 0x14;
A3 = A3 & 0xFFF;
A3 = A3 ^ A2;
V0 = A3 << (0x0 + 32);
// After JR
V0 = V0 >> (0x0 + 32);
S1 += 0x7;
// After BNE
S3 = S3 ^ V0;
}
for (i = NUMBYTES - 1; i >= 0; i--)
{
T1 = bytes[i]; // LBU
T3 = A3 & 0xFFFFFFFF; // LW
T8 = S1 & 0x000F;
T9 = T1 << T8;
T2 = A3 >> 32 ; // LW
T5 = T9 + T3;
T0 = T9 >> 0x1F;
if (T5 < T3){ AT = 1; } else { AT = 0; }
T4 = AT + T0;
T4 = T4 + T2;
// After JAL
A3 = (T4 << 32) + T5; // LD
A2 = A3 << (0x1F + 32);
A1 = A3 << 0x1F;
A2 = A2 >> 0x1F;
A1 = A1 >> (0x0 + 32);
A3 = A3 << (0xC + 32);
A2 = A2 | A1;
A3 = A3 >> (0x0 + 32);
A2 = A2 ^ A3;
A3 = A2 >> 0x14;
A3 = A3 & 0xFFF;
A3 = A3 ^ A2;
V0 = A3 << (0x0 + 32);
// After JR
V0 = V0 >> (0x0 + 32);
S1 += 0x3;
// After BNE
S3 = S3 ^ V0;
}
printf("Checksum: %lX\n", S3);
return 0;
}
@echo off
cd C:\mingw\bin
echo Compiling...
gcc -s -O3 -o ../_eep.exe ../_eep.c
echo Done.
pause
@echo off
cd C:\mingw\bin
echo Compiling...
gcc -s -O3 -o ../_eep.exe ../_eep.c
echo Done.
pause
REM 2016 NOTE: USE fscanf() in future C projects!
@echo off
echo Compiling...
cd C:\mingw\bin
gcc -s -O3 -o ..\src\build\%~n1.exe %1
echo Done.
pause
// -----------------------------------------------+
// Includes //
// ----------------------------------------------//
#include <stdio.h> //
// -----------------------------------------------+
// -----------------------------------------------+
// Main function //
// ----------------------------------------------//
main(int argc, char * argv[])
{
unsigned char EEPROM[512];
FILE * f = fopen(argv[1], "r");
fread(EEPROM, 1, 512, f);
fclose(f);
printf("%c%c\n", EEPROM[0x1DC],EEPROM[0x1DD]);
system("pause");
}
// -----------------------------------------------+
typedef struct {
uchar Star : 1;
uchar Star : 1;
uchar Star : 1;
uchar Star : 1;
uchar Star : 1;
uchar Star : 1;
uchar Star : 1;
uchar Reserved : 1;
} lvl;
typedef struct {
char id;
char unknown;
short x;
short y;
short z;
} lostc;
typedef struct {
lostc lostCap;
lvl Castle;
uchar capLostInSnow : 1;
uchar capLostInSand : 1;
uchar capLostInTall : 1;
uchar unused : 1;
uchar starDoor3rdFlr : 1;
uchar unused : 3;
uchar wallPushed : 1;
uchar moatDrained : 1;
uchar starDoorSlide : 1;
uchar starDoorWhomp : 1;
uchar starDoorCool : 1;
uchar starDoorJolly : 1;
uchar starDoorBowsr1 : 1;
uchar starDoorBowsr2 : 1;
uchar slotIsOccupied : 1;
uchar capSwitchR : 1;
uchar capSwitchG : 1;
uchar capSwitchB : 1;
uchar haveKeyBasemnt : 1;
uchar haveKey2ndFlr : 1;
uchar DoorUnlckBasemnt : 1;
uchar DoorUnlck2ndFlr : 1;
lvl main[15];
lvl other[10];
uchar coinScore[15];
char magicNumber[2];
short checksum;
} gdat;
typedef struct {
char unknown[16];
short soundSetting;
short langSetting;
char _unknown[8];
char magicNumber[2];
short checksum;
} odat;
struct File {
gdat gameData[8];
odat otherData[2];
} file;
#define uchar unsigned char
struct levelFlagsTemplate
{
uchar Star1 : 1;
uchar Star2 : 1;
uchar Star3 : 1;
uchar Star4 : 1;
uchar Star5 : 1;
uchar Star6 : 1;
uchar Star7 : 1;
uchar Reserved : 1;
};
struct _lostCap
{
char id;
char unknown;
short x;
short y;
short z;
};
struct _miscFlags
{
uchar capLostInSnow : 1;
uchar capLostInSand : 1;
uchar capLostInTall : 1;
uchar unused : 1;
uchar starDoor3rdFlr : 1;
uchar _unused : 3;
uchar wallPushed : 1;
uchar moatDrained : 1;
uchar starDoorSlide : 1;
uchar starDoorWhomp : 1;
uchar starDoorCool : 1;
uchar starDoorJolly : 1;
uchar starDoorBowsr1 : 1;
uchar starDoorBowsr2 : 1;
uchar slotIsOccupied : 1;
uchar capSwitchR : 1;
uchar capSwitchG : 1;
uchar capSwitchB : 1;
uchar haveKeyBasemnt : 1;
uchar haveKey2ndFlr : 1;
uchar DoorUnlckBasemnt : 1;
uchar DoorUnlck2ndFlr : 1;
};
struct _gameData
{
struct _lostCap lostCap;
struct levelFlagsTemplate Castle;
struct _miscFlags miscFlags;
struct levelFlagsTemplate mainLevels[15];
struct levelFlagsTemplate otherLevels[10];
uchar coinScore[15];
char magicNumber[2];
short checksum;
};
struct _otherData
{
char unknown[16];
short soundSetting;
short langSetting;
char _unknown[8];
char magicNumber[2];
short checksum;
};
struct File
{
struct _gameData gameData[8];
struct _otherData otherData[2];
} file;
#include <windows.h>
char glpszText[1024];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
sprintf(glpszText,
"Hello World\nGetCommandLine(): [%s]\n"
"WinMain lpCmdLine: [%s]\n",
lpCmdLine, GetCommandLine() );
WNDCLASSEX wcex;
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "HELLO";
wcex.hIconSm = NULL;
if (!RegisterClassEx(&wcex))
return FALSE;
HWND hWnd;
hWnd = CreateWindow("HELLO", "Hello", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, glpszText, strlen(glpszText), &rt, DT_TOP | DT_LEFT);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
#include <stdio.h>
#ifdef __unix__
# include <unistd.h>
#elif defined _WIN32
# include <windows.h>
#define sleep(x) Sleep(1000 * x)
#endif
int main(int argc, char **argv)
{
while(1){
FILE *stream = fopen(argv[1], "rb");
unsigned char buffer[512];
fread(buffer, 1, 512, stream);
int i;
for (i = 0; i < 512; i++)
{
printf("%.2X ", buffer[i]);
if (i % 16 == 15)
{
printf("\n");
}
}
sleep(100);
system("cls");
}
getchar();
return 0;
}
#include <stdio.h>
int main( int argc, char *argv[] )
{
while(1){
system("cls");
FILE *file = fopen( argv[1], "r" );
int x;
int i;
i = 0;
while ( ( x = fgetc( file ) ) != EOF )
{
if(i==16){ i=0; printf( "\n" ); }
printf( "%02x ", x );
i++;
}
fclose( file );
}
printf( "\n" );
system( "pause" );
}
@echo off
cd C:\mingw\bin
echo Compiling...
gcc -s -O3 -o ../hexview.exe ../hexview.c
echo Done.
pause
#include <stdio.h>
#define uchar unsigned char
unsigned char rawData[512] = {
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x00, 0xFF, 0x6F,
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01, 0x00, 0x01,
0x00, 0x92, 0x8D, 0x64, 0x88, 0x8D, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x0A, 0x0A, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x19, 0x00, 0xFF, 0x6F, 0x7F, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x03, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x92, 0x8D, 0x64,
0x88, 0x8D, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x44, 0x41, 0x0A, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x00, 0x85,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x01, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x44, 0x41, 0x00, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x08, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x00, 0x99,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x44, 0x41, 0x00, 0x85, 0x00, 0x00, 0x00, 0x04, 0x15, 0x55, 0x55, 0x59,
0x3F, 0xFF, 0xFF, 0xF3, 0x2A, 0xAA, 0xAA, 0xAE, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x49, 0x07, 0x09,
0x00, 0x00, 0x00, 0x04, 0x15, 0x55, 0x55, 0x59, 0x3F, 0xFF, 0xFF, 0xF3,
0x2A, 0xAA, 0xAA, 0xAE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x48, 0x49, 0x07, 0x09
};
typedef struct
{
uchar star1 : 1;
uchar star2 : 1;
uchar star3 : 1;
uchar star4 : 1;
uchar star5 : 1;
uchar star6 : 1;
uchar starC : 1;
uchar reserved : 1;
} lvl;
struct
{
struct
{
char id;
char unknown;
short x;
short y;
short z;
} lostCap;
lvl Castle;
uchar capLostInSnow : 1;
uchar capLostInSand : 1;
uchar capLostInTall : 1;
uchar : 1;
uchar starDoor3rdFlr : 1;
uchar : 3;
uchar wallPushed : 1;
uchar moatDrained : 1;
uchar starDoorSlide : 1;
uchar starDoorWhomp : 1;
uchar starDoorCool : 1;
uchar starDoorJolly : 1;
uchar starDoorBowsr1 : 1;
uchar starDoorBowsr2 : 1;
uchar slotIsOccupied : 1;
uchar capSwitchR : 1;
uchar capSwitchG : 1;
uchar capSwitchB : 1;
uchar haveKeyBasemnt : 1;
uchar haveKey2ndFlr : 1;
uchar DoorUnlckBasemnt : 1;
uchar DoorUnlck2ndFlr : 1;
lvl main[15];
lvl other[10];
uchar coinScore[15];
char magicNumber[2];
short checksum;
} gameData[8];
struct
{
char unknown0[16];
short soundSetting;
short langSetting;
char unknown1[8];
char magicNumber[2];
short checksum;
} otherData[2];
int main(void)
{
printf("\n\t%d\n", rawData[0]);
return 0;
}
#include <stdio.h>
unsigned char data[512] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x00, 0xFF, 0x6F,
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0x01, 0x01, 0x01, 0x00, 0x01,
0x00, 0x92, 0x8D, 0x64, 0x88, 0x8D, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x0A, 0x0A, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x19, 0x00, 0xFF, 0x6F, 0x7F, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x03, 0x01, 0x01, 0x01, 0x00, 0x01, 0x00, 0x92, 0x8D, 0x64,
0x88, 0x8D, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x44, 0x41, 0x0A, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x00, 0x85,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x01, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x44, 0x41, 0x00, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x08, 0x01, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x00, 0x99,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x44, 0x41, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x44, 0x41, 0x00, 0x85, 0x00, 0x00, 0x00, 0x04, 0x15, 0x55, 0x55, 0x59,
0x3F, 0xFF, 0xFF, 0xF3, 0x2A, 0xAA, 0xAA, 0xAE, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x49, 0x07, 0x09,
0x00, 0x00, 0x00, 0x04, 0x15, 0x55, 0x55, 0x59, 0x3F, 0xFF, 0xFF, 0xF3,
0x2A, 0xAA, 0xAA, 0xAE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x48, 0x49, 0x07, 0x09
};
int main(void)
{
int i;
for(i = 0; i < 448; i += 56)
{
int magic = (data[i + 52] << 8 | data[i + 53]);
int oldSum = (data[i + 54] << 8 | data[i + 55]);
int newSum = 0;
int j = 54;
while(j--)
{
newSum += data[i + j];
}
if(magic == 0x4441 && oldSum == newSum)
{
printf("\t%04X: Valid\n", i);
}
else
{
printf("\t%04X: Error\n", i);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(int argc, char **argv)
{
while(1)
{
FILE *stream = fopen(argv[1], "rb");
unsigned char buffer[2048];
fread(buffer, 1, 2048, stream);
int i;
for (i = 0; i < 2048; i++)
{
printf("%.2X ", buffer[i]);
if (i % 26 == 25)
{
printf("\n");
}
}
Sleep(500);
system("cls");
}
return 0;
}
#include <stdio.h>
int mult(int x, int y)
{
return x*y;
}
int main(void)
{
printf("Ok: %d",mult(12,12));
return 0;
}
@ECHO OFF
TITLE MinGW Compiler Suite Invocation
CD C:\MINGW\BIN\
ECHO Calling CC1.EXE (C compiler)...
GCC.EXE -S -O3 -o ../test.s ../test.c
ECHO.
ECHO Calling AS.EXE (GNU assembler)...
AS.EXE --statistics -o ../test.o ../test.s
ECHO.
ECHO Calling LD.EXE (GNU linker)...
GCC.EXE -s -o ../test.exe ../test.o
PAUSE
#include <stdio.h>
unsigned int i = 0x0000; /* array index count for memory addressing */
FILE *stream;
int main(int argc, char *argv[])
{
unsigned char EEPROM[0x0200];
if (argv[1] == NULL)
{
printf("Command syntax missing domain.\n");
return 1;
}
if (argc > 2)
{
printf("Too many parameters specified.\n");
return 1;
}
stream = fopen(argv[1], "rb");
if (stream == NULL)
{
printf("Specified file was unreadable.\n");
return 1;
}
while (i < 0x0200)
{
EEPROM[i] = fgetc(stream);
printf("%03X-[%02X]\n", i, EEPROM[i] );
++i;
}
fclose(stream);
if (validate(EEPROM) == 1)
{
printf("EEPROM input had invalidities.\n");
}
system("PAUSE");
return 0;
}
int validate(unsigned char memory[512])
{
unsigned short checksum = 0x0000;
if (memory[0x0034] != 0x44)
{
return 1;
}
if (memory[0x0035] != 0x41)
{
return 1;
}
if (memory[0x00A4] != 0x44)
{
return 1;
}
if (memory[0x00A5] != 0x41)
{
return 1;
}
if (memory[0x0114] != 0x44)
{
return 1;
}
if (memory[0x0115] != 0x41)
{
return 1;
}
if (memory[0x0184] != 0x44)
{
return 1;
}
if (memory[0x0185] != 0x41)
{
return 1;
}
if (memory[0x01DC] != 0x48)
{
return 1;
}
if (memory[0x01DD] != 0x49)
{
return 1;
}
for (i = 0x0000; i < 0x0036; i++)
{
checksum += memory[i];
}
if (memory[0x0036] != (unsigned char)checksum & 0xFF00)
{
return 1;
}
if (memory[0x0037] != (unsigned char)checksum & 0x00FF)
{
printf("memory[0x0037] == 0x%X; checksum == 0x%X\n", memory[0x0037], checksum & 0x00FF);
return 1;
}
checksum = 0x0000; /* reset since previous calculation */
for (i = 0x0070; i < 0x00A6; ++i)
{
checksum += memory[i];
}
if (memory[0x00A6] != checksum & 0xFF00)
{
return 1;
}
if (memory[0x00A7] != checksum & 0x00FF)
{
return 1;
}
checksum = 0x0000; /* reset since previous calculation */
for (i = 0x00E0; i < 0x0116; ++i)
{
checksum += memory[i];
}
if (memory[0x0116] != checksum & 0xFF00)
{
return 1;
}
if (memory[0x0117] != checksum & 0x00FF)
{
return 1;
}
checksum = 0x0000; /* reset since previous calculation */
for (i = 0x0150; i < 0x0186; ++i)
{
checksum += memory[i];
}
if (memory[0x0186] != checksum & 0xFF00)
{
return 1;
}
if (memory[0x0187] != checksum & 0x00FF)
{
return 1;
}
checksum = 0x0000; /* reset since previous calculation */
for (i = 0x01C0; i < 0x01DE; ++i)
{
checksum += memory[i];
}
if (memory[0x01DE] != checksum & 0xFF00)
{
return 1;
}
if (memory[0x01DF] != checksum & 0x00FF)
{
return 1;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned char EEPROM[0x0200] = "";
unsigned int i = 0x0000; /* register for array index memory addressing */
char string[81] = "";
signed char chr_in(void);
void str_in(void);
void configure_file(char file);
int main(int argc, char *argv[])
{
FILE *stream;
if (argv[1] == NULL)
{
printf("Command syntax missing domain.\n");
return 1;
}
if (argc > 2)
{
printf("Too many parameters specified.\n");
return 1;
}
stream = fopen(argv[1], "rb");
if (stream == NULL)
{
printf("Specified file was unreadable.\n");
return 1;
}
fread(EEPROM, sizeof(unsigned char), 0x0200, stream);
fclose(stream);
while (0 == 0)
{
if (i == 0xFFFF)
{
break;
}
printf("Writing to game slot MARIO_");
switch (chr_in() & 0xDF)
{
case 'A':
configure_file('A');
break;
case 'B':
configure_file('B');
break;
case 'C':
configure_file('C');
break;
case 'D':
configure_file('D');
break;
default :
printf("Invalid file slot specifier.\n");
break;
}
}
stream = fopen(argv[1], "wb");
fwrite(EEPROM, sizeof(unsigned char), 0x0200, stream);
fclose(stream);
return 0;
}
void configure_file(char file)
{
char data[0x38] = "";
int course = 0;
unsigned cannon = 0;
for (i = 0x0000; i < 0x0038; ++i)
{
data[i] = EEPROM[i + (file - 65) * 0x0070];
}
if (data[0x01] & 0x01)
{
printf("Mario's cap was last lost at course %d.\n", data[0x00]);
printf("Theft coordinates: x = %d, y = %d, z = %d\n",
(data[0x02] << 8) + data[0x03], (data[0x04] << 8) + data[0x05],
(data[0x06] << 8) + data[0x07]);
}
printf("0:Exit\n1:File\n2 Cap\n3 Paths\n4 Doors\n5 Caps\n6 Castle Stars\n7 Worlds\n");
switch (chr_in() & 0x07)
{
case 0:
i = 0xFFFF;
return;
case 1:
if (EEPROM[0x0B] & 0x01)
{
strcpy(string, "occupied");
}
else
{
strcpy(string, "vacant");
}
printf("Game slot is marked as %s.\n", string);
printf("%d\n",EEPROM[0x0B]);
printf("Mark file as vacant or occupied? ");
EEPROM[0x0B] = (chr_in() & 0x01) ^ EEPROM[0x0B];
printf("%d\n",EEPROM[0x0B]);
break;
case 2:
switch (data[0x09] & 0x03)
{
case 0x00: strcpy(string, "his head");
break;
case 0x01: strcpy(string, "Snowman's Land");
break;
case 0x02: strcpy(string, "Shifting Sand Land");
break;
case 0x03: strcpy(string, "Tall, Tall Mountain");
break;
}
printf("Mario's cap is on %s.\n", string);
printf("Set Mario's cap where? ");
data[0x09] |= chr_in() & 0x07;
break;
case 3:
printf("Repel course 9 entrance? ");
data[0x0A] |= chr_in() << 0 & 0x01;
printf("Drain castle moat? ");
data[0x0A] |= chr_in() << 1 & 0x02;
printf("Basement unlocked? ");
data[0x0B] |= chr_in() << 6 & 0x40;
printf("Have key? ");
data[0x0B] |= chr_in() << 4 & 0x10;
printf("Second floor unlocked? ");
data[0x0B] |= chr_in() << 7 & 0x80;
printf("Have key? ");
data[0x0B] |= chr_in() << 5 & 0x20;
break;
case 4:
printf("Star doors have not been implemented yet.\n");
break;
case 5:
printf("Wing cap switch? ");
data[0x0B] |= chr_in() << 1 & 0x02;
printf("Metal cap switch? ");
data[0x0B] |= chr_in() << 2 & 0x04;
printf("Invisibility cap switch? ");
data[0x0B] |= chr_in() << 3 & 0x08;
break;
case 6:
printf("Flags set is %02X. \n", data[0x08]);
printf("Overwrite flags set with: ");
scanf("%i", &data[0x08]);
break;
case 7:
printf("Edit stars for what course number? ");
scanf("%i", &course);
switch (course)
{
case 1: strcpy(string, "Bob-omb Battlefield");
cannon = 1;
break;
case 2: strcpy(string, "Whomp's Fortress");
cannon = 1;
break;
case 3: strcpy(string, "Jolly Roger Bay");
cannon = 1;
break;
case 4: strcpy(string, "Cool, Cool Mountain");
cannon = 1;
break;
case 5: strcpy(string, "Big Boo's Haunt");
break;
case 6: strcpy(string, "Hazy Maze Cave");
break;
case 7: strcpy(string, "Lethal Lava Land");
break;
case 8: strcpy(string, "Shifting Sand Land");
cannon = 1;
break;
case 9: strcpy(string, "Dire, Dire Docks");
break;
case 10: strcpy(string, "Snowman's Land");
cannon = 1;
break;
case 11: strcpy(string, "Wet-Dry World");
cannon = 1;
break;
case 12: strcpy(string, "Tall, Tall Mountain");
cannon = 1;
break;
case 13: strcpy(string, "Tiny-Huge Island");
cannon = 1;
break;
case 14: strcpy(string, "Tick Tock Clock");
break;
case 15: strcpy(string, "Rainbow Ride");
cannon = 1;
break;
case 16: strcpy(string, "Bowser in the Dark World");
break;
case 17: strcpy(string, "Bowser in the Fire Sea");
break;
case 18: strcpy(string, "Bowser in the Sky");
break;
case 19: strcpy(string, "The Princess' Secret Slide");
break;
case 20: strcpy(string, "Cavern of the Metal Cap");
break;
case 21: strcpy(string, "Tower of the Wing Cap");
break;
case 22: strcpy(string, "Vanish Cap Under the Moat");
break;
case 23: strcpy(string, "Wing Mario Over the Rainbow");
cannon = 1;
break;
case 24: strcpy(string, "The Secret Aquarium");
break;
case 25: strcpy(string, "unused star space");
break;
default: printf("Invalid world number specifier.\n");
return;
}
printf("Star flags set: %02X\n", (data[0x0B + course] & 0x7F));
printf("Overwrite flags set for %s: \n", string);
scanf("%i", &i);
data[0x0B + course] &= i & 0x7F;
printf("High score: %d coins\n", data[0x24 + course]);
printf("Specify new high score: ");
scanf("%d", &data[0x24 + course]);
if (cannon)
{
printf("Open cannon? ");
data[0x0B + course] |= (chr_in() << 7) & 0x80;
}
break;
default : printf("Invalid write command specifier.\n");
}
cannon = (file - 'A') * 0x0070; // re-use cannon register for memory addressing
for (i = cannon; i < (cannon + 0x0070); ++i)
{
EEPROM[i] = data[i];
}
return;
}
void str_in(void)
{
int count = 0, in = 0;
while (count < 80)
{
in = fgetc(stdin);
if ((in == EOF) || (in == '\n'))
{
break;
}
string[count] = (char) in;
++count;
}
string[count] = '\0';
}
signed char chr_in(void)
{
char buffer[2];
int in = 0;
in = fgetc(stdin);
buffer[0] = (signed char) in;
buffer[1] = '\0';
while (0 == 0)
{
if (in == '\n')
{
break;
}
else if (in == EOF)
{
break;
}
in = fgetc(stdin);
} /* Use fgetc() to increment the stdin file ptr. */
return buffer[0];
}
@ECHO OFF
TITLE MinGW Compiler Suite Invocation
CD C:\MINGW\BIN\
ECHO Calling CC1.EXE (C compiler)...
GCC.EXE -s -O3 -o ../sm.exe ../sm.c
PAUSE
#include <stdio.h>
/*
* another checksum to
lol(char *byte)
{
int chk, i;
for(chk = i = 0; i < len; ++i)
{
chk += byte[i];
chk += (chk << 10);
chk ^= (chk >> 6);
}
chk += (chk << 3);
chk ^= (chk >> 11);
chk += (chk << 15);
return chk;
}*/
///////////////////////////////////////////////////////
// checksum-algorithm-to-replace-additive-checksum
///////////////////////////////////////////////////////
int catrac(int offset, int length, unsigned char data[])
{
int chk = 0xAA55, i;
for(i = offset; i < offset+length; i++)
{
// original: chk = chk >> 1 ^ (chk + data[i])
chk = ((chk << 5) + chk) + data[i] & 0xFFFF;
}
return chk;
}
///////////////////////////////////////////////////////
// additive checksum
///////////////////////////////////////////////////////
int addchk(int offset, int length, unsigned char data[])
{
int chk = 0, i;
for(i = offset; i < offset+length; i++)
{
chk = chk + data[i];
}
return chk;
}
// 1 CHECK MAGIC
// 2 CHECK CHECKSUM
// 3 CHECK DUPLICATE
// Game Data
//////////////////
// Duplicate Range: 0 to 56 ( Do not forget Backup)
// Checksum Range: 0 to 54
// Checksum Offset: 55 and 56
// Magic Offset: 53 and 54
// Magic: 44 41
// Global Data
//////////////////
// Duplicate Range: 0 to 32
// Checksum Range 0 to 30
// Checksum Offset: 31 and 32
// Magic Offset: 29 and 30
// Magic: 48 49
//Slot A - 0 56,
//Slot B - 112 56,
//Slot C - 224 56,
//Slot D - 336 56,
//Global - 448 32
void parse(unsigned char a[])
{
// verify dupe, checksum and magic
int i, b, check = 0;
for(i = 0,b=0; i < 512; i += 112, b+=3)
{
int s = i >= 448 ? 32 : 56;
int m0 = i >= 448 ? 0x4849 : 0x4441;
int m1 = (a[i + s - 4] << 8) + a[i + s - 3];
int c1 = (a[i + s - 2] << 8) + a[i + s - 1];
int c0 = addchk( i, s-2, a);
int d0 = catrac( i, s, a);
int d1 = catrac(i+s, s, a);
check |= ((d0==d1) << 0+b) + ((c0==c1) << 1+b) + ((m0==m1) << 2+b);
//printf("%X,%X,%X\n",0+b, 1+b, 2+b );
}
if(check!=0x7FFF)
{
printf("Fail\n");
for(i=0,b=0;i<5;i++,b+=3)
{
printf("\t%d - CKSM=%d MGIC=%d DUPE=%d\n", i+1, 0!=(check&(1<<1+b)), 0!=(check&(1<<2+b)), 0!=(check&(1<<0+b)));
}
}
else
{
printf("Pass\n");
}
}
///////////////////////////////////////////////////////
// program initalization
///////////////////////////////////////////////////////
void main(int argc, char *argv[])
{
if (argc == 2)
{
FILE * fp;
fp = fopen(argv[1], "rb");
if(fp != 0) {
unsigned char eeprom[512];
fread(eeprom, 512, 1, fp);
parse(eeprom);
}
else { printf("Error reading file.\n"); }
}
else { printf("You must supply the filename as a single argument.\n"); }
}
#include <stdio.h>
typedef struct level {
unsigned char star : 7;
unsigned char reserved : 1;
} level;
typedef struct caploc {
char id;
char unknown;
short x;
short y;
short z;
} caploc;
typedef struct gamedata {
caploc lostCap;
level Castle;
unsigned char capLostInSnow : 1;
unsigned char capLostInSand : 1;
unsigned char capLostInTall : 1;
unsigned char unused : 1;
unsigned char starDoor3rdFlr : 1;
unsigned char unused2 : 3;
unsigned char wallPushed : 1;
unsigned char moatDrained : 1;
unsigned char starDoorSlide : 1;
unsigned char starDoorWhomp : 1;
unsigned char starDoorCool : 1;
unsigned char starDoorJolly : 1;
unsigned char starDoorBowsr1 : 1;
unsigned char starDoorBowsr2 : 1;
unsigned char slotIsOccupied : 1;
unsigned char capSwitchR : 1;
unsigned char capSwitchG : 1;
unsigned char capSwitchB : 1;
unsigned char haveKeyBasemnt : 1;
unsigned char haveKey2ndFlr : 1;
unsigned char DoorUnlckBasemnt : 1;
unsigned char DoorUnlck2ndFlr : 1;
level main[15];
level other[10];
unsigned char coinScore[15];
short magicNumber;
short checksum;
} gamedata;
typedef struct miscdata {
char unknown[16];
short soundSetting;
short langSetting;
char _unknown[8];
short magicNumber;
short checksum;
} miscdata;
struct file {
gamedata gameData[8];
miscdata otherData[2];
} file;
int main() {
file.gameData[0].magicNumber=0x4144;
printf( "%d\n", sizeof(file) );
FILE * fp = fopen("a.eep", "ab+");
fwrite(&file,sizeof(file),1,fp);
getchar();
}
#include <stdio.h>
typedef struct {
unsigned char Star : 7;
unsigned char Reserved : 1;
} lvl;
typedef struct {
char id;
char unknown;
short x;
short y;
short z;
} lostc;
typedef struct {
lostc lostCap;
lvl Castle;
unsigned char capLostInSnow : 1;
unsigned char capLostInSand : 1;
unsigned char capLostInTall : 1;
unsigned char unused : 1;
unsigned char starDoor3rdFlr : 1;
unsigned char unused2 : 3;
unsigned char wallPushed : 1;
unsigned char moatDrained : 1;
unsigned char starDoorSlide : 1;
unsigned char starDoorWhomp : 1;
unsigned char starDoorCool : 1;
unsigned char starDoorJolly : 1;
unsigned char starDoorBowsr1 : 1;
unsigned char starDoorBowsr2 : 1;
unsigned char slotIsOccupied : 1;
unsigned char capSwitchR : 1;
unsigned char capSwitchG : 1;
unsigned char capSwitchB : 1;
unsigned char haveKeyBasemnt : 1;
unsigned char haveKey2ndFlr : 1;
unsigned char DoorUnlckBasemnt : 1;
unsigned char DoorUnlck2ndFlr : 1;
lvl main[15];
lvl other[10];
unsigned char coinScore[15];
char magicNumber[2];
short checksum;
} gdat;
typedef struct {
char unknown[16];
short soundSetting;
short langSetting;
char _unknown[8];
char magicNumber[2];
short checksum;
} odat;
struct File {
gdat gameData[8];
odat otherData[2];
} file;
unsigned EEPROM[512];
loadData(char * file)
{
if(file){
FILE * stream = fopen(file, "rb");
int addr;
for (addr = 0; addr < 512; addr++)
{ EEPROM[addr] = fgetc(stream); }
fclose(stream);
printf("Data was loaded.\n");
//checkData();
} else {
printf("There was no input.");
}
}
main(int argc, char * argv[])
{
loadData(argv[1]);
getc(stdin); // keep window open
}
#include <stdio.h>
int main(int argc, char * argv[])
{
int addr, ch;
char EEPROM[512];
FILE * stream = fopen(argv[1], "rb");
for (addr = 0; addr < 512; ++addr)
{
EEPROM[addr] = ch;
ch = fgetc(stream);
printf("%03X-[%02X]\n", addr, ch);
}
// Just close the file
fclose(stream);
printf("Finished.\n");
//printf("%s", EEPROM[56]);
system("pause");
}
#include <stdio.h>
unsigned char arr[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
main(int argc, char*argv [])
{
if ( access(argv[1], 0) != -1 && argc == 2 )
{
printf("OK\n");
}
else if( access(argv[1], 0) == -1 && argc == 2 )
{
printf("Reading file: %s\nThe file doesn't seem to exist", argv[1] );
}
else
{
printf("Controller for operations version 0.0.1 (bryc)\n----\nusage: %s fname\n", argv[0] );
}
}
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Code::Blocks Template Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment