LoginSignup
0
0

More than 3 years have passed since last update.

windowsでiverilog その52

Posted at

概要

windowsでiverilogやってみた。
polyphonyで浮動小数点やってみる。
乗算やってみた。

計算手順

①符号、指数、仮数に分ける。
②指数部同士を足す。
③仮数部同士を掛ける。(2進数の乗算)
④仮数部のbit数をオーバー(オーバーフロー)したら指数部にそれを反映させる。
⑤32bitに組み立てる。

サンプルコード

import polyphony
from polyphony import testbench, module, is_worker_running
from polyphony.io import Port
from polyphony.typing import bit, bit32, bit23, bit24, bit25

@module
class mul:
    def __init__(self):
        self.a = Port(bit32, 'in')
        self.b = Port(bit32, 'in')
        self.c = Port(bit32, 'out')
        self.append_worker(self.worker)
    def float2b(self, a:bit32):
        a_sig:bit = (a >> 31) & 0x1
        a_exp:bit8 = (a >> 23) & 0xff
        a_fra:bit24 = a & 0x7fffff | 0x800000
        return (a_sig, a_exp, a_fra)
    def b2float(self, a_sig:bit32, a_exp:bit32, a_fra:bit32):
        return (a_sig << 31) | (a_exp << 23) | a_fra
    def worker(self):
        while is_worker_running():
            a0:bit32 = self.a.rd()
            b0:bit32 = self.b.rd()
            (a_sig, a_exp, a_fra) = self.float2b(a0)
            (b_sig, b_exp, b_fra) = self.float2b(b0)
            b_exp = a_exp + b_exp - 127
            b_exp = b_exp & 0xff
            fra:bit23
            frac:bit48 = a_fra * b_fra
            frac = frac >> 23
            while (frac & 0xffffff000000):
                frac = frac >> 1
                b_exp += 1
            fra = frac & 0x7fffff
            c0:bit32 = self.b2float(b_sig, b_exp, fra)
            self.c(c0)

m = mul()

@testbench
def test(m):
    m.a.wr(0x3dcccccd)
    m.b.wr(0x3f800000)
    print ("0.1 * 1.0") #3dcccccd
    c = m.c.rd()
    print('{:08x}'.format(c))
    m.a.wr(0x3dcccccd)
    m.b.wr(0x40800000)
    print ("0.1 * 4.0") #3ecccccd
    c = m.c.rd()
    print('{:08x}'.format(c))

test(m)





python実行結果



0.1 * 1.0
3dcccccd
0.1 * 4.0
3ecccccd


以上。

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