sync
This commit is contained in:
40
C/Adidas/shared/bit_stuff.c
Normal file
40
C/Adidas/shared/bit_stuff.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "bit_stuff.h"
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
unsigned int count_ones(unsigned int value){
|
||||
unsigned int count = 0;
|
||||
while (value){
|
||||
count += value & 1;
|
||||
value >>= 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void make_bitmask(unsigned int width, unsigned int shift, unsigned int* mask){
|
||||
if (width == 0 || width > sizeof(unsigned int)*CHAR_BIT) return;
|
||||
if (width == sizeof(unsigned int) * CHAR_BIT) *mask = ~0U << shift;
|
||||
else *mask = ((1U << width) - 1) << shift;
|
||||
}
|
||||
|
||||
void apply_bitmask(unsigned int value, unsigned int mask, unsigned int* masked_value) {
|
||||
if (masked_value != NULL) {
|
||||
*masked_value = value & mask;
|
||||
}
|
||||
}
|
||||
|
||||
void flip_bit(unsigned int value, unsigned int bit_index, unsigned int* updated_value){
|
||||
if (updated_value == 0 || bit_index >= sizeof(unsigned int) * CHAR_BIT) return;
|
||||
*updated_value = value ^ (1U << bit_index);
|
||||
}
|
||||
|
||||
void extract_nibbles_from_byte(uint8_t value, uint8_t* high_nibble, uint8_t* low_nibble){
|
||||
if (high_nibble == NULL || low_nibble == NULL) return;
|
||||
*high_nibble = (value >> 4) & 0xF;
|
||||
*low_nibble = value & 0xF;
|
||||
}
|
||||
|
||||
void combine_nibles_to_byte(uint8_t high_nibble, uint8_t low_nibble, uint8_t* value){
|
||||
if (value == NULL) return;
|
||||
*value = (high_nibble << 4) | (low_nibble);
|
||||
}
|
||||
36
C/Adidas/shared/bit_stuff.h
Normal file
36
C/Adidas/shared/bit_stuff.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* pre : -
|
||||
* post: the number of bits with value 1 is counted and returned
|
||||
*/
|
||||
unsigned int count_ones(unsigned int value);
|
||||
|
||||
/* pre : -
|
||||
* post: a bitmask with a given width and a given shift is generated (so w=5 and
|
||||
* s=1 gives 00111110)
|
||||
*/
|
||||
void make_bitmask(unsigned int width, unsigned int shift, unsigned int* mask);
|
||||
|
||||
/* pre : -
|
||||
* post: 'masked_value' is assigned the value of 'value' with the 'mask' applied
|
||||
*/
|
||||
void apply_bitmask(unsigned int value, unsigned int mask, unsigned int* masked_value);
|
||||
|
||||
/* pre : -
|
||||
* post: the bit of index 'bit_index' of 'value' is flipped: 0 --> 1, 1 --> 0.
|
||||
*/
|
||||
void flip_bit(unsigned int value, unsigned int bit_index, unsigned int* updated_value);
|
||||
|
||||
/* pre : -
|
||||
* post: the high and low nibbles of 'value' of stored in 'high_nibble' and
|
||||
* 'low_nibble'.
|
||||
*/
|
||||
void extract_nibbles_from_byte(uint8_t value, uint8_t* high_nibble, uint8_t* low_nibbe);
|
||||
|
||||
/* pre : -
|
||||
* post: the nibble values of the 'high_nibble' and 'low_nibble' are combined
|
||||
* and stored in 'value'
|
||||
*/
|
||||
void combine_nibles_to_byte(uint8_t high_nibble, uint8_t low_nibbe, uint8_t* value);
|
||||
4
C/Adidas/shared/channel.c
Normal file
4
C/Adidas/shared/channel.c
Normal file
@@ -0,0 +1,4 @@
|
||||
#include "channel.h"
|
||||
// add here includes, if needed
|
||||
|
||||
// add here your implementation
|
||||
22
C/Adidas/shared/channel.h
Normal file
22
C/Adidas/shared/channel.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef CHANNEL_H
|
||||
#define CHANNEL_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
/*!
|
||||
* Initialiase the channel module
|
||||
*/
|
||||
void channel_init();
|
||||
|
||||
/*!
|
||||
* Randomly invert one the bits of the input 'value' parameter
|
||||
*
|
||||
* @param value: A byte
|
||||
*
|
||||
* @return: The input 'value' where on of the bits is randomly inverted
|
||||
*/
|
||||
uint8_t channel_change_one_random_bit(uint8_t value);
|
||||
|
||||
#endif
|
||||
|
||||
4
C/Adidas/shared/decode.c
Normal file
4
C/Adidas/shared/decode.c
Normal file
@@ -0,0 +1,4 @@
|
||||
#include "decode.h"
|
||||
// add here includes, if needed
|
||||
|
||||
// add here your implementation
|
||||
28
C/Adidas/shared/decode.h
Normal file
28
C/Adidas/shared/decode.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef DECODE_H_
|
||||
#define DECODE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*!
|
||||
* Combines the nibbles located at the 4-least-significant bits of
|
||||
* parameters 'low' and 'high' into one byte.
|
||||
*
|
||||
* @param high: A nibble that contains the 4 most-significant bits
|
||||
* @param low: A nibble that contains the 4 most-significant bits
|
||||
*
|
||||
* @return: A byte that combines the high and low nibble.
|
||||
*/
|
||||
uint8_t decode_combine_nibbles(uint8_t high, uint8_t low);
|
||||
|
||||
/*!
|
||||
* Decodes a nibble from a byte that contains the nible (4-bits) and
|
||||
* corresponding parity bits (3-bits). See assignment for more details.
|
||||
*
|
||||
* @param in: A bytes that contains a nibble and parity bits.
|
||||
* @param nibble: The address to which the decoded nibble
|
||||
* of 'in' will be written.
|
||||
*
|
||||
*/
|
||||
void decode_byte(uint8_t in, uint8_t* nibble);
|
||||
|
||||
#endif
|
||||
34
C/Adidas/shared/encode.c
Normal file
34
C/Adidas/shared/encode.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "encode.h"
|
||||
#include "parity.h"
|
||||
#include "bit_stuff.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void encode_get_nibbles(uint8_t value, uint8_t* high, uint8_t* low) {
|
||||
if (high == NULL || low == NULL) {
|
||||
return;
|
||||
}
|
||||
// Just extract the nibbles without encoding them
|
||||
extract_nibbles_from_byte(value, high, low);
|
||||
}
|
||||
|
||||
uint8_t encode_nibble(uint8_t nibble) {
|
||||
uint8_t p0, p1, p2;
|
||||
calculate_parity_bits(nibble, &p0, &p1, &p2);
|
||||
// Format: [0][d3][d2][d1][d0][p2][p1][p0] (MSB first)
|
||||
return ((nibble & 0x0F) << 3) | (p2 << 2) | (p1 << 1) | p0;
|
||||
}
|
||||
|
||||
void encode_value(uint8_t input, uint8_t* high, uint8_t* low) {
|
||||
if (high == NULL || low == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t high_nibble, low_nibble;
|
||||
encode_get_nibbles(input, &high_nibble, &low_nibble);
|
||||
|
||||
// Encode each nibble with parity bits
|
||||
*high = encode_nibble(high_nibble);
|
||||
*low = encode_nibble(low_nibble);
|
||||
}
|
||||
33
C/Adidas/shared/encode.h
Normal file
33
C/Adidas/shared/encode.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef ENCODE_H_
|
||||
#define ENCODE_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/*!
|
||||
* Split a byte 'value' into two nibbles (4-bits) named 'low' and 'high'.
|
||||
* See assignment for more details.
|
||||
*
|
||||
* @param value: A byte that will be encoded.
|
||||
* @param low: The address to which the 4 least-significant-bits
|
||||
* of 'value' will be written.
|
||||
* @param high: The address to which the 4 most-significant-bits
|
||||
* of 'value' will be written.
|
||||
*/
|
||||
void encode_get_nibbles(uint8_t value, uint8_t* high, uint8_t* low);
|
||||
|
||||
/*!
|
||||
* Encodes a the byte 'value' into two bytes named 'low' and 'high'.
|
||||
* The encoded bytes contain the parity bits. See assignment for more details.
|
||||
*
|
||||
* @param value: A byte that will be encoded.
|
||||
* @param low: The address to which the encoded value of the
|
||||
* 4 least-significant-bits of 'value' will be written.
|
||||
* @param high: The address to which the encoded value of the
|
||||
* 4 most-significant-bits of 'value' will be written.
|
||||
*/
|
||||
void encode_value(uint8_t input, uint8_t* high, uint8_t* low);
|
||||
|
||||
uint8_t encode_nibble(uint8_t nibble);
|
||||
|
||||
#endif
|
||||
14
C/Adidas/shared/parity.c
Normal file
14
C/Adidas/shared/parity.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "parity.h"
|
||||
|
||||
void calculate_parity_bits(uint8_t nibble, uint8_t* p0, uint8_t* p1, uint8_t* p2) {
|
||||
// Extract each data bit
|
||||
uint8_t d0 = (nibble >> 0) & 1;
|
||||
uint8_t d1 = (nibble >> 1) & 1;
|
||||
uint8_t d2 = (nibble >> 2) & 1;
|
||||
uint8_t d3 = (nibble >> 3) & 1;
|
||||
|
||||
// Parity bits as per assignment examples
|
||||
*p0 = d0 ^ d1 ^ d2; // Circle with d0,d1,d2
|
||||
*p1 = d0 ^ d1 ^ d3; // Circle with d0,d1,d3
|
||||
*p2 = d0 ^ d2 ^ d3; // Circle with d0,d2,d3
|
||||
}
|
||||
8
C/Adidas/shared/parity.h
Normal file
8
C/Adidas/shared/parity.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef PARITY_H_
|
||||
#define PARITY_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void calculate_parity_bits(uint8_t nibble, uint8_t* p0, uint8_t* p1, uint8_t* p2);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user