xref: /netbsd-src/external/gpl3/gdb/dist/sim/rx/misc.c (revision 1f4e7eb9e5e045e008f1894823a8e4e6c9f46890)
14e98e3e1Schristos /* misc.c --- miscellaneous utility functions for RX simulator.
24e98e3e1Schristos 
3*1f4e7eb9Schristos Copyright (C) 2005-2024 Free Software Foundation, Inc.
44e98e3e1Schristos Contributed by Red Hat, Inc.
54e98e3e1Schristos 
64e98e3e1Schristos This file is part of the GNU simulators.
74e98e3e1Schristos 
84e98e3e1Schristos This program is free software; you can redistribute it and/or modify
94e98e3e1Schristos it under the terms of the GNU General Public License as published by
104e98e3e1Schristos the Free Software Foundation; either version 3 of the License, or
114e98e3e1Schristos (at your option) any later version.
124e98e3e1Schristos 
134e98e3e1Schristos This program is distributed in the hope that it will be useful,
144e98e3e1Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
154e98e3e1Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
164e98e3e1Schristos GNU General Public License for more details.
174e98e3e1Schristos 
184e98e3e1Schristos You should have received a copy of the GNU General Public License
194e98e3e1Schristos along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
204e98e3e1Schristos 
214b169a6bSchristos /* This must come before any other includes.  */
224b169a6bSchristos #include "defs.h"
234e98e3e1Schristos 
244e98e3e1Schristos #include <stdio.h>
254e98e3e1Schristos 
264e98e3e1Schristos #include "cpu.h"
274e98e3e1Schristos #include "misc.h"
284e98e3e1Schristos 
294e98e3e1Schristos int
304e98e3e1Schristos bcd2int (int bcd, int w)
314e98e3e1Schristos {
324e98e3e1Schristos   int v = 0, m = 1, i;
334e98e3e1Schristos   for (i = 0; i < (w ? 4 : 2); i++)
344e98e3e1Schristos     {
354e98e3e1Schristos       v += (bcd % 16) * m;
364e98e3e1Schristos       m *= 10;
374e98e3e1Schristos       bcd /= 16;
384e98e3e1Schristos     }
394e98e3e1Schristos   return v;
404e98e3e1Schristos }
414e98e3e1Schristos 
424e98e3e1Schristos int
434e98e3e1Schristos int2bcd (int v, int w)
444e98e3e1Schristos {
454e98e3e1Schristos   int bcd = 0, m = 1, i;
464e98e3e1Schristos   for (i = 0; i < (w ? 4 : 2); i++)
474e98e3e1Schristos     {
484e98e3e1Schristos       bcd += (v % 10) * m;
494e98e3e1Schristos       m *= 16;
504e98e3e1Schristos       v /= 10;
514e98e3e1Schristos     }
524e98e3e1Schristos   return bcd;
534e98e3e1Schristos }
544e98e3e1Schristos 
554e98e3e1Schristos char *
564e98e3e1Schristos comma (unsigned int u)
574e98e3e1Schristos {
584e98e3e1Schristos   static char buf[5][20];
594e98e3e1Schristos   static int bi = 0;
604e98e3e1Schristos   int comma = 0;
614e98e3e1Schristos   char *bp;
624e98e3e1Schristos 
634e98e3e1Schristos   bi = (bi + 1) % 5;
644e98e3e1Schristos   bp = buf[bi] + 19;
654e98e3e1Schristos   *--bp = 0;
664e98e3e1Schristos   do
674e98e3e1Schristos     {
684e98e3e1Schristos       if (comma == 3)
694e98e3e1Schristos 	{
704e98e3e1Schristos 	  *--bp = ',';
714e98e3e1Schristos 	  comma = 0;
724e98e3e1Schristos 	}
734e98e3e1Schristos       comma++;
744e98e3e1Schristos       *--bp = '0' + (u % 10);
754e98e3e1Schristos       u /= 10;
764e98e3e1Schristos     }
774e98e3e1Schristos   while (u);
784e98e3e1Schristos   return bp;
794e98e3e1Schristos }
80