xref: /netbsd/external/gpl3/gdb.old/dist/sim/rx/misc.c (revision 184b2d41)
16ca2c52aSchristos /* misc.c --- miscellaneous utility functions for RX simulator.
26ca2c52aSchristos 
3*184b2d41Schristos Copyright (C) 2005-2020 Free Software Foundation, Inc.
46ca2c52aSchristos Contributed by Red Hat, Inc.
56ca2c52aSchristos 
66ca2c52aSchristos This file is part of the GNU simulators.
76ca2c52aSchristos 
86ca2c52aSchristos This program is free software; you can redistribute it and/or modify
96ca2c52aSchristos it under the terms of the GNU General Public License as published by
106ca2c52aSchristos the Free Software Foundation; either version 3 of the License, or
116ca2c52aSchristos (at your option) any later version.
126ca2c52aSchristos 
136ca2c52aSchristos This program is distributed in the hope that it will be useful,
146ca2c52aSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
156ca2c52aSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
166ca2c52aSchristos GNU General Public License for more details.
176ca2c52aSchristos 
186ca2c52aSchristos You should have received a copy of the GNU General Public License
196ca2c52aSchristos along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
206ca2c52aSchristos 
216ca2c52aSchristos 
226ca2c52aSchristos #include "config.h"
236ca2c52aSchristos #include <stdio.h>
246ca2c52aSchristos 
256ca2c52aSchristos #include "cpu.h"
266ca2c52aSchristos #include "misc.h"
276ca2c52aSchristos 
286ca2c52aSchristos int
bcd2int(int bcd,int w)296ca2c52aSchristos bcd2int (int bcd, int w)
306ca2c52aSchristos {
316ca2c52aSchristos   int v = 0, m = 1, i;
326ca2c52aSchristos   for (i = 0; i < (w ? 4 : 2); i++)
336ca2c52aSchristos     {
346ca2c52aSchristos       v += (bcd % 16) * m;
356ca2c52aSchristos       m *= 10;
366ca2c52aSchristos       bcd /= 16;
376ca2c52aSchristos     }
386ca2c52aSchristos   return v;
396ca2c52aSchristos }
406ca2c52aSchristos 
416ca2c52aSchristos int
int2bcd(int v,int w)426ca2c52aSchristos int2bcd (int v, int w)
436ca2c52aSchristos {
446ca2c52aSchristos   int bcd = 0, m = 1, i;
456ca2c52aSchristos   for (i = 0; i < (w ? 4 : 2); i++)
466ca2c52aSchristos     {
476ca2c52aSchristos       bcd += (v % 10) * m;
486ca2c52aSchristos       m *= 16;
496ca2c52aSchristos       v /= 10;
506ca2c52aSchristos     }
516ca2c52aSchristos   return bcd;
526ca2c52aSchristos }
536ca2c52aSchristos 
546ca2c52aSchristos char *
comma(unsigned int u)556ca2c52aSchristos comma (unsigned int u)
566ca2c52aSchristos {
576ca2c52aSchristos   static char buf[5][20];
586ca2c52aSchristos   static int bi = 0;
596ca2c52aSchristos   int comma = 0;
606ca2c52aSchristos   char *bp;
616ca2c52aSchristos 
626ca2c52aSchristos   bi = (bi + 1) % 5;
636ca2c52aSchristos   bp = buf[bi] + 19;
646ca2c52aSchristos   *--bp = 0;
656ca2c52aSchristos   do
666ca2c52aSchristos     {
676ca2c52aSchristos       if (comma == 3)
686ca2c52aSchristos 	{
696ca2c52aSchristos 	  *--bp = ',';
706ca2c52aSchristos 	  comma = 0;
716ca2c52aSchristos 	}
726ca2c52aSchristos       comma++;
736ca2c52aSchristos       *--bp = '0' + (u % 10);
746ca2c52aSchristos       u /= 10;
756ca2c52aSchristos     }
766ca2c52aSchristos   while (u);
776ca2c52aSchristos   return bp;
786ca2c52aSchristos }
79