📄 sale2.v
字号:
module sale( reset, //low active charge, //button for get the charge clock, ten_in, five_in, one_in, p_five_in, goods_a, // 1 dollar goods_b, // 1.5 dollars goods_c, // 2 dollars goods_d, // 2.5 dollars goods_e, // 3 dollars goods_out, one_back, p_five_back, total_money, flag_enough ); input reset; input clock; input charge; input ten_in, five_in, one_in, p_five_in; input goods_a, goods_b, goods_c, goods_d, goods_e; // buttons to press, which stands for a certain price; output[4:0]goods_out; //bit0: goods_a //bit1: goods_b //bit2: goods_c //bit3: goods_d //bit4: goods_e output[11:0] one_back, p_five_back; //charges output[11:0]total_money; //bit0 to bit3 stand for the part less than one dollar //bit4 to bit7 stand for ones of dollars //bit9 to bit11 stand for tens of dollars output flag_enough; //whether the money input is enough for the goods to buy //***********************************/ reg[4:0] goods_out; reg[3:0] one_back,p_five_back; reg[11:0] total_money; reg flag_enough; reg cout; reg money; reg[3:0] state; parameter IDLE= 4'b0001, GIVE_MONEY=4'b0010, BUY= 4'b0100, CHARGE= 4'b1000; always@(posedge clock) if(!reset) begin goods_out<=5'b0000; {one_back,p_five_back}<=0; total_money<=0; flag_enough<=0; state<=IDLE; end else case(state) IDLE:begin goods_out<=5'b00000; {one_back,p_five_back}<=0; total_money<=0; flag_enough<=0; state<=GIVE_MONEY; end GIVE_MONEY: if(ten_in) begin total_money<=total_money+100; state<=GIVE_MONEY; end else if(five_in) begin total_money<=total_money+50; state<=GIVE_MONEY; end else if(one_in) begin total_money<=total_money+10; state<=GIVE_MONEY; end else if(p_five_in) begin total_money<=total_money+5; state<=GIVE_MONEY; end else state<=BUY; BUY: if(goods_a) begin if(total_money>=10) begin total_money<=total_money-10; goods_out<=5'b00001; state<=BUY; end else begin flag_enough<=0; state<=BUY; end end else if(goods_b) begin if(total_money>=15) begin total_money<=total_money-15; goods_out<=5'b00010; state<=BUY; end else begin flag_enough<=0; state<=BUY; end end else if(goods_c) begin if(total_money>=20) begin total_money<=total_money-20; goods_out<=5'b00100; end else begin flag_enough<=0; state<=BUY; end end else if(goods_d) begin if(total_money>=25) begin total_money<=total_money-25; goods_out<=5'b01000; state<=BUY; end else begin flag_enough<=0; state<=BUY; end end else if(goods_e) begin if(total_money>=30) begin total_money<=total_money-30; goods_out<=5'b10000; state<=BUY; end else begin flag_enough<=0; state<=BUY; end end else state<=CHARGE; CHARGE: if(total_money>=10) begin one_back<=one_back+1; state<=CHARGE; end else if(total_money>=5) begin p_five_back<=p_five_back+1; state<=CHARGE; end else state<=IDLE; endcaseendmodule
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -