source: buchla-68k/ram/dec2fr.c@ e225e77

Last change on this file since e225e77 was e225e77, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Added missing includes and declarations.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 =============================================================================
3 dec2fr.c -- Buchla 700 fractional 2's complement conversion functions
4 Version 10 -- 1987-12-06 -- D.N. Lynx Crowe
5 =============================================================================
6*/
7
8#define TESTER 0
9
10#include "stddefs.h"
11#include "stdio.h"
12
13#define SFACT 1000L /* scale factor */
14
15static int32_t d2f[][10] = { /* decimal to fraction table */
16
17 {0x00000000L, 0x000019A0L, 0x00003336L, 0x00004CD0L, 0x00006668L,
18 0x00008000L, 0x000099A0L, 0x0000B336L, 0x0000CCCEL, 0x0000E668L},
19
20 {0x00000000L, 0x00000290L, 0x00000520L, 0x000007B0L, 0x00000A40L,
21 0x00000CD0L, 0x00000F60L, 0x000011F0L, 0x00001480L, 0x00001710L}
22};
23
24static int32_t f2d[] = { /* fraction to decimal table */
25
26 50000L, 25000L, 12500L, 6250L,
27 3125L, 1562L, 781L, 390L,
28 195L, 97L, 48L, 24L,
29 12L, 6L, 3L, 1L
30};
31
32/*
33
34*/
35
36/*
37 =============================================================================
38 dec2fr() -- convert an ASCII decimal string to fractional binary
39
40 The input string is 4 characters in the range 100- to 100+ inclusive.
41 The result is a 2's complement binary fractional value.
42 =============================================================================
43*/
44
45int16_t dec2fr(int8_t *s)
46{
47 register int16_t i;
48
49 if (s[0] EQ '1') {
50
51 if (s[1] EQ '0') {
52
53 if (s[2] EQ '0') {
54
55 if (s[3] EQ '-')
56 return(0x8000); /* -100 */
57 else
58 return(0x7FFF); /* +100 */
59
60 } else {
61
62 return(0xFFFF); /* ERROR */
63 }
64
65 } else {
66
67 return(0xFFFF); /* ERROR */
68
69 }
70
71/*
72
73*/
74
75 } else if (s[0] EQ '0') {
76
77 i = (d2f[0][s[1] - '0'] + d2f[1][s[2] - '0']) >> 1;
78
79 if (s[3] EQ '-') {
80
81 if (i)
82 return(~i); /* negative number */
83 else
84 return(0); /* zero is always 0 */
85
86 } else {
87
88 return(i); /* positive number */
89 }
90 }
91
92 return(0xFFFF); /* ERROR */
93}
94
95/*
96
97*/
98
99/*
100 =============================================================================
101 fr2dec() -- convert a 2's complement fraction to decimal ASCII notation
102
103 The input is a 2's complement binary fractional value.
104 The result string is 4 characters in the range 100- to 100+ inclusive.
105 =============================================================================
106*/
107
108int8_t *fr2dec(int16_t v, int8_t *s)
109{
110 register int32_t acc, sc;
111 register int16_t i, p;
112
113 if (v EQ 0x7FFF) { /* +100 */
114
115 sprintf(s, "100+");
116 return(s);
117 }
118
119 if (v EQ 0x8000) { /* -100 */
120
121 sprintf(s, "100-");
122 return(s);
123 }
124
125 if (v & 0x8000) {
126
127 v = ~v; /* negative number */
128 p = '-';
129
130 } else {
131
132 p = '+'; /* positive number */
133 }
134
135 acc = 0;
136
137 for (i = 0; i LT 15; i++)
138 if (v & (1 << (14 - i)))
139 acc += f2d[i];
140
141 sc = SFACT;
142 acc /= sc;
143 sprintf(s, "%03d%c", (int16_t)acc, p);
144 return(s);
145}
146
147/*
148
149*/
150
151#if TESTER
152
153#include "stdio.h"
154
155#include "stdio.h"
156
157extern int memcmp();
158
159char t[8], v[8];
160
161/*
162 =============================================================================
163 test program for fractional two's complement conversion functions
164 =============================================================================
165*/
166
167main()
168{
169 register short i;
170 register short u;
171
172 for (i = 0; i < 101; i++) {
173
174 if (i EQ 50)
175 printf("\f");
176
177 sprintf(t, "%03d+", i);
178 u = dec2fr(t);
179
180 if (u EQ 0xFFFF) {
181
182 printf("[%s] = ERROR = ?????? ", t);
183
184 } else {
185
186 fr2dec(u, v);
187 printf("[%s] = 0x%04x = [%s]", t, u, v);
188 }
189
190 printf(" %s ", memcmp(t, v, 4) ? "<--" : " ");
191
192 sprintf(t, "%03d-", i);
193 u = dec2fr(t);
194
195 if (u EQ 0xFFFF) {
196
197 printf(" [%s] = ERROR = ??????\n", t);
198
199 } else {
200
201 fr2dec(u, v);
202 printf(" [%s] = 0x%04x = [%s]", t, u, v);
203 }
204
205 printf(" %s\n", memcmp(t, v, 4) ? "<--" : "");
206
207 }
208}
209
210#endif
Note: See TracBrowser for help on using the repository browser.