/* Example 5: "swbinto2segs" Show the binary number specified in the slide switches 0-7 in the two righmost digits, in hexadecimal. With a delayed counter "delay_counter" we solve the problem of mixed digits, without a need to set the clock frequency arbitrarily slow. */ module esim5(CLK,SW_IN,SEG_OUT,DIGIT_OUT); input CLK; input [7:0] SW_IN; output [7:0] SEG_OUT; output [3:0] DIGIT_OUT; function [6:0] HEX2LED; input [3:0] HEX; begin case (HEX) 4'b0001 : HEX2LED = 7'b1111001; //1 4'b0010 : HEX2LED = 7'b0100100; //2 4'b0011 : HEX2LED = 7'b0110000; //3 4'b0100 : HEX2LED = 7'b0011001; //4 4'b0101 : HEX2LED = 7'b0010010; //5 4'b0110 : HEX2LED = 7'b0000010; //6 4'b0111 : HEX2LED = 7'b1111000; //7 4'b1000 : HEX2LED = 7'b0000000; //8 4'b1001 : HEX2LED = 7'b0010000; //9 4'b1010 : HEX2LED = 7'b0001000; //A 4'b1011 : HEX2LED = 7'b0000011; //b 4'b1100 : HEX2LED = 7'b1000110; //C 4'b1101 : HEX2LED = 7'b0100001; //d 4'b1110 : HEX2LED = 7'b0000110; //E 4'b1111 : HEX2LED = 7'b0001110; //F default : HEX2LED = 7'b1000000; //0 endcase end endfunction parameter dcw = 15; reg [dcw:0] delay_counter = 0; wire hi_nybble_now = delay_counter[dcw]; // Use the highest bit as a flag. assign SEG_OUT[6:0] = HEX2LED(hi_nybble_now ? SW_IN[7:4] : SW_IN[3:0]); assign SEG_OUT[7] = hi_nybble_now; // For debugging, keep the 2nd dp off. assign DIGIT_OUT = ((0 == hi_nybble_now) ? 4'b1110 : 4'b1101); always @(posedge CLK) begin delay_counter <= delay_counter+1; // Wraps around, eventually. end endmodule