misc.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* misc.c --- miscellaneous utility functions for RX simulator.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat, Inc.
  4. This file is part of the GNU simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include <stdio.h>
  18. #include "cpu.h"
  19. #include "misc.h"
  20. int
  21. bcd2int (int bcd, int w)
  22. {
  23. int v = 0, m = 1, i;
  24. for (i = 0; i < (w ? 4 : 2); i++)
  25. {
  26. v += (bcd % 16) * m;
  27. m *= 10;
  28. bcd /= 16;
  29. }
  30. return v;
  31. }
  32. int
  33. int2bcd (int v, int w)
  34. {
  35. int bcd = 0, m = 1, i;
  36. for (i = 0; i < (w ? 4 : 2); i++)
  37. {
  38. bcd += (v % 10) * m;
  39. m *= 16;
  40. v /= 10;
  41. }
  42. return bcd;
  43. }
  44. char *
  45. comma (unsigned int u)
  46. {
  47. static char buf[5][20];
  48. static int bi = 0;
  49. int comma = 0;
  50. char *bp;
  51. bi = (bi + 1) % 5;
  52. bp = buf[bi] + 19;
  53. *--bp = 0;
  54. do
  55. {
  56. if (comma == 3)
  57. {
  58. *--bp = ',';
  59. comma = 0;
  60. }
  61. comma++;
  62. *--bp = '0' + (u % 10);
  63. u /= 10;
  64. }
  65. while (u);
  66. return bp;
  67. }