⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vending_machine.v

📁 RTL in Verilog (Vending Machine)
💻 V
字号:
//-----------------------------------------------------------------------------
//
// Title       : vending_machine
// Design      : vending_machine
// Author      : 茄己痹
// Company     : POSTECH
//
//-----------------------------------------------------------------------------
//
// File        : vending_machine.v
// Generated   : Sun Apr 12 23:53:24 2009
// From        : interface description file
// By          : Itf2Vhdl ver. 1.21
//
//-----------------------------------------------------------------------------
//
// Description : 
//
//-----------------------------------------------------------------------------
`timescale 1 ns / 1 ns

//{{ Section below this comment is automatically maintained
//   and may be overwritten
//{module {vending_machine}}
module vending_machine ( return_thousand ,return_f_hundred ,return_hundred ,current_total_4 ,current_total_3 ,current_total_2 ,current_total_1 ,current_total_0 ,sufficient_item_4 ,sufficient_item_3 ,sufficient_item_2 ,sufficient_item_1 ,output_item ,clk ,return_trigger ,sel_item_4 ,sel_item_3 ,sel_item_2 ,sel_item_1 ,input_thousand ,input_f_hundred ,input_hundred, neg_reset );

output  return_thousand ;
//wire  return_thousand ;
output  return_f_hundred ;
//wire  return_f_hundred ;
output  return_hundred ;
//wire  return_hundred ;
output [3:0] current_total_4 ;
//wire [3:0] current_total_4 ;
output [3:0] current_total_3 ;
//wire [3:0] current_total_3 ;
output [3:0] current_total_2 ;
//wire [3:0] current_total_2 ;
output [3:0] current_total_1 ;
//wire [3:0] current_total_1 ;
output [3:0] current_total_0 ;
//wire [3:0] current_total_0 ;
output  sufficient_item_4 ;
//wire  sufficient_item_4 ;
output  sufficient_item_3 ;
//wire  sufficient_item_3 ;
output  sufficient_item_2 ;
//wire  sufficient_item_2 ;
output sufficient_item_1 ;
//wire sufficient_item_1 ;
output  output_item ;
//wire  output_item ;
	
input  clk ;
wire  clk ;
input  return_trigger ;
wire  return_trigger ;
input  sel_item_4 ;
wire  sel_item_4 ;
input  sel_item_3 ;
wire  sel_item_3 ;
input  sel_item_2 ;
wire  sel_item_2 ;
input  sel_item_1 ;
wire  sel_item_1 ;
input  input_thousand ;
wire  input_thousand ;
input  input_f_hundred ;
wire  input_f_hundred ;
input  input_hundred ;
wire  input_hundred ;
input  neg_reset;
wire  neg_reset;

// Definition of State
`define S0 3'b000  // Idle
`define	S1 3'b001 // Count Coins
`define S2 3'b010	 // Give_Change
`define S3 3'b011 //  Select_Items
`define S4 3'b100 // Dispense

// Price of Items
`define price_item_1 16'd500
`define price_item_2 16'd600
`define price_item_3 16'd700
`define price_item_4 16'd800

reg [2:0] state;		  
reg [2:0] next_state;
reg [31:0] qt_coin;
reg sufficient_item_1 ;
reg sufficient_item_2 ;
reg sufficient_item_3 ;
reg sufficient_item_4 ;
reg output_item;
reg return_thousand;
reg return_f_hundred;
reg return_hundred;
reg [31:0] clk_count;
wire [3:0] current_total_0 ;
wire [3:0] current_total_1 ;
wire [3:0] current_total_2 ;
wire [3:0] current_total_3 ;
wire [3:0] current_total_4 ;


integer2bcd integer2bcd (qt_coin, current_total_0, current_total_1, current_total_2, current_total_3, current_total_4);

// -- Enter your statements here -- //			
// Combinatorial
always @ (return_trigger or sel_item_4 or sel_item_3 or sel_item_2 or sel_item_1 or input_thousand or input_f_hundred or input_hundred or state)
begin : define_next_state
	case (state)
		`S0:	// Idle State
			begin: Idle
				if (input_thousand | input_f_hundred | input_hundred) next_state <= `S1;
				else next_state <= `S0;	
			end				
		`S1:	// Count Coins	 
			begin: Count_Coins
				if (input_hundred) qt_coin = qt_coin + 16'd100;
				if (input_f_hundred) qt_coin = qt_coin + 16'd500;
				if (input_thousand) qt_coin = qt_coin + 16'd1000;	
				if (qt_coin >= `price_item_1) sufficient_item_1 = 1'b1; else sufficient_item_1 = 1'b0;
				if (qt_coin >= `price_item_2) sufficient_item_2 = 1'b1;	 else sufficient_item_2 = 1'b0;
				if (qt_coin >= `price_item_3) sufficient_item_3 = 1'b1;	 else sufficient_item_3 = 1'b0;
				if (qt_coin >= `price_item_4) sufficient_item_4 = 1'b1;	 else sufficient_item_4 = 1'b0;
				if (sufficient_item_1 | sufficient_item_2 | sufficient_item_3 | sufficient_item_4) next_state <= `S3;
				else next_state <= `S1;
				if (return_trigger) next_state <= `S2;
				if (clk_count == 4'b1000)
					begin
						clk_count = 0;
						next_state <= `S2;
					end
			end
		`S2:	  // Give_Change
			begin: Give_Change
				output_item = 0;
				if (qt_coin / 16'd1000 > 0)
					begin
						qt_coin = qt_coin - 16'd1000;
						return_thousand = 1;
						return_f_hundred = 0;
						return_hundred = 0;
					end	
				else if(qt_coin / 16'd500 > 0)
					begin
						qt_coin = qt_coin - 16'd500;
						return_thousand = 0;
						return_f_hundred = 1;
						return_hundred = 0;
					end
				else if(qt_coin / 16'd100 > 0)
					begin
						qt_coin = qt_coin - 16'd100;
						return_thousand = 0;
						return_f_hundred = 0;
						return_hundred = 1;
					end
				else
					begin
						return_thousand = 0;
						return_f_hundred = 0;
						return_hundred = 0;
						next_state <= `S0;
					end
			end
		`S3:	// Select Items
			begin: Select_Items	
				if (sel_item_1)
					begin
						if (sufficient_item_1)
							begin
								qt_coin = qt_coin - `price_item_1;
								output_item = 1'b1;
								next_state <= `S4;
							end
						else 	next_state <= `S3;
					end
				else if (sel_item_2)
					begin
						if (sufficient_item_2)
							begin
								qt_coin = qt_coin - `price_item_2;
								output_item = 1'b1;
								next_state <= `S4;
							end
						else next_state <= `S3;
					end		
				else if (sel_item_3)
					begin
						if (sufficient_item_3)
							begin
								qt_coin = qt_coin - `price_item_3;
								output_item = 1'b1;
								next_state <= `S4;
							end
						else next_state <= `S3;
					end		
				else if (sel_item_4)
					begin
						if (sufficient_item_3)
							begin
								qt_coin = qt_coin - `price_item_4;
								output_item = 1'b1;
								next_state <= `S4;
							end
						else next_state <= `S3;
					end		
				else 	next_state <= `S3;
				if (return_trigger) next_state <= `S2;	
			end	
		`S4: // Dispense	
			begin: Dispense
				output_item = 1'b1;
				next_state <= `S2; 
			end
	endcase
end

// Sequential
always @ (posedge clk or posedge neg_reset)
begin : curr
	if (neg_reset)
		begin 
			qt_coin = 0; 
			clk_count <= 0;
			state <= `S0;
		end
	else
		begin
			state <= next_state;
			clk_count <= clk_count + 1;
		end
end		

endmodule

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -