从零开始动手做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

三、仿真图

四、原理图

相关推荐
1560820721918 小时前
关于国微SQM7VX690T和复旦微JFM7VX690T调试
fpga开发
minglie11 天前
简易寄存器接口SMMR---正交编码器
fpga开发
神电测控2 天前
新品发布:32通道50MS/s同步并行14位采集模块PG-FMC9249(支持任意FPGA/ZYNQ,兼容LabVIEW FPGA软件开发)
fpga开发·labview·ni·神电测控·王电令·ad9249·32通道
Drgfd2 天前
半导体国产替代进入“深水区”:决战设备、材料、IP三大高地
网络·fpga开发
国产HT1621B2 天前
YL1650 LED驱动芯片详解:从原理到实战应用(附51单片机驱动代码)
stm32·单片机·嵌入式硬件·mcu·fpga开发·lcd驱动
szxinmai主板定制专家2 天前
驱控一体新范式:RK3576+FPGA+CODESYS工业实时AI控制架构全解
人工智能·嵌入式硬件·计算机视觉·fpga开发·架构·工业ai·驱控一体
国科安芯2 天前
ASC0101S自动双向电平转换原理深剖
单片机·嵌入式硬件·fpga开发·云计算·信息与通信
落chen2 天前
7 Series FPGAs Clocking Resources
fpga开发
落chen3 天前
基于FPGA的IIC协议通信-强化篇
fpga开发
梦开始的地方 �3 天前
实例化和句柄
fpga开发