Skip to content

Commit 19145a6

Browse files
committed
Match bitstream.c
1 parent 08144bf commit 19145a6

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed

include/sys/bitstream.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef _SYS_BITSTREAM_H
2+
#define _SYS_BITSTREAM_H
3+
4+
#include "PR/ultratypes.h"
5+
6+
typedef struct {
7+
/*0x0*/ u8 *data;
8+
/*0x4*/ s32 byteLength;
9+
/*0x8*/ s32 bitLength;
10+
/*0xC*/ s32 capacity;
11+
/*0x10*/ s32 bitPos;
12+
} BitStream;
13+
14+
BitStream *bitstream_init(BitStream *stream, u8 *data, s32 bitLength, s32 capacity);
15+
u32 bitstream_read(BitStream *stream, u8 n);
16+
void bitstream_write(BitStream *stream, u32 data, u8 n);
17+
void bitstream_append(BitStream *stream, u32 data, u8 n);
18+
void bitstream_set_pos(BitStream *stream, s32 bitPos);
19+
20+
#endif

src/bitstream.c

+84-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,89 @@
1-
#include "common.h"
1+
#include "PR/ultratypes.h"
2+
#include "sys/bitstream.h"
23

3-
#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000B8B0.s")
4+
BitStream *bitstream_init(BitStream *stream, u8 *data, s32 bitLength, s32 capacity) {
5+
stream->byteLength = bitLength >> 3;
6+
if (bitLength & 7) {
7+
// need one extra byte to hold bits
8+
stream->byteLength += 1;
9+
}
410

5-
#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000B8E4.s")
11+
stream->bitLength = bitLength;
12+
stream->capacity = capacity;
13+
stream->data = data;
14+
stream->bitPos = 0;
615

7-
#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000B970.s")
16+
return stream;
17+
}
818

9-
#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000BA20.s")
19+
u32 bitstream_read(BitStream *stream, u8 n) {
20+
u32 data;
21+
s32 i;
22+
s32 byteIdx;
23+
s32 bitIdx;
24+
u8 bit;
1025

11-
#pragma GLOBAL_ASM("asm/nonmatchings/bitstream/func_8000BA50.s")
26+
data = 0;
27+
i = 0;
28+
29+
if (stream->bitPos < stream->bitLength) {}
30+
31+
while (n != 0 && stream->bitPos < stream->bitLength) {
32+
byteIdx = stream->bitPos >> 3;
33+
bitIdx = stream->bitPos & 7;
34+
35+
bit = (stream->data[byteIdx] >> bitIdx) & 1;
36+
data |= bit << i;
37+
38+
i++;
39+
stream->bitPos++;
40+
n--;
41+
}
42+
43+
return data;
44+
}
45+
46+
void bitstream_write(BitStream *stream, u32 data, u8 n) {
47+
s32 i;
48+
s32 byteIdx;
49+
s32 bitIdx;
50+
s32 bitPos;
51+
s32 capacity;
52+
u8 bit;
53+
54+
if (stream->capacity) {}
55+
if (stream->bitPos) {}
56+
57+
i = 0;
58+
59+
while (n != 0 && stream->bitPos < stream->capacity) {
60+
byteIdx = stream->bitPos >> 3;
61+
bitIdx = stream->bitPos & 7;
62+
63+
bit = (data >> i) & 1;
64+
stream->data[byteIdx] |= bit << bitIdx;
65+
66+
i++;
67+
68+
stream->bitPos++;
69+
if (stream->bitLength < stream->bitPos) {
70+
stream->bitLength += 1;
71+
}
72+
73+
n--;
74+
}
75+
}
76+
77+
void bitstream_append(BitStream *stream, u32 data, u8 n) {
78+
s32 len;
79+
80+
len = stream->bitLength;
81+
82+
stream->bitPos = len;
83+
84+
bitstream_write(stream, data, n);
85+
}
86+
87+
void bitstream_set_pos(BitStream *stream, s32 bitPos) {
88+
stream->bitPos = bitPos;
89+
}

symbol_addrs.txt

+5
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,11 @@ delayByteMirror = 0x8008c954; // size:0x1
985985
pointerIntArrayCounter = 0x800b1798; // size:0x2
986986
SHORT_ARRAY_800b17d0 = 0x800b17d0;
987987
gFramebufferEnd = 0x800bce08; // size:0x4
988+
bitstream_init = 0x8000b8b0; // type:func
989+
bitstream_read = 0x8000b8e4; // type:func
990+
bitstream_write = 0x8000b970; // type:func
991+
bitstream_append = 0x8000ba20; // type:func
992+
bitstream_set_pos = 0x8000ba50; // type:func
988993
linked_list_init = 0x8000BA60; // type:func
989994
linked_list_prepend = 0x8000BA80; // type:func
990995
linked_list_append = 0x8000BAA8; // type:func

0 commit comments

Comments
 (0)