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