summaryrefslogtreecommitdiffstats
path: root/firmware/system.h
blob: 28c33d6c198ef6f6c5687f24f4e81b7103cb6185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Alan Korr
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#ifndef __SYSTEM_H__
#define __SYSTEM_H__

#include "sh7034.h"
#include "config.h"

#ifdef ARCHOS_RECORDER
# define FREQ     11059200
#else
# define FREQ     12000000 /* cycle time ~83.3ns */
#endif
#define BAUDRATE 9600

#ifndef NULL
#define NULL ((void*)0)
#endif

#ifndef MIN
#define MIN(a, b) (((a)<(b))?(a):(b))
#endif

#ifndef MAX
#define MAX(a, b) (((a)>(b))?(a):(b))
#endif

#ifdef LITTLE_ENDIAN
#define SWAB16(x) (x)
#define SWAB32(x) (x)
#endif

#define nop \
  asm volatile ("nop")

#define __set_mask_constant(mask,address) \
  asm                                     \
    ("or.b\t%0,@(r0,gbr)"                 \
     :                                    \
     : /* %0 */ "I"((char)(mask)),        \
       /* %1 */ "z"(address-GBR))

#define __clear_mask_constant(mask,address) \
  asm                                       \
    ("and.b\t%0,@(r0,gbr)"                  \
     :                                      \
     : /* %0 */ "I"((char)~(mask)),         \
       /* %1 */ "z"(address-GBR))

#define __toggle_mask_constant(mask,address) \
  asm                                        \
    ("xor.b\t%0,@(r0,gbr)"                   \
     :                                       \
     : /* %0 */ "I"((char)(mask)),           \
       /* %1 */ "z"(address-GBR))

#define __test_mask_constant(mask,address)    \
  ({                                          \
    int result;                               \
    asm                                       \
      ("tst.b\t%1,@(r0,gbr)\n\tmovt\t%0"      \
       : "=r"(result)                         \
       : "I"((char)(mask)),"z"(address-GBR)); \
    result;                                   \
  })

#define __set_bit_constant(bit,address) \
  asm                                   \
    ("or.b\t%0,@(r0,gbr)"               \
     :                                  \
     : /* %0 */ "I"((char)(1<<(bit))),  \
       /* %1 */ "z"(address-GBR))

#define __clear_bit_constant(bit,address) \
  asm                                     \
    ("and.b\t%0,@(r0,gbr)"                \
     :                                    \
     : /* %0 */ "I"((char)~(1<<(bit))),   \
       /* %1 */ "z"(address-GBR))

#define __toggle_bit_constant(bit,address) \
  asm                                      \
    ("xor.b\t%0,@(r0,gbr)"                 \
     :                                     \
     : /* %0 */ "I"((char)(1<<(bit))),     \
       /* %1 */ "z"(address-GBR))

#define __test_bit_constant(bit,address)      \
  ({                                          \
    int result;                               \
    asm                                       \
      ("tst.b\t%1,@(r0,gbr)\n\tmovt\t%0"      \
       : "=r"(result)                         \
       : "I"((char)(1<<(bit))),"z"(address-GBR)); \
    result;                                   \
  })

#define __set_mask(mask,address)      /* FIXME */
#define __test_mask(mask,address)   0 /* FIXME */
#define __clear_mask(mask,address)    /* FIXME */
#define __toggle_mask(mask,address)   /* FIXME */

#define __set_bit(bit,address)      /* FIXME */
#define __test_bit(bit,address)   0 /* FIXME */
#define __clear_bit(bit,address)    /* FIXME */
#define __toggle_bit(bit,address)   /* FIXME */

#define set_mask(mask,address)          \
  if (__builtin_constant_p (mask))      \
    __set_mask_constant (mask,address); \
  else                                  \
    __set_mask (mask,address)

#define clear_mask(mask,address)          \
  if (__builtin_constant_p (mask))        \
    __clear_mask_constant (mask,address); \
  else                                    \
    __clear_mask (mask,address)

#define toggle_mask(mask,address)          \
  if (__builtin_constant_p (mask))         \
    __toggle_mask_constant (mask,address); \
  else                                     \
    __toggle_mask (mask,address)

#define test_mask(mask,address)              \
  (                                          \
    (__builtin_constant_p (mask))            \
  ? (int)__test_mask_constant (mask,address) \
  : (int)__test_mask (mask,address)          \
  )


#define set_bit(bit,address)          \
  if (__builtin_constant_p (bit))     \
    __set_bit_constant (bit,address); \
  else                                \
    __set_bit (bit,address)

#define clear_bit(bit,address)          \
  if (__builtin_constant_p (bit))       \
    __clear_bit_constant (bit,address); \
  else                                  \
    __clear_bit (bit,address)

#define toggle_bit(bit,address)          \
  if (__builtin_constant_p (bit))        \
    __toggle_bit_constant (bit,address); \
  else                                   \
    __toggle_bit (bit,address)

#define test_bit(bit,address)              \
  (                                        \
    (__builtin_constant_p (bit))           \
  ? (int)__test_bit_constant (bit,address) \
  : (int)__test_bit (bit,address)          \
  )


extern char __swap_bit[256];

#define swap_bit(byte) \
  __swap_bit[byte]

#ifndef SIMULATOR

static inline short SWAB16(short value)
  /*
    result[15..8] = value[ 7..0];
    result[ 7..0] = value[15..8];
  */    
{
    short result;
    asm volatile ("swap.b\t%1,%0" : "=r"(result) : "r"(value));
    return result;
}

static inline long SWAW32(long value)
  /*
    result[31..16] = value[15.. 0];
    result[15.. 0] = value[31..16];
  */    
{
    long result;
    asm volatile ("swap.w\t%1,%0" : "=r"(result) : "r"(value));
    return result;
}

static inline long SWAB32(long value)
  /*
    result[31..24] = value[ 7.. 0];
    result[23..16] = value[15.. 8];
    result[15.. 8] = value[23..16];
    result[ 7.. 0] = value[31..24];
  */    
{
    asm volatile ("swap.b\t%0,%0\n"
                  "swap.w\t%0,%0\n"
                  "swap.b\t%0,%0\n" : "+r"(value));
    return value;
}

/* Test And Set - UNTESTED */
static inline int tas (volatile int *pointer)
  {
    int result;
    asm volatile ("tas.b\t@%1;movt\t%0" : "=t"(result) : "r"((char *)pointer) : "memory");
    return result;
  }

static inline void sti (void)
  {
    asm volatile ("ldc\t%0,sr" : : "r"(0<<4));
  }

static inline void cli (void)
  {
    asm volatile ("ldc\t%0,sr" : : "r"(15<<4));
  }

/* Compare And Swap */
static inline int cas (volatile int *pointer,int requested_value,int new_value)
  {
    cli();
    if (*pointer == requested_value)
      {
        *pointer = new_value;
        sti ();
        return 1;
      }
    sti ();
    return 0;
  }

static inline int cas2 (volatile int *pointer1,volatile int *pointer2,int requested_value1,int requested_value2,int new_value1,int new_value2)
  {
    cli();
    if (*pointer1 == requested_value1 && *pointer2 == requested_value2)
      {
        *pointer1 = new_value1;
        *pointer2 = new_value2;
        sti ();
        return 1;
      }
    sti ();
    return 0;
  }

#endif

extern void system_reboot (void);
extern void system_init(void);

#endif