diff --git a/common/auxiliar.c b/common/auxiliar.c index 9adbb20..8230ad4 100644 --- a/common/auxiliar.c +++ b/common/auxiliar.c @@ -120,4 +120,44 @@ void normalize(char **inputs, int num_inputs) } } } +} + +int bin2dec(char *bin) +{ + int i; + int num = 0; + int length = strlen(bin); + int pos = length - 1; + char *aux = (char*) malloc(sizeof(char) * length); + strcpy(aux,bin); + normalize(&aux,1); + for(i = 0; i < length; i++) + { + if(aux[pos--]) + { + num += pow(2,i); + } + } + free(aux); + return num; +} + +char* dec2bin(int dec, int length) +{ + char *bin = (char*) malloc(sizeof(char) * length + 1); + int pos = length-1; + int i; + for(i = 0; i < length; i++) + { + if(dec & 0x1) + { + bin[pos--] = '1'; + } + else + { + bin[pos--] = '0'; + } + dec >>= 1; + } + return bin; } \ No newline at end of file diff --git a/common/auxiliar.h b/common/auxiliar.h index b3eb016..1633f57 100644 --- a/common/auxiliar.h +++ b/common/auxiliar.h @@ -13,3 +13,5 @@ void error(); void free_mem(char **inputs, int num_inputs); void normalize(char **inputs, int num_inputs); +int bin2dec(char *bin); +char* dec2bin(int dec, int length); \ No newline at end of file diff --git a/plexers/Makefile b/plexers/Makefile index 3229256..69be5b7 100644 --- a/plexers/Makefile +++ b/plexers/Makefile @@ -1,4 +1,4 @@ -TARGETS = multiplexor +TARGETS = multiplexor demultiplexor encoder decoder .PHONY: all clean @@ -9,3 +9,11 @@ clean: multiplexor: gcc -I../common/ ../common/auxiliar.c multiplexor.c -o multiplexor -lm + +demultiplexor: + gcc -I../common/ ../common/auxiliar.c demultiplexor.c -o demultiplexor -lm + +encoder: + gcc -I../common/ ../common/auxiliar.c encoder.c -o encoder -lm +decoder: + gcc -I../common/ ../common/auxiliar.c decoder.c -o decoder -lm diff --git a/plexers/decoder.c b/plexers/decoder.c new file mode 100644 index 0000000..0b193cd --- /dev/null +++ b/plexers/decoder.c @@ -0,0 +1,47 @@ +#include +#include +#include "auxiliar.h" +#include + +int main(int argc, char **argv) +{ + int i; + if(argc != 2) + { + error(); + } + char *input = argv[1]; + if(!strcmp(input,"error")) + { + error(); + } + char **inputs = comma_separate(input); + if(inputs == NULL) + { + error(); + } + int num_inputs = num_occurrences(input,','); + if(num_inputs != 1) + { + error(); + } + int selected = bin2dec(inputs[0]); + int num_outputs = pow(2,strlen(inputs[0])); + for(i = 0; i < num_outputs; i++) + { + if(i == selected) + { + printf("1"); + } + else + { + printf("0"); + } + if(i != (num_outputs-1)) + { + printf(","); + } + } + free_mem(inputs,num_inputs); + return 0; +} diff --git a/plexers/demultiplexor.c b/plexers/demultiplexor.c new file mode 100644 index 0000000..78547d1 --- /dev/null +++ b/plexers/demultiplexor.c @@ -0,0 +1,52 @@ +#include +#include +#include "auxiliar.h" +#include + +int main(int argc, char **argv) +{ + int i; + int j; + if(argc != 2) + { + error(); + } + char *input = argv[1]; + if(!strcmp(input,"error")) + { + error(); + } + char **inputs = comma_separate(input); + if(inputs == NULL) + { + error(); + } + int num_inputs = num_occurrences(input,','); + if(num_inputs != 2) + { + error(); + } + int input_length = strlen(inputs[0]); + int selected = bin2dec(inputs[1]); + int num_outputs = pow(2,strlen(inputs[1])); + for(i = 0; i < num_outputs; i++) + { + if(i == selected) + { + printf("%s",inputs[0]); + } + else + { + for(j = 0; j < input_length; j++) + { + printf("0"); + } + } + if(i != (num_outputs-1)) + { + printf(","); + } + } + free_mem(inputs,num_inputs); + return 0; +} diff --git a/plexers/encoder.c b/plexers/encoder.c new file mode 100644 index 0000000..c0086f2 --- /dev/null +++ b/plexers/encoder.c @@ -0,0 +1,68 @@ +#include +#include +#include "auxiliar.h" +#include + +int check_length(char **inputs, uint8_t num_inputs) +{ + int i; + for(i = 0; i < num_inputs; i++) + { + if(strlen(inputs[i]) != 1) + { + return -1; + } + } + int max_value = 1; + i = 0; + while(max_value < num_inputs) + { + max_value = pow(2,++i); + } + return i; +} + +int main(int argc, char **argv) +{ + int i; + int pos; + if(argc != 2) + { + error(); + } + char *input = argv[1]; + if(!strcmp(input,"error")) + { + error(); + } + char **inputs = comma_separate(input); + if(inputs == NULL) + { + error(); + } + int num_inputs = num_occurrences(input,','); + pos = num_inputs - 1; + int sel_length = check_length(inputs,num_inputs); + if(sel_length == -1) + { + error(); + } + int selected = -1; + for(i = 0; i < num_inputs; i++) + { + if(inputs[i][0] == '1') + { + selected = i; + break; + } + } + if(selected == -1) + { + error(); + } + char *result = dec2bin(selected,sel_length); + printf("%s",result); + free(result); + free_mem(inputs,num_inputs); + return 0; +} diff --git a/plexers/multiplexor b/plexers/multiplexor deleted file mode 100755 index 181bc09..0000000 --- a/plexers/multiplexor +++ /dev/null diff --git a/plexers/multiplexor.c b/plexers/multiplexor.c index 0d8dde0..1207657 100644 --- a/plexers/multiplexor.c +++ b/plexers/multiplexor.c @@ -3,26 +3,6 @@ #include "auxiliar.h" #include -int bin2dec(char *bin) -{ - int i; - int num = 0; - int length = strlen(bin); - int pos = length - 1; - char *aux = (char*) malloc(sizeof(char) * length); - strcpy(aux,bin); - normalize(&aux,1); - for(i = 0; i < length; i++) - { - if(aux[pos--]) - { - num += pow(2,i); - } - } - free(aux); - return num; -} - int check_length(char **inputs, uint8_t num_inputs) { int i; diff --git a/todo b/todo index 970a6f4..3088924 100644 --- a/todo +++ b/todo @@ -1,6 +1,3 @@ -demuxer -coder -decoder bit selector adder