comet2monitorの修正
[YACASL2.git] / src / word.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <ctype.h>
6
7 #include "word.h"
8 #include "cerr.h"
9
10 /**
11  * @brief 10進数値を表す文字列をWORD値に変換する
12  *
13  * @return WORD値
14  *
15  * @param *str 10進数値を表す文字列
16  */
17 WORD n2word(const char *str);
18
19 /**
20  * @brief 16進数の文字列をWORD値に変換する
21  *
22  * @return WORD値
23  *
24  * @param *str 16進数値を表す文字列
25  */
26 WORD h2word(const char *str);
27
28 /**
29  * @brief wordのエラー定義
30  */
31 static CERR cerr_word[] = {
32     { 114, "not integer" },
33     { 115, "not hex" },
34     { 116, "out of hex range" },
35 };
36
37 WORD n2word(const char *str)
38 {
39     assert(isdigit(*str) || *str == '-');
40
41     char *check;
42     int n;
43     /* WORD値に変換 */
44     n = strtol(str, &check, 10);
45     if(check[0]) {
46         setcerr(114, str);    /* not integer */
47         return 0x0;
48     }
49     /* nが-32768から32767の範囲にないときは、その下位16ビットを格納 */
50     if(n < -32768 || n > 32767) {
51         n &= 0xFFFF;
52     }
53     return (WORD)n;
54 }
55
56 WORD h2word(const char *str)
57 {
58     assert(*str == '#');
59
60     WORD w = 0x0;
61     char *check;
62     str++;
63     if(*str == '-' || strlen(str) > 4) {
64         setcerr(116, str-1);    /* out of hex range */
65         return 0x0;
66     }
67     /* WORD値に変換 */
68     w = (WORD)strtol(str, &check, 16);
69     if(check[0]) {
70         setcerr(115, str-1);    /* not hex */
71         return 0x0;
72     }
73     return w;
74 }
75
76 /* word.hで定義された関数群 */
77 void addcerrlist_word()
78 {
79     addcerrlist(ARRAYSIZE(cerr_word), cerr_word);
80 }
81
82 WORD nh2word(const char *str)
83 {
84     WORD w;
85
86     assert(sizeof(WORD) * 8 == 16); /* WORD型のサイズが16ビットであることを確認 */
87     if(str == NULL) {
88         return 0x0;
89     }
90     if(!isdigit(*str) && *str != '-' && *str != '#') {
91         setcerr(114, str);    /* not integer */
92         return 0x0;
93     }
94     if(*str == '#') {
95         w = h2word(str);
96     } else {
97         w = n2word(str);
98     }
99     return w;
100 }
101
102 char *word2n(WORD word)
103 {
104     enum {
105         MAXLEN = 5,        /* WORD値を10進数で表したときの最大桁数 */
106     };
107     char *p = malloc_chk(MAXLEN + 1, "word2n.p"), *digit = malloc_chk(MAXLEN + 1, "word2n.digit");
108     int i = 0, j;
109
110     do{
111         p[i++] = word % 10 + '0';
112     } while((word /= 10) > 0);
113     for(j = 0; j < i; j++) {
114         digit[j] = p[(i-1)-j];
115     }
116     digit[j] = '\0';
117     FREE(p);
118     return digit;
119 }
120
121 char *word2bit(const WORD word)
122 {
123     enum {
124         MAXLEN = 16,        /* WORD値を2進数で表したときの最大桁数 */
125     };
126     WORD mask = 0x8000;
127     char *bit = malloc_chk(MAXLEN + 1, "word2bit.bit");
128     int i = 0;
129
130     do {
131         bit[i++] = (word & mask) ? '1' : '0';
132     } while((mask >>= 1) > 0);
133     bit[i] = '\0';
134     return bit;
135 }
136
137 void print_dumpword(WORD word, bool logicalmode)
138 {
139     char *bit = word2bit(word);
140
141     if(logicalmode == true) {
142         fprintf(stdout, "%6d", word);
143     } else {
144         fprintf(stdout, "%6d", (signed short)word);
145     }
146     fprintf(stdout, " = #%04X = %s", word, bit);
147     /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */
148     if(word >= 0x20 && word <= 0x7E) {
149         fprintf(stdout, " = \'%c\'", word);
150     } else if(word == 0xA) {
151         fprintf(stdout, " = \'\\n\'");
152     } else if(word == '\t') {
153         fprintf(stdout, " = \'\\t\'");
154     }
155     FREE(bit);
156 }