From c48feff62c7e8a7b8a432184839eae88c58f6343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Pedraza?= Date: Tue, 12 Oct 2010 10:11:07 -0500 Subject: [PATCH] .. --- Examples/ehw4/logic/ehw.v | 208 ++---- Examples/ehw4/logic/evalfit_peripheral.vhd | 4 +- Examples/ehw4/src/Makefile | 52 +- Examples/ehw4/src/genetic.c | 175 +++-- Examples/ehw4/src/genetic.h | 47 +- Examples/ehw4/src/jz47xx_gpio.c | 119 ++++ Examples/ehw4/src/jz47xx_gpio.h | 84 +++ Examples/ehw4/src/jz47xx_mmap.c | 64 ++ Examples/ehw4/src/jz47xx_mmap.h | 17 + Examples/ehw4/src/sintesishw_client.c | 156 +++-- Examples/ehw4/src/sintesishw_client.h | 2 +- Examples/ehw4/src/sintesishw_server.c | 28 +- Examples/ehw4/src/test/EvalfitTest.c | 752 +++++++++++++++++++++ Examples/ehw4/src/test/Makefile | 13 +- Examples/ehw4/src/test/evalfit.h | 97 +++ Examples/ehw4/src/test/gen_fun_lut.c | 147 ++++ 16 files changed, 1618 insertions(+), 347 deletions(-) create mode 100644 Examples/ehw4/src/jz47xx_gpio.c create mode 100644 Examples/ehw4/src/jz47xx_gpio.h create mode 100644 Examples/ehw4/src/jz47xx_mmap.c create mode 100644 Examples/ehw4/src/jz47xx_mmap.h create mode 100755 Examples/ehw4/src/test/EvalfitTest.c mode change 100755 => 100644 Examples/ehw4/src/test/Makefile create mode 100755 Examples/ehw4/src/test/evalfit.h create mode 100755 Examples/ehw4/src/test/gen_fun_lut.c diff --git a/Examples/ehw4/logic/ehw.v b/Examples/ehw4/logic/ehw.v index d6fa350..feadcae 100644 --- a/Examples/ehw4/logic/ehw.v +++ b/Examples/ehw4/logic/ehw.v @@ -12,14 +12,13 @@ module ehw(clk, sram_data, addr, nwe, ncs, noe, reset, led, // synchronize signals reg sncs, snwe; reg [12:0] buffer_addr; - reg [B:0] buffer_data; - wire led; + reg [B:0] buffer_data; // bram-cpu interfaz reg we; reg w_st=0; reg [B:0] wdBus; - reg [B:0] rdBus; + wire [B:0] rdBus; wire [12:0] addr; reg [7:0] bae; @@ -45,11 +44,7 @@ module ehw(clk, sram_data, addr, nwe, ncs, noe, reset, led, wire [3:0] max_lev; wire [7:0] control; - reg [7:0] reg_bank [31:0]; - wire enReg; - wire [4:0] address; - -// Test : LED blinking + // Test : LED blinking reg [25:0] counter; always @(posedge clk) begin if (~reset) @@ -59,11 +54,11 @@ module ehw(clk, sram_data, addr, nwe, ncs, noe, reset, led, end assign led = counter[24]; -// Data Bus direction control + // Data Bus direction control wire T = ~noe | ncs; assign sram_data = T?8'bZ:rdBus; -// synchronize assignment + // synchronize assignment always @(negedge clk) begin sncs <= ncs; @@ -72,7 +67,7 @@ module ehw(clk, sram_data, addr, nwe, ncs, noe, reset, led, buffer_addr <= addr; end -// write access cpu to bram + // write access cpu to bram always @(posedge clk) if(~reset) {w_st, we, wdBus} <= 0; else begin @@ -115,164 +110,63 @@ module ehw(clk, sram_data, addr, nwe, ncs, noe, reset, led, else bae <= 8'h00; end - wire en1, en2; // enable memory signals - assign en0 = bae[0] | bae[1] | bae[2] | bae[3]; - assign en1 = bae[4] | bae[5] | bae[6] | bae[7]; - reg[31:0] DIA_Aux; - always @ (posedge clk) begin - if (bae[0]) DIA_Aux[7:0] = wdBus[7:0]; - if (bae[1]) DIA_Aux[15:8] = wdBus[7:0]; - if (bae[2]) DIA_Aux[23:16] = wdBus[7:0]; - if (bae[3]) DIA_Aux[31:24] = wdBus[7:0]; - if (bae[4]) DIA_Aux[7:0] = wdBus[7:0]; - if (bae[5]) DIA_Aux[15:8] = wdBus[7:0]; - if (bae[6]) DIA_Aux[23:16] = wdBus[7:0]; - if (bae[7]) DIA_Aux[31:24] = wdBus[7:0]; - end - reg [2:0] state, nextstate; //FSM for write in 32bit mode to memory - wire we0, we1; - wire nreset; - assign nreset = ~reset; - parameter S0 = 3'b000; - parameter S1 = 3'b001; - parameter S2 = 3'b010; - - always @ (posedge clk, posedge nreset) - if (nreset) state <= S0; - else state <= nextstate; - // next state logic - always@(*) - case (state) - S0:if (bae[3]&we) nextstate = S1; - else - if (bae[7]&we) nextstate = S2; - else nextstate = S0; - S1: nextstate = S0; - S2: nextstate = S0; - default: nextstate = S0; - endcase - // output logic - assign we0 = (state == S1); - assign we1 = (state == S2); + // Memories -// Read control - reg [7:0] MemDOA; - wire [63:0] DOA_Aux; - - always @(posedge clk) - if(~reset)begin - rdBus = 8'h00; - end - else begin - if(enReg) - rdBus = reg_bank[address]; - else - rdBus = MemDOA[7:0]; - end -// memory output mux - always @(buffer_addr[2:0]) - case (buffer_addr[2:0]) - 0 : MemDOA = DOA_Aux[7:0]; - 1 : MemDOA = DOA_Aux[15:8]; - 2 : MemDOA = DOA_Aux[23:16]; - 3 : MemDOA = DOA_Aux[31:24]; - 4 : MemDOA = DOA_Aux[39:32]; - 5 : MemDOA = DOA_Aux[47:40]; - 6 : MemDOA = DOA_Aux[55:48]; - 7 : MemDOA = DOA_Aux[63:56]; - default: MemDOA = 8'h00; - endcase - -// Store Inputs - always @(posedge clk) - begin - if(enReg) begin - reg_bank[20] = error[7:0]; - reg_bank[21] = error[15:8]; - reg_bank[22] = status[7:0]; - reg_bank[24] = mt_rnd[7:0]; - reg_bank[25] = mt_rnd[15:8]; - reg_bank[26] = mt_rnd[23:16]; - reg_bank[27] = mt_rnd[31:24]; - end - end +RAMB16_S4_S4 ba0( .CLKA(~clk), .ENA(bae[0]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[3:0]), .DOB(ev_di[3:0])); //D3-D0 +RAMB16_S4_S4 ba1( .CLKA(~clk), .ENA(bae[0]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[7:4]), .DOB(ev_di[7:4])); //D7-D4 - assign address[4:0] = buffer_addr[4:0]; - assign enReg = buffer_addr[12]; +RAMB16_S4_S4 ba2( .CLKA(~clk), .ENA(bae[1]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[11:8]), .DOB(ev_di[11:8])); //D11-D8 +RAMB16_S4_S4 ba3( .CLKA(~clk), .ENA(bae[1]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[15:12]), .DOB(ev_di[15:12]));//D15-D12 - assign reg0[7:0] = reg_bank[0]; - assign reg0[15:8] = reg_bank[1]; - assign reg0[23:16] = reg_bank[2]; - assign reg0[31:24] = reg_bank[3]; - assign reg1[7:0] = reg_bank[4]; - assign reg1[15:8] = reg_bank[5]; - assign reg1[23:16] = reg_bank[6]; - assign reg1[31:24] = reg_bank[7]; - assign reg2[7:0] = reg_bank[8]; - assign reg2[15:8] = reg_bank[9]; - assign reg2[23:16] = reg_bank[10]; - assign reg2[31:24] = reg_bank[11]; - assign reg3[7:0] = reg_bank[12]; - assign reg3[15:8] = reg_bank[13]; - assign reg3[23:16] = reg_bank[14]; - assign reg3[31:24] = reg_bank[15]; - assign reg4[7:0] = reg_bank[16]; - assign reg4[15:8] = reg_bank[17]; - assign reg4[23:16] = reg_bank[18]; - assign reg4[31:24] = reg_bank[19]; +RAMB16_S4_S4 ba4( .CLKA(~clk), .ENA(bae[2]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[19:16]), .DOB(ev_di[19:16])); //D19-D16 +RAMB16_S4_S4 ba5( .CLKA(~clk), .ENA(bae[2]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[23:20]), .DOB(ev_di[23:20])); //D23-D20 - assign max_lev = reg_bank[28]; - assign control = reg_bank[29]; - assign max_com[7:0] = reg_bank[30]; - assign max_com[15:8] = reg_bank[31]; - // Write control - always @(negedge clk) - if(we & enReg) begin - case (address) - 0: reg_bank[0] = wdBus; - 1: reg_bank[1] = wdBus; - 2: reg_bank[2] = wdBus; - 3: reg_bank[3] = wdBus; - 4: reg_bank[4] = wdBus; - 5: reg_bank[5] = wdBus; - 6: reg_bank[6] = wdBus; - 7: reg_bank[7] = wdBus; - 8: reg_bank[8] = wdBus; - 9: reg_bank[9] = wdBus; - 10: reg_bank[10] = wdBus; - 11: reg_bank[11] = wdBus; - 12: reg_bank[12] = wdBus; - 13: reg_bank[13] = wdBus; - 14: reg_bank[14] = wdBus; - 15: reg_bank[15] = wdBus; - 16: reg_bank[16] = wdBus; - 17: reg_bank[17] = wdBus; - 18: reg_bank[18] = wdBus; - 19: reg_bank[19] = wdBus; - 28: reg_bank[28] = wdBus; - 29: reg_bank[29] = wdBus; - 30: reg_bank[30] = wdBus; - 31: reg_bank[31] = wdBus; - endcase - end +RAMB16_S4_S4 ba6( .CLKA(~clk), .ENA(bae[3]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[27:24]), .DOB(ev_di[27:24])); //D27-D24 +RAMB16_S4_S4 ba7( .CLKA(~clk), .ENA(bae[3]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[31:28]), .DOB(ev_di[31:28])); //D31-D28 -RAMB16_S36_S36 #(.INIT_00(256'hABCDEF00_00000000_00000000_00000000_00000000_00000000_00000000_76543210) ) - mem0 ( .CLKA(~clk), .ENA(en0), .SSRA(1'b0), .ADDRA(buffer_addr[11:3]), .WEA(we0), .DIA(DIA_Aux[31:0]), .DIPA(0'b0), .DOA(DOA_Aux[31:0]), - .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval), .DIB(ev_do[31:0]), .DIPB(0'b0), .DOB(ev_di[31:0])); - -RAMB16_S36_S36 mem1( .CLKA(~clk), .ENA(en1), .SSRA(1'b0), .ADDRA(buffer_addr[11:3]), .WEA(we1), .DIA(DIA_Aux[31:0]), .DIPA(0'b0), .DOA(DOA_Aux[63:32]), - .CLKB(~clk), .ENB(en_ev),.SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval), .DIB(ev_do[63:32]), .DIPB(0'b0), .DOB(ev_di[63:32])); +RAMB16_S4_S4 ba8( .CLKA(~clk), .ENA(bae[4]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[35:32]), .DOB(ev_di[35:32])); //D35-D32 +RAMB16_S4_S4 ba9( .CLKA(~clk), .ENA(bae[4]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[39:36]), .DOB(ev_di[39:36])); //D39-D36 -// evalfit_peripheral -evalfit_peripheral evalfit( .clk(clk), .reset(~reset), .habilita(control[0]), .maxcombs(max_com), .nivel_max(max_lev), +RAMB16_S4_S4 ba10(.CLKA(~clk), .ENA(bae[5]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[43:40]), .DOB(ev_di[43:40])); //D43-D40 +RAMB16_S4_S4 ba11(.CLKA(~clk), .ENA(bae[5]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[47:44]), .DOB(ev_di[47:44])); //D47-D44 + +RAMB16_S4_S4 ba12(.CLKA(~clk), .ENA(bae[6]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[51:48]), .DOB(ev_di[51:48])); //D51-D48 +RAMB16_S4_S4 ba13(.CLKA(~clk), .ENA(bae[6]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[55:52]), .DOB(ev_di[55:52])); //D55-D52 + +RAMB16_S4_S4 ba14(.CLKA(~clk), .ENA(bae[7]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[59:56]), .DOB(ev_di[59:56])); //D59-D56 +RAMB16_S4_S4 ba15(.CLKA(~clk), .ENA(bae[7]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]), + .CLKB(~clk), .ENB(en_ev), .SSRB(1'b0), .ADDRB(evalfit_addr), .WEB(we_eval),.DIB(ev_do[63:60]), .DOB(ev_di[63:60])); //D63-D60 + + // evalfit_peripheral +evalfit_peripheral evalfit( .clk(clk), .reset(reset), .habilita(control[0]), .maxcombs(max_com), .nivel_max(max_lev), .peripheral_mem_in(ev_di), .peripheral_mem_en(en_eval), .peripheral_mem_out(ev_do), .peripheral_mem_we(we_eval), .peripheral_mem_addr(evalfit_addr), .evalfit3_estado(status), .errores(error), .fin_ack(irq_pin), .reg0_s(reg0), .reg1_s(reg1), .reg2_s(reg2), .reg3_s(reg3), .reg4_s(reg4)); -// MersenneTwister - mt_mem random( .clk(clk), .ena(1'b1), .resetn(reset), .random(mt_rnd)); +reg_bank RegBank( .clk(clk), .reset(reset), .en(buffer_addr[12]), .we(we), .wdBus(wdBus), .rdBus(rdBus), .address(buffer_addr[4:0]), + .reg0(reg0), .reg1(reg1), .reg2(reg2), .reg3(reg3), .reg4(reg4), .error(error), .status(status), + .max_com(max_com), .max_lev(max_lev), .control(control)); +// mt_mem + mt_mem random( .clk(clk), .ena(1'b1), .resetn(~reset), .random(mt_rnd)); + + endmodule diff --git a/Examples/ehw4/logic/evalfit_peripheral.vhd b/Examples/ehw4/logic/evalfit_peripheral.vhd index 21f3225..f6cca81 100644 --- a/Examples/ehw4/logic/evalfit_peripheral.vhd +++ b/Examples/ehw4/logic/evalfit_peripheral.vhd @@ -25,9 +25,9 @@ entity evalfit_peripheral is Port ( clk, reset, habilita: in STD_LOGIC; maxcombs : in STD_LOGIC_VECTOR (0 to 15); nivel_max : in STD_LOGIC_VECTOR (0 to 3); - peripheral_mem_in : in STD_LOGIC_VECTOR (0 to 63); + peripheral_mem_in : in STD_LOGIC_VECTOR (0 to 63); peripheral_mem_en : out std_logic; - peripheral_mem_out : out STD_LOGIC_VECTOR (0 to 63); + peripheral_mem_out : out STD_LOGIC_VECTOR (0 to 63); peripheral_mem_we : out STD_LOGIC; peripheral_mem_addr : out STD_LOGIC_VECTOR (0 to 8); evalfit3_estado : out std_logic_vector(0 to 7); diff --git a/Examples/ehw4/src/Makefile b/Examples/ehw4/src/Makefile index b8c4d33..77bb001 100644 --- a/Examples/ehw4/src/Makefile +++ b/Examples/ehw4/src/Makefile @@ -1,10 +1,11 @@ -CC = mipsel-openwrt-linux-gcc +CROSS = mipsel-openwrt-linux-gcc +CC = gcc -all: jz_init_sram jz_test_gpio enable_rx enable_irq +all: client_jz server_jz client_386 server_386 DEBUG = -O3 -g0 -COMMON_SOURCES = jz47xx_gpio.c jz47xx_mmap.c +COMMON_SOURCES = jz47xx_gpio.c jz47xx_mmap.c H_SOURCES = jz47xx_gpio.h jz47xx_mmap.h @@ -16,18 +17,47 @@ CCFLAGS = ${INCLUDE} ${DEBUG} ${WARNINGS} LDFLAGS = -COMMON_OBJECTS = $(COMMON_SOURCES:.c=.o) +COMMON_OBJECTS_JZ = jz47xx_gpio_jz.o jz47xx_mmap_jz.o +COMMON_OBJECTS_386 = jz47xx_gpio_386.o jz47xx_mmap_386.o NANO_IP = 192.168.254.101 -genetic.o: genetic.c genetic.h - ${CC} -lm -I. -c genetic.c -o genetic.o +genetic_jz.o: genetic.c genetic.h $(COMMON_OBJECTS_JZ) + ${CROSS} -lm -I. -c genetic.c $(COMMON_OBJECTS_JZ) -o genetic_jz.o -client: sintesishw_client.c genetic.o sintesishw_client.h - ${CC} sintesishw_client.c genetic.o -o sintesishw_client -lm -I. +client_jz: sintesishw_client.c genetic_jz.o sintesishw_client.h $(COMMON_OBJECTS_JZ) + ${CROSS} sintesishw_client.c genetic_jz.o $(COMMON_OBJECTS_JZ) -o sintesishw_client_jz -lm -I. -server: sintesishw_server.c genetic.o - ${CC} sintesishw_server.c genetic.o -o sintesishw_server -lm -lpthread -I. +server_jz: sintesishw_server.c genetic_jz.o $(COMMON_OBJECTS_JZ) + ${CROSS} sintesishw_server.c genetic_jz.o $(COMMON_OBJECTS_JZ) -o sintesishw_server_jz -lm -lpthread -I. + +jz47xx_mmap_jz.o: jz47xx_mmap.c jz47xx_mmap.h + ${CROSS} -lm -I. -c jz47xx_mmap.c -o jz47xx_mmap_jz.o + +jz47xx_gpio_jz.o: jz47xx_gpio.c jz47xx_gpio.h + ${CROSS} -lm -I. -c jz47xx_gpio.c -o jz47xx_gpio_jz.o + + + +genetic_386.o: genetic.c genetic.h $(COMMON_OBJECTS_386) + ${CC} -lm -I. -c genetic.c $(COMMON_OBJECTS_386) -o genetic_386.o + +client_386: sintesishw_client.c genetic_386.o sintesishw_client.h $(COMMON_OBJECTS_386) + ${CC} sintesishw_client.c genetic_386.o $(COMMON_OBJECTS_386) -o sintesishw_client_386 -lm -I. + +server_386: sintesishw_server.c genetic_386.o $(COMMON_OBJECTS_386) + ${CC} sintesishw_server.c genetic_386.o $(COMMON_OBJECTS_386) -o sintesishw_server_386 -lm -lpthread -I. + +jz47xx_mmap_386.o: jz47xx_mmap.c jz47xx_mmap.h + ${CC} -lm -I. -c jz47xx_mmap.c -o jz47xx_mmap_386.o + +jz47xx_gpio_386.o: jz47xx_gpio.c jz47xx_gpio.h + ${CC} -lm -I. -c jz47xx_gpio.c -o jz47xx_gpio_386.o + + + +upload: sintesishw_server_jz + scp sintesishw_server_jz root@$(NANO_IP):ehw clean: - rm -f *.o sintesishw_client sintesishw_server ${EXEC} *~ + rm -f *.o sintesishw_client_jz sintesishw_server_jz sintesishw_client_386 sintesishw_server_386 ${EXEC} *~ diff --git a/Examples/ehw4/src/genetic.c b/Examples/ehw4/src/genetic.c index 9d43add..929f34d 100644 --- a/Examples/ehw4/src/genetic.c +++ b/Examples/ehw4/src/genetic.c @@ -14,6 +14,11 @@ #include "errno.h" #include "sys/un.h" #include "genetic.h" +#include "jz47xx_gpio.h" +#include + +#define CS2_PORT JZ_GPIO_PORT_B +#define CS2_PIN 26 /************************************************************************************************************************************** imprime un cromosoma completo */ @@ -87,7 +92,7 @@ char random_var(int max) mascara = (mascara << 1) + 1; }while(max > mascara); - if(HW_ENABLE == 1) variable = (*(int *)(evalfit_ptr1 + EVALFIT_RDREG8)) & mascara; + if(HW_ENABLE == 1) variable = (*(int *)(evalfit_ptr1 + EVALFIT_RANDOM)) & mascara; else variable = random() & mascara; while(variable > max) @@ -442,16 +447,15 @@ int i; /** insertar cromosoma en periferico **/ for(i = 0; i < ARBOLES_INDIV; i++) - { - *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i)) = *(char *)(cromo +(LONG_ARBOL*i)+7); -// printf("\n%i: %0x",i,*(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i))); - *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 4) = *(int *)(cromo +(LONG_ARBOL*i)); -// printf(": %0x",*(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i)+4)); - } - *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i)) = 0xF; //para terminar - i++; - *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i)) = 0xF; //para terminar - + { + *(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0) =*(char *)(cromo +(LONG_ARBOL*i)+7); + //printf("\n%04hhu: Level:%X ", *(cromo+(i*LONG_ARBOL) + 6), *(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0)); + *(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 4) = *(int *)(cromo +(LONG_ARBOL*i)); + //printf("LUT-VARS: %08x",*(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 4)); + } + *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0) = 0xF; //para terminar + i++; + *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0) = 0xF; //para terminar /** Iniciar **/ *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = CONTROL_START_MASK; } @@ -463,18 +467,20 @@ int evalfit_hw_wait(char *cromo, int pentarboles, int nivel_max, void *evalfit_p int errores, puertas, i; // tiempo1 = get_timestamp(); - do{ }while((*(int *)(evalfit_ptr + EVALFIT_RDREG1) & DONE_MASK) != 1); - // tiempo2 = get_timestamp(); - errores = *(int *)(evalfit_ptr + EVALFIT_RDREG2) & ERRORS_MASK; //errores - *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0; //terminar, bajar habilita - - puertas = 0; + do{ + //printf("\nSTATUS: %0x",*(short *)(evalfit_ptr + EVALFIT_STATUS)); + //usleep(100); + }while((*(short *)(evalfit_ptr + EVALFIT_STATUS) & DONE_MASK) != 0x8000); //wait for DONE + //tiempo2 = get_timestamp(); + //printf("\n\nErrors calculated HW: %0x",(*(int *)(evalfit_ptr + EVALFIT_ERRORS) & ERRORS_MASK)); //errores + errores = (*(int *)(evalfit_ptr + EVALFIT_ERRORS) & ERRORS_MASK); //errores + *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0; //end , disable + puertas = 0; for(i = 0; i < ARBOLES_INDIV; i++) { puertas = puertas + *(puertas_ap + *(char *)(cromo +(LONG_ARBOL*i)+6)); } - -return ((errores * PESO_SALIDA) + (PESO_PUERTAS * puertas) + (PESO_NIVELES * nivel_max)); + return ((errores * PESO_SALIDA) + (PESO_PUERTAS * puertas) + (PESO_NIVELES * nivel_max)); } /************************************************************************************************************************************** @@ -579,25 +585,58 @@ void minterm2peripheral(int *fun_obj, int tamano, void *evalfit_ptr) } } + +/************************************************************************************************************************************** + map peripheral **/ +int periph_map(off_t offset) +{ +int basemem, baseperiph; + basemem = open("/dev/mem", (O_RDWR | O_SYNC)); //abrir dispositivo memoria para mapear dir del periferico + if(basemem == -1) + { + printf("Error to open /dev/mem \n"); + return -1; + } + baseperiph = (int )mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, basemem, offset);// & ~MAP_MASK); + if (baseperiph == -1) + { + printf ("Cannot mmap.\n"); + return -1; + } +return baseperiph; +} + /************************************************************************************************************************************** inicializar periferico **/ int init_peripheral(int offset_int, int basemem) { /* Variables para periferico*/ -int *aux, base_periferico, *evalfit_ptr; - off_t offset = offset_int; //Direccion base del periferico - base_periferico = (int *)mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, basemem, offset & ~MAP_MASK); - evalfit_ptr = base_periferico + (offset & MAP_MASK); - printf("\r\nPeripheral_ptr: %0x", evalfit_ptr); - if( (int *)evalfit_ptr == (int *)MAP_FAILED) - { printf("error mmap!\n"); - fflush(stdout); - return -1; - } - *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = CONTROL_RESET_MASK; //reset - *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0; - printf("\nDate + Status: %0x\n", *(int *)(evalfit_ptr + EVALFIT_STATUS_OFFSET)); - return evalfit_ptr; + int *aux, base_periferico, *ConfigRegsBase_ptr, maxcombs, nivel_max; + void *pio, *evalfit_ptr; + basemem = open("/dev/mem", (O_RDWR | O_SYNC)); //abrir dispositivo memoria para mapear dir del periferico + if(basemem == -1) + { + printf("Error al abrir /dev/mem \n"); + return -1; + } + pio = jz_gpio_map(CS2_PORT); + jz_gpio_as_func (pio, CS2_PIN, 0); + ConfigRegsBase_ptr = (int *)periph_map(CONFIG_REGS_BASE);// + SACR2_OFFSET/sizeof(int);//SMCR2_OFFSET/sizeof(int); + printf("\n%0x ", *(ConfigRegsBase_ptr + SMCR2_OFFSET/sizeof(int))); + munmap(ConfigRegsBase_ptr, MAP_SIZE); + evalfit_ptr = (int *)periph_map(EVALFIT_PHYSBASE1); + + printf("\r\nevalfit_ptr: %0x", evalfit_ptr); + if( evalfit_ptr == (int *)MAP_FAILED) + { printf("error mmap!\n"); + fflush(stdout); + return -1; + } + *(char *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = CONTROL_RESET_MASK; //reset + //sleep(1); + *(char *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0; + printf("\nDate + Status: %0x\n", *(char *)(evalfit_ptr + EVALFIT_STATUS)); +return evalfit_ptr; } /************************************************************************************************************************************** @@ -643,57 +682,63 @@ float tiempof1, tiempof2, tiempof3; /* datos = malloc(sizeof(datos)*maxgeneraciones*2); if(datos==0) printf("Error en malloc");*/ /* x=0;*/ - objetivo = gen_datos -> objetivo; - tamaobj = gen_datos -> tamaobj; - pentarboles = gen_datos -> pentarboles; - tamacromo = gen_datos -> tamacrom; - cromo_sal = gen_datos -> cromo_sal; - fitness_sal = gen_datos -> fitness; - cromo_entrada = gen_datos -> cromo_entrada; - fitness_entrada = gen_datos -> fitness_entrada; - nivel_max = gen_datos -> nivel_max; - vars = gen_datos -> vars; - generacion = gen_datos->generacion; - aux = gen_datos->aux; - aux_sal = gen_datos->aux_sal; + objetivo = gen_datos -> objetivo; + tamaobj = gen_datos -> tamaobj; + pentarboles = gen_datos -> pentarboles; + tamacromo = gen_datos -> tamacrom; + cromo_sal = gen_datos -> cromo_sal; + fitness_sal = gen_datos -> fitness; + cromo_entrada = gen_datos -> cromo_entrada; + fitness_entrada = gen_datos -> fitness_entrada; + nivel_max = gen_datos -> nivel_max; + vars = gen_datos -> vars; + generacion = gen_datos->generacion; + aux = gen_datos->aux; + aux_sal = gen_datos->aux_sal; - cromo = malloc(sizeof(cromo) * (poblacion + 2) * LONG_INDIV); if(cromo==0) printf("Error en malloc"); - fitness = malloc(sizeof(fitness) * (poblacion+1)); if(fitness==0) printf("Error en malloc"); - fitness2 = malloc(sizeof(fitness2) * (poblacion+1)); if(fitness2==0) printf("Error en malloc"); - ordenpoblacion = malloc(sizeof(ordenpoblacion) * (poblacion+1)); if(ordenpoblacion==0) printf("Error en malloc"); + cromo = malloc(sizeof(cromo) * (poblacion + 2) * LONG_INDIV); if(cromo==0) printf("Error en malloc"); + fitness = malloc(sizeof(fitness) * (poblacion+1)); if(fitness==0) printf("Error en malloc"); + fitness2 = malloc(sizeof(fitness2) * (poblacion+1)); if(fitness2==0) printf("Error en malloc"); + ordenpoblacion = malloc(sizeof(ordenpoblacion) * (poblacion+1)); if(ordenpoblacion==0) printf("Error en malloc"); if(gen_datos->en_cromo_entrada == 1) - { + { for(i = 0; i < LONG_INDIV; i++ ) { *(char *)(cromo + i) = *(char *)(cromo_entrada + i); } - } + } - if(gen_datos->en_cromo_entrada==0) gen_poblacion(cromo, pentarboles, vars, poblacion); - else gen_poblacion(cromo + LONG_INDIV, pentarboles, vars, poblacion-1); + if(gen_datos->en_cromo_entrada == 0) + gen_poblacion(cromo, pentarboles, vars, poblacion); //generate chromosome + else + gen_poblacion(cromo + LONG_INDIV, pentarboles, vars, poblacion-1); - for(i = 0; i < poblacion; i++) - { - *(ordenpoblacion + i) = i; //se inicializa el stream para el orden poblacional - *(fitness + i) = 100000; - *(fitness2 + i) = 100000; - } + for(i = 0; i < poblacion; i++) + { + *(ordenpoblacion + i) = i; //se inicializa el stream para el orden poblacional + *(fitness + i) = 100000; + *(fitness2 + i) = 100000; + } /** copiar miniterminos a periferico **/ if(HW_ENABLE == 1) { minterm2peripheral(objetivo, tamaobj, (int *)(evalfit_ptr1)); +/* minterm2peripheral(objetivo, tamaobj, (int *)(evalfit_ptr2)); minterm2peripheral(objetivo, tamaobj, (int *)(evalfit_ptr3)); minterm2peripheral(objetivo, tamaobj, (int *)(evalfit_ptr4)); +*/ } /** Insertar maxcombs y nivel_max **/ if(HW_ENABLE == 1) { *(int *)(evalfit_ptr1 + EVALFIT_WREG4) = (COMBS-1) + (nivel_max << 16); +/* *(int *)(evalfit_ptr2 + EVALFIT_WREG4) = (COMBS-1) + (nivel_max << 16); *(int *)(evalfit_ptr3 + EVALFIT_WREG4) = (COMBS-1) + (nivel_max << 16); *(int *)(evalfit_ptr4 + EVALFIT_WREG4) = (COMBS-1) + (nivel_max << 16); +*/ } o=0; *generacion = 0; @@ -726,28 +771,36 @@ float tiempof1, tiempof2, tiempof3; // evaluar cromosomas de poblacion o=0; // tiempo1 = get_timestamp(); - for(i = 0; i < poblacion; i=i+4) + for(i = 0; i < poblacion; i=i+1) { if(HW_ENABLE == 1) { evalfit_hw_init((cromo + (i * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr1); +/* evalfit_hw_init((cromo + ((i+1) * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr2); evalfit_hw_init((cromo + ((i+2) * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr3); evalfit_hw_init((cromo + ((i+3) * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr4); +*/ *(fitness + i) = evalfit_hw_wait((cromo + (i * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr1); +/* *(fitness + i+1) = evalfit_hw_wait((cromo + ((i+1) * LONG_INDIV)), pentarboles,nivel_max, (int *)evalfit_ptr2); *(fitness + i+2) = evalfit_hw_wait((cromo + ((i+2) * LONG_INDIV)), pentarboles,nivel_max, (int *)evalfit_ptr3); *(fitness + i+3) = evalfit_hw_wait((cromo + ((i+3) * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr4); +*/ }else{ *(fitness + i) = eval_fit_sw((cromo + (i * LONG_INDIV)), objetivo, tamaobj, pentarboles, vars); +/* *(fitness + i+1) = eval_fit_sw((cromo + ((i+1) * LONG_INDIV)), objetivo, tamaobj, pentarboles, vars); *(fitness + i+2) = eval_fit_sw((cromo + ((i+2) * LONG_INDIV)), objetivo, tamaobj, pentarboles, vars); *(fitness + i+3) = eval_fit_sw((cromo + ((i+3) * LONG_INDIV)), objetivo, tamaobj, pentarboles, vars); +*/ } *(fitness2 + i) = *(fitness + i); +/* *(fitness2 + i+1) = *(fitness + i+1); *(fitness2 + i+2) = *(fitness + i+2); *(fitness2 + i+3) = *(fitness + i+3); +*/ if(*(fitness + i) < PESO_SALIDA) { diff --git a/Examples/ehw4/src/genetic.h b/Examples/ehw4/src/genetic.h index 3914e13..e62283e 100644 --- a/Examples/ehw4/src/genetic.h +++ b/Examples/ehw4/src/genetic.h @@ -43,36 +43,46 @@ int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; #define INDICE_TARA4 ((*(ordenpoblacion+poblacion-4)) * LONG_INDIV) -/************************** peripheral Definitions ***************************/ +/************************** peripheral Definitions ***************************/ -#define USR_REGS 0x10 -#define MAP_SIZE 0x4000Ul -#define MAP_MASK (MAP_SIZE - 1) +#define CONFIG_REGS_BASE 0x13010000 +#define SMCR2_OFFSET 0x18 +#define SACR2_OFFSET 0x38 -#define DONE_MASK 0x1 -#define ERRORS_MASK 0xFFFF +#define EVALFIT_PHYSBASE1 0x14000000 +#define MAP_SIZE 0x4000Ul +#define MAP_MASK (MAP_SIZE - 1) + +#define DONE_MASK 0x8000 +#define ERRORS_MASK 0xFFFF /** CONTROL REGISTERS **/ -#define EVALFIT_REGBASE_OFFSET 0 +#define EVALFIT_REGBASE_OFFSET 0x1000 -#define EVALFIT_CONTROL_OFFSET EVALFIT_REGBASE_OFFSET + 0 -#define CONTROL_RESET_MASK 0x80000000 -#define CONTROL_START_MASK 0x40000000 +#define EVALFIT_CONTROL_OFFSET EVALFIT_REGBASE_OFFSET + 0x1D +#define CONTROL_RESET_MASK 0x80 +#define CONTROL_START_MASK 0x40 -#define EVALFIT_STATUS_OFFSET EVALFIT_REGBASE_OFFSET + 0 -#define EVALFIT_SRCADDR_OFFSET EVALFIT_REGBASE_OFFSET + 4 -#define EVALFIT_DSTADDR_OFFSET EVALFIT_REGBASE_OFFSET + 8 -#define EVALFIT_TRANSFERSIZE_OFFSET EVALFIT_REGBASE_OFFSET + 12 -#define EVALFIT_REG_OFFSET EVALFIT_REGBASE_OFFSET + 16 +#define EVALFIT_REG_OFFSET EVALFIT_REGBASE_OFFSET + 0 /** WRITE REGISTERS **/ + +#define EVALFIT_MAX_COMBS EVALFIT_REGBASE_OFFSET + 0x1E +#define EVALFIT_MAX_LEVEL EVALFIT_REGBASE_OFFSET + 0x1C + + #define EVALFIT_WREG4 (EVALFIT_REGBASE_OFFSET + (4*4)) #define EVALFIT_WREG5 (EVALFIT_REGBASE_OFFSET + (4*5)) #define EVALFIT_wREG6 (EVALFIT_REGBASE_OFFSET + (4*6)) #define EVALFIT_wREG7 (EVALFIT_REGBASE_OFFSET + (4*7)) -#define EVALFIT_wREG8 (EVALFIT_REGBASE_OFFSET + (4*8)) +#define EVALFIT_wREG8 (EVALFIT_REGBASE_OFFSET + (4*8)) /** READ REGISTERS **/ +#define EVALFIT_ERRORS EVALFIT_REGBASE_OFFSET + 0x14 +#define EVALFIT_RANDOM EVALFIT_REGBASE_OFFSET + 0x18 +#define EVALFIT_STATUS EVALFIT_REGBASE_OFFSET + 0x16 + + #define EVALFIT_RDREG0 (EVALFIT_REGBASE_OFFSET) #define EVALFIT_RDREG1 (EVALFIT_REGBASE_OFFSET + (4*1)) #define EVALFIT_RDREG2 (EVALFIT_REGBASE_OFFSET + (4*2)) @@ -83,12 +93,9 @@ int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; #define EVALFIT_RDREG7 (EVALFIT_REGBASE_OFFSET + (4*7)) #define EVALFIT_RDREG8 (EVALFIT_REGBASE_OFFSET + (4*8)) /** MEMORY **/ -#define EVALFIT_MEMOFFSET 0x1000 +#define EVALFIT_MEMOFFSET 0x0 #define EVALFIT_OBJOFFSET EVALFIT_MEMOFFSET + (0x40 * 8) -/** CROMOSOMA **/ -#define CROMO_NIVEL_OFFSET - /* Variables globales */ char *funlut_ap, *puertas_ap; void *evalfit_ptr1, *evalfit_ptr4, *evalfit_ptr3, *evalfit_ptr2; diff --git a/Examples/ehw4/src/jz47xx_gpio.c b/Examples/ehw4/src/jz47xx_gpio.c new file mode 100644 index 0000000..7100647 --- /dev/null +++ b/Examples/ehw4/src/jz47xx_gpio.c @@ -0,0 +1,119 @@ +/* + JZ47xx GPIO at userspace + + Copyright (C) 2010 Andres Calderon andres.calderon@emqbit.com + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#include +#include +#include + +#include +#include + + +#define JZ_GPIO_BASE 0x10010000 + +void +jz_gpio_as_output (JZ_PIO * pio, unsigned int o) +{ + pio->PXFUNC = (1 << (o)); + pio->PXSELC = (1 << (o)); + pio->PXDIRS = (1 << (o)); +} + +void +jz_gpio_as_input (JZ_PIO * pio, unsigned int o) +{ + pio->PXFUNC = (1 << (o)); + pio->PXSELC = (1 << (o)); + pio->PXDIRC = (1 << (o)); +} + +void +jz_gpio_as_irq (JZ_PIO * pio, unsigned int o) +{ + pio->PXFUNC = (1 << (o)); + pio->PXSELS = (1 << (o)); + pio->PXDIRC = (1 << (o)); +} + +void +jz_gpio_set_pin (JZ_PIO * pio, unsigned int o) +{ + pio->PXDATS = (1 << (o)); +} + +void +jz_gpio_clear_pin (JZ_PIO * pio, unsigned int o) +{ + pio->PXDATC = (1 << (o)); +} + +void +jz_gpio_out (JZ_PIO * pio, unsigned int o, unsigned int val) +{ + if (val == 0) + pio->PXDATC = (1 << (o)); + else + pio->PXDATS = (1 << (o)); +} + +unsigned int +jz_gpio_get_pin (JZ_PIO * pio, unsigned int o) +{ + return (pio->PXPIN & (1 << o)) ? 1 : 0; +} + +int +jz_gpio_as_func (JZ_PIO * pio, unsigned int o, int func) +{ + switch (func) + { + case 0: + pio->PXFUNS = (1 << o); + pio->PXTRGC = (1 << o); + pio->PXSELC = (1 << o); + pio->PXPES = (1 << o); + return 1; + + case 1: + pio->PXFUNS = (1 << o); + pio->PXTRGC = (1 << o); + pio->PXSELS = (1 << o); + pio->PXPES = (1 << o); + return 1; + + case 2: + pio->PXFUNS = (1 << o); + pio->PXTRGS = (1 << o); + pio->PXSELC = (1 << o); + pio->PXPES = (1 << o); + return 1; + } + return 0; +} + +JZ_PIO * +jz_gpio_map (int port) +{ + JZ_PIO *pio; + + pio = (JZ_PIO *) jz_mmap (JZ_GPIO_BASE); + pio = (JZ_PIO *) ((unsigned int) pio + port * 0x100); + + return pio; +} diff --git a/Examples/ehw4/src/jz47xx_gpio.h b/Examples/ehw4/src/jz47xx_gpio.h new file mode 100644 index 0000000..d8b0113 --- /dev/null +++ b/Examples/ehw4/src/jz47xx_gpio.h @@ -0,0 +1,84 @@ +/* + JZ47xx GPIO at userspace + + Copyright (C) 2010 Andres Calderon andres.calderon@emqbit.com + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + +#ifndef __jz47xx_gpio_h__ +#define __jz47xx_gpio_h__ + +#define JZ_GPIO_PORT_A 0 +#define JZ_GPIO_PORT_B 1 +#define JZ_GPIO_PORT_C 2 +#define JZ_GPIO_PORT_D 3 + +typedef volatile unsigned int JZ_REG; /* Hardware register definition */ + +typedef struct _JZ_PIO +{ + JZ_REG PXPIN; /* PIN Level Register */ + JZ_REG Reserved0; + JZ_REG Reserved1; + JZ_REG Reserved2; + JZ_REG PXDAT; /* Port Data Register */ + JZ_REG PXDATS; /* Port Data Set Register */ + JZ_REG PXDATC; /* Port Data Clear Register */ + JZ_REG Reserved3; + JZ_REG PXIM; /* Interrupt Mask Register */ + JZ_REG PXIMS; /* Interrupt Mask Set Reg */ + JZ_REG PXIMC; /* Interrupt Mask Clear Reg */ + JZ_REG Reserved4; + JZ_REG PXPE; /* Pull Enable Register */ + JZ_REG PXPES; /* Pull Enable Set Reg. */ + JZ_REG PXPEC; /* Pull Enable Clear Reg. */ + JZ_REG Reserved5; + JZ_REG PXFUN; /* Function Register */ + JZ_REG PXFUNS; /* Function Set Register */ + JZ_REG PXFUNC; /* Function Clear Register */ + JZ_REG Reserved6; + JZ_REG PXSEL; /* Select Register */ + JZ_REG PXSELS; /* Select Set Register */ + JZ_REG PXSELC; /* Select Clear Register */ + JZ_REG Reserved7; + JZ_REG PXDIR; /* Direction Register */ + JZ_REG PXDIRS; /* Direction Set Register */ + JZ_REG PXDIRC; /* Direction Clear Register */ + JZ_REG Reserved8; + JZ_REG PXTRG; /* Trigger Register */ + JZ_REG PXTRGS; /* Trigger Set Register */ + JZ_REG PXTRGC; /* Trigger Set Register */ + JZ_REG Reserved9; + JZ_REG PXFLG; /* Port Flag Register */ + JZ_REG PXFLGC; /* Port Flag clear Register */ +} JZ_PIO, *PJZ_PIO; + +void jz_gpio_as_output (JZ_PIO * pio, unsigned int o); + +void jz_gpio_as_input (JZ_PIO * pio, unsigned int o); + +void jz_gpio_set_pin (JZ_PIO * pio, unsigned int o); + +void jz_gpio_clear_pin (JZ_PIO * pio, unsigned int o); + +void jz_gpio_out (JZ_PIO * pio, unsigned int o, unsigned int val); + +unsigned int jz_gpio_get_pin (JZ_PIO * pio, unsigned int o); + +int jz_gpio_as_func (JZ_PIO * pio, unsigned int o, int func); + +JZ_PIO *jz_gpio_map (int port); + +#endif diff --git a/Examples/ehw4/src/jz47xx_mmap.c b/Examples/ehw4/src/jz47xx_mmap.c new file mode 100644 index 0000000..93f70fa --- /dev/null +++ b/Examples/ehw4/src/jz47xx_mmap.c @@ -0,0 +1,64 @@ +/* + * JZ47xx GPIO lines + * + * Written 2010 by Andres Calderon andres.calderon@emqbit.com + */ + +#include +#include +#include +#include +#include +#include + +#include + + +void * +jz_mmap (off_t address) +{ + int fd; + + void *pio; + + if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1) + { + fprintf (stderr, "Cannot open /dev/mem.\n"); + return 0; + } + + pio = (void *) mmap (0, getpagesize (), PROT_READ | PROT_WRITE, MAP_SHARED, fd, address); + + if (pio == (void *) -1) + { + fprintf (stderr, "Cannot mmap.\n"); + return 0; + } + + return pio; +} + +void * +jz_fpga_map (off_t address) +{ + int fd; + + void *fpga; + + if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1) + { + fprintf (stderr, "Cannot open /dev/mem.\n"); + return 0; + } + + fpga = (void *) mmap (0, FPGA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, address); + + if (fpga == (void *) -1) + { + fprintf (stderr, "Cannot mmap.\n"); + return 0; + } + + return fpga; +} + diff --git a/Examples/ehw4/src/jz47xx_mmap.h b/Examples/ehw4/src/jz47xx_mmap.h new file mode 100644 index 0000000..dbb4821 --- /dev/null +++ b/Examples/ehw4/src/jz47xx_mmap.h @@ -0,0 +1,17 @@ +/* + * JZ47xx GPIO lines + * + * Written 2010 by Andres Calderon andres.calderon@emqbit.com + */ + +#ifndef __jz47xx_mmap_h__ +#define __jz47xx_mmap_h__ + +#include + +#define FPGA_SIZE (1 << 15) + +void *jz_mmap (off_t address); +void *jz_fpga_map (off_t address); + +#endif diff --git a/Examples/ehw4/src/sintesishw_client.c b/Examples/ehw4/src/sintesishw_client.c index 2f89fc4..482aea6 100644 --- a/Examples/ehw4/src/sintesishw_client.c +++ b/Examples/ehw4/src/sintesishw_client.c @@ -244,13 +244,13 @@ for(j=0;j<1;j++) int main( int argc, char** argv ) { - int *generacion, k, a, b, z, i, j= 0, error, nivel_max, vars, *tiempo, poblacion_total, m, p, iteraciones, T; - int conta=0, aux1, aux2, pentarboles, *fitness1, *fitness2, *entrada, *orderesult, aux, *aux_sal; - char o, *ap, *valor_devuelto;; - char *cromo_sal1, *cromo_sal2, *cromo_entrada; - int tamaobj, objetivo[8192], obj_combs;//= {0,254,123,16,87,56,34,76,89,155,199}; - long int tiempo1, tiempo2; - float tiempof, tiempof2, Tfloat, float1, float2; + int *generacion, k, a, b, z, i, j= 0, error, nivel_max, vars, *tiempo, poblacion_total, m, p, iteraciones, T; + int conta=0, aux1, aux2, pentarboles, *fitness1, *fitness2, *entrada, *orderesult, aux, *aux_sal; + char o, *ap, *valor_devuelto;; + char *cromo_sal1, *cromo_sal2, *cromo_entrada; + int tamaobj, objetivo[8192], obj_combs;//= {0,254,123,16,87,56,34,76,89,155,199}; + long int tiempo1, tiempo2; + float tiempof, tiempof2, Tfloat, float1, float2; int nivel2[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int nivel3[]={0,0,1,1,1,2,2,2,2,3,3, 3, 3, 4, 4, 4}; @@ -258,19 +258,19 @@ int main( int argc, char** argv ) int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; /* Estructuras para datos de cromosomas*/ - struct gen_datos_tipo data_struct1, data_struct2; - struct gen_datos_tipo *data_struct_ap1, *data_struct_ap2; - data_struct_ap1 = &data_struct1; - data_struct_ap2 = &data_struct2; + struct gen_datos_tipo data_struct1, data_struct2; + struct gen_datos_tipo *data_struct_ap1, *data_struct_ap2; + data_struct_ap1 = &data_struct1; + data_struct_ap2 = &data_struct2; /* Variables para tablas de lut*/ - FILE *f1; - int size1; - srand ( (time(NULL)) ); + FILE *f1; + int size1; + srand ( (time(NULL)) ); /* Variables para almacenar datos para graficar*/ - int *datos, *datos2, x, media=4, puntos; - FILE *fich, *fich2; + int *datos, *datos2, x, media=4, puntos; + FILE *fich, *fich2; char output_file_name[128], output_file_fitness_name[128]; sscanf(argv[1], "%i",&vars); @@ -279,14 +279,14 @@ int main( int argc, char** argv ) sscanf(argv[4], "%i", &nodos); sscanf(argv[5], "%s", output_file_name); sscanf(argv[6], "%s", output_file_fitness_name); -// printf("\nvars: %i indivs:%i generations:%i nodos:%i ", vars, poblacion_total, maxgeneraciones, nodos); + printf("\nvars: %i indivs:%i generations:%i nodos:%i ", vars, poblacion_total, maxgeneraciones, nodos); fflush(stdout); - pentarboles = 1; //MODIFIQUE NIVEL_MAX - nivel_max = 2; - poblacion = poblacion_total/nodos; + pentarboles = 1; //MODIFIQUE NIVEL_MAX + nivel_max = 2; + poblacion = poblacion_total/nodos; m = 1; //datos a migrar - p = 8; //frecuencia de migracion + p = maxgeneraciones;//8; //frecuencia de migracion T = 4; //temperatura para crear nuevos indiv. A mayor T menor temperatura i=0; tamaobj=0; @@ -297,85 +297,83 @@ int main( int argc, char** argv ) obj_combs = pow(2, (vars/2)); /*Armar funcion objetivo comparador*/ /* OJO, SE ESTÁN METIENDO VALORES DE 1EXX EN LAS LUT Y ESOS INDIVIDUOS AL PARECER QUEDAN MAL */ - for(a=0; a < obj_combs ;a++) - { - for(b=0; b < obj_combs ;b++) - { - if(a > b)z=1; if(a < b)z=2; if(a == b)z=4; - if((z & 0x4) != 0 ) - { - objetivo[tamaobj] = i; - printf("%i ",objetivo[tamaobj]); - tamaobj++; - } - i++; - } - } + for(a=0; a < obj_combs ;a++) + { + for(b=0; b < obj_combs ;b++) + { + if(a > b)z=1; if(a < b)z=2; if(a == b)z=4; + if((z & 0x4) != 0 ) + { + objetivo[tamaobj] = i; + printf("%i ",objetivo[tamaobj]); + tamaobj++; + } + i++; + } + } // printf("Tama:%i ",tamaobj); /* Tabla para las LUT*/ - f1 = fopen("funlut.dat","r"); - if(f1 == NULL){ - printf("\nError de lectura de archivo!"); - return 0;} + f1 = fopen("funlut.dat","r"); + if(f1 == NULL){ + printf("\nError de lectura de archivo!"); + return 0;} - fseek (f1, 0, SEEK_END); - size1 = ftell(f1); - funlut_ap = malloc(size1); if(funlut_ap==0) printf("Error en malloc"); - rewind (f1); - fread(funlut_ap,1,size1,f1); - fclose(f1); + fseek (f1, 0, SEEK_END); + size1 = ftell(f1); + funlut_ap = malloc(size1); if(funlut_ap==0) printf("Error en malloc"); + rewind (f1); + fread(funlut_ap,1,size1,f1); + fclose(f1); - puntos = 16; /*numero de puntos para la grafica*/ - datos = malloc(sizeof(datos)*puntos*3); if(datos==0) printf("Error en malloc"); - fich=fopen(output_file_name,"wb"); - datos2 = malloc(sizeof(datos2) * maxgeneraciones * p * nodos); if(datos2==0) printf("Error en malloc"); - fich2=fopen(output_file_fitness_name,"wb"); + puntos = 16; /*numero de puntos para la grafica*/ + datos = malloc(sizeof(datos)*puntos*3); if(datos==0) printf("Error en malloc"); + fich=fopen(output_file_name,"wb"); + datos2 = malloc(sizeof(datos2) * maxgeneraciones * p * nodos); if(datos2==0) printf("Error en malloc"); + fich2=fopen(output_file_fitness_name,"wb"); cromo_sal1 = malloc(sizeof(cromo_sal1) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal1==0) printf("Error en malloc"); - fitness1 = malloc(sizeof(fitness1) * RESULTADOS * nodos); if(fitness1==0) printf("Error en malloc"); - cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc"); - fitness2 = malloc(sizeof(fitness2) * RESULTADOS * nodos); if(fitness2==0) printf("Error en malloc"); - cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc"); - generacion = malloc(sizeof(generacion)* RESULTADOS * nodos); if(generacion==0) printf("Error en malloc"); - tiempo = malloc(sizeof(tiempo)* RESULTADOS * nodos); if(tiempo==0) printf("Error en malloc"); - cromo_entrada = malloc(sizeof(cromo_entrada)* LONG_INDIV * m); if(cromo_entrada==0) printf("Error en malloc"); + fitness1 = malloc(sizeof(fitness1) * RESULTADOS * nodos); if(fitness1==0) printf("Error en malloc"); + cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc"); + fitness2 = malloc(sizeof(fitness2) * RESULTADOS * nodos); if(fitness2==0) printf("Error en malloc"); + cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc"); + generacion = malloc(sizeof(generacion)* RESULTADOS * nodos); if(generacion==0) printf("Error en malloc"); + tiempo = malloc(sizeof(tiempo)* RESULTADOS * nodos); if(tiempo==0) printf("Error en malloc"); + cromo_entrada = malloc(sizeof(cromo_entrada)* LONG_INDIV * m); if(cromo_entrada==0) printf("Error en malloc"); orderesult = malloc(sizeof(orderesult) * nodos*RESULTADOS); if(orderesult==0) printf("Error en malloc"); aux_sal = malloc(sizeof(aux_sal) * nodos * maxgeneraciones * 2); if(aux_sal==0) printf("Error en malloc"); - data_struct_ap1->objetivo = objetivo; - data_struct_ap1->tamaobj = tamaobj; - data_struct_ap1->pentarboles = pentarboles; - data_struct_ap1->maxgen = maxgeneraciones; - data_struct_ap1->cromo_sal = cromo_sal1; - data_struct_ap1->fitness = fitness1; - data_struct_ap1->cromo_entrada = cromo_entrada; - data_struct_ap1->fitness_entrada = 0; - data_struct_ap1->nivel_max = nivel_max; - data_struct_ap1->vars= vars; - data_struct_ap1->en_cromo_entrada = 0; - data_struct_ap1->generacion = generacion; - data_struct_ap1->tiempo = tiempo; - data_struct_ap1->aux = aux; - data_struct_ap1->aux_sal = aux_sal; + data_struct_ap1->objetivo = objetivo; + data_struct_ap1->tamaobj = tamaobj; + data_struct_ap1->pentarboles = pentarboles; + data_struct_ap1->maxgen = maxgeneraciones; + data_struct_ap1->cromo_sal = cromo_sal1; + data_struct_ap1->fitness = fitness1; + data_struct_ap1->cromo_entrada = cromo_entrada; + data_struct_ap1->fitness_entrada = 0; + data_struct_ap1->nivel_max = nivel_max; + data_struct_ap1->vars= vars; + data_struct_ap1->en_cromo_entrada = 0; + data_struct_ap1->generacion = generacion; + data_struct_ap1->tiempo = tiempo; + data_struct_ap1->aux = aux; + data_struct_ap1->aux_sal = aux_sal; /* printf("\npentarboles:%i nivel_max%i ", pentarboles ,nivel_max);*/ /* fflush(stdout); */ -/* Iniciar evolucion */ - - x = 0; - fflush(stdout); + /* Iniciar evolucion */ + x = 0; for(k = 0; k < iteraciones ; k++) { tiempo1 = get_timestamp(); - iniciar_evol(data_struct_ap1); + iniciar_evol(data_struct_ap1); //evolution tiempo2 = get_timestamp(); *tiempo = tiempo2 - tiempo1; tiempof2 = *tiempo; tiempof = tiempof2/(1000000); -// printf("\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof); - fprintf(fich, "\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof); + printf("\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof); + //fprintf(fich, "\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof); for(i = 0; i < nodos*RESULTADOS; i++) //Organizar lo q llego, ¡solo se indexa orderesult, que dice en q orden estan los cromosomas! { diff --git a/Examples/ehw4/src/sintesishw_client.h b/Examples/ehw4/src/sintesishw_client.h index 7a14ada..09aedd9 100644 --- a/Examples/ehw4/src/sintesishw_client.h +++ b/Examples/ehw4/src/sintesishw_client.h @@ -17,7 +17,7 @@ #define PORT 3550 /* El puerto que sera abierto */ #define BACKLOG 2 /* El numero de conexiones permitidas */ #define MAXDATASIZE 10000 /* El numero maximo de datos en bytes */ -#define IP0 "192.168.0.4" +#define IP0 "192.168.254.101" #define IP1 "192.168.0.6" #define IP2 "192.168.0.7" #define IP3 "192.168.0.8" diff --git a/Examples/ehw4/src/sintesishw_server.c b/Examples/ehw4/src/sintesishw_server.c index 3a0d9d8..208e473 100644 --- a/Examples/ehw4/src/sintesishw_server.c +++ b/Examples/ehw4/src/sintesishw_server.c @@ -121,9 +121,11 @@ int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; /** iniciar periferico **/ if(HW_ENABLE == 1){ evalfit_ptr1 = (int *)init_peripheral(EVALFIT_PHYSBASE1, basemem); +/* evalfit_ptr2 = (int *)init_peripheral(EVALFIT_PHYSBASE2, basemem); evalfit_ptr3 = (int *)init_peripheral(EVALFIT_PHYSBASE3, basemem); evalfit_ptr4 = (int *)init_peripheral(EVALFIT_PHYSBASE4, basemem); +*/ } data_struct_ap1 = &data_struct1; data_struct_ap2 = &data_struct2; @@ -164,13 +166,13 @@ int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; ap1 = data_socket_rx_ap; /* Cargar datos en la estructura para la evolucion */ data_struct_ap1->tamaobj = htonl(*(int *)ap1) & 0xFFFF; maxgeneraciones = htonl(*(int *)ap1) >> 16; -// printf("\ntama obj:%0x maxgens:%i numbytes:%i-- ", data_struct_ap1->tamaobj, maxgeneraciones,numbytes); + //printf("\ntama obj:%0x maxgens:%i numbytes:%i-- ", data_struct_ap1->tamaobj, maxgeneraciones,numbytes); fflush(stdout); ap1 = ap1 + 4; for(i=0;i < (data_struct_ap1->tamaobj); i++) { objetivo[i] =htonl(*(int *)ap1); //XX -// printf("obj:%i %i ",i,objetivo[i]); + //printf("obj:%i %i ",i,objetivo[i]); ap1 = ap1 + 4; } data_struct_ap1->objetivo = objetivo; @@ -181,7 +183,7 @@ int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; ap1 = ap1 + 4; data_struct_ap1->tamacrom = htonl(*(int *)ap1) & 0xFFFF; poblacion = htonl(*(int *)ap1) >> 16; -// printf("\ntamacrom:%0x poblacion:%i vars:%i pentarboles:%i maxgeneraciones:%i ", data_struct_ap1->tamacrom, poblacion,vars,pentarboles,maxgeneraciones); + //printf("\ntamacrom:%0x poblacion:%i vars:%i pentarboles:%i maxgeneraciones:%i ", data_struct_ap1->tamacrom, poblacion,vars,pentarboles,maxgeneraciones); ap1 = ap1 + 4; cromo_entrada = malloc(sizeof(cromo_entrada) * RESULTADOS * LONG_INDIV); if(cromo_entrada==0) printf("Error en malloc"); data_struct_ap1->cromo_entrada = cromo_entrada; @@ -196,9 +198,9 @@ int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; ap1 = ap1 + 4; data_struct_ap1->nivel_max = htonl(*(int *)ap1) & 0xFFFF; data_struct_ap1->en_cromo_entrada = htonl(*(int *)ap1) >> 16; //han enviado un cromosoma? - ap1 = ap1 + 4; - data_struct_ap1->aux = htonl(*(int *)ap1); -/* printf("\nnivel_max:%0x-- ", data_struct_ap1->nivel_max);*/ + ap1 = ap1 + 4; + data_struct_ap1->aux = htonl(*(int *)ap1); + //printf("\nnivel_max:%0x-- ", data_struct_ap1->nivel_max); data_struct_ap1->maxgen = maxgeneraciones; cromo_sal1 = malloc(sizeof(cromo_sal1) * RESULTADOS * LONG_INDIV); if(cromo_sal1==0) printf("Error en malloc"); /* reservar para cromosomas */ @@ -220,11 +222,13 @@ int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; evolucionar(data_struct_ap1); // evolucionar -/* for(i=0; i < RESULTADOS ;i++)*/ -/* {*/ -/* printf(" %i-- fit:%i \t",i , *(fitness1 + i));*/ -/* mostrar_indiv(cromo_sal1 + (i*LONG_INDIV), pentarboles, vars);*/ -/* }*/ +/* + for(i=0; i < RESULTADOS ;i++) + { + printf(" %i-- fit:%i \t",i , *(fitness1 + i)); + mostrar_indiv(cromo_sal1 + (i*LONG_INDIV), pentarboles, vars); + } +*/ ap1 = data_socket_tx_ap; /* devolver resultados */ *(int *)ap1 = ntohl(LONG_INDIV); @@ -265,9 +269,11 @@ int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0}; if(HW_ENABLE == 1){ close_peripheral(evalfit_ptr1); +/* close_peripheral(evalfit_ptr2); close_peripheral(evalfit_ptr3); close_peripheral(evalfit_ptr4); +*/ } close(basemem); diff --git a/Examples/ehw4/src/test/EvalfitTest.c b/Examples/ehw4/src/test/EvalfitTest.c new file mode 100755 index 0000000..0b771fb --- /dev/null +++ b/Examples/ehw4/src/test/EvalfitTest.c @@ -0,0 +1,752 @@ +/*********************************************************************************************************** +* programa para hacer test perifericos evalfit del proyecto ehw4, con mersenne twister incorporado +* +* Funciona con HW evalfit hasta con 14 vars. Evalfit funciona para 5 arboles. nivel max de 4. +* +************************************************************************************************************/ + +// clk => PLB_Clk, +// reset => regs_wr(0)(0), +// habilita => regs_wr(0)(1), +// maxcombs => regs_wr(4)(16 to 31), +// nivel_max => regs_wr(4)(12 to 15), +// peripheral_mem_in => do_mem_usr,-- 64 bits +// peripheral_mem_en => en_mem_usr, +// peripheral_mem_out => di_mem_usr, -- 64 bits +// peripheral_mem_we => we_mem_usr, +// peripheral_mem_addr => addr_mem_usr, --9 bits +// evalfit3_estado => regs_rd(1)(8 to 15), +// errores => regs_rd(2)(16 to 31), +// fin_ack => regs_rd(1)(31) ); + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "jz47xx_gpio.h" +#include + +#define CS2_PORT JZ_GPIO_PORT_B +#define CS2_PIN 26 + +typedef long long timestamp_t; +static timestamp_t + get_timestamp () +{ + struct timeval now; + gettimeofday (&now, NULL); + return now.tv_usec + (timestamp_t)now.tv_sec *1000000 ; +} + + +int nivel2(int pentarboles) +{ +int x; + if(nivel1 > 1) + x = (int)ceil((float)nivel1 / 4); + else + x = 0; +return x; + +} +int nivel3(int pentarboles) +{ +int x; + if(nivel2(pentarboles) > 1) + x = (int)ceil((float)nivel1 / 16); + else + x = 0; +return x; +} +int nivel4(int pentarboles) +{ +int x; + if(nivel3(pentarboles) > 1) + x = (int)ceil((float)nivel1/ 64); + else + x = 0; +return x; +} +int nivel5(int pentarboles) +{ +int x; + if(nivel4(pentarboles) > 1) + x = (int)ceil((float)nivel1 / 256); + else + x = 0; +return x; +} + + +/************************************************************************************************************************************** + genera un numero de forma aleatoria*/ +char random_var(int max) +{ + char variable, mascara; + int i; + mascara = 1; + do + { + mascara = (mascara << 1) + 1; + }while(max > mascara); + variable = random() & mascara; + + while(variable > max) + { + variable = variable - max; + } + return variable; +} + +/************************************************************************************************************************************** + imprime un cromosoma completo */ +void mostrar_indiv_lut(char *cromo, int pentarboles) +{ + char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES + char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'}; + ap = cromo; + + if(*(cromo + 7) == 1) + { + vn[VARS] = '.'; + 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]); + } + else + { + vn[4] = '.'; + 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]); + } + printf("\t %0x %0x ", *(short int *)(ap+2), *(ap+6) ); + for(i = 0; i < LONG_ARBOL; i++) + { + printf("%0x,",*(cromo+i)); + } +// printf("\n"); +} + + +/************************************************************************************************************************************** + imprime un cromosoma completo */ +void mostrar_indiv(char *cromo, int pentarboles) +{ +char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES +char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'}; + +ap = cromo; +for(i = 0; i < ARBOLES_INDIV; i++){ + if(*(cromo + (i * LONG_ARBOL) + 7) == 1) + { + vn[VARS] = '.'; + 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)]); + } + else + { + vn[4] = '.'; + 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)]); + } + ap = ap + LONG_ARBOL; +} +printf("\t"); +//for(i = 0; i < LONG_INDIV; i++) +//{ +// printf("%i,",*(cromo+i)); +//} +printf("\n"); +} + +/************************************************************************************************************************************** + imprime un cromosoma completo */ +void mostrar_indiv2(char *cromo, int pentarboles) +{ +char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES +char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'}; + +ap = cromo; + + if(*(cromo + (i * LONG_ARBOL) + 7) == 1) + { + vn[VARS] = '.'; + 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)]); + } + else + { + vn[4] = '.'; + 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)]); + } + +printf("\n"); +} + + +/************************************************************************************************************************************** + Genera un arbol de forma aleatoria + Las funciones y variables son representadas por enteros del 0 al 3 o mas... + - el arbol se encuentra compuesto de tres funciones, dos de entrada y una de salida. +*/ +void gen_arbol_lut(char *ap, int variables) +{ + int lut; + short int dato_lut; + lut = random_var(FUNCOMBS-1); + dato_lut = (*(funlut_ap+(lut*2)) << 8) + (*(funlut_ap+(lut*2)+1)); + *(short int *)ap = dato_lut; + *(ap+2) = *(ap+3) = 0; + *(ap+2) = random_var(variables-1) + (random_var(variables-1) << 4); + *(ap+3) = random_var(variables-1) + (random_var(variables-1) << 4); + *(ap+6) = lut;(ap); +} + + +/************************************************************************************************************************************** + Genera un arbol de forma aleatoria + - estructura de un arbol: FFVVFVV F = funcion V = variable + Las funciones y variables son representadas por enteros del 0 al 3 o mas... + - el arbol se encuentra compuesto de tres funciones, dos de entrada y una de salida. +*/ +void gen_arbol(char *ap, int variables) +{ + *ap = random_var((FUNCIONES)); //funcion de salida, no YES + *(ap+1) = random_var(FUNCIONES); //PRIMER ARBOL + *(ap+2) = random_var(variables); + *(ap+3) = random_var(variables); + *(ap+4) = random_var(FUNCIONES); //SEGUNDO ARBOL + *(ap+5) = random_var(variables); + *(ap+6) = random_var(variables); + +} + + +/************************************************************************************************************************************** + genera un individuo de forma aleatoria */ +void gen_indiv(char *cromo, int pentarboles) +{ +char i=0; +int n1, n2, n3, n4, n5, c1, c2, c3, c4, c5, sig, indice; +n1 = n2 = n3 = n4 = n5 = 0; +c1 = c2 = c3 = c4 = c5 = 0; +indice = 0; + +sig=1; + + +do +{ + +if((sig == 1) && (c1 < nivel1)) +{ + gen_arbol(cromo + (indice * LONG_ARBOL), VARS-1); + *(cromo +(indice*LONG_ARBOL) + 7) = 1; + n1 ++; + c1 ++; + indice ++; + if(n1 < 4) + sig = 1; + else + { + sig = 2; + n1 = 0; + }//printf("1"); +} +else +{ + if((sig == 2) && (c2 < nivel2(pentarboles))) + { + gen_arbol(cromo + (indice*LONG_ARBOL), 3); + *(cromo +(indice*LONG_ARBOL) + 7) = 2; + n2++; + c2 ++; + indice ++; + if(c2 == nivel2(pentarboles)) + sig = 3; + else + { + if(n2 < 4) + sig = 1; + else + { + sig = 3; + n2 = 0; + } + }//printf("2"); + } + else + { + if((sig == 3) && (c3 < nivel3(pentarboles))) + { + gen_arbol(cromo + (indice*LONG_ARBOL), 3); + *(cromo +(indice*LONG_ARBOL) + 7) = 3; + n3++; + c3++; + indice++; + if(c3 == nivel3(pentarboles)) + sig = 4; + else + { + if(n3 < 4) + sig = 1; + else + { + sig = 4; + n3 = 0; + } + }//printf("3"); + } + else + { + if((sig == 4) && (c4 < nivel4(pentarboles))) + { + gen_arbol(cromo + (indice*LONG_ARBOL), 3); + *(cromo +(indice*LONG_ARBOL) + 7) = 4; + n4++; + c4++; + indice++; + if(c4 == nivel4(pentarboles)) + sig = 5; + else + { + if(n4 < 4) + sig = 1; + else + { + sig = 5; + n4 = 0; + } + }//printf("4"); + } + else + { + if((sig == 5) && (c5 < nivel5(pentarboles))) + { + gen_arbol(cromo + (indice*LONG_ARBOL), 3); + *(cromo +(indice*LONG_ARBOL) + 7) = 5; + c5++; + indice++; + if(c5 == nivel5(pentarboles)) + sig = 1; + else + sig = 1; + } + } + + } + } + +} + +}while(indice < ARBOLES_INDIV); + +} + +/************************************************************************************************************************************** + halla la salida de un arbol para una entrada de x de un cromo basado en LUT*/ +int eval_func_lut(char *ap, int x ) //var apunta al valor de las variables +{ +char Y; +char var[VARS], i, a, b, c, d; +int lut; + +for(i=0;i <= VARS-1;i++) +{ + var[i] = (x >> i) & 0x1; +// printf("-%i",var[i]); +} +var[VARS] = 0; + +a = *(ap + 3) & 0xF; +b = (*(ap + 3) >> 4) & 0xF; +c = *(ap + 2) & 0xF; +d = (*(ap + 2) >> 4) & 0xF; +i = var[a] + (var[b]*2) + (var[c]*4) + (var[d]*8); +lut = *(short int *)ap; +Y = (lut >> i) & 0x1; +return Y; +} + +/************************************************************************************************************************************** + halla la salida de un arbol para una entrada de x */ +int eval_func(char *ap, int x ) //var apunta al valor de las variables +{ +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 +char apl0, apl11, apl12; +char Y, f1, f2; +char var[VARS], i; + + +for(i=0;i <= VARS-1;i++) +{ + var[i] = (x >> i) & 0x1; +// printf("-%i",var[i]); +} +var[VARS] = 0; + + f1 = 2*apfun[((*(ap+1))*4) + (2* (*(var+(*(ap+2))))) + *(var+(*(ap+3))) + 1]; +// ^2da funcion ^1ra variable ^2da variable + f2 = apfun[((*(ap+4))*4) + (2* (*(var+(*(ap+5))))) + *(var+(*(ap+6))) + 1]; +// ^3ra funcion ^3ra variable ^4da variable + Y = apfun[((*(ap))*4) + f1 + f2 + 1]; +// ^1ra funcion +// printf("\n"); + +return Y; +} + + + +/************************************************************************************************************************************** + retorna las salidas de un cromosoma de 5 arboles*/ +void eval_pentarbol_sw(char *ap, int *salida, int *entrada) +{ + int i, k ; + char salidas[ARBOLES][COMBS], aux[COMBS]; + for(i=0; i <= ARBOLES-2; i++){ //se evaluan las salidas de los primeros arboles y se almacenan en salidas + for(k=0; k<= (COMBS-1); k++) + { + salidas[i][k] = eval_func_lut((ap+(i*LONG_ARBOL)), k); + } + } + + //se calculan los minterminos para el arbol de salida + for(k=0; k <= (COMBS-1); k++) + { + aux[k] = ((salidas[0][k])*1) + ((salidas[1][k])*2) + ((salidas[2][k])*4) + ((salidas[3][k])*8); + } + + for(i=0; i <= (COMBS-1); i++) + { + *(salida + i) = eval_func_lut(ap + ((ARBOLES-1) * LONG_ARBOL), aux[i]); + } +} + + +/************************************************************************************************************************************** + retorna el numero de errores y las salidas de un cromosoma de 1 o mas pentarboles*/ +int eval_fit_sw(char *ap, int *objetivo, int num_min, int pentarboles ) +{ + int *obj_min, *med_min2, *med_min3, *med_min4, *med_min5, *entrada, i, j, errores, puertas; + int *salida, n2, n3, n4, n5, x; + + obj_min = malloc(sizeof(obj_min)*COMBS); + med_min2 = malloc(sizeof(med_min2)*COMBS); + med_min3 = malloc(sizeof(med_min3)*COMBS); + med_min4 = malloc(sizeof(med_min4)*COMBS); + med_min5 = malloc(sizeof(med_min5)*COMBS); + entrada = malloc(sizeof(entrada)*COMBS); + salida = malloc(sizeof(salida)*COMBS); + errores = 0; + + for(i=0; i < COMBS; i++) + { + *(obj_min+i) = 0; + *(entrada+i) = i; + *(med_min2 + i) = 0; + *(med_min3 + i) = 0; + *(med_min4 + i) = 0; + *(med_min5 + i) = 0; + } + + for(i = 0; i < num_min; i++) + { + *(obj_min + (*(objetivo + i))) = 1; //se convierte el objetivo a un arreglo + } + + i = 0; + n2 = 0; + n3 = 0; + n4 = 0; + n5 = 0; + + do + { + if(*(ap + (i * LONG_ARBOL) + 7) == 1) + { + eval_pentarbol_sw(ap + (i * LONG_ARBOL), salida, entrada); + for(j = 0; j < COMBS; j++) + { + *(med_min2 + j) = *(med_min2 + j) + (*(salida + j) << n2); +// printf("%i ",*(med_min2 + j));////////quitar + } + if(n2 == 3) n2 = 0; else n2++; + i = i + 5; +// printf(" ");////////quitar + } + else + { + if(*(ap + (i * LONG_ARBOL) + 7) == 3) + { + for(j = 0; j < COMBS; j++) + { + *(salida + j) = eval_func_lut(ap + (i * LONG_ARBOL), *(med_min2 + j)); + } + for(j = 0; j < COMBS; j++) + { + *(med_min3 + j) = *(med_min3 + j) + (*(salida + j) << n3); +// printf("%i ",*(med_min3 + j));////////quitar + } + if(n3 == 3) n3 = 0; else n3++; + i++; + } + else + { + + if(*(ap + (i * LONG_ARBOL) + 7) == 4) + { + for(j = 0; j < COMBS; j++) + { + *(salida + j) = eval_func_lut(ap + (i * LONG_ARBOL), *(med_min3 + j)); + } + for(j = 0; j < COMBS; j++) + { + *(med_min4 + j) = *(med_min4 + j) + (*(salida + j) << n4); + } + if(n4 == 3) n4 = 0; else n4++; + i++; + } + } + } + }while(i <= (ARBOLES_INDIV-1)); + + if(*(ap + ((ARBOLES_INDIV-1) * LONG_ARBOL) + 7) == 2) + { + for(j = 0; j < COMBS; j++) //se evalua el arbol de salida + { + errores = errores + abs(*(med_min2 + j) - *(obj_min + j)); //errores + } + } + + if(*(ap + ((ARBOLES_INDIV-1) * LONG_ARBOL) + 7) == 3) + { + for(j = 0; j < COMBS; j++) //se evalua el arbol de salida + { + errores = errores + abs(*(med_min3 + j) - *(obj_min + j)); //errores + } + } + + if(*(ap + ((ARBOLES_INDIV-1) * LONG_ARBOL) + 7) == 4) + { + for(j = 0; j < COMBS; j++) //se evalua el arbol de salida + { + errores = errores + abs(*(med_min4 + j) - *(obj_min + j)); //errores + } + } + + if(*(ap + ((ARBOLES_INDIV-1) * LONG_ARBOL) + 7) == 5) + { + for(j = 0; j < COMBS; j++) //se evalua el arbol de salida + { + errores = errores + abs(*(med_min5 + j) - *(obj_min + j)); //errores + } + } + free(obj_min); + free(med_min2); + free(med_min3); + free(med_min4); + free(med_min5); + free(entrada); + free(salida); + + puertas = 0; + + return ((errores * PESO_SALIDA) + puertas); +} + +/************************************************************************************************************************************** + Introducir minterminos **/ +void minterm2peripheral(int *fun_obj, int tamano) +{ int i, j, a; + + for(i = 0; i < 512; i++) //rellenar con 0 + { + *(int *)(evalfit_ptr + EVALFIT_OBJOFFSET + (i*4)) = 0; + } + + for(i = 0; i < tamano; i++) //insertar 1's en segmento de mem objetivo + { + j=0; + a=fun_obj[i]; + while(a > 31) + { + a -= 32; + j++; + } + *(int *)(evalfit_ptr + EVALFIT_OBJOFFSET + (j*4)) += (1 << (31-a)); + } +} +/*************************************************************************************************************************************/ + + +/************************************************************************************************************************************** + map peripheral **/ +int periph_map(off_t offset) +{ +int basemem, baseperiph; + basemem = open("/dev/mem", (O_RDWR | O_SYNC)); //abrir dispositivo memoria para mapear dir del periferico + if(basemem == -1) + { + printf("Error to open /dev/mem \n"); + return -1; + } + baseperiph = (int )mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, basemem, offset);// & ~MAP_MASK); + if (baseperiph == -1) + { + printf ("Cannot mmap.\n"); + return -1; + } +return baseperiph; +} + + +/************************************************************************************************************************************** +main **/ +int main() +{ + int *aux, base_periferico, *ConfigRegsBase_ptr, basemem, maxcombs, nivel_max; + int a, b, c, i,j, pentarboles, sum1, sum2, sum3; + long int tiempo1, tiempo2; + char *cromo, *cromo2, *obj; + short int dato_lut; + int fun_obj[16]; + void *pio; + + for(i=0;i<16;i++) + { + fun_obj[i] = i; + } + +/** Variables para tablas de lut **/ + FILE *f1; + int size1; + + srand ( (time(NULL)) ); + + cromo = malloc(sizeof(char) * LONG_ARBOL*21); + if(cromo==0) printf("Error en malloc"); + cromo2 = malloc(sizeof(char) * LONG_ARBOL*21); + if(cromo2==0) printf("Error en malloc"); + obj = malloc(COMBS); + if(obj==0) printf("Error en malloc"); + a= sizeof(fun_obj)/4; + printf("{%0x %0x}",a,COMBS); + + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define FUNCIONES 5 +#define FUNCOMBS (int) pow(FUNCIONES, 3) +#define VARS 4 +#define COMBS (int) pow(2,VARS) + +#define POBLACION 64 +#define ARBOLES 5 +#define LONG_ARBOL 8 + +/************************************************************************************************************************************** + halla la salida de un arbol para una entrada de x */ +int eval_func(char *ap, int x ) //var apunta al valor de las variables +{ + 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 + char apl0, apl11, apl12; + char Y, f1, f2; + char var[VARS], i; + + for(i=0;i <= VARS-1;i++) + { + var[i] = (x >> i) & 0x1; + //printf("-%i",var[i]); + } + var[VARS] = 0; + + f1 = 2*apfun[((*(ap+1))*4) + (2* (*(var+(*(ap+2))))) + *(var+(*(ap+3))) + 1]; +// ^2da funcion ^1ra variable ^2da variable + f2 = apfun[((*(ap+4))*4) + (2* (*(var+(*(ap+5))))) + *(var+(*(ap+6))) + 1]; +// ^3ra funcion ^3ra variable ^4da variable + Y = apfun[((*(ap))*4) + f1 + f2 + 1]; +// ^1ra funcion +// printf("f1:%i f2:%i Y:%i ",f1,f2,Y); + return Y; +} + +/************************************************************************************************************************************** + imprime un cromosoma completo */ +mostrar_indiv(char *cromo, int pentarboles) +{ + char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES + char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'}; + + ap = cromo; + for(i = 0; i < 1; i++){ + vn[VARS] = '.'; + 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)]); + } + printf("\n"); +} + + +main() +{ +char *cromo, *tabla,*puertas; +int i, j, k, l, salida,x; +char ruta[]="funlut.dat"; +char ruta2[]="puertas.dat"; +short int *funlut_ap; +int size1; + +tabla = malloc(sizeof(short int) * FUNCOMBS*4); +if(tabla==0) printf("Error en malloc"); +puertas = malloc(sizeof(short int) * FUNCOMBS*4); +if(tabla==0) printf("Error en malloc"); +cromo = malloc(sizeof(char) * FUNCOMBS); +if(cromo==0) printf("Error en malloc"); + +FILE *f1, *fich=fopen(ruta,"wb"); +FILE *f2, *fich2=fopen(ruta2,"wb"); + +printf("COMBS%i ", FUNCOMBS); + +for(i = 0; i < FUNCIONES; i++) +{ + for(j = 0; j < FUNCIONES; j++) + { + for(k = 0; k < FUNCIONES; k++) + { + x = 0; + for(l = 0; l < COMBS; l++) + { + *(puertas + ((i*25) + (j*5) + k))=0; + *cromo = i; + *(cromo+1) = j; + *(cromo+2) = 0; + *(cromo+3) = 1; + *(cromo+4) = k; + *(cromo+5) = 2; + *(cromo+6) = 3; + salida = eval_func(cromo, l); + printf("%i", salida); + x = x + (salida << l); + if(*cromo != 4) + (*(puertas + ((i*25) + (j*5) + k) ))++; + if(*(cromo + 1) != 4) + (*(puertas + ((i*25) + (j*5) + k)))++; + if(*(cromo + 4) != 4 && *cromo != 4) + (*(puertas + ((i*25) + (j*5) + k)))++; + } + *(tabla + ((i*25) + (j*5) + k)*2 + 1 ) = 0xFF & (x>>8); // se guarda con little endian, para JZ e intel. + *(tabla + ((i*25) + (j*5) + k)*2 ) = 0xFF & x; // intercambia estos dos para cambiar endianismo + 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)); + mostrar_indiv(cromo, 1); + } + } +} + +fwrite((short int *)tabla, FUNCOMBS, sizeof(short int),fich); +fclose(fich); +fwrite((char *)puertas, FUNCOMBS, sizeof(char),fich2); +fclose(fich2); + +f1 = fopen("funlut.dat","r"); +if(f1 == NULL){ + printf("\nError de lectura de archivo!"); + return 0;} +fseek (f1, 0, SEEK_END); +size1 = ftell(f1); +funlut_ap = malloc(size1); +if(funlut_ap==0) printf("Error en malloc"); +rewind (f1); +fread(funlut_ap,2,size1/(sizeof(short int)),f1); +fclose(f1); + for(i = 0; i < (size1/sizeof(short int)); i++) +printf("\n%0x : %0x ",i, *(unsigned short int*)(funlut_ap+i)); +free(funlut_ap); + +}