LoginSignup
0
2

More than 3 years have passed since last update.

[FPGA] 擬似乱数を作る

Posted at

擬似乱数を作成する方法はいくつかありますが、
FPGAと相性が良い「Xorshift」を紹介します。

Xorshift

Wikipedia[1]によると、George Marsagliaが2003年に提案したアルゴリズムらしいです。排他的論理和とビットシフトの2つの演算で構成されるため、FPGAで実装するのに最適です。

モジュール定義

以下、定義です(ベタ書きで申し訳ないです)。
多分有名なので、他のgitには上がっていると思います。
こちらでもそのうちgitにアップします。

module xorshift(
    input clk,
    input rstn,
    output reg [31:0] rand
    );

reg [31:0] x;
reg [31:0] y;
reg [31:0] z;
reg [31:0] w;
reg [31:0] t;

always @ (posedge clk)
begin
    if (!rstn)
    begin
        x <= 123456789;
        y <= 362436069;
        z <= 521288629;
        w <= 88675123;
        t <= 0;
        rand <= 0;
    end
    else
    begin
        t <= x ^ (x << 11);
        x <= y; 
        y <= z; 
        z <= w;
        w <= (w ^ (w >> 19)) ^ (t ^ (t >> 8));
        rand <= w;
    end
end // always

endmodule

参考URL

[1]Xorshift Wikipedia

0
2
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
0
2