Complete the code to declare a 2-bit binary state register.
reg [[1]:0] state;
The state register is 2 bits wide, so the index goes from 1 down to 0.
Complete the code to assign a one-hot encoded state for state 2.
assign one_hot_state = 4'b[1];
In one-hot encoding, only one bit is high. For state 2, the third bit (index 2) is set: 0100.
Fix the error in the Gray code assignment for 3-bit states.
assign gray_state = state ^ (state >> [1]);The Gray code is generated by XORing the state with itself shifted right by 1 bit.
Fill both blanks to complete the binary to one-hot conversion.
assign one_hot = 1 << [1]; // shift 1 by state index [2]
To convert binary state to one-hot, shift 1 left by the state value.
Fill all three blanks to complete the Gray code to binary conversion.
always @(*) begin binary[WIDTH-1] = [1]; for (int i = WIDTH-2; i >= [2]; i = i - 1) begin binary[i] = binary[i+1] ^ [3][i]; end end
The binary MSB is the Gray MSB. Then each binary bit is XOR of previous binary bit and current Gray bit.
