File: byteops.c

package info (click to toggle)
nvram-wakeup 1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,052 kB
  • ctags: 242
  • sloc: ansic: 1,943; sh: 309; ruby: 190; makefile: 102
file content (85 lines) | stat: -rw-r--r-- 2,836 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 *   NVRAM WakeUp
 *   Copyright (C) 2001-2002, Sergei Haller.
 *
 *   $Id: byteops.c 381 2003-03-18 13:44:07Z bistr-o-math $
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#define CVSREV_byteops_c \
     "$Id: byteops.c 381 2003-03-18 13:44:07Z bistr-o-math $"

#include "nvram-wakeup.h"

/*
 *  computes the so called BCD notation of an unsigned char,
 *  e.g. the bcd notation of 34 is 0x34 
 *
 *  We don't check, if the conversion makes sense, i.e.
 *  bin2bcd(112) would return 
 *     (112/10) << 4 + 112 % 10 =
 *        11    << 4 +    2     =
 *             0xB0  +    2     = 0xB2
 */
unsigned char bin2bcd( const unsigned char c ) {
     return ((c / 10) << 4)  + (c % 10);
}

/*
 *  computes the unsigned char from its so called BCD notation,
 *  e.g. from 0x34 to 34.
 *
 *  We don't check, if the conversion makes sense, i.e.
 *  bcd2bin(0xAC) would return 
 *     (0xAC >> 4)*10 + 0xAC & 0x0F = 
 *          0xA   *10 +     0xC     =
 *              100   +     12      = 112
 */
unsigned char bcd2bin( const unsigned char c ) {
     return (c >> 4)*10 + (c & 0x0F);
}

/*
 *  this function returns the byte stored in the bits 
 *              sh+nr,..,sh+1 
 *  of byte b, e.g.
 *     b=01011101, sh=0, nr=3
 *            ^^^ --> 101 --> 5 (decimal)
 */
unsigned char calculate_read( const unsigned char b, 
                              const unsigned char nr, 
                              const unsigned char sh) {
     unsigned char msk = 255 >> (8-nr) << sh;
     return (msk & b) >> sh;
}

/*
 *  this function returns the byte calculated by setting the bits 
 *              sh+nr,..,sh+1 
 *  of byte b to the bits nr,..,1 of new, e.g.
 *     b=01011101, sh=2, nr=4, new=00000101
 *         ^^^^  will be set to 0101:  ^^^^
 *  -->  01010101
 *  WARNING: we do not check if new <= 2^(nr-1)
 */
unsigned char calculate_write( const unsigned char b, 
                               const unsigned char _new, 
                               const unsigned char nr, 
                               const unsigned char sh) {
     unsigned char msk = 255 >> (8-nr) << sh;
     return (b & ~msk) | (_new << sh);;
}