移位乘法器的设计

  • 格式:doc
  • 大小:145.50 KB
  • 文档页数:11

移位乘法器的设计
一.设计要求。

乘法器的输入为两个四位二进制数a 和b ,闸门信号STB 启动乘法操作,时钟信号提供系统定时。

乘法结果为8位二进制信号RESULT, 乘法结束后置信号
DONE 为1。

框图如下:
二.算法思路如下:
采用原码移位算法,即对两操作数进行逐位的移位相加,迭代四次后获得乘法结果。

1. 在被乘数和乘数的高位补0后扩展成8位。

2. 乘数依次向右移位,并检查其最低位,如果该位为1,则将被乘数与部分积的和相加,然后被乘数向左端移位;如果最低位为0,则仅仅对被乘数进行移位操作。

移位时,乘数的高端和被乘数的低端都移入0。

3. 当乘数变成全0后,乘法结束。

三. 模块划分和进程设计:
把乘法器电路映射为控制器进程CONTROLLER、锁存移位进程SRA和SRB、加法进程ADDER以及锁存结果的进程ACC。

四. 移位乘法器的进程模块图
五. 按照书本上的代码仿真后的波形如下:
得出的是错误的结果。

经分析,可知道是由于在第三周期是shift 的值出现错误,才导致结果错误。

为此修改源代码。

如下(红色为修改的部分):
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity shift_mul is
port (a,b :in std_logic_vector(3 downto 0);
stb,clk :in std_logic;
done :out std_logic;
result :out std_logic_vector(7 downto 0));
end shift_mul;
architecture behav of shift_mul is
signal init,shift,stop,add :std_logic;
signal sraa,srbb,accout,addout:std_logic_vector(7 downto 0); begin
controller :process
begin
wait until clk'event and clk='1' and stb='1';
done<='0';
init<='1';
shift<='0';add<='0';result<="00000000";
wait until clk'event and clk='1';
init<='0';
wait until clk'event and clk='1';
wait until clk'event and clk='1';
run_loop: while (stop/='1') loop
wait until clk'event and clk='1';
if sraa(0)='1' then
wait until clk'event and clk='1';
add<='1';
wait until clk'event and clk='1';
add<='0';
shift<='1';
wait until clk'event and clk='1';
else
wait until clk'event and clk='1';
shift<='1';
wait until clk'event and clk='1';
end if;
shift<='0';
end loop run_loop;
done<='1'; result<=accout;
end process controller;
sral:process
begin
wait until clk'event and clk='1';
if init='1'then
sraa<="0000"&a;
elsif shift='1'then
sraa<='0'&sraa(7 downto 1);
end if;
stop<=not (sraa(3) or sraa(2) or sraa(1) or sraa(0) );
end process sral;
srar:process
begin
wait until clk'event and clk='1';
if init='1'then
srbb<="0000"&b;
elsif shift='1'then
srbb<=srbb(6 downto 0)&'0';
end if;
end process srar;
adder: process(accout,srbb)
variable sum,tmp1,tmp2:std_logic_vector(7 downto 0);
variable carry:std_logic;
begin
tmp1:=accout;
tmp2:=srbb;
carry:='0';
for I in 0 to 7 loop
sum(I):=tmp1(I) xor tmp2(I) xor carry;
carry:=(tmp1(I) and tmp2(I)) or (tmp1(I) and carry) or (tmp2(I) and carry);
end loop;
addout<=sum;
end process adder;
acc:process
begin
wait until clk'event and clk='1';
if init='1' then
accout<=(others =>'0');
elsif add='1' then
accout<=addout;
end if;
end process acc;
end behav;
七。

测试平台如下:
Library IEEE;
use IEEE.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity shift_mul_tb is
end;
architecture behav of shift_mul_tb is
component shift_mul
port( a,b:in std_logic_vector(3 downto 0);
stb,clk: in std_logic;
done:out std_logic;
result: out std_logic_vector(7 downto 0));
end component;
signal a,b: std_logic_vector(3 downto 0);
signal stb,clk,done: std_logic;
signal result: std_logic_vector(7 downto 0);
begin
uut_a: shift_mul port map (a,b,stb,clk,done,result);
process
begin
clk <='0';
wait for 100 ns;
clk <='1';
wait for 100 ns;
end process;
a<="1101";
b<="1010";
stb<='0','1' after 5 ns,'0' after 400 ns;
end;
configuration cfg_shift_mul of shift_mul_tb is
for behav
end for;
end;
经验证,其结果正确,为1101×1010=10000010(13×10=130)图形如下:。