]> git.sur5r.net Git - openocd/blob - src/helper/binarybuffer.h
Add a bit-level transfer queue
[openocd] / src / helper / binarybuffer.h
1 /***************************************************************************
2  *   Copyright (C) 2004, 2005 by Dominic Rath                              *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23
24 #ifndef BINARYBUFFER_H
25 #define BINARYBUFFER_H
26
27 #include "list.h"
28
29 /** @file
30  * Support functions to access arbitrary bits in a byte array
31  */
32
33 /**
34  * Sets @c num bits in @c _buffer, starting at the @c first bit,
35  * using the bits in @c value.  This routine fast-paths writes
36  * of little-endian, byte-aligned, 32-bit words.
37  * @param _buffer The buffer whose bits will be set.
38  * @param first The bit offset in @c _buffer to start writing (0-31).
39  * @param num The number of bits from @c value to copy (1-32).
40  * @param value Up to 32 bits that will be copied to _buffer.
41  */
42 static inline void buf_set_u32(void *_buffer,
43         unsigned first, unsigned num, uint32_t value)
44 {
45         uint8_t *buffer = (uint8_t *)_buffer;
46
47         if ((num == 32) && (first == 0)) {
48                 buffer[3] = (value >> 24) & 0xff;
49                 buffer[2] = (value >> 16) & 0xff;
50                 buffer[1] = (value >> 8) & 0xff;
51                 buffer[0] = (value >> 0) & 0xff;
52         } else {
53                 for (unsigned i = first; i < first + num; i++) {
54                         if (((value >> (i - first)) & 1) == 1)
55                                 buffer[i / 8] |= 1 << (i % 8);
56                         else
57                                 buffer[i / 8] &= ~(1 << (i % 8));
58                 }
59         }
60 }
61 /**
62  * Retrieves @c num bits from @c _buffer, starting at the @c first bit,
63  * returning the bits in a 32-bit word.  This routine fast-paths reads
64  * of little-endian, byte-aligned, 32-bit words.
65  * @param _buffer The buffer whose bits will be read.
66  * @param first The bit offset in @c _buffer to start reading (0-31).
67  * @param num The number of bits from @c _buffer to read (1-32).
68  * @returns Up to 32-bits that were read from @c _buffer.
69  */
70 static inline uint32_t buf_get_u32(const void *_buffer,
71         unsigned first, unsigned num)
72 {
73         uint8_t *buffer = (uint8_t *)_buffer;
74
75         if ((num == 32) && (first == 0)) {
76                 return (((uint32_t)buffer[3]) << 24) |
77                                 (((uint32_t)buffer[2]) << 16) |
78                                 (((uint32_t)buffer[1]) << 8) |
79                                 (((uint32_t)buffer[0]) << 0);
80         } else {
81                 uint32_t result = 0;
82                 for (unsigned i = first; i < first + num; i++) {
83                         if (((buffer[i / 8] >> (i % 8)) & 1) == 1)
84                                 result |= 1 << (i - first);
85                 }
86                 return result;
87         }
88 }
89
90 /**
91  * Inverts the ordering of bits inside a 32-bit word (e.g. 31..0 -> 0..31).
92  * This routine can be used to flip smaller data types by using smaller
93  * values for @c width.
94  * @param value The word to flip.
95  * @param width The number of bits in value (2-32).
96  * @returns A 32-bit word with @c value in reversed bit-order.
97  */
98 uint32_t flip_u32(uint32_t value, unsigned width);
99
100 bool buf_cmp(const void *buf1, const void *buf2, unsigned size);
101 bool buf_cmp_mask(const void *buf1, const void *buf2,
102                 const void *mask, unsigned size);
103
104 /**
105  * Copies @c size bits out of @c from and into @c to.  Any extra
106  * bits in the final byte will be set to zero.
107  * @param from The buffer to copy into @c to.
108  * @param to The buffer that will receive the copy of @c from.
109  * @param size The number of bits to copy.
110  */
111 void *buf_cpy(const void *from, void *to, unsigned size);
112
113 /**
114  * Set the contents of @c buf with @c count bits, all set to 1.
115  * @param buf The buffer to fill with ones.
116  * @param size The number of bits.
117  * @returns The original buffer (@c buf).
118  */
119 void *buf_set_ones(void *buf, unsigned size);
120
121 void *buf_set_buf(const void *src, unsigned src_start,
122                   void *dst, unsigned dst_start, unsigned len);
123
124 int str_to_buf(const char *str, unsigned len,
125                 void *bin_buf, unsigned buf_size, unsigned radix);
126 char *buf_to_str(const void *buf, unsigned size, unsigned radix);
127
128 /* read a uint32_t from a buffer in target memory endianness */
129 static inline uint32_t fast_target_buffer_get_u32(const void *p, bool le)
130 {
131         return le ? le_to_h_u32(p) : be_to_h_u32(p);
132 }
133
134 static inline void bit_copy(uint8_t *dst, unsigned dst_offset, const uint8_t *src,
135         unsigned src_offset, unsigned bit_count)
136 {
137         buf_set_buf(src, src_offset, dst, dst_offset, bit_count);
138 }
139
140 struct bit_copy_queue {
141         struct list_head list;
142 };
143
144 struct bit_copy_queue_entry {
145         uint8_t *dst;
146         unsigned dst_offset;
147         const uint8_t *src;
148         unsigned src_offset;
149         unsigned bit_count;
150         struct list_head list;
151 };
152
153 void bit_copy_queue_init(struct bit_copy_queue *q);
154 int bit_copy_queued(struct bit_copy_queue *q, uint8_t *dst, unsigned dst_offset, const uint8_t *src,
155                     unsigned src_offset, unsigned bit_count);
156 void bit_copy_execute(struct bit_copy_queue *q);
157 void bit_copy_discard(struct bit_copy_queue *q);
158
159 #endif /* BINARYBUFFER_H */