orca-sim
DataConvertionHelper.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * This file is part of project ORCA. More information on the project
3  * can be found at the following repositories at GitHub's website.
4  *
5  * http://https://github.com/andersondomingues/orca-sim
6  * http://https://github.com/andersondomingues/orca-software
7  * http://https://github.com/andersondomingues/orca-mpsoc
8  * http://https://github.com/andersondomingues/orca-tools
9  *
10  * Copyright (C) 2018-2020 Anderson Domingues, <ti.andersondomingues@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 ******************************************************************************/
26 #include "DataConvertionHelper.hpp"
27 
28 #include <iomanip>
29 
30 // string hex to int
31 int orcasim::gdbrsp::strhti(char* buffer, int length) {
32  char tmp[length];
33 
34  for (int i = 0; i < length; i++)
35  tmp[i] = '\0';
36 
37  for (int i = 0; i < length; i++) {
38  // is [0-9] digit
39  if (buffer[i] >= 48 && buffer[i] <= 57) {
40  tmp[i] = buffer[i];
41  // is [a-f] digit
42  } else if (buffer[i] >= 97 && buffer[i] <= 122) {
43  tmp[i] = buffer[i];
44  } else {
45  break;
46  }
47  }
48 
49  return static_cast<int>(strtol(tmp, NULL, 16));
50 }
51 
52 // find first occurrence of a character, returns index
53 int orcasim::gdbrsp::strfind(char* buffer, char find, int limit) {
54  for (int i = 0; i < limit; i++)
55  if (buffer[i] == find) return i;
56 
57  return -1;
58 }
59 
60 // byte array to hexa
61 void orcasim::gdbrsp::hexstr(char* output, char* input, uint32_t integers) {
62  uint32_t mask = 0xFFFFFFFF;
63 
64  uint32_t* input_i = (uint32_t*)input;
65  uint32_t output_i = 0;
66 
67  // converts integer by integer
68  for (uint32_t i =0; i < integers; i++) {
69  // TODO(a): check whether the two lines below are equivalent
70  // snprintf(&output[output_i],
71  // sizeof(&output[output_i]), "%08x", endswap(input_i[i] & mask));
72  sprintf(&output[output_i], "%08x", endswap(input_i[i] & mask));
73  output_i += 8;
74  }
75 }
76 
77 uint32_t orcasim::gdbrsp::endswap(uint32_t value) {
78  uint32_t tmp;
79 
80  tmp = ((value << 8) & 0xFF00FF00) | ((value >> 8) & 0xFF00FF);
81  value = (tmp << 16) | (tmp >> 16);
82 
83  return value;
84 }
int strfind(char *buffer, char find, int limit)
String find.
int strhti(char *buffer, int length)
String-to-int.
uint32_t endswap(uint32_t value)
Endianess Swap.
void hexstr(char *output, char *input, uint32_t integers)
String to hex.