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

三、仿真图

四、原理图

相关推荐
坏孩子的诺亚方舟5 天前
FPGA系统架构设计实践15_高云Arora V系列时钟体系
fpga开发·系统架构
FPGA小徐5 天前
入门 CNN 结构全解析|从流程图理论到 FPGA Verilog 硬件实现(含习题带讲解)
fpga开发
FPGA小徐5 天前
FPGA 数字信号处理:并行 FIR 与串行滤波器设计原理、对比与完整 Verilog 实现
fpga开发
Saniffer_SH6 天前
【高清视频】Gen6 服务器还没到,Gen6 SSD 怎么测?Emily 现场演示三种测试环境
人工智能·驱动开发·测试工具·缓存·fpga开发·计算机外设·压力测试
zlinear数据采集卡6 天前
双核架构深度解析:ARM+FPGA如何让数据采集卡实现500Ksps高性能?
arm开发·fpga开发·架构
9527华安6 天前
FPGA实现GTH Transceivers Wizard传输2路视频,基于aurora 8b10b编解码架构,提供4套工程源码和技术支持
fpga开发·gth·aurora 8b10b·transceivers
FPGA小徐7 天前
FPGA 数字信号处理(二):并行 FIR 滤波器的 Verilog 全流程设计与实现
fpga开发
国科安芯7 天前
基于AS32S601ZIT2型抗辐照MCU的商业航天卫星姿态确定与控制系统研究
单片机·嵌入式硬件·安全·fpga开发·架构·risc-v
ALINX技术博客7 天前
【黑金云课堂】FPGA技术教程FPGA基础:I2C 总线通信技术
fpga开发·i2c
Hello-FPGA7 天前
Xilinx KU040 FPGA Camera Link 图像采集
c++·fpga开发