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