-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.v
More file actions
193 lines (168 loc) · 6.6 KB
/
Copy pathevaluation.v
File metadata and controls
193 lines (168 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
`timescale 1ns/1ns
module evaluation(clk,reset,functionMemIsReady,gene,geneFuncMem,functionMem,state_controller,fitness,fitCounter,state_evaluationFSM);
/*
This function control the evaluation phase of the genetic algorithm.
This phase has 7 state, initial_evaluationFSM which sets the registers like fitness value and mismatches zero,
blockCalculation_evaluationFSM in which we are waiting for completion of the result of the one gene,
mismatch_evaluationFSM in which the result will compare with the correct result from the table,
shiftMismatches_evaluationFSM in which fitness function will shift for every mismatches which have found,
fitnessResult_evaluationFSM in which we have found all mismatches for all verity of inputs for
one configuration and have the final result of fitness;
geneFound_evaluationFSM in this state we find if the gene is completely correct or not, if it was correct, it will report,
finished_evaluationFSM we are here when the evaluation of all genes is completed;
*/
parameter geneBit = 110;
parameter row = 3;
parameter column = 3;
parameter selBit = 4;
parameter funcBit = 3;
parameter funcCount=8;
parameter geneResultBit = 2;
parameter primaryInputCount=8;//tedade voroodi haye momken bara geneBlock
parameter primaryInputBit=3;
parameter funcResultBit = 4;
parameter population = 24;
parameter maxSupport = 128;
parameter fitness_controller = 3'b001;
parameter initial_evaluationFSM = 3'b000,
blockCalculation_evaluationFSM = 3'b001,
mismatch_evaluationFSM = 3'b010,
shiftMismatches_evaluationFSM = 3'b011,
fitnessResult_evaluationFSM = 3'b100,
geneFound_evaluationFSM=3'b101,
finished_evaluationFSM = 3'b110;
parameter initial_geneBlockFSM = 2'b00,
geneIsReady_geneBlockFSM = 2'b01,
waitForResult_geneBlockFSM = 2'b10,
finished_geneBlockFSM = 2'b11;
input clk,reset,functionMemIsReady;
input [geneBit-1:0]gene;
input [primaryInputCount*geneResultBit-1:0]geneFuncMem;
input [funcCount*funcResultBit-1:0]functionMem;
input [2:0]state_controller;
output reg [primaryInputCount+1:0]fitness;
output reg [7:0]fitCounter;
output reg[2:0]state_evaluationFSM;
//output [7:0]fitnessFinished;
//output geneFound;
// output reg fitnessReady;
integer geneResultBitIndex,primaryInputIndex;
reg [7:0]geneResultBitCounter;
//reg [2:0]state_evaluationFSM;
wire [geneBit+primaryInputBit-1:0]geneIn;
reg [primaryInputCount+1:0]genesFitness[geneResultBit-1:0];
wire [geneResultBit-1:0]geneResult;
wire blockResultIsReady;
reg [geneResultBit-1:0]mismatch;
reg [geneResultBit-1:0]geneFunc [primaryInputCount-1:0];
reg [primaryInputBit:0]inputGeneCounter;//primaryInputBit
reg geneInputIsReady;
reg fitnessReady;
wire [1:0]state_geneBlockFSM;
geneBlock #(geneBit+primaryInputBit,row,column,selBit,funcBit,funcCount,geneResultBit,primaryInputBit,funcResultBit,fitness_controller,initial_geneBlockFSM,geneIsReady_geneBlockFSM,waitForResult_geneBlockFSM,finished_geneBlockFSM)gb(clk,~functionMemIsReady,geneInputIsReady,geneIn,functionMem,state_controller,geneResult,blockResultIsReady,state_geneBlockFSM);
assign geneIn = {inputGeneCounter[primaryInputBit-1:0],gene};
always@(posedge clk)begin
if(reset)begin
geneInputIsReady<=0;
end else begin
if(state_evaluationFSM==shiftMismatches_evaluationFSM || state_evaluationFSM==initial_evaluationFSM )begin
geneInputIsReady<=1;
end else begin
geneInputIsReady<=0;
end
end
end
always@(posedge clk) begin
if(reset)begin
state_evaluationFSM <= initial_evaluationFSM;
fitnessReady<=0;
geneResultBitCounter<=0;
inputGeneCounter<=0;
fitness<=0;
mismatch<=0;
fitCounter<=0;
//geneInputIsReady <=0;
end else if(state_controller==fitness_controller )begin
case(state_evaluationFSM)
initial_evaluationFSM :
begin
if(state_geneBlockFSM==geneIsReady_geneBlockFSM)begin
//columnCounter<=0;
fitnessReady<=0;
geneResultBitCounter<=0;
inputGeneCounter<=0;
fitness<=0;
fitCounter<=0;
for(primaryInputIndex=0;primaryInputIndex<primaryInputCount;primaryInputIndex=primaryInputIndex+1)begin
geneFunc[primaryInputIndex]<=geneFuncMem[primaryInputIndex*geneResultBit +: geneResultBit];
end
for(geneResultBitIndex=0;geneResultBitIndex<geneResultBit;geneResultBitIndex=geneResultBitIndex+1)begin
genesFitness[geneResultBitIndex]<=1;
end
state_evaluationFSM <= blockCalculation_evaluationFSM;
end
//geneInputIsReady <= 1;
end
blockCalculation_evaluationFSM :
begin
//geneInputIsReady <=0;
if(fitCounter>=population)begin
state_evaluationFSM <= finished_evaluationFSM;
end else
if(inputGeneCounter>=primaryInputCount)begin
state_evaluationFSM <= fitnessResult_evaluationFSM;
end else
if(blockResultIsReady)begin
state_evaluationFSM <= mismatch_evaluationFSM;
end
if(inputGeneCounter==0)begin
fitness<=0;
for(geneResultBitIndex=0;geneResultBitIndex<geneResultBit;geneResultBitIndex=geneResultBitIndex+1)begin
genesFitness[geneResultBitIndex]<=1;
end
end
end
mismatch_evaluationFSM :
begin
mismatch = geneFunc[inputGeneCounter] ^ geneResult;
state_evaluationFSM <= shiftMismatches_evaluationFSM;
end
shiftMismatches_evaluationFSM :
begin
//columnCounter<=0;
//geneInputIsReady <=1;
inputGeneCounter<=inputGeneCounter+1;
for(geneResultBitIndex=0;geneResultBitIndex<geneResultBit;geneResultBitIndex=geneResultBitIndex+1)begin
if(mismatch[geneResultBitIndex]==1)begin
genesFitness[geneResultBitIndex]<=genesFitness[geneResultBitIndex]<<1;
end
end
state_evaluationFSM <= blockCalculation_evaluationFSM;
end
fitnessResult_evaluationFSM :
begin
if(geneResultBitCounter<geneResultBit)begin
fitness <= genesFitness[geneResultBitCounter]+fitness;
geneResultBitCounter <= geneResultBitCounter+1;
end else begin
fitnessReady<=1;
fitCounter<=fitCounter+1;
inputGeneCounter<=0;
geneResultBitCounter<=0;
if(fitness==geneResultBit)begin
state_evaluationFSM<=geneFound_evaluationFSM;
end else
state_evaluationFSM <= blockCalculation_evaluationFSM;
end
end
geneFound_evaluationFSM:
begin
end
finished_evaluationFSM :
begin
state_evaluationFSM<= initial_evaluationFSM;
end
endcase
end
end//always
endmodule