-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortFSM.v
More file actions
150 lines (125 loc) · 3.69 KB
/
Copy pathsortFSM.v
File metadata and controls
150 lines (125 loc) · 3.69 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
`timescale 1ns/1ns
module sortFSM(CLOCK_50,reset,gene,state_controller,compareGeneCounterFitness,andAll,orAll,compareGeneCounter,sortGeneCount,sortBitCount,sortedCounter,state_sortFSM);
/*
in this function we sort the genes based on their fitnesses
the sort algorithm is counting sort and the order is o(n)
*/
parameter primaryInputCount = 8;
parameter population = 24;
parameter sort_controller = 3'b01;
parameter initial_sortFSM = 4'b0000,
zeros_sortFSM = 4'b0001,
zeroCounter_sortFSM = 4'b0010,
ones_sortFSM = 4'b0011,
oneCounter_sortFSM = 4'b0100,
baseBitCounter_sortFSM = 4'b0101,
sortedCounterZeros_sortFSM = 4'b0110,
sortedCounterOnes_sortFSM = 4'b0111,
finished_sortFSM = 4'b1000;
input CLOCK_50,reset;
input gene;
input [2:0]state_controller;
input [primaryInputCount+1:0]compareGeneCounterFitness;
input [primaryInputCount+1:0]andAll,orAll;
output reg [7:0]compareGeneCounter;
output reg [7:0]sortGeneCount,sortBitCount,sortedCounter;
output reg [3:0]state_sortFSM;
/*always@(posedge CLOCK_50)begin//and_or
if(reset)begin
andAll <=1;
orAll <=0;
end else begin
if( state_sortFSM == initial_sortFSM)begin
for(compareGeneCounter=0;compareGeneCounter<population;compareGeneCounter=compareGeneCounter+1)begin : genesss
andAll <= andAll & compareGeneCounterFitness;//genesFitness[compareGeneCounter];
orAll <= orAll | compareGeneCounterFitness;//genesFitness[compareGeneCounter];
end
end
end
end//always*/
//genesFitness[sortGeneCount][sortBitCount],genesFitness[sortGeneCount][sortBitCount]
always@(posedge CLOCK_50)begin//sortFSM
if(reset)begin
state_sortFSM <= initial_sortFSM;
//sortFinished <= 0;
sortBitCount <= 8'd0;
sortGeneCount <= 8'd0;
sortedCounter <= 0;
end else if(state_controller==sort_controller)begin
case(state_sortFSM)
initial_sortFSM :
begin
sortBitCount <= 8'd0;
sortGeneCount <= 8'd0;
sortedCounter <= 0;
state_sortFSM <= zeros_sortFSM;
end
zeros_sortFSM :
if(sortBitCount<primaryInputCount+2)begin
if(andAll==orAll)begin
state_sortFSM <= baseBitCounter_sortFSM;
end else begin
if(sortGeneCount<population && sortedCounter<population)begin
if(gene==0)begin
state_sortFSM <= sortedCounterZeros_sortFSM;
end else
state_sortFSM <= zeroCounter_sortFSM;
end else begin
sortGeneCount<=0;
state_sortFSM <= ones_sortFSM;
end
end
end else begin
state_sortFSM <= finished_sortFSM;
end
zeroCounter_sortFSM :
begin
sortGeneCount <= sortGeneCount+8'd1;
state_sortFSM <= zeros_sortFSM;
end
ones_sortFSM :
if(sortGeneCount<population && sortedCounter<population)begin
if(gene==1)begin
state_sortFSM <= sortedCounterOnes_sortFSM;
end else
state_sortFSM <= oneCounter_sortFSM;
end else begin
state_sortFSM <= baseBitCounter_sortFSM;
end
oneCounter_sortFSM :
begin
sortGeneCount <= sortGeneCount+8'd1;
state_sortFSM <= ones_sortFSM;
end
baseBitCounter_sortFSM :
begin
sortedCounter <= 0;
sortGeneCount <= 0;
sortBitCount <= sortBitCount+8'd1;
state_sortFSM <= zeros_sortFSM;
end
sortedCounterZeros_sortFSM :
begin
sortedCounter <= sortedCounter+8'd1;
sortGeneCount <= sortGeneCount+8'd1;
state_sortFSM <= zeros_sortFSM;
end
sortedCounterOnes_sortFSM :
begin
sortedCounter <= sortedCounter+8'd1;
sortGeneCount <= sortGeneCount+8'd1;
state_sortFSM <= ones_sortFSM;
end
finished_sortFSM :
begin
end
default:
begin
end
endcase
end else begin//if
//sortFinished <= 0;
state_sortFSM <= initial_sortFSM;
end
end//always
endmodule