1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - include - dsp - common
3 File: byteaccess.h
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 #ifndef TWL_DSP_BYTEACCESS_H_
18 #define TWL_DSP_BYTEACCESS_H_
19
20
21 #ifdef SDK_TWL
22 #include <twl/types.h>
23 #include <twl/os.h>
24 #else
25 #include <dsp/types.h>
26 #endif
27
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33
34 /*---------------------------------------------------------------------------*/
35 /* Declarations */
36
37 // Type definitions to use when sharing data during inter-processor communication.
38 typedef u16 DSPAddr; // This type expresses an address in the DSP (2 bytes, 1 word)
39 typedef u16 DSPWord; // This type expresses a size in the DSP (2 bytes, 1 word)
40 typedef u16 DSPByte; // This type expresses a single-byte unit in the DSP (1 byte, 1 word)
41 typedef u32 DSPWord32; // This type expresses a size in the DSP (2 bytes, 1 word)
42 typedef u32 DSPByte32; // This type expresses a single-byte unit in the DSP (1 byte, 1 word)
43 typedef u32 DSPAddrInARM; // This type has converted DSP addresses into bytes
44
45 // Explicit Type-Conversion Macros
46 #define DSP_ADDR_TO_ARM(address) (u32)((address) << 1)
47 #define DSP_ADDR_TO_DSP(address) (u16)((u32)(address) >> 1)
48 #define DSP_WORD_TO_ARM(word) (u16)((word) << 1)
49 #define DSP_WORD_TO_DSP(word) (u16)((word) >> 1)
50 #define DSP_32BIT_TO_ARM(value) (u32)(((u32)(value) >> 16) | ((u32)(value) << 16))
51 #define DSP_32BIT_TO_DSP(value) (u32)(((u32)(value) >> 16) | ((u32)(value) << 16))
52 #ifdef SDK_TWL
53 #define DSP_BYTE_TO_UNIT(byte) (u16)(byte)
54 #define DSP_UNIT_TO_BYTE(unit) (u16)(unit)
55 #else
56 #define DSP_BYTE_TO_UNIT(byte) (u16)((byte) >> 1)
57 #define DSP_UNIT_TO_BYTE(unit) (u16)((unit) << 1)
58 #endif
59
60 // The native size for sizeof(char) (this is 2 on the DSP and 1 on an ARM processor)
61 #define DSP_WORD_UNIT (3 - sizeof(DSPWord))
62
63
64 /*---------------------------------------------------------------------------*/
65 /* Functions */
66
67 #ifdef SDK_TWL
68
69 /*---------------------------------------------------------------------------*
70 Name: DSP_LoadWord
71
72 Description: Loads a single word (an even number of 16-bit units) from the DSP's data memory space.
73
74 Arguments: offset: The DSP address to transfer from (in words)
75
76 Returns: The loaded 16-bit data.
77 *---------------------------------------------------------------------------*/
DSP_LoadWord(DSPAddr offset)78 SDK_INLINE u16 DSP_LoadWord(DSPAddr offset)
79 {
80 u16 value;
81 OSIntrMode cpsr = OS_DisableInterrupts();
82 DSP_RecvFifo(DSP_FIFO_MEMSEL_DATA, &value, offset, DSP_WORD_TO_DSP(sizeof(u16)));
83 reg_DSP_PCFG &= ~(REG_DSP_PCFG_DRS_MASK | REG_DSP_PCFG_AIM_MASK);
84 (void)OS_RestoreInterrupts(cpsr);
85 return value;
86 }
87
88 /*---------------------------------------------------------------------------*
89 Name: DSP_StoreWord
90
91 Description: Writes a single word (an even number of 16-bit units) to the DSP's data memory space.
92
93 Arguments: offset: The DSP address to transfer to (in words)
94 value: The word value to write
95
96 Returns: None.
97 *---------------------------------------------------------------------------*/
DSP_StoreWord(DSPAddr offset,u16 value)98 SDK_INLINE void DSP_StoreWord(DSPAddr offset, u16 value)
99 {
100 OSIntrMode cpsr = OS_DisableInterrupts();
101 DSP_SendFifo(DSP_FIFO_MEMSEL_DATA, offset, &value, DSP_WORD_TO_DSP(sizeof(u16)));
102 // reg_DSP_PCFG &= ~(REG_DSP_PCFG_DRS_MASK | REG_DSP_PCFG_AIM_MASK);
103 (void)OS_RestoreInterrupts(cpsr);
104 }
105
106 /*---------------------------------------------------------------------------*
107 Name: DSP_Load8
108
109 Description: Loads 8 bits from the DSP's data memory space.
110
111 Arguments: offset: The DSP address to transfer from (in bytes)
112
113 Returns: The loaded 8-bit data.
114 *---------------------------------------------------------------------------*/
DSP_Load8(DSPAddrInARM offset)115 SDK_INLINE u8 DSP_Load8(DSPAddrInARM offset)
116 {
117 return (u8)(DSP_LoadWord(DSP_WORD_TO_DSP(offset)) >> ((offset & 1) << 3));
118 }
119
120 /*---------------------------------------------------------------------------*
121 Name: DSP_Load16
122
123 Description: Loads 16 bits from the DSP's data memory space.
124
125 Arguments: offset: The DSP address to transfer from (in bytes)
126
127 Returns: The loaded 16-bit data.
128 *---------------------------------------------------------------------------*/
129 u16 DSP_Load16(DSPAddrInARM offset);
130
131 /*---------------------------------------------------------------------------*
132 Name: DSP_Load32
133
134 Description: Loads 32 bits from the DSP's data memory space.
135
136 Arguments: offset: The DSP address to transfer from (in bytes)
137
138 Returns: The loaded 32-bit data.
139 *---------------------------------------------------------------------------*/
140 u32 DSP_Load32(DSPAddrInARM offset);
141
142 /*---------------------------------------------------------------------------*
143 Name: DSP_LoadData
144
145 Description: Loads data of an arbitrary length from the DSP's data memory space.
146
147 Arguments: offset: The DSP address to transfer from (in bytes)
148 buffer: Buffer to transfer data to
149 length: Size of the transmission
150
151 Returns: None.
152 *---------------------------------------------------------------------------*/
153 void DSP_LoadData(DSPAddrInARM offset, void *buffer, u32 length);
154
155 /*---------------------------------------------------------------------------*
156 Name: DSP_Store8
157
158 Description: Writes 8 bits to the DSP's data memory space.
159
160 Arguments: offset: The DSP address to transfer to (in bytes)
161 value: The 8-bit value to write
162
163 Returns: None.
164 *---------------------------------------------------------------------------*/
165 void DSP_Store8(DSPAddrInARM offset, u8 value);
166
167 /*---------------------------------------------------------------------------*
168 Name: DSP_Store16
169
170 Description: Writes 16 bits to the DSP's data memory space.
171
172 Arguments: offset: The DSP address to transfer to (in bytes)
173 value: The 16-bit value to write
174
175 Returns: None.
176 *---------------------------------------------------------------------------*/
177 void DSP_Store16(DSPAddrInARM offset, u16 value);
178
179 /*---------------------------------------------------------------------------*
180 Name: DSP_Store32
181
182 Description: Writes 32 bits to the DSP's data memory space.
183
184 Arguments: offset: The DSP address to transfer to (in bytes)
185 value: The 32-bit value to write
186
187 Returns: None.
188 *---------------------------------------------------------------------------*/
189 void DSP_Store32(DSPAddrInARM offset, u32 value);
190
191 /*---------------------------------------------------------------------------*
192 Name: DSP_StoreData
193
194 Description: Writes data of an arbitrary length to the DSP's data memory space.
195
196 Arguments: offset: The DSP address to transfer to (in bytes)
197 buffer: Buffer to transfer data from
198 length: Size of the transmission
199
200 Returns: None.
201 *---------------------------------------------------------------------------*/
202 void DSP_StoreData(DSPAddrInARM offset, const void *buffer, u32 length);
203
204
205 #endif // SDK_TWL
206
207
208 /*===========================================================================*/
209
210 #ifdef __cplusplus
211 } /* extern "C" */
212 #endif
213
214 #endif /* TWL_DSP_BYTEACCESS_H_ */
215