/* Example 14: by karttu, Jan 2 2005. Show permutations '43210' - '01234', the four rightmost in 7-segment display, and the leftmost's binary expansion in the three rightmost leds. Permutation changes with each successive pressing of the rightmost push button. */ module esim14(CLK,PB_IN,SEG_OUT,DIGIT_OUT,LED_OUT); input CLK; input PB_IN; output [7:0] SEG_OUT; output [3:0] DIGIT_OUT; output [2:0] LED_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); parameter w = 2; reg [w:0] d [3:0]; wire [w:0] d_sig [3:0]; `define DIG0 {1'b0,d[0]} `define DIG1 {1'b0,d[1]} `define DIG2 {1'b0,d[2]} `define DIG3 {1'b0,d[3]} 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); fex4perm FEX4PERM(f0,f1,f2,f3,d_sig[0],d_sig[1],d_sig[2],d_sig[3],LED_OUT); integer i; 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); for(i=0; i<=3; i=i+1) d[i] <= d_sig[i]; end endmodule