1 /*
2 * libzvbi -- BCD arithmetic for Teletext page numbers
3 *
4 * Copyright (C) 2001, 2002 Michael H. Schimek
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA.
20 */
21
22 /* $Id: bcd.h,v 1.19 2008-02-21 07:18:52 mschimek Exp $ */
23
24 #ifndef BCD_H
25 #define BCD_H
26
27 #include "misc.h"
28
29 /**
30 * @addtogroup BCD BCD arithmetic for Teletext page numbers
31 * @ingroup HiDec
32 *
33 * Teletext page numbers are expressed as packed binary coded decimal
34 * numbers in range 0x100 to 0x8FF. The bcd format encodes one decimal
35 * digit in every hex nibble (four bits) of the number. Page numbers
36 * containing digits 0xA to 0xF are reserved for various system purposes
37 * and not intended for display.
38 */
39
40 /* Public */
41
42 /**
43 * @ingroup HiDec
44 *
45 * Teletext or Closed Caption page number. For Teletext pages
46 * this is a packed bcd number in range 0x100 ... 0x8FF. Page
47 * numbers containing digits 0xA to 0xF are reserved for various
48 * system purposes, these pages are not intended for display.
49 *
50 * Closed Caption page numbers between 1 ... 8 correspond
51 * to the four Caption and Text channels:
52 * <table>
53 * <tr><td>1</td><td>Caption 1</td><td>
54 * "Primary synchronous caption service [English]"</td></tr>
55 * <tr><td>2</td><td>Caption 2</td><td>
56 * "Special non-synchronous data that is intended to
57 * augment information carried in the program"</td></tr>
58 * <tr><td>3</td><td>Caption 3</td><td>
59 * "Secondary synchronous caption service, usually
60 * second language [Spanish, French]"</td></tr>
61 * <tr><td>4</td><td>Caption 4</td><td>
62 * "Special non-synchronous data similar to Caption 2"</td></tr>
63 * <tr><td>5</td><td>Text 1</td><td>
64 * "First text service, data usually not program related"</td></tr>
65 * <tr><td>6</td><td>Text 2</td><td>
66 * "Second text service, additional data usually not program related
67 * [ITV data]"</td></tr>
68 * <tr><td>7</td><td>Text 3</td><td>
69 * "Additional text channel"</td></tr>
70 * <tr><td>8</td><td>Text 4</td><td>
71 * "Additional text channel"</td></tr>
72 * </table>
73 */
74 /* XXX unsigned? */
75 typedef int vbi_pgno;
76
77 /**
78 * @ingroup HiDec
79 *
80 * This is the subpage number only applicable to Teletext pages,
81 * a packed bcd number in range 0x00 ... 0x99. On special 'clock' pages
82 * (for example listing the current time in different time zones)
83 * it can assume values between 0x0000 ... 0x2359 expressing
84 * local time. These are not actually subpages.
85 */
86 typedef int vbi_subno;
87
88 /**
89 * @ingroup HiDec
90 */
91 #define VBI_ANY_SUBNO 0x3F7F
92 /**
93 * @ingroup HiDec
94 */
95 #define VBI_NO_SUBNO 0x3F7F
96
97 /**
98 * @ingroup BCD
99 * @param dec Decimal number.
100 *
101 * Converts a two's complement binary between 0 ... 999 to a
102 * packed bcd number in range 0x000 ... 0x999. Extra digits in
103 * the input will be discarded.
104 *
105 * @return
106 * BCD number.
107 */
108 _vbi_inline unsigned int
vbi_dec2bcd(unsigned int dec)109 vbi_dec2bcd(unsigned int dec)
110 {
111 return (dec % 10) + ((dec / 10) % 10) * 16 + ((dec / 100) % 10) * 256;
112 }
113
114 /**
115 * @ingroup BCD
116 * @since 0.2.28
117 */
118 #define vbi_bin2bcd(n) vbi_dec2bcd(n)
119
120 /**
121 * @ingroup BCD
122 * @param bcd BCD number.
123 *
124 * Converts a packed bcd number between 0x000 ... 0xFFF to a two's
125 * complement binary in range 0 ... 999. Extra digits in the input
126 * will be discarded.
127 *
128 * @return
129 * Decimal number. The result is undefined when the bcd number contains
130 * hex digits 0xA ... 0xF.
131 **/
132 _vbi_inline unsigned int
vbi_bcd2dec(unsigned int bcd)133 vbi_bcd2dec(unsigned int bcd)
134 {
135 return (bcd & 15) + ((bcd >> 4) & 15) * 10 + ((bcd >> 8) & 15) * 100;
136 }
137
138 /**
139 * @ingroup BCD
140 * @since 0.2.28
141 */
142 #define vbi_bcd2bin(n) vbi_bcd2dec(n)
143
144 /**
145 * @ingroup BCD
146 * @param a BCD number.
147 * @param b BCD number.
148 *
149 * Adds two packed bcd numbers, returning a packed bcd sum. Arguments
150 * and result are in range 0xF000 0000 ... 0x0999 9999, that
151 * is -10**7 ... +10**7 - 1 in decimal notation. To subtract you can
152 * add the 10's complement, e. g. -1 = 0xF999 9999.
153 *
154 * @return
155 * Packed bcd number. The result is undefined when any of the arguments
156 * contain hex digits 0xA ... 0xF.
157 */
158 _vbi_inline unsigned int
vbi_add_bcd(unsigned int a,unsigned int b)159 vbi_add_bcd(unsigned int a, unsigned int b)
160 {
161 unsigned int t;
162
163 a += 0x06666666;
164 t = a + b;
165 b ^= a ^ t;
166 b = (~b & 0x11111110) >> 3;
167 b |= b * 2;
168
169 return t - b;
170 }
171
172 /**
173 * @ingroup BCD
174 * @param bcd BCD number.
175 *
176 * Tests if @a bcd forms a valid BCD number. The argument must be
177 * in range 0x0000 0000 ... 0x0999 9999.
178 *
179 * @return
180 * @c FALSE if @a bcd contains hex digits 0xA ... 0xF.
181 */
182 _vbi_inline vbi_bool
vbi_is_bcd(unsigned int bcd)183 vbi_is_bcd(unsigned int bcd)
184 {
185 static const unsigned int x = 0x06666666;
186
187 return (((bcd + x) ^ (bcd ^ x)) & 0x11111110) == 0;
188 }
189
190 /**
191 * @ingroup BCD
192 * @param bcd Unsigned BCD number.
193 * @param maximum Unsigned maximum value.
194 *
195 * Compares an unsigned packed bcd number digit-wise against a maximum
196 * value, for example 0x295959. @a maximum can contain digits 0x0
197 * ... 0xF.
198 *
199 * @return
200 * @c TRUE if any digit of @a bcd is greater than the
201 * corresponding digit of @a maximum.
202 *
203 * @since 0.2.28
204 */
205 _vbi_inline vbi_bool
vbi_bcd_digits_greater(unsigned int bcd,unsigned int maximum)206 vbi_bcd_digits_greater (unsigned int bcd,
207 unsigned int maximum)
208 {
209 maximum ^= ~0;
210
211 return 0 != (((bcd + maximum) ^ bcd ^ maximum) & 0x11111110);
212 }
213
214 /* Private */
215
216 #endif /* BCD_H */
217
218 /*
219 Local variables:
220 c-set-style: K&R
221 c-basic-offset: 8
222 End:
223 */
224