/* 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 esim12(CLK,PB_IN,SEG_OUT,DIGIT_OUT); input CLK; input PB_IN; output [7:0] SEG_OUT; output [3:0] DIGIT_OUT; reg [3:0] DIG[3:0]; // Three one-hot shift-registers + 1-bit register for f0. reg f0 = 0; reg [2:0] f1 = 1; reg [3:0] f2 = 1; reg [4:0] f3 = 1; wire inc_f0; wire [2:0] inc_f1; wire [3:0] inc_f2; wire [4: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); show4hex DIGDISPLAY(CLK,DIG[0],DIG[1],DIG[2],DIG[3],SEG_OUT,DIGIT_OUT); inc4fex INC4FEX(f0,f1,f2,f3,inc_f0,inc_f1,inc_f2,inc_f3); always @(posedge CLK) begin // Update the digits depending on the state of shift-registers: DIG[0] <= (f0 ? 4'b0001 : 4'b0000); DIG[1] <= (f1[2] ? 4'b0010 : f1[1] ? 4'b0001 : 4'b0000); DIG[2] <= (f2[3] ? 4'b0011 : f2[2] ? 4'b0010 : f2[1] ? 4'b0001 : 4'b0000); DIG[3] <= (f3[4] ? 4'b0100 : f3[3] ? 4'b0011 : f3[2] ? 4'b0010 : f3[1] ? 4'b0001 : 4'b0000); 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