-------------------------------------------------------------------------
-- Project: JCPU, a portable 8-bit RISC CPU written in VHDL
-- This file: cpu_embedded using cpu_core and rom
--
-- Copyright (C) 2007 J. Ahrensfeld
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- For questions and ideas, please contact the author at jens@jayfield.org
--
--------------------------------------------------------------------------

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.ALL;

entity tb_juart_rx is
end;

architecture behave of tb_juart_rx is

	-- Number of user data words for simulation
	constant CLK_PERIOD		: time := 10 ns;
	constant BAUD_DIV		: natural := 2;

	signal rst				: std_logic := '1';  
	signal clk				: std_logic := '1';  
	signal en_16_x_baud		: std_logic := '1';  
	signal ser_in			: std_logic := '1';  
	signal dout_vld			: std_logic;  
	signal dout				: unsigned(7 downto 0);
	signal serial_word		: unsigned(7 downto 0) := X"AA";
	
	signal baud_count		: unsigned(15 downto 0);
	signal baud_reg			: unsigned(15 downto 0) := to_unsigned(BAUD_DIV-1, baud_count'length);
	signal data_reg			: unsigned(7 downto 0);
begin

dut : entity work.juart_rx
	PORT MAP
	(
		rst					=> rst,
		clk					=> clk,
		en_16_x_baud		=> en_16_x_baud,
		dout_vld			=> dout_vld,
		dout				=> dout,
		ser_in				=> ser_in
	);


CLK_GEN: process
	begin
		wait for CLK_PERIOD/2;
		clk <= not clk;
	end process;

PROC_DATAREG: process
	begin
		wait until rising_edge(clk) and dout_vld = '1';
		data_reg <= dout;
	end process;

baud_timer:
process(clk)
begin
	if rising_edge(clk) then
		en_16_x_baud <= '0';
		if rst = '1' then
			baud_count <= (others => '0');
		elsif baud_count = baud_reg then
			baud_count <= (others => '0');
			en_16_x_baud <= '1';
		else
			baud_count <= baud_count + 1;
		end if;
	end if;
end process;

------------------------------------------------------------------------------------------

STIMULUS: process


	begin

		wait for 3*CLK_PERIOD;
		rst <= '0';
		
		serial_word	<= X"AA";
		wait for 16*baud_div*CLK_PERIOD;
		ser_in <= '0';
		wait for 16*baud_div*CLK_PERIOD;
		for i in serial_word'length-1 downto 0 loop
			ser_in <= serial_word(i);
			wait for 16*baud_div*CLK_PERIOD;
		end loop;
		ser_in <= '1';
		wait for 16*baud_div*CLK_PERIOD;
		serial_word	<= X"55";
		ser_in <= '0';
		wait for 16*baud_div*CLK_PERIOD;
		
		for i in serial_word'length-1 downto 0 loop
			ser_in <= serial_word(i);
			wait for 16*baud_div*CLK_PERIOD;
		end loop;
		ser_in <= '1';
		wait; 
	
	end process;
	
end architecture behave;
