1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - MI - demos - uncompressBLZ
3   File:     main.c
4 
5   Copyright 2008 Nintendo. 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   $Date:: 2008-12-08#$
14   $Rev: 9555 $
15   $Author: kitase_hirotake $
16  *---------------------------------------------------------------------------*/
17 
18 #include <nitro.h>
19 #include "DEMO.h"
20 
21 #define BLZ_FILE_PATH   "/data/image1.bin_BLZ.bin"
22 
InitAlloc(void)23 static void InitAlloc(void)
24 {
25     void   *tempLo;
26     OSHeapHandle hh;
27 
28     tempLo = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), 1);
29     OS_SetArenaLo(OS_ARENA_MAIN, tempLo);
30     hh = OS_CreateHeap(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi());
31     if (hh < 0)
32     {
33         OS_Panic("ARM9: Fail to create heap...\n");
34     }
35     hh = OS_SetCurrentHeap(OS_ARENA_MAIN, hh);
36 }
37 
38 
NitroMain(void)39 void NitroMain(void)
40 {
41     int    result;
42     u32    comp_size, uncomp_size;
43     FSFile file[1];
44     u8*    buf;
45 
46     //---------------------------------------------------------------------------
47     // Initialize:
48     // Enable IRQ interrupts and initialize VRAM
49     //---------------------------------------------------------------------------
50     DEMOInitCommon();
51     DEMOInitVRAM();
52     InitAlloc();
53     FS_Init(3);
54     OS_InitTick();
55 
56     //---------------------------------------------------------------------------
57     // Map VRAM bank A onto LCDC.
58     //---------------------------------------------------------------------------
59     GX_SetBankForLCDC(GX_VRAM_LCDC_A);
60 
61     OS_TPrintf("uncompress BLZ file demo.\n");
62 
63     // Get the the data size before and after decompression
64     FS_InitFile(file);
65 
66     OS_TPrintf("open BLZ file...\n");
67     if (FS_OpenFileEx(file, BLZ_FILE_PATH, FS_FILEMODE_R))
68     {
69         // Before decompression
70         comp_size = FS_GetFileLength(file);
71 
72         // After decompression
73         if (!FS_SeekFile(file, -4, FS_SEEK_END))
74         {
75             (void)FS_CloseFile(file);
76             OS_Panic("seek file failed.\n");
77         }
78 
79         // From the footer of the BLZ file, get the increase in data size after decompression
80         (void)FS_ReadFile(file, &uncomp_size, sizeof(u32));
81         uncomp_size += comp_size;
82         OS_TPrintf("success.\n");
83     }
84     else
85     {
86         OS_Panic("BLZ file opening failed.\n");
87     }
88 
89     // In main memory, allocate a buffer of the size of obtained data
90     OS_TPrintf("allocate buffer after uncompressing...\n");
91     if (NULL == (buf = OS_Alloc(uncomp_size)))
92     {
93         OS_Panic("failed.\n");
94     }
95 
96     // Load the BLZ data into the allocated buffer
97     OS_TPrintf("load BLZ file to main memory.\n");
98     (void)FS_SeekFileToBegin(file);
99     if (FS_ReadFile(file, buf, (s32)comp_size) == -1)
100     {
101         (void)FS_CloseFile(file);
102         OS_Panic("load failed.\n");
103     }
104     (void)FS_CloseFile(file);
105 
106     // BLZ decompression
107     OS_TPrintf("uncompress BLZ file...\n");
108     if ((result = MI_SecureUncompressBLZ(buf, comp_size, uncomp_size)) < 0)
109     {
110         OS_Panic("failed. (error: %d)\n", result);
111     }
112     OS_TPrintf("success. (%d -> %d byte)\n", comp_size, uncomp_size);
113 
114     // From VRAM transfer to display
115     OS_TPrintf("display uncompressed file.\n");
116     MI_DmaCopy32(3, buf, (void *)HW_LCDC_VRAM_A, 256 * 192 * sizeof(unsigned short));
117 
118     GX_SetGraphicsMode(GX_DISPMODE_VRAM_A, (GXBGMode)0, (GXBG0As)0);
119     DEMOStartDisplay();
120     OS_WaitVBlankIntr();
121 
122     // Free the allocated buffer
123     OS_Free(buf);
124 
125     OS_TPrintf("demo end.\n");
126     OS_Terminate();
127 }
128 
129 //---------------------------------------------------------------------------
130 // V-Blank interrupt function:
131 //
132 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
133 // OS_EnableIrqMask selects IRQ interrupts to enable, and
134 // OS_EnableIrq enables IRQ interrupts.
135 // Notice that you have to call OS_SetIrqCheckFlag to check a V-Blank interrupt.
136 //---------------------------------------------------------------------------
VBlankIntr(void)137 void VBlankIntr(void)
138 {
139     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // Checking V-Blank interrupt
140 }
141