-- -----------------------------------------------------------------------------
--  RXD2CHAR - RS232 TxD to ASCII character converter (RS232 receiver)        -- 
-- -----------------------------------------------------------------------------
--
--  File        : 'txd2irda.vhd'
--  Author      :  Lars Larsson 
-- 
--  Date        :  February 16, 1999
--
--  Description :  This converts a RS232 RxD signal (8n1) into an ASCII character 
--                 and prints it together with the character's code on the console. 
--                 The design is a behavioral RS232 receiver for simulation. 
--
-- -----------------------------------------------------------------------------
--
-- Copyright (C) 1999 Lars Larsson, Dept. of Computer Science
--                                  University of Hamburg
--                                  Vogt-Koelln-Str. 30
--                                  D - 22041 Hamburg, Germany
--                                  larsson@informatik.uni-hamburg.de
--                                  http://tech-www.informatik.uni-hamburg.de/~larsson
--
-- 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 2 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, write to the Free Software Foundation, Inc.,
-- 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
--
-- -----------------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity rxd2char is
  port(         
               t :  in time;                           -- T_BAUD = 1/(baud rate)
       rs232_rxd :  in std_ulogic;                     -- RS232 RxD signal input 
             bin : out std_ulogic_vector (7 downto 0); -- binary code 
             chr : out character;                      -- character
          strobe : out std_ulogic                      -- strobe signal 
       );
end rxd2char;

architecture simulate of rxd2char is

  type char_indexed_by_integer is array (integer range 0 to 127) of character;
  constant integer_to_char : char_indexed_by_integer :=
    (
        NUL, SOH, STX, ETX, EOT, ENQ, ACK, BEL,
        BS,  HT,  LF,  VT,  FF,  CR,  SO,  SI,
        DLE, DC1, DC2, DC3, DC4, NAK, SYN, ETB,
        CAN, EM,  SUB, ESC, FSP, GSP, RSP, USP,
        ' ', '!', '"', '#', '$', '%', '&', ''', 
        '(', ')', '*', '+', ',', '-', '.', '/', 
        '0', '1', '2', '3', '4', '5', '6', '7', 
        '8', '9', ':', ';', '<', '=', '>', '?', 
        '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 
        'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 
        'X', 'Y', 'Z', '[', '\', ']', '^', '_', 
        '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 
        'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 
        'x', 'y', 'z', '{', '|', '}', '`', DEL
    );

   signal irda_s : std_ulogic;

   signal baud_clk_s  : std_ulogic; 
   signal strobe_s    : std_ulogic := '0';
   signal shift_reg_s : std_ulogic_vector (9 downto 0) := (others=>'1');
   signal byte_s      : std_ulogic_vector (7 downto 0) := (others=>'0');

   signal char_s : character;

   function v2s( x : std_ulogic_vector ) return string is
     variable s : string( x'left+1 downto 1 ) := (others => '.');
     begin
         for i in x'range loop
            if (    x(i) = '0' ) then s(i+1) := '0';
            elsif ( x(i) = '1' ) then s(i+1) := '1';
            elsif ( x(i) = 'Z' ) then s(i+1) := 'Z';
            elsif ( x(i) = 'U' ) then s(i+1) := 'U';
            elsif ( x(i) = 'X' ) then s(i+1) := 'X';
            else                      s(i+1) := '*';
            end if;
         end loop;
       return s;
       end;
    
   begin

       baud_p : process
                begin
                   wait on rs232_rxd for 1 ns;
                   baud_clk_s <= '0'; wait for t/2; 
                   baud_clk_s <= '1'; wait for t/2;
                end process;

      shift_p : process ( baud_clk_s, rs232_rxd, shift_reg_s )
                begin
                  if (baud_clk_s'event and baud_clk_s='1') then
                      if (strobe_s='1') then
                          byte_s <= shift_reg_s(8 downto 1);
                          shift_reg_s <= rs232_rxd & "111111111";
                      else
                          shift_reg_s <= rs232_rxd & shift_reg_s(9 downto 1);
                      end if;
                  end if;
                end process;

       save_p : process ( shift_reg_s )
                begin
                  if (shift_reg_s(9)='1' and shift_reg_s(0)='0') then
                      strobe_s <= '1';
                  else
                      strobe_s <= '0';
                  end if;
                end process;

       bin <= byte_s; 
       chr <= char_s;

       strobe <= strobe_s after t;

       chk_p : process (byte_s)
               begin
               -- char_s <= integer_to_char(conv_integer(To_StdLogicVector(byte_s)));
                  if (byte_s(7)='0') then
                      char_s <= character'val(conv_integer(To_StdLogicVector(byte_s)));
                  else
                      char_s <= '*';
                  end if;
               end process;

       assert (rising_edge(strobe_s))
         report "RS232 RECEIVER SIMULATOR (rxd2char) : (" & v2s(byte_s) & ")_2 = '" & char_s & "'   " 
         severity note;

    end simulate;

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

<div align="center"><br /><script type="text/javascript"><!--
google_ad_client = "pub-7293844627074885";
//468x60, Created at 07. 11. 25
google_ad_slot = "8619794253";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />&nbsp;</div>