06 December 2011

Well after battling around for a while I was finally able to get this short but still exciting example working. Now, I will post more about this later but basically it is quite likely that the code you are reading is horrible and the worst possible practice imaginable. In fact it is quite likely.

I’m teaching myself how to work with FPGAs and design hardware based solutions. I’m starting effectively from scratch. If you don’t know what a constraints file is, why you need one or that if you don’t comment out the unused definitions it will blow up in your face, these posts might be useful to you. I am planning to post what I’ve come up with so that it’s at the point where you can actually deploy it to your board and see it actually doing something other than the demo it came with.

For now though, here’s the code I wrote to get LED’s to flash based on switch and button position. I’ll post a video later so you can see what it is actually supposed to do.


-- Company: The Hong Kong Polytechnic University
-- Engineer: Peter Membrey
-- Create Date:    11:09:32 12/05/2011 
-- Design Name: 
-- Module Name:    HelloWorld with clock - Behavioral 
-- Project Name: 
-- Target Devices: Digilent Atlys Spartan-6
-- Tool versions: 
-- Description: 
-- Dependencies: 
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
library IEEE;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values

-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity HelloWorld is
    Port ( clk  : in  STD_LOGIC;
           sw0  : in  STD_LOGIC;
           bt0  : in  STD_LOGIC;
           sw1  : in  STD_LOGIC;
           led0 : out  STD_LOGIC;
           led1 : out  STD_LOGIC);
end HelloWorld;

architecture Behavioral of HelloWorld is
  signal p0,p1,p2,p3,p4,p5 : STD_LOGIC;
  -- p0 goes high if sw0 is on
  -- p2 goes high if sw1 is on
  -- p1 XOR of p0 if but0 is on else p0
  -- p3 XOR of p2 if but0 is on else p2
  -- p4 didn't use it for some reason
  -- p5 1Hz clock signal
 process (sw0,sw1,bt0,clk)

	variable cnt : integer := 0;
	variable cntOn : boolean;
   if rising_edge(clk) then
      if(cnt =50000000) then
         cnt:= 0;
         if(cntOn= true) then
            p5 <= '1';
            cntOn:= false;
            p5 <= '0';
            cntOn:= true;
         end if;			
         cnt:= cnt +1;
      end if;
   end if;
   if(sw0='1') then
	  p0 <= '1';
     p0 <= '0';
	end if;
	if(sw1='1') then
	  p2 <= '1';
	  p2 <= '0';
	end if;
	if(bt0='1') then
	  p1 <= not p0;
	  p3 <= not p2;
	  p1 <= p0;
	  p3 <= p2;
	end if;
	if(p1='1' and p5='1') then
	  led0 <= '1';
	  led0 <= '0';
	end if;
	if(p3='1' and p5='1') then
	  led1 <= '1';
	  led1 <= '0';
	end if;
 end Process;
end Behavioral;

blog comments powered by Disqus