/* Example 12: by karttu, Jan 2 2005. Show numbers 0 - 120 in factorial expansion, as '0000' - '4321'. One with each successive pressing of the rightmost push button. (show the previous word after the user presses the leftmost button). */ module esim12b(CLK,PB_IN,SEG_OUT,DIGIT_OUT); input CLK; input PB_IN; output [7:0] SEG_OUT; output [3:0] DIGIT_OUT; // Binary coded registers for the digits of the factorial expansion: reg f0 = 0; reg [1:0] f1 = 0; reg [1:0] f2 = 0; reg [2:0] f3 = 0; wire inc_f0; wire [1:0] inc_f1; wire [1:0] inc_f2; wire [2:0] inc_f3; wire PB_INC; // State of the debounced push button for incrementing. reg pb_inc_prev = 0; // is saved here also. wire inc_it = (PB_INC && ~pb_inc_prev); debounced_button DBB0(CLK,PB_IN,PB_INC); `define DIG0 {3'b000,f0} `define DIG1 {2'b00,f1} `define DIG2 {2'b00,f2} `define DIG3 {1'b0,f3} show4hex DIGDISPLAY(CLK,`DIG0,`DIG1,`DIG2,`DIG3,SEG_OUT,DIGIT_OUT); inc4fexb INC4FEX(f0,f1,f2,f3,inc_f0,inc_f1,inc_f2,inc_f3); always @(posedge CLK) begin pb_inc_prev <= PB_INC; f0 <= (inc_it ? inc_f0 : f0); f1 <= (inc_it ? inc_f1 : f1); f2 <= (inc_it ? inc_f2 : f2); f3 <= (inc_it ? inc_f3 : f3); end endmodule