Subversion Repositories freemyipod

Rev

Rev 881 | Rev 939 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
881 theseven 1
#ifndef __SYS_UTIL_H__
2
#define __SYS_UTIL_H__
3
 
4
#include "global.h"
5
 
6
#define STRINGIFY2(x) #x
7
#define STRINGIFY(x) STRINGIFY2(x)
8
#define CONCAT2(x,y) x#y
9
#define CONCAT(x,y) CONCAT2(x,y)
10
 
11
#define MIN(a, b) (((a)<(b))?(a):(b))
12
#define MAX(a, b) (((a)>(b))?(a):(b))
13
#define ABS(a) (((a)>0)?(a):-(a))
14
#define BIT(x) (1 << (x))
15
#define BITRANGE(x, y) ((0xfffffffful >> (31 + (x) - (y))) << (x))
16
#define ARRAYLEN(x) (sizeof(x) ? sizeof(x) / sizeof(*(x)) : 0)
17
#define PUN_PTR(type, p) ((type)(intptr_t)(p))
18
#define OFFSETOF(type, membername) ((off_t)&((type *)0)->membername)
19
#define SKIPBYTES(p, count) ((typeof (p))((char *)(p) + (count)))
20
#define ALIGN_UP_DIV(x, y) (((x) + (y) - 1) / (y))
21
#define ALIGN_UP(x, y) ((typeof(x))ALIGN_UP_DIV((uint32_t)x, y) * (y))
22
#define OBJ_CALL(obj, class, func, args...) ((obj)->class->func((obj), ## args))
23
 
24
#define SWAP_16(x) ((typeof(x))(unsigned short)(((unsigned short)(x) >> 8) | ((unsigned short)(x) << 8)))
25
#define SWAP_32(x) ((typeof(x))(unsigned long)((((unsigned long)(x) & 0xff000000ul) >> 24) | \
26
                                                  (((unsigned long)(x) & 0xff0000ul) >> 8) | \
27
                                                  (((unsigned long)(x) & 0xff00ul) << 8) | \
28
                                                  (((unsigned long)(x) & 0xfful) << 24)))
29
#define REVERSE_32(x) ((typeof(x))(unsigned long)((((unsigned long)(x) & 0x80000000ul) >> 31) | \
30
                                                     (((unsigned long)(x) & 0x40000000ul) >> 29) | \
31
                                                     (((unsigned long)(x) & 0x20000000ul) >> 27) | \
32
                                                     (((unsigned long)(x) & 0x10000000ul) >> 25) | \
33
                                                     (((unsigned long)(x) & 0x8000000ul) >> 23) | \
34
                                                     (((unsigned long)(x) & 0x4000000ul) >> 21) | \
35
                                                     (((unsigned long)(x) & 0x2000000ul) >> 19) | \
36
                                                     (((unsigned long)(x) & 0x1000000ul) >> 17) | \
37
                                                     (((unsigned long)(x) & 0x800000ul) >> 15) | \
38
                                                     (((unsigned long)(x) & 0x400000ul) >> 13) | \
39
                                                     (((unsigned long)(x) & 0x200000ul) >> 11) | \
40
                                                     (((unsigned long)(x) & 0x100000ul) >> 9) | \
41
                                                     (((unsigned long)(x) & 0x80000ul) >> 7) | \
42
                                                     (((unsigned long)(x) & 0x40000ul) >> 5) | \
43
                                                     (((unsigned long)(x) & 0x20000ul) >> 3) | \
44
                                                     (((unsigned long)(x) & 0x10000ul) >> 1) | \
45
                                                     (((unsigned long)(x) & 0x8000ul) << 1) | \
46
                                                     (((unsigned long)(x) & 0x4000ul) << 3) | \
47
                                                     (((unsigned long)(x) & 0x2000ul) << 5) | \
48
                                                     (((unsigned long)(x) & 0x1000ul) << 7) | \
49
                                                     (((unsigned long)(x) & 0x800ul) << 9) | \
50
                                                     (((unsigned long)(x) & 0x400ul) << 11) | \
51
                                                     (((unsigned long)(x) & 0x200ul) << 13) | \
52
                                                     (((unsigned long)(x) & 0x100ul) << 15) | \
53
                                                     (((unsigned long)(x) & 0x80ul) << 17) | \
54
                                                     (((unsigned long)(x) & 0x40ul) << 19) | \
55
                                                     (((unsigned long)(x) & 0x20ul) << 21) | \
56
                                                     (((unsigned long)(x) & 0x10ul) << 23) | \
57
                                                     (((unsigned long)(x) & 0x8ul) << 25) | \
58
                                                     (((unsigned long)(x) & 0x4ul) << 27) | \
59
                                                     (((unsigned long)(x) & 0x2ul) << 29) | \
60
                                                     (((unsigned long)(x) & 0x1ul) << 31)))
925 theseven 61
#ifdef ENDIANNESS_LITTLE
881 theseven 62
#define LE_TO_H16(x) (x)
63
#define LE_TO_H32(x) (x)
64
#define H_TO_LE16(x) (x)
65
#define H_TO_LE32(x) (x)
66
#define BE_TO_H16(x) SWAP_16(x)
67
#define BE_TO_H32(x) SWAP_32(x)
68
#define H_TO_BE16(x) SWAP_16(x)
69
#define H_TO_BE32(x) SWAP_32(x)
70
#define letoh16(x) (x)
71
#define letoh32(x) (x)
72
#define htole16(x) (x)
73
#define htole32(x) (x)
74
#define betoh16(x) swap16(x)
75
#define betoh32(x) swap32(x)
76
#define htobe16(x) swap16(x)
77
#define htobe32(x) swap32(x)
78
#else
79
#define LE_TO_H16(x) SWAP_16(x)
80
#define LE_TO_H32(x) SWAP_32(x)
81
#define H_TO_LE16(x) SWAP_16(x)
82
#define H_TO_LE32(x) SWAP_32(x)
83
#define BE_TO_H16(x) (x)
84
#define BE_TO_H32(x) (x)
85
#define H_TO_BE16(x) (x)
86
#define H_TO_BE32(x) (x)
87
#define letoh16(x) swap16(x)
88
#define letoh32(x) swap32(x)
89
#define htole16(x) swap16(x)
90
#define htole32(x) swap32(x)
91
#define betoh16(x) (x)
92
#define betoh32(x) (x)
93
#define htobe16(x) (x)
94
#define htobe32(x) (x)
95
#endif
96
 
97
#ifndef CACHEALIGN_BITS
98
#define CACHEALIGN_BITS 2
99
#endif
100
#define CACHEALIGN_SIZE BIT(CACHEALIGN_BITS)
101
#define CACHEALIGN_ATTR __attribute__((aligned(CACHEALIGN_SIZE)))
102
#define CACHEALIGN_AT_LEAST_ATTR(x) __attribute__((aligned(MAX(CACHEALIGN_SIZE, (x)))))
103
#ifndef DMAALIGN_BITS
104
#define DMAALIGN_BITS 2
105
#endif
106
#define DMAALIGN_SIZE BIT(DMAALIGN_BITS)
107
#define DMAALIGN_ATTR __attribute__((aligned(DMAALIGN_SIZE)))
108
#define DMAALIGN_AT_LEAST_ATTR(x) __attribute__((aligned(MAX(DMAALIGN_SIZE, (x)))))
109
 
110
extern __attribute__((noreturn)) void powerdown();
111
extern __attribute__((noreturn)) void hang();
112
extern __attribute__((pure)) void idle();
113
extern __attribute__((noreturn)) void execfirmware(void* address);
114
extern void enter_critical_section();
115
extern void leave_critical_section();
116
extern void clean_dcache(const void* addr, uint32_t len);
117
extern void invalidate_dcache(const void* addr, uint32_t len);
118
extern void invalidate_icache(const void* addr, uint32_t len);
119
extern void enable_mmu();
120
extern void disable_mmu();
121
extern __attribute__((pure)) uint32_t swap32(uint32_t data);
122
extern __attribute__((pure)) uint32_t swap16(uint32_t data);
123
extern __attribute__((pure)) uint32_t reverse32(uint32_t data);
124
extern void* memcpy(void* dst, const void* src, size_t len);
125
extern void* memmove(void* dst, const void* src, size_t len);
126
extern void* memset(void* dst, int val, size_t len);
925 theseven 127
extern int memcmp(const void* a, const void* b, size_t len);
128
extern size_t strlen(const char* string);
881 theseven 129
extern void to_hex(char* dest, int len, uint32_t value);
130
 
131
static inline void discard(uint32_t data)
132
{
133
}
134
 
135
#endif