4 * @brief Handy DriverGen functions are located here.
6 * @author Copyright (c) 2004 - 2009 CERN. Georgievskiy Yury
8 * @date Created on 27/02/2004
11 #include "driverGen.h"
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 };
28 int sign
= (val
< 0) ? sign
= -val
: val
;
30 /* check if the base is valid */
31 if (base
< 2 || base
> 16)
34 /* base 16 and base 2 cases */
35 if (base
== 16 || base
== 2) {
36 unsigned int hval
= val
;
37 unsigned int hbase
= base
;
39 *out
= "0123456789abcdef"[hval
% hbase
];
44 if (base
== 16) /* apply 0x prefix */
45 *out
-- = 'x', *out
= '0';
52 /* for all remaining bases */
54 *out
= "0123456789abcdef"[sign
% base
];
59 if (val
< 0 && base
== 10) /* apply negative sign only for base 10 */
68 * @brief Convert a string to lower case.
70 * @param string -- string to convert.
74 void StrToLower(char *string
)
78 while (string
[++i
] != '\0')
79 string
[i
] = (char)tolower(string
[i
]);
83 * @brief Convert a string to upper case.
85 * @param string -- string to convert
89 void StrToUpper(char *string
)
93 while (string
[++i
] != '\0')
94 string
[i
] = (char)toupper(string
[i
]);
98 * @brief Service function for debug purposes.
100 * @param reginfo -- register description
101 * @param regNum -- register amount
105 void PrintoutRegInfo(RegisterDef_t
* reginfo
, int regNum
)
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
)
129 int bid
= reg
->blockP
->blockID
;
131 while (bid
== reg
->blockP
->blockID
) {
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
)
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) *
166 } else { /* count gaps only for real driver */
167 reg
= ®
[ram
-1]; /* last block register */
168 bsz
= reg
->offset
+ ((reg
->depth
) ? reg
->depth
: 1)
177 * @brief Find out how many gaps does block has.
179 * @param reg -- first register in the block
183 int calc_block_gap_amount(RegisterDef_t
*reg
)
188 int ram
= calc_block_reg_amount(reg
);
193 for (i
= 1, preg
= ®
[i
-1], reg
= ®
[1]; i
< ram
;
194 i
++, reg
++, preg
++) {
195 addr
= preg
->offset
+ ((preg
->depth
) ? preg
->depth
: 1)
197 if (reg
->offset
- addr
)
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
)
221 ram
= calc_block_reg_amount(regs
);
222 ptr
= ®s
[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
);