The same method as described above, but parameterized. Use XOR shifted by one bit to determine where the bit is changing, then use a downstream priority encoder to output the first change location. I filled up my_reg[0], so the first bit does not create a delta.
localparam width=16;
reg [width-1:0] my_reg;
wire [width:0] delta;
reg [$clog2(width)-1:0] index; // Note: $clog2 was added in IEEE1364-2005
integer i;
assign delta = my_reg ^ { my_reg, my_reg[0] };
always @* begin
index = 0;
for (i=0; i<width; i=i+1)
if (delta[i])
index = i;
end
Above the code on the EDA playground (thanks for heads-up on this, BTW)
http://www.edaplayground.com/x/3uP
source
share