]>
Commit | Line | Data |
---|---|---|
1eee94d3 GM |
1 | (* BitByteOps.mod provides a Logitech-3.0 compatible library. |
2 | ||
83ffe9cd | 3 | Copyright (C) 2007-2023 Free Software Foundation, Inc. |
1eee94d3 GM |
4 | Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>. |
5 | ||
6 | This file is part of GNU Modula-2. | |
7 | ||
8 | GNU Modula-2 is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU Modula-2 is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | Under Section 7 of GPL version 3, you are granted additional | |
19 | permissions described in the GCC Runtime Library Exception, version | |
20 | 3.1, as published by the Free Software Foundation. | |
21 | ||
22 | You should have received a copy of the GNU General Public License and | |
23 | a copy of the GCC Runtime Library Exception along with this program; | |
24 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
25 | <http://www.gnu.org/licenses/>. *) | |
26 | ||
27 | IMPLEMENTATION MODULE BitByteOps ; | |
28 | ||
01cca857 | 29 | FROM SYSTEM IMPORT ADR, SHIFT, ROTATE, TSIZE, BITSET8, CARDINAL8 ; |
1eee94d3 GM |
30 | |
31 | ||
32 | (* | |
33 | GetBits - returns the bits firstBit..lastBit from source. | |
34 | Bit 0 of byte maps onto the firstBit of source. | |
35 | *) | |
36 | ||
37 | PROCEDURE GetBits (source: BYTE; firstBit, lastBit: CARDINAL) : BYTE ; | |
38 | VAR | |
39 | si : CARDINAL8 ; | |
40 | sb : BITSET8 ; | |
41 | mask: BITSET8 ; | |
42 | i : CARDINAL ; | |
43 | BEGIN | |
44 | sb := VAL (BITSET8, source) ; | |
45 | mask := BITSET8 {} ; | |
46 | FOR i := firstBit TO lastBit DO | |
47 | INCL (mask, i) | |
48 | END ; | |
49 | sb := VAL (BITSET8, source) * mask ; | |
50 | i := 1 ; | |
51 | WHILE firstBit > 0 DO | |
52 | DEC (firstBit) ; | |
53 | i := i*2 | |
54 | END ; | |
55 | si := VAL (CARDINAL8, sb) ; | |
56 | RETURN VAL (BYTE, si DIV VAL (CARDINAL8, i)) | |
57 | END GetBits ; | |
58 | ||
59 | ||
60 | (* | |
61 | SetBits - sets bits in, byte, starting at, firstBit, and ending at, | |
62 | lastBit, with, pattern. The bit zero of, pattern, will | |
63 | be placed into, byte, at position, firstBit. | |
64 | *) | |
65 | ||
66 | PROCEDURE SetBits (VAR byte: BYTE; firstBit, lastBit: CARDINAL; | |
67 | pattern: BYTE) ; | |
68 | VAR | |
69 | pb, pp: BITSET8 ; | |
70 | i, j : CARDINAL ; | |
71 | BEGIN | |
72 | pb := VAL (BITSET8, byte) ; | |
73 | pp := VAL (BITSET8, pattern) ; | |
74 | j := 0 ; | |
75 | FOR i := firstBit TO lastBit DO | |
76 | IF j IN pp | |
77 | THEN | |
78 | INCL (pb, i) | |
79 | ELSE | |
80 | EXCL (pb, i) | |
81 | END ; | |
82 | INC (j) | |
83 | END ; | |
84 | byte := VAL (BYTE, pb) | |
85 | END SetBits ; | |
86 | ||
87 | ||
88 | (* | |
89 | ByteAnd - returns a bitwise (left AND right) | |
90 | *) | |
91 | ||
92 | PROCEDURE ByteAnd (left, right: BYTE) : BYTE ; | |
93 | BEGIN | |
94 | RETURN VAL (BYTE, VAL (BITSET8, left) * VAL (BITSET8, right)) | |
95 | END ByteAnd ; | |
96 | ||
97 | ||
98 | (* | |
99 | ByteOr - returns a bitwise (left OR right) | |
100 | *) | |
101 | ||
102 | PROCEDURE ByteOr (left, right: BYTE) : BYTE ; | |
103 | BEGIN | |
104 | RETURN VAL (BYTE, VAL (BITSET8, left) + VAL (BITSET8, right)) | |
105 | END ByteOr ; | |
106 | ||
107 | ||
108 | (* | |
109 | ByteXor - returns a bitwise (left XOR right) | |
110 | *) | |
111 | ||
112 | PROCEDURE ByteXor (left, right: BYTE) : BYTE ; | |
113 | BEGIN | |
114 | RETURN VAL (BYTE, VAL (BITSET8, left) DIV VAL (BITSET8, right)) | |
115 | END ByteXor ; | |
116 | ||
117 | ||
118 | (* | |
119 | ByteNot - returns a byte with all bits inverted. | |
120 | *) | |
121 | ||
122 | PROCEDURE ByteNot (byte: BYTE) : BYTE ; | |
123 | BEGIN | |
124 | RETURN VAL (BYTE, -VAL (BITSET8, byte)) | |
125 | END ByteNot ; | |
126 | ||
127 | ||
128 | (* | |
129 | ByteShr - returns a, byte, which has been shifted, count | |
130 | bits to the right. | |
131 | *) | |
132 | ||
133 | PROCEDURE ByteShr (byte: BYTE; count: CARDINAL) : BYTE ; | |
134 | BEGIN | |
135 | RETURN VAL (BYTE, SHIFT (VAL (BITSET8, byte), count)) | |
136 | END ByteShr ; | |
137 | ||
138 | ||
139 | (* | |
140 | ByteShl - returns a, byte, which has been shifted, count | |
141 | bits to the left. | |
142 | *) | |
143 | ||
144 | PROCEDURE ByteShl (byte: BYTE; count: CARDINAL) : BYTE ; | |
145 | BEGIN | |
146 | RETURN VAL (BYTE, SHIFT (VAL (BITSET8, byte), -VAL (INTEGER, count))) | |
147 | END ByteShl ; | |
148 | ||
149 | ||
150 | (* | |
151 | ByteSar - shift byte arthemetic right. Preserves the top | |
152 | end bit as the value is shifted right. | |
153 | *) | |
154 | ||
155 | PROCEDURE ByteSar (byte: BYTE; count: CARDINAL) : BYTE ; | |
156 | VAR | |
157 | b: BYTE ; | |
158 | BEGIN | |
159 | IF MAX(BITSET8) IN VAL(BITSET8, byte) | |
160 | THEN | |
161 | b := VAL (BYTE, SHIFT (VAL (BITSET8, byte), count) + BITSET8 {MAX (BITSET8)}) ; | |
162 | RETURN b | |
163 | ELSE | |
164 | RETURN VAL (BYTE, SHIFT (VAL (BITSET8, byte), count)) | |
165 | END | |
166 | END ByteSar ; | |
167 | ||
168 | ||
169 | (* | |
170 | ByteRor - returns a, byte, which has been rotated, count | |
171 | bits to the right. | |
172 | *) | |
173 | ||
174 | PROCEDURE ByteRor (byte: BYTE; count: CARDINAL) : BYTE ; | |
175 | BEGIN | |
176 | RETURN VAL (BYTE, ROTATE (VAL (BITSET8, byte), count)) | |
177 | END ByteRor ; | |
178 | ||
179 | ||
180 | (* | |
181 | ByteRol - returns a, byte, which has been rotated, count | |
182 | bits to the left. | |
183 | *) | |
184 | ||
185 | PROCEDURE ByteRol (byte: BYTE; count: CARDINAL) : BYTE ; | |
186 | BEGIN | |
187 | RETURN VAL (BYTE, ROTATE (VAL (BITSET8, byte), -VAL (INTEGER, count))) | |
188 | END ByteRol ; | |
189 | ||
190 | ||
191 | (* | |
192 | HighHibble - returns the top nibble only from, byte, | |
193 | in the lowest nibble position. | |
194 | *) | |
195 | ||
196 | PROCEDURE HighNibble (byte: BYTE) : BYTE ; | |
197 | BEGIN | |
198 | RETURN VAL (BYTE, VAL (CARDINAL8, byte) DIV 16) | |
199 | END HighNibble ; | |
200 | ||
201 | ||
202 | (* | |
203 | LowNibble - returns the low nibble only from, byte. | |
204 | The top nibble is replaced by zeros. | |
205 | *) | |
206 | ||
207 | PROCEDURE LowNibble (byte: BYTE) : BYTE ; | |
208 | BEGIN | |
209 | RETURN VAL (BYTE, VAL (BITSET8, byte) * BITSET8 {0..3}) | |
210 | END LowNibble ; | |
211 | ||
212 | ||
213 | (* | |
214 | Swap - swaps the low and high nibbles in the, byte. | |
215 | *) | |
216 | ||
217 | PROCEDURE Swap (byte: BYTE) : BYTE ; | |
218 | BEGIN | |
219 | RETURN VAL(BYTE, | |
220 | VAL(BITSET8, VAL (CARDINAL8, | |
221 | VAL (BITSET8, byte) * | |
222 | BITSET8 {4..7}) DIV 16) + | |
223 | VAL(BITSET8, byte) * BITSET8 {0..3}) | |
224 | END Swap ; | |
225 | ||
226 | ||
227 | END BitByteOps. |