score_keeper.vhd

simulation results
------------------------------------------------------------------
-- Score Keeper							
-- File Name:	score_keeper.vhd							--
-- Author	: 	Gabriel Chereches						--
-- Function	: 	This Block Keeps track of all the scores for				-- 
--            	the Blackjack.							--
-- (The features are subject to change as the system evolves)			--
------------------------------------------------------------------

-- IEEE Library
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE IEEE.std_logic_unsigned.all;

ENTITY score_keeper IS
-- Generic parameters
GENERIC (n	:INTEGER := 2);	-- Number of players
PORT(
	Clk		:IN STD_LOGIC;	-- Global Clk
	Reset		:IN STD_LOGIC;	-- Global Reset
	
-- from Card Keeper 
	Card		:IN STD_LOGIC_VECTOR (3 downto 0); -- Card Value
	
-- form controller
	PL_Accept		:IN STD_LOGIC_VECTOR (n-1 downto 0);--output form controller
	HP_Accept		:IN STD_LOGIC;
	
-- from the Player modules
	PL_Stand		:IN STD_LOGIC_VECTOR (n-1 downto 0); -- Player Stand signals
	PL_Ace		:IN STD_LOGIC_VECTOR(n-1 downto 0);-- when '1' Card = 11; when '0' Card = 1
	PL1_Bet		:IN STD_LOGIC_VECTOR (7 downto 0);-- Number of chips bet by player1
	PL2_Bet		:IN STD_LOGIC_VECTOR (7 downto 0);-- Number of chips bet by player2
	PL1_Chips		:OUT STD_LOGIC_VECTOR (7 downto 0);-- Number of chips won by player1
	PL2_Chips		:OUT STD_LOGIC_VECTOR (7 downto 0);-- Number of chips won by player2
--	PL_DQ		:OUT STD_LOGIC_VECTOR (3 downto 0);-- Player Disqualified

-- Inputs from House_Player module
	HP_Stand		:IN STD_LOGIC;	-- House Player Stand
	HP_Ace		:IN STD_LOGIC; -- when '1' Card = 11; when '0' Card = 1

-- Temporary - for display on waveform
	Bet1		:BUFFER STD_LOGIC_VECTOR (7 downto 0);--signal
	Bet2		:BUFFER STD_LOGIC_VECTOR (7 downto 0);--signal
	PL1_Sum		:BUFFER STD_LOGIC_VECTOR (4 downto 0);--signal
	PL2_Sum		:BUFFER STD_LOGIC_VECTOR (4 downto 0);--signal
	HP_Sum		:BUFFER STD_LOGIC_VECTOR (4 downto 0);--signal
	CardValue		:BUFFER STD_LOGIC_VECTOR (3 downto 0);--signal
	Accept		:BUFFER STD_LOGIC_VECTOR(n-1 downto 0);--signal
	HP_Stop		:BUFFER STD_LOGIC;--signal
	PL_Stop		:BUFFER STD_LOGIC_VECTOR(n-1 downto 0);--signal
	Push		:BUFFER STD_LOGIC_VECTOR(n-1 downto 0);--signal
	Win		:BUFFER STD_LOGIC_VECTOR(n-1 downto 0);--signal
	Lose		:BUFFER STD_LOGIC_VECTOR(n-1 downto 0)--signal

	);

END score_keeper;

ARCHITECTURE behavior OF score_keeper IS

signal	LoadCard		: STD_LOGIC_VECTOR (3 downto 0);
signal	HP_Greater		: STD_LOGIC;
signal	PL_Greater		: STD_LOGIC_VECTOR(n-1 downto 0);
signal	HP_Equal		: STD_LOGIC;--signal
signal	PL_Equal		: STD_LOGIC_VECTOR(n-1 downto 0);
signal	HP_Less		: STD_LOGIC;--signal
signal	PL_Less		: STD_LOGIC_VECTOR(n-1 downto 0);
signal	PL1_Total		: STD_LOGIC_VECTOR (7 downto 0);
signal	PL2_Total		: STD_LOGIC_VECTOR (7 downto 0);

begin

--------------------------------
-- Take Bets From Each Player --
--------------------------------
-- Player 1
process (Clk, Reset, PL1_Bet)
Begin
	if (Reset = '1') then
		Bet1 <= (OTHERS => '0');
	elsif (Clk'event and Clk = '1') then
		if (PL1_Bet > X"00") then
			Bet1 <= PL1_Bet;
		else 
			Bet1 <= X"00";
		end if;
	end if;
end process;

-- Player 2
process (Clk, Reset, PL2_Bet)
Begin
	if (Reset = '1') then
		Bet2 <= (OTHERS => '0');
	elsif (Clk'event and Clk = '1') then
		if (PL2_Bet > X"00") then
			Bet2 <= PL2_Bet;
		else 
			Bet2 <= X"00";
		end if;
	end if;
end process;

------------------------------------
-- Delay by one clock cycle
------------------------------------
process (Clk, Reset, Card, PL_Accept, HP_Accept, HP_Stop)
Begin
	if (Reset = '1') then
		LoadCard <= (OTHERS => '0');
		Accept <= (OTHERS => '0');
	elsif (Clk'event and Clk = '1') then
--------------
		if (HP_Stop = '0') then
			LoadCard <= Card;
		else
			LoadCard <= "0000";
		end if;
--------------
		if (PL_Accept= "01") then
			Accept <= "01";
		elsif (PL_Accept = "10") then
			Accept <= "10";
		elsif (HP_Accept = '1') then
			Accept <= "11";
		else
			Accept <= "00";
		end if;
	end if;
end process;
--------------------------
Compare11: process (LoadCard, PL_Ace, HP_Ace)
begin
	if (LoadCard = "0001") then
		if ((HP_Ace = '1') 
			OR (PL_Ace(0) = '1') 
			OR (PL_Ace(1) = '1')) then
			CardValue <= "1011";	-- 1011(bin) = 11(dec)
		else
			CardValue <= "0001";
		end if;
	else
		CardValue <= LoadCard;
	end if;
end process;

----------------------------------
-- Sum of Cards for each Player --
----------------------------------
-- House
process (Clk, Reset, Accept, CardValue, HP_Sum)
begin
	if (Reset = '1') then
		HP_Sum <= (OTHERS => '0');
	elsif (Clk'event and Clk = '1') then
		if (Accept = "11") then
			HP_Sum <= HP_Sum + CardValue;
		else
			HP_Sum <= HP_Sum;
		end if;
	end if;
end process;

-- Player 1
process (Clk, Reset, Accept, CardValue, PL1_Sum)
begin
	if (Reset = '1') then
		PL1_Sum <= (OTHERS => '0');
	elsif (Clk'event and Clk = '1') then
		if (Accept = "01") then
			PL1_Sum <= PL1_Sum + CardValue;
		else
			PL1_Sum <= PL1_Sum;
		end if;
	end if;
end process;

-- Player 2
process (Clk, Reset, Accept, CardValue, PL2_Sum)
begin
	if (Reset = '1') then
		PL2_Sum <= (OTHERS => '0');
	elsif (Clk'event and Clk = '1') then
		if (Accept = "10") then
			PL2_Sum <= PL2_Sum + CardValue;
		else
			PL2_Sum <= PL2_Sum;
		end if;
	end if;
end process;

--------------------------
-- Compare Sum of Cards --
--------------------------
-- Greater then 21
------------------
process (HP_Sum, PL1_Sum, PL2_Sum)
begin
-- House
	if (HP_Sum > "10101") then
		HP_Greater <= '1';
	else
		HP_Greater <= '0';
	end if;
-- Player 1
	if (PL1_Sum > "10101") then
		PL_Greater(0) <= '1';
	else
		PL_Greater(0) <= '0';
	end if;
-- Player 2
	if (PL2_Sum > "10101") then
		PL_Greater(1) <= '1';
	else
		PL_Greater(1) <= '0';
	end if;
end process;

-- Equal to 21
--------------
process (HP_Sum, PL1_Sum, PL2_Sum)
begin
-- House
	if (HP_Sum = "10101") then
		HP_Equal <= '1';
	else
		HP_Equal <= '0';
	end if;
-- Player 1
	if (PL1_Sum = "10101") then
		PL_Equal(0) <= '1';
	else
		PL_Equal(0) <= '0';
	end if;
-- Player 2
	if (PL2_Sum = "10101") then
		PL_Equal(1) <= '1';
	else
		PL_Equal(1) <= '0';
	end if;
end process;

-- Less than 21
---------------
process (HP_Sum, PL1_Sum, PL2_Sum, HP_Stand, PL_Stand)
begin
-- House
	if (HP_Stand = '1') then
		if (HP_Sum < "10101") then
			HP_Less <= '1';
		else
			HP_Less <= '0';
		end if;
	else
		HP_Less <= '0';
	end if;
-- Player 1
	if (PL_Stand(0) = '1') then
		if (PL1_Sum < "10101") then
			PL_Less(0) <= '1';
		else
			PL_Less(0) <= '0';
		end if;
	else
		PL_Less(0) <= '0';
	end if;
-- Player 2
	if (PL_Stand(1) = '1') then
		if (PL2_Sum < "10101") then
			PL_Less(1) <= '1';
		else
			PL_Less(1) <= '0';
		end if;
	else
		PL_Less(1) <= '0';
	end if;
end process;

-------------------------------
-- Stop From Accepting Cards --
-------------------------------
-- House
--------
process (HP_Greater, HP_Equal, HP_Less)
begin
	if ((HP_Greater = '1') OR (HP_Equal = '1') OR (HP_Less = '1')) then
		HP_Stop <= '1';
	else
		HP_Stop <= '0';
	end if;
end process;

-- Player 1
-----------
process (PL_Greater(0), PL_Equal(0), PL_Less(0))
begin
	if ((PL_Greater(0) = '1') OR (PL_Equal(0) = '1') OR (PL_Less(0) = '1')) then
		PL_Stop(0) <= '1';
	else
		PL_Stop(0) <= '0';
	end if;
end process;

-- Player 2
-----------
process (PL_Greater(1), PL_Equal(1), PL_Less(1))
begin
	if ((PL_Greater(1) = '1') OR (PL_Equal(1) = '1') OR (PL_Less(1) = '1')) then
		PL_Stop(1) <= '1';
	else
		PL_Stop(1) <= '0';
	end if;
end process;

------------------------------------------
-- Find Win, Push, Lose For Each Player --
------------------------------------------
process (HP_Stop, HP_Greater, PL_Greater)
begin
	if (HP_Stop = '1') then
	-- Case when House Greater then 21
		if (HP_Greater = '1') then
			if (PL_Greater = "11") then
					Push <= "11";
			elsif (PL_Greater = "01") then
					Push <= "01";
					Win <= "10";
			elsif (PL_Greater = "10") then
					Push <= "10";
					Win <= "01";
			else
				Win <= "11";
			end if;
		else--if (HP_Greater = '0') then
			if (PL_Greater = "11") then
					Lose <= "11";
			elsif (PL_Greater = "01") then
					Lose <= "01";
			elsif (PL_Greater = "10") then
					Lose <= "10";
			end if;
		end if;
	-- Case when House Equals 21
		if (HP_Equal = '1') then
			if (PL_Equal = "11") then
					Push <= "11";
			elsif (PL_Equal = "01") then
					Push <= "01";
					Lose <= "10";
			elsif (PL_Equal = "10") then
					Push <= "10";
					Lose <= "01";
			else
				Lose <= "11";
			end if;
		else--if (HP_Equal = '0') then
			if (PL_Equal = "11") then
					Win <= "11";
			elsif (PL_Equal = "01") then
					Win <= "01";
			elsif (PL_Equal = "10") then
					Win <= "10";
			end if;
		end if;

	else
		Push <= (OTHERS => '0');
		Win <= (OTHERS => '0');
		Lose <= (OTHERS => '0');
	end if;
end process;

-------------------
-- Handling Chip --
-------------------
process (Win, Push, Lose)
begin
-- Player Wins against House
	if (Win(0) = '1') then
		PL1_Total <= PL1_Total + Bet1 + Bet1;
		PL1_Chips <= Bet1 + Bet1;
	end if;
	if (Win(1) = '1') then
		PL2_Total <= PL2_Total + Bet2 + Bet2;
		PL2_Chips <= Bet2 + Bet2;
	end if;
-- Player Ties the House
	if (Push(0) = '1') then
		PL1_Total <= PL1_Total + Bet1;
		PL1_Chips <= Bet1;
	end if;
	if (Push(1) = '1') then
		PL2_Total <= PL2_Total + Bet2;
		PL2_Chips <= Bet2;
	end if;
-- Player Losses against House
	if (Lose(0) = '1') then
		PL1_Total <= PL1_Total;
		PL1_Chips <= "00000000";
	end if;
	if (Lose(1) = '1') then
		PL2_Total <= PL2_Total;
		PL2_Chips <= "00000000";
	end if;
end process;

END behavior;