Reputation: 470
I am looking to map the four basic operations (multiplication, addition, subtraction, and division) to one of four keys which are located on my FPGA board. I have an if-statement which would check to see which key is pressed and execute the appropriate statements. However, when writing the always block sensitivity list no matter what I place in the block it will not recognize all four keys. If I leave the sensitivity block empty then it will recognize all of the keys but will perform the operation of the first key and wait for the other keys to be pressed to perform those operations.
always @(negedge KEY) begin
if (KEY[0] == 0) begin
...
end else if(KEY[1] == 0) begin
//Check for value for A and B
if(SW[15:8] < SW[7:0]) begin
...
end
end else if(KEY[2] == 0) begin
...
end
end
Implementing the code like this will calculate only the operation which is connected to KEY1. The rest of the keys act as if they have not been programmed. Is there any way around this small annoyance?
Thanks!
Upvotes: 2
Views: 6662
Reputation: 10281
Your specific problem is that event control expressions are only evaluated on the least significant bit of a multi-bit expression. So, the always block was only waiting for a falling edge on KEY[0].
If this is just a simulation model, there are multiple way to fix this. always @(negedge KEY[0], negedge KEY[1], negedge KEY[2], negedge KEY[3])
would do the job, as would always @*
. If you're synthesising, you should use a clock to sample the 4 bits.
Upvotes: 0
Reputation: 35933
Unless you're modelling a clocked flip flop, you should always use the default (@*) sensitivity list for a combinational block. There's no synthesizable combinational circuit which is only sensitive to the negative edge of a signal.
If you really want to do something only when a key is initially pressed, then compare the key state to the value of the key from the previous clock edge stored in a register.
always @* begin
if(key[0] && !key_last[0]) begin
//assert some signal on key0 press
end
if(key[1] ...) begin
//assert on key1 press
end
end
always @(posedge clock) key_last <= key;
If you intend to check the state of more than one key, then you shouldn't use if/else statement, as that will stop checking after the first true statement. Just write four individual if statements.
Upvotes: 3