Binary 在VHDL中将8位二进制数转换为BCD

Binary 在VHDL中将8位二进制数转换为BCD,binary,vhdl,bcd,Binary,Vhdl,Bcd,该算法是众所周知的,你做8次左移位,并在每次移位后检查单位,10位或数百位(每个4位)。如果他们高于4,你会在小组中增加3,依此类推 这是一个基于流程的解决方案,它不起作用。它将编译,但输出不是我想要的。有什么想法吗?有什么问题吗 library ieee ; use ieee.std_logic_1164.all ; use ieee.std_logic_unsigned.all ; entity hex2bcd is port ( hex_in : in std_logic_v

该算法是众所周知的,你做8次左移位,并在每次移位后检查单位,10位或数百位(每个4位)。如果他们高于4,你会在小组中增加3,依此类推

这是一个基于流程的解决方案,它不起作用。它将编译,但输出不是我想要的。有什么想法吗?有什么问题吗

library ieee ;
use ieee.std_logic_1164.all ;
use ieee.std_logic_unsigned.all ;

entity hex2bcd is
    port ( hex_in  : in  std_logic_vector (7 downto 0) ;
           bcd_hun : out std_logic_vector (3 downto 0) ;
           bcd_ten : out std_logic_vector (3 downto 0) ;
           bcd_uni : out std_logic_vector (3 downto 0) ) ;
end hex2bcd ;

architecture arc_hex2bcd of hex2bcd is
begin
    process ( hex_in )
        variable hex_src : std_logic_vector (7 downto 0) ;
        variable bcd     : std_logic_vector (11 downto 0) ;
    begin
        hex_src := hex_in ;
        bcd     := (others => '0') ;

        for i in 0 to 7 loop
            bcd := bcd(11 downto 1) & hex_src(7) ; -- shift bcd + 1 new entry
            hex_src := hex_src(7 downto 1) & '0' ; -- shift src + pad with 0

            if bcd(3 downto 0) > "0100" then
                bcd(3 downto 0) := bcd(3 downto 0) + "0011" ;
            end if ;
            if bcd(7 downto 4) > "0100" then
                bcd(7 downto 4) := bcd(7 downto 4) + "0011" ;
            end if ;
            if bcd(11 downto 8) > "0100" then
                bcd(11 downto 8) := bcd(11 downto 8) + "0011" ;
            end if ;
        end loop ;

        bcd_hun <= bcd(11 downto 8) ;
        bcd_ten <= bcd(7  downto 4) ;
        bcd_uni <= bcd(3  downto 0) ;

    end process ;
end arc_hex2bcd ;
ieee库;
使用ieee.std_logic_1164.all;
使用ieee.std_logic_unsigned.all;
实体hex2bcd是
端口(十六进制输入:标准逻辑向量(7到0);
bcd_hun:输出标准逻辑向量(3到0);
bcd十:输出标准逻辑向量(3到0);
bcd_uni:out标准逻辑向量(3到0);
结束hex2bcd;
hex2bcd的架构arc_hex2bcd是
开始
过程(十六进制输入)
变量hex_src:std_逻辑_向量(7到0);
变量bcd:std_逻辑_向量(11到0);
开始
hex_src:=hex_in;
bcd:=(其他=>'0');
对于0到7循环中的i
bcd:=bcd(11向下至1)和hex_src(7);--班次bcd+1新条目
hex_src:=hex_src(从7到1)和“0”;--用0移位src+pad
如果bcd(3向下至0)>“0100”,则
bcd(3向下至0):=bcd(3向下至0)+“0011”;
如果结束;
如果bcd(7至4)>“0100”,则
bcd(7向下至4):=bcd(7向下至4)+“0011”;
如果结束;
如果bcd(11至8)>“0100”,则
bcd(11向下至8):=bcd(11向下至8)+“0011”;
如果结束;
端环;

bcd_hun至少出现两个问题:

  • 添加在shift之后完成,而不是在算法中描述的之前

  • bcd
    转换为
    bcd(11向下转换为1)
    ,但应为
    bcd(10向下转换为0)

因此,请尝试以下代码:

process ( hex_in )
    variable hex_src : std_logic_vector (7 downto 0) ;
    variable bcd     : std_logic_vector (11 downto 0) ;
begin
    hex_src := hex_in ;
    bcd     := (others => '0') ;

    for i in 0 to 7 loop
        if bcd(3 downto 0) > "0100" then
            bcd(3 downto 0) := bcd(3 downto 0) + "0011" ;
        end if ;
        if bcd(7 downto 4) > "0100" then
            bcd(7 downto 4) := bcd(7 downto 4) + "0011" ;
        end if ;
        if bcd(11 downto 8) > "0100" then
            bcd(11 downto 8) := bcd(11 downto 8) + "0011" ;
        end if ;

        bcd := bcd(10 downto 0) & hex_src(7) ; -- shift bcd + 1 new entry
        hex_src := hex_src(6 downto 0) & '0' ; -- shift src + pad with 0
    end loop ;

    bcd_hun <= bcd(11 downto 8) ;
    bcd_ten <= bcd(7  downto 4) ;
    bcd_uni <= bcd(3  downto 0) ;

end process ;
过程(十六进制)
变量hex_src:std_逻辑_向量(7到0);
变量bcd:std_逻辑_向量(11到0);
开始
hex_src:=hex_in;
bcd:=(其他=>'0');
对于0到7循环中的i
如果bcd(3向下至0)>“0100”,则
bcd(3向下至0):=bcd(3向下至0)+“0011”;
如果结束;
如果bcd(7至4)>“0100”,则
bcd(7向下至4):=bcd(7向下至4)+“0011”;
如果结束;
如果bcd(11至8)>“0100”,则
bcd(11向下至8):=bcd(11向下至8)+“0011”;
如果结束;
bcd:=bcd(10到0)&hex_src(7);--班次bcd+1新条目
hex_src:=hex_src(从6到0)和“0”;--用0移位src+pad
端环;

bcd_hun至少出现两个问题:

  • 添加在shift之后完成,而不是在算法中描述的之前

  • bcd
    转换为
    bcd(11向下转换为1)
    ,但应为
    bcd(10向下转换为0)

因此,请尝试以下代码:

process ( hex_in )
    variable hex_src : std_logic_vector (7 downto 0) ;
    variable bcd     : std_logic_vector (11 downto 0) ;
begin
    hex_src := hex_in ;
    bcd     := (others => '0') ;

    for i in 0 to 7 loop
        if bcd(3 downto 0) > "0100" then
            bcd(3 downto 0) := bcd(3 downto 0) + "0011" ;
        end if ;
        if bcd(7 downto 4) > "0100" then
            bcd(7 downto 4) := bcd(7 downto 4) + "0011" ;
        end if ;
        if bcd(11 downto 8) > "0100" then
            bcd(11 downto 8) := bcd(11 downto 8) + "0011" ;
        end if ;

        bcd := bcd(10 downto 0) & hex_src(7) ; -- shift bcd + 1 new entry
        hex_src := hex_src(6 downto 0) & '0' ; -- shift src + pad with 0
    end loop ;

    bcd_hun <= bcd(11 downto 8) ;
    bcd_ten <= bcd(7  downto 4) ;
    bcd_uni <= bcd(3  downto 0) ;

end process ;
过程(十六进制)
变量hex_src:std_逻辑_向量(7到0);
变量bcd:std_逻辑_向量(11到0);
开始
hex_src:=hex_in;
bcd:=(其他=>'0');
对于0到7循环中的i
如果bcd(3向下至0)>“0100”,则
bcd(3向下至0):=bcd(3向下至0)+“0011”;
如果结束;
如果bcd(7至4)>“0100”,则
bcd(7向下至4):=bcd(7向下至4)+“0011”;
如果结束;
如果bcd(11至8)>“0100”,则
bcd(11向下至8):=bcd(11向下至8)+“0011”;
如果结束;
bcd:=bcd(10到0)&hex_src(7);--班次bcd+1新条目
hex_src:=hex_src(从6到0)和“0”;--用0移位src+pad
端环;

bcd_hun评论太长了

考虑以下框图:

这表示一个展开的循环(
表示0到7循环中的i
),并显示LS BCD数字在i=2之前没有发生add+3,中间BCD数字在i=5之前没有发生add+3,MS BCD数字没有发生调整,这是静态“0”值的一部分

这给我们提供了总共7个add3模块(由封闭的if语句和条件add+3表示)

这在VHDL中得到了演示:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity bin8bcd is
    port (
        bin:    in  std_logic_vector (7 downto 0);
        bcd:    out std_logic_vector (11 downto 0)
    );
end entity;

architecture struct of bin8bcd is
    procedure add3 (signal bin: in  std_logic_vector (3 downto 0); 
                    signal bcd: out std_logic_vector (3 downto 0)) is
    variable is_gt_4:  std_logic;
    begin
        is_gt_4 := bin(3) or (bin(2) and (bin(1) or bin(0)));

        if is_gt_4 = '1' then
        -- if to_integer(unsigned (bin)) > 4 then
            bcd <= std_logic_vector(unsigned(bin) + "0011");
        else
            bcd <= bin;
        end if;
    end procedure;

    signal U0bin,U1bin,U2bin,U3bin,U4bin,U5bin,U6bin:
                std_logic_vector (3 downto 0);

    signal U0bcd,U1bcd,U2bcd,U3bcd,U4bcd,U5bcd,U6bcd:
                std_logic_vector (3 downto 0);       
begin
    U0bin <= '0' & bin (7 downto 5);
    U1bin <= U0bcd(2 downto 0) & bin(4);
    U2bin <= U1bcd(2 downto 0) & bin(3);
    U3bin <= U2bcd(2 downto 0) & bin(2);
    U4bin <= U3bcd(2 downto 0) & bin(1);

    U5bin <= '0' & U0bcd(3) & U1bcd(3) & U2bcd(3);
    U6bin <= U5bcd(2 downto 0) & U3bcd(3);

U0: add3(U0bin,U0bcd);

U1: add3(U1bin,U1bcd);

U2: add3(U2bin,U2bcd);

U3: add3(U3bin,U3bcd);

U4: add3(U4bin,U4bcd);

U5: add3(U5bin,U5bcd);

U6: add3(U6bin,U6bcd);

OUTP:
    bcd <= '0' & '0' & U5bcd(3) & U6bcd & U4bcd & bin(0);

end architecture;

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity bin8bcd_tb is
end entity;

architecture foo of bin8bcd_tb is
    signal bin: std_logic_vector (7 downto 0) := (others => '0');
    -- (initialized to prevent those annoying metavalue reports)

    signal bcd: std_logic_vector (11 downto 0);

begin

DUT:
    entity work.bin8bcd
        port map (
            bin => bin,
            bcd => bcd
        );

STIMULUS:
    process

    begin
        for i in 0 to 255 loop
            bin <= std_logic_vector(to_unsigned(i,8));
            wait for 1 ns;
        end loop;
        wait for 1 ns;
        wait;
    end process;
end architecture;
ieee库;
使用ieee.std_logic_1164.all;
使用ieee.numeric_std.all;
实体bin8bcd是
港口(
bin:标准逻辑向量(7到0);
bcd:输出标准逻辑向量(11到0)
);
终端实体;
bin8bcd的体系结构为
程序add3(信号箱:标准逻辑向量中(3至0);
信号bcd:out标准逻辑向量(3到0)为
变量为_gt_4:std_逻辑;
开始
is_gt_4:=第(3)或(第(2)和(第(1)或第(0)条);
如果is_gt_4='1',则
--如果to_integer(unsigned(bin))>4,则

bcd评论太长了

考虑以下框图:

这表示一个展开的循环(
表示0到7循环中的i
),并显示LS BCD数字在i=2之前没有发生add+3,中间BCD数字在i=5之前没有发生add+3,MS BCD数字没有发生调整,这是静态“0”值的一部分

这给我们提供了总共7个add3模块(由封闭的if语句和条件add+3表示)

这在VHDL中得到了演示:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity bin8bcd is
    port (
        bin:    in  std_logic_vector (7 downto 0);
        bcd:    out std_logic_vector (11 downto 0)
    );
end entity;

architecture struct of bin8bcd is
    procedure add3 (signal bin: in  std_logic_vector (3 downto 0); 
                    signal bcd: out std_logic_vector (3 downto 0)) is
    variable is_gt_4:  std_logic;
    begin
        is_gt_4 := bin(3) or (bin(2) and (bin(1) or bin(0)));

        if is_gt_4 = '1' then
        -- if to_integer(unsigned (bin)) > 4 then
            bcd <= std_logic_vector(unsigned(bin) + "0011");
        else
            bcd <= bin;
        end if;
    end procedure;

    signal U0bin,U1bin,U2bin,U3bin,U4bin,U5bin,U6bin:
                std_logic_vector (3 downto 0);

    signal U0bcd,U1bcd,U2bcd,U3bcd,U4bcd,U5bcd,U6bcd:
                std_logic_vector (3 downto 0);       
begin
    U0bin <= '0' & bin (7 downto 5);
    U1bin <= U0bcd(2 downto 0) & bin(4);
    U2bin <= U1bcd(2 downto 0) & bin(3);
    U3bin <= U2bcd(2 downto 0) & bin(2);
    U4bin <= U3bcd(2 downto 0) & bin(1);

    U5bin <= '0' & U0bcd(3) & U1bcd(3) & U2bcd(3);
    U6bin <= U5bcd(2 downto 0) & U3bcd(3);

U0: add3(U0bin,U0bcd);

U1: add3(U1bin,U1bcd);

U2: add3(U2bin,U2bcd);

U3: add3(U3bin,U3bcd);

U4: add3(U4bin,U4bcd);

U5: add3(U5bin,U5bcd);

U6: add3(U6bin,U6bcd);

OUTP:
    bcd <= '0' & '0' & U5bcd(3) & U6bcd & U4bcd & bin(0);

end architecture;

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity bin8bcd_tb is
end entity;

architecture foo of bin8bcd_tb is
    signal bin: std_logic_vector (7 downto 0) := (others => '0');
    -- (initialized to prevent those annoying metavalue reports)

    signal bcd: std_logic_vector (11 downto 0);

begin

DUT:
    entity work.bin8bcd
        port map (
            bin => bin,
            bcd => bcd
        );

STIMULUS:
    process

    begin
        for i in 0 to 255 loop
            bin <= std_logic_vector(to_unsigned(i,8));
            wait for 1 ns;
        end loop;
        wait for 1 ns;
        wait;
    end process;
end architecture;
ieee库;
使用ieee.std_logic_1164.all;
使用ieee.numeric_std.all;
实体bin8bcd是
港口(
bin:标准逻辑向量(7到0);
bcd:输出标准逻辑向量(11到0)
);
终端实体;
bin8bcd的体系结构为
程序add3(信号箱:标准逻辑向量中(3至0);
信号bcd:out标准逻辑向量(3到0)为
变量为_gt_4:std_逻辑;
开始
is_gt_4:=第(3)或(第(2)和(第(1)或第(0)条);
如果is_gt_4='1',则
--如果to_integer(unsigned(bin))>4,则

bcd1。你需要从bcd的10位到0位来回移动
    LIBRARY ieee;                       
USE ieee.std_logic_1164.ALL; 
use ieee.numeric_std.all;  
--converting a 8bit binary number to a 12bit bcd
entity bin2bcd is
port (bin :in std_logic_vector (7 downto 0);
        bcd1 : out std_logic_vector (3 downto 0);
        bcd2 : out std_logic_vector (3 downto 0);
        bcd3 : out std_logic_vector (3 downto 0));
end entity;

architecture rtl of bin2bcd is 
begin
process ( bin )
    variable binx : std_logic_vector (7 downto 0) ;
    variable bcd     : std_logic_vector (11 downto 0) ;
begin
    bcd             := (others => '0') ;
    binx        := bin(7 downto 0) ;

    for i in binx'range loop
        if bcd(3 downto 0) > "0100" then
          bcd(3 downto 0) := std_logic_vector(unsigned( bcd(3 downto 0)) + "0011"); 

        end if ;
        if bcd(7 downto 4) > "0100" then
           bcd(7 downto 4) := std_logic_vector(unsigned( bcd(7 downto 4)) + "0011");    
        end if ;
        bcd := bcd(10 downto 0) & binx(7) ; 
        binx := binx(6 downto 0) & '0' ; 
    end loop ;

    bcd3 <= bcd(11 downto 8) ;
    bcd2 <= bcd(7  downto 4) ;
    bcd1 <= bcd(3  downto 0) ;
end process ;
end architecture;