Commit 8a0a82367b7ccddba59f6e54f277b2b5468bf674

Authored by Imanol-Mikel Barba Sabariego
0 parents

Initial import

common/auxiliar.c 0 → 100644
  1 +++ a/common/auxiliar.c
  1 +#include "auxiliar.h"
  2 +
  3 +char* substring(char *string, uint8_t start, uint8_t end)
  4 +{
  5 + uint8_t size = end - start;
  6 + char *substr;
  7 + substr = (char*) malloc(sizeof(char)*size + 1);
  8 + memset(substr,0x00,size+1);
  9 + strncpy(substr,string+start,size);
  10 + return substr;
  11 +}
  12 +
  13 +int next_occurrence(char *string, char character)
  14 +{
  15 + int i;
  16 + int pos = -1;
  17 + for(i = 0; i < strlen(string); i++)
  18 + {
  19 + if(string[i] == character)
  20 + {
  21 + pos = i;
  22 + break;
  23 + }
  24 + }
  25 + return pos;
  26 +}
  27 +
  28 +int num_occurrences(char *string, char character)
  29 +{
  30 + int i;
  31 + int count = 0;
  32 + for(i = 0; i < strlen(string); i++)
  33 + {
  34 + if(string[i] == character)
  35 + {
  36 + count++;
  37 + }
  38 + }
  39 + if(!count)
  40 + {
  41 + return -1;
  42 + }
  43 + return count;
  44 +}
  45 +
  46 +void error()
  47 +{
  48 + printf("error");
  49 + exit(-1);
  50 +}
  51 +
  52 +char** comma_separate(char *input)
  53 +{
  54 + int i;
  55 + int next_pos = 0;
  56 + int pos = 0;
  57 + int input_num = num_occurrences(input,',');
  58 + if(input_num <= 0)
  59 + {
  60 + return NULL;
  61 + }
  62 + char **inputs;
  63 + inputs = malloc(sizeof(char*) * input_num);
  64 + for(i = 0; i < input_num; i++)
  65 + {
  66 + next_pos += next_occurrence(input+pos,',');
  67 + if(next_pos == -1)
  68 + {
  69 + return NULL;
  70 + }
  71 + inputs[i] = substring(input,pos,next_pos);
  72 + next_pos++;
  73 + pos = next_pos;
  74 + }
  75 + return inputs;
  76 +}
  77 +
  78 +void free_mem(char **inputs, int num_inputs)
  79 +{
  80 + int i;
  81 + for(i = 0; i < num_inputs; i++)
  82 + {
  83 + free(inputs[i]);
  84 + }
  85 + free(inputs);
  86 +}
  87 +
  88 +int last_occurrence(char *string, char character)
  89 +{
  90 + int i;
  91 + int pos = -1;
  92 + for(i = 0; i < strlen(string); i++)
  93 + {
  94 + if(string[i] == character)
  95 + {
  96 + pos = i;
  97 + }
  98 + }
  99 + return pos;
  100 +}
  101 +
  102 +void normalize(char **inputs, int num_inputs)
  103 +{
  104 + int i;
  105 + int j;
  106 + int length;
  107 +
  108 + for(i = 0; i < num_inputs; i++)
  109 + {
  110 + length = strlen(inputs[i]);
  111 + for(j = 0; j < length; j++)
  112 + {
  113 + if(inputs[i][j] == '0')
  114 + {
  115 + inputs[i][j] = 0;
  116 + }
  117 + else
  118 + {
  119 + inputs[i][j] = 1;
  120 + }
  121 + }
  122 + }
  123 +}
0 124 \ No newline at end of file
... ...
common/auxiliar.h 0 → 100644
  1 +++ a/common/auxiliar.h
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <stdlib.h>
  4 +#include <stdint.h>
  5 +
  6 +char* substring(char *string, uint8_t start, uint8_t end);
  7 +int next_occurrence(char *string, char character);
  8 +int num_occurrences(char *string, char character);
  9 +int last_occurrence(char *string, char character);
  10 +char** comma_separate(char *input);
  11 +
  12 +void error();
  13 +void free_mem(char **inputs, int num_inputs);
  14 +
  15 +void normalize(char **inputs, int num_inputs);
... ...
examples/multiplexor2.sh 0 → 100755
  1 +++ a/examples/multiplexor2.sh
  1 +#!/bin/bash
  2 +
  3 +INPUT="$1"
  4 +A=`echo "$INPUT" | cut -d',' -f 1`
  5 +B=`echo "$INPUT" | cut -d',' -f 2`
  6 +S=`echo "$INPUT" | cut -d',' -f 3`
  7 +
  8 +N1=`./not $S,`
  9 +N2=`./and $N1,$A,`
  10 +N3=`./and $S,$B,`
  11 +Z=`./or $N2,$N3,`
  12 +
  13 +echo $Z
... ...
examples/multiplexor4.sh 0 → 100755
  1 +++ a/examples/multiplexor4.sh
  1 +#!/bin/bash
  2 +
  3 +INPUT="$1"
  4 +
  5 +A=`echo "$INPUT" | cut -d',' -f 1`
  6 +B=`echo "$INPUT" | cut -d',' -f 2`
  7 +C=`echo "$INPUT" | cut -d',' -f 3`
  8 +D=`echo "$INPUT" | cut -d',' -f 4`
  9 +S=`echo "$INPUT" | cut -d',' -f 5`
  10 +
  11 +S1=`echo "$S" | cut -b1`
  12 +S2=`echo "$S" | cut -b2`
  13 +
  14 +N1=`./not $S1,`
  15 +N2=`./not $S2,`
  16 +N3=`./and $D,$S1,`
  17 +N4=`./and $C,$N1,`
  18 +N5=`./and $B,$S1,`
  19 +N6=`./and $A,$N1,`
  20 +N7=`./and $N3,$S2,`
  21 +N8=`./and $N4,$S2,`
  22 +N9=`./and $N5,$N2,`
  23 +N10=`./and $N6,$N2,`
  24 +N11=`./or $N7,$N8,`
  25 +N12=`./or $N9,$N10,`
  26 +Z=`./or $N11,$N12,`
  27 +
  28 +echo $Z
... ...
logic_gate/Makefile 0 → 100644
  1 +++ a/logic_gate/Makefile
  1 +SYMLINKS = or nor xor xnor not and nand
  2 +TARGETS = logic_gate
  3 +
  4 +.PHONY: all clean
  5 +
  6 +all: $(TARGETS) symlinks
  7 +
  8 +clean:
  9 + rm -rf $(TARGETS) $(SYMLINKS)
  10 +
  11 +symlinks: logic_gate
  12 + ln -s logic_gate or
  13 + ln -s logic_gate nor
  14 + ln -s logic_gate xor
  15 + ln -s logic_gate xnor
  16 + ln -s logic_gate not
  17 + ln -s logic_gate and
  18 + ln -s logic_gate nand
  19 +
  20 +logic_gate:
  21 + gcc -I../common/ ../common/auxiliar.c logic_gate.c -o logic_gate
... ...
logic_gate/logic_gate.c 0 → 100644
  1 +++ a/logic_gate/logic_gate.c
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include <stdint.h>
  4 +#include "auxiliar.h"
  5 +
  6 +int check_length(char **inputs, uint8_t num_inputs)
  7 +{
  8 + int i;
  9 + int length = strlen(inputs[0]);
  10 + for(i = 1; i < num_inputs; i++)
  11 + {
  12 + if(strlen(inputs[i]) != length)
  13 + {
  14 + return -1;
  15 + }
  16 + }
  17 + return length;
  18 +}
  19 +
  20 +char* or(char **inputs, int num_inputs, int input_length)
  21 +{
  22 + int i;
  23 + int j;
  24 + char *result = malloc(sizeof(char) * input_length + 1);
  25 + memset(result, 0x00, input_length);
  26 + for(i = 0; i < input_length; i++)
  27 + {
  28 + for(j = 0; j < num_inputs; j++)
  29 + {
  30 + result[i] |= inputs[j][i];
  31 + }
  32 + if(result[i])
  33 + {
  34 + result[i] = '1';
  35 + }
  36 + else
  37 + {
  38 + result[i] = '0';
  39 + }
  40 +
  41 + }
  42 + return result;
  43 +}
  44 +
  45 +char* nor(char **inputs, int num_inputs, int input_length)
  46 +{
  47 + int i;
  48 + int j;
  49 + char *result = malloc(sizeof(char) * input_length + 1);
  50 + memset(result, 0x00, input_length);
  51 + for(i = 0; i < input_length; i++)
  52 + {
  53 + for(j = 0; j < num_inputs; j++)
  54 + {
  55 + result[i] |= inputs[j][i];
  56 +
  57 + }
  58 + if(result[i])
  59 + {
  60 + result[i] = '0';
  61 + }
  62 + else
  63 + {
  64 + result[i] = '1';
  65 + }
  66 + }
  67 + return result;
  68 +}
  69 +
  70 +char* xor(char **inputs, int num_inputs, int input_length)
  71 +{
  72 + int i;
  73 + int j;
  74 + char *result = malloc(sizeof(char) * input_length + 1);
  75 + memset(result, 0x00, input_length);
  76 + for(i = 0; i < input_length; i++)
  77 + {
  78 + for(j = 0; j < num_inputs; j++)
  79 + {
  80 + result[i] ^= inputs[j][i];
  81 + }
  82 + if(result[i])
  83 + {
  84 + result[i] = '1';
  85 + }
  86 + else
  87 + {
  88 + result[i] = '0';
  89 + }
  90 + }
  91 + return result;
  92 +}
  93 +
  94 +char* xnor(char **inputs, int num_inputs, int input_length)
  95 +{
  96 + int i;
  97 + int j;
  98 + char *result = malloc(sizeof(char) * input_length + 1);
  99 + memset(result, 0x00, input_length);
  100 + for(i = 0; i < input_length; i++)
  101 + {
  102 + for(j = 0; j < num_inputs; j++)
  103 + {
  104 + result[i] ^= inputs[j][i];
  105 + }
  106 + if(result[i])
  107 + {
  108 + result[i] = '0';
  109 + }
  110 + else
  111 + {
  112 + result[i] = '1';
  113 + }
  114 + }
  115 + return result;
  116 +}
  117 +
  118 +char* and(char **inputs, int num_inputs, int input_length)
  119 +{
  120 + int i;
  121 + int j;
  122 + char *result = malloc(sizeof(char) * input_length + 1);
  123 + memset(result, 0x01, input_length);
  124 + for(i = 0; i < input_length; i++)
  125 + {
  126 + for(j = 0; j < num_inputs; j++)
  127 + {
  128 + result[i] &= inputs[j][i];
  129 + }
  130 + if(result[i])
  131 + {
  132 + result[i] = '1';
  133 + }
  134 + else
  135 + {
  136 + result[i] = '0';
  137 + }
  138 + }
  139 + return result;
  140 +}
  141 +
  142 +char* nand(char **inputs, int num_inputs, int input_length)
  143 +{
  144 + int i;
  145 + int j;
  146 + char *result = malloc(sizeof(char) * input_length + 1);
  147 + memset(result, 0x01, input_length);
  148 + for(i = 0; i < input_length; i++)
  149 + {
  150 + for(j = 0; j < num_inputs; j++)
  151 + {
  152 + result[i] &= inputs[j][i];
  153 + }
  154 + if(result[i])
  155 + {
  156 + result[i] = '0';
  157 + }
  158 + else
  159 + {
  160 + result[i] = '1';
  161 + }
  162 + }
  163 + return result;
  164 +}
  165 +
  166 +char* not(char **inputs, int num_inputs, int input_length)
  167 +{
  168 + int i;
  169 + int j;
  170 + char *result = malloc(sizeof(char) * input_length + 1);
  171 + memset(result, 0x01, input_length);
  172 + for(i = 0; i < input_length; i++)
  173 + {
  174 + for(j = 0; j < num_inputs; j++)
  175 + {
  176 + result[i] = inputs[j][i];
  177 + }
  178 + if(result[i])
  179 + {
  180 + result[i] = '0';
  181 + }
  182 + else
  183 + {
  184 + result[i] = '1';
  185 + }
  186 + }
  187 + return result;
  188 +}
  189 +
  190 +int main(int argc, char **argv)
  191 +{
  192 + if(argc != 2)
  193 + {
  194 + error();
  195 + }
  196 + char *input = argv[1];
  197 + if(!strcmp(input,"error"))
  198 + {
  199 + error();
  200 + }
  201 + char **inputs = comma_separate(input);
  202 + if(inputs == NULL)
  203 + {
  204 + error();
  205 + }
  206 + int num_inputs = num_occurrences(input,',');
  207 + int input_length = check_length(inputs,num_inputs);
  208 + if(input_length == -1)
  209 + {
  210 + error();
  211 + }
  212 + normalize(inputs,num_inputs);
  213 + char *result;
  214 + char *command_name;
  215 + if(last_occurrence(argv[0],'/') == -1)
  216 + {
  217 + command_name = argv[0];
  218 + }
  219 + else
  220 + {
  221 + command_name = substring(argv[0],last_occurrence(argv[0],'/')+1,strlen(argv[0]));
  222 + }
  223 + if(!strcmp(command_name,"or"))
  224 + {
  225 + result = or(inputs,num_inputs,input_length);
  226 + }
  227 + else if(!strcmp(command_name,"nor"))
  228 + {
  229 + result = nor(inputs,num_inputs,input_length);
  230 + }
  231 + else if(!strcmp(command_name,"xor"))
  232 + {
  233 + result = xor(inputs,num_inputs,input_length);
  234 + }
  235 + else if(!strcmp(command_name,"xnor"))
  236 + {
  237 + result = xnor(inputs,num_inputs,input_length);
  238 + }
  239 + else if(!strcmp(command_name,"and"))
  240 + {
  241 + result = and(inputs,num_inputs,input_length);
  242 + }
  243 + else if(!strcmp(command_name,"nand"))
  244 + {
  245 + result = nand(inputs,num_inputs,input_length);
  246 + }
  247 + else if(!strcmp(command_name,"not"))
  248 + {
  249 + result = not(inputs,num_inputs,input_length);
  250 + }
  251 + else
  252 + {
  253 + error();
  254 + }
  255 + free_mem(inputs,num_inputs);
  256 + printf("%s",result);
  257 + free(result);
  258 + return 0;
  259 +}
... ...
plexers/Makefile 0 → 100644
  1 +++ a/plexers/Makefile
  1 +TARGETS = multiplexor
  2 +
  3 +.PHONY: all clean
  4 +
  5 +all: $(TARGETS)
  6 +
  7 +clean:
  8 + rm -rf $(TARGETS)
  9 +
  10 +multiplexor:
  11 + gcc -I../common/ ../common/auxiliar.c multiplexor.c -o multiplexor -lm
... ...
plexers/multiplexor 0 → 100755
No preview for this file type
plexers/multiplexor.c 0 → 100644
  1 +++ a/plexers/multiplexor.c
  1 +#include <stdio.h>
  2 +#include <string.h>
  3 +#include "auxiliar.h"
  4 +#include <math.h>
  5 +
  6 +int bin2dec(char *bin)
  7 +{
  8 + int i;
  9 + int num = 0;
  10 + int length = strlen(bin);
  11 + int pos = length - 1;
  12 + char *aux = (char*) malloc(sizeof(char) * length);
  13 + strcpy(aux,bin);
  14 + normalize(&aux,1);
  15 + for(i = 0; i < length; i++)
  16 + {
  17 + if(aux[pos--])
  18 + {
  19 + num += pow(2,i);
  20 + }
  21 + }
  22 + free(aux);
  23 + return num;
  24 +}
  25 +
  26 +int check_length(char **inputs, uint8_t num_inputs)
  27 +{
  28 + int i;
  29 + int length = strlen(inputs[0]);
  30 + for(i = 1; i < num_inputs-1; i++)
  31 + {
  32 + if(strlen(inputs[i]) != length)
  33 + {
  34 + return -1;
  35 + }
  36 + }
  37 + int sel_length = strlen(inputs[num_inputs-1]);
  38 + if(pow(2,sel_length) < (num_inputs-1))
  39 + {
  40 + return -1;
  41 + }
  42 + return length;
  43 +}
  44 +
  45 +int main(int argc, char **argv)
  46 +{
  47 + int i;
  48 + if(argc != 2)
  49 + {
  50 + error();
  51 + }
  52 + char *input = argv[1];
  53 + if(!strcmp(input,"error"))
  54 + {
  55 + error();
  56 + }
  57 + char **inputs = comma_separate(input);
  58 + if(inputs == NULL)
  59 + {
  60 + error();
  61 + }
  62 + int num_inputs = num_occurrences(input,',');
  63 + int input_length = check_length(inputs,num_inputs);
  64 + if(input_length == -1)
  65 + {
  66 + error();
  67 + }
  68 + int selected = bin2dec(inputs[num_inputs-1]);
  69 + if(selected >= (num_inputs-1))
  70 + {
  71 + for(i = 0; i < input_length; i++)
  72 + {
  73 + printf("0");
  74 + }
  75 + }
  76 + else
  77 + {
  78 + char *result = inputs[selected];
  79 + printf("%s",result);
  80 + }
  81 + free_mem(inputs,num_inputs);
  82 + return 0;
  83 +}
... ...
todo 0 → 100644
  1 +++ a/todo
  1 +demuxer
  2 +coder
  3 +decoder
  4 +bit selector
  5 +
  6 +adder
  7 +substractor
  8 +multiplier
  9 +divider
  10 +negator
  11 +comparator
  12 +shifter
  13 +bit adder
  14 +bit finder
  15 +
  16 +biestable t
  17 +biestable d
  18 +biestable jk
  19 +biestable sr
  20 +register
  21 +counter
  22 +shift register
  23 +ram
  24 +rom
  25 +
  26 +
  27 +7seg
  28 +tty
  29 +led
... ...