omara007
Advanced Member level 4
- Joined
- Jan 6, 2003
- Messages
- 1,237
- Helped
- 50
- Reputation
- 102
- Reaction score
- 16
- Trophy points
- 1,318
- Location
- Cairo/Egypt
- Activity points
- 9,716
muni123 said:The following function can be implemented to check for first 1 from MSB side:
function func_first1(input [7:0]x);
begin
for(i=0;i<8;i=i+1)
begin
if(x[0])
func_first1 = i;
else
x=x>>1;
end
end
Accordingly we can write the function for first one from LSB side and first 0 from both sides too...
Correct me if any mistakes.
lordsathish said:What you are asking for is just a priority encoder...
This code is readily available in many text books and
And yx.yang has given the code...
may be this will help ?omara007 said:Can you suggest a code for a vector of a generic width ?
module select
(
input [N-1:0] in_sel,
output [N-1:0] sel
);
parameter N = 4;
genvar i;
assign sel[0] = in_sel[0];
generate for ( i=1; i<N; i=i+1 )
begin: x
sum #( N ) // AND gate
inst
(.out(sel[i]),
.in( {
{ (N-1-i){1'b1} }, // inputs above 'i' index
in_sel[i],
~in_sel[i-1:0] // inputs below 'i' index
}
)
);
end
endgenerate
endmodule
module sum // simple AND 'N' input gate
(
input [N-1:0] in,
output out
);
parameter N = 4;
assign out = ∈
endmodule
not me, but - eventually - the author of the threadFvM said:But to complete the above examples, you have to define first what's
the intended output for an all zero vector.
FvM said:The variants utilizing a for loop are basically an appropriate way to define the priority encoder as a parameterizable function.
But to complete the above examples, you have to define first what's the intended output for an all zero vector.
I also think so, but if you define no output value for the function, the compiler is free to assign an arbitrary (wrong) value.but it seems quite natural that if the input vector contains all '0' nothing is selected so no action triggered
FvM said:I also think so, but if you define no output value for the function, the compiler is free to assign an arbitrary (wrong) value.but it seems quite natural that if the input vector contains all '0' nothing is selected so no action triggered
in my example the compiler has no free choice for "all '0'" input;FvM said:/.../ if you define no output value for the function,
the compiler is free to assign an arbitrary (wrong) value
May be, I was referring to the original specification, that defined a result range of 0 to 7, or in other words a bit index. I would expect, that a different value (e. g. 8)should be used to mark the all zero case, cause a value of 0 is already assigned to bit 0.in my example the compiler has no free choice for "all '0'" input; zero in gives zero out;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?