/* Example 9: by karttu, Dec 12 2004. Show the successive Factorial numbers when the user presses the rightmost push button. */ module esim9(CLK,PB_IN,SEG_OUT,DIGIT_OUT); input CLK; input PB_IN; output [7:0] SEG_OUT; output [3:0] DIGIT_OUT; reg [15:0] next_n = 1; reg [15:0] fact_n = 1; wire PB_DEB; // State of the debounced push button. reg pb_deb_prev = 0; // is saved here also. debounced_button DBB(CLK,PB_IN,PB_DEB); shw16dec DECDISPLAY(CLK,PB_DEB,fact_n,SEG_OUT,DIGIT_OUT); always @(posedge CLK) begin if(PB_DEB && ~pb_deb_prev) // Only on @(posedge PB_DEB) begin // Do the thing. fact_n <= next_n * fact_n; next_n <= next_n + 1; end pb_deb_prev <= PB_DEB; end endmodule