Date:2010-10-12 17:11:07 (9 years 1 month ago)
Author:César Pedraza
Commit:c48feff62c7e8a7b8a432184839eae88c58f6343
Message:..

Files: Examples/ehw4/logic/ehw.v (5 diffs)
Examples/ehw4/logic/evalfit_peripheral.vhd (1 diff)
Examples/ehw4/src/Makefile (2 diffs)
Examples/ehw4/src/genetic.c (7 diffs)
Examples/ehw4/src/genetic.h (2 diffs)
Examples/ehw4/src/jz47xx_gpio.c (1 diff)
Examples/ehw4/src/jz47xx_gpio.h (1 diff)
Examples/ehw4/src/jz47xx_mmap.c (1 diff)
Examples/ehw4/src/jz47xx_mmap.h (1 diff)
Examples/ehw4/src/sintesishw_client.c (4 diffs)
Examples/ehw4/src/sintesishw_client.h (1 diff)
Examples/ehw4/src/sintesishw_server.c (6 diffs)
Examples/ehw4/src/test/EvalfitTest.c (1 diff)
Examples/ehw4/src/test/Makefile (2 diffs)
Examples/ehw4/src/test/evalfit.h (1 diff)
Examples/ehw4/src/test/gen_fun_lut.c (1 diff)

Change Details

Examples/ehw4/logic/ehw.v
1212    // synchronize signals
1313    reg sncs, snwe;
1414    reg [12:0] buffer_addr;
15    reg [B:0] buffer_data;
16     wire led;
15    reg [B:0] buffer_data;
1716
1817    // bram-cpu interfaz
1918    reg we;
2019    reg w_st=0;
2120    reg [B:0] wdBus;
22    reg [B:0] rdBus;
21    wire [B:0] rdBus;
2322    wire [12:0] addr;
2423    reg [7:0] bae;
2524
...... 
4544    wire [3:0] max_lev;
4645    wire [7:0] control;
4746
48    reg [7:0] reg_bank [31:0];
49    wire enReg;
50    wire [4:0] address;
51
52// Test : LED blinking
47     // Test : LED blinking
5348    reg [25:0] counter;
5449    always @(posedge clk) begin
5550        if (~reset)
...... 
5954    end
6055    assign led = counter[24];
6156
62// Data Bus direction control
57    // Data Bus direction control
6358    wire T = ~noe | ncs;
6459    assign sram_data = T?8'bZ:rdBus;
6560
66// synchronize assignment
61    // synchronize assignment
6762    always @(negedge clk)
6863    begin
6964        sncs <= ncs;
...... 
7267        buffer_addr <= addr;
7368    end
7469
75// write access cpu to bram
70    // write access cpu to bram
7671    always @(posedge clk)
7772    if(~reset) {w_st, we, wdBus} <= 0;
7873      else begin
...... 
115110     else
116111       bae <= 8'h00;
117112   end
118    wire en1, en2; // enable memory signals
119    assign en0 = bae[0] | bae[1] | bae[2] | bae[3];
120    assign en1 = bae[4] | bae[5] | bae[6] | bae[7];
121113
122    reg[31:0] DIA_Aux;
123    always @ (posedge clk) begin
124        if (bae[0]) DIA_Aux[7:0] = wdBus[7:0];
125        if (bae[1]) DIA_Aux[15:8] = wdBus[7:0];
126        if (bae[2]) DIA_Aux[23:16] = wdBus[7:0];
127        if (bae[3]) DIA_Aux[31:24] = wdBus[7:0];
128        if (bae[4]) DIA_Aux[7:0] = wdBus[7:0];
129        if (bae[5]) DIA_Aux[15:8] = wdBus[7:0];
130        if (bae[6]) DIA_Aux[23:16] = wdBus[7:0];
131        if (bae[7]) DIA_Aux[31:24] = wdBus[7:0];
132    end
133114
134    reg [2:0] state, nextstate; //FSM for write in 32bit mode to memory
135    wire we0, we1;
136    wire nreset;
137    assign nreset = ~reset;
138    parameter S0 = 3'b000;
139    parameter S1 = 3'b001;
140    parameter S2 = 3'b010;
141
142  always @ (posedge clk, posedge nreset)
143    if (nreset) state <= S0;
144    else state <= nextstate;
145    // next state logic
146    always@(*)
147      case (state)
148        S0:if (bae[3]&we) nextstate = S1;
149           else
150             if (bae[7]&we) nextstate = S2;
151             else nextstate = S0;
152        S1: nextstate = S0;
153        S2: nextstate = S0;
154        default: nextstate = S0;
155      endcase
156  // output logic
157  assign we0 = (state == S1);
158  assign we1 = (state == S2);
115 // Memories
159116
160// Read control
161    reg [7:0] MemDOA;
162    wire [63:0] DOA_Aux;
163
164    always @(posedge clk)
165      if(~reset)begin
166         rdBus = 8'h00;
167            end
168      else begin
169    if(enReg)
170      rdBus = reg_bank[address];
171      else
172                    rdBus = MemDOA[7:0];
173      end
174// memory output mux
175  always @(buffer_addr[2:0])
176    case (buffer_addr[2:0])
177      0 : MemDOA = DOA_Aux[7:0];
178      1 : MemDOA = DOA_Aux[15:8];
179      2 : MemDOA = DOA_Aux[23:16];
180      3 : MemDOA = DOA_Aux[31:24];
181      4 : MemDOA = DOA_Aux[39:32];
182      5 : MemDOA = DOA_Aux[47:40];
183      6 : MemDOA = DOA_Aux[55:48];
184      7 : MemDOA = DOA_Aux[63:56];
185    default: MemDOA = 8'h00;
186  endcase
187
188// Store Inputs
189    always @(posedge clk)
190     begin
191      if(enReg) begin
192    reg_bank[20] = error[7:0];
193    reg_bank[21] = error[15:8];
194    reg_bank[22] = status[7:0];
195    reg_bank[24] = mt_rnd[7:0];
196    reg_bank[25] = mt_rnd[15:8];
197    reg_bank[26] = mt_rnd[23:16];
198    reg_bank[27] = mt_rnd[31:24];
199      end
200     end
117RAMB16_S4_S4 ba0( .CLKA(~clk), .ENA(bae[0]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]),
118                  .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
119RAMB16_S4_S4 ba1( .CLKA(~clk), .ENA(bae[0]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]),
120                  .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
121
122RAMB16_S4_S4 ba2( .CLKA(~clk), .ENA(bae[1]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]),
123                  .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
124RAMB16_S4_S4 ba3( .CLKA(~clk), .ENA(bae[1]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]),
125                  .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
126
127RAMB16_S4_S4 ba4( .CLKA(~clk), .ENA(bae[2]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]),
128                  .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
129RAMB16_S4_S4 ba5( .CLKA(~clk), .ENA(bae[2]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]),
130                  .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
201131
202    assign address[4:0] = buffer_addr[4:0];
203    assign enReg = buffer_addr[12];
132RAMB16_S4_S4 ba6( .CLKA(~clk), .ENA(bae[3]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]),
133                  .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
134RAMB16_S4_S4 ba7( .CLKA(~clk), .ENA(bae[3]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]),
135                  .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
204136
205    assign reg0[7:0] = reg_bank[0];
206    assign reg0[15:8] = reg_bank[1];
207    assign reg0[23:16] = reg_bank[2];
208    assign reg0[31:24] = reg_bank[3];
209    assign reg1[7:0] = reg_bank[4];
210    assign reg1[15:8] = reg_bank[5];
211    assign reg1[23:16] = reg_bank[6];
212    assign reg1[31:24] = reg_bank[7];
213    assign reg2[7:0] = reg_bank[8];
214    assign reg2[15:8] = reg_bank[9];
215    assign reg2[23:16] = reg_bank[10];
216    assign reg2[31:24] = reg_bank[11];
217    assign reg3[7:0] = reg_bank[12];
218    assign reg3[15:8] = reg_bank[13];
219    assign reg3[23:16] = reg_bank[14];
220    assign reg3[31:24] = reg_bank[15];
221    assign reg4[7:0] = reg_bank[16];
222    assign reg4[15:8] = reg_bank[17];
223    assign reg4[23:16] = reg_bank[18];
224    assign reg4[31:24] = reg_bank[19];
137RAMB16_S4_S4 ba8( .CLKA(~clk), .ENA(bae[4]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]),
138                  .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
139RAMB16_S4_S4 ba9( .CLKA(~clk), .ENA(bae[4]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]),
140                  .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
225141
226    assign max_lev = reg_bank[28];
227    assign control = reg_bank[29];
228    assign max_com[7:0] = reg_bank[30];
229    assign max_com[15:8] = reg_bank[31];
230    // Write control
231    always @(negedge clk)
232    if(we & enReg) begin
233      case (address)
234         0: reg_bank[0] = wdBus;
235         1: reg_bank[1] = wdBus;
236         2: reg_bank[2] = wdBus;
237         3: reg_bank[3] = wdBus;
238         4: reg_bank[4] = wdBus;
239         5: reg_bank[5] = wdBus;
240         6: reg_bank[6] = wdBus;
241         7: reg_bank[7] = wdBus;
242         8: reg_bank[8] = wdBus;
243         9: reg_bank[9] = wdBus;
244         10: reg_bank[10] = wdBus;
245         11: reg_bank[11] = wdBus;
246         12: reg_bank[12] = wdBus;
247         13: reg_bank[13] = wdBus;
248         14: reg_bank[14] = wdBus;
249         15: reg_bank[15] = wdBus;
250         16: reg_bank[16] = wdBus;
251         17: reg_bank[17] = wdBus;
252         18: reg_bank[18] = wdBus;
253         19: reg_bank[19] = wdBus;
254         28: reg_bank[28] = wdBus;
255         29: reg_bank[29] = wdBus;
256         30: reg_bank[30] = wdBus;
257         31: reg_bank[31] = wdBus;
258      endcase
259    end
142RAMB16_S4_S4 ba10(.CLKA(~clk), .ENA(bae[5]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]),
143                  .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
144RAMB16_S4_S4 ba11(.CLKA(~clk), .ENA(bae[5]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]),
145                  .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
260146
261RAMB16_S36_S36 #(.INIT_00(256'hABCDEF00_00000000_00000000_00000000_00000000_00000000_00000000_76543210) )
262    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]),
263           .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]));
264
265RAMB16_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]),
266                  .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]));
147RAMB16_S4_S4 ba12(.CLKA(~clk), .ENA(bae[6]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]),
148                  .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
149RAMB16_S4_S4 ba13(.CLKA(~clk), .ENA(bae[6]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]),
150                  .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
267151
268// evalfit_peripheral
269evalfit_peripheral evalfit( .clk(clk), .reset(~reset), .habilita(control[0]), .maxcombs(max_com), .nivel_max(max_lev),
152RAMB16_S4_S4 ba14(.CLKA(~clk), .ENA(bae[7]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[3:0]),
153                  .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
154RAMB16_S4_S4 ba15(.CLKA(~clk), .ENA(bae[7]), .SSRA(1'b0), .ADDRA(buffer_addr[12:3]), .WEA(we), .DIA(wdBus[7:4]),
155                  .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
156
157 // evalfit_peripheral
158evalfit_peripheral evalfit( .clk(clk), .reset(reset), .habilita(control[0]), .maxcombs(max_com), .nivel_max(max_lev),
270159                            .peripheral_mem_in(ev_di), .peripheral_mem_en(en_eval), .peripheral_mem_out(ev_do), .peripheral_mem_we(we_eval),
271160                            .peripheral_mem_addr(evalfit_addr), .evalfit3_estado(status), .errores(error),
272161                            .fin_ack(irq_pin), .reg0_s(reg0), .reg1_s(reg1), .reg2_s(reg2), .reg3_s(reg3), .reg4_s(reg4));
273162
274// MersenneTwister
275 mt_mem random( .clk(clk), .ena(1'b1), .resetn(reset), .random(mt_rnd));
163reg_bank RegBank( .clk(clk), .reset(reset), .en(buffer_addr[12]), .we(we), .wdBus(wdBus), .rdBus(rdBus), .address(buffer_addr[4:0]),
164                  .reg0(reg0), .reg1(reg1), .reg2(reg2), .reg3(reg3), .reg4(reg4), .error(error), .status(status),
165                  .max_com(max_com), .max_lev(max_lev), .control(control));
166
167// mt_mem
168 mt_mem random( .clk(clk), .ena(1'b1), .resetn(~reset), .random(mt_rnd));
276169
170
277171endmodule
278172
Examples/ehw4/logic/evalfit_peripheral.vhd
2525    Port ( clk, reset, habilita: in STD_LOGIC;
2626            maxcombs : in STD_LOGIC_VECTOR (0 to 15);
2727              nivel_max : in STD_LOGIC_VECTOR (0 to 3);
28                       peripheral_mem_in : in STD_LOGIC_VECTOR (0 to 63);
28               peripheral_mem_in : in STD_LOGIC_VECTOR (0 to 63);
2929            peripheral_mem_en : out std_logic;
30                       peripheral_mem_out : out STD_LOGIC_VECTOR (0 to 63);
30               peripheral_mem_out : out STD_LOGIC_VECTOR (0 to 63);
3131            peripheral_mem_we : out STD_LOGIC;
3232            peripheral_mem_addr : out STD_LOGIC_VECTOR (0 to 8);
3333            evalfit3_estado : out std_logic_vector(0 to 7);
Examples/ehw4/src/Makefile
1CC = mipsel-openwrt-linux-gcc
1CROSS = mipsel-openwrt-linux-gcc
2CC = gcc
23
3all: jz_init_sram jz_test_gpio enable_rx enable_irq
4all: client_jz server_jz client_386 server_386
45
56DEBUG = -O3 -g0
67
7COMMON_SOURCES = jz47xx_gpio.c jz47xx_mmap.c
8COMMON_SOURCES = jz47xx_gpio.c jz47xx_mmap.c
89
910H_SOURCES = jz47xx_gpio.h jz47xx_mmap.h
1011
...... 
1617
1718LDFLAGS =
1819
19COMMON_OBJECTS = $(COMMON_SOURCES:.c=.o)
20COMMON_OBJECTS_JZ = jz47xx_gpio_jz.o jz47xx_mmap_jz.o
21COMMON_OBJECTS_386 = jz47xx_gpio_386.o jz47xx_mmap_386.o
2022
2123NANO_IP = 192.168.254.101
2224
23genetic.o: genetic.c genetic.h
24    ${CC} -lm -I. -c genetic.c -o genetic.o
25genetic_jz.o: genetic.c genetic.h $(COMMON_OBJECTS_JZ)
26    ${CROSS} -lm -I. -c genetic.c $(COMMON_OBJECTS_JZ) -o genetic_jz.o
2527
26client: sintesishw_client.c genetic.o sintesishw_client.h
27    ${CC} sintesishw_client.c genetic.o -o sintesishw_client -lm -I.
28client_jz: sintesishw_client.c genetic_jz.o sintesishw_client.h $(COMMON_OBJECTS_JZ)
29    ${CROSS} sintesishw_client.c genetic_jz.o $(COMMON_OBJECTS_JZ) -o sintesishw_client_jz -lm -I.
2830
29server: sintesishw_server.c genetic.o
30    ${CC} sintesishw_server.c genetic.o -o sintesishw_server -lm -lpthread -I.
31server_jz: sintesishw_server.c genetic_jz.o $(COMMON_OBJECTS_JZ)
32    ${CROSS} sintesishw_server.c genetic_jz.o $(COMMON_OBJECTS_JZ) -o sintesishw_server_jz -lm -lpthread -I.
33
34jz47xx_mmap_jz.o: jz47xx_mmap.c jz47xx_mmap.h
35    ${CROSS} -lm -I. -c jz47xx_mmap.c -o jz47xx_mmap_jz.o
36
37jz47xx_gpio_jz.o: jz47xx_gpio.c jz47xx_gpio.h
38    ${CROSS} -lm -I. -c jz47xx_gpio.c -o jz47xx_gpio_jz.o
39
40
41
42genetic_386.o: genetic.c genetic.h $(COMMON_OBJECTS_386)
43    ${CC} -lm -I. -c genetic.c $(COMMON_OBJECTS_386) -o genetic_386.o
44
45client_386: sintesishw_client.c genetic_386.o sintesishw_client.h $(COMMON_OBJECTS_386)
46    ${CC} sintesishw_client.c genetic_386.o $(COMMON_OBJECTS_386) -o sintesishw_client_386 -lm -I.
47
48server_386: sintesishw_server.c genetic_386.o $(COMMON_OBJECTS_386)
49    ${CC} sintesishw_server.c genetic_386.o $(COMMON_OBJECTS_386) -o sintesishw_server_386 -lm -lpthread -I.
50
51jz47xx_mmap_386.o: jz47xx_mmap.c jz47xx_mmap.h
52    ${CC} -lm -I. -c jz47xx_mmap.c -o jz47xx_mmap_386.o
53
54jz47xx_gpio_386.o: jz47xx_gpio.c jz47xx_gpio.h
55    ${CC} -lm -I. -c jz47xx_gpio.c -o jz47xx_gpio_386.o
56
57
58
59upload: sintesishw_server_jz
60    scp sintesishw_server_jz root@$(NANO_IP):ehw
3161
3262clean:
33    rm -f *.o sintesishw_client sintesishw_server ${EXEC} *~
63    rm -f *.o sintesishw_client_jz sintesishw_server_jz sintesishw_client_386 sintesishw_server_386 ${EXEC} *~
Examples/ehw4/src/genetic.c
1414#include "errno.h"
1515#include "sys/un.h"
1616#include "genetic.h"
17#include "jz47xx_gpio.h"
18#include <jz47xx_mmap.h>
19
20#define CS2_PORT JZ_GPIO_PORT_B
21#define CS2_PIN 26
1722
1823/**************************************************************************************************************************************
1924    imprime un cromosoma completo */
...... 
8792        mascara = (mascara << 1) + 1;
8893    }while(max > mascara);
8994
90    if(HW_ENABLE == 1) variable = (*(int *)(evalfit_ptr1 + EVALFIT_RDREG8)) & mascara;
95    if(HW_ENABLE == 1) variable = (*(int *)(evalfit_ptr1 + EVALFIT_RANDOM)) & mascara;
9196    else variable = random() & mascara;
9297
9398    while(variable > max)
...... 
442447
443448/** insertar cromosoma en periferico **/
444449    for(i = 0; i < ARBOLES_INDIV; i++)
445    {
446      *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i)) = *(char *)(cromo +(LONG_ARBOL*i)+7);
447// printf("\n%i: %0x",i,*(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i)));
448        *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 4) = *(int *)(cromo +(LONG_ARBOL*i));
449// printf(": %0x",*(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i)+4));
450    }
451    *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i)) = 0xF; //para terminar
452    i++;
453    *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i)) = 0xF; //para terminar
454
450    {
451        *(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0) =*(char *)(cromo +(LONG_ARBOL*i)+7);
452        //printf("\n%04hhu: Level:%X ", *(cromo+(i*LONG_ARBOL) + 6), *(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0));
453        *(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 4) = *(int *)(cromo +(LONG_ARBOL*i));
454        //printf("LUT-VARS: %08x",*(int *)(evalfit_ptr + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 4));
455    }
456    *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0) = 0xF; //para terminar
457    i++;
458    *(int *)((evalfit_ptr) + EVALFIT_MEMOFFSET + (LONG_ARBOL*i) + 0) = 0xF; //para terminar
455459/** Iniciar **/
456460    *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = CONTROL_START_MASK;
457461}
...... 
463467int errores, puertas, i;
464468
465469   // tiempo1 = get_timestamp();
466   do{ }while((*(int *)(evalfit_ptr + EVALFIT_RDREG1) & DONE_MASK) != 1);
467    // tiempo2 = get_timestamp();
468    errores = *(int *)(evalfit_ptr + EVALFIT_RDREG2) & ERRORS_MASK; //errores
469    *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0; //terminar, bajar habilita
470
471    puertas = 0;
470    do{
471        //printf("\nSTATUS: %0x",*(short *)(evalfit_ptr + EVALFIT_STATUS));
472        //usleep(100);
473    }while((*(short *)(evalfit_ptr + EVALFIT_STATUS) & DONE_MASK) != 0x8000); //wait for DONE
474    //tiempo2 = get_timestamp();
475    //printf("\n\nErrors calculated HW: %0x",(*(int *)(evalfit_ptr + EVALFIT_ERRORS) & ERRORS_MASK)); //errores
476    errores = (*(int *)(evalfit_ptr + EVALFIT_ERRORS) & ERRORS_MASK); //errores
477    *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0; //end , disable
478    puertas = 0;
472479    for(i = 0; i < ARBOLES_INDIV; i++)
473480    {
474481        puertas = puertas + *(puertas_ap + *(char *)(cromo +(LONG_ARBOL*i)+6));
475482    }
476
477return ((errores * PESO_SALIDA) + (PESO_PUERTAS * puertas) + (PESO_NIVELES * nivel_max));
483    return ((errores * PESO_SALIDA) + (PESO_PUERTAS * puertas) + (PESO_NIVELES * nivel_max));
478484}
479485
480486/**************************************************************************************************************************************
...... 
579585    }
580586}
581587
588
589/**************************************************************************************************************************************
590    map peripheral **/
591int periph_map(off_t offset)
592{
593int basemem, baseperiph;
594    basemem = open("/dev/mem", (O_RDWR | O_SYNC)); //abrir dispositivo memoria para mapear dir del periferico
595    if(basemem == -1)
596    {
597        printf("Error to open /dev/mem \n");
598        return -1;
599    }
600    baseperiph = (int )mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, basemem, offset);// & ~MAP_MASK);
601    if (baseperiph == -1)
602    {
603        printf ("Cannot mmap.\n");
604        return -1;
605    }
606return baseperiph;
607}
608
582609/**************************************************************************************************************************************
583610     inicializar periferico **/
584611int init_peripheral(int offset_int, int basemem)
585612{
586613/* Variables para periferico*/
587int *aux, base_periferico, *evalfit_ptr;
588    off_t offset = offset_int; //Direccion base del periferico
589    base_periferico = (int *)mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, basemem, offset & ~MAP_MASK);
590    evalfit_ptr = base_periferico + (offset & MAP_MASK);
591    printf("\r\nPeripheral_ptr: %0x", evalfit_ptr);
592    if( (int *)evalfit_ptr == (int *)MAP_FAILED)
593    { printf("error mmap!\n");
594    fflush(stdout);
595    return -1;
596    }
597    *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = CONTROL_RESET_MASK; //reset
598    *(int *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0;
599    printf("\nDate + Status: %0x\n", *(int *)(evalfit_ptr + EVALFIT_STATUS_OFFSET));
600    return evalfit_ptr;
614    int *aux, base_periferico, *ConfigRegsBase_ptr, maxcombs, nivel_max;
615    void *pio, *evalfit_ptr;
616    basemem = open("/dev/mem", (O_RDWR | O_SYNC)); //abrir dispositivo memoria para mapear dir del periferico
617    if(basemem == -1)
618    {
619            printf("Error al abrir /dev/mem \n");
620            return -1;
621    }
622    pio = jz_gpio_map(CS2_PORT);
623    jz_gpio_as_func (pio, CS2_PIN, 0);
624    ConfigRegsBase_ptr = (int *)periph_map(CONFIG_REGS_BASE);// + SACR2_OFFSET/sizeof(int);//SMCR2_OFFSET/sizeof(int);
625    printf("\n%0x ", *(ConfigRegsBase_ptr + SMCR2_OFFSET/sizeof(int)));
626    munmap(ConfigRegsBase_ptr, MAP_SIZE);
627    evalfit_ptr = (int *)periph_map(EVALFIT_PHYSBASE1);
628
629    printf("\r\nevalfit_ptr: %0x", evalfit_ptr);
630    if( evalfit_ptr == (int *)MAP_FAILED)
631    { printf("error mmap!\n");
632            fflush(stdout);
633            return -1;
634    }
635    *(char *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = CONTROL_RESET_MASK; //reset
636    //sleep(1);
637    *(char *)(evalfit_ptr + EVALFIT_CONTROL_OFFSET) = 0;
638    printf("\nDate + Status: %0x\n", *(char *)(evalfit_ptr + EVALFIT_STATUS));
639return evalfit_ptr;
601640}
602641
603642/**************************************************************************************************************************************
...... 
643682/* datos = malloc(sizeof(datos)*maxgeneraciones*2); if(datos==0) printf("Error en malloc");*/
644683/* x=0;*/
645684
646    objetivo = gen_datos -> objetivo;
647    tamaobj = gen_datos -> tamaobj;
648    pentarboles = gen_datos -> pentarboles;
649    tamacromo = gen_datos -> tamacrom;
650    cromo_sal = gen_datos -> cromo_sal;
651    fitness_sal = gen_datos -> fitness;
652    cromo_entrada = gen_datos -> cromo_entrada;
653    fitness_entrada = gen_datos -> fitness_entrada;
654    nivel_max = gen_datos -> nivel_max;
655    vars = gen_datos -> vars;
656    generacion = gen_datos->generacion;
657    aux = gen_datos->aux;
658    aux_sal = gen_datos->aux_sal;
659
660    cromo = malloc(sizeof(cromo) * (poblacion + 2) * LONG_INDIV); if(cromo==0) printf("Error en malloc");
661    fitness = malloc(sizeof(fitness) * (poblacion+1)); if(fitness==0) printf("Error en malloc");
662    fitness2 = malloc(sizeof(fitness2) * (poblacion+1)); if(fitness2==0) printf("Error en malloc");
663    ordenpoblacion = malloc(sizeof(ordenpoblacion) * (poblacion+1)); if(ordenpoblacion==0) printf("Error en malloc");
685    objetivo = gen_datos -> objetivo;
686    tamaobj = gen_datos -> tamaobj;
687    pentarboles = gen_datos -> pentarboles;
688    tamacromo = gen_datos -> tamacrom;
689    cromo_sal = gen_datos -> cromo_sal;
690    fitness_sal = gen_datos -> fitness;
691    cromo_entrada = gen_datos -> cromo_entrada;
692    fitness_entrada = gen_datos -> fitness_entrada;
693    nivel_max = gen_datos -> nivel_max;
694    vars = gen_datos -> vars;
695    generacion = gen_datos->generacion;
696    aux = gen_datos->aux;
697    aux_sal = gen_datos->aux_sal;
698
699    cromo = malloc(sizeof(cromo) * (poblacion + 2) * LONG_INDIV); if(cromo==0) printf("Error en malloc");
700    fitness = malloc(sizeof(fitness) * (poblacion+1)); if(fitness==0) printf("Error en malloc");
701    fitness2 = malloc(sizeof(fitness2) * (poblacion+1)); if(fitness2==0) printf("Error en malloc");
702    ordenpoblacion = malloc(sizeof(ordenpoblacion) * (poblacion+1)); if(ordenpoblacion==0) printf("Error en malloc");
664703
665704    if(gen_datos->en_cromo_entrada == 1)
666    {
705    {
667706        for(i = 0; i < LONG_INDIV; i++ )
668707        {
669708            *(char *)(cromo + i) = *(char *)(cromo_entrada + i);
670709        }
671    }
710    }
672711
673    if(gen_datos->en_cromo_entrada==0) gen_poblacion(cromo, pentarboles, vars, poblacion);
674    else gen_poblacion(cromo + LONG_INDIV, pentarboles, vars, poblacion-1);
712    if(gen_datos->en_cromo_entrada == 0)
713        gen_poblacion(cromo, pentarboles, vars, poblacion); //generate chromosome
714    else
715        gen_poblacion(cromo + LONG_INDIV, pentarboles, vars, poblacion-1);
675716
676    for(i = 0; i < poblacion; i++)
677    {
678        *(ordenpoblacion + i) = i; //se inicializa el stream para el orden poblacional
679        *(fitness + i) = 100000;
680        *(fitness2 + i) = 100000;
681    }
717    for(i = 0; i < poblacion; i++)
718    {
719        *(ordenpoblacion + i) = i; //se inicializa el stream para el orden poblacional
720        *(fitness + i) = 100000;
721        *(fitness2 + i) = 100000;
722    }
682723
683724/** copiar miniterminos a periferico **/
684725    if(HW_ENABLE == 1) {
685726        minterm2peripheral(objetivo, tamaobj, (int *)(evalfit_ptr1));
727/*
686728         minterm2peripheral(objetivo, tamaobj, (int *)(evalfit_ptr2));
687729        minterm2peripheral(objetivo, tamaobj, (int *)(evalfit_ptr3));
688730        minterm2peripheral(objetivo, tamaobj, (int *)(evalfit_ptr4));
731*/
689732    }
690733
691734/** Insertar maxcombs y nivel_max **/
692735    if(HW_ENABLE == 1) {
693736        *(int *)(evalfit_ptr1 + EVALFIT_WREG4) = (COMBS-1) + (nivel_max << 16);
737/*
694738        *(int *)(evalfit_ptr2 + EVALFIT_WREG4) = (COMBS-1) + (nivel_max << 16);
695739        *(int *)(evalfit_ptr3 + EVALFIT_WREG4) = (COMBS-1) + (nivel_max << 16);
696740        *(int *)(evalfit_ptr4 + EVALFIT_WREG4) = (COMBS-1) + (nivel_max << 16);
741*/
697742    }
698743    o=0;
699744    *generacion = 0;
...... 
726771// evaluar cromosomas de poblacion
727772        o=0;
728773// tiempo1 = get_timestamp();
729        for(i = 0; i < poblacion; i=i+4)
774        for(i = 0; i < poblacion; i=i+1)
730775        {
731776            if(HW_ENABLE == 1)
732777            {
733778                evalfit_hw_init((cromo + (i * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr1);
779/*
734780                evalfit_hw_init((cromo + ((i+1) * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr2);
735781                evalfit_hw_init((cromo + ((i+2) * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr3);
736782                evalfit_hw_init((cromo + ((i+3) * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr4);
783*/
737784                *(fitness + i) = evalfit_hw_wait((cromo + (i * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr1);
785/*
738786                *(fitness + i+1) = evalfit_hw_wait((cromo + ((i+1) * LONG_INDIV)), pentarboles,nivel_max, (int *)evalfit_ptr2);
739787                *(fitness + i+2) = evalfit_hw_wait((cromo + ((i+2) * LONG_INDIV)), pentarboles,nivel_max, (int *)evalfit_ptr3);
740788                *(fitness + i+3) = evalfit_hw_wait((cromo + ((i+3) * LONG_INDIV)), pentarboles, nivel_max, (int *)evalfit_ptr4);
789*/
741790            }else{
742791                *(fitness + i) = eval_fit_sw((cromo + (i * LONG_INDIV)), objetivo, tamaobj, pentarboles, vars);
792/*
743793                *(fitness + i+1) = eval_fit_sw((cromo + ((i+1) * LONG_INDIV)), objetivo, tamaobj, pentarboles, vars);
744794                *(fitness + i+2) = eval_fit_sw((cromo + ((i+2) * LONG_INDIV)), objetivo, tamaobj, pentarboles, vars);
745795                *(fitness + i+3) = eval_fit_sw((cromo + ((i+3) * LONG_INDIV)), objetivo, tamaobj, pentarboles, vars);
796*/
746797            }
747798            *(fitness2 + i) = *(fitness + i);
799/*
748800            *(fitness2 + i+1) = *(fitness + i+1);
749801            *(fitness2 + i+2) = *(fitness + i+2);
750802            *(fitness2 + i+3) = *(fitness + i+3);
803*/
751804
752805            if(*(fitness + i) < PESO_SALIDA)
753806            {
Examples/ehw4/src/genetic.h
4343#define INDICE_TARA4 ((*(ordenpoblacion+poblacion-4)) * LONG_INDIV)
4444
4545
46/************************** peripheral Definitions ***************************/
46/************************** peripheral Definitions ***************************/
4747
48#define USR_REGS 0x10
49#define MAP_SIZE 0x4000Ul
50#define MAP_MASK (MAP_SIZE - 1)
48#define CONFIG_REGS_BASE 0x13010000
49#define SMCR2_OFFSET 0x18
50#define SACR2_OFFSET 0x38
5151
52#define DONE_MASK 0x1
53#define ERRORS_MASK 0xFFFF
52#define EVALFIT_PHYSBASE1 0x14000000
53#define MAP_SIZE 0x4000Ul
54#define MAP_MASK (MAP_SIZE - 1)
55
56#define DONE_MASK 0x8000
57#define ERRORS_MASK 0xFFFF
5458
5559/** CONTROL REGISTERS **/
56#define EVALFIT_REGBASE_OFFSET 0
60#define EVALFIT_REGBASE_OFFSET 0x1000
5761
58#define EVALFIT_CONTROL_OFFSET EVALFIT_REGBASE_OFFSET + 0
59#define CONTROL_RESET_MASK 0x80000000
60#define CONTROL_START_MASK 0x40000000
62#define EVALFIT_CONTROL_OFFSET EVALFIT_REGBASE_OFFSET + 0x1D
63#define CONTROL_RESET_MASK 0x80
64#define CONTROL_START_MASK 0x40
6165
62#define EVALFIT_STATUS_OFFSET EVALFIT_REGBASE_OFFSET + 0
63#define EVALFIT_SRCADDR_OFFSET EVALFIT_REGBASE_OFFSET + 4
64#define EVALFIT_DSTADDR_OFFSET EVALFIT_REGBASE_OFFSET + 8
65#define EVALFIT_TRANSFERSIZE_OFFSET EVALFIT_REGBASE_OFFSET + 12
66#define EVALFIT_REG_OFFSET EVALFIT_REGBASE_OFFSET + 16
66#define EVALFIT_REG_OFFSET EVALFIT_REGBASE_OFFSET + 0
6767
6868/** WRITE REGISTERS **/
69
70#define EVALFIT_MAX_COMBS EVALFIT_REGBASE_OFFSET + 0x1E
71#define EVALFIT_MAX_LEVEL EVALFIT_REGBASE_OFFSET + 0x1C
72
73
6974#define EVALFIT_WREG4 (EVALFIT_REGBASE_OFFSET + (4*4))
7075#define EVALFIT_WREG5 (EVALFIT_REGBASE_OFFSET + (4*5))
7176#define EVALFIT_wREG6 (EVALFIT_REGBASE_OFFSET + (4*6))
7277#define EVALFIT_wREG7 (EVALFIT_REGBASE_OFFSET + (4*7))
73#define EVALFIT_wREG8 (EVALFIT_REGBASE_OFFSET + (4*8))
78#define EVALFIT_wREG8 (EVALFIT_REGBASE_OFFSET + (4*8))
7479
7580/** READ REGISTERS **/
81#define EVALFIT_ERRORS EVALFIT_REGBASE_OFFSET + 0x14
82#define EVALFIT_RANDOM EVALFIT_REGBASE_OFFSET + 0x18
83#define EVALFIT_STATUS EVALFIT_REGBASE_OFFSET + 0x16
84
85
7686#define EVALFIT_RDREG0 (EVALFIT_REGBASE_OFFSET)
7787#define EVALFIT_RDREG1 (EVALFIT_REGBASE_OFFSET + (4*1))
7888#define EVALFIT_RDREG2 (EVALFIT_REGBASE_OFFSET + (4*2))
...... 
8393#define EVALFIT_RDREG7 (EVALFIT_REGBASE_OFFSET + (4*7))
8494#define EVALFIT_RDREG8 (EVALFIT_REGBASE_OFFSET + (4*8))
8595/** MEMORY **/
86#define EVALFIT_MEMOFFSET 0x1000
96#define EVALFIT_MEMOFFSET 0x0
8797#define EVALFIT_OBJOFFSET EVALFIT_MEMOFFSET + (0x40 * 8)
8898
89/** CROMOSOMA **/
90#define CROMO_NIVEL_OFFSET
91
9299/* Variables globales */
93100char *funlut_ap, *puertas_ap;
94101void *evalfit_ptr1, *evalfit_ptr4, *evalfit_ptr3, *evalfit_ptr2;
Examples/ehw4/src/jz47xx_gpio.c
1/*
2  JZ47xx GPIO at userspace
3
4  Copyright (C) 2010 Andres Calderon andres.calderon@emqbit.com
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23
24#include <jz47xx_gpio.h>
25#include <jz47xx_mmap.h>
26
27
28#define JZ_GPIO_BASE 0x10010000
29
30void
31jz_gpio_as_output (JZ_PIO * pio, unsigned int o)
32{
33  pio->PXFUNC = (1 << (o));
34  pio->PXSELC = (1 << (o));
35  pio->PXDIRS = (1 << (o));
36}
37
38void
39jz_gpio_as_input (JZ_PIO * pio, unsigned int o)
40{
41  pio->PXFUNC = (1 << (o));
42  pio->PXSELC = (1 << (o));
43  pio->PXDIRC = (1 << (o));
44}
45
46void
47jz_gpio_as_irq (JZ_PIO * pio, unsigned int o)
48{
49  pio->PXFUNC = (1 << (o));
50  pio->PXSELS = (1 << (o));
51  pio->PXDIRC = (1 << (o));
52}
53
54void
55jz_gpio_set_pin (JZ_PIO * pio, unsigned int o)
56{
57  pio->PXDATS = (1 << (o));
58}
59
60void
61jz_gpio_clear_pin (JZ_PIO * pio, unsigned int o)
62{
63  pio->PXDATC = (1 << (o));
64}
65
66void
67jz_gpio_out (JZ_PIO * pio, unsigned int o, unsigned int val)
68{
69  if (val == 0)
70    pio->PXDATC = (1 << (o));
71  else
72    pio->PXDATS = (1 << (o));
73}
74
75unsigned int
76jz_gpio_get_pin (JZ_PIO * pio, unsigned int o)
77{
78  return (pio->PXPIN & (1 << o)) ? 1 : 0;
79}
80
81int
82jz_gpio_as_func (JZ_PIO * pio, unsigned int o, int func)
83{
84  switch (func)
85    {
86    case 0:
87      pio->PXFUNS = (1 << o);
88      pio->PXTRGC = (1 << o);
89      pio->PXSELC = (1 << o);
90      pio->PXPES = (1 << o);
91      return 1;
92
93    case 1:
94      pio->PXFUNS = (1 << o);
95      pio->PXTRGC = (1 << o);
96      pio->PXSELS = (1 << o);
97      pio->PXPES = (1 << o);
98      return 1;
99
100    case 2:
101      pio->PXFUNS = (1 << o);
102      pio->PXTRGS = (1 << o);
103      pio->PXSELC = (1 << o);
104      pio->PXPES = (1 << o);
105      return 1;
106    }
107  return 0;
108}
109
110JZ_PIO *
111jz_gpio_map (int port)
112{
113  JZ_PIO *pio;
114
115  pio = (JZ_PIO *) jz_mmap (JZ_GPIO_BASE);
116  pio = (JZ_PIO *) ((unsigned int) pio + port * 0x100);
117
118  return pio;
119}
Examples/ehw4/src/jz47xx_gpio.h
1/*
2  JZ47xx GPIO at userspace
3
4  Copyright (C) 2010 Andres Calderon andres.calderon@emqbit.com
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19
20#ifndef __jz47xx_gpio_h__
21#define __jz47xx_gpio_h__
22
23#define JZ_GPIO_PORT_A 0
24#define JZ_GPIO_PORT_B 1
25#define JZ_GPIO_PORT_C 2
26#define JZ_GPIO_PORT_D 3
27
28typedef volatile unsigned int JZ_REG; /* Hardware register definition */
29
30typedef struct _JZ_PIO
31{
32  JZ_REG PXPIN; /* PIN Level Register */
33  JZ_REG Reserved0;
34  JZ_REG Reserved1;
35  JZ_REG Reserved2;
36  JZ_REG PXDAT; /* Port Data Register */
37  JZ_REG PXDATS; /* Port Data Set Register */
38  JZ_REG PXDATC; /* Port Data Clear Register */
39  JZ_REG Reserved3;
40  JZ_REG PXIM; /* Interrupt Mask Register */
41  JZ_REG PXIMS; /* Interrupt Mask Set Reg */
42  JZ_REG PXIMC; /* Interrupt Mask Clear Reg */
43  JZ_REG Reserved4;
44  JZ_REG PXPE; /* Pull Enable Register */
45  JZ_REG PXPES; /* Pull Enable Set Reg. */
46  JZ_REG PXPEC; /* Pull Enable Clear Reg. */
47  JZ_REG Reserved5;
48  JZ_REG PXFUN; /* Function Register */
49  JZ_REG PXFUNS; /* Function Set Register */
50  JZ_REG PXFUNC; /* Function Clear Register */
51  JZ_REG Reserved6;
52  JZ_REG PXSEL; /* Select Register */
53  JZ_REG PXSELS; /* Select Set Register */
54  JZ_REG PXSELC; /* Select Clear Register */
55  JZ_REG Reserved7;
56  JZ_REG PXDIR; /* Direction Register */
57  JZ_REG PXDIRS; /* Direction Set Register */
58  JZ_REG PXDIRC; /* Direction Clear Register */
59  JZ_REG Reserved8;
60  JZ_REG PXTRG; /* Trigger Register */
61  JZ_REG PXTRGS; /* Trigger Set Register */
62  JZ_REG PXTRGC; /* Trigger Set Register */
63  JZ_REG Reserved9;
64  JZ_REG PXFLG; /* Port Flag Register */
65  JZ_REG PXFLGC; /* Port Flag clear Register */
66} JZ_PIO, *PJZ_PIO;
67
68void jz_gpio_as_output (JZ_PIO * pio, unsigned int o);
69
70void jz_gpio_as_input (JZ_PIO * pio, unsigned int o);
71
72void jz_gpio_set_pin (JZ_PIO * pio, unsigned int o);
73
74void jz_gpio_clear_pin (JZ_PIO * pio, unsigned int o);
75
76void jz_gpio_out (JZ_PIO * pio, unsigned int o, unsigned int val);
77
78unsigned int jz_gpio_get_pin (JZ_PIO * pio, unsigned int o);
79
80int jz_gpio_as_func (JZ_PIO * pio, unsigned int o, int func);
81
82JZ_PIO *jz_gpio_map (int port);
83
84#endif
Examples/ehw4/src/jz47xx_mmap.c
1/*
2 * JZ47xx GPIO lines
3 *
4 * Written 2010 by Andres Calderon andres.calderon@emqbit.com
5 */
6
7#include <stdio.h>
8#include <sys/mman.h>
9#include <fcntl.h>
10#include <stdlib.h>
11#include <termios.h>
12#include <unistd.h>
13
14#include <jz47xx_mmap.h>
15
16
17void *
18jz_mmap (off_t address)
19{
20  int fd;
21
22  void *pio;
23
24  if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1)
25    {
26      fprintf (stderr, "Cannot open /dev/mem.\n");
27      return 0;
28    }
29
30  pio = (void *) mmap (0, getpagesize (), PROT_READ | PROT_WRITE, MAP_SHARED, fd, address);
31
32  if (pio == (void *) -1)
33    {
34      fprintf (stderr, "Cannot mmap.\n");
35      return 0;
36    }
37
38  return pio;
39}
40
41void *
42jz_fpga_map (off_t address)
43{
44  int fd;
45
46  void *fpga;
47
48  if ((fd = open ("/dev/mem", O_RDWR | O_SYNC)) == -1)
49    {
50      fprintf (stderr, "Cannot open /dev/mem.\n");
51      return 0;
52    }
53
54  fpga = (void *) mmap (0, FPGA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, address);
55
56  if (fpga == (void *) -1)
57    {
58      fprintf (stderr, "Cannot mmap.\n");
59      return 0;
60    }
61
62  return fpga;
63}
64
Examples/ehw4/src/jz47xx_mmap.h
1/*
2 * JZ47xx GPIO lines
3 *
4 * Written 2010 by Andres Calderon andres.calderon@emqbit.com
5 */
6
7#ifndef __jz47xx_mmap_h__
8#define __jz47xx_mmap_h__
9
10#include <sys/mman.h>
11
12#define FPGA_SIZE (1 << 15)
13
14void *jz_mmap (off_t address);
15void *jz_fpga_map (off_t address);
16
17#endif
Examples/ehw4/src/sintesishw_client.c
244244
245245int main( int argc, char** argv )
246246{
247    int *generacion, k, a, b, z, i, j= 0, error, nivel_max, vars, *tiempo, poblacion_total, m, p, iteraciones, T;
248    int conta=0, aux1, aux2, pentarboles, *fitness1, *fitness2, *entrada, *orderesult, aux, *aux_sal;
249    char o, *ap, *valor_devuelto;;
250    char *cromo_sal1, *cromo_sal2, *cromo_entrada;
251    int tamaobj, objetivo[8192], obj_combs;//= {0,254,123,16,87,56,34,76,89,155,199};
252    long int tiempo1, tiempo2;
253    float tiempof, tiempof2, Tfloat, float1, float2;
247    int *generacion, k, a, b, z, i, j= 0, error, nivel_max, vars, *tiempo, poblacion_total, m, p, iteraciones, T;
248    int conta=0, aux1, aux2, pentarboles, *fitness1, *fitness2, *entrada, *orderesult, aux, *aux_sal;
249    char o, *ap, *valor_devuelto;;
250    char *cromo_sal1, *cromo_sal2, *cromo_entrada;
251    int tamaobj, objetivo[8192], obj_combs;//= {0,254,123,16,87,56,34,76,89,155,199};
252    long int tiempo1, tiempo2;
253    float tiempof, tiempof2, Tfloat, float1, float2;
254254
255255    int nivel2[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
256256    int nivel3[]={0,0,1,1,1,2,2,2,2,3,3, 3, 3, 4, 4, 4};
...... 
258258    int nivel5[]={0,0,0,0,0,0,0,0,0,0,0, 0, 0, 0, 0, 0};
259259
260260/* Estructuras para datos de cromosomas*/
261    struct gen_datos_tipo data_struct1, data_struct2;
262    struct gen_datos_tipo *data_struct_ap1, *data_struct_ap2;
263    data_struct_ap1 = &data_struct1;
264    data_struct_ap2 = &data_struct2;
261    struct gen_datos_tipo data_struct1, data_struct2;
262    struct gen_datos_tipo *data_struct_ap1, *data_struct_ap2;
263    data_struct_ap1 = &data_struct1;
264    data_struct_ap2 = &data_struct2;
265265
266266/* Variables para tablas de lut*/
267    FILE *f1;
268    int size1;
269    srand ( (time(NULL)) );
267    FILE *f1;
268    int size1;
269    srand ( (time(NULL)) );
270270
271271/* Variables para almacenar datos para graficar*/
272    int *datos, *datos2, x, media=4, puntos;
273    FILE *fich, *fich2;
272    int *datos, *datos2, x, media=4, puntos;
273    FILE *fich, *fich2;
274274
275275    char output_file_name[128], output_file_fitness_name[128];
276276    sscanf(argv[1], "%i",&vars);
...... 
279279    sscanf(argv[4], "%i", &nodos);
280280    sscanf(argv[5], "%s", output_file_name);
281281    sscanf(argv[6], "%s", output_file_fitness_name);
282// printf("\nvars: %i indivs:%i generations:%i nodos:%i ", vars, poblacion_total, maxgeneraciones, nodos);
282    printf("\nvars: %i indivs:%i generations:%i nodos:%i ", vars, poblacion_total, maxgeneraciones, nodos);
283283    fflush(stdout);
284284
285    pentarboles = 1; //MODIFIQUE NIVEL_MAX
286    nivel_max = 2;
287    poblacion = poblacion_total/nodos;
285    pentarboles = 1; //MODIFIQUE NIVEL_MAX
286    nivel_max = 2;
287    poblacion = poblacion_total/nodos;
288288    m = 1; //datos a migrar
289    p = 8; //frecuencia de migracion
289    p = maxgeneraciones;//8; //frecuencia de migracion
290290    T = 4; //temperatura para crear nuevos indiv. A mayor T menor temperatura
291291    i=0;
292292    tamaobj=0;
...... 
297297    obj_combs = pow(2, (vars/2));
298298
299299/*Armar funcion objetivo comparador*/ /* OJO, SE ESTÁN METIENDO VALORES DE 1EXX EN LAS LUT Y ESOS INDIVIDUOS AL PARECER QUEDAN MAL */
300    for(a=0; a < obj_combs ;a++)
301    {
302        for(b=0; b < obj_combs ;b++)
303        {
304            if(a > b)z=1; if(a < b)z=2; if(a == b)z=4;
305            if((z & 0x4) != 0 )
306            {
307                objetivo[tamaobj] = i;
308                printf("%i ",objetivo[tamaobj]);
309                tamaobj++;
310            }
311            i++;
312        }
313    }
300    for(a=0; a < obj_combs ;a++)
301    {
302        for(b=0; b < obj_combs ;b++)
303        {
304            if(a > b)z=1; if(a < b)z=2; if(a == b)z=4;
305            if((z & 0x4) != 0 )
306            {
307                objetivo[tamaobj] = i;
308                printf("%i ",objetivo[tamaobj]);
309                tamaobj++;
310            }
311            i++;
312        }
313    }
314314// printf("Tama:%i ",tamaobj);
315315
316316/* Tabla para las LUT*/
317    f1 = fopen("funlut.dat","r");
318    if(f1 == NULL){
319        printf("\nError de lectura de archivo!");
320        return 0;}
321
322    fseek (f1, 0, SEEK_END);
323    size1 = ftell(f1);
324    funlut_ap = malloc(size1); if(funlut_ap==0) printf("Error en malloc");
325    rewind (f1);
326    fread(funlut_ap,1,size1,f1);
327    fclose(f1);
328
329    puntos = 16; /*numero de puntos para la grafica*/
330    datos = malloc(sizeof(datos)*puntos*3); if(datos==0) printf("Error en malloc");
331    fich=fopen(output_file_name,"wb");
332    datos2 = malloc(sizeof(datos2) * maxgeneraciones * p * nodos); if(datos2==0) printf("Error en malloc");
333    fich2=fopen(output_file_fitness_name,"wb");
317    f1 = fopen("funlut.dat","r");
318    if(f1 == NULL){
319            printf("\nError de lectura de archivo!");
320            return 0;}
321
322    fseek (f1, 0, SEEK_END);
323    size1 = ftell(f1);
324    funlut_ap = malloc(size1); if(funlut_ap==0) printf("Error en malloc");
325    rewind (f1);
326    fread(funlut_ap,1,size1,f1);
327    fclose(f1);
328
329    puntos = 16; /*numero de puntos para la grafica*/
330    datos = malloc(sizeof(datos)*puntos*3); if(datos==0) printf("Error en malloc");
331    fich=fopen(output_file_name,"wb");
332    datos2 = malloc(sizeof(datos2) * maxgeneraciones * p * nodos); if(datos2==0) printf("Error en malloc");
333    fich2=fopen(output_file_fitness_name,"wb");
334334
335335    cromo_sal1 = malloc(sizeof(cromo_sal1) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal1==0) printf("Error en malloc");
336    fitness1 = malloc(sizeof(fitness1) * RESULTADOS * nodos); if(fitness1==0) printf("Error en malloc");
337    cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc");
338    fitness2 = malloc(sizeof(fitness2) * RESULTADOS * nodos); if(fitness2==0) printf("Error en malloc");
339    cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc");
340    generacion = malloc(sizeof(generacion)* RESULTADOS * nodos); if(generacion==0) printf("Error en malloc");
341    tiempo = malloc(sizeof(tiempo)* RESULTADOS * nodos); if(tiempo==0) printf("Error en malloc");
342    cromo_entrada = malloc(sizeof(cromo_entrada)* LONG_INDIV * m); if(cromo_entrada==0) printf("Error en malloc");
336    fitness1 = malloc(sizeof(fitness1) * RESULTADOS * nodos); if(fitness1==0) printf("Error en malloc");
337    cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc");
338    fitness2 = malloc(sizeof(fitness2) * RESULTADOS * nodos); if(fitness2==0) printf("Error en malloc");
339    cromo_sal2 = malloc(sizeof(cromo_sal2) * RESULTADOS * LONG_INDIV * nodos); if(cromo_sal2==0) printf("Error en malloc");
340    generacion = malloc(sizeof(generacion)* RESULTADOS * nodos); if(generacion==0) printf("Error en malloc");
341    tiempo = malloc(sizeof(tiempo)* RESULTADOS * nodos); if(tiempo==0) printf("Error en malloc");
342    cromo_entrada = malloc(sizeof(cromo_entrada)* LONG_INDIV * m); if(cromo_entrada==0) printf("Error en malloc");
343343    orderesult = malloc(sizeof(orderesult) * nodos*RESULTADOS); if(orderesult==0) printf("Error en malloc");
344344    aux_sal = malloc(sizeof(aux_sal) * nodos * maxgeneraciones * 2); if(aux_sal==0) printf("Error en malloc");
345345
346    data_struct_ap1->objetivo = objetivo;
347    data_struct_ap1->tamaobj = tamaobj;
348    data_struct_ap1->pentarboles = pentarboles;
349    data_struct_ap1->maxgen = maxgeneraciones;
350    data_struct_ap1->cromo_sal = cromo_sal1;
351    data_struct_ap1->fitness = fitness1;
352    data_struct_ap1->cromo_entrada = cromo_entrada;
353    data_struct_ap1->fitness_entrada = 0;
354    data_struct_ap1->nivel_max = nivel_max;
355    data_struct_ap1->vars= vars;
356    data_struct_ap1->en_cromo_entrada = 0;
357    data_struct_ap1->generacion = generacion;
358    data_struct_ap1->tiempo = tiempo;
359    data_struct_ap1->aux = aux;
360    data_struct_ap1->aux_sal = aux_sal;
346    data_struct_ap1->objetivo = objetivo;
347    data_struct_ap1->tamaobj = tamaobj;
348    data_struct_ap1->pentarboles = pentarboles;
349    data_struct_ap1->maxgen = maxgeneraciones;
350    data_struct_ap1->cromo_sal = cromo_sal1;
351    data_struct_ap1->fitness = fitness1;
352    data_struct_ap1->cromo_entrada = cromo_entrada;
353    data_struct_ap1->fitness_entrada = 0;
354    data_struct_ap1->nivel_max = nivel_max;
355    data_struct_ap1->vars= vars;
356    data_struct_ap1->en_cromo_entrada = 0;
357    data_struct_ap1->generacion = generacion;
358    data_struct_ap1->tiempo = tiempo;
359    data_struct_ap1->aux = aux;
360    data_struct_ap1->aux_sal = aux_sal;
361361
362362/* printf("\npentarboles:%i nivel_max%i ", pentarboles ,nivel_max);*/
363363/* fflush(stdout); */
364364
365/* Iniciar evolucion */
366
367    x = 0;
368    fflush(stdout);
365        /* Iniciar evolucion */
366        x = 0;
369367    for(k = 0; k < iteraciones ; k++)
370368    {
371369        tiempo1 = get_timestamp();
372        iniciar_evol(data_struct_ap1);
370        iniciar_evol(data_struct_ap1); //evolution
373371        tiempo2 = get_timestamp();
374372        *tiempo = tiempo2 - tiempo1;
375373        tiempof2 = *tiempo;
376374        tiempof = tiempof2/(1000000);
377// printf("\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof);
378        fprintf(fich, "\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof);
375        printf("\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof);
376        //fprintf(fich, "\n%i %i %i %5f",nodos, vars, poblacion_total, tiempof);
379377
380378        for(i = 0; i < nodos*RESULTADOS; i++) //Organizar lo q llego, ¡solo se indexa orderesult, que dice en q orden estan los cromosomas!
381379        {
Examples/ehw4/src/sintesishw_client.h
1717#define PORT 3550 /* El puerto que sera abierto */
1818#define BACKLOG 2 /* El numero de conexiones permitidas */
1919#define MAXDATASIZE 10000 /* El numero maximo de datos en bytes */
20#define IP0 "192.168.0.4"
20#define IP0 "192.168.254.101"
2121#define IP1 "192.168.0.6"
2222#define IP2 "192.168.0.7"
2323#define IP3 "192.168.0.8"
Examples/ehw4/src/sintesishw_server.c
121121/** iniciar periferico **/
122122    if(HW_ENABLE == 1){
123123        evalfit_ptr1 = (int *)init_peripheral(EVALFIT_PHYSBASE1, basemem);
124/*
124125        evalfit_ptr2 = (int *)init_peripheral(EVALFIT_PHYSBASE2, basemem);
125126        evalfit_ptr3 = (int *)init_peripheral(EVALFIT_PHYSBASE3, basemem);
126127        evalfit_ptr4 = (int *)init_peripheral(EVALFIT_PHYSBASE4, basemem);
128*/
127129    }
128130    data_struct_ap1 = &data_struct1;
129131    data_struct_ap2 = &data_struct2;
...... 
164166        ap1 = data_socket_rx_ap; /* Cargar datos en la estructura para la evolucion */
165167        data_struct_ap1->tamaobj = htonl(*(int *)ap1) & 0xFFFF;
166168        maxgeneraciones = htonl(*(int *)ap1) >> 16;
167// printf("\ntama obj:%0x maxgens:%i numbytes:%i-- ", data_struct_ap1->tamaobj, maxgeneraciones,numbytes);
169        //printf("\ntama obj:%0x maxgens:%i numbytes:%i-- ", data_struct_ap1->tamaobj, maxgeneraciones,numbytes);
168170        fflush(stdout);
169171        ap1 = ap1 + 4;
170172        for(i=0;i < (data_struct_ap1->tamaobj); i++)
171173        {
172174            objetivo[i] =htonl(*(int *)ap1); //XX
173// printf("obj:%i %i ",i,objetivo[i]);
175            //printf("obj:%i %i ",i,objetivo[i]);
174176            ap1 = ap1 + 4;
175177        }
176178        data_struct_ap1->objetivo = objetivo;
...... 
181183        ap1 = ap1 + 4;
182184        data_struct_ap1->tamacrom = htonl(*(int *)ap1) & 0xFFFF;
183185        poblacion = htonl(*(int *)ap1) >> 16;
184// printf("\ntamacrom:%0x poblacion:%i vars:%i pentarboles:%i maxgeneraciones:%i ", data_struct_ap1->tamacrom, poblacion,vars,pentarboles,maxgeneraciones);
186        //printf("\ntamacrom:%0x poblacion:%i vars:%i pentarboles:%i maxgeneraciones:%i ", data_struct_ap1->tamacrom, poblacion,vars,pentarboles,maxgeneraciones);
185187        ap1 = ap1 + 4;
186188        cromo_entrada = malloc(sizeof(cromo_entrada) * RESULTADOS * LONG_INDIV); if(cromo_entrada==0) printf("Error en malloc");
187189        data_struct_ap1->cromo_entrada = cromo_entrada;
...... 
196198        ap1 = ap1 + 4;
197199        data_struct_ap1->nivel_max = htonl(*(int *)ap1) & 0xFFFF;
198200        data_struct_ap1->en_cromo_entrada = htonl(*(int *)ap1) >> 16; //han enviado un cromosoma?
199        ap1 = ap1 + 4;
200        data_struct_ap1->aux = htonl(*(int *)ap1);
201/* printf("\nnivel_max:%0x-- ", data_struct_ap1->nivel_max);*/
201                ap1 = ap1 + 4;
202                data_struct_ap1->aux = htonl(*(int *)ap1);
203        //printf("\nnivel_max:%0x-- ", data_struct_ap1->nivel_max);
202204        data_struct_ap1->maxgen = maxgeneraciones;
203205
204206        cromo_sal1 = malloc(sizeof(cromo_sal1) * RESULTADOS * LONG_INDIV); if(cromo_sal1==0) printf("Error en malloc"); /* reservar para cromosomas */
...... 
220222
221223        evolucionar(data_struct_ap1); // evolucionar
222224
223/* for(i=0; i < RESULTADOS ;i++)*/
224/* {*/
225/* printf(" %i-- fit:%i \t",i , *(fitness1 + i));*/
226/* mostrar_indiv(cromo_sal1 + (i*LONG_INDIV), pentarboles, vars);*/
227/* }*/
225/*
226        for(i=0; i < RESULTADOS ;i++)
227        {
228            printf(" %i-- fit:%i \t",i , *(fitness1 + i));
229            mostrar_indiv(cromo_sal1 + (i*LONG_INDIV), pentarboles, vars);
230        }
231*/
228232
229233        ap1 = data_socket_tx_ap; /* devolver resultados */
230234        *(int *)ap1 = ntohl(LONG_INDIV);
...... 
265269
266270if(HW_ENABLE == 1){
267271    close_peripheral(evalfit_ptr1);
272/*
268273    close_peripheral(evalfit_ptr2);
269274    close_peripheral(evalfit_ptr3);
270275    close_peripheral(evalfit_ptr4);
276*/
271277}
272278close(basemem);
273279
Examples/ehw4/src/test/EvalfitTest.c
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
38typedef long long timestamp_t;
39static 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
48int nivel2(int pentarboles)
49{
50int x;
51    if(nivel1 > 1)
52        x = (int)ceil((float)nivel1 / 4);
53    else
54        x = 0;
55return x;
56
57}
58int nivel3(int pentarboles)
59{
60int x;
61    if(nivel2(pentarboles) > 1)
62        x = (int)ceil((float)nivel1 / 16);
63    else
64        x = 0;
65return x;
66}
67int nivel4(int pentarboles)
68{
69int x;
70    if(nivel3(pentarboles) > 1)
71        x = (int)ceil((float)nivel1/ 64);
72    else
73        x = 0;
74return x;
75}
76int nivel5(int pentarboles)
77{
78int x;
79    if(nivel4(pentarboles) > 1)
80        x = (int)ceil((float)nivel1 / 256);
81    else
82        x = 0;
83return x;
84}
85
86
87/**************************************************************************************************************************************
88    genera un numero de forma aleatoria*/
89char 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 */
109void 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 */
136void mostrar_indiv(char *cromo, int pentarboles)
137{
138char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES
139char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'};
140
141ap = cromo;
142for(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}
155printf("\t");
156//for(i = 0; i < LONG_INDIV; i++)
157//{
158// printf("%i,",*(cromo+i));
159//}
160printf("\n");
161}
162
163/**************************************************************************************************************************************
164    imprime un cromosoma completo */
165void mostrar_indiv2(char *cromo, int pentarboles)
166{
167char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES
168char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'};
169
170ap = 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
183printf("\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*/
192void 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*/
212void 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 */
227void gen_indiv(char *cromo, int pentarboles)
228{
229char i=0;
230int n1, n2, n3, n4, n5, c1, c2, c3, c4, c5, sig, indice;
231n1 = n2 = n3 = n4 = n5 = 0;
232c1 = c2 = c3 = c4 = c5 = 0;
233indice = 0;
234
235sig=1;
236
237
238do
239{
240
241if((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}
256else
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*/
348int eval_func_lut(char *ap, int x ) //var apunta al valor de las variables
349{
350char Y;
351char var[VARS], i, a, b, c, d;
352int lut;
353
354for(i=0;i <= VARS-1;i++)
355{
356    var[i] = (x >> i) & 0x1;
357// printf("-%i",var[i]);
358}
359var[VARS] = 0;
360
361a = *(ap + 3) & 0xF;
362b = (*(ap + 3) >> 4) & 0xF;
363c = *(ap + 2) & 0xF;
364d = (*(ap + 2) >> 4) & 0xF;
365i = var[a] + (var[b]*2) + (var[c]*4) + (var[d]*8);
366lut = *(short int *)ap;
367Y = (lut >> i) & 0x1;
368return Y;
369}
370
371/**************************************************************************************************************************************
372    halla la salida de un arbol para una entrada de x */
373int eval_func(char *ap, int x ) //var apunta al valor de las variables
374{
375char 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
376char apl0, apl11, apl12;
377char Y, f1, f2;
378char var[VARS], i;
379
380
381for(i=0;i <= VARS-1;i++)
382{
383    var[i] = (x >> i) & 0x1;
384// printf("-%i",var[i]);
385}
386var[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
396return Y;
397}
398
399
400
401/**************************************************************************************************************************************
402    retorna las salidas de un cromosoma de 5 arboles*/
403void 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*/
429int 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 **/
560void 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 **/
585int periph_map(off_t offset)
586{
587int 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    }
600return baseperiph;
601}
602
603
604/**************************************************************************************************************************************
605main **/
606int 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);
751return 0;
752}
Examples/ehw4/src/test/Makefile
1212
1313NANO_IP = 192.168.254.101
1414
15all: xburst
15all: EvalfitTest
1616
17xburst: xburst.c xburst.h $(COMMON_OBJECTS)
18    $(CROSS)gcc $(COMMON_OBJECTS) xburst.c -o xburst ${CCFLAGS}
17EvalfitTest: EvalfitTest.c evalfit.h $(COMMON_OBJECTS)
18    $(CROSS)gcc $(COMMON_OBJECTS) EvalfitTest.c -o EvalfitTest ${CCFLAGS}
1919
2020.c.o:
2121    $(CROSS)gcc $(CCFLAGS) -c $< -o $@
...... 
2525indent:
2626    indent -bad -bap -nbc -bl -nce -i2 --no-tabs --line-length120 $(COMMON_SOURCES) $(H_SOURCES)
2727
28upload: xburst
29    scp xburst root@$(NANO_IP):binaries
28clean:
29    rm -f *.o EvalfitTest ${OBJECTS} ${EXEC} *~
30
31upload: EvalfitTest
32    scp EvalfitTest root@$(NANO_IP):ehw
Examples/ehw4/src/test/evalfit.h
1//evalfit.h
2
3/************************** Constant Definitions ***************************/
4/*
5    VARS numero de variables
6    FUNCIONES funciones booleanas, aparte de la YES. 0=NOT, 1=AND, 2=XOR, 3=OR, 4=YES . YES es obligatoria.
7*/
8#define VARS 4
9#define FUNCIONES 4
10#define COMBS (int) pow(2,VARS)
11#define FUNCOMBS (int) pow(FUNCIONES + 1, 3)
12#define COMBSLUT 16
13//#define TAMA_ARBOL 16
14//#define NIVEL_ARBOL_MASC 3
15#define FUN_NOT 0
16#define FUN_AND 1
17#define FUN_XOR 2
18#define FUN_OR 3
19#define YES FUNCIONES
20#define NOVAR VARS
21#define PESO_SALIDA 1
22#define PESO_PUERTAS 1
23#define MAXGENERACIONES 10000
24#define RESULTADOS 4
25
26/* Numero de generaciones en el que se amplia la logitud del cromosoma*/
27#define UMBRAL_GENERACION (int) pow(3, pentarboles) * 5000
28#define MAX_PENTARBOLES 16
29#define POBLACION 64
30#define ARBOLES 5
31#define LONG_ARBOL 8
32/*
33POBLACION define el numero de individuos. minimo 6. 2 padres 4 taras
34ARBOLES define el numero de arboles en un individuo. Cada arbol tiene 3 funciones 4 variables.
35INDICES_XXX define el desplazamiento de un individuo en el cromosoma completo de la poblacion, cromo
36*/
37#define nivel1 pentarboles * 4
38#define ARBOLES_INDIV (int)(nivel1 + nivel2(pentarboles) + nivel3(pentarboles) + nivel4(pentarboles) + nivel5(pentarboles))
39#define LONG_INDIV (int)(ARBOLES_INDIV * LONG_ARBOL)
40
41/* Variables globales */
42char *funlut_ap;
43void *evalfit_ptr;
44
45
46/************************** Constant Definitions ***************************/
47
48#define CONFIG_REGS_BASE 0x13010000
49#define SMCR2_OFFSET 0x18
50#define SACR2_OFFSET 0x38
51
52#define EVALFIT_PHYSBASE1 0x14000000
53#define MAP_SIZE 0x4000Ul
54#define MAP_MASK (MAP_SIZE - 1)
55
56#define DONE_MASK 0x8000
57#define ERRORS_MASK 0xFFFF
58
59/** CONTROL REGISTERS **/
60#define EVALFIT_REGBASE_OFFSET 0x1000
61
62#define EVALFIT_CONTROL_OFFSET EVALFIT_REGBASE_OFFSET + 0x1D
63#define CONTROL_RESET_MASK 0x80
64#define CONTROL_START_MASK 0x40
65
66#define EVALFIT_REG_OFFSET EVALFIT_REGBASE_OFFSET + 0
67
68/** WRITE REGISTERS **/
69
70#define EVALFIT_MAX_COMBS EVALFIT_REGBASE_OFFSET + 0x1E
71#define EVALFIT_MAX_LEVEL EVALFIT_REGBASE_OFFSET + 0x1C
72
73
74#define EVALFIT_WREG4 (EVALFIT_REGBASE_OFFSET + (4*4))
75#define EVALFIT_WREG5 (EVALFIT_REGBASE_OFFSET + (4*5))
76#define EVALFIT_wREG6 (EVALFIT_REGBASE_OFFSET + (4*6))
77#define EVALFIT_wREG7 (EVALFIT_REGBASE_OFFSET + (4*7))
78#define EVALFIT_wREG8 (EVALFIT_REGBASE_OFFSET + (4*8))
79
80/** READ REGISTERS **/
81#define EVALFIT_ERRORS EVALFIT_REGBASE_OFFSET + 0x14
82#define EVALFIT_RANDOM EVALFIT_REGBASE_OFFSET + 0x18
83#define EVALFIT_STATUS EVALFIT_REGBASE_OFFSET + 0x16
84
85
86#define EVALFIT_RDREG0 (EVALFIT_REGBASE_OFFSET)
87#define EVALFIT_RDREG1 (EVALFIT_REGBASE_OFFSET + (4*1))
88#define EVALFIT_RDREG2 (EVALFIT_REGBASE_OFFSET + (4*2))
89#define EVALFIT_RDREG3 (EVALFIT_REGBASE_OFFSET + (4*3))
90#define EVALFIT_RDREG4 (EVALFIT_REGBASE_OFFSET + (4*4))
91#define EVALFIT_RDREG5 (EVALFIT_REGBASE_OFFSET + (4*5))
92#define EVALFIT_RDREG6 (EVALFIT_REGBASE_OFFSET + (4*6))
93#define EVALFIT_RDREG7 (EVALFIT_REGBASE_OFFSET + (4*7))
94#define EVALFIT_RDREG8 (EVALFIT_REGBASE_OFFSET + (4*8))
95/** MEMORY **/
96#define EVALFIT_MEMOFFSET 0x0
97#define EVALFIT_OBJOFFSET EVALFIT_MEMOFFSET + (0x40 * 8)
Examples/ehw4/src/test/gen_fun_lut.c
1/*********************************************************************************************************
2** Programa para generar los posibles datos que van en una LUT
3** - Son 3 funciones por LUT
4** - Son 5 posibles funciones. 0 NOT, 1 AND, 2 XOR, 3 OR, 4 YES
5**
6**
7**
8**
9**********************************************************************************************************/
10#include <stdio.h>
11#include <termios.h>
12#include <sys/mman.h>
13#include <stdlib.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <fcntl.h>
17#include <time.h>
18#include <math.h>
19#include <pthread.h>
20#define FUNCIONES 5
21#define FUNCOMBS (int) pow(FUNCIONES, 3)
22#define VARS 4
23#define COMBS (int) pow(2,VARS)
24
25#define POBLACION 64
26#define ARBOLES 5
27#define LONG_ARBOL 8
28
29/**************************************************************************************************************************************
30    halla la salida de un arbol para una entrada de x */
31int eval_func(char *ap, int x ) //var apunta al valor de las variables
32{
33    char apfun[32] = {0, 0x1, 0x1,0x0,0x0, 0x0,0x0,0x0,0x1, 0x0,0x1,0x1,0x0, 0x0,0x1,0x1,0x1, 0x0,0x0,0x1,0x1}; //0=NOT,1=AND,2=XOR,3=OR,4=YES
34    char apl0, apl11, apl12;
35    char Y, f1, f2;
36    char var[VARS], i;
37
38    for(i=0;i <= VARS-1;i++)
39    {
40        var[i] = (x >> i) & 0x1;
41    //printf("-%i",var[i]);
42    }
43    var[VARS] = 0;
44
45    f1 = 2*apfun[((*(ap+1))*4) + (2* (*(var+(*(ap+2))))) + *(var+(*(ap+3))) + 1];
46// ^2da funcion ^1ra variable ^2da variable
47    f2 = apfun[((*(ap+4))*4) + (2* (*(var+(*(ap+5))))) + *(var+(*(ap+6))) + 1];
48// ^3ra funcion ^3ra variable ^4da variable
49    Y = apfun[((*(ap))*4) + f1 + f2 + 1];
50// ^1ra funcion
51// printf("f1:%i f2:%i Y:%i ",f1,f2,Y);
52    return Y;
53}
54
55/**************************************************************************************************************************************
56    imprime un cromosoma completo */
57mostrar_indiv(char *cromo, int pentarboles)
58{
59    char *ap, i, fn[8] = {'!', '&', '^', '|', '_'}; //NOT, AND, XOR, OR, NADA o YES
60    char vn[] = {'A', 'B', 'C', 'D', 'E' , 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '.'};
61
62    ap = cromo;
63    for(i = 0; i < 1; i++){
64            vn[VARS] = '.';
65            printf(" %c%c%c%c%c%c%c", fn[*ap], fn[*(ap+1)], vn[*(ap+2)], vn[*(ap+3)], fn[*(ap+4)], vn[*(ap+5)], vn[*(ap+6)]);
66    }
67    printf("\n");
68}
69
70
71main()
72{
73char *cromo, *tabla,*puertas;
74int i, j, k, l, salida,x;
75char ruta[]="funlut.dat";
76char ruta2[]="puertas.dat";
77short int *funlut_ap;
78int size1;
79
80tabla = malloc(sizeof(short int) * FUNCOMBS*4);
81if(tabla==0) printf("Error en malloc");
82puertas = malloc(sizeof(short int) * FUNCOMBS*4);
83if(tabla==0) printf("Error en malloc");
84cromo = malloc(sizeof(char) * FUNCOMBS);
85if(cromo==0) printf("Error en malloc");
86
87FILE *f1, *fich=fopen(ruta,"wb");
88FILE *f2, *fich2=fopen(ruta2,"wb");
89
90printf("COMBS%i ", FUNCOMBS);
91
92for(i = 0; i < FUNCIONES; i++)
93{
94    for(j = 0; j < FUNCIONES; j++)
95    {
96        for(k = 0; k < FUNCIONES; k++)
97        {
98            x = 0;
99            for(l = 0; l < COMBS; l++)
100            {
101                *(puertas + ((i*25) + (j*5) + k))=0;
102                *cromo = i;
103                *(cromo+1) = j;
104                *(cromo+2) = 0;
105                *(cromo+3) = 1;
106                *(cromo+4) = k;
107                *(cromo+5) = 2;
108                *(cromo+6) = 3;
109                salida = eval_func(cromo, l);
110                printf("%i", salida);
111                x = x + (salida << l);
112                if(*cromo != 4)
113                    (*(puertas + ((i*25) + (j*5) + k) ))++;
114                if(*(cromo + 1) != 4)
115                    (*(puertas + ((i*25) + (j*5) + k)))++;
116                if(*(cromo + 4) != 4 && *cromo != 4)
117                    (*(puertas + ((i*25) + (j*5) + k)))++;
118            }
119            *(tabla + ((i*25) + (j*5) + k)*2 + 1 ) = 0xFF & (x>>8); // se guarda con little endian, para JZ e intel.
120            *(tabla + ((i*25) + (j*5) + k)*2 ) = 0xFF & x; // intercambia estos dos para cambiar endianismo
121            printf(" %i %04hX %4X", (i*25) + (j*5) + k, *(unsigned short *)(tabla + ((i*25) + (j*5) + k)*2), *(unsigned char *)(tabla + ((i*25) + (j*5) + k)*2 + 1));
122            mostrar_indiv(cromo, 1);
123        }
124    }
125}
126
127fwrite((short int *)tabla, FUNCOMBS, sizeof(short int),fich);
128fclose(fich);
129fwrite((char *)puertas, FUNCOMBS, sizeof(char),fich2);
130fclose(fich2);
131
132f1 = fopen("funlut.dat","r");
133if(f1 == NULL){
134    printf("\nError de lectura de archivo!");
135    return 0;}
136fseek (f1, 0, SEEK_END);
137size1 = ftell(f1);
138funlut_ap = malloc(size1);
139if(funlut_ap==0) printf("Error en malloc");
140rewind (f1);
141fread(funlut_ap,2,size1/(sizeof(short int)),f1);
142fclose(f1);
143    for(i = 0; i < (size1/sizeof(short int)); i++)
144printf("\n%0x : %0x ",i, *(unsigned short int*)(funlut_ap+i));
145free(funlut_ap);
146
147}

Archive Download the corresponding diff file

Branches:
master



interactive