-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecodefunc.c
103 lines (94 loc) · 2.06 KB
/
decodefunc.c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
unsigned int addrshift[32];
unsigned int addrshiftreverse[32];
unsigned int datashift[8];
unsigned int datashiftreverse[8];
int addrlines = 0;
int datalines = 8;
static void decodeinit(void)
{
int i;
// init the mapping
for (i = 0; i < 8; i++) {
datashift[i] = i;
}
for (i = 0; i < 32; i++) {
addrshift[i] = i;
}
}
static void updatereverselookup(void)
{
int i;
for (i = 0; i < addrlines; i++) {
addrshiftreverse[addrshift[i]] = i;
}
for (i = 0; i < datalines; i++) {
datashiftreverse[datashift[i]] = i;
}
}
static unsigned char makedata(unsigned int data)
{
unsigned int outdata = 0;
int i;
for (i = 0; i < 8; i++) {
outdata |= ((data & (1 << i)) >> i) << datashift[i];
}
return outdata;
}
static unsigned int makeaddr(unsigned int addr)
{
unsigned int outaddr = 0;
int i;
for (i = 0; i < addrlines; i++) {
outaddr |= ((addr & (1 << i)) >> i) << addrshift[i];
}
return outaddr;
}
static int decode(unsigned char *out, unsigned char *in, unsigned int len)
{
unsigned int addr;
unsigned char data;
int i;
for (i = 0; i < len; i++) {
data = makedata(in[i]);
addr = makeaddr(i);
out[addr] = data;
}
}
static unsigned int getnumbits(unsigned int num)
{
unsigned int test = 1;
int count = 1;
while ((num & test) != num) {
test = (test << 1) | 1;
count++;
}
return count;
}
static int dumpdatalines(void)
{
int i;
printf("datalines: ");
for (i = 0; i < datalines; i++) {
printf("%02x ", datashift[i]);
}
printf("\n");
printf("reverse: ");
for (i = 0; i < datalines; i++) {
printf("%02x ", datashiftreverse[i]);
}
printf("\n");
}
static int dumpaddrlines(void)
{
int i;
printf("addrlines: ");
for (i = 0; i < addrlines; i++) {
printf("%02x ", addrshift[i]);
}
printf("\n");
printf("reverse: ");
for (i = 0; i < addrlines; i++) {
printf("%02x ", addrshiftreverse[i]);
}
printf("\n");
}