Commit 61a62015a3ba453ca9e73078e9d1e58eaaeefc8e

Authored by Imanol-Mikel Barba Sabariego
1 parent 35b99e06

--no commit message

arith/Makefile
1   -TARGETS = adder substractor
  1 +TARGETS = adder substractor multiplier divider negator comparator
2 2  
3 3 .PHONY: all clean
4 4  
... ... @@ -10,4 +10,12 @@ clean:
10 10 adder:
11 11 gcc -I../common/ ../common/auxiliar.c adder.c -o adder
12 12 substractor:
13   - gcc -I../common/ ../common/auxiliar.c substractor.c -o substractor
14 13 \ No newline at end of file
  14 + gcc -I../common/ ../common/auxiliar.c substractor.c -o substractor
  15 +multiplier:
  16 + gcc -I../common/ ../common/auxiliar.c multiplier.c -o multiplier
  17 +divider:
  18 + gcc -I../common/ ../common/auxiliar.c divider.c -o divider
  19 +negator:
  20 + gcc -I../common/ ../common/auxiliar.c negator.c -o negator
  21 +comparator:
  22 + gcc -I../common/ ../common/auxiliar.c comparator.c -o comparator
15 23 \ No newline at end of file
... ...
arith/comparator.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]))
  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 != 2)
  35 + {
  36 + error();
  37 + }
  38 + int input_length = check_length(inputs,num_inputs);
  39 + int a,b;
  40 + a = bin2dec(inputs[0]);
  41 + b = bin2dec(inputs[1]);
  42 + if(a > b)
  43 + {
  44 + printf("1,0,0");
  45 + }
  46 + else if(a < b)
  47 + {
  48 + printf("0,0,1");
  49 + }
  50 + else if(a == b)
  51 + {
  52 + printf("0,1,0");
  53 + }
  54 + free_mem(inputs,num_inputs);
  55 + return 0;
  56 +}
... ...
arith/divider.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 + int i;
  9 + for(i = 1; i < 3; i++)
  10 + {
  11 + if(input_length != strlen(inputs[i]))
  12 + {
  13 + return -1;
  14 + }
  15 + }
  16 + return input_length;
  17 +}
  18 +
  19 +int main(int argc, char **argv)
  20 +{
  21 + int i;
  22 + int pos;
  23 + if(argc != 2)
  24 + {
  25 + error();
  26 + }
  27 + char *input = argv[1];
  28 + if(!strcmp(input,"error"))
  29 + {
  30 + error();
  31 + }
  32 + char **inputs = comma_separate(input);
  33 + if(inputs == NULL)
  34 + {
  35 + error();
  36 + }
  37 + int num_inputs = num_occurrences(input,',');
  38 + if(num_inputs != 3)
  39 + {
  40 + error();
  41 + }
  42 + int input_length = check_length(inputs,num_inputs);
  43 + if(input_length == -1)
  44 + {
  45 + error();
  46 + }
  47 + int a,b,c,rem;
  48 + char *upper = inputs[0];
  49 + char *lower = inputs[1];
  50 + char *op = (char*) malloc(sizeof(char)*(input_length+1));
  51 + memset(op,0x00,input_length+1);
  52 + strcpy(op,upper);
  53 + strcat(op,lower);
  54 + a = bin2dec(op);
  55 + b = bin2dec(inputs[2]);
  56 + c = a/b;
  57 + rem = a%b;
  58 + char *result = dec2bin(c,input_length);
  59 + char *rem_bin = dec2bin(rem,input_length);
  60 + printf("%s,%s",result,rem_bin);
  61 + free(result);
  62 + free(rem_bin);
  63 + free(op);
  64 + free_mem(inputs,num_inputs);
  65 + return 0;
  66 +}
... ...
arith/multiplier.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 + int i;
  9 + for(i = 1; i < 3; i++)
  10 + {
  11 + if(input_length != strlen(inputs[i]))
  12 + {
  13 + return -1;
  14 + }
  15 + }
  16 + return input_length;
  17 +}
  18 +
  19 +int main(int argc, char **argv)
  20 +{
  21 + int i;
  22 + int pos;
  23 + if(argc != 2)
  24 + {
  25 + error();
  26 + }
  27 + char *input = argv[1];
  28 + if(!strcmp(input,"error"))
  29 + {
  30 + error();
  31 + }
  32 + char **inputs = comma_separate(input);
  33 + if(inputs == NULL)
  34 + {
  35 + error();
  36 + }
  37 + int num_inputs = num_occurrences(input,',');
  38 + if(num_inputs != 3)
  39 + {
  40 + error();
  41 + }
  42 + int input_length = check_length(inputs,num_inputs);
  43 + if(input_length == -1)
  44 + {
  45 + error();
  46 + }
  47 + int a,b,carry_in,c;
  48 + a = bin2dec(inputs[0]);
  49 + b = bin2dec(inputs[1]);
  50 + carry_in = bin2dec(inputs[2]);
  51 + c = (a*b)+carry_in;
  52 + char *result = dec2bin(c,input_length*2);
  53 + printf("%s,%.*s",result+input_length,input_length,result);
  54 + free(result);
  55 + free_mem(inputs,num_inputs);
  56 + return 0;
  57 +}
... ...
arith/negator.c 0 → 100644
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include "auxiliar.h"
  4 +
  5 +int main(int argc, char **argv)
  6 +{
  7 + int i;
  8 + int pos;
  9 + if(argc != 2)
  10 + {
  11 + error();
  12 + }
  13 + char *input = argv[1];
  14 + if(!strcmp(input,"error"))
  15 + {
  16 + error();
  17 + }
  18 + char **inputs = comma_separate(input);
  19 + if(inputs == NULL)
  20 + {
  21 + error();
  22 + }
  23 + int num_inputs = num_occurrences(input,',');
  24 + if(num_inputs != 1)
  25 + {
  26 + error();
  27 + }
  28 + int input_length = strlen(inputs[0]);
  29 + int num = bin2dec(inputs[0]);
  30 + num *= -1;
  31 + char *result = dec2bin(num,input_length);
  32 + printf("%s",result);
  33 + free(result);
  34 + free_mem(inputs,num_inputs);
  35 + return 0;
  36 +}
... ...
arith/substractor.c
... ... @@ -14,7 +14,7 @@ int check_length(char **inputs, uint8_t num_inputs)
14 14  
15 15 int main(int argc, char **argv)
16 16 {
17   - /*int i;
  17 + int i;
18 18 int pos;
19 19 if(argc != 2)
20 20 {
... ... @@ -54,7 +54,5 @@ int main(int argc, char **argv)
54 54 printf("%s,%c",result,borrow_out);
55 55 free(result);
56 56 free_mem(inputs,num_inputs);
57   - return 0;*/
58   - printf("%d",bin2dec("11000000"));
59 57 return 0;
60 58 }
... ...
common/auxiliar.c
... ... @@ -60,7 +60,7 @@ char** comma_separate(char *input)
60 60 return NULL;
61 61 }
62 62 char **inputs;
63   - inputs = malloc(sizeof(char*) * (input_num+1));
  63 + inputs = (char**) malloc(sizeof(char*) * (input_num+1));
64 64 for(i = 0; i < input_num; i++)
65 65 {
66 66 next_pos += next_occurrence(input+pos,',');
... ... @@ -130,6 +130,7 @@ int bin2dec(char *bin)
130 130 int length = strlen(bin);
131 131 int pos = length - 1;
132 132 char *aux = (char*) malloc(sizeof(char) * (length+1));
  133 + memset(aux,0x00,length+1);
133 134 strcpy(aux,bin);
134 135 if(aux[0] == '1')
135 136 {
... ... @@ -152,6 +153,7 @@ int bin2dec(char *bin)
152 153 char* dec2bin(int dec, int length)
153 154 {
154 155 char *bin = (char*) malloc(sizeof(char) * (length + 1));
  156 + memset(bin,0x00,length+1);
155 157 int pos = length-1;
156 158 int i;
157 159 for(i = 0; i < length; i++)
... ... @@ -172,7 +174,8 @@ char* dec2bin(int dec, int length)
172 174  
173 175 void expand_input(char **input, int required_length)
174 176 {
175   - char *new_input = malloc(sizeof(char)*(required_length + 1));
  177 + char *new_input = (char*) malloc(sizeof(char)*(required_length + 1));
  178 + memset(new_input,0x00,required_length + 1);
176 179 memset(new_input,'0',required_length+1);
177 180 int offset = required_length - strlen(*input);
178 181 strcpy(new_input + offset,*input);
... ...
logic_gate/logic_gate.c
... ... @@ -21,7 +21,7 @@ char* or(char **inputs, int num_inputs, int input_length)
21 21 {
22 22 int i;
23 23 int j;
24   - char *result = malloc(sizeof(char) * (input_length + 1));
  24 + char *result = (char*) malloc(sizeof(char) * (input_length + 1));
25 25 memset(result, 0x00, input_length+1);
26 26 for(i = 0; i < input_length; i++)
27 27 {
... ... @@ -46,7 +46,7 @@ char* nor(char **inputs, int num_inputs, int input_length)
46 46 {
47 47 int i;
48 48 int j;
49   - char *result = malloc(sizeof(char) * (input_length + 1));
  49 + char *result = (char*) malloc(sizeof(char) * (input_length + 1));
50 50 memset(result, 0x00, input_length+1);
51 51 for(i = 0; i < input_length; i++)
52 52 {
... ... @@ -71,7 +71,7 @@ char* xor(char **inputs, int num_inputs, int input_length)
71 71 {
72 72 int i;
73 73 int j;
74   - char *result = malloc(sizeof(char) * (input_length + 1));
  74 + char *result = (char*) malloc(sizeof(char) * (input_length + 1));
75 75 memset(result, 0x00, input_length+1);
76 76 for(i = 0; i < input_length; i++)
77 77 {
... ... @@ -95,7 +95,7 @@ char* xnor(char **inputs, int num_inputs, int input_length)
95 95 {
96 96 int i;
97 97 int j;
98   - char *result = malloc(sizeof(char) * (input_length + 1));
  98 + char *result = (char*) malloc(sizeof(char) * (input_length + 1));
99 99 memset(result, 0x00, input_length+1);
100 100 for(i = 0; i < input_length; i++)
101 101 {
... ... @@ -119,7 +119,7 @@ char* and(char **inputs, int num_inputs, int input_length)
119 119 {
120 120 int i;
121 121 int j;
122   - char *result = malloc(sizeof(char) * (input_length + 1));
  122 + char *result = (char*) malloc(sizeof(char) * (input_length + 1));
123 123 memset(result, 0x01, input_length+1);
124 124 for(i = 0; i < input_length; i++)
125 125 {
... ... @@ -143,7 +143,7 @@ char* nand(char **inputs, int num_inputs, int input_length)
143 143 {
144 144 int i;
145 145 int j;
146   - char *result = malloc(sizeof(char) * (input_length + 1));
  146 + char *result = (char*) malloc(sizeof(char) * (input_length + 1));
147 147 memset(result, 0x01, input_length+1);
148 148 for(i = 0; i < input_length; i++)
149 149 {
... ... @@ -167,7 +167,7 @@ char* not(char **inputs, int num_inputs, int input_length)
167 167 {
168 168 int i;
169 169 int j;
170   - char *result = malloc(sizeof(char) * (input_length + 1));
  170 + char *result = (char*) malloc(sizeof(char) * (input_length + 1));
171 171 memset(result, 0x01, input_length+1);
172 172 for(i = 0; i < input_length; i++)
173 173 {
... ...
plexers/bit_selector.c
... ... @@ -56,7 +56,7 @@ int main(int argc, char **argv)
56 56 {
57 57 expand_input(&inputs[0],required_length);
58 58 }
59   - char *result = malloc(sizeof(char) * (output_length + 1));
  59 + char *result = (char*) malloc(sizeof(char) * (output_length + 1));
60 60 memset(result,0x00,output_length+1);
61 61 int selected = bin2dec(inputs[1]);
62 62 int pos = 0;
... ...
1   -adder
2   -substractor
3   -multiplier
4   -divider
5   -negator
6   -comparator
7 1 shifter
8 2 bit adder
9 3 bit finder
... ...