| 1 | /* misc.c --- miscellaneous utility functions for M32C simulator. |
| 2 | |
| 3 | Copyright (C) 2005-2019 Free Software Foundation, Inc. |
| 4 | Contributed by Red Hat, Inc. |
| 5 | |
| 6 | This file is part of the GNU simulators. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along 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 | |
| 27 | int |
| 28 | bcd2int (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 | |
| 40 | int |
| 41 | int2bcd (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 | |
| 53 | char * |
| 54 | comma (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 | } |