|
楼主 |
发表于 2011-1-22 11:22:34
|
显示全部楼层
那好吧。我贴上代码:
` timescale 1ns/1ns
// define WIDTH 4
// define NUM_INPUTS 8
// define INDEX_WIDTH 3
module DW_minmax(
a,
tc,
min_max,
value,
index
);
parameter WIDTH = 4; // element WIDTH
parameter NUM_INPUTS = 8; // number of elements in input array
parameter INDEX_WIDTH = 3; // size of index pointer = ceil(log2(NUM_INPUTS))
input [ WIDTH* NUM_INPUTS-1:0] a; //Concatenated input vector
input tc; //0=unsigned,1=signed
input min_max; //0=find min,1=find max
output [ WIDTH-1:0] value; //min or max value found
output [ INDEX_WIDTH-1:0] index; //index to value found
reg [ WIDTH-1:0]b[ NUM_INPUTS-1:0];
reg [ WIDTH* NUM_INPUTS-1:0] a_tmp;
reg [ INDEX_WIDTH-1:0] pi;
reg [ WIDTH-1:0] max_tmp;
reg [ WIDTH-1:0] min_tmp;
reg [ INDEX_WIDTH-1:0] max_index;
reg [ INDEX_WIDTH-1:0] min_index;
wire [ WIDTH-1:0] value;
wire [ INDEX_WIDTH-1:0] index;
always @(a)
begin: takeout
a_tmp=a;
for (pi=0;pi< NUM_INPUTS-1;pi=pi+1)
begin
b[pi] = a_tmp[ WIDTH-1:0];
a_tmp = a_tmp>> WIDTH;
end
b[pi] = a_tmp[ WIDTH-1:0];
a_tmp = a_tmp>> WIDTH;
end
always @(a)
begin: max_min
max_tmp=b[0];
min_tmp=b[0];
max_index=0;
min_index=0;
for(pi=1;pi< NUM_INPUTS-1;pi=pi+1)
begin
if(tc==1'b0)
begin
if(max_tmp<b[pi])
begin
max_tmp=b[pi];
max_index=pi;
end
if(min_tmp>b[pi])
begin
min_tmp=b[pi];
min_index=pi;
end
end
else if(tc==1'b1)
begin
//find max
if(max_tmp[ WIDTH-1] > b[pi][ WIDTH-1])//when highest bit =1,the number is less then the number of highest bit=0
begin //change
max_tmp=b[pi];
max_index=pi;
end
else if(max_tmp[ WIDTH-1] < b[pi][ WIDTH-1])//when highest bit =0,the number is greater then the number of highest bit=1
begin //dont change (latch)
max_tmp=max_tmp;
max_index=max_index;
end
else if(max_tmp[ WIDTH-1] == b[pi][ WIDTH-1])//when highest bit equal, normal compare
begin
if(max_tmp<b[pi])
begin
max_tmp=b[pi];
max_index=pi;
end
end
//find min
if(min_tmp[ WIDTH-1] < b[pi][ WIDTH-1])
begin
min_tmp=b[pi];
min_index=pi;
end
else if(min_tmp[ WIDTH-1] > b[pi][ WIDTH-1])
begin
min_tmp=min_tmp;
min_index=min_index;
end
else if(min_tmp[ WIDTH-1] == b[pi][ WIDTH-1])
begin
if(min_tmp>b[pi])
begin
min_tmp=b[pi];
min_index=pi;
end
end
end
end
if(tc==1'b0)
begin
if(max_tmp<b[pi])
begin
max_tmp=b[pi];
max_index=pi;
end
if(min_tmp>b[pi])
begin
min_tmp=b[pi];
min_index=pi;
end
end
else if(tc==1'b1)
begin
//find max
if(max_tmp[ WIDTH-1] > b[pi][ WIDTH-1])//when highest bit =1,the number is less then the number of highest bit=0
begin //change
max_tmp=b[pi];
max_index=pi;
end
else if(max_tmp[ WIDTH-1] < b[pi][ WIDTH-1])//when highest bit =0,the number is greater then the number of highest bit=1
begin //dont change (latch)
max_tmp=max_tmp;
max_index=max_index;
end
else if(max_tmp[ WIDTH-1] == b[pi][ WIDTH-1])//when highest bit equal, normal compare
begin
if(max_tmp<b[pi])
begin
max_tmp=b[pi];
max_index=pi;
end
end
//find min
if(min_tmp[ WIDTH-1] < b[pi][ WIDTH-1])
begin
min_tmp=b[pi];
min_index=pi;
end
else if(min_tmp[ WIDTH-1] > b[pi][ WIDTH-1])
begin
min_tmp=min_tmp;
min_index=min_index;
end
else if(min_tmp[ WIDTH-1] == b[pi][ WIDTH-1])
begin
if(min_tmp>b[pi])
begin
min_tmp=b[pi];
min_index=pi;
end
end
end
end
assign value=min_max?max_tmp:min_tmp;
assign index=min_max?max_index:min_index;
endmodule |
|