1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - demos - exceptionDisplay-1
3   File:     main.c
4 
5   Copyright 2007-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-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18 
19 extern  _u32_div_f(void);
20 void    myExceptionCallback(u32 context, void *arg);
21 
22 /*---------------------------------------------------------------------------*
23   Name:         NitroMain
24 
25   Description:  Main.
26 
27   Arguments:    None.
28 
29   Returns:      None.
30  *---------------------------------------------------------------------------*/
NitroMain()31 void NitroMain()
32 {
33     OS_Init();
34 
35 	OS_EnableUserExceptionHandlerOnDebugger();
36     OS_SetUserExceptionHandler(myExceptionCallback, (void *)0);
37 
38     //---- The reason we're flushing the cache here is because the protection unit changes when an exception is generated. We want to reliably write addresses, etc. related to the exception.
39     //
40     //
41     DC_FlushAll();
42     DC_WaitWriteBufferEmpty();
43 
44 
45     //---- normal division
46 	OS_Printf("now calculate 120 / 6\n" );
47     {
48         int     a = 120;
49         volatile int b = 6;
50         volatile int c = a / b;
51     }
52     OS_Printf("not occurred exception.\n");
53 
54     //---- divisor is 0
55 	OS_Printf("now calculate 240 / 0\n" );
56     {
57         int     a = 240;
58         volatile b = 0;
59         volatile int c = a / b;
60     }
61     OS_Printf("not occurred exception.\n");
62 
63     OS_Terminate();
64 }
65 
66 /*---------------------------------------------------------------------------*
67   Name:         _u32_div_f
68 
69   Description:  Integer division
70                 If divisor is 0, access 0 address.
71 
72   Arguments:    None.
73 
74   Returns:      None.
75  *---------------------------------------------------------------------------*/
76 #include <nitro/code32.h>
_s32_div_f(void)77 SDK_FORCE_EXPORT static asm s32 _s32_div_f(void)
78 {
79        stmfd       sp!,{r4,lr}
80 
81        //---- Compare divisor to 0
82        cmp         r1,#0
83        bne         _1
84 
85        //---- Force exception to occur
86        ldr         r11, [r1,#0]
87 _1:
88        eor         r4,r0,r1
89        and         r4,r4,#-2147483648
90        cmp         r0,#0
91        rsblt       r0,r0,#0
92        addlt       r4,r4,#1
93        cmp         r1,#0
94        rsblt       r1,r1,#0
95        bl          _u32_div_f
96        ands        r3,r4,#-2147483648
97        rsbne       r0,r0,#0
98        ands        r3,r4,#1
99        rsbne       r1,r1,#0
100        ldmfd       sp!,{r4,lr}
101        bx          lr
102 }
103 #include <nitro/codereset.h>
104 
105 /*---------------------------------------------------------------------------*
106   Name:         myExceptionCallback
107 
108   Description:  User callback for exception.
109 
110   Arguments:    context:
111                 arg:
112 
113   Returns:      None.
114  *---------------------------------------------------------------------------*/
myExceptionCallback(u32 context,void * arg)115 void myExceptionCallback(u32 context, void *arg)
116 {
117 #ifdef SDK_FINALROM
118 #pragma unused( context, arg )
119 #endif
120 
121     OS_Printf("---- USER EXCEPTION CALLBACK\n");
122     OS_Printf("context=%x  arg=%x\n", context, arg);
123 
124     OS_Printf("---- Thread LIST\n");
125     OS_DumpThreadList();
126 }
127 
128 /*====== End of main.c ======*/
129