Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 1 | /*********************************************************************************************************** |
| 2 | * programa para hacer test perifericos evalfit del proyecto ehw4, con mersenne twister incorporado |
| 3 | * |
| 4 | * Funciona con HW evalfit hasta con 14 vars. Evalfit funciona para 5 arboles. nivel max de 4. |
| 5 | * |
| 6 | ************************************************************************************************************/ |
| 7 | |
| 8 | // clk => PLB_Clk, |
| 9 | // reset => regs_wr(0)(0), |
| 10 | // habilita => regs_wr(0)(1), |
| 11 | // maxcombs => regs_wr(4)(16 to 31), |
| 12 | // nivel_max => regs_wr(4)(12 to 15), |
| 13 | // peripheral_mem_in => do_mem_usr,-- 64 bits |
| 14 | // peripheral_mem_en => en_mem_usr, |
| 15 | // peripheral_mem_out => di_mem_usr, -- 64 bits |
| 16 | // peripheral_mem_we => we_mem_usr, |
| 17 | // peripheral_mem_addr => addr_mem_usr, --9 bits |
| 18 | // evalfit3_estado => regs_rd(1)(8 to 15), |
| 19 | // errores => regs_rd(2)(16 to 31), |
| 20 | // fin_ack => regs_rd(1)(31) ); |
| 21 | |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <termios.h> |
| 25 | #include <sys/mman.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <time.h> |
| 31 | #include <evalfit.h> |
| 32 | #include "jz47xx_gpio.h" |
| 33 | #include <jz47xx_mmap.h> |
| 34 | |
| 35 | #define CS2_PORT JZ_GPIO_PORT_B |
| 36 | #define CS2_PIN 26 |
| 37 | |
| 38 | typedef long long timestamp_t; |
| 39 | static timestamp_t |
| 40 | get_timestamp () |
| 41 | { |
| 42 | struct timeval now; |
| 43 | gettimeofday (&now, NULL); |
| 44 | return now.tv_usec + (timestamp_t)now.tv_sec *1000000 ; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | int nivel2(int pentarboles) |
| 49 | { |
| 50 | int x; |
| 51 | if(nivel1 > 1) |
| 52 | x = (int)ceil((float)nivel1 / 4); |
| 53 | else |
| 54 | x = 0; |
| 55 | return x; |
| 56 | |
| 57 | } |
| 58 | int nivel3(int pentarboles) |
| 59 | { |
| 60 | int x; |
| 61 | if(nivel2(pentarboles) > 1) |
| 62 | x = (int)ceil((float)nivel1 / 16); |
| 63 | else |
| 64 | x = 0; |
| 65 | return x; |
| 66 | } |
| 67 | int nivel4(int pentarboles) |
| 68 | { |
| 69 | int x; |
| 70 | if(nivel3(pentarboles) > 1) |
| 71 | x = (int)ceil((float)nivel1/ 64); |
| 72 | else |
| 73 | x = 0; |
| 74 | return x; |
| 75 | } |
| 76 | int nivel5(int pentarboles) |
| 77 | { |
| 78 | int x; |
| 79 | if(nivel4(pentarboles) > 1) |
| 80 | x = (int)ceil((float)nivel1 / 256); |
| 81 | else |
| 82 | x = 0; |
| 83 | return x; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /************************************************************************************************************************************** |
| 88 | genera un numero de forma aleatoria*/ |
| 89 | char random_var(int max) |
| 90 | { |
| 91 | char variable, mascara; |
| 92 | int i; |
| 93 | mascara = 1; |
| 94 | do |
| 95 | { |
| 96 | mascara = (mascara << 1) + 1; |
| 97 | }while(max > mascara); |
| 98 | variable = random() & mascara; |
| 99 | |
| 100 | while(variable > max) |
| 101 | { |
| 102 | variable = variable - max; |
| 103 | } |
| 104 | return variable; |
| 105 | } |
| 106 | |
| 107 | /************************************************************************************************************************************** |
| 108 | imprime un cromosoma completo */ |
| 109 | void mostrar_indiv_lut(char *cromo, int pentarboles) |
| 110 | { |
| 111 | char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES |
| 112 | char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'}; |
| 113 | ap = cromo; |
| 114 | |
| 115 | if(*(cromo + 7) == 1) |
| 116 | { |
| 117 | vn[VARS] = '.'; |
| 118 | printf(": %0x%c%c%c%c", *(unsigned short int *)ap, vn[*(ap+2) >> 4], vn[*(ap+2) & 0xF], vn[*(ap+3) >> 4], vn[*(ap+3) & 0xF]); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | vn[4] = '.'; |
| 123 | printf(": %0x%c%c%c%c", *(unsigned short int *)ap, vn[*(ap+2) >> 4], vn[*(ap+2) & 0xF], vn[*(ap+3) >> 4], vn[*(ap+3) & 0xF]); |
| 124 | } |
| 125 | printf("\t %0x %0x ", *(short int *)(ap+2), *(ap+6) ); |
| 126 | for(i = 0; i < LONG_ARBOL; i++) |
| 127 | { |
| 128 | printf("%0x,",*(cromo+i)); |
| 129 | } |
| 130 | // printf("\n"); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /************************************************************************************************************************************** |
| 135 | imprime un cromosoma completo */ |
| 136 | void mostrar_indiv(char *cromo, int pentarboles) |
| 137 | { |
| 138 | char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES |
| 139 | char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'}; |
| 140 | |
| 141 | ap = cromo; |
| 142 | for(i = 0; i < ARBOLES_INDIV; i++){ |
| 143 | if(*(cromo + (i * LONG_ARBOL) + 7) == 1) |
| 144 | { |
| 145 | vn[VARS] = '.'; |
| 146 | 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)]); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | vn[4] = '.'; |
| 151 | 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)]); |
| 152 | } |
| 153 | ap = ap + LONG_ARBOL; |
| 154 | } |
| 155 | printf("\t"); |
| 156 | //for(i = 0; i < LONG_INDIV; i++) |
| 157 | //{ |
| 158 | // printf("%i,",*(cromo+i)); |
| 159 | //} |
| 160 | printf("\n"); |
| 161 | } |
| 162 | |
| 163 | /************************************************************************************************************************************** |
| 164 | imprime un cromosoma completo */ |
| 165 | void mostrar_indiv2(char *cromo, int pentarboles) |
| 166 | { |
| 167 | char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES |
| 168 | char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'}; |
| 169 | |
| 170 | ap = cromo; |
| 171 | |
| 172 | if(*(cromo + (i * LONG_ARBOL) + 7) == 1) |
| 173 | { |
| 174 | vn[VARS] = '.'; |
| 175 | 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)]); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | vn[4] = '.'; |
| 180 | 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)]); |
| 181 | } |
| 182 | |
| 183 | printf("\n"); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /************************************************************************************************************************************** |
| 188 | Genera un arbol de forma aleatoria |
| 189 | Las funciones y variables son representadas por enteros del 0 al 3 o mas... |
| 190 | - el arbol se encuentra compuesto de tres funciones, dos de entrada y una de salida. |
| 191 | */ |
| 192 | void gen_arbol_lut(char *ap, int variables) |
| 193 | { |
| 194 | int lut; |
| 195 | short int dato_lut; |
| 196 | lut = random_var(FUNCOMBS-1); |
| 197 | dato_lut = (*(funlut_ap+(lut*2)) << 8) + (*(funlut_ap+(lut*2)+1)); |
| 198 | *(short int *)ap = dato_lut; |
| 199 | *(ap+2) = *(ap+3) = 0; |
| 200 | *(ap+2) = random_var(variables-1) + (random_var(variables-1) << 4); |
| 201 | *(ap+3) = random_var(variables-1) + (random_var(variables-1) << 4); |
| 202 | *(ap+6) = lut;(ap); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /************************************************************************************************************************************** |
| 207 | Genera un arbol de forma aleatoria |
| 208 | - estructura de un arbol: FFVVFVV F = funcion V = variable |
| 209 | Las funciones y variables son representadas por enteros del 0 al 3 o mas... |
| 210 | - el arbol se encuentra compuesto de tres funciones, dos de entrada y una de salida. |
| 211 | */ |
| 212 | void gen_arbol(char *ap, int variables) |
| 213 | { |
| 214 | *ap = random_var((FUNCIONES)); //funcion de salida, no YES |
| 215 | *(ap+1) = random_var(FUNCIONES); //PRIMER ARBOL |
| 216 | *(ap+2) = random_var(variables); |
| 217 | *(ap+3) = random_var(variables); |
| 218 | *(ap+4) = random_var(FUNCIONES); //SEGUNDO ARBOL |
| 219 | *(ap+5) = random_var(variables); |
| 220 | *(ap+6) = random_var(variables); |
| 221 | |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /************************************************************************************************************************************** |
| 226 | genera un individuo de forma aleatoria */ |
| 227 | void gen_indiv(char *cromo, int pentarboles) |
| 228 | { |
| 229 | char i=0; |
| 230 | int n1, n2, n3, n4, n5, c1, c2, c3, c4, c5, sig, indice; |
| 231 | n1 = n2 = n3 = n4 = n5 = 0; |
| 232 | c1 = c2 = c3 = c4 = c5 = 0; |
| 233 | indice = 0; |
| 234 | |
| 235 | sig=1; |
| 236 | |
| 237 | |
| 238 | do |
| 239 | { |
| 240 | |
| 241 | if((sig == 1) && (c1 < nivel1)) |
| 242 | { |
| 243 | gen_arbol(cromo + (indice * LONG_ARBOL), VARS-1); |
| 244 | *(cromo +(indice*LONG_ARBOL) + 7) = 1; |
| 245 | n1 ++; |
| 246 | c1 ++; |
| 247 | indice ++; |
| 248 | if(n1 < 4) |
| 249 | sig = 1; |
| 250 | else |
| 251 | { |
| 252 | sig = 2; |
| 253 | n1 = 0; |
| 254 | }//printf("1"); |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | if((sig == 2) && (c2 < nivel2(pentarboles))) |
| 259 | { |
| 260 | gen_arbol(cromo + (indice*LONG_ARBOL), 3); |
| 261 | *(cromo +(indice*LONG_ARBOL) + 7) = 2; |
| 262 | n2++; |
| 263 | c2 ++; |
| 264 | indice ++; |
| 265 | if(c2 == nivel2(pentarboles)) |
| 266 | sig = 3; |
| 267 | else |
| 268 | { |
| 269 | if(n2 < 4) |
| 270 | sig = 1; |
| 271 | else |
| 272 | { |
| 273 | sig = 3; |
| 274 | n2 = 0; |
| 275 | } |
| 276 | }//printf("2"); |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | if((sig == 3) && (c3 < nivel3(pentarboles))) |
| 281 | { |
| 282 | gen_arbol(cromo + (indice*LONG_ARBOL), 3); |
| 283 | *(cromo +(indice*LONG_ARBOL) + 7) = 3; |
| 284 | n3++; |
| 285 | c3++; |
| 286 | indice++; |
| 287 | if(c3 == nivel3(pentarboles)) |
| 288 | sig = 4; |
| 289 | else |
| 290 | { |
| 291 | if(n3 < 4) |
| 292 | sig = 1; |
| 293 | else |
| 294 | { |
| 295 | sig = 4; |
| 296 | n3 = 0; |
| 297 | } |
| 298 | }//printf("3"); |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | if((sig == 4) && (c4 < nivel4(pentarboles))) |
| 303 | { |
| 304 | gen_arbol(cromo + (indice*LONG_ARBOL), 3); |
| 305 | *(cromo +(indice*LONG_ARBOL) + 7) = 4; |
| 306 | n4++; |
| 307 | c4++; |
| 308 | indice++; |
| 309 | if(c4 == nivel4(pentarboles)) |
| 310 | sig = 5; |
| 311 | else |
| 312 | { |
| 313 | if(n4 < 4) |
| 314 | sig = 1; |
| 315 | else |
| 316 | { |
| 317 | sig = 5; |
| 318 | n4 = 0; |
| 319 | } |
| 320 | }//printf("4"); |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | if((sig == 5) && (c5 < nivel5(pentarboles))) |
| 325 | { |
| 326 | gen_arbol(cromo + (indice*LONG_ARBOL), 3); |
| 327 | *(cromo +(indice*LONG_ARBOL) + 7) = 5; |
| 328 | c5++; |
| 329 | indice++; |
| 330 | if(c5 == nivel5(pentarboles)) |
| 331 | sig = 1; |
| 332 | else |
| 333 | sig = 1; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | } |
| 341 | |
| 342 | }while(indice < ARBOLES_INDIV); |
| 343 | |
| 344 | } |
| 345 | |
| 346 | /************************************************************************************************************************************** |
| 347 | halla la salida de un arbol para una entrada de x de un cromo basado en LUT*/ |
| 348 | int eval_func_lut(char *ap, int x ) //var apunta al valor de las variables |
| 349 | { |
| 350 | char Y; |
| 351 | char var[VARS], i, a, b, c, d; |
| 352 | int lut; |
| 353 | |
| 354 | for(i=0;i <= VARS-1;i++) |
| 355 | { |
| 356 | var[i] = (x >> i) & 0x1; |
| 357 | // printf("-%i",var[i]); |
| 358 | } |
| 359 | var[VARS] = 0; |
| 360 | |
| 361 | a = *(ap + 3) & 0xF; |
| 362 | b = (*(ap + 3) >> 4) & 0xF; |
| 363 | c = *(ap + 2) & 0xF; |
| 364 | d = (*(ap + 2) >> 4) & 0xF; |
| 365 | i = var[a] + (var[b]*2) + (var[c]*4) + (var[d]*8); |
| 366 | lut = *(short int *)ap; |
| 367 | Y = (lut >> i) & 0x1; |
| 368 | return Y; |
| 369 | } |
| 370 | |
| 371 | /************************************************************************************************************************************** |
| 372 | halla la salida de un arbol para una entrada de x */ |
| 373 | int eval_func(char *ap, int x ) //var apunta al valor de las variables |
| 374 | { |
| 375 | 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 |
| 376 | char apl0, apl11, apl12; |
| 377 | char Y, f1, f2; |
| 378 | char var[VARS], i; |
| 379 | |
| 380 | |
| 381 | for(i=0;i <= VARS-1;i++) |
| 382 | { |
| 383 | var[i] = (x >> i) & 0x1; |
| 384 | // printf("-%i",var[i]); |
| 385 | } |
| 386 | var[VARS] = 0; |
| 387 | |
| 388 | f1 = 2*apfun[((*(ap+1))*4) + (2* (*(var+(*(ap+2))))) + *(var+(*(ap+3))) + 1]; |
| 389 | // ^2da funcion ^1ra variable ^2da variable |
| 390 | f2 = apfun[((*(ap+4))*4) + (2* (*(var+(*(ap+5))))) + *(var+(*(ap+6))) + 1]; |
| 391 | // ^3ra funcion ^3ra variable ^4da variable |
| 392 | Y = apfun[((*(ap))*4) + f1 + f2 + 1]; |
| 393 | // ^1ra funcion |
| 394 | // printf("\n"); |
| 395 | |
| 396 | return Y; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | |
| 401 | /************************************************************************************************************************************** |
| 402 | retorna las salidas de un cromosoma de 5 arboles*/ |
| 403 | void eval_pentarbol_sw(char *ap, int *salida, int *entrada) |
| 404 | { |
| 405 | int i, k ; |
| 406 | char salidas[ARBOLES][COMBS], aux[COMBS]; |
| 407 | for(i=0; i <= ARBOLES-2; i++){ //se evaluan las salidas de los primeros arboles y se almacenan en salidas |
| 408 | for(k=0; k<= (COMBS-1); k++) |
| 409 | { |
| 410 | salidas[i][k] = eval_func_lut((ap+(i*LONG_ARBOL)), k); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | //se calculan los minterminos para el arbol de salida |
| 415 | for(k=0; k <= (COMBS-1); k++) |
| 416 | { |
| 417 | aux[k] = ((salidas[0][k])*1) + ((salidas[1][k])*2) + ((salidas[2][k])*4) + ((salidas[3][k])*8); |
| 418 | } |
| 419 | |
| 420 | for(i=0; i <= (COMBS-1); i++) |
| 421 | { |
| 422 | *(salida + i) = eval_func_lut(ap + ((ARBOLES-1) * LONG_ARBOL), aux[i]); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | |
| 427 | /************************************************************************************************************************************** |
| 428 | retorna el numero de errores y las salidas de un cromosoma de 1 o mas pentarboles*/ |
| 429 | int eval_fit_sw(char *ap, int *objetivo, int num_min, int pentarboles ) |
| 430 | { |
| 431 | int *obj_min, *med_min2, *med_min3, *med_min4, *med_min5, *entrada, i, j, errores, puertas; |
| 432 | int *salida, n2, n3, n4, n5, x; |
| 433 | |
| 434 | obj_min = malloc(sizeof(obj_min)*COMBS); |
| 435 | med_min2 = malloc(sizeof(med_min2)*COMBS); |
| 436 | med_min3 = malloc(sizeof(med_min3)*COMBS); |
| 437 | med_min4 = malloc(sizeof(med_min4)*COMBS); |
| 438 | med_min5 = malloc(sizeof(med_min5)*COMBS); |
| 439 | entrada = malloc(sizeof(entrada)*COMBS); |
| 440 | salida = malloc(sizeof(salida)*COMBS); |
| 441 | errores = 0; |
| 442 | |
| 443 | for(i=0; i < COMBS; i++) |
| 444 | { |
| 445 | *(obj_min+i) = 0; |
| 446 | *(entrada+i) = i; |
| 447 | *(med_min2 + i) = 0; |
| 448 | *(med_min3 + i) = 0; |
| 449 | *(med_min4 + i) = 0; |
| 450 | *(med_min5 + i) = 0; |
| 451 | } |
| 452 | |
| 453 | for(i = 0; i < num_min; i++) |
| 454 | { |
| 455 | *(obj_min + (*(objetivo + i))) = 1; //se convierte el objetivo a un arreglo |
| 456 | } |
| 457 | |
| 458 | i = 0; |
| 459 | n2 = 0; |
| 460 | n3 = 0; |
| 461 | n4 = 0; |
| 462 | n5 = 0; |
| 463 | |
| 464 | do |
| 465 | { |
| 466 | if(*(ap + (i * LONG_ARBOL) + 7) == 1) |
| 467 | { |
| 468 | eval_pentarbol_sw(ap + (i * LONG_ARBOL), salida, entrada); |
| 469 | for(j = 0; j < COMBS; j++) |
| 470 | { |
| 471 | *(med_min2 + j) = *(med_min2 + j) + (*(salida + j) << n2); |
| 472 | // printf("%i ",*(med_min2 + j));////////quitar |
| 473 | } |
| 474 | if(n2 == 3) n2 = 0; else n2++; |
| 475 | i = i + 5; |
| 476 | // printf(" ");////////quitar |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | if(*(ap + (i * LONG_ARBOL) + 7) == 3) |
| 481 | { |
| 482 | for(j = 0; j < COMBS; j++) |
| 483 | { |
| 484 | *(salida + j) = eval_func_lut(ap + (i * LONG_ARBOL), *(med_min2 + j)); |
| 485 | } |
| 486 | for(j = 0; j < COMBS; j++) |
| 487 | { |
| 488 | *(med_min3 + j) = *(med_min3 + j) + (*(salida + j) << n3); |
| 489 | // printf("%i ",*(med_min3 + j));////////quitar |
| 490 | } |
| 491 | if(n3 == 3) n3 = 0; else n3++; |
| 492 | i++; |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | |
| 497 | if(*(ap + (i * LONG_ARBOL) + 7) == 4) |
| 498 | { |
| 499 | for(j = 0; j < COMBS; j++) |
| 500 | { |
| 501 | *(salida + j) = eval_func_lut(ap + (i * LONG_ARBOL), *(med_min3 + j)); |
| 502 | } |
| 503 | for(j = 0; j < COMBS; j++) |
| 504 | { |
| 505 | *(med_min4 + j) = *(med_min4 + j) + (*(salida + j) << n4); |
| 506 | } |
| 507 | if(n4 == 3) n4 = 0; else n4++; |
| 508 | i++; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | }while(i <= (ARBOLES_INDIV-1)); |
| 513 | |
| 514 | if(*(ap + ((ARBOLES_INDIV-1) * LONG_ARBOL) + 7) == 2) |
| 515 | { |
| 516 | for(j = 0; j < COMBS; j++) //se evalua el arbol de salida |
| 517 | { |
| 518 | errores = errores + abs(*(med_min2 + j) - *(obj_min + j)); //errores |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | if(*(ap + ((ARBOLES_INDIV-1) * LONG_ARBOL) + 7) == 3) |
| 523 | { |
| 524 | for(j = 0; j < COMBS; j++) //se evalua el arbol de salida |
| 525 | { |
| 526 | errores = errores + abs(*(med_min3 + j) - *(obj_min + j)); //errores |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | if(*(ap + ((ARBOLES_INDIV-1) * LONG_ARBOL) + 7) == 4) |
| 531 | { |
| 532 | for(j = 0; j < COMBS; j++) //se evalua el arbol de salida |
| 533 | { |
| 534 | errores = errores + abs(*(med_min4 + j) - *(obj_min + j)); //errores |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | if(*(ap + ((ARBOLES_INDIV-1) * LONG_ARBOL) + 7) == 5) |
| 539 | { |
| 540 | for(j = 0; j < COMBS; j++) //se evalua el arbol de salida |
| 541 | { |
| 542 | errores = errores + abs(*(med_min5 + j) - *(obj_min + j)); //errores |
| 543 | } |
| 544 | } |
| 545 | free(obj_min); |
| 546 | free(med_min2); |
| 547 | free(med_min3); |
| 548 | free(med_min4); |
| 549 | free(med_min5); |
| 550 | free(entrada); |
| 551 | free(salida); |
| 552 | |
| 553 | puertas = 0; |
| 554 | |
| 555 | return ((errores * PESO_SALIDA) + puertas); |
| 556 | } |
| 557 | |
| 558 | /************************************************************************************************************************************** |
| 559 | Introducir minterminos **/ |
| 560 | void minterm2peripheral(int *fun_obj, int tamano) |
| 561 | { int i, j, a; |
| 562 | |
| 563 | for(i = 0; i < 512; i++) //rellenar con 0 |
| 564 | { |
| 565 | *(int *)(evalfit_ptr + EVALFIT_OBJOFFSET + (i*4)) = 0; |
| 566 | } |
| 567 | |
| 568 | for(i = 0; i < tamano; i++) //insertar 1's en segmento de mem objetivo |
| 569 | { |
| 570 | j=0; |
| 571 | a=fun_obj[i]; |
| 572 | while(a > 31) |
| 573 | { |
| 574 | a -= 32; |
| 575 | j++; |
| 576 | } |
| 577 | *(int *)(evalfit_ptr + EVALFIT_OBJOFFSET + (j*4)) += (1 << (31-a)); |
| 578 | } |
| 579 | } |
| 580 | /*************************************************************************************************************************************/ |
| 581 | |
| 582 | |
| 583 | /************************************************************************************************************************************** |
| 584 | map peripheral **/ |
| 585 | int periph_map(off_t offset) |
| 586 | { |
| 587 | int basemem, baseperiph; |
| 588 | basemem = open("/dev/mem", (O_RDWR | O_SYNC)); //abrir dispositivo memoria para mapear dir del periferico |
| 589 | if(basemem == -1) |
| 590 | { |
| 591 | printf("Error to open /dev/mem \n"); |
| 592 | return -1; |
| 593 | } |
| 594 | baseperiph = (int )mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, basemem, offset);// & ~MAP_MASK); |
| 595 | if (baseperiph == -1) |
| 596 | { |
| 597 | printf ("Cannot mmap.\n"); |
| 598 | return -1; |
| 599 | } |
| 600 | return baseperiph; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | /************************************************************************************************************************************** |
| 605 | main **/ |
| 606 | int main() |
| 607 | { |
| 608 | int *aux, base_periferico, *ConfigRegsBase_ptr, basemem, maxcombs, nivel_max; |
| 609 | int a, b, c, i,j, pentarboles, sum1, sum2, sum3; |
| 610 | long int tiempo1, tiempo2; |
| 611 | char *cromo, *cromo2, *obj; |
| 612 | short int dato_lut; |
| 613 | int fun_obj[16]; |
| 614 | void *pio; |
| 615 | |
| 616 | for(i=0;i<16;i++) |
| 617 | { |
| 618 | fun_obj[i] = i; |
| 619 | } |
| 620 | |
| 621 | /** Variables para tablas de lut **/ |
| 622 | FILE *f1; |
| 623 | int size1; |
| 624 | |
| 625 | srand ( (time(NULL)) ); |
| 626 | |
| 627 | cromo = malloc(sizeof(char) * LONG_ARBOL*21); |
| 628 | if(cromo==0) printf("Error en malloc"); |
| 629 | cromo2 = malloc(sizeof(char) * LONG_ARBOL*21); |
| 630 | if(cromo2==0) printf("Error en malloc"); |
| 631 | obj = malloc(COMBS); |
| 632 | if(obj==0) printf("Error en malloc"); |
| 633 | a= sizeof(fun_obj)/4; |
| 634 | printf("{%0x %0x}",a,COMBS); |
| 635 | |
| 636 | for(i=0;i<a;i++) |
| 637 | { |
| 638 | *(obj + fun_obj[i]) = 1; |
| 639 | } |
| 640 | |
| 641 | /** Tabla para las LUT **/ |
| 642 | f1 = fopen("funlut.dat","r"); |
| 643 | if(f1 == NULL){ |
| 644 | printf("\nError de lectura de archivo!"); |
| 645 | return 0;} |
| 646 | fseek (f1, 0, SEEK_END); |
| 647 | size1 = ftell(f1); |
| 648 | funlut_ap = malloc(size1); |
| 649 | |
| 650 | if(funlut_ap==0) printf("Error en malloc"); |
| 651 | rewind (f1); |
| 652 | fread(funlut_ap,1,size1,f1); |
| 653 | fclose(f1); |
| 654 | |
| 655 | |
| 656 | /** periferico **/ |
| 657 | printf("Evalfit Test..."); |
| 658 | basemem = open("/dev/mem", (O_RDWR | O_SYNC)); //abrir dispositivo memoria para mapear dir del periferico |
| 659 | if(basemem == -1) |
| 660 | { |
| 661 | printf("Error al abrir /dev/mem \n"); |
| 662 | return -1; |
| 663 | } |
| 664 | pio = jz_gpio_map(CS2_PORT); |
| 665 | jz_gpio_as_func (pio, CS2_PIN, 0); |
| 666 | ConfigRegsBase_ptr = (int *)periph_map(CONFIG_REGS_BASE);// + SACR2_OFFSET/sizeof(int);//SMCR2_OFFSET/sizeof(int); |
| 667 | printf("\n%0x ", *(ConfigRegsBase_ptr + SMCR2_OFFSET/sizeof(int))); |
| 668 | munmap(ConfigRegsBase_ptr, MAP_SIZE); |
| 669 | evalfit_ptr = (int *)periph_map(EVALFIT_PHYSBASE1); |
| 670 | |
| 671 | printf("\r\nevalfit_ptr: %0x", evalfit_ptr); |
| 672 | if( evalfit_ptr == (int *)MAP_FAILED) |
| 673 | { printf("error mmap!\n"); |
| 674 | fflush(stdout); |
| 675 | return -1; |
| 676 | } |
| 677 | *(char *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = CONTROL_RESET_MASK; //reset |
| 678 | //sleep(1); |
| 679 | *(char *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0; |
| 680 | printf("\nDate + Status: %0x\n", *(char *)(evalfit_ptr + EVALFIT_STATUS)); |
| 681 | |
| 682 | /* pentarboles */ |
| 683 | maxcombs = COMBS; |
| 684 | pentarboles = 4; |
| 685 | nivel_max = 0x3; |
| 686 | printf("\nVars:%i COMBS: %0xH LONG_ARBOL:%0xH LONG_INDIV:%0xH ARBOLES_INDIV:%i\n", VARS, maxcombs, LONG_ARBOL , LONG_INDIV, ARBOLES_INDIV); |
| 687 | |
| 688 | gen_indiv(cromo2, pentarboles); |
| 689 | mostrar_indiv(cromo2, pentarboles); |
| 690 | |
| 691 | /******* convertir a formato de luts ******/ |
| 692 | for(i = 0; i < ARBOLES_INDIV; i++) |
| 693 | { |
| 694 | *(short int *)(cromo+(i*LONG_ARBOL)) = *(short int *)(funlut_ap + ( (*(cromo2 + (i*LONG_ARBOL)) * 25) + |
| 695 | (*(cromo2+(i*LONG_ARBOL)+1)*5) + *(cromo2+(i*LONG_ARBOL)+4) ) *2 ); |
| 696 | *(cromo+(i*LONG_ARBOL) + 3) = *(cromo2+(i*LONG_ARBOL) + 2) + (*(cromo2+(i*LONG_ARBOL) + 3) << 4); |
| 697 | *(cromo+(i*LONG_ARBOL) + 2) = *(cromo2+(i*LONG_ARBOL) + 5) + (*(cromo2+(i*LONG_ARBOL) + 6) << 4); |
| 698 | *(cromo+(i*LONG_ARBOL) + 6) = (*(cromo2+(i*LONG_ARBOL)) * 25) + (*(cromo2+(i*LONG_ARBOL)+1)*5) + *(cromo2+(i*LONG_ARBOL)+4); |
| 699 | *(cromo+(i*LONG_ARBOL) + 7) = *(cromo2+(i*LONG_ARBOL) + 7); |
| 700 | } |
| 701 | |
| 702 | /** copiar miniterminos a periferico **/ |
| 703 | minterm2peripheral(fun_obj, sizeof(fun_obj)/4); |
| 704 | |
| 705 | /** Insertar maxcombs y nivel_max **/ |
| 706 | *(short *)(evalfit_ptr + EVALFIT_MAX_COMBS) = maxcombs-1; |
| 707 | *(char *)(evalfit_ptr + EVALFIT_MAX_LEVEL) = nivel_max; |
| 708 | |
| 709 | /** insertar cromosoma en periferico **/ |
| 710 | printf("\nInserting chromosome in peripheral..."); fflush(stdout); |
| 711 | for(i = 0; i < ARBOLES_INDIV; i++) |
| 712 | { |
| 713 | *(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0) =*(char *)(cromo +(LONG_ARBOL*i)+7); |
| 714 | printf("\n%04hhu: Level:%X ", *(cromo+(i*LONG_ARBOL) + 6), *(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0)); |
| 715 | *(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 4) = *(int *)(cromo +(LONG_ARBOL*i)); |
| 716 | printf("LUT-VARS: %08x",*(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 4)); |
| 717 | } |
| 718 | *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0) = 0xF; //para terminar |
| 719 | i++; |
| 720 | *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0) = 0xF; //para terminar |
| 721 | |
| 722 | /** Iniciar HW **/ |
| 723 | *(char *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = CONTROL_START_MASK; |
| 724 | tiempo1 = get_timestamp(); |
| 725 | |
| 726 | do{ |
| 727 | //printf("\nSTATUS: %0x",*(short *)(evalfit_ptr + EVALFIT_STATUS)); |
| 728 | //usleep(100); |
| 729 | }while((*(short *)(evalfit_ptr + EVALFIT_STATUS) & DONE_MASK) != 0x8000); //wait for DONE |
| 730 | tiempo2 = get_timestamp(); |
| 731 | printf("\n\nErrors calculated HW: %0x",(*(int *)(evalfit_ptr + EVALFIT_ERRORS) & ERRORS_MASK)); //errores |
| 732 | *(char *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0; //disable peripheral |
| 733 | printf("\ndt_hw:%i microsegs\n", tiempo2 - tiempo1); |
| 734 | |
| 735 | /** por SW **/ |
| 736 | tiempo1 = get_timestamp(); |
| 737 | a = eval_fit_sw(cromo, fun_obj, sizeof(fun_obj)/4, pentarboles); |
| 738 | tiempo2 = get_timestamp(); |
| 739 | printf("\nErrors calculated SW: %0x", a); |
| 740 | printf("\ndt_sw:%i microsegs\n", tiempo2 - tiempo1); |
| 741 | |
| 742 | /* Mersenne twister test */ |
| 743 | printf("\n\nnumero aleatorio: %0x",(*(int *)(evalfit_ptr + EVALFIT_RANDOM))); //random |
| 744 | free(funlut_ap); |
| 745 | free(cromo); |
| 746 | free(cromo2); |
| 747 | free(obj); |
| 748 | close(basemem); |
| 749 | munmap(base_periferico, MAP_SIZE); |
| 750 | exit(0); |
| 751 | return 0; |
| 752 | } |
| 753 |
Branches:
master
