| 1 |
(* BitBlockOps.mod provides a Logitech compatible module. |
| 2 |
|
| 3 |
Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 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 BitBlockOps ; |
| 28 |
|
| 29 |
|
| 30 |
FROM SYSTEM IMPORT BITSPERBYTE, SHIFT, BYTE, BITSET8, TSIZE ; |
| 31 |
FROM Builtins IMPORT memmove, memset ; |
| 32 |
|
| 33 |
TYPE |
| 34 |
ptrToByte = POINTER TO BITSET8 ; |
| 35 |
ptrToBitset = POINTER TO BITSET ; |
| 36 |
|
| 37 |
|
| 38 |
(* |
| 39 |
BlockAnd - performs a bitwise AND on blocks |
| 40 |
[dest..dest+size-1] := [dest..dest+size-1] AND |
| 41 |
[src..src+size-1] |
| 42 |
*) |
| 43 |
|
| 44 |
PROCEDURE BlockAnd (dest, src: ADDRESS; size: CARDINAL) ; |
| 45 |
VAR |
| 46 |
bitsetDest, bitsetSrc: ptrToBitset ; |
| 47 |
byteDest, byteSrc : ptrToByte ; |
| 48 |
BEGIN |
| 49 |
bitsetDest := dest ; |
| 50 |
bitsetSrc := src ; |
| 51 |
WHILE size > TSIZE (BITSET) DO |
| 52 |
bitsetDest^ := bitsetDest^ * bitsetSrc^ ; |
| 53 |
INC (bitsetDest, TSIZE (BITSET)) ; |
| 54 |
INC (bitsetSrc, TSIZE (BITSET)) ; |
| 55 |
DEC (size, TSIZE (BITSET)) |
| 56 |
END ; |
| 57 |
byteDest := VAL (ptrToByte, bitsetDest) ; |
| 58 |
byteSrc := VAL (ptrToByte, bitsetSrc) ; |
| 59 |
WHILE size > 0 DO |
| 60 |
byteDest^ := byteDest^ * byteSrc^ ; |
| 61 |
INC(byteDest) ; |
| 62 |
INC(byteSrc) ; |
| 63 |
DEC(size) |
| 64 |
END |
| 65 |
END BlockAnd ; |
| 66 |
|
| 67 |
|
| 68 |
(* |
| 69 |
BlockOr - performs a bitwise OR on blocks |
| 70 |
[dest..dest+size-1] := [dest..dest+size-1] OR |
| 71 |
[src..src+size-1] |
| 72 |
*) |
| 73 |
|
| 74 |
PROCEDURE BlockOr (dest, src: ADDRESS; size: CARDINAL) ; |
| 75 |
VAR |
| 76 |
bitsetDest, bitsetSrc: ptrToBitset ; |
| 77 |
byteDest, byteSrc : ptrToByte ; |
| 78 |
BEGIN |
| 79 |
bitsetDest := dest ; |
| 80 |
bitsetSrc := src ; |
| 81 |
WHILE size > TSIZE (BITSET) DO |
| 82 |
bitsetDest^ := bitsetDest^ + bitsetSrc^ ; |
| 83 |
INC (bitsetDest, TSIZE (BITSET)) ; |
| 84 |
INC (bitsetSrc, TSIZE (BITSET)) ; |
| 85 |
DEC (size, TSIZE (BITSET)) |
| 86 |
END ; |
| 87 |
byteDest := VAL (ptrToByte, bitsetDest) ; |
| 88 |
byteSrc := VAL (ptrToByte, bitsetSrc) ; |
| 89 |
WHILE size > 0 DO |
| 90 |
byteDest^ := byteDest^ + byteSrc^ ; |
| 91 |
INC (byteDest) ; |
| 92 |
INC (byteSrc) ; |
| 93 |
DEC (size) |
| 94 |
END |
| 95 |
END BlockOr ; |
| 96 |
|
| 97 |
|
| 98 |
(* |
| 99 |
BlockXor - performs a bitwise XOR on blocks |
| 100 |
[dest..dest+size-1] := [dest..dest+size-1] XOR |
| 101 |
[src..src+size-1] |
| 102 |
*) |
| 103 |
|
| 104 |
PROCEDURE BlockXor (dest, src: ADDRESS; size: CARDINAL) ; |
| 105 |
VAR |
| 106 |
bitsetDest, bitsetSrc: ptrToBitset ; |
| 107 |
byteDest, byteSrc : ptrToByte ; |
| 108 |
BEGIN |
| 109 |
bitsetDest := dest ; |
| 110 |
bitsetSrc := src ; |
| 111 |
WHILE size>TSIZE(BITSET) DO |
| 112 |
bitsetDest^ := bitsetDest^ - bitsetSrc^ ; |
| 113 |
INC(bitsetDest, TSIZE(BITSET)) ; |
| 114 |
INC(bitsetSrc, TSIZE(BITSET)) ; |
| 115 |
DEC(size, TSIZE(BITSET)) |
| 116 |
END ; |
| 117 |
byteDest := VAL(ptrToByte, bitsetDest) ; |
| 118 |
byteSrc := VAL(ptrToByte, bitsetSrc) ; |
| 119 |
WHILE size>0 DO |
| 120 |
byteDest^ := byteDest^ - byteSrc^ ; |
| 121 |
INC(byteDest) ; |
| 122 |
INC(byteSrc) ; |
| 123 |
DEC(size) |
| 124 |
END |
| 125 |
END BlockXor ; |
| 126 |
|
| 127 |
|
| 128 |
(* |
| 129 |
BlockNot - performs a bitsize NOT on the block as defined |
| 130 |
by: [dest..dest+size-1] |
| 131 |
*) |
| 132 |
|
| 133 |
PROCEDURE BlockNot (dest: ADDRESS; size: CARDINAL) ; |
| 134 |
VAR |
| 135 |
bitsetDest: ptrToBitset ; |
| 136 |
byteDest : ptrToByte ; |
| 137 |
BEGIN |
| 138 |
bitsetDest := dest ; |
| 139 |
WHILE size>TSIZE(BITSET) DO |
| 140 |
bitsetDest^ := -bitsetDest^ ; |
| 141 |
INC(bitsetDest, TSIZE(BITSET)) ; |
| 142 |
DEC(size, TSIZE(BITSET)) |
| 143 |
END ; |
| 144 |
byteDest := VAL(ptrToByte, bitsetDest) ; |
| 145 |
WHILE size>0 DO |
| 146 |
byteDest^ := - byteDest^ ; |
| 147 |
INC (byteDest) ; |
| 148 |
DEC (size) |
| 149 |
END |
| 150 |
END BlockNot ; |
| 151 |
|
| 152 |
|
| 153 |
(* |
| 154 |
BlockShr - performs a block shift right of, count, bits. |
| 155 |
Where the block is defined as: |
| 156 |
[dest..dest+size-1]. |
| 157 |
The block is considered to be an ARRAY OF BYTEs |
| 158 |
which is shifted, bit at a time over each byte in |
| 159 |
turn. The left most byte is considered the byte |
| 160 |
located at the lowest address. |
| 161 |
If you require an endianness SHIFT use |
| 162 |
the SYSTEM.SHIFT procedure and declare the |
| 163 |
block as a POINTER TO set type. |
| 164 |
*) |
| 165 |
|
| 166 |
PROCEDURE BlockShr (dest: ADDRESS; size, count: CARDINAL) ; |
| 167 |
VAR |
| 168 |
p : ptrToByte ; |
| 169 |
carry, |
| 170 |
hi, lo : BITSET8 ; |
| 171 |
byteOffset, |
| 172 |
bitOffset : CARDINAL ; |
| 173 |
BEGIN |
| 174 |
byteOffset := count DIV BITSPERBYTE ; |
| 175 |
IF byteOffset >= size |
| 176 |
THEN |
| 177 |
(* shifted all data out, nothing left *) |
| 178 |
p := dest ; |
| 179 |
dest := memset (p, 0, size) |
| 180 |
ELSE |
| 181 |
bitOffset := count MOD BITSPERBYTE ; |
| 182 |
IF byteOffset > 0 |
| 183 |
THEN |
| 184 |
(* move whole bytes using memmove *) |
| 185 |
p := dest ; |
| 186 |
dest := memmove (dest, dest+VAL(ADDRESS, byteOffset), size-byteOffset) ; |
| 187 |
(* zero leading bytes *) |
| 188 |
dest := memset (p, 0, byteOffset) |
| 189 |
END ; |
| 190 |
IF bitOffset > 0 |
| 191 |
THEN |
| 192 |
(* some real shifting is necessary *) |
| 193 |
p := dest + VAL (ADDRESS, byteOffset) ; |
| 194 |
hi := BITSET8 {} ; |
| 195 |
WHILE size>byteOffset DO |
| 196 |
lo := SHIFT (p^, -bitOffset) ; |
| 197 |
carry := SHIFT (p^, BITSPERBYTE - bitOffset) ; |
| 198 |
p^ := lo + hi ; |
| 199 |
INC (p) ; |
| 200 |
hi := carry ; |
| 201 |
DEC (size) |
| 202 |
END |
| 203 |
END |
| 204 |
END |
| 205 |
END BlockShr ; |
| 206 |
|
| 207 |
|
| 208 |
(* |
| 209 |
BlockShl - performs a block shift left of, count, bits. |
| 210 |
Where the block is defined as: |
| 211 |
[dest..dest+size-1]. |
| 212 |
The block is considered to be an ARRAY OF BYTEs |
| 213 |
which is shifted, bit at a time over each byte in |
| 214 |
turn. The left most byte is considered the byte |
| 215 |
located at the lowest address. |
| 216 |
If you require an endianness SHIFT use |
| 217 |
the SYSTEM.SHIFT procedure and declare the |
| 218 |
block as a POINTER TO set type. |
| 219 |
*) |
| 220 |
|
| 221 |
PROCEDURE BlockShl (dest: ADDRESS; size, count: CARDINAL) ; |
| 222 |
VAR |
| 223 |
p : ptrToByte ; |
| 224 |
carry, |
| 225 |
hi, lo : BITSET8 ; |
| 226 |
byteOffset, |
| 227 |
bitOffset : CARDINAL ; |
| 228 |
BEGIN |
| 229 |
byteOffset := count DIV BITSPERBYTE ; |
| 230 |
IF byteOffset>=size |
| 231 |
THEN |
| 232 |
(* shifted all data out, nothing left *) |
| 233 |
p := dest ; |
| 234 |
dest := memset(p, 0, size) |
| 235 |
ELSE |
| 236 |
bitOffset := count MOD BITSPERBYTE ; |
| 237 |
IF byteOffset>0 |
| 238 |
THEN |
| 239 |
(* move whole bytes using memmove *) |
| 240 |
p := dest ; |
| 241 |
dest := memmove (dest, dest + VAL (ADDRESS, byteOffset), size - byteOffset) ; |
| 242 |
(* zero leading bytes *) |
| 243 |
dest := memset (p, 0, byteOffset) |
| 244 |
END ; |
| 245 |
IF bitOffset>0 |
| 246 |
THEN |
| 247 |
(* some real shifting is necessary *) |
| 248 |
p := dest + VAL (ADDRESS, byteOffset) ; |
| 249 |
hi := BITSET8 {} ; |
| 250 |
WHILE size > byteOffset DO |
| 251 |
lo := SHIFT (p^, -bitOffset) ; |
| 252 |
carry := SHIFT (p^, (BITSPERBYTE - bitOffset)) ; |
| 253 |
p^ := lo + hi ; |
| 254 |
INC (p) ; |
| 255 |
hi := carry ; |
| 256 |
DEC (size) |
| 257 |
END |
| 258 |
END |
| 259 |
END |
| 260 |
END BlockShl ; |
| 261 |
|
| 262 |
|
| 263 |
(* |
| 264 |
BlockRor - performs a block rotate right of, count, bits. |
| 265 |
Where the block is defined as: |
| 266 |
[dest..dest+size-1]. |
| 267 |
The block is considered to be an ARRAY OF BYTEs |
| 268 |
which is shifted, bit at a time over each byte in |
| 269 |
turn. The left most byte is considered the byte |
| 270 |
located at the lowest address. |
| 271 |
If you require an endianness ROTATE use |
| 272 |
the SYSTEM.ROTATE procedure and declare the |
| 273 |
block as a POINTER TO set type. |
| 274 |
*) |
| 275 |
|
| 276 |
PROCEDURE BlockRor (dest: ADDRESS; size, count: CARDINAL) ; |
| 277 |
BEGIN |
| 278 |
(* not yet implemented *) |
| 279 |
HALT |
| 280 |
END BlockRor ; |
| 281 |
|
| 282 |
|
| 283 |
(* |
| 284 |
BlockRol - performs a block rotate left of, count, bits. |
| 285 |
Where the block is defined as: |
| 286 |
[dest..dest+size-1]. |
| 287 |
The block is considered to be an ARRAY OF BYTEs |
| 288 |
which is shifted, bit at a time over each byte in |
| 289 |
turn. The left most byte is considered the byte |
| 290 |
located at the lowest address. |
| 291 |
If you require an endianness ROTATE use |
| 292 |
the SYSTEM.ROTATE procedure and declare the |
| 293 |
block as a POINTER TO set type. |
| 294 |
*) |
| 295 |
|
| 296 |
PROCEDURE BlockRol (dest: ADDRESS; size, count: CARDINAL) ; |
| 297 |
BEGIN |
| 298 |
(* not yet implemented *) |
| 299 |
HALT |
| 300 |
END BlockRol ; |
| 301 |
|
| 302 |
|
| 303 |
END BitBlockOps. |