| 1 | /*------------------------------------------------------------\ |
|---|
| 2 | | | |
|---|
| 3 | | Tool : systemcass | |
|---|
| 4 | | | |
|---|
| 5 | | File : sc_numrep.h | |
|---|
| 6 | | | |
|---|
| 7 | | Author : Buchmann Richard | |
|---|
| 8 | | | |
|---|
| 9 | | Date : 04_05_2005 | |
|---|
| 10 | | | |
|---|
| 11 | \------------------------------------------------------------*/ |
|---|
| 12 | #ifndef __SC_NUMREP_H__ |
|---|
| 13 | #define __SC_NUMREP_H__ |
|---|
| 14 | |
|---|
| 15 | #include <cstdio> |
|---|
| 16 | #include <iostream> |
|---|
| 17 | #include "sc_string.h" |
|---|
| 18 | |
|---|
| 19 | namespace sc_dt { |
|---|
| 20 | |
|---|
| 21 | typedef enum { |
|---|
| 22 | SC_NOBASE = 0, |
|---|
| 23 | SC_BIN = 2, // binary |
|---|
| 24 | SC_OCT = 8, // octal, two's complement |
|---|
| 25 | SC_DEC = 10, // decimal |
|---|
| 26 | SC_HEX = 16, // hexadecimal, two's complement |
|---|
| 27 | SC_BIN_US , // binary, unsigned |
|---|
| 28 | SC_BIN_SM , // binary, sign magnitude |
|---|
| 29 | SC_OCT_US , |
|---|
| 30 | SC_OCT_SM , |
|---|
| 31 | SC_HEX_US , |
|---|
| 32 | SC_HEX_SM , |
|---|
| 33 | SC_CSD // canonical signed digit |
|---|
| 34 | } sc_numrep; |
|---|
| 35 | |
|---|
| 36 | inline |
|---|
| 37 | const unsigned int bitstring_to_uint (const char * val) |
|---|
| 38 | { |
|---|
| 39 | unsigned int i, res = 0; |
|---|
| 40 | for (i = 0; val[i] != '\0'; ++i) |
|---|
| 41 | { |
|---|
| 42 | res <<= 1; |
|---|
| 43 | res |= (val[i] == '1')?1:0; |
|---|
| 44 | } |
|---|
| 45 | return res; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | template <typename T> |
|---|
| 49 | const sc_string to_string (T val, size_t nbits, sc_numrep n) |
|---|
| 50 | { |
|---|
| 51 | sc_string s(nbits + 3,'0'); |
|---|
| 52 | switch (n) { |
|---|
| 53 | case SC_HEX : { |
|---|
| 54 | char t[64]; |
|---|
| 55 | char format[64]; |
|---|
| 56 | sprintf (format, "0x0%%0%dx", nbits/4); |
|---|
| 57 | sprintf (t, format, val); |
|---|
| 58 | s = t; |
|---|
| 59 | break; |
|---|
| 60 | } |
|---|
| 61 | case SC_BIN : { |
|---|
| 62 | int i; |
|---|
| 63 | s[1] = 'b'; |
|---|
| 64 | for (i = nbits - 1; i >= 0; --i) |
|---|
| 65 | { |
|---|
| 66 | s[i + 3] = (val & 1)?'1':'0'; |
|---|
| 67 | val >>= 1; |
|---|
| 68 | } |
|---|
| 69 | break; |
|---|
| 70 | } |
|---|
| 71 | default: |
|---|
| 72 | { std::cerr << "error\n"; return s; } |
|---|
| 73 | } |
|---|
| 74 | #if 0 |
|---|
| 75 | std::cerr << "\"" << s << "\""; |
|---|
| 76 | #endif |
|---|
| 77 | return s; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | const std::string to_string (sc_numrep); |
|---|
| 81 | |
|---|
| 82 | } // end of sc_dt namespace |
|---|
| 83 | |
|---|
| 84 | using sc_dt::sc_numrep; |
|---|
| 85 | using sc_dt::SC_BIN; |
|---|
| 86 | using sc_dt::SC_HEX; |
|---|
| 87 | using sc_dt::SC_OCT; |
|---|
| 88 | using sc_dt::SC_DEC; |
|---|
| 89 | using sc_dt::SC_HEX; |
|---|
| 90 | using sc_dt::SC_BIN_US; |
|---|
| 91 | using sc_dt::SC_BIN_SM; |
|---|
| 92 | using sc_dt::SC_OCT_US; |
|---|
| 93 | using sc_dt::SC_OCT_SM; |
|---|
| 94 | using sc_dt::SC_HEX_US; |
|---|
| 95 | using sc_dt::SC_HEX_SM; |
|---|
| 96 | using sc_dt::SC_CSD; |
|---|
| 97 | |
|---|
| 98 | #endif |
|---|
| 99 | |
|---|