Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 1 | /********************************************************************************************************* |
| 2 | ** Programa para generar los posibles datos que van en una LUT |
| 3 | ** - Son 3 funciones por LUT |
| 4 | ** - Son 5 posibles funciones. 0 NOT, 1 AND, 2 XOR, 3 OR, 4 YES |
| 5 | ** |
| 6 | ** |
| 7 | ** |
| 8 | ** |
| 9 | **********************************************************************************************************/ |
| 10 | #include <stdio.h> |
| 11 | #include <termios.h> |
| 12 | #include <sys/mman.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <time.h> |
| 18 | #include <math.h> |
| 19 | #include <pthread.h> |
| 20 | #define FUNCIONES 5 |
| 21 | #define FUNCOMBS (int) pow(FUNCIONES, 3) |
| 22 | #define VARS 4 |
| 23 | #define COMBS (int) pow(2,VARS) |
| 24 | |
| 25 | #define POBLACION 64 |
| 26 | #define ARBOLES 5 |
| 27 | #define LONG_ARBOL 8 |
| 28 | |
| 29 | /************************************************************************************************************************************** |
| 30 | halla la salida de un arbol para una entrada de x */ |
| 31 | int eval_func(char *ap, int x ) //var apunta al valor de las variables |
| 32 | { |
| 33 | char apfun[32] = {0, 0x1, 0x1,0x0,0x0, 0x0,0x0,0x0,0x1, 0x0,0x1,0x1,0x0, 0x0,0x1,0x1,0x1, 0x0,0x0,0x1,0x1}; //0=NOT,1=AND,2=XOR,3=OR,4=YES |
| 34 | char apl0, apl11, apl12; |
| 35 | char Y, f1, f2; |
| 36 | char var[VARS], i; |
| 37 | |
| 38 | for(i=0;i <= VARS-1;i++) |
| 39 | { |
| 40 | var[i] = (x >> i) & 0x1; |
| 41 | //printf("-%i",var[i]); |
| 42 | } |
| 43 | var[VARS] = 0; |
| 44 | |
| 45 | f1 = 2*apfun[((*(ap+1))*4) + (2* (*(var+(*(ap+2))))) + *(var+(*(ap+3))) + 1]; |
| 46 | // ^2da funcion ^1ra variable ^2da variable |
| 47 | f2 = apfun[((*(ap+4))*4) + (2* (*(var+(*(ap+5))))) + *(var+(*(ap+6))) + 1]; |
| 48 | // ^3ra funcion ^3ra variable ^4da variable |
| 49 | Y = apfun[((*(ap))*4) + f1 + f2 + 1]; |
| 50 | // ^1ra funcion |
| 51 | // printf("f1:%i f2:%i Y:%i ",f1,f2,Y); |
| 52 | return Y; |
| 53 | } |
| 54 | |
| 55 | /************************************************************************************************************************************** |
| 56 | imprime un cromosoma completo */ |
| 57 | mostrar_indiv(char *cromo, int pentarboles) |
| 58 | { |
| 59 | char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES |
| 60 | char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'}; |
| 61 | |
| 62 | ap = cromo; |
| 63 | for(i = 0; i < 1; i++){ |
| 64 | vn[VARS] = '.'; |
| 65 | printf(" %c%c%c%c%c%c%c", fn[*ap], fn[*(ap+1)], vn[*(ap+2)], vn[*(ap+3)], fn[*(ap+4)], vn[*(ap+5)], vn[*(ap+6)]); |
| 66 | } |
| 67 | printf("\n"); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | main() |
| 72 | { |
| 73 | char *cromo, *tabla,*puertas; |
| 74 | int i, j, k, l, salida,x; |
| 75 | char ruta[]="funlut.dat"; |
| 76 | char ruta2[]="puertas.dat"; |
| 77 | short int *funlut_ap; |
| 78 | int size1; |
| 79 | |
| 80 | tabla = malloc(sizeof(short int) * FUNCOMBS*4); |
| 81 | if(tabla==0) printf("Error en malloc"); |
| 82 | puertas = malloc(sizeof(short int) * FUNCOMBS*4); |
| 83 | if(tabla==0) printf("Error en malloc"); |
| 84 | cromo = malloc(sizeof(char) * FUNCOMBS); |
| 85 | if(cromo==0) printf("Error en malloc"); |
| 86 | |
| 87 | FILE *f1, *fich=fopen(ruta,"wb"); |
| 88 | FILE *f2, *fich2=fopen(ruta2,"wb"); |
| 89 | |
| 90 | printf("COMBS%i ", FUNCOMBS); |
| 91 | |
| 92 | for(i = 0; i < FUNCIONES; i++) |
| 93 | { |
| 94 | for(j = 0; j < FUNCIONES; j++) |
| 95 | { |
| 96 | for(k = 0; k < FUNCIONES; k++) |
| 97 | { |
| 98 | x = 0; |
| 99 | for(l = 0; l < COMBS; l++) |
| 100 | { |
| 101 | *(puertas + ((i*25) + (j*5) + k))=0; |
| 102 | *cromo = i; |
| 103 | *(cromo+1) = j; |
| 104 | *(cromo+2) = 0; |
| 105 | *(cromo+3) = 1; |
| 106 | *(cromo+4) = k; |
| 107 | *(cromo+5) = 2; |
| 108 | *(cromo+6) = 3; |
| 109 | salida = eval_func(cromo, l); |
| 110 | printf("%i", salida); |
| 111 | x = x + (salida << l); |
| 112 | if(*cromo != 4) |
| 113 | (*(puertas + ((i*25) + (j*5) + k) ))++; |
| 114 | if(*(cromo + 1) != 4) |
| 115 | (*(puertas + ((i*25) + (j*5) + k)))++; |
| 116 | if(*(cromo + 4) != 4 && *cromo != 4) |
| 117 | (*(puertas + ((i*25) + (j*5) + k)))++; |
| 118 | } |
| 119 | *(tabla + ((i*25) + (j*5) + k)*2 + 1 ) = 0xFF & (x>>8); // se guarda con little endian, para JZ e intel. |
| 120 | *(tabla + ((i*25) + (j*5) + k)*2 ) = 0xFF & x; // intercambia estos dos para cambiar endianismo |
| 121 | printf(" %i %04hX %4X", (i*25) + (j*5) + k, *(unsigned short *)(tabla + ((i*25) + (j*5) + k)*2), *(unsigned char *)(tabla + ((i*25) + (j*5) + k)*2 + 1)); |
| 122 | mostrar_indiv(cromo, 1); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | fwrite((short int *)tabla, FUNCOMBS, sizeof(short int),fich); |
| 128 | fclose(fich); |
| 129 | fwrite((char *)puertas, FUNCOMBS, sizeof(char),fich2); |
| 130 | fclose(fich2); |
| 131 | |
| 132 | f1 = fopen("funlut.dat","r"); |
| 133 | if(f1 == NULL){ |
| 134 | printf("\nError de lectura de archivo!"); |
| 135 | return 0;} |
| 136 | fseek (f1, 0, SEEK_END); |
| 137 | size1 = ftell(f1); |
| 138 | funlut_ap = malloc(size1); |
| 139 | if(funlut_ap==0) printf("Error en malloc"); |
| 140 | rewind (f1); |
| 141 | fread(funlut_ap,2,size1/(sizeof(short int)),f1); |
| 142 | fclose(f1); |
| 143 | for(i = 0; i < (size1/sizeof(short int)); i++) |
| 144 | printf("\n%0x : %0x ",i, *(unsigned short int*)(funlut_ap+i)); |
| 145 | free(funlut_ap); |
| 146 | |
| 147 | } |
| 148 |
Branches:
master
