feat: Ability to send command to ATA device.
[lunaix-os.git] / lunaix-os / includes / lunaix / spike.h
1 #ifndef __LUNAIX_SPIKE_H
2 #define __LUNAIX_SPIKE_H
3
4 // Some helper functions. As helpful as Spike the Dragon! :)
5
6 // 除法向上取整
7 #define CEIL(v, k) (((v) + (1 << (k)) - 1) >> (k))
8
9 // 除法向下取整
10 #define FLOOR(v, k) ((v) >> (k))
11
12 // 获取v最近的最大k倍数
13 #define ROUNDUP(v, k) (((v) + (k)-1) & ~((k)-1))
14
15 // 获取v最近的最小k倍数
16 #define ROUNDDOWN(v, k) ((v) & ~((k)-1))
17
18 #define __USER__ __attribute__((section(".usrtext")))
19
20 inline static void
21 spin()
22 {
23     while (1)
24         ;
25 }
26
27 #ifdef __LUNAIXOS_DEBUG__
28 #define assert(cond)                                                           \
29     if (!(cond)) {                                                             \
30         __assert_fail(#cond, __FILE__, __LINE__);                              \
31     }
32
33 #define assert_msg(cond, msg)                                                  \
34     if (!(cond)) {                                                             \
35         __assert_fail(msg, __FILE__, __LINE__);                                \
36     }
37 void
38 __assert_fail(const char* expr, const char* file, unsigned int line)
39   __attribute__((noinline, noreturn));
40 #else
41 #define assert(cond) (void)(cond);          // assert nothing
42 #define assert_msg(cond, msg) (void)(cond); // assert nothing
43 #endif
44
45 void
46 panick(const char* msg);
47
48 #define wait_until(cond)                                                       \
49     while (!(cond))                                                            \
50         ;
51 #define loop_until(cond)                                                       \
52     while (!(cond))                                                            \
53         ;
54
55 #define wait_until_expire(cond, max)                                           \
56     ({                                                                         \
57         unsigned int __wcounter__ = (max);                                     \
58         while (!(cond) && __wcounter__-- > 0)                                  \
59             ;                                                                  \
60         __wcounter__;                                                          \
61     })
62
63 #endif /* __LUNAIX_SPIKE_H */