diff --git a/arith/Makefile b/arith/Makefile new file mode 100644 index 0000000..a183b5b --- /dev/null +++ b/arith/Makefile @@ -0,0 +1,11 @@ +TARGETS = adder + +.PHONY: all clean + +all: $(TARGETS) + +clean: + rm -rf $(TARGETS) + +adder: + gcc -I../common/ ../common/auxiliar.c adder.c -o adder \ No newline at end of file diff --git a/arith/adder.c b/arith/adder.c new file mode 100644 index 0000000..c890fa1 --- /dev/null +++ b/arith/adder.c @@ -0,0 +1,53 @@ +#include +#include +#include "auxiliar.h" + +int check_length(char **inputs, uint8_t num_inputs) +{ + int input_length = strlen(inputs[0]); + if((input_length != strlen(inputs[1])) || (strlen(inputs[2]) != 1)) + { + return -1; + } + return input_length; +} + +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,','); + if(num_inputs != 3) + { + error(); + } + int input_length = check_length(inputs,num_inputs); + if(input_length == -1) + { + error(); + } + int a,b,carry_in,c; + a = bin2dec(inputs[0]); + b = bin2dec(inputs[1]); + carry_in = bin2dec(inputs[2]); + c = a+b+carry_in; + char *result = dec2bin(c,input_length+1); + printf("%s,%c",result+1,result[0]); + free(result); + free_mem(inputs,num_inputs); + return 0; +} diff --git a/common/auxiliar.c b/common/auxiliar.c index 8230ad4..4a1200a 100644 --- a/common/auxiliar.c +++ b/common/auxiliar.c @@ -4,7 +4,7 @@ char* substring(char *string, uint8_t start, uint8_t end) { uint8_t size = end - start; char *substr; - substr = (char*) malloc(sizeof(char)*size + 1); + substr = (char*) malloc(sizeof(char)*(size + 1)); memset(substr,0x00,size+1); strncpy(substr,string+start,size); return substr; @@ -60,7 +60,7 @@ char** comma_separate(char *input) return NULL; } char **inputs; - inputs = malloc(sizeof(char*) * input_num); + inputs = malloc(sizeof(char*) * (input_num+1)); for(i = 0; i < input_num; i++) { next_pos += next_occurrence(input+pos,','); @@ -128,14 +128,14 @@ int bin2dec(char *bin) int num = 0; int length = strlen(bin); int pos = length - 1; - char *aux = (char*) malloc(sizeof(char) * length); + char *aux = (char*) malloc(sizeof(char) * (length+1)); strcpy(aux,bin); normalize(&aux,1); for(i = 0; i < length; i++) { if(aux[pos--]) { - num += pow(2,i); + num += 0x1 << i; } } free(aux); @@ -144,7 +144,7 @@ int bin2dec(char *bin) char* dec2bin(int dec, int length) { - char *bin = (char*) malloc(sizeof(char) * length + 1); + char *bin = (char*) malloc(sizeof(char) * (length + 1)); int pos = length-1; int i; for(i = 0; i < length; i++) @@ -160,4 +160,28 @@ char* dec2bin(int dec, int length) dec >>= 1; } return bin; -} \ No newline at end of file +} + + +void expand_input(char **input, int required_length) +{ + char *new_input = malloc(sizeof(char)*(required_length + 1)); + memset(new_input,'0',required_length+1); + int offset = required_length - strlen(*input); + strcpy(new_input + offset,*input); + free(*input); + *input = new_input; +} + +int str2int(char *string) +{ + int i; + int num = 0; + int mul = 1; + for(i = 0; i < strlen(string); i++) + { + num += mul*(string[i] - 48); + mul *= 10; + } + return num; +} diff --git a/common/auxiliar.h b/common/auxiliar.h index 1633f57..e98d974 100644 --- a/common/auxiliar.h +++ b/common/auxiliar.h @@ -8,10 +8,12 @@ int next_occurrence(char *string, char character); int num_occurrences(char *string, char character); int last_occurrence(char *string, char character); char** comma_separate(char *input); +int str2int(char *string); 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 +char* dec2bin(int dec, int length); +void expand_input(char **input, int required_length); diff --git a/logic_gate/logic_gate.c b/logic_gate/logic_gate.c index e9ef26b..d82fae4 100644 --- a/logic_gate/logic_gate.c +++ b/logic_gate/logic_gate.c @@ -21,8 +21,8 @@ char* or(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * input_length + 1); - memset(result, 0x00, input_length); + char *result = malloc(sizeof(char) * (input_length + 1)); + memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) @@ -46,8 +46,8 @@ char* nor(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * input_length + 1); - memset(result, 0x00, input_length); + char *result = malloc(sizeof(char) * (input_length + 1)); + memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) @@ -71,8 +71,8 @@ char* xor(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * input_length + 1); - memset(result, 0x00, input_length); + char *result = malloc(sizeof(char) * (input_length + 1)); + memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) @@ -95,8 +95,8 @@ char* xnor(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * input_length + 1); - memset(result, 0x00, input_length); + char *result = malloc(sizeof(char) * (input_length + 1)); + memset(result, 0x00, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) @@ -119,8 +119,8 @@ char* and(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * input_length + 1); - memset(result, 0x01, input_length); + char *result = malloc(sizeof(char) * (input_length + 1)); + memset(result, 0x01, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) @@ -143,8 +143,8 @@ char* nand(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * input_length + 1); - memset(result, 0x01, input_length); + char *result = malloc(sizeof(char) * (input_length + 1)); + memset(result, 0x01, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) @@ -167,8 +167,8 @@ char* not(char **inputs, int num_inputs, int input_length) { int i; int j; - char *result = malloc(sizeof(char) * input_length + 1); - memset(result, 0x01, input_length); + char *result = malloc(sizeof(char) * (input_length + 1)); + memset(result, 0x01, input_length+1); for(i = 0; i < input_length; i++) { for(j = 0; j < num_inputs; j++) diff --git a/plexers/Makefile b/plexers/Makefile index 69be5b7..3de9494 100644 --- a/plexers/Makefile +++ b/plexers/Makefile @@ -1,4 +1,4 @@ -TARGETS = multiplexor demultiplexor encoder decoder +TARGETS = multiplexor demultiplexor encoder decoder bit_selector .PHONY: all clean @@ -8,12 +8,14 @@ clean: rm -rf $(TARGETS) multiplexor: - gcc -I../common/ ../common/auxiliar.c multiplexor.c -o multiplexor -lm + gcc -I../common/ ../common/auxiliar.c multiplexor.c -o multiplexor demultiplexor: - gcc -I../common/ ../common/auxiliar.c demultiplexor.c -o demultiplexor -lm + gcc -I../common/ ../common/auxiliar.c demultiplexor.c -o demultiplexor encoder: - gcc -I../common/ ../common/auxiliar.c encoder.c -o encoder -lm + gcc -I../common/ ../common/auxiliar.c encoder.c -o encoder decoder: - gcc -I../common/ ../common/auxiliar.c decoder.c -o decoder -lm + gcc -I../common/ ../common/auxiliar.c decoder.c -o decoder +bit_selector: + gcc -I../common/ ../common/auxiliar.c bit_selector.c -o bit_selector diff --git a/plexers/bit_selector.c b/plexers/bit_selector.c new file mode 100644 index 0000000..44b8285 --- /dev/null +++ b/plexers/bit_selector.c @@ -0,0 +1,72 @@ +#include +#include +#include "auxiliar.h" +#include + +int check_length(char **inputs, int num_inputs, int output_length) +{ + int input_length = strlen(inputs[0]); + int selector_length = strlen(inputs[1]); + int sel_combinations = 0x1 << selector_length; + int num_groups = ceil(((float)input_length)/output_length); + int required_combinations = 1; + int i = 0; + while(required_combinations < num_groups) + { + required_combinations = 0x1 << ++i; + } + if(sel_combinations < required_combinations) + { + return -1; + } + return required_combinations * output_length; +} + +int main(int argc, char **argv) +{ + int i; + int j; + if(argc != 3) + { + error(); + } + char *input = argv[1]; + int output_length = str2int(argv[2]); + 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 required_length = check_length(inputs,num_inputs,output_length); + if(required_length == -1) + { + error(); + } + if(input_length != required_length) + { + expand_input(&inputs[0],required_length); + } + char *result = malloc(sizeof(char) * (output_length + 1)); + memset(result,0x00,output_length+1); + int selected = bin2dec(inputs[1]); + int pos = 0; + for(i = (selected+1)*(output_length)-1; i >= selected*output_length; i--) + { + result[pos++] = inputs[0][required_length-(i+1)]; + } + printf("%s",result); + free(result); + free_mem(inputs,num_inputs); + return 0; +} + diff --git a/plexers/decoder.c b/plexers/decoder.c index 0b193cd..d4ed33e 100644 --- a/plexers/decoder.c +++ b/plexers/decoder.c @@ -26,7 +26,7 @@ int main(int argc, char **argv) error(); } int selected = bin2dec(inputs[0]); - int num_outputs = pow(2,strlen(inputs[0])); + int num_outputs = 0x1 << strlen(inputs[0]); for(i = 0; i < num_outputs; i++) { if(i == selected) diff --git a/plexers/demultiplexor.c b/plexers/demultiplexor.c index 78547d1..ef52c2b 100644 --- a/plexers/demultiplexor.c +++ b/plexers/demultiplexor.c @@ -28,7 +28,7 @@ int main(int argc, char **argv) } int input_length = strlen(inputs[0]); int selected = bin2dec(inputs[1]); - int num_outputs = pow(2,strlen(inputs[1])); + int num_outputs = 0x1 << strlen(inputs[1]); for(i = 0; i < num_outputs; i++) { if(i == selected) diff --git a/plexers/encoder.c b/plexers/encoder.c index c0086f2..82bbf11 100644 --- a/plexers/encoder.c +++ b/plexers/encoder.c @@ -17,7 +17,7 @@ int check_length(char **inputs, uint8_t num_inputs) i = 0; while(max_value < num_inputs) { - max_value = pow(2,++i); + max_value = 0x1 << ++i; } return i; } diff --git a/plexers/multiplexor.c b/plexers/multiplexor.c index 1207657..f04d8dd 100644 --- a/plexers/multiplexor.c +++ b/plexers/multiplexor.c @@ -15,7 +15,7 @@ int check_length(char **inputs, uint8_t num_inputs) } } int sel_length = strlen(inputs[num_inputs-1]); - if(pow(2,sel_length) < (num_inputs-1)) + if((0x1 << sel_length) < (num_inputs-1)) { return -1; } diff --git a/todo b/todo index 3088924..c593e23 100644 --- a/todo +++ b/todo @@ -1,5 +1,3 @@ -bit selector - adder substractor multiplier