LoginSignup
4
3

More than 5 years have passed since last update.

FPGAでフィボナッチを計算してみる

Last updated at Posted at 2017-02-16

フィボナッチをFPGAで書いてみた
http://qiita.com/kazunori279/items/70030eaa08fe632b6b49
こちら方の記事をみて、高位合成ではなく、ガチのRTLでフィボナッチ数を計算したらどれぐらいの速さでできるのかやってみた。

ソースコード

とりあえずこんな感じ?
n_inに0~127をセットしてstartを'1'にすると計算を開始して、
計算が終わるとend_flgが’1’になってresultに計算結果が出力される。

fib.vhd
LIBRARY ieee;
use ieee.std_logic_1164.all; 
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;

entity fib is

    port(
        clk : in std_logic;                 
        rst   : in std_logic;

        n_in  : in std_logic_vector(6 downto 0);
        start : in std_logic;

        end_flg : out std_logic;
        result : out std_logic_vector(88 downto 0)
        );  

end fib;

architecture RTL of fib is

    signal n_m1 : std_logic_vector(88 downto 0);
    signal n_m2 : std_logic_vector(88 downto 0);
    signal counter : std_logic_vector(6 downto 0);
    signal end_flg_buff : std_logic;

begin

    process(clk,rst)begin
        if(rst = '0')then
            n_m1 <= CONV_STD_LOGIC_VECTOR(1,89);
            n_m2 <= CONV_STD_LOGIC_VECTOR(1,89);
            counter <= "0000010";
        elsif(clk'event and clk = '1')then
            if(start = '0')then
                n_m1 <= CONV_STD_LOGIC_VECTOR(1,89);
                n_m2 <= CONV_STD_LOGIC_VECTOR(1,89);
                counter <= "0000010";
                end_flg_buff <= '0';    
            else
                if(counter < n_in)then
                    n_m1 <= n_m1 + n_m2;
                    n_m2 <= n_m1;
                    counter <= counter +1;
                else
                    end_flg_buff <= '1';
                end if;
            end if;
        end if;
    end process;

    result <= n_m1;
    end_flg <= end_flg_buff;

end RTL;

論理合成

iCE40-HX8K Breakout Board
http://www.latticesemi.com/Products/DevelopmentBoardsAndKits/iCE40HX8KBreakoutBoard.aspx
sntPl5us.jpg

手持ちで持ってるこれをターゲットに論理合成してみる。
とりあえず、ピンアサインとか制約とかは全くせずに。
ソフトはこんな感じで。
puro.png

結果

リソース

Total Logic Cells: 399/7680
    Combinational Logic Cells: 213      out of   7680      2.77344%
    Sequential Logic Cells:    186      out of   7680      2.42188%
    Logic Tiles:               65       out of   960       6.77083%

ほぼsignal宣言したDFFって感じ。

動作周波数

Clock Summary
Number of clocks: 1
Clock: fib|clk | Frequency: 77.61 MHz | Target: 100.00 MHz

77MHzまで動くっぽい。意外とはやいぞ。

シミュレーション

適当にテストベンチ作って付属のActive-HDLでRTLシミュレーションしてみる。
クロック周波数は50MHz
sim.png
n=65で27777890035288
1ずれているから修正かなこれ。
計算は63クロックの1.26usで完了している。

このあと

とりあえず、ソースの修正とか、実際にFPGAにダウンロードして試してみたいところ。
あとは、回路の最適化もやろうかと。
今日はここまで

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3