Untitled

mail@pastecode.io avatar
unknown
verilog
7 months ago
3.9 kB
2
Indexable
Never
always @(posedge clk, posedge rst) begin
	if(rst) begin
		items <= 8'd9;
		products <= 0;
	end
	else begin
		products <= next_products;
		items <= next_items;
	end
end

always @(posedge clk, posedge rst) begin
	if(rst) begin
		price <= 8'd10;
	end
	else begin
		if(state == SET)
			price <= next_price;
		else
			price <= price;
	end
end

always @(posedge clk, posedge rst) begin
	if(rst) begin
		money <= 8'd0;
	end
	else begin
		money <= next_money;
	end
end

always @(*) begin
	if(rst) begin
		next_items = 8'd9;
		next_price = 8'd10;
	end
	else begin
		if(state == SET) begin
			if(set_mode == 0) begin
				next_price = price;
				if(key_valid == 1'b1 && key_down[last_change] == 1'b1 && isPressed == 1'b0)
					if(key_num <= 4'd9 && key_num >= 0)
						next_items = key_num;
					else
						next_items = items;
				else
					next_items = items;
			end
			else if(set_mode == 1) begin
				next_items = items;
				if(key_valid == 1'b1 && key_down[last_change] == 1'b1 && isPressed == 1'b0) begin
					if(key_num <= 4'd9 && key_num >= 0)
						next_price = (((price % 10) * 10) + key_num);
					else
						next_price = price;
					end
				else
					next_price = price;
			end
			else if(key_valid == 1'b1 && key_down[last_change] == 1'b1 && last_change == ENTER) begin
				next_price = price;
				if(money > (items * price))
					next_items = 0;
				else
					next_items = items - next_products;
			end
			else begin
				next_price = price;
				next_items = items;
			end
		end
		else begin
			next_price = price;
			next_items = items;
		end
	end
end

// items amount and price in SET state
always @(*) begin
	if(rst) begin
		next_products = 0;
		next_money = 0;
	end
	else begin
		if(state == PAYMENT) begin
			if(key_valid == 1'b1 && key_down[last_change] == 1'b1 && isPressed == 1'b0) begin
				if(key_num == 0)
					next_money = 0;
				else if(key_num == 1)
					next_money = ((money + 1) > 99) ? 99 : money + 1;
				else if(key_num == 2)
					next_money = ((money + 5) > 99) ? 99 : money + 5;
				else if(key_num == 3)
					next_money = ((money + 10) > 99) ? 99 : money + 10;
				else if(key_num == 4)
					next_money = ((money + 50) > 99) ? 99 : money + 50;
				else if(last_change == ENTER) begin // go to buy state
					if(money > (items * price)) begin
						next_products = items;
						next_money = money - price * items;
					end
					else begin
						next_products = money / price;
						next_money = money - price * next_products;
					end
				end
			end
			else begin
				next_money = money;
				next_products = products;
			end
		end
		else begin
			next_money = money;
			next_products = products;
		end
	end
end

// state transition
always @(posedge clk, posedge rst) begin
	if(rst) begin
		state <= IDLE;
		LED <= 16'b0;
		total_price <= 0;
	end
	else begin
		LED <= next_led;
		state <= next_state;
		total_price <= (products * price);
	end
end

always @(*) begin
	if(rst)
		next_state = IDLE;
	else begin
		case (state)
		IDLE: begin
			if(btnL_op == 1'b1)
				next_state = SET;
			else if(btnR_op == 1'b1)
				next_state = PAYMENT;
			else
				next_state = IDLE;
		end
		SET: begin
			if(key_valid == 1'b1 && key_down[last_change] == 1'b1 && last_change == ENTER)
				next_state = IDLE;
			else
				next_state = SET;
		end
		PAYMENT: begin
			if(key_valid == 1'b1 && key_down[last_change] == 1'b1 && last_change == ENTER) begin
				if(money >= price && items > 0)
					next_state = BUY;
				else
					next_state = CHANGE;
			end
			else
				next_state = PAYMENT;
		end
		BUY: begin
			if(seconds >= 6)
				next_state = CHANGE;
			else
				next_state = BUY;
		end
		CHANGE: begin
			if(rounds >= 3)
				next_state = IDLE;
			else 
				next_state = CHANGE;
		end
		default:
			next_state = IDLE;
		endcase
	end
end