Hi Jling,
I would expect both code snippets to behave the same if you first take care of the contact bounce and synchronize the WF_BUTTON input signal. Running an asynchronous signal into a digital system is fraught with problems and unexpected behavior.
You can google for contact bounce, it is real and must be dealt with when being used with digital logic systems. Here is a good explanation.
Please see the DOs and DON’Ts section here. I will add asynchronous signal handling to the list. WebFPGA_book
Please see the example on the webfpga IDE website, “WF_blinky_with_switch.v”
https://beta.webfpga.io/dashboard
You can download it to your board to experiment. The example uses the WF_BUTTON to change the rate in which the WF_LED blinks. In the example there is a WF_switch_debounce module which cleans up the contact bounce and synchronizes the signal for use in the second always block.
As an experiment you can run the WF_BUTTON directly into that always block and download it to the board and you will see some erratic behavior.
Change line 32, ===> if (~WF_BUTTON).
// create smaller counter when button pushed
always @ (posedge WF_CLK)
if (blink_cnt == 0)
blink_cnt <= 4'b0001;
else
if (~WF_BUTTON) // For experiment change this line
blink_cnt <= { blink_cnt[2:0],blink_cnt[3]}; // rotate a one
Contact bounce will take milliseconds to settle. So we must also include some sampling point for the logic to eliminate the effects of the bounce. We use the WF_timer modules for this to create a pulse every 10ms. The debounce module uses this pulse as a sampling signal to clean up the bouncing signal and output a clean signal to be used in the digital logic block.
I would also remove the negedge
from your always @
statement. It is not needed, and would not recommended a new user to verilog in using it unless they are fully aware of how the synthesis tools will resolve it. Designers will use that for asynchronous reset of a system and synchronous unreset. Special care is needed to make sure no timing violation can occur by design.
Mick