stringify.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* -*- c-file-style: "linux";indent-tabs-mode:t -*- */
  2. /* Copyright (C) 2016 Jeremiah Orians
  3. * Copyright (C) 2017 Jan Nieuwenhuizen <janneke@gnu.org>
  4. * This file is part of mescc-tools.
  5. *
  6. * mescc-tools is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * mescc-tools is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with mescc-tools. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. // CONSTANT HEX 16
  21. #define HEX 16
  22. // CONSTANT OCTAL 8
  23. #define OCTAL 8
  24. // CONSTANT BINARY 2
  25. #define BINARY 2
  26. /***********************************************************
  27. * Needed for current implementation of little endian *
  28. * Can be used to support little bit endian instruction *
  29. * sets if we ever find one that might be useful *
  30. * But I seriously doubt it *
  31. ***********************************************************/
  32. void reverseBitOrder(char* c, int ByteMode)
  33. {
  34. if(NULL == c) return;
  35. if(0 == c[1]) return;
  36. int hold = c[0];
  37. if(HEX == ByteMode)
  38. {
  39. c[0] = c[1];
  40. c[1] = hold;
  41. reverseBitOrder(c+2, ByteMode);
  42. }
  43. else if(OCTAL == ByteMode)
  44. {
  45. c[0] = c[2];
  46. c[2] = hold;
  47. reverseBitOrder(c+3, ByteMode);
  48. }
  49. else if(BINARY == ByteMode)
  50. {
  51. c[0] = c[7];
  52. c[7] = hold;
  53. hold = c[1];
  54. c[1] = c[6];
  55. c[6] = hold;
  56. hold = c[2];
  57. c[2] = c[5];
  58. c[5] = hold;
  59. hold = c[3];
  60. c[3] = c[4];
  61. c[4] = hold;
  62. reverseBitOrder(c+8, ByteMode);
  63. }
  64. }
  65. void LittleEndian(char* start, int ByteMode)
  66. {
  67. char* end = start;
  68. char* c = start;
  69. while(0 != end[0]) end = end + 1;
  70. int hold;
  71. for(end = end - 1; start < end; start = start + 1)
  72. {
  73. hold = start[0];
  74. start[0] = end[0];
  75. end[0] = hold;
  76. end = end - 1;
  77. }
  78. /* The above makes a reversed bit order */
  79. reverseBitOrder(c, ByteMode);
  80. }
  81. int hex2char(int c)
  82. {
  83. if((c >= 0) && (c <= 9)) return (c + 48);
  84. else if((c >= 10) && (c <= 15)) return (c + 55);
  85. else return -1;
  86. }
  87. int stringify(char* s, int digits, int divisor, int value, int shift)
  88. {
  89. int i = value;
  90. if(digits > 1)
  91. {
  92. i = stringify(s+1, (digits - 1), divisor, value, shift);
  93. }
  94. s[0] = hex2char(i & (divisor - 1));
  95. return (i >> shift);
  96. }