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

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

Removed form-feed comments.

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