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