从零开始动手做Verilog实验--03--自动售卖机

一、背景知识

1.接受的只有两种面值: 0.5美元和1美元纸币

2.商品定价为1.5美元

二、代码

sale_machine.v代码如下:

c 复制代码
`timescale 1ns / 1ps

module sale_machine(
    input clk                  ,
    input rst                  ,
    input half_dollar          ,
    input one_dollar           ,
    output reg half_out            ,
    output reg [1:0] sell                ,
    output reg fetch               
    
    );
    
    reg [2:0] status;
    localparam idle = 3'b000;
    localparam half = 3'b001;
    localparam one = 3'b010;
    localparam one_half = 3'b011;
    localparam two = 3'b100;
    
    always @(posedge clk)
    begin
            if(!rst)
            begin
                half_out    = 0;
                sell        = 0;
                fetch       = 0;
                status = 0;
            end
            else
                case(status)
                idle: 
                        if(half_dollar)   begin
                        status = half;   
                        half_out  = 0;      
                        sell        = 2'b00;
                        fetch    =1'b0;     
                        end                                       
                        else if(one_dollar) begin
                        half_out  = 0;            
                        sell        = 2'b00;      
                        fetch    =1'b0;                               
                        status = one;
                        end
                half: 
                          if(half_dollar) begin
                          status = one;
                          half_out  = 0;            
                          sell        = 2'b00;      
                          fetch    =1'b0;           
                          end                       
                                               
                          else if(one_dollar) begin
                          status =one_half;
                          half_out  = 0;            
                          sell        = 2'b00;      
                          fetch    =1'b0;           
                          end                       
                     
                one_half:
                        if(half_dollar) begin
                        status = two;
                        half_out  = 0;            
                        sell        = 2'b00;      
                        fetch    =1'b0;           
                        end                       
                                            
                        else if(one_dollar) 
                            begin
                                half_out  = 0;
                                sell        = 2'b01;   
                                fetch    =1'b1;   
                                status = idle;
                             end
                 two: 
                        if(half_dollar)
                           begin                        
                               half_out  = 1'b1;               
                               sell        = 2'b01;     
                               fetch    =1'b1;         
                               status = idle;          
                            end                        
                        else if( one_dollar )
                                begin                              
                                    half_out  = 1'b1;              
                                    sell        = 2'b10;           
                                    fetch    =1'b1;                
                                    status = idle;                 
                                 end                               
                   default: begin
                            half_out  = 0;           
                            sell        = 2'b01;     
                            fetch    =1'b1;    
                            status = idle;                                   
                            end
                     endcase       
                        
    end                     
 
endmodule

仿真文件sale_machine_tb.v:

c 复制代码
`timescale 1ns / 1ps


module sale_machine_tb;

reg       clk                ;
reg       rst                ;
reg       half_dollar        ;
reg       one_dollar         ;
wire      half_out      ;
wire      [1:0] sell    ;
wire      fetch         ;     

parameter cycle = 20;
always #(cycle/2) clk =~clk;



sale_machine u_machine(
    .clk         (clk        ) ,
    .rst         (rst        ) ,
    .half_dollar (half_dollar) ,
    .one_dollar  (one_dollar ) ,
    .half_out   (half_out    )      ,
    .sell       (sell        )      ,
    .fetch      (fetch       )      
    
    );
    
    
    initial begin
    
    clk =0; 
    rst=0;
    half_dollar  =0;
    one_dollar   =0;

    #cycle rst = 1'b1; half_dollar  = 1'b1;
   #(cycle * 10  )   $finish;
     
    end
endmodule

三、仿真图

四、原理图

相关推荐
国科安芯10 小时前
ASL706S:让 MCU 不再“失忆跑飞“的守护芯片
分布式·单片机·嵌入式硬件·安全·fpga开发·架构
明德扬13 小时前
多路MIPI摄像头如何同步聚合?一文看懂FPGA视频汇聚方案
fpga开发·音视频
松小白song18 小时前
FPGA开发三大铁律:位宽、逻辑与连接,为何仿真通过上板却挂?
fpga开发
FPGA小徐1 天前
FPGA 时钟频率计算与单位换算
fpga开发
h×31 天前
FPGA视频4分屏项目 - 系统架构详细讲解
fpga开发·音视频
秋91 天前
硬件工程师全岗位薪资全景解析(2026版):IC设计·嵌入式·板级硬件·FPGA·晶圆制造
fpga开发·制造
博览鸿蒙1 天前
FPGA 工程师到底有哪些方向?每个岗位都在干什么?一篇给你讲清楚
fpga开发
xiaojiaohuazi2 天前
国产安陆EG4S20 FPGA实现千兆以太网TCP/IP:AD7606C 8通道1 MSPS采集、SDRAM缓存与Python上位机
tcp/ip·缓存·fpga开发
尤老师FPGA2 天前
HDMI数据的接收发送实验(二十四)
fpga开发
MDYFPGA2 天前
明德扬 Kintex-7 FPGA核心板及多接口扩展底板介绍
fpga开发·fpga