Skip to content

Instantly share code, notes, and snippets.

Created January 26, 2013 02:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
DE0 Hex 4-digit counter (zero suppress) submodule: segleddec,unchat,cntb.
// ()MAXCNT-1)bit freerun counter
//
module cntb(clk,rst, q);
parameter cntb_MAXCNT=15; // default 15:0
input clk;
input rst;
output [cntb_MAXCNT:0] q;
reg [cntb_MAXCNT:0] cnt;
assign q=cnt;
always @(posedge clk or posedge rst) begin
if (rst==1'b1)
cnt=0;
else
cnt=cnt+1;
end
endmodule
// terasIC DE0 topmodule skeleton
// onboard I/O 3-button,10-sw,10-LED,4-7SEG
//
// HEX 4digit-counter (zero suppress)
//
module hexcounter(clk,btn,sw,led,hled0,hled1,hled2,hled3,lcd_bl);
input clk;
input [2:0] btn;
input [9:0] sw;
output [9:0] led;
output [7:0] hled0;
output [7:0] hled1;
output [7:0] hled2;
output [7:0] hled3;
output lcd_bl;
wire rst;
wire [47:0] cnt;
wire [2:0] s_btn;
wire [9:0] s_sw;
wire bout3_2,bout2_1,bout1_0,bout0_0;
wire clk_ms;
wire [14:0] dmy;
cntb #(15) freerun(clk,1'b0,{clk_ms,dmy[14:0]});
cntb #(39) cnt40b_1(clk,rst, cnt);
unchat unchat_btn2(clk_ms,btn[2], s_btn[2]);
unchat unchat_sw0(clk_ms,sw[0],s_sw[0]);
unchat unchat_sw1(clk_ms,sw[1],s_sw[1]);
unchat unchat_sw2(clk_ms,sw[2],s_sw[2]);
unchat unchat_sw3(clk_ms,sw[3],s_sw[3]);
unchat unchat_sw4(clk_ms,sw[4],s_sw[4]);
unchat unchat_sw5(clk_ms,sw[5],s_sw[5]);
unchat unchat_sw6(clk_ms,sw[6],s_sw[6]);
unchat unchat_sw7(clk_ms,sw[7],s_sw[7]);
unchat unchat_sw8(clk_ms,sw[8],s_sw[8]);
unchat unchat_sw9(clk_ms,sw[9],s_sw[9]);
segleddec seg0(cnt[27:24],1'b0,1'b0, hled0,bout0_0); // Digit xxx0 always on
segleddec seg1(cnt[31:28],1'b0,bout2_1, hled1,bout1_0);
segleddec seg2(cnt[35:32],1'b0,bout3_2, hled2,bout2_1);
segleddec seg3(cnt[39:36],1'b0,1'b1, hled3,bout3_2);
assign rst=~s_btn[2];
// assign led[9:0]=cnt[31:(31-9)];
assign led[9:0]=s_sw[9:0];
endmodule
// 7segment LED decoder(anode common)
// support zero suppress
//
module segleddec(bcd,dp,bin, led,bout);
input [3:0] bcd;
input dp; // decimal point
input bin;
output [7:0] led;
output bout;
assign bout=(bcd==4'b0000)?1'b1:1'b0; // ~&bcd
assign led=(bout&&bin)?8'b11111111:ALED(bcd,dp);
function [7:0] ALED;
input [3:0] bcd;
input dp;
case(bcd)
4'h0: ALED={ ~dp, 7'b1000000 }; // 0
4'h1: ALED={ ~dp, 7'b1111001 }; // 1
4'h2: ALED={ ~dp, 7'b0100100 }; // 2
4'h3: ALED={ ~dp, 7'b0110000 }; // 3
4'h4: ALED={ ~dp, 7'b0011001 }; // 4
4'h5: ALED={ ~dp, 7'b0010010 }; // 5
4'h6: ALED={ ~dp, 7'b0000011 }; // 6
4'h7: ALED={ ~dp, 7'b1111000 }; // 7
4'h8: ALED={ ~dp, 7'b0000000 }; // 8
4'h9: ALED={ ~dp, 7'b0011000 }; // 9
4'ha: ALED={ ~dp, 7'b0001000 }; // A
4'hb: ALED={ ~dp, 7'b0000011 }; // B
4'hc: ALED={ ~dp, 7'b0100111 }; // C
4'hd: ALED={ ~dp, 7'b0100001 }; // D
4'he: ALED={ ~dp, 7'b0000110 }; // E
4'hf: ALED={ ~dp, 7'b0001110 }; // F
default: ALED={ ~dp, 7'b1111111 }; // off
endcase
endfunction
endmodule
// un-chattering
//
module unchat(c_clk,inkey, s_key);
input c_clk; // 1-30ms
input inkey;
output s_key;
reg hold;
assign s_key=hold;
always @(posedge c_clk) begin
hold=inkey;
end
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment