summaryrefslogtreecommitdiff
path: root/src/os/crc.c
blob: 178a784703ecadf598237e6b1f0ddc303480d5ce (plain)
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
#include "libultra_internal.h"

u8 __osContAddressCrc(u16 addr) {
    u8 temp;
    u8 temp2;
    int i;
    temp = 0;
    for (i = 0; i < 16; i++) {
        if (temp & 0x10) {
            temp2 = 21;
        } else {
            temp2 = 0;
        }

        temp <<= 1;
        temp |= (u8) ((addr & 0x400) ? 1 : 0);
        addr <<= 1;
        temp ^= temp2;
    }
    return temp & 0x1f;
}

u8 __osContDataCrc(u8* data) {
    u8 temp;
    u8 temp2;
    int i;
    int j;
    temp = 0;
    for (i = 0; i <= 32; i++, data++) {
        for (j = 7; j >= 0; j--) {
            if (temp & 0x80) {
                temp2 = 133;
            } else {
                temp2 = 0;
            }
            temp <<= 1;
            if (i == 32) {
                temp &= -1;
            } else {
                temp |= ((*data & (1 << j)) ? 1 : 0);
            }
            temp ^= temp2;
        }
    }
    return temp;
}