デバッガー機能の実装
[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 = 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(!isdigit(*str) && *str != '-' && *str != '#') {
88         setcerr(114, str);    /* not integer */
89         return 0x0;
90     }
91     if(*str == '#') {
92         w = h2word(str);
93     } else {
94         w = n2word(str);
95     }
96     return w;
97 }
98
99 char *word2n(WORD word)
100 {
101     enum {
102         MAXLEN = 5,        /* WORD値を10進数で表したときの最大桁数 */
103     };
104     char *p = malloc_chk(MAXLEN + 1, "word2n.p"), *digit = malloc_chk(MAXLEN + 1, "word2n.digit");
105     int i = 0, j;
106
107     do{
108         *(p + i++) = word % 10 + '0';
109     } while((word /= 10) > 0);
110     for(j = 0; j < i; j++) {
111         *(digit + j) = *(p + (i - 1) - j);
112     }
113     *(digit + j + 1) = '\0';
114     FREE(p);
115     return digit;
116 }
117
118 char *word2bit(const WORD word)
119 {
120     enum {
121         MAXLEN = 16,        /* WORD値を2進数で表したときの最大桁数 */
122     };
123     WORD mask = 0x8000;
124     char *bit, *p;
125
126     bit = p = malloc_chk(MAXLEN + 1, "word2bit.bit");
127     do {
128         *p++ = (word & mask) ? '1' : '0';
129     } while((mask >>= 1) > 0);
130     *p = '\0';
131     return bit;
132 }
133
134 void print_dumpword(WORD word, bool logicalmode)
135 {
136     char *b;
137
138     if(logicalmode == true) {
139         fprintf(stdout, "%6d", word);
140     } else {
141         fprintf(stdout, "%6d", (signed short)word);
142     }
143     fprintf(stdout, " = #%04X = %s", word, (b = word2bit(word)));
144     /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */
145     if(word >= 0x20 && word <= 0x7E) {
146         fprintf(stdout, " = \'%c\'", word);
147     } else if(word == 0xA) {
148         fprintf(stdout, " = \'\\n\'");
149     } else if(word == '\t') {
150         fprintf(stdout, " = \'\\t\'");
151     }
152     FREE(b);
153 }