/*---------------------------------------------------------------------------* Project: Horizon File: main.cpp Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 46365 $ *---------------------------------------------------------------------------*/ #include #include #include namespace { static const size_t BUFFER_SIZE = 4096; u8 s_InputData[BUFFER_SIZE]; u8 s_CompressedData[BUFFER_SIZE]; u8 s_UncompressedData[BUFFER_SIZE]; union WorkBuffer { u8 buffer[1]; u8 huffman[nn::cx::HUFFMAN_COMPRESS_WORK_SIZE]; u8 lz[nn::cx::LZ_COMPRESS_WORK_SIZE]; } s_Work; // Initializes the data buffer with appropriate values void InitializeBuffer(void* _p, size_t size) { ::std::srand(static_cast(nn::os::Tick::GetSystemCurrent())); u32* p = reinterpret_cast(_p); for (s32 i = 0; i < size / sizeof(s32); i++) { p[i] = ::std::rand(); // Adjusts so that the size of the compressed data is smaller than the input data if (p[i] & 0x1) { p[i] = 0xffffffff; } } } // Checks that the data is equal bool VerifyData(const void* _p0, const void* _p1, size_t size) { const s64* p0 = reinterpret_cast(_p0); const s64* p1 = reinterpret_cast(_p1); for (s32 i = 0; i < size / sizeof(s64); i++) { if (p0[i] != p1[i]) return false; } return true; } } void nnMain() { // Call only nn::applet::Enable to also allow execution from the HOME Menu // HOME Menu transitions, POWER Button presses, and sleep are all unsupported nn::applet::Enable(); NN_LOG("Compress demo\n"); bool ret; NN_UNUSED_VAR(ret); // ---- Run-Length Encoding NN_LOG(" Run-length Encoding Test\n"); while (true) { // Initialize input data InitializeBuffer(s_InputData, BUFFER_SIZE); // Compression NN_LOG(" Compressing... "); size_t size = nn::cx::CompressRL( s_InputData, BUFFER_SIZE, s_CompressedData); // Redo if the size of the compressed data is larger than the input data if (size == 0) { NN_LOG("Failed\n"); continue; } NN_LOG("Done [size = %5d bytes]\n", size); // Extract NN_LOG(" Uncompressing... "); nn::cx::UncompressRL(s_CompressedData, s_UncompressedData); NN_LOG("Done\n"); // Check NN_LOG(" Verifying... "); ret = VerifyData(s_InputData, s_UncompressedData, BUFFER_SIZE); NN_ASSERT(ret); NN_LOG("Done\n"); NN_LOG(" Finished\n"); break; } // ---- Huffman encoding for (s32 bit = 4; bit <= 8; bit += 4) { NN_LOG(" Huffman Coding (%dbit) Test\n", bit); while (true) { // Initialize input data InitializeBuffer(s_InputData, BUFFER_SIZE); // Compression NN_LOG(" Compressing... "); size_t size = nn::cx::CompressHuffman( s_InputData, BUFFER_SIZE, s_CompressedData, bit, s_Work.buffer); // Redo if the size of the compressed data is larger than the input data if (size == 0) { NN_LOG("Failed\n"); continue; } NN_LOG("Done [size = %5d bytes]\n", size); // Extract NN_LOG(" Uncompressing... "); nn::cx::UncompressHuffman(s_CompressedData, s_UncompressedData); NN_LOG("Done\n"); // Check NN_LOG(" Verifying... "); ret = VerifyData(s_InputData, s_UncompressedData, BUFFER_SIZE); NN_ASSERT(ret); NN_LOG("Done\n"); NN_LOG(" Finished\n"); break; } } // ---- LZ Encoding NN_LOG(" LZ Encoding Test \n"); while (true) { // Initialize input data InitializeBuffer(s_InputData, BUFFER_SIZE); // Compression NN_LOG(" Compressing... "); size_t size = nn::cx::CompressLZ( s_InputData, BUFFER_SIZE, s_CompressedData, s_Work.buffer); // Redo if the size of the compressed data is larger than the input data if (size == 0) { NN_LOG("Failed\n"); continue; } NN_LOG("Done [size = %5d bytes]\n", size); // Extract NN_LOG(" Uncompressing... "); nn::cx::UncompressLZ(s_CompressedData, s_UncompressedData); NN_LOG("Done\n"); // Check NN_LOG(" Verifying... "); ret = VerifyData(s_InputData, s_UncompressedData, BUFFER_SIZE); NN_ASSERT(ret); NN_LOG("Done\n"); NN_LOG(" Finished\n"); break; } NN_LOG("Compress demo finished\n"); while (true) {} } /*---------------------------------------------------------------------------* End of file *---------------------------------------------------------------------------*/