Advertisement
Guest User

Untitled

a guest
Sep 21st, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 4.02 KB | None | 0 0
  1. -- here is our clock enable module.  We will take the incoming clock, lets say 100mhz and
  2. -- only enable once a second lets say.  So we want a single pulse once a second to enable
  3. -- the clock for a 1hz equivalent speed.  This is important to do since the hardware inside
  4. -- of the FPGA expects this - you don't want to piss off the hardware *insert spooky music*
  5. entity clockEnable is
  6.     PORT (
  7.         clk : in STD_LOGIC;         -- input clock
  8.         clk_en : out STD_LOGIC;         -- output clock enable, a pulse
  9.                             --   one clock period wide
  10.         by : in integer range 0 to 100000000    -- our division value
  11.     );
  12. end clockEnable ;
  13.  
  14. architecture Behavioral of clockEnable is
  15.     -- this will hold the count for the number of clocks that have gone by since the
  16.     -- last clock enable pulse
  17.     signal count: integer range 0 to 100000000;
  18. begin
  19.  
  20.     -- our primary process where the pulse will be generated.
  21.     process( clk )
  22.     being
  23.         if( rising_edge( clk ) ) then      
  24.             -- every 'by' clocks pulse.
  25.             if( count = by ) then
  26.                 clk_en <= '1';
  27.             else
  28.                 clk_en <= '0';
  29.             end if;
  30.         end if;
  31.     end process;
  32.  
  33. end Behavioral; -- all done!
  34.  
  35. -- we are going to make a 4 LED knightrider pattern - i don't know if you know what
  36. -- knightrider is so here is a youtube link: http://www.youtube.com/watch?v=Mo8Qls0HnWo
  37. entity funLogicStuff is
  38.     PORT (
  39.         clk : in STD_LOGIC;             -- input clock
  40.         clk_en : in STD_LOGIC;              -- input clock enable
  41.         leds : out STD_LOGIC_VECTOR(3 downto 0)     -- output LED's
  42.     );
  43. end clockEnable ;
  44.  
  45. architecture Behavioral of clockEnable is
  46.     signal led_reg: std_logic_vector(3 downto 0);
  47. begin
  48.  
  49.     -- so this is a pretty big gotcha from the perspective of FPGA's.  the signal 'leds'
  50.     -- is an output, there for we can not read it.  So we use a 'temporary' signal and
  51.     -- read from that.  We will then take that 'temporary' signal and pass it out to
  52.     -- the outside world!
  53.     leds <= led_reg;
  54.  
  55.     -- our primary process
  56.     process( clk )
  57.     being
  58.         -- the process is going to get clocked off of the main clock
  59.         if( rising_edge( clk ) ) then
  60.             -- this is VERY important.  We call them defaults - they are the default value
  61.             -- that EVERY signal in the process should have.  So, you may have a situation
  62.             -- where a signal is not changed on a clock tick, you still need to make sure
  63.             -- that the value is assigned.  that is there should always be a default, or
  64.             -- every IF should have an ELSE
  65.             led_reg <= led_reg; -- we do this so the contents do not change if we are not
  66.                                 -- on a clock enable tick
  67.            
  68.             -- however we are only going to do anything unless we see a clock
  69.             -- enable pulse.  in our case, once a second.
  70.             if( clk_en = '1' ) then
  71.                 case led_reg is
  72.                     when "0000" =>
  73.                         led_reg <= "0001";
  74.                     when "0001" =>
  75.                         led_reg <= "0010";
  76.                     when "0010" =>
  77.                         led_reg <= "0100";
  78.                     when "0100" =>
  79.                         led_reg <= "1000";
  80.                     when "1000" =>
  81.                         led_reg <= "0001";
  82.                     when others => null
  83.                 end case;
  84.             end if;
  85.         end if;
  86.     end process;
  87.  
  88. end Behavioral;
  89.  
  90. entity topLevel is
  91.     PORT (
  92.         clk : in STD_LOGIC;                         -- input clock
  93.         leds : out STD_LOGIC_VECTOR(3 downto 0)     -- output LED's
  94.     );
  95. end topLevel ;
  96.  
  97. architecture Behavioral of topLevel is
  98.  
  99.     component clockEnable
  100.         PORT (
  101.             clk : in std_logic;
  102.             clk_en : out std_logic;
  103.             by : in integer range 0 to 100000000
  104.         );
  105.     end component;
  106.    
  107.     component funLogicStuff
  108.         PORT (
  109.             clk : in std_logic;
  110.             clk_en : in std_logic;
  111.             leds : out std_logic_vector(3 downto 0)
  112.         );
  113.     end component;
  114.  
  115.     -- our generated clock enable signal that needs to be brought from
  116.     -- our clock generator to our funlogicstuff module.
  117.     signal clk_en : std_logic := '0';
  118.    
  119. begin
  120.  
  121.     Inst_clockEnable clockEnable port map(
  122.         clk => clk, -- system clock
  123.         clk_en => clk_en, -- our clock enable output
  124.         by => 100000000 -- out constant that defines our clk en rate
  125.     );
  126.  
  127.     Inst_funLogicStuff funLogicStuff port map(
  128.         clk => clk, -- system clock
  129.         clk_en => clk_en, -- note connected as input here
  130.         leds => leds -- to the outside world!
  131.     );
  132.  
  133. end Behavioral
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement