xref: /XiangShan/tools/readmemh/groupby-4byte.c (revision f320e0f01bd645f0a3045a8a740e60dd770734a9)
1c6d43980SLemover /***************************************************************************************
2c6d43980SLemover * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3*f320e0f0SYinan Xu * Copyright (c) 2020-2021 Peng Cheng Laboratory
4c6d43980SLemover *
5c6d43980SLemover * XiangShan is licensed under Mulan PSL v2.
6c6d43980SLemover * You can use this software according to the terms and conditions of the Mulan PSL v2.
7c6d43980SLemover * You may obtain a copy of Mulan PSL v2 at:
8c6d43980SLemover *          http://license.coscl.org.cn/MulanPSL2
9c6d43980SLemover *
10c6d43980SLemover * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11c6d43980SLemover * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12c6d43980SLemover * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13c6d43980SLemover *
14c6d43980SLemover * See the Mulan PSL v2 for more details.
15c6d43980SLemover ***************************************************************************************/
16c6d43980SLemover 
17b543b09fSZihao Yu #include <stdio.h>
18b543b09fSZihao Yu #include <assert.h>
19b543b09fSZihao Yu #include <stdint.h>
20b543b09fSZihao Yu 
main(int argc,char * argv[])21b543b09fSZihao Yu int main(int argc, char *argv[]) {
22b543b09fSZihao Yu   assert(argc == 3);
23b543b09fSZihao Yu 
24b543b09fSZihao Yu   FILE *in = fopen(argv[1], "rb");
25b543b09fSZihao Yu   assert(in != NULL);
26b543b09fSZihao Yu 
27b543b09fSZihao Yu   FILE *out = fopen(argv[2], "w");
28b543b09fSZihao Yu   assert(out != NULL);
29b543b09fSZihao Yu 
30b543b09fSZihao Yu   char line[128];
31b543b09fSZihao Yu   uint32_t addr;
32b543b09fSZihao Yu   union {
33b543b09fSZihao Yu     uint8_t _8[4];
34b543b09fSZihao Yu     uint32_t _32;
35b543b09fSZihao Yu   } data[4];
36b543b09fSZihao Yu   while (fgets(line, 128, in) != NULL) {
37b543b09fSZihao Yu     if (line[0] == '@') {
38b543b09fSZihao Yu       sscanf(line + 1, "%x", &addr);
39b543b09fSZihao Yu       assert(addr % 4 == 0);
40b543b09fSZihao Yu       fprintf(out, "@%08x\n", addr / 4);
41b543b09fSZihao Yu     }
42b543b09fSZihao Yu     else {
43b543b09fSZihao Yu       int ret = sscanf(line,
44b543b09fSZihao Yu           "%hhx%hhx%hhx%hhx"
45b543b09fSZihao Yu           "%hhx%hhx%hhx%hhx"
46b543b09fSZihao Yu           "%hhx%hhx%hhx%hhx"
47b543b09fSZihao Yu           "%hhx%hhx%hhx%hhx",
48b543b09fSZihao Yu           &data[0]._8[0], &data[0]._8[1], &data[0]._8[2], &data[0]._8[3],
49b543b09fSZihao Yu           &data[1]._8[0], &data[1]._8[1], &data[1]._8[2], &data[1]._8[3],
50b543b09fSZihao Yu           &data[2]._8[0], &data[2]._8[1], &data[2]._8[2], &data[2]._8[3],
51b543b09fSZihao Yu           &data[3]._8[0], &data[3]._8[1], &data[3]._8[2], &data[3]._8[3]);
52b543b09fSZihao Yu 
53b543b09fSZihao Yu       assert(ret == EOF || ret == 4 || ret == 8 || ret == 12 || ret == 16);
54b543b09fSZihao Yu 
55b543b09fSZihao Yu       if (ret == EOF) continue;
56b543b09fSZihao Yu 
57b543b09fSZihao Yu       if (ret >=  4) fprintf(out, "%08x ", data[0]._32);
58b543b09fSZihao Yu       if (ret >=  8) fprintf(out, "%08x ", data[1]._32);
59b543b09fSZihao Yu       if (ret >= 12) fprintf(out, "%08x ", data[2]._32);
60b543b09fSZihao Yu       if (ret >= 16) fprintf(out, "%08x ", data[3]._32);
61b543b09fSZihao Yu       fprintf(out, "\n");
62b543b09fSZihao Yu     }
63b543b09fSZihao Yu   }
64b543b09fSZihao Yu 
65b543b09fSZihao Yu   fclose(in);
66b543b09fSZihao Yu   fclose(out);
67b543b09fSZihao Yu 
68b543b09fSZihao Yu   return 0;
69b543b09fSZihao Yu }
70