pureikyubu/Docs/RE/Descrambler.c
ogamespec c21147ccff Cumulative changes all around project, regarding Stage 1
Docs: New TODO moved to EMU, old renamed to old_todo
Docs: Old coding style is deprecated (old_style.txt)
Docs: Removed mention about CubeDocumented and fixed old emails
RE: boot.s, proved first asm line (lis instruction parameter)
RE: Added PAL and NTSC Boot and IPL IDA files
RE: Some work on NTSC IPL (identified many lib calls, including OS, GX)
RE: Added EXI Bootrom descrambler by segher
RE: GXInit
RE: Internal GX lib structures (GXPrivate.h)
RE: More details on lomem (OS versions)
RE: OSInit and OS.c
RE: OSAlloc (heap allocator)
RE: Very first code of Metrowerk runtime (__start.c)
Docs: Added copy of http://gcdev.narod.ru
Source\Utils: Command processor (Cmd.c)
Source\Utils: File wrapper
Source\Utils: Gekko disasm cleaned up and ported to plain C
Source\Utils: Double-linked lists
Source\Utils: Ported old Profiler code
Source\Utils: String utils
2015-09-04 19:45:10 +03:00

52 lines
1.2 KiB
C

// bootrom descrambler reversed by segher
// Copyright 2008 Segher Boessenkool <segher@kernel.crashing.org>
void Descrambler(u8* data, u32 size)
{
u8 acc = 0;
u8 nacc = 0;
u16 t = 0x2953;
u16 u = 0xd9c2;
u16 v = 0x3ff1;
u8 x = 1;
for (u32 it = 0; it < size;)
{
int t0 = t & 1;
int t1 = (t >> 1) & 1;
int u0 = u & 1;
int u1 = (u >> 1) & 1;
int v0 = v & 1;
x ^= t1 ^ v0;
x ^= (u0 | u1);
x ^= (t0 ^ u1 ^ v0) & (t0 ^ u0);
if (t0 == u0)
{
v >>= 1;
if (v0)
v ^= 0xb3d0;
}
if (t0 == 0)
{
u >>= 1;
if (u0)
u ^= 0xfb10;
}
t >>= 1;
if (t0)
t ^= 0xa740;
nacc++;
acc = 2*acc + x;
if (nacc == 8)
{
data[it++] ^= acc;
nacc = 0;
}
}
}