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