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