[driver-gen-git] Add git library files.
[driver-gen.git] / utilities.c
blob903311175cc376e6643885497c9917385e6ccacd
1 /**
2 * @file utilities.c
4 * @brief Handy DriverGen functions are located here.
6 * @author Copyright (c) 2004 - 2009 CERN. Georgievskiy Yury
8 * @date Created on 27/02/2004
9 */
10 #include <ctype.h>
11 #include "driverGen.h"
13 /**
14 * @brief Constructs a string representation of an integer.
16 * @param val -- value to convert
17 * @param base -- base of the value [2 - 16]
19 * Different based are possible (at least was tested for 2, 10, 16).
21 * @return pointer to the string with converted value - if succeed.
22 * @return NULL - if fails.
24 char *itoa(int val, int base)
26 static char buf[34] = { 0 };
27 char *out = &buf[33];
28 int sign = (val < 0) ? sign = -val : val;
30 /* check if the base is valid */
31 if (base < 2 || base > 16)
32 return (NULL);
34 /* base 16 and base 2 cases */
35 if (base == 16 || base == 2) {
36 unsigned int hval = val;
37 unsigned int hbase = base;
38 do {
39 *out = "0123456789abcdef"[hval % hbase];
40 --out;
41 hval /= hbase;
42 } while (hval);
44 if (base == 16) /* apply 0x prefix */
45 *out-- = 'x', *out = '0';
46 else
47 ++out;
49 return (out);
52 /* for all remaining bases */
53 do {
54 *out = "0123456789abcdef"[sign % base];
55 --out;
56 sign /= base;
57 } while (sign);
59 if (val < 0 && base == 10) /* apply negative sign only for base 10 */
60 *out = '-';
61 else
62 ++out;
64 return (out);
67 /**
68 * @brief Convert a string to lower case.
70 * @param string -- string to convert.
72 * @return void
74 void StrToLower(char *string)
76 int i = -1;
78 while (string[++i] != '\0')
79 string[i] = (char)tolower(string[i]);
82 /**
83 * @brief Convert a string to upper case.
85 * @param string -- string to convert
87 * @return void
89 void StrToUpper(char *string)
91 int i = -1;
93 while (string[++i] != '\0')
94 string[i] = (char)toupper(string[i]);
97 /**
98 * @brief Service function for debug purposes.
100 * @param reginfo -- register description
101 * @param regNum -- register amount
103 * @return void
105 void PrintoutRegInfo(RegisterDef_t * reginfo, int regNum)
107 int cntr;
109 for (cntr = 0; cntr < regNum; cntr++) {
110 printf("----- [%03d] %s -----\n\tblock %d\n\toffset %#x\n"
111 "\tdepth %d (%#x)\n--------------------\n",
112 cntr, reginfo[cntr].name, reginfo[cntr].blockP->blockID,
113 reginfo[cntr].offset, reginfo[cntr].depth,
114 reginfo[cntr].depth);
120 * @brief How many registers does block has
122 * @param reg -- first register in the block
124 * @return register amount
126 static int calc_block_reg_amount(RegisterDef_t *reg)
128 int cntr = 0;
129 int bid = reg->blockP->blockID;
131 while (bid == reg->blockP->blockID) {
132 ++cntr;
133 if (reg->last)
134 break;
135 else
136 ++reg;
139 return cntr;
143 * @brief Get block size in bytes.
145 * @param reg -- first register in the block
146 * @param type -- driver (DRIVER_FT) or simulator (SIM_FT)
148 * Calculate block size, depending on the type (driver/simulator).
149 * If driver -- gaps are taken into account.
150 * If simulator -- there will be no gaps.
152 * @return block size in bytes
154 int calc_block_size(RegisterDef_t *reg, int type)
156 long bsz = 0;
157 int i;
158 int ram = calc_block_reg_amount(reg);
160 /* for simulator -- count size without gaps */
161 if (type == SIM_FT) {
162 for (i = 0; i < ram; i++) {
163 bsz += ((reg[i].depth) ? reg[i].depth : 1) *
164 reg[i].regSize;
166 } else { /* count gaps only for real driver */
167 reg = &reg[ram-1]; /* last block register */
168 bsz = reg->offset + ((reg->depth) ? reg->depth : 1)
169 * reg->regSize;
172 return bsz;
177 * @brief Find out how many gaps does block has.
179 * @param reg -- first register in the block
181 * @return gap amount
183 int calc_block_gap_amount(RegisterDef_t *reg)
185 int gap = 0;
186 int i, addr;
187 RegisterDef_t *preg;
188 int ram = calc_block_reg_amount(reg);
190 if (reg->offset)
191 ++gap;
193 for (i = 1, preg = &reg[i-1], reg = &reg[1]; i < ram;
194 i++, reg++, preg++) {
195 addr = preg->offset + ((preg->depth) ? preg->depth : 1)
196 * preg->regSize;
197 if (reg->offset - addr)
198 ++gap;
201 return gap;
205 * @brief Set block size and block register amount information.
207 * @param regs -- register descr
209 * This function is called after we get and check all info from the DB
210 * in order to set missing information blocks, namely:
211 * 1. block size for the real driver (gap sizes are taken into account)
212 * 2. block size for driver simulator (gaps are not considered)
213 * 3. number of registers in the block.
215 void set_extra_block_data(RegisterDef_t *regs)
217 int ram = 0;
218 RegisterDef_t *ptr;
220 do {
221 ram = calc_block_reg_amount(regs);
222 ptr = &regs[ram-1]; /* 'last register' detection */
223 regs->blockP->blksz_drvr = calc_block_size(regs, DRIVER_FT);
224 regs->blockP->blksz_sim = calc_block_size(regs, SIM_FT);
225 regs->blockP->reg_am = ram;
226 regs += ram; /* move to the first register of next block */
227 } while (!ptr->last);