Tweak ZXBox colours: 1) 'Bright' black is the same as normal black on a real Spectrum...
[kugel-rb.git] / firmware / general.c
blob117e6c3548511a6ddacb44bb3a3e5e42f3c76b55
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Michael Sevakis
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <limits.h>
20 #include "system.h"
21 #include "config.h"
22 #include "general.h"
24 int round_value_to_list32(unsigned long value,
25 const unsigned long list[],
26 int count,
27 bool signd)
29 unsigned long dmin = ULONG_MAX;
30 int idmin = -1, i;
32 for (i = 0; i < count; i++)
34 unsigned long diff;
36 if (list[i] == value)
38 idmin = i;
39 break;
42 if (signd ? ((long)list[i] < (long)value) : (list[i] < value))
43 diff = value - list[i];
44 else
45 diff = list[i] - value;
47 if (diff < dmin)
49 dmin = diff;
50 idmin = i;
54 return idmin;
55 } /* round_value_to_list32 */
57 /* Number of bits set in src_mask should equal src_list length */
58 int make_list_from_caps32(unsigned long src_mask,
59 const unsigned long *src_list,
60 unsigned long caps_mask,
61 unsigned long *caps_list)
63 int i, count;
64 unsigned long mask;
66 for (mask = src_mask, count = 0, i = 0;
67 mask != 0;
68 src_mask = mask, i++)
70 unsigned long test_bit;
71 mask &= mask - 1; /* Zero lowest bit set */
72 test_bit = mask ^ src_mask; /* Isolate the bit */
73 if (test_bit & caps_mask) /* Add item if caps has test bit set */
74 caps_list[count++] = src_list ? src_list[i] : (unsigned long)i;
77 return count;
78 } /* make_list_from_caps32 */
80 /* Align a buffer and size to a size boundary while remaining within
81 * the original boundaries */
82 size_t align_buffer(void **start, size_t size, size_t align)
84 void *newstart = *start;
85 void *newend = newstart + size;
87 /* Align the end down and the start up */
88 newend = (void *)ALIGN_DOWN((intptr_t)newend, align);
89 newstart = (void *)ALIGN_UP((intptr_t)newstart, align);
91 /* Hmmm - too small for this */
92 if (newend <= newstart)
93 return 0;
95 /* Return adjusted pointer and size */
96 *start = newstart;
97 return newend - newstart;