Daixiwen, I did not try the simulator. I'm not setup on ModelSim and I understand there's a learning curve.
I dumped the function out to the 7-segment display on the dev board. Guess what? There's no error now. I tried it with index*2 and index+1 and got the same correct result. I also tried compiling in Quartus 9.1. Same result. I'm not sure what happened.
I ended up writing the function without the barrel shifter as follows. Thanks for everyone's help and replies.
function mask (input tap);
integer index;
mask = tap;
for (index = 30; index >= 0; index = index - 1) begin
mask = tap | mask;
end
endfunction