Skip to content

Commit

Permalink
Add bitbuffer NRZI(NRZS/NRZM) decodes
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Jan 27, 2019
1 parent f980ea5 commit cce293c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/bitbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ void bitbuffer_extract_bytes(bitbuffer_t *bitbuffer, unsigned row,
/// Invert all bits in the bitbuffer (do not invert the empty bits)
void bitbuffer_invert(bitbuffer_t *bits);

// Non-Return-to-Zero Space (NRZI) decode the bitbuffer.
// "One" is represented by no change in level, "Zero" is represented by change in level.
void bitbuffer_nrzs_decode(bitbuffer_t *bits);

// Non-Return-to-Zero Mark (NRZI) decode the bitbuffer.
// "One" is represented by change in level, "Zero" is represented by no change in level.
void bitbuffer_nrzm_decode(bitbuffer_t *bits);

/// Print the content of the bitbuffer
void bitbuffer_print(const bitbuffer_t *bits);

Expand Down
47 changes: 47 additions & 0 deletions src/bitbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,42 @@ void bitbuffer_invert(bitbuffer_t *bits)
}
}

void bitbuffer_nrzs_decode(bitbuffer_t *bits)
{
for (unsigned row = 0; row < bits->num_rows; ++row) {
if (bits->bits_per_row[row] > 0) {
const unsigned last_col = (bits->bits_per_row[row] - 1) / 8;
const unsigned last_bits = ((bits->bits_per_row[row] - 1) % 8) + 1;

int prev = 0;
for (unsigned col = 0; col <= last_col; ++col) {
int mask = (prev << 7) | bits->bb[row][col] >> 1;
prev = bits->bb[row][col];
bits->bb[row][col] = bits->bb[row][col] ^ ~mask;
}
bits->bb[row][last_col] &= 0xFF << (8 - last_bits); // Clear unused bits in last byte
}
}
}

void bitbuffer_nrzm_decode(bitbuffer_t *bits)
{
for (unsigned row = 0; row < bits->num_rows; ++row) {
if (bits->bits_per_row[row] > 0) {
const unsigned last_col = (bits->bits_per_row[row] - 1) / 8;
const unsigned last_bits = ((bits->bits_per_row[row] - 1) % 8) + 1;

int prev = 0;
for (unsigned col = 0; col <= last_col; ++col) {
int mask = (prev << 7) | bits->bb[row][col] >> 1;
prev = bits->bb[row][col];
bits->bb[row][col] = bits->bb[row][col] ^ mask;
}
bits->bb[row][last_col] &= 0xFF << (8 - last_bits); // Clear unused bits in last byte
}
}
}

void bitbuffer_extract_bytes(bitbuffer_t *bitbuffer, unsigned row,
unsigned pos, uint8_t *out, unsigned len)
{
Expand Down Expand Up @@ -374,6 +410,7 @@ int bitbuffer_find_repeated_row(bitbuffer_t *bits, unsigned min_repeats, unsigne

// Unit testing
#ifdef _TEST
#include <assert.h>
int main(int argc, char **argv)
{
fprintf(stderr, "bitbuffer:: test\n");
Expand Down Expand Up @@ -408,6 +445,16 @@ int main(int argc, char **argv)
bitbuffer_invert(&bits);
bitbuffer_print(&bits);

fprintf(stderr, "TEST: bitbuffer:: nrzs_decode\n");
bits.num_rows = 1;
bits.bb[0][0] = 0x74;
bits.bb[0][1] = 0x60;
bits.bits_per_row[0] = 12;
bitbuffer_nrzs_decode(&bits);
bitbuffer_print(&bits);
assert(bits.bb[0][0] == 0xB1);
assert(bits.bb[0][1] == 0xA0);

fprintf(stderr, "TEST: bitbuffer:: Clear\n");
bitbuffer_clear(&bits);
bitbuffer_print(&bits);
Expand Down

0 comments on commit cce293c

Please sign in to comment.