async_8n1_tx.vhd 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. entity async_8n1_tx is
  5. port
  6. (
  7. clk : in std_logic;
  8. rst : in std_logic;
  9. en : in std_logic;
  10. wr : in std_logic;
  11. data_in : in unsigned (7 downto 0);
  12. rdy : out std_logic;
  13. data_out : out std_logic
  14. );
  15. end entity;
  16. architecture beh of async_8n1_tx is
  17. component generic_counter is
  18. generic (
  19. counter_bits : integer := 4
  20. );
  21. port(
  22. clk : in std_logic;
  23. rst : in std_logic;
  24. en : in std_logic;
  25. cnt : out unsigned (counter_bits -1 downto 0)
  26. );
  27. end component;
  28. type t_tx_state is (idle, send_start, send_data, send_stop);
  29. signal i_data_out : std_logic;
  30. signal i_rdy : std_logic;
  31. signal i_data_reg : unsigned(7 downto 0);
  32. signal i_current_state : t_tx_state;
  33. signal i_next_state : t_tx_state;
  34. signal i_cnt : unsigned(3 downto 0);
  35. signal i_clear_counter : std_logic;
  36. signal tmp_current_state : integer;
  37. signal tmp_next_state : integer;
  38. begin
  39. tmp_current_state <= t_tx_state'pos(i_current_state);
  40. tmp_next_state <= t_tx_state'pos(i_next_state);
  41. data_out <= i_data_out;
  42. rdy <= i_rdy;
  43. i_counter : generic_counter
  44. port map(
  45. clk => clk,
  46. rst => i_clear_counter,
  47. en => en,
  48. cnt => i_cnt);
  49. -- Concurrent process, determine output
  50. process(i_current_state, i_data_reg(0)) is
  51. begin
  52. case i_current_state is
  53. when idle =>
  54. i_data_out <= '1';
  55. i_rdy <= '1';
  56. i_clear_counter <= '1';
  57. when send_start =>
  58. i_data_out <= '0';
  59. i_rdy <= '0';
  60. i_clear_counter <= '1';
  61. when send_data =>
  62. i_data_out <= i_data_reg(0);
  63. i_rdy <= '0';
  64. i_clear_counter <= '0';
  65. when send_stop =>
  66. i_data_out <= '1';
  67. i_rdy <= '1';
  68. i_clear_counter <= '1';
  69. end case;
  70. end process;
  71. -- Concurrent process, determine next state
  72. process(i_current_state, wr, i_cnt) is
  73. begin
  74. case i_current_state is
  75. when idle =>
  76. if wr = '1' then
  77. i_next_state <= send_start;
  78. else
  79. i_next_state <= i_current_state;
  80. end if;
  81. when send_start =>
  82. i_next_state <= send_data;
  83. when send_data =>
  84. if i_cnt = to_unsigned(7, 4) then
  85. i_next_state <= send_stop;
  86. else
  87. i_next_state <= i_current_state;
  88. end if;
  89. when send_stop =>
  90. if wr = '1' then
  91. i_next_state <= send_start;
  92. else
  93. i_next_state <= idle;
  94. end if;
  95. end case;
  96. end process;
  97. -- Shift register
  98. process(clk) is
  99. begin
  100. if rising_edge(clk) then
  101. if rst = '1' then
  102. i_data_reg <= (others => '0');
  103. elsif en = '1' and i_current_state = send_data then
  104. i_data_reg <= '1' & i_data_reg(7 downto 1);
  105. elsif en = '1' and i_current_state = idle and wr = '1' then
  106. i_data_reg <= data_in;
  107. else
  108. i_data_reg <= i_data_reg;
  109. end if;
  110. end if;
  111. end process;
  112. -- State machine
  113. process(clk) is
  114. begin
  115. if rising_edge(clk) then
  116. if rst = '1' then
  117. i_current_state <= idle;
  118. elsif en = '1' then
  119. i_current_state <= i_next_state;
  120. end if;
  121. end if;
  122. end process;
  123. end architecture;