1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: main.cpp
4
5 Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Rev: 46365 $
14 *---------------------------------------------------------------------------*/
15
16 #include <nn.h>
17 #include <nn/cx.h>
18 #include <nn/applet.h>
19
20 namespace
21 {
22 static const size_t BUFFER_SIZE = 4096;
23 u8 s_InputData[BUFFER_SIZE];
24 u8 s_CompressedData[BUFFER_SIZE];
25 u8 s_UncompressedData[BUFFER_SIZE];
26
27 union WorkBuffer
28 {
29 u8 buffer[1];
30 u8 huffman[nn::cx::HUFFMAN_COMPRESS_WORK_SIZE];
31 u8 lz[nn::cx::LZ_COMPRESS_WORK_SIZE];
32 } s_Work;
33
34 // Initializes the data buffer with appropriate values
InitializeBuffer(void * _p,size_t size)35 void InitializeBuffer(void* _p, size_t size)
36 {
37 ::std::srand(static_cast<s32>(nn::os::Tick::GetSystemCurrent()));
38
39 u32* p = reinterpret_cast<u32*>(_p);
40 for (s32 i = 0; i < size / sizeof(s32); i++)
41 {
42 p[i] = ::std::rand();
43
44 // Adjusts so that the size of the compressed data is smaller than the input data
45 if (p[i] & 0x1)
46 {
47 p[i] = 0xffffffff;
48 }
49 }
50 }
51
52 // Checks that the data is equal
VerifyData(const void * _p0,const void * _p1,size_t size)53 bool VerifyData(const void* _p0, const void* _p1, size_t size)
54 {
55 const s64* p0 = reinterpret_cast<const s64*>(_p0);
56 const s64* p1 = reinterpret_cast<const s64*>(_p1);
57 for (s32 i = 0; i < size / sizeof(s64); i++)
58 {
59 if (p0[i] != p1[i]) return false;
60 }
61 return true;
62 }
63 }
64
nnMain()65 void nnMain()
66 {
67 // Call only nn::applet::Enable to also allow execution from the HOME Menu
68 // HOME Menu transitions, POWER Button presses, and sleep are all unsupported
69 nn::applet::Enable();
70
71 NN_LOG("Compress demo\n");
72
73 bool ret;
74 NN_UNUSED_VAR(ret);
75
76 // ---- Run-Length Encoding
77 NN_LOG(" Run-length Encoding Test\n");
78 while (true)
79 {
80 // Initialize input data
81 InitializeBuffer(s_InputData, BUFFER_SIZE);
82
83 // Compression
84 NN_LOG(" Compressing... ");
85 size_t size = nn::cx::CompressRL(
86 s_InputData, BUFFER_SIZE, s_CompressedData);
87
88 // Redo if the size of the compressed data is larger than the input data
89 if (size == 0)
90 {
91 NN_LOG("Failed\n");
92 continue;
93 }
94 NN_LOG("Done [size = %5d bytes]\n", size);
95
96 // Extract
97 NN_LOG(" Uncompressing... ");
98 nn::cx::UncompressRL(s_CompressedData, s_UncompressedData);
99 NN_LOG("Done\n");
100
101 // Check
102 NN_LOG(" Verifying... ");
103 ret = VerifyData(s_InputData, s_UncompressedData, BUFFER_SIZE);
104 NN_ASSERT(ret);
105 NN_LOG("Done\n");
106
107 NN_LOG(" Finished\n");
108 break;
109 }
110
111 // ---- Huffman encoding
112 for (s32 bit = 4; bit <= 8; bit += 4)
113 {
114 NN_LOG(" Huffman Coding (%dbit) Test\n", bit);
115 while (true)
116 {
117 // Initialize input data
118 InitializeBuffer(s_InputData, BUFFER_SIZE);
119
120 // Compression
121 NN_LOG(" Compressing... ");
122 size_t size = nn::cx::CompressHuffman(
123 s_InputData, BUFFER_SIZE, s_CompressedData, bit, s_Work.buffer);
124
125 // Redo if the size of the compressed data is larger than the input data
126 if (size == 0)
127 {
128 NN_LOG("Failed\n");
129 continue;
130 }
131 NN_LOG("Done [size = %5d bytes]\n", size);
132
133 // Extract
134 NN_LOG(" Uncompressing... ");
135 nn::cx::UncompressHuffman(s_CompressedData, s_UncompressedData);
136 NN_LOG("Done\n");
137
138 // Check
139 NN_LOG(" Verifying... ");
140 ret = VerifyData(s_InputData, s_UncompressedData, BUFFER_SIZE);
141 NN_ASSERT(ret);
142 NN_LOG("Done\n");
143
144 NN_LOG(" Finished\n");
145 break;
146 }
147 }
148
149 // ---- LZ Encoding
150 NN_LOG(" LZ Encoding Test \n");
151 while (true)
152 {
153 // Initialize input data
154 InitializeBuffer(s_InputData, BUFFER_SIZE);
155
156 // Compression
157 NN_LOG(" Compressing... ");
158 size_t size = nn::cx::CompressLZ(
159 s_InputData, BUFFER_SIZE, s_CompressedData, s_Work.buffer);
160
161 // Redo if the size of the compressed data is larger than the input data
162 if (size == 0)
163 {
164 NN_LOG("Failed\n");
165 continue;
166 }
167 NN_LOG("Done [size = %5d bytes]\n", size);
168
169 // Extract
170 NN_LOG(" Uncompressing... ");
171 nn::cx::UncompressLZ(s_CompressedData, s_UncompressedData);
172 NN_LOG("Done\n");
173
174 // Check
175 NN_LOG(" Verifying... ");
176 ret = VerifyData(s_InputData, s_UncompressedData, BUFFER_SIZE);
177 NN_ASSERT(ret);
178 NN_LOG("Done\n");
179
180 NN_LOG(" Finished\n");
181 break;
182 }
183
184 NN_LOG("Compress demo finished\n");
185 while (true) {}
186 }
187
188 /*---------------------------------------------------------------------------*
189 End of file
190 *---------------------------------------------------------------------------*/
191