Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LIBRARY ieee;
- USE ieee.std_logic_1164.all;
- USE ieee.numeric_std.all;
- -- This program computes the square root of a number
- ENTITY squareRoot IS PORT (
- SW : IN STD_LOGIC_VECTOR(3 DOWNTO 0); -- Only using first 3 switches
- HEX3,HEX2,HEX1,HEX0 : OUT STD_LOGIC_VECTOR(6 DOWNTO 0); -- 7 LEDs on each seg
- CLOCK_50 : IN STD_LOGIC
- );
- END squareRoot;
- ARCHITECTURE topLevel OF squareRoot IS
- COMPONENT sevenSegmentDecoder IS PORT (
- inputInteger : IN INTEGER RANGE 0 TO 10;
- hexOut : OUT STD_LOGIC_VECTOR(6 DOWNTO 0)
- );
- END COMPONENT;
- COMPONENT change IS
- PORT
- (
- aclr : IN STD_LOGIC ;
- clock : IN STD_LOGIC ;
- dataa : IN STD_LOGIC_VECTOR (3 DOWNTO 0);
- result : OUT STD_LOGIC_VECTOR (31 DOWNTO 0)
- );
- END COMPONENT;
- COMPONENT sqroot IS
- PORT
- (
- aclr : IN STD_LOGIC ;
- clock : IN STD_LOGIC ;
- data : IN STD_LOGIC_VECTOR (31 DOWNTO 0);
- result : OUT STD_LOGIC_VECTOR (31 DOWNTO 0)
- );
- END COMPONENT;
- COMPONENT floattofixed IS
- PORT
- (
- aclr : IN STD_LOGIC ;
- clock : IN STD_LOGIC ;
- dataa : IN STD_LOGIC_VECTOR (31 DOWNTO 0);
- result : OUT STD_LOGIC_VECTOR (13 DOWNTO 0)
- );
- END COMPONENT;
- SIGNAL clear : STD_LOGIC := '0';
- SIGNAL itof : STD_LOGIC_VECTOR (31 DOWNTO 0); -- Change integer to float
- SIGNAL sqnum : STD_LOGIC_VECTOR(31 DOWNTO 0); -- Square the number
- SIGNAL anstofix : STD_LOGIC_VECTOR (13 DOWNTO 0); -- Changing square root to fixed point
- SIGNAL A : INTEGER RANGE 0 TO 7;
- SIGNAL b,c,d,e,f,g,h,i,j,k,l : INTEGER RANGE 0 TO 1000000;
- SIGNAL answer : INTEGER RANGE 0 TO 1000000;
- SIGNAL unitsDigit : INTEGER RANGE 0 TO 9;
- SIGNAL tenthDigit : INTEGER RANGE 0 TO 9;
- SIGNAL hundredthDigit : INTEGER RANGE 0 TO 9;
- SIGNAL thousandthDigit : INTEGER RANGE 0 TO 9;
- BEGIN
- intfloat : change PORT MAP ( -- First mega function
- aclr => clear,
- clock => CLOCK_50,
- dataa => SW(3 DOWNTO 0),
- result => itof
- );
- sqrroot : sqroot PORT MAP( -- Second mega function
- aclr => clear,
- clock => CLOCK_50,
- data => itof,
- result => sqnum
- );
- rootfixed : floattofixed PORT MAP( -- Third mega function
- aclr => clear,
- clock => CLOCK_50,
- dataa => sqnum,
- result => anstofix
- );
- A <= TO_INTEGER(UNSIGNED(anstofix(13 DOWNTO 11)));
- WITH anstofix(10) SELECT b <= 500000 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(9) SELECT c <= 250000 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(8) SELECT d <= 125000 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(7) SELECT e <= 62500 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(6) SELECT f <= 31250 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(5) SELECT g <= 15625 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(4) SELECT h <= 7812 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(3) SELECT i <= 3906 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(2) SELECT j <= 1953 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(1) SELECT k <= 976 WHEN '1', 0 WHEN OTHERS;
- WITH anstofix(0) SELECT l <= 488 WHEN '1', 0 WHEN OTHERS;
- answer <= (b+c+d+e+f+g+h+i+j+k+l);
- tenthDigit <= (answer/100000);
- hundredthDigit <= (answer REM 100)/10;
- thousandthDigit <= ((answer REM 10)REM 10);
- unitsDisplay : sevenSegmentDecoder PORT MAP (inputInteger => unitsDigit, hexOut => HEX3);
- tenthDisplay : sevenSegmentDecoder PORT MAP (inputInteger => tenthDigit, hexOut => HEX2);
- hundredthDisplay : sevenSegmentDecoder PORT MAP (inputInteger => hundredthDigit, hexOut => HEX1);
- thousandthDisplay : sevenSegmentDecoder PORT MAP (inputInteger => thousandthDigit, hexOut => HEX0);
- END topLevel;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement