xref: /XiangShan/tools/readmemh/split-readmemh.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 #include <string.h>
21b543b09fSZihao Yu 
22b543b09fSZihao Yu char outname [4][4096];
23b543b09fSZihao Yu 
main(int argc,char * argv[])24b543b09fSZihao Yu int main(int argc, char *argv[]) {
25b543b09fSZihao Yu   assert(argc == 2);
26b543b09fSZihao Yu 
27b543b09fSZihao Yu   FILE *in = fopen(argv[1], "rb");
28b543b09fSZihao Yu   assert(in != NULL);
29b543b09fSZihao Yu 
30b543b09fSZihao Yu   strcat(stpcpy(outname[0], argv[1]), "_0");
31b543b09fSZihao Yu   strcat(stpcpy(outname[1], argv[1]), "_1");
32b543b09fSZihao Yu   strcat(stpcpy(outname[2], argv[1]), "_2");
33b543b09fSZihao Yu   strcat(stpcpy(outname[3], argv[1]), "_3");
34b543b09fSZihao Yu 
35b543b09fSZihao Yu   FILE *out[4];
36b543b09fSZihao Yu   out[0] = fopen(outname[0], "w");
37b543b09fSZihao Yu   out[1] = fopen(outname[1], "w");
38b543b09fSZihao Yu   out[2] = fopen(outname[2], "w");
39b543b09fSZihao Yu   out[3] = fopen(outname[3], "w");
40b543b09fSZihao Yu   assert(out[0] != NULL && out[1] != NULL && out[2] != NULL && out[3] != NULL);
41b543b09fSZihao Yu 
42b543b09fSZihao Yu   char line[128];
43b543b09fSZihao Yu   int idx = 0;
44b543b09fSZihao Yu   while (fgets(line, 128, in) != NULL) {
45b543b09fSZihao Yu     if (line[0] == '@') {
46b543b09fSZihao Yu       uint32_t addr;
47b543b09fSZihao Yu       sscanf(line + 1, "%x", &addr);
48b543b09fSZihao Yu       assert(addr % 4 == 0);
49b543b09fSZihao Yu       fprintf(out[0], "\n@%08x\n", addr / 4);
50b543b09fSZihao Yu       fprintf(out[1], "\n@%08x\n", addr / 4);
51b543b09fSZihao Yu       fprintf(out[2], "\n@%08x\n", addr / 4);
52b543b09fSZihao Yu       fprintf(out[3], "\n@%08x\n", addr / 4);
53b543b09fSZihao Yu       idx = 0;
54b543b09fSZihao Yu     }
55b543b09fSZihao Yu     else {
56b543b09fSZihao Yu       // remove white spaces at the end
57b543b09fSZihao Yu       char *p = line + strlen(line) - 1;
58b543b09fSZihao Yu       while (p >= line && (*p == ' ' || *p == '\n' || *p == '\r')) p --;
59b543b09fSZihao Yu       p[1] = '\0';
60b543b09fSZihao Yu 
61b543b09fSZihao Yu       p = line;
62b543b09fSZihao Yu       char *byte;
63b543b09fSZihao Yu       while ((byte = strsep(&p, " "))) {
64b543b09fSZihao Yu         fprintf(out[idx % 4], "%s ", byte);
65b543b09fSZihao Yu         idx ++;
66b543b09fSZihao Yu       }
67b543b09fSZihao Yu 
68b543b09fSZihao Yu       if ((idx >> 2) % 16 == 0) {
69b543b09fSZihao Yu         fprintf(out[0], "\n");
70b543b09fSZihao Yu         fprintf(out[1], "\n");
71b543b09fSZihao Yu         fprintf(out[2], "\n");
72b543b09fSZihao Yu         fprintf(out[3], "\n");
73b543b09fSZihao Yu       }
74b543b09fSZihao Yu     }
75b543b09fSZihao Yu   }
76b543b09fSZihao Yu 
77b543b09fSZihao Yu   fclose(in);
78b543b09fSZihao Yu   fclose(out[0]);
79b543b09fSZihao Yu   fclose(out[1]);
80b543b09fSZihao Yu   fclose(out[2]);
81b543b09fSZihao Yu   fclose(out[3]);
82b543b09fSZihao Yu 
83b543b09fSZihao Yu   return 0;
84b543b09fSZihao Yu }
85