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