1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - CP - src
3   File:     context.c
4 
5   Copyright 2003-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-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #include <nitro/cp/context.h>
19 
20 #if defined(SDK_TCM_APPLY) && defined(SDK_ARM9)
21 #include    <nitro/itcm_begin.h>
22 #endif
23 #include <nitro/code32.h>
24 
25 /*---------------------------------------------------------------------------*
26   Name:         CP_SaveContext
27 
28   Description:  Save current context into specified memory
29 
30   Arguments:    context   pointer to the memory to be stored the current context
31 
32   Returns:      None
33  *---------------------------------------------------------------------------*/
CP_SaveContext(register CPContext * context)34 asm void CP_SaveContext(register CPContext *context)
35 {
36     ldr     r1,  =REG_DIV_NUMER_ADDR
37     stmfd   sp!, {r4}   // avoid stall
38     ldmia   r1,  {r2-r4,r12}
39     stmia   r0!, {r2-r4,r12}
40     ldrh    r12, [r1, #REG_DIVCNT_OFFSET - REG_DIV_NUMER_OFFSET]
41 
42     add     r1,  r1, #REG_SQRT_PARAM_OFFSET - REG_DIV_NUMER_OFFSET
43     ldmia   r1,  {r2-r3}
44     stmia   r0!, {r2-r3}
45 
46     and     r12, r12, #REG_CP_DIVCNT_MODE_MASK
47     ldrh    r2,  [r1, #REG_SQRTCNT_OFFSET - REG_SQRT_PARAM_OFFSET]
48     strh    r12, [r0]
49     and     r2,  r2, #REG_CP_SQRTCNT_MODE_MASK
50     strh    r2,  [r0, #2]
51     ldmfd   sp!, {r4}
52 
53     bx lr
54 }
55 
56 
57 /*---------------------------------------------------------------------------*
58   Name:         CP_RestoreContext
59 
60   Description:  Reload specified context as current context
61 
62   Arguments:    context   pointer to the memory to switch to the context
63 
64   Returns:      None
65  *---------------------------------------------------------------------------*/
CPi_RestoreContext(register const CPContext * context)66 asm void CPi_RestoreContext(register const CPContext *context)
67 {
68     stmfd   sp!, {r4}
69     ldr     r1,  =REG_DIV_NUMER_ADDR
70     ldmia   r0,  {r2-r4,r12}
71     stmia   r1,  {r2-r4,r12}
72     ldrh    r2,  [r0, #CPContext.div_mode]
73     ldrh    r3,  [r0, #CPContext.sqrt_mode]
74 
75     strh    r2,  [r1, #REG_DIVCNT_OFFSET  - REG_DIV_NUMER_OFFSET]
76     strh    r3,  [r1, #REG_SQRTCNT_OFFSET - REG_DIV_NUMER_OFFSET]
77 
78     add     r0,  r0, #CPContext.sqrt
79     add     r1,  r1, #REG_SQRT_PARAM_OFFSET - REG_DIV_NUMER_OFFSET
80 
81     ldmia   r0,  {r2-r3}
82     stmia   r1,  {r2-r3}
83 
84     ldmfd   sp!, {r4}
85 
86     bx lr
87 }
88 
89 #include <nitro/codereset.h>
90 
91 #if defined(SDK_TCM_APPLY) && defined(SDK_ARM9)
92 #include    <nitro/itcm_end.h>
93 #endif
94