Advertisement
ChipSkylarkk

EE2900 FINAL LAB

Feb 28th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. LIBRARY ieee;
  2. USE ieee.std_logic_1164.all;
  3. USE ieee.numeric_std.all;
  4.  
  5. -- This program computes the square root of a number
  6. ENTITY squareRoot IS PORT (
  7.         SW : IN STD_LOGIC_VECTOR(3 DOWNTO 0); -- Only using first 3 switches
  8.         HEX3,HEX2,HEX1,HEX0 : OUT STD_LOGIC_VECTOR(6 DOWNTO 0); -- 7 LEDs on each seg
  9.         CLOCK_50 : IN STD_LOGIC
  10.     );
  11. END squareRoot;
  12.  
  13. ARCHITECTURE topLevel OF squareRoot IS
  14.     COMPONENT sevenSegmentDecoder IS PORT (
  15.             inputInteger : IN INTEGER RANGE 0 TO 10;
  16.             hexOut : OUT STD_LOGIC_VECTOR(6 DOWNTO 0)
  17.         );
  18.     END COMPONENT;
  19.    
  20. COMPONENT change IS
  21.     PORT
  22.     (
  23.         aclr        : IN STD_LOGIC ;
  24.         clock       : IN STD_LOGIC ;
  25.         dataa       : IN STD_LOGIC_VECTOR (3 DOWNTO 0);
  26.         result      : OUT STD_LOGIC_VECTOR (31 DOWNTO 0)
  27.     );
  28. END COMPONENT;
  29.  
  30. COMPONENT sqroot IS
  31.     PORT
  32.     (
  33.         aclr        : IN STD_LOGIC ;
  34.         clock       : IN STD_LOGIC ;
  35.         data        : IN STD_LOGIC_VECTOR (31 DOWNTO 0);
  36.         result      : OUT STD_LOGIC_VECTOR (31 DOWNTO 0)
  37.     );
  38. END COMPONENT;
  39.  
  40. COMPONENT floattofixed IS
  41.     PORT
  42.     (
  43.         aclr        : IN STD_LOGIC ;
  44.         clock       : IN STD_LOGIC ;
  45.         dataa       : IN STD_LOGIC_VECTOR (31 DOWNTO 0);
  46.         result      : OUT STD_LOGIC_VECTOR (13 DOWNTO 0)
  47.     );
  48. END COMPONENT;
  49.  
  50. SIGNAL clear : STD_LOGIC := '0';
  51. SIGNAL itof : STD_LOGIC_VECTOR (31 DOWNTO 0); -- Change integer to float
  52. SIGNAL sqnum : STD_LOGIC_VECTOR(31 DOWNTO 0); -- Square the number
  53. SIGNAL anstofix : STD_LOGIC_VECTOR (13 DOWNTO 0); -- Changing square root to fixed point
  54. SIGNAL A : INTEGER RANGE 0 TO 7;
  55.  
  56. SIGNAL b,c,d,e,f,g,h,i,j,k,l : INTEGER RANGE 0 TO 1000000;
  57. SIGNAL answer : INTEGER RANGE 0 TO 1000000;
  58.  
  59. SIGNAL unitsDigit : INTEGER RANGE 0 TO 9;
  60. SIGNAL tenthDigit : INTEGER RANGE 0 TO 9;
  61. SIGNAL hundredthDigit : INTEGER RANGE 0 TO 9;
  62. SIGNAL thousandthDigit : INTEGER RANGE 0 TO 9;
  63.  
  64. BEGIN
  65. intfloat : change PORT MAP ( -- First mega function
  66.    aclr => clear,
  67.    clock => CLOCK_50,
  68.    dataa => SW(3 DOWNTO 0),
  69.    result => itof
  70.    );
  71. sqrroot : sqroot PORT MAP( -- Second mega function
  72.    aclr => clear,
  73.    clock => CLOCK_50,
  74.    data => itof,
  75.    result => sqnum
  76.    );
  77. rootfixed : floattofixed PORT MAP( -- Third mega function
  78.    aclr => clear,
  79.    clock => CLOCK_50,
  80.    dataa => sqnum,
  81.    result => anstofix
  82.    );
  83.    
  84.     A <= TO_INTEGER(UNSIGNED(anstofix(13 DOWNTO 11)));
  85.    
  86.     WITH anstofix(10) SELECT b <=   500000 WHEN '1', 0 WHEN OTHERS;
  87.     WITH anstofix(9) SELECT c <=    250000 WHEN '1', 0 WHEN OTHERS;
  88.     WITH anstofix(8) SELECT d <=    125000 WHEN '1', 0 WHEN OTHERS;
  89.     WITH anstofix(7) SELECT e <=    62500 WHEN '1', 0 WHEN OTHERS;
  90.     WITH anstofix(6) SELECT f <=    31250 WHEN '1', 0 WHEN OTHERS;
  91.     WITH anstofix(5) SELECT g <=    15625 WHEN '1', 0 WHEN OTHERS;
  92.     WITH anstofix(4) SELECT h <=    7812 WHEN '1', 0 WHEN OTHERS;
  93.     WITH anstofix(3) SELECT i <=    3906 WHEN '1', 0 WHEN OTHERS;
  94.     WITH anstofix(2) SELECT j <=    1953 WHEN '1', 0 WHEN OTHERS;
  95.     WITH anstofix(1) SELECT k <=    976 WHEN '1', 0 WHEN OTHERS;
  96.     WITH anstofix(0) SELECT l <=    488 WHEN '1', 0 WHEN OTHERS;
  97.    
  98.     answer <= (b+c+d+e+f+g+h+i+j+k+l);
  99.        
  100.     tenthDigit <= (answer/100000);
  101.     hundredthDigit <= (answer REM 100)/10;
  102.     thousandthDigit <= ((answer REM 10)REM 10);
  103.    
  104.     unitsDisplay : sevenSegmentDecoder PORT MAP (inputInteger => unitsDigit,    hexOut => HEX3);
  105.     tenthDisplay : sevenSegmentDecoder PORT MAP (inputInteger => tenthDigit,    hexOut => HEX2);
  106.     hundredthDisplay : sevenSegmentDecoder PORT MAP (inputInteger => hundredthDigit,    hexOut => HEX1);
  107.     thousandthDisplay : sevenSegmentDecoder PORT MAP (inputInteger => thousandthDigit,  hexOut => HEX0);
  108.        
  109. END topLevel;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement