Commit 7aeec1aa320b44c835a301fb79ca52174c3cc01e

Authored by Imanol-Mikel Barba Sabariego
1 parent 131e732d

--no commit message

arith/Makefile 0 → 100644
  1 +TARGETS = adder
  2 +
  3 +.PHONY: all clean
  4 +
  5 +all: $(TARGETS)
  6 +
  7 +clean:
  8 + rm -rf $(TARGETS)
  9 +
  10 +adder:
  11 + gcc -I../common/ ../common/auxiliar.c adder.c -o adder
0 \ No newline at end of file 12 \ No newline at end of file
arith/adder.c 0 → 100644
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include "auxiliar.h"
  4 +
  5 +int check_length(char **inputs, uint8_t num_inputs)
  6 +{
  7 + int input_length = strlen(inputs[0]);
  8 + if((input_length != strlen(inputs[1])) || (strlen(inputs[2]) != 1))
  9 + {
  10 + return -1;
  11 + }
  12 + return input_length;
  13 +}
  14 +
  15 +int main(int argc, char **argv)
  16 +{
  17 + int i;
  18 + int pos;
  19 + if(argc != 2)
  20 + {
  21 + error();
  22 + }
  23 + char *input = argv[1];
  24 + if(!strcmp(input,"error"))
  25 + {
  26 + error();
  27 + }
  28 + char **inputs = comma_separate(input);
  29 + if(inputs == NULL)
  30 + {
  31 + error();
  32 + }
  33 + int num_inputs = num_occurrences(input,',');
  34 + if(num_inputs != 3)
  35 + {
  36 + error();
  37 + }
  38 + int input_length = check_length(inputs,num_inputs);
  39 + if(input_length == -1)
  40 + {
  41 + error();
  42 + }
  43 + int a,b,carry_in,c;
  44 + a = bin2dec(inputs[0]);
  45 + b = bin2dec(inputs[1]);
  46 + carry_in = bin2dec(inputs[2]);
  47 + c = a+b+carry_in;
  48 + char *result = dec2bin(c,input_length+1);
  49 + printf("%s,%c",result+1,result[0]);
  50 + free(result);
  51 + free_mem(inputs,num_inputs);
  52 + return 0;
  53 +}
common/auxiliar.c
@@ -4,7 +4,7 @@ char* substring(char *string, uint8_t start, uint8_t end) @@ -4,7 +4,7 @@ char* substring(char *string, uint8_t start, uint8_t end)
4 { 4 {
5 uint8_t size = end - start; 5 uint8_t size = end - start;
6 char *substr; 6 char *substr;
7 - substr = (char*) malloc(sizeof(char)*size + 1); 7 + substr = (char*) malloc(sizeof(char)*(size + 1));
8 memset(substr,0x00,size+1); 8 memset(substr,0x00,size+1);
9 strncpy(substr,string+start,size); 9 strncpy(substr,string+start,size);
10 return substr; 10 return substr;
@@ -60,7 +60,7 @@ char** comma_separate(char *input) @@ -60,7 +60,7 @@ char** comma_separate(char *input)
60 return NULL; 60 return NULL;
61 } 61 }
62 char **inputs; 62 char **inputs;
63 - inputs = malloc(sizeof(char*) * input_num); 63 + inputs = malloc(sizeof(char*) * (input_num+1));
64 for(i = 0; i < input_num; i++) 64 for(i = 0; i < input_num; i++)
65 { 65 {
66 next_pos += next_occurrence(input+pos,','); 66 next_pos += next_occurrence(input+pos,',');
@@ -128,14 +128,14 @@ int bin2dec(char *bin) @@ -128,14 +128,14 @@ int bin2dec(char *bin)
128 int num = 0; 128 int num = 0;
129 int length = strlen(bin); 129 int length = strlen(bin);
130 int pos = length - 1; 130 int pos = length - 1;
131 - char *aux = (char*) malloc(sizeof(char) * length); 131 + char *aux = (char*) malloc(sizeof(char) * (length+1));
132 strcpy(aux,bin); 132 strcpy(aux,bin);
133 normalize(&aux,1); 133 normalize(&aux,1);
134 for(i = 0; i < length; i++) 134 for(i = 0; i < length; i++)
135 { 135 {
136 if(aux[pos--]) 136 if(aux[pos--])
137 { 137 {
138 - num += pow(2,i); 138 + num += 0x1 << i;
139 } 139 }
140 } 140 }
141 free(aux); 141 free(aux);
@@ -144,7 +144,7 @@ int bin2dec(char *bin) @@ -144,7 +144,7 @@ int bin2dec(char *bin)
144 144
145 char* dec2bin(int dec, int length) 145 char* dec2bin(int dec, int length)
146 { 146 {
147 - char *bin = (char*) malloc(sizeof(char) * length + 1); 147 + char *bin = (char*) malloc(sizeof(char) * (length + 1));
148 int pos = length-1; 148 int pos = length-1;
149 int i; 149 int i;
150 for(i = 0; i < length; i++) 150 for(i = 0; i < length; i++)
@@ -160,4 +160,28 @@ char* dec2bin(int dec, int length) @@ -160,4 +160,28 @@ char* dec2bin(int dec, int length)
160 dec >>= 1; 160 dec >>= 1;
161 } 161 }
162 return bin; 162 return bin;
163 -}  
164 \ No newline at end of file 163 \ No newline at end of file
  164 +}
  165 +
  166 +
  167 +void expand_input(char **input, int required_length)
  168 +{
  169 + char *new_input = malloc(sizeof(char)*(required_length + 1));
  170 + memset(new_input,'0',required_length+1);
  171 + int offset = required_length - strlen(*input);
  172 + strcpy(new_input + offset,*input);
  173 + free(*input);
  174 + *input = new_input;
  175 +}
  176 +
  177 +int str2int(char *string)
  178 +{
  179 + int i;
  180 + int num = 0;
  181 + int mul = 1;
  182 + for(i = 0; i < strlen(string); i++)
  183 + {
  184 + num += mul*(string[i] - 48);
  185 + mul *= 10;
  186 + }
  187 + return num;
  188 +}
common/auxiliar.h
@@ -8,10 +8,12 @@ int next_occurrence(char *string, char character); @@ -8,10 +8,12 @@ int next_occurrence(char *string, char character);
8 int num_occurrences(char *string, char character); 8 int num_occurrences(char *string, char character);
9 int last_occurrence(char *string, char character); 9 int last_occurrence(char *string, char character);
10 char** comma_separate(char *input); 10 char** comma_separate(char *input);
  11 +int str2int(char *string);
11 12
12 void error(); 13 void error();
13 void free_mem(char **inputs, int num_inputs); 14 void free_mem(char **inputs, int num_inputs);
14 15
15 void normalize(char **inputs, int num_inputs); 16 void normalize(char **inputs, int num_inputs);
16 int bin2dec(char *bin); 17 int bin2dec(char *bin);
17 -char* dec2bin(int dec, int length);  
18 \ No newline at end of file 18 \ No newline at end of file
  19 +char* dec2bin(int dec, int length);
  20 +void expand_input(char **input, int required_length);
logic_gate/logic_gate.c
@@ -21,8 +21,8 @@ char* or(char **inputs, int num_inputs, int input_length) @@ -21,8 +21,8 @@ char* or(char **inputs, int num_inputs, int input_length)
21 { 21 {
22 int i; 22 int i;
23 int j; 23 int j;
24 - char *result = malloc(sizeof(char) * input_length + 1);  
25 - memset(result, 0x00, input_length); 24 + char *result = malloc(sizeof(char) * (input_length + 1));
  25 + memset(result, 0x00, input_length+1);
26 for(i = 0; i < input_length; i++) 26 for(i = 0; i < input_length; i++)
27 { 27 {
28 for(j = 0; j < num_inputs; j++) 28 for(j = 0; j < num_inputs; j++)
@@ -46,8 +46,8 @@ char* nor(char **inputs, int num_inputs, int input_length) @@ -46,8 +46,8 @@ char* nor(char **inputs, int num_inputs, int input_length)
46 { 46 {
47 int i; 47 int i;
48 int j; 48 int j;
49 - char *result = malloc(sizeof(char) * input_length + 1);  
50 - memset(result, 0x00, input_length); 49 + char *result = malloc(sizeof(char) * (input_length + 1));
  50 + memset(result, 0x00, input_length+1);
51 for(i = 0; i < input_length; i++) 51 for(i = 0; i < input_length; i++)
52 { 52 {
53 for(j = 0; j < num_inputs; j++) 53 for(j = 0; j < num_inputs; j++)
@@ -71,8 +71,8 @@ char* xor(char **inputs, int num_inputs, int input_length) @@ -71,8 +71,8 @@ char* xor(char **inputs, int num_inputs, int input_length)
71 { 71 {
72 int i; 72 int i;
73 int j; 73 int j;
74 - char *result = malloc(sizeof(char) * input_length + 1);  
75 - memset(result, 0x00, input_length); 74 + char *result = malloc(sizeof(char) * (input_length + 1));
  75 + memset(result, 0x00, input_length+1);
76 for(i = 0; i < input_length; i++) 76 for(i = 0; i < input_length; i++)
77 { 77 {
78 for(j = 0; j < num_inputs; j++) 78 for(j = 0; j < num_inputs; j++)
@@ -95,8 +95,8 @@ char* xnor(char **inputs, int num_inputs, int input_length) @@ -95,8 +95,8 @@ char* xnor(char **inputs, int num_inputs, int input_length)
95 { 95 {
96 int i; 96 int i;
97 int j; 97 int j;
98 - char *result = malloc(sizeof(char) * input_length + 1);  
99 - memset(result, 0x00, input_length); 98 + char *result = malloc(sizeof(char) * (input_length + 1));
  99 + memset(result, 0x00, input_length+1);
100 for(i = 0; i < input_length; i++) 100 for(i = 0; i < input_length; i++)
101 { 101 {
102 for(j = 0; j < num_inputs; j++) 102 for(j = 0; j < num_inputs; j++)
@@ -119,8 +119,8 @@ char* and(char **inputs, int num_inputs, int input_length) @@ -119,8 +119,8 @@ char* and(char **inputs, int num_inputs, int input_length)
119 { 119 {
120 int i; 120 int i;
121 int j; 121 int j;
122 - char *result = malloc(sizeof(char) * input_length + 1);  
123 - memset(result, 0x01, input_length); 122 + char *result = malloc(sizeof(char) * (input_length + 1));
  123 + memset(result, 0x01, input_length+1);
124 for(i = 0; i < input_length; i++) 124 for(i = 0; i < input_length; i++)
125 { 125 {
126 for(j = 0; j < num_inputs; j++) 126 for(j = 0; j < num_inputs; j++)
@@ -143,8 +143,8 @@ char* nand(char **inputs, int num_inputs, int input_length) @@ -143,8 +143,8 @@ char* nand(char **inputs, int num_inputs, int input_length)
143 { 143 {
144 int i; 144 int i;
145 int j; 145 int j;
146 - char *result = malloc(sizeof(char) * input_length + 1);  
147 - memset(result, 0x01, input_length); 146 + char *result = malloc(sizeof(char) * (input_length + 1));
  147 + memset(result, 0x01, input_length+1);
148 for(i = 0; i < input_length; i++) 148 for(i = 0; i < input_length; i++)
149 { 149 {
150 for(j = 0; j < num_inputs; j++) 150 for(j = 0; j < num_inputs; j++)
@@ -167,8 +167,8 @@ char* not(char **inputs, int num_inputs, int input_length) @@ -167,8 +167,8 @@ char* not(char **inputs, int num_inputs, int input_length)
167 { 167 {
168 int i; 168 int i;
169 int j; 169 int j;
170 - char *result = malloc(sizeof(char) * input_length + 1);  
171 - memset(result, 0x01, input_length); 170 + char *result = malloc(sizeof(char) * (input_length + 1));
  171 + memset(result, 0x01, input_length+1);
172 for(i = 0; i < input_length; i++) 172 for(i = 0; i < input_length; i++)
173 { 173 {
174 for(j = 0; j < num_inputs; j++) 174 for(j = 0; j < num_inputs; j++)
plexers/Makefile
1 -TARGETS = multiplexor demultiplexor encoder decoder 1 +TARGETS = multiplexor demultiplexor encoder decoder bit_selector
2 2
3 .PHONY: all clean 3 .PHONY: all clean
4 4
@@ -8,12 +8,14 @@ clean: @@ -8,12 +8,14 @@ clean:
8 rm -rf $(TARGETS) 8 rm -rf $(TARGETS)
9 9
10 multiplexor: 10 multiplexor:
11 - gcc -I../common/ ../common/auxiliar.c multiplexor.c -o multiplexor -lm 11 + gcc -I../common/ ../common/auxiliar.c multiplexor.c -o multiplexor
12 12
13 demultiplexor: 13 demultiplexor:
14 - gcc -I../common/ ../common/auxiliar.c demultiplexor.c -o demultiplexor -lm 14 + gcc -I../common/ ../common/auxiliar.c demultiplexor.c -o demultiplexor
15 15
16 encoder: 16 encoder:
17 - gcc -I../common/ ../common/auxiliar.c encoder.c -o encoder -lm 17 + gcc -I../common/ ../common/auxiliar.c encoder.c -o encoder
18 decoder: 18 decoder:
19 - gcc -I../common/ ../common/auxiliar.c decoder.c -o decoder -lm 19 + gcc -I../common/ ../common/auxiliar.c decoder.c -o decoder
  20 +bit_selector:
  21 + gcc -I../common/ ../common/auxiliar.c bit_selector.c -o bit_selector
plexers/bit_selector.c 0 → 100644
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include "auxiliar.h"
  4 +#include <math.h>
  5 +
  6 +int check_length(char **inputs, int num_inputs, int output_length)
  7 +{
  8 + int input_length = strlen(inputs[0]);
  9 + int selector_length = strlen(inputs[1]);
  10 + int sel_combinations = 0x1 << selector_length;
  11 + int num_groups = ceil(((float)input_length)/output_length);
  12 + int required_combinations = 1;
  13 + int i = 0;
  14 + while(required_combinations < num_groups)
  15 + {
  16 + required_combinations = 0x1 << ++i;
  17 + }
  18 + if(sel_combinations < required_combinations)
  19 + {
  20 + return -1;
  21 + }
  22 + return required_combinations * output_length;
  23 +}
  24 +
  25 +int main(int argc, char **argv)
  26 +{
  27 + int i;
  28 + int j;
  29 + if(argc != 3)
  30 + {
  31 + error();
  32 + }
  33 + char *input = argv[1];
  34 + int output_length = str2int(argv[2]);
  35 + if(!strcmp(input,"error"))
  36 + {
  37 + error();
  38 + }
  39 + char **inputs = comma_separate(input);
  40 + if(inputs == NULL)
  41 + {
  42 + error();
  43 + }
  44 + int num_inputs = num_occurrences(input,',');
  45 + if(num_inputs != 2)
  46 + {
  47 + error();
  48 + }
  49 + int input_length = strlen(inputs[0]);
  50 + int required_length = check_length(inputs,num_inputs,output_length);
  51 + if(required_length == -1)
  52 + {
  53 + error();
  54 + }
  55 + if(input_length != required_length)
  56 + {
  57 + expand_input(&inputs[0],required_length);
  58 + }
  59 + char *result = malloc(sizeof(char) * (output_length + 1));
  60 + memset(result,0x00,output_length+1);
  61 + int selected = bin2dec(inputs[1]);
  62 + int pos = 0;
  63 + for(i = (selected+1)*(output_length)-1; i >= selected*output_length; i--)
  64 + {
  65 + result[pos++] = inputs[0][required_length-(i+1)];
  66 + }
  67 + printf("%s",result);
  68 + free(result);
  69 + free_mem(inputs,num_inputs);
  70 + return 0;
  71 +}
  72 +
plexers/decoder.c
@@ -26,7 +26,7 @@ int main(int argc, char **argv) @@ -26,7 +26,7 @@ int main(int argc, char **argv)
26 error(); 26 error();
27 } 27 }
28 int selected = bin2dec(inputs[0]); 28 int selected = bin2dec(inputs[0]);
29 - int num_outputs = pow(2,strlen(inputs[0])); 29 + int num_outputs = 0x1 << strlen(inputs[0]);
30 for(i = 0; i < num_outputs; i++) 30 for(i = 0; i < num_outputs; i++)
31 { 31 {
32 if(i == selected) 32 if(i == selected)
plexers/demultiplexor.c
@@ -28,7 +28,7 @@ int main(int argc, char **argv) @@ -28,7 +28,7 @@ int main(int argc, char **argv)
28 } 28 }
29 int input_length = strlen(inputs[0]); 29 int input_length = strlen(inputs[0]);
30 int selected = bin2dec(inputs[1]); 30 int selected = bin2dec(inputs[1]);
31 - int num_outputs = pow(2,strlen(inputs[1])); 31 + int num_outputs = 0x1 << strlen(inputs[1]);
32 for(i = 0; i < num_outputs; i++) 32 for(i = 0; i < num_outputs; i++)
33 { 33 {
34 if(i == selected) 34 if(i == selected)
plexers/encoder.c
@@ -17,7 +17,7 @@ int check_length(char **inputs, uint8_t num_inputs) @@ -17,7 +17,7 @@ int check_length(char **inputs, uint8_t num_inputs)
17 i = 0; 17 i = 0;
18 while(max_value < num_inputs) 18 while(max_value < num_inputs)
19 { 19 {
20 - max_value = pow(2,++i); 20 + max_value = 0x1 << ++i;
21 } 21 }
22 return i; 22 return i;
23 } 23 }
plexers/multiplexor.c
@@ -15,7 +15,7 @@ int check_length(char **inputs, uint8_t num_inputs) @@ -15,7 +15,7 @@ int check_length(char **inputs, uint8_t num_inputs)
15 } 15 }
16 } 16 }
17 int sel_length = strlen(inputs[num_inputs-1]); 17 int sel_length = strlen(inputs[num_inputs-1]);
18 - if(pow(2,sel_length) < (num_inputs-1)) 18 + if((0x1 << sel_length) < (num_inputs-1))
19 { 19 {
20 return -1; 20 return -1;
21 } 21 }
1 -bit selector  
2 -  
3 adder 1 adder
4 substractor 2 substractor
5 multiplier 3 multiplier