split-readmemh.c 2.4 KB

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