I have a set of registers : reg [7:0] dataRegs [3:0]
and I have a one-hot signal: reg [3:0] oneHot
. I want to write a value to just the register indicated by the one-hot signal. So if oneHot
= 4'b0010, that would write to dataRegs[2]
. What would be the best way to do that?
So to read from a specific register, I have tri-state buffers as follows:
for (i=0; i<4; i++) begin
output = oneHot[i]?dataRegs[i]:'bz;
end
But, I'm not sure how I could use the same oneHot
signal to also write to this register. I'm wondering if there is a way to avoid converting oneHot
to a binary number just to index into the reg array. Could I do something using a write enable for the registers?
When i=0, write only to reg[0]. When i=1, write only to reg[1], etc.
always @(posedge clk) begin
for (i=0; i<4; i++) begin
if (oneHot[i]) dataRegs[i] <= wdata;
end
end