mc8051_tmrctr_rtl.vhd
上传用户:sztwq510
上传日期:2007-04-20
资源大小:209k
文件大小:29k
源码类别:

VHDL/FPGA/Verilog

开发平台:

Matlab

  1. -------------------------------------------------------------------------------
  2. --                                                                           --
  3. --          X       X   XXXXXX    XXXXXX    XXXXXX    XXXXXX      X          --
  4. --          XX     XX  X      X  X      X  X      X  X           XX          --
  5. --          X X   X X  X         X      X  X      X  X          X X          --
  6. --          X  X X  X  X         X      X  X      X  X         X  X          --
  7. --          X   X   X  X          XXXXXX   X      X   XXXXXX      X          --
  8. --          X       X  X         X      X  X      X         X     X          --
  9. --          X       X  X         X      X  X      X         X     X          --
  10. --          X       X  X      X  X      X  X      X         X     X          --
  11. --          X       X   XXXXXX    XXXXXX    XXXXXX    XXXXXX      X          --
  12. --                                                                           --
  13. --                                                                           --
  14. --                       O R E G A N O   S Y S T E M S                       --
  15. --                                                                           --
  16. --                            Design & Consulting                            --
  17. --                                                                           --
  18. -------------------------------------------------------------------------------
  19. --                                                                           --
  20. --         Web:           http://www.oregano.at/                             --
  21. --                                                                           --
  22. --         Contact:       mc8051@oregano.at                                  --
  23. --                                                                           --
  24. -------------------------------------------------------------------------------
  25. --                                                                           --
  26. --  MC8051 - VHDL 8051 Microcontroller IP Core                               --
  27. --  Copyright (C) 2001 OREGANO SYSTEMS                                       --
  28. --                                                                           --
  29. --  This library is free software; you can redistribute it and/or            --
  30. --  modify it under the terms of the GNU Lesser General Public               --
  31. --  License as published by the Free Software Foundation; either             --
  32. --  version 2.1 of the License, or (at your option) any later version.       --
  33. --                                                                           --
  34. --  This library is distributed in the hope that it will be useful,          --
  35. --  but WITHOUT ANY WARRANTY; without even the implied warranty of           --
  36. --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        --
  37. --  Lesser General Public License for more details.                          --
  38. --                                                                           --
  39. --  Full details of the license can be found in the file LGPL.TXT.           --
  40. --                                                                           --
  41. --  You should have received a copy of the GNU Lesser General Public         --
  42. --  License along with this library; if not, write to the Free Software      --
  43. --  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  --
  44. --                                                                           --
  45. -------------------------------------------------------------------------------
  46. --
  47. --
  48. --         Author:                 Roland H鰈ler
  49. --
  50. --         Filename:               mc8051_tmrctr_rtl.vhd
  51. --
  52. --         Date of Creation:       Mon Aug  9 12:14:48 1999
  53. --
  54. --         Version:                $Revision: 1.7 $
  55. --
  56. --         Date of Latest Version: $Date: 2002/01/30 16:42:57 $
  57. --
  58. --
  59. --         Description: Timer/Counter unit of the mc8051 microcontroller.
  60. --
  61. --
  62. --
  63. --
  64. -------------------------------------------------------------------------------
  65. architecture rtl of mc8051_tmrctr is
  66.   signal s_pre_count    : unsigned(3 downto 0);  -- these two signals provide
  67.   signal s_count_enable : std_logic;             -- a clock enable signal which
  68.                                                  -- masks out every sixteenth
  69.                                                  -- rising edge of clk
  70.   
  71.   signal s_count0       : unsigned(15 downto 0); -- count for tmr/ctr0
  72.   signal s_countl0      : unsigned(7 downto 0);  -- count register for tmr/ctr0
  73.   signal s_counth0      : unsigned(7 downto 0);  -- count register for tmr/ctr0
  74.   signal s_count1       : unsigned(15 downto 0); -- count for tmr/ctr1
  75.   signal s_countl1      : unsigned(7 downto 0);  -- count register for tmr/ctr1
  76.   signal s_counth1      : unsigned(7 downto 0);  -- count register for tmr/ctr1
  77.   signal s_gate0        : std_logic;             -- gate bit for tmr/ctr 0
  78.   signal s_gate1        : std_logic;             -- gate bit for tmr/ctr 1
  79.   signal s_c_t0         : std_logic;             -- tmr/ctr 0 is timer if 0
  80.   signal s_c_t1         : std_logic;             -- tmr/ctr 1 is timer if 0
  81.   signal s_tmr_ctr0_en  : std_logic;             -- starts tmr/ctr 0 if 1
  82.   signal s_tmr_ctr1_en  : std_logic;             -- starts tmr/ctr 1 if 1
  83.   signal s_mode0        : unsigned(1 downto 0);  -- mode of tmr/ctr 0
  84.   signal s_mode1        : unsigned(1 downto 0);  -- mode of tmr/ctr 1
  85.   signal s_tf0          : std_logic;             -- overflow flag of tmr/ctr 0
  86.   signal s_tf1          : std_logic;             -- overflow flag of tmr/ctr 1
  87.   signal s_t0ff0        : std_logic;             -- flipflop for edge dedection
  88.   signal s_t0ff1        : std_logic;             -- flipflop for edge dedection
  89.   signal s_t0ff2        : std_logic;             -- flipflop for edge dedection
  90.   signal s_t1ff0        : std_logic;             -- flipflop for edge dedection
  91.   signal s_t1ff1        : std_logic;             -- flipflop for edge dedection
  92.   signal s_t1ff2        : std_logic;             -- flipflop for edge dedection
  93.   signal s_ext_edge0    : std_logic;             -- 1 if external edge dedected
  94.   signal s_ext_edge1    : std_logic;             -- 1 if external edge dedected
  95.   
  96.   
  97.   
  98. begin                 -- architecture rtl
  99.   -- The names of the following signals make the code more readable.
  100.   s_gate0 <= tmod_i(3);
  101.   s_c_t0  <= tmod_i(2);
  102.   s_mode0(1) <= tmod_i(1);
  103.   s_mode0(0) <= tmod_i(0);
  104.   
  105.   s_gate1 <= tmod_i(7);
  106.   s_c_t1  <= tmod_i(6);
  107.   s_mode1(1) <= tmod_i(5);
  108.   s_mode1(0) <= tmod_i(4);
  109.   -- These two signals start the corresponding timer/counter if they are 1.
  110.   s_tmr_ctr0_en <= tcon_tr0_i and (not(s_gate0) or int0_i);
  111.   s_tmr_ctr1_en <= tcon_tr1_i and (not(s_gate1) or int1_i);
  112.   -- The outputs of this unit are the two timer overflow flags, which are read
  113.   -- by the control unit and the two 16 bit count registers to enable read
  114.   -- access.
  115.   tf0_o <= s_tf0;
  116.   tf1_o <= s_tf1;
  117.   th0_o <= std_logic_vector(s_count0(15 downto 8));
  118.   tl0_o <= std_logic_vector(s_count0(7 downto 0));
  119.   th1_o <= std_logic_vector(s_count1(15 downto 8));
  120.   tl1_o <= std_logic_vector(s_count1(7 downto 0));
  121. -------------------------------------------------------------------------------
  122.   -- The register s_pre_count is driven with the system clock. So a
  123.   -- good enable signal (which is stable when clk has its rising edge) can be
  124.   -- derived to mask out every sixteenth pulse of clk.
  125.   s_count_enable <= '1' when s_pre_count = conv_unsigned(15,4) else '0';
  126.   
  127.   p_divide_clk: process (clk, reset)
  128.     
  129.     begin
  130.       if reset = '1' then
  131.         s_pre_count <= conv_unsigned(0,4);
  132.       else
  133.         if clk'event and clk='1' then
  134.           s_pre_count <= s_pre_count + conv_unsigned(1,1);
  135.         end if;
  136.       end if;    
  137.   end process p_divide_clk;
  138. -------------------------------------------------------------------------------
  139.   -- The two flip flops are updated every second clock period.
  140.   -- If a falling edge
  141.   -- on the port t0_i is dedected the signal s_ext_edge0 is set to 1 and with
  142.   -- the next rising edge of clk the counter0 is incremented.
  143.   -- The same function is realised for counter1.
  144.   s_ext_edge0 <= '1' when (s_t0ff1 = '0' and s_t0ff2 = '1') else '0';      
  145.   p_sample_t0: process (clk, reset)
  146.       
  147.     begin
  148.       if reset = '1' then
  149.         s_t0ff0 <= '0';
  150.         s_t0ff1 <= '0';
  151.         s_t0ff2 <= '0';
  152.       else
  153.             
  154.         if clk'event and clk = '1' then
  155.           if s_pre_count = conv_unsigned(6,3) then
  156.             if s_c_t0 = '1' then
  157.               s_t0ff0 <= t0_i;
  158.               s_t0ff1 <= s_t0ff0;
  159.               s_t0ff2 <= s_t0ff1;
  160.             end if;
  161.           end if;
  162.         end if;  
  163.       end if;    
  164.   end process p_sample_t0;
  165.       
  166.   s_ext_edge1 <= '1' when (s_t1ff1 = '0' and s_t1ff2 = '1') else '0';
  167.   p_sample_t1: process (clk, reset)
  168.       
  169.     begin
  170.       if reset = '1' then
  171.         s_t1ff0 <= '0';
  172.         s_t1ff1 <= '0';
  173.         s_t1ff2 <= '0';
  174.       else
  175.         if clk'event and clk = '1' then
  176.           if s_pre_count = conv_unsigned(6,3) then
  177.             if s_c_t1 = '1' then
  178.               s_t1ff0 <= t1_i;
  179.               s_t1ff1 <= s_t1ff0;
  180.               s_t1ff2 <= s_t1ff1;
  181.             end if;              
  182.           end if;
  183.         end if;  
  184.       end if;    
  185.   end process p_sample_t1;
  186. ------------------------------------------------------------------------------
  187. --+++++++++++++++++++++   TIMER / COUNTER 0   ++++++++++++++++++++++++++++++--
  188. ------------------------------------------------------------------------------
  189. -- This is timer/counter0. It is built around the 16 bit count register
  190. -- s_count0 and realises its four operating modes
  191. ------------------------------------------------------------------------------
  192.   s_count0(15 downto 8) <= s_counth0;
  193.   s_count0(7 downto 0) <= s_countl0;
  194.   s_count1(15 downto 8) <= s_counth1;
  195.   s_count1(7 downto 0) <= s_countl1;
  196.       
  197.   p_tmr_ctr: process (clk, reset)
  198.     
  199.   begin
  200.     if reset = '1' then                 -- perform asynchronous reset
  201.       s_countl0 <= conv_unsigned(0,8);
  202.       s_counth0 <= conv_unsigned(0,8);
  203.       s_countl1 <= conv_unsigned(0,8);
  204.       s_counth1 <= conv_unsigned(0,8);
  205.       s_tf1    <= '0';
  206.       s_tf0    <= '0';
  207.         
  208.     else
  209.         
  210.       if clk'event and clk = '1' then
  211.           
  212. -------------------------------------------------------------------------------
  213. -- operating mode 0 (13 bit timer/counter)
  214. -------------------------------------------------------------------------------
  215.       case s_mode0 is
  216.         when "00" =>
  217.         -- This section generates the timer/counter overflow flag0
  218.         if s_tmr_ctr0_en = '1' then
  219.           if s_count_enable = '1' then   
  220.             if s_c_t0 = '0' or (s_ext_edge0 = '1' and s_c_t0 = '1')  then
  221.               if s_count0 = conv_unsigned(8191,16) then
  222.                 s_tf0 <= '1';
  223.               else
  224.                 s_tf0 <= '0';
  225.               end if;
  226.             end if;
  227.           end if;
  228.         end if;
  229.         
  230.         -- This section generates the low byte register of tmr/ctr0
  231.         if wt_i = "00" and wt_en_i = '1' then
  232.           s_countl0 <= unsigned(reload_i);  
  233.         else
  234.           if s_tmr_ctr0_en = '1' then
  235.             if s_count_enable = '1' then   
  236.               if s_c_t0 = '0' then
  237.                 if s_count0 = conv_unsigned(8191,16) then
  238.                   s_countl0 <= conv_unsigned(0,8);
  239.                 else
  240.                   s_countl0 <= s_countl0 + conv_unsigned(1,1);
  241.                 end if;
  242.               else
  243.                 if s_ext_edge0 = '1' then
  244.                   if s_count0 = conv_unsigned(8191,16) then
  245.                     s_countl0 <= conv_unsigned(0,8);
  246.                   else
  247.                     s_countl0 <= s_countl0 + conv_unsigned(1,1);
  248.                   end if;
  249.                 end if;                  
  250.               end if;
  251.             end if; 
  252.           end if;
  253.         end if;
  254.         
  255.         -- This section generates the high byte register of tmr/ctr0
  256.         if wt_i = "10" and wt_en_i = '1' then
  257.           s_counth0 <= unsigned(reload_i);  
  258.         else
  259.           if s_tmr_ctr0_en = '1' then
  260.             if s_count_enable = '1' then   
  261.               if s_c_t0 = '0' then
  262.                 if s_count0 = conv_unsigned(8191,16) then
  263.                   s_counth0 <= conv_unsigned(0,8);
  264.                 else
  265.                   if s_countl0 = conv_unsigned(255,8) then
  266.                     s_counth0 <= s_counth0 + conv_unsigned(1,1);
  267.                   end if;
  268.                 end if;
  269.               else
  270.                 if s_ext_edge0 = '1' then
  271.                   if s_count0 = conv_unsigned(8191,16) then
  272.                     s_counth0 <= conv_unsigned(0,8);
  273.                   else
  274.                     if s_countl0 = conv_unsigned(255,8) then
  275.                       s_counth0 <= s_counth0 + conv_unsigned(1,1);
  276.                     end if;
  277.                   end if;
  278.                 end if;                  
  279.               end if;
  280.             end if;
  281.           end if;
  282.         end if;
  283. -------------------------------------------------------------------------------
  284. -- operating mode 1 (16 bit timer/counter)
  285. -------------------------------------------------------------------------------
  286.       when "01" =>
  287.         -- This section generates the timer/counter overflow flag0
  288.         if s_tmr_ctr0_en = '1' then
  289.           if s_count_enable = '1' then   
  290.             if s_c_t0 = '0' or (s_ext_edge0 = '1' and s_c_t0 = '1')  then
  291.               if s_count0 = conv_unsigned(65535,16) then
  292.                 s_tf0 <= '1';
  293.               else
  294.                 s_tf0 <= '0';
  295.               end if;
  296.             end if;
  297.           end if;
  298.         end if;
  299.         
  300.         -- This section generates the low byte register of tmr/ctr0
  301.         if wt_i = "00" and wt_en_i = '1' then
  302.           s_countl0 <= unsigned(reload_i);  
  303.         else
  304.           if s_tmr_ctr0_en = '1' then
  305.             if s_count_enable = '1' then   
  306.               if s_c_t0 = '0' then
  307.                 if s_count0 = conv_unsigned(65535,16) then
  308.                   s_countl0 <= conv_unsigned(0,8);
  309.                 else
  310.                   s_countl0 <= s_countl0 + conv_unsigned(1,1);
  311.                 end if;
  312.               else
  313.                 if s_ext_edge0 = '1' then
  314.                   if s_count0 = conv_unsigned(65535,16) then
  315.                     s_countl0 <= conv_unsigned(0,8);
  316.                   else
  317.                     s_countl0 <= s_countl0 + conv_unsigned(1,1);
  318.                   end if;
  319.                 end if;                  
  320.               end if;
  321.             end if; 
  322.           end if;
  323.         end if;
  324.         
  325.         -- This section generates the high byte register of tmr/ctr0
  326.         if wt_i = "10" and wt_en_i = '1' then
  327.           s_counth0 <= unsigned(reload_i);  
  328.         else
  329.           if s_tmr_ctr0_en = '1' then
  330.             if s_count_enable = '1' then   
  331.               if s_c_t0 = '0' then
  332.                 if s_count0 = conv_unsigned(65535,16) then
  333.                   s_counth0 <= conv_unsigned(0,8);
  334.                 else
  335.                   if s_countl0 = conv_unsigned(255,8) then
  336.                     s_counth0 <= s_counth0 + conv_unsigned(1,1);
  337.                   end if;
  338.                 end if;
  339.               else
  340.                 if s_ext_edge0 = '1' then
  341.                   if s_count0 = conv_unsigned(65535,16) then
  342.                     s_counth0 <= conv_unsigned(0,8);
  343.                   else
  344.                     if s_countl0 = conv_unsigned(255,8) then
  345.                       s_counth0 <= s_counth0 + conv_unsigned(1,1);
  346.                     end if;
  347.                   end if;
  348.                 end if;                  
  349.               end if;
  350.             end if;
  351.           end if;
  352.         end if;
  353. -------------------------------------------------------------------------------
  354. -- operating mode 2 (8 bit timer/counter, autoreloaded from high byte register)
  355. -------------------------------------------------------------------------------
  356.       when "10" =>
  357.                
  358.         -- This section generates the timer/counter overflow flag0
  359.         if s_tmr_ctr0_en = '1' then
  360.           if s_count_enable = '1' then   
  361.             if s_c_t0 = '0' or (s_ext_edge0 = '1' and s_c_t0 = '1')  then
  362.               if s_count0(7 downto 0) = conv_unsigned(255,16) then
  363.                 s_tf0 <= '1';
  364.               else
  365.                 s_tf0 <= '0';
  366.               end if;
  367.             end if;
  368.           end if;
  369.         end if;
  370.         
  371.         -- This section generates the low byte register of tmr/ctr0
  372.         if wt_i = "00" and wt_en_i = '1' then
  373.           s_countl0 <= unsigned(reload_i);  
  374.         else
  375.           if s_tmr_ctr0_en = '1' then
  376.             if s_count_enable = '1' then   
  377.               if s_c_t0 = '0' then
  378.                 if s_countl0 = conv_unsigned(255,8) then
  379.                   s_countl0 <= s_counth0;
  380.                 else
  381.                   s_countl0 <= s_countl0 + conv_unsigned(1,1);
  382.                 end if;
  383.               else
  384.                 if s_ext_edge0 = '1' then
  385.                   if s_countl0 = conv_unsigned(255,8) then
  386.                     s_countl0 <= s_counth0;
  387.                   else
  388.                     s_countl0 <= s_countl0 + conv_unsigned(1,1);
  389.                   end if;
  390.                 end if;                  
  391.               end if;
  392.             end if; 
  393.           end if;
  394.         end if;
  395.         
  396.         -- This section generates the high byte register of tmr/ctr0
  397.         if wt_i = "10" and wt_en_i = '1' then
  398.           s_counth0 <= unsigned(reload_i);
  399.         end if;
  400.         
  401. -------------------------------------------------------------------------------
  402. -- operating mode 3 (One 8 bit timer/counter and one 8 bit timer)
  403. -------------------------------------------------------------------------------
  404.       when "11" =>
  405.            
  406.         -- This section generates the timer/counter overflow flag0
  407.         if s_tmr_ctr0_en = '1' then
  408.           if s_count_enable = '1' then   
  409.             if s_c_t0 = '0' or (s_ext_edge0 = '1' and s_c_t0 = '1')  then
  410.               if s_count0(7 downto 0) = conv_unsigned(255,16) then
  411.                 s_tf0 <= '1';
  412.               else
  413.                 s_tf0 <= '0';
  414.               end if;
  415.             end if;
  416.           end if;
  417.         end if;
  418.         
  419.         -- This section generates the low byte register of tmr/ctr0
  420.         if wt_i = "00" and wt_en_i = '1' then
  421.           s_countl0 <= unsigned(reload_i);  
  422.         else
  423.           if s_tmr_ctr0_en = '1' then
  424.             if s_count_enable = '1' then   
  425.               if s_c_t0 = '0' then
  426.                 if s_countl0 = conv_unsigned(255,8) then
  427.                   s_countl0 <= conv_unsigned(0,8);
  428.                 else
  429.                   s_countl0 <= s_countl0 + conv_unsigned(1,1);
  430.                 end if;
  431.               else
  432.                 if s_ext_edge0 = '1' then
  433.                   if s_countl0 = conv_unsigned(255,8) then
  434.                     s_countl0 <= conv_unsigned(0,8);
  435.                   else
  436.                     s_countl0 <= s_countl0 + conv_unsigned(1,1);
  437.                   end if;
  438.                 end if;                  
  439.               end if;
  440.             end if; 
  441.           end if;
  442.         end if;
  443.                    
  444.         -- This section generates the timer/counter overflow flag1
  445.         if tcon_tr1_i = '1' then
  446.           if s_count_enable = '1' then   
  447.             if s_count0(15 downto 8) = conv_unsigned(255,8) then
  448.               s_tf1 <= '1';
  449.             else
  450.               s_tf1 <= '0';
  451.             end if;
  452.           end if;
  453.         end if;
  454.         
  455.         -- This section generates the high byte register of tmr/ctr0
  456.         if wt_i = "10" and wt_en_i = '1' then
  457.           s_counth0 <= unsigned(reload_i);  
  458.         else
  459.           if tcon_tr1_i = '1' then
  460.             if s_count_enable = '1' then   
  461.               if s_counth0 = conv_unsigned(255,8) then
  462.                 s_counth0 <= conv_unsigned(0,8);
  463.               else
  464.                 s_counth0 <= s_counth0 + conv_unsigned(1,1);
  465.               end if;
  466.             end if; 
  467.           end if;
  468.         end if;
  469.         
  470.       when others => null;
  471.     end case;
  472.   
  473. ------------------------------------------------------------------------------
  474. --+++++++++++++++++   END OF TIMER / COUNTER 0   +++++++++++++++++++++++++++--
  475. ------------------------------------------------------------------------------
  476.   
  477. ------------------------------------------------------------------------------
  478. --+++++++++++++++++++++   TIMER / COUNTER 1   ++++++++++++++++++++++++++++++--
  479. ------------------------------------------------------------------------------
  480. -- This is timer/counter1. It is built around the 16 bit count register
  481. -- s_count1 and realises its four operating modes
  482. ------------------------------------------------------------------------------
  483. -------------------------------------------------------------------------------
  484. -- operating mode 0 (13 bit timer/counter)
  485. -------------------------------------------------------------------------------
  486.       case s_mode1 is
  487.         when "00" =>
  488.         -- This section generates the timer/counter overflow flag1
  489.         if s_tmr_ctr1_en = '1' then
  490.           if s_count_enable = '1' then
  491.             if s_mode0 = conv_unsigned(1,2) or
  492.                s_mode0 = conv_unsigned(0,2) or
  493.                s_mode0 = conv_unsigned(2,2) then
  494.               if s_c_t1 = '0' or (s_ext_edge1 = '1' and s_c_t1 = '1')  then
  495.                 if s_count1 = conv_unsigned(8191,16) then
  496.                   s_tf1 <= '1';
  497.                 else
  498.                   s_tf1 <= '0';
  499.                 end if;
  500.               end if;
  501.             else
  502.               null;
  503.             end if;
  504.           end if;
  505.         end if;
  506.         
  507.         -- This section generates the low byte register of tmr/ctr1
  508.         if wt_i = "01" and wt_en_i = '1' then
  509.           s_countl1 <= unsigned(reload_i);  
  510.         else
  511.           if s_tmr_ctr1_en = '1' then
  512.             if s_count_enable = '1' then   
  513.               if s_c_t1 = '0' then
  514.                 if s_count1 = conv_unsigned(8191,16) then
  515.                   s_countl1 <= conv_unsigned(0,8);
  516.                 else
  517.                   s_countl1 <= s_countl1 + conv_unsigned(1,1);
  518.                 end if;
  519.               else
  520.                 if s_ext_edge1 = '1' then
  521.                   if s_count1 = conv_unsigned(8191,16) then
  522.                     s_countl1 <= conv_unsigned(0,8);
  523.                   else
  524.                     s_countl1 <= s_countl1 + conv_unsigned(1,1);
  525.                   end if;
  526.                 end if;                  
  527.               end if;
  528.             end if; 
  529.           end if;
  530.         end if;
  531.         
  532.         -- This section generates the high byte register of tmr/ctr1
  533.         if wt_i = "11" and wt_en_i = '1' then
  534.           s_counth1 <= unsigned(reload_i);  
  535.         else
  536.           if s_tmr_ctr1_en = '1' then
  537.             if s_count_enable = '1' then   
  538.               if s_c_t1 = '0' then
  539.                 if s_count1 = conv_unsigned(8191,16) then
  540.                   s_counth1 <= conv_unsigned(0,8);
  541.                 else
  542.                   if s_countl1 = conv_unsigned(255,8) then
  543.                     s_counth1 <= s_counth1 + conv_unsigned(1,1);
  544.                   end if;
  545.                 end if;
  546.               else
  547.                 if s_ext_edge1 = '1' then
  548.                   if s_count1 = conv_unsigned(8191,16) then
  549.                     s_counth1 <= conv_unsigned(0,8);
  550.                   else
  551.                     if s_countl1 = conv_unsigned(255,8) then
  552.                       s_counth1 <= s_counth1 + conv_unsigned(1,1);
  553.                     end if;
  554.                   end if;
  555.                 end if;                  
  556.               end if;
  557.             end if;
  558.           end if;
  559.         end if;
  560. -------------------------------------------------------------------------------
  561. -- operating mode 1 (16 bit timer/counter)
  562. -------------------------------------------------------------------------------
  563.         when "01" =>
  564.         -- This section generates the timer/counter overflow flag1
  565.         if s_tmr_ctr1_en = '1' then
  566.           if s_count_enable = '1' then
  567.             if s_mode0 = conv_unsigned(1,2) or
  568.                s_mode0 = conv_unsigned(0,2) or
  569.                s_mode0 = conv_unsigned(2,2) then
  570.               if s_c_t1 = '0' or (s_ext_edge1 = '1' and s_c_t1 = '1')  then
  571.                 if s_count1 = conv_unsigned(65535,16) then
  572.                   s_tf1 <= '1';
  573.                 else
  574.                   s_tf1 <= '0';
  575.                 end if;
  576.               end if;
  577.             else
  578.               null;
  579.             end if;
  580.           end if;
  581.         end if;
  582.         
  583.         -- This section generates the low byte register of tmr/ctr1
  584.         if wt_i = "01" and wt_en_i = '1' then
  585.           s_countl1 <= unsigned(reload_i);  
  586.         else
  587.           if s_tmr_ctr1_en = '1' then
  588.             if s_count_enable = '1' then   
  589.               if s_c_t1 = '0' then
  590.                 if s_count1 = conv_unsigned(65535,16) then
  591.                   s_countl1 <= conv_unsigned(0,8);
  592.                 else
  593.                   s_countl1 <= s_countl1 + conv_unsigned(1,1);
  594.                 end if;
  595.               else
  596.                 if s_ext_edge1 = '1' then
  597.                   if s_count1 = conv_unsigned(65535,16) then
  598.                     s_countl1 <= conv_unsigned(0,8);
  599.                   else
  600.                     s_countl1 <= s_countl1 + conv_unsigned(1,1);
  601.                   end if;
  602.                 end if;                  
  603.               end if;
  604.             end if; 
  605.           end if;
  606.         end if;
  607.         
  608.         -- This section generates the high byte register of tmr/ctr1
  609.         if wt_i = "11" and wt_en_i = '1' then
  610.           s_counth1 <= unsigned(reload_i);  
  611.         else
  612.           if s_tmr_ctr1_en = '1' then
  613.             if s_count_enable = '1' then   
  614.               if s_c_t1 = '0' then
  615.                 if s_count1 = conv_unsigned(65535,16) then
  616.                   s_counth1 <= conv_unsigned(0,8);
  617.                 else
  618.                   if s_countl1 = conv_unsigned(255,8) then
  619.                     s_counth1 <= s_counth1 + conv_unsigned(1,1);
  620.                   end if;
  621.                 end if;
  622.               else
  623.                 if s_ext_edge1 = '1' then
  624.                   if s_count1 = conv_unsigned(65535,16) then
  625.                     s_counth1 <= conv_unsigned(0,8);
  626.                   else
  627.                     if s_countl1 = conv_unsigned(255,8) then
  628.                       s_counth1 <= s_counth1 + conv_unsigned(1,1);
  629.                     end if;
  630.                   end if;
  631.                 end if;                  
  632.               end if;
  633.             end if;
  634.           end if;
  635.         end if;
  636.         
  637. -------------------------------------------------------------------------------
  638. -- operating mode 2 (8 bit timer/counter, auto reloaded)
  639. -------------------------------------------------------------------------------
  640.         when "10" =>
  641.                
  642.         -- This section generates the timer/counter overflow flag1
  643.         if s_tmr_ctr1_en = '1' then
  644.           if s_count_enable = '1' then   
  645.             if s_mode0 = conv_unsigned(1,2) or
  646.                s_mode0 = conv_unsigned(0,2) or
  647.                s_mode0 = conv_unsigned(2,2) then
  648.               if s_c_t1 = '0' or (s_ext_edge1 = '1' and s_c_t1 = '1')  then
  649.                 if s_count1(7 downto 0) = conv_unsigned(255,16) then
  650.                   s_tf1 <= '1';
  651.                 else
  652.                   s_tf1 <= '0';
  653.                 end if;
  654.               end if;
  655.             else
  656.               null;
  657.             end if;
  658.           end if;
  659.         end if;
  660.         
  661.         -- This section generates the low byte register of tmr/ctr1
  662.         if wt_i = "01" and wt_en_i = '1' then
  663.           s_countl1 <= unsigned(reload_i);  
  664.         else
  665.           if s_tmr_ctr1_en = '1' then
  666.             if s_count_enable = '1' then   
  667.               if s_c_t1 = '0' then
  668.                 if s_countl1 = conv_unsigned(255,8) then
  669.                   s_countl1 <= s_counth1;
  670.                 else
  671.                   s_countl1 <= s_countl1 + conv_unsigned(1,1);
  672.                 end if;
  673.               else
  674.                 if s_ext_edge1 = '1' then
  675.                   if s_countl1 = conv_unsigned(255,8) then
  676.                     s_countl1 <= s_counth1;
  677.                   else
  678.                     s_countl1 <= s_countl1 + conv_unsigned(1,1);
  679.                   end if;
  680.                 end if;                  
  681.               end if;
  682.             end if; 
  683.           end if;
  684.         end if;
  685.         
  686.         -- This section generates the high byte register of tmr/ctr1
  687.         if wt_i = "11" and wt_en_i = '1' then
  688.           s_counth1 <= unsigned(reload_i);
  689.         end if;
  690.         
  691. -------------------------------------------------------------------------------
  692. -- operating mode 3 (One 8 bit timer/counter and one 8 bit timer)
  693. -------------------------------------------------------------------------------
  694.       when "11" =>
  695.         
  696.         -- This section generates the low byte register of tmr/ctr1
  697.         if wt_i = "01" and wt_en_i = '1' then
  698.           s_countl1 <= unsigned(reload_i);  
  699.         end if;
  700.         
  701.         -- This section generates the high byte register of tmr/ctr1
  702.         if wt_i = "11" and wt_en_i = '1' then
  703.           s_counth1 <= unsigned(reload_i);
  704.         end if;
  705.           
  706.       when others => null;
  707.     end case;
  708. ------------------------------------------------------------------------------
  709. --+++++++++++++++++   END OF TIMER / COUNTER 1   +++++++++++++++++++++++++++--
  710. ------------------------------------------------------------------------------
  711.       
  712.     end if;  
  713.   end if;
  714.   
  715.   end process p_tmr_ctr;
  716.   
  717. end rtl;