Реверс-инжиниринг ToumaPet, китайского клона тамагочи с цветным экраном

9be076bb33c56b38d35b068a00819d64.jpg
static const uint8_t adpcm_value[256] = {
	0x00, 0x01, 0x02, 0x04, 0x05, 0x08, 0x0a, 0x0d,
	0x00, 0x02, 0x05, 0x06, 0x0a, 0x0f, 0x16, 0x1a,
	0x00, 0x03, 0x07, 0x09, 0x0c, 0x10, 0x12, 0x1c,
	0x01, 0x04, 0x08, 0x0b, 0x0f, 0x14, 0x1b, 0x27,
	0x02, 0x05, 0x0a, 0x0f, 0x14, 0x1a, 0x23, 0x32,
	0x02, 0x07, 0x0c, 0x12, 0x19, 0x20, 0x29, 0x3b,
	0x02, 0x09, 0x10, 0x17, 0x1e, 0x26, 0x30, 0x44,
	0x02, 0x0a, 0x12, 0x1a, 0x22, 0x2c, 0x39, 0x51,
	0x02, 0x0b, 0x15, 0x1e, 0x27, 0x32, 0x40, 0x5b,
	0x03, 0x0d, 0x16, 0x20, 0x2a, 0x36, 0x45, 0x61,
	0x04, 0x0f, 0x18, 0x22, 0x2d, 0x39, 0x4a, 0x6f,
	0x04, 0x10, 0x1c, 0x26, 0x32, 0x40, 0x54, 0x7e,
	0x04, 0x11, 0x1e, 0x2a, 0x38, 0x47, 0x61, 0xa0,
	0x04, 0x12, 0x20, 0x2d, 0x3c, 0x4f, 0x6c, 0xa3,
	0x05, 0x15, 0x23, 0x31, 0x41, 0x54, 0x70, 0xa6,
	0x06, 0x17, 0x26, 0x35, 0x45, 0x58, 0x74, 0xaf,
	0x06, 0x18, 0x29, 0x3a, 0x4a, 0x5e, 0x78, 0xb0,
	0x08, 0x1a, 0x2b, 0x3d, 0x4f, 0x63, 0x7e, 0xb1,
	0x09, 0x1d, 0x30, 0x43, 0x57, 0x6d, 0x89, 0xb4,
	0x0a, 0x1e, 0x33, 0x47, 0x5c, 0x73, 0x8e, 0xb5,
	0x0b, 0x21, 0x37, 0x4c, 0x63, 0x7b, 0x97, 0xc0,
	0x0c, 0x24, 0x3c, 0x52, 0x68, 0x83, 0x9f, 0xce,
	0x0d, 0x25, 0x41, 0x59, 0x72, 0x8d, 0xa9, 0xd1,
	0x0f, 0x2a, 0x48, 0x60, 0x7c, 0x97, 0xb2, 0xd3,
	0x11, 0x2f, 0x4e, 0x6c, 0x89, 0xa3, 0xc2, 0xe4,
	0x12, 0x34, 0x56, 0x74, 0x8f, 0xaf, 0xd0, 0xec,
	0x13, 0x3a, 0x5b, 0x7c, 0x97, 0xb1, 0xd3, 0xef,
	0x18, 0x3c, 0x63, 0x83, 0xa4, 0xc1, 0xdd, 0xf0,
	0x1a, 0x42, 0x69, 0x93, 0xb0, 0xd3, 0xec, 0xf5,
	0x1b, 0x46, 0x6a, 0x96, 0xb9, 0xde, 0xed, 0xf9,
	0x1d, 0x48, 0x6b, 0xa1, 0xba, 0xe3, 0xfe, 0xfe,
	0x1e, 0x4d, 0x79, 0xb3, 0xf5, 0xfe, 0xfe, 0xfe };

static uint8_t adpcm_next[256] {
	0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x20,
	0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x10, 0x28,
	0x08, 0x08, 0x08, 0x10, 0x18, 0x18, 0x18, 0x30,
	0x10, 0x10, 0x10, 0x18, 0x20, 0x20, 0x20, 0x38,
	0x18, 0x18, 0x18, 0x20, 0x28, 0x28, 0x28, 0x40,
	0x20, 0x20, 0x20, 0x28, 0x30, 0x30, 0x30, 0x48,
	0x28, 0x28, 0x28, 0x30, 0x38, 0x38, 0x38, 0x50,
	0x30, 0x30, 0x30, 0x38, 0x40, 0x40, 0x40, 0x58,
	0x38, 0x38, 0x38, 0x40, 0x48, 0x48, 0x48, 0x60,
	0x40, 0x40, 0x40, 0x48, 0x50, 0x50, 0x50, 0x68,
	0x48, 0x48, 0x48, 0x50, 0x58, 0x58, 0x58, 0x70,
	0x50, 0x50, 0x50, 0x58, 0x60, 0x60, 0x60, 0x80,
	0x58, 0x58, 0x58, 0x60, 0x68, 0x68, 0x68, 0x90,
	0x60, 0x60, 0x60, 0x68, 0x70, 0x70, 0x70, 0x98,
	0x68, 0x68, 0x68, 0x70, 0x78, 0x78, 0x78, 0xa0,
	0x70, 0x70, 0x70, 0x78, 0x80, 0x80, 0x80, 0xa8,
	0x78, 0x78, 0x78, 0x80, 0x88, 0x88, 0x88, 0xb0,
	0x80, 0x80, 0x80, 0x88, 0x90, 0x90, 0x90, 0xb8,
	0x88, 0x88, 0x88, 0x90, 0x98, 0x98, 0x98, 0xc0,
	0x90, 0x90, 0x90, 0x98, 0xa0, 0xa0, 0xa0, 0xc8,
	0x90, 0x98, 0x98, 0xa0, 0xa8, 0xa8, 0xa8, 0xd0,
	0x98, 0xa0, 0xa0, 0xa8, 0xb0, 0xb0, 0xb0, 0xd8,
	0xa0, 0xa8, 0xa8, 0xb0, 0xb8, 0xb8, 0xb8, 0xe0,
	0xa8, 0xb0, 0xb0, 0xb8, 0xc0, 0xc0, 0xc0, 0xe8,
	0xb0, 0xb8, 0xb8, 0xc0, 0xc8, 0xc8, 0xc8, 0xf0,
	0xb8, 0xc0, 0xc0, 0xc8, 0xd0, 0xd0, 0xd0, 0xf8,
	0xc0, 0xc0, 0xc8, 0xd0, 0xd8, 0xd8, 0xd8, 0xf8,
	0xc8, 0xc8, 0xd0, 0xd0, 0xe0, 0xe0, 0xe0, 0xf8,
	0xd0, 0xd0, 0xd0, 0xd8, 0xe8, 0xe8, 0xe8, 0xf8,
	0xd8, 0xd8, 0xd8, 0xd8, 0xf0, 0xf0, 0xf0, 0xf8,
	0xd8, 0xd8, 0xe0, 0xe0, 0xf8, 0xf8, 0xf8, 0xf8,
	0xe0, 0xe0, 0xe8, 0xe8, 0xf8, 0xf8, 0xf8, 0xf8 };

typedef struct { uint8_t idx; } adpcm_status_t;

void adpcm_init(adpcm_status_t *adpcm) { adpcm->idx = 0; }

int adpcm_decode(adpcm_status_t *adpcm, unsigned x) {
	unsigned a = (x & 7) | adpcm->idx;
	adpcm->idx = adpcm_next[a];
	a = adpcm_value[a];
	return (x & 8 ? -a : a);
}

© Habrahabr.ru