1 // Misc utility functions.
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "util.h" // call16
8 #include "bregs.h" // struct bregs
9 #include "config.h" // BUILD_STACK_ADDR
12 /****************************************************************
14 ****************************************************************/
16 // Call a function with a specified register state. Note that on
17 // return, the interrupt enable/disable flag may be altered.
19 call16(struct bregs *callregs)
21 if (!MODESEGMENT && getesp() > BUILD_STACK_ADDR)
22 panic("call16 with invalid stack\n");
29 "calll __call16_from32"
31 : "+a" (callregs), "+m" (*callregs)
33 : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
37 call16big(struct bregs *callregs)
40 if (getesp() > BUILD_STACK_ADDR)
41 panic("call16 with invalid stack\n");
43 "calll __call16big_from32"
44 : "+a" (callregs), "+m" (*callregs)
46 : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
50 __call16_int(struct bregs *callregs, u16 offset)
53 callregs->code.seg = GET_SEG(CS);
55 callregs->code.seg = SEG_BIOS;
56 callregs->code.offset = offset;
61 /****************************************************************
63 ****************************************************************/
65 // Sum the bytes in the specified area.
67 checksum_far(u16 buf_seg, void *buf_far, u32 len)
73 sum += GET_VAR(ES, ((u8*)buf_far)[i]);
78 checksum(void *buf, u32 len)
80 return checksum_far(GET_SEG(SS), buf, len);
86 if (__builtin_constant_p(s))
87 return __builtin_strlen(s);
94 // Compare two areas of memory.
96 memcmp(const void *s1, const void *s2, size_t n)
99 if (*(u8*)s1 != *(u8*)s2)
100 return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
108 // Compare two strings.
110 strcmp(const char *s1, const char *s2)
114 return *s1 < *s2 ? -1 : 1;
123 memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
127 "rep stosb %%es:(%%di)"
128 : "+c"(len), "+D"(d_far)
134 memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
139 "rep stosw %%es:(%%di)"
140 : "+c"(len), "+D"(d_far)
146 memset(void *s, int c, size_t n)
149 ((char *)s)[--n] = c;
154 memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
161 "rep movsb (%%si),%%es:(%%di)\n"
163 : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
169 memcpy_fl(void *d_fl, const void *s_fl, size_t len)
172 memcpy_far(FLATPTR_TO_SEG(d_fl), (void*)FLATPTR_TO_OFFSET(d_fl)
173 , FLATPTR_TO_SEG(s_fl), (void*)FLATPTR_TO_OFFSET(s_fl)
176 memcpy(d_fl, s_fl, len);
181 memcpy(void *d1, const void *s1, size_t len)
183 #define memcpy __builtin_memcpy
186 SET_SEG(ES, GET_SEG(SS));
188 if (((u32)d1 | (u32)s1 | len) & 3) {
189 // non-aligned memcpy
191 "rep movsb (%%esi),%%es:(%%edi)"
192 : "+c"(len), "+S"(s1), "+D"(d)
196 // Common case - use 4-byte copy
199 "rep movsl (%%esi),%%es:(%%edi)"
200 : "+c"(len), "+S"(s1), "+D"(d)
205 // Copy from memory mapped IO. IO mem is very slow, so yield
206 // periodically. 'len' must be 4 byte aligned.
208 iomemcpy(void *d, const void *s, u32 len)
218 "rep movsl (%%esi),%%es:(%%edi)"
219 : "+c"(copylen), "+S"(s), "+D"(d)
226 memmove(void *d, const void *s, size_t len)
229 return memcpy(d, s, len);
234 *(char*)d = *(char*)s;
242 // Copy a string - truncating it if necessary.
244 strtcpy(char *dest, const char *src, size_t len)
247 while (--len && *src != '\0')
254 /****************************************************************
256 ****************************************************************/
258 // See if a keystroke is pending in the keyboard buffer.
260 check_for_keystroke(void)
263 memset(&br, 0, sizeof(br));
266 call16_int(0x16, &br);
267 return !(br.flags & F_ZF);
270 // Return a keystroke - waiting forever if necessary.
272 get_raw_keystroke(void)
275 memset(&br, 0, sizeof(br));
277 call16_int(0x16, &br);
281 // Read a keystroke - waiting up to 'msec' milliseconds.
283 get_keystroke(int msec)
285 u32 end = calc_future_timer(msec);
287 if (check_for_keystroke())
288 return get_raw_keystroke();
289 if (check_timer(end))