VHDL在if语句中改变和保持信号

VHDL在if语句中改变和保持信号,vhdl,fpga,Vhdl,Fpga,我是超级新的VHDL和我有一个指定的项目要做。基本上,我的目标是显示2个数字和减法,并在开关的帮助下添加它们。(在FPGA板上) 例如: 假设我有一个位值为9的信号a和位值为2的信号B,每当我打开开关时,它将操作a-B并显示7。问题是,当我关闭开关时,我得到的是9而不是7。(它不保存该值)我想要的是在打开和关闭同一个开关时显示所有的减法结果:9,7,5,3,1 到目前为止我所做的: 我为七段显示编码了一个解码器 我有一个5位的位片加法器-减法器实现 在我的主模块中,我将它们作为组件和实例化 在主

我是超级新的VHDL和我有一个指定的项目要做。基本上,我的目标是显示2个数字和减法,并在开关的帮助下添加它们。(在FPGA板上)

例如: 假设我有一个位值为9的信号a和位值为2的信号B,每当我打开开关时,它将操作a-B并显示7。问题是,当我关闭开关时,我得到的是9而不是7。(它不保存该值)我想要的是在打开和关闭同一个开关时显示所有的减法结果:9,7,5,3,1

到目前为止我所做的:

  • 我为七段显示编码了一个解码器
  • 我有一个5位的位片加法器-减法器实现
  • 在我的主模块中,我将它们作为组件和实例化
  • 在主模块中,我有我声明的两个数字的信号 作为我的位片实例化的输入值
  • 在这个过程中,我为我的信号指定了默认值(9和2)
  • 在if语句中,我将信号A分配给结果 信号(逐位分配给加法器-减法器实例化)并分配七段的信号以产生结果
  • 当我打开开关时,它工作,但当我关闭开关时,默认值保持不变。如何使用每次的结果更新A的值

    我的代码:

    library IEEE;
    use IEEE.STD_LOGIC_1164.ALL;
    
    -- Uncomment the following library declaration if using
    -- arithmetic functions with Signed or Unsigned values
    --use IEEE.NUMERIC_STD.ALL;
    
    -- Uncomment the following library declaration if instantiating
    -- any Xilinx primitives in this code.
    --library UNISIM;
    --use UNISIM.VComponents.all;
    
    entity main is
    
        Port ( 
                  S : in  STD_LOGIC_VECTOR (1 downto 0);
               Cin : in  STD_LOGIC;
                  Cout: out STD_LOGIC;
                  StartGameSwitch : IN std_logic;
                  SevenSegControl : OUT std_logic_vector(7 downto 0):=x"ff";
                  SevenSegBus : OUT std_logic_vector(7 downto 0);
                  clk : IN std_logic);
    end main;
    
    
    architecture Behavioral of main is
    
    --COMPONENTS-------------------------------------------------------------------------------------------------------
    
    COMPONENT sevenSegment
        PORT(
            A : IN std_logic_vector(4 downto 0);
            B : IN std_logic_vector(4 downto 0);
            C : IN std_logic_vector(4 downto 0);
            D : IN std_logic_vector(4 downto 0);
            E : IN std_logic_vector(4 downto 0);
            F : IN std_logic_vector(4 downto 0);
            G : IN std_logic_vector(4 downto 0);
            H : IN std_logic_vector(4 downto 0);
            SevenSegControl : OUT std_logic_vector(7 downto 0);
            SevenSegBus : OUT std_logic_vector(7 downto 0);
            clk : IN std_logic          
            );
    END COMPONENT;
    
    COMPONENT logic
        PORT(
            A : IN std_logic;
            B : IN std_logic;
            COld : IN std_logic;
            S : IN std_logic_vector(1 downto 0);
            AG : IN std_logic;
            BG : IN std_logic;          
            CNew : OUT std_logic;
            NumberBit : OUT std_logic;
            negativeSign : OUT std_logic
            );
    END COMPONENT;
    
    COMPONENT comparator
        PORT(
            A : IN std_logic_vector(4 downto 0);
            B : IN std_logic_vector(4 downto 0);          
            AG : OUT std_logic;
            BG : OUT std_logic
            );
    END COMPONENT;
    
    --SIGNALS-------------------------------------------------------------------------------------------------------
    
    signal sA,sB,sC,sD,sE,sF,sG,sH: std_logic_vector (4 downto 0) ;
    signal logicLed,result,negativeSign,sevenSegmentResult,sevenSegmentNegativeSign: std_logic_vector (4 downto 0);
    signal c0,c1,c2,c3,c4: std_logic;
    signal AG,BG: std_logic;
    signal HP1,HP2,ATK1,ATK2: std_logic_vector (4 downto 0);
    
    --CONSTANTS-------------------------------------------------------------------------------------------------------
    
    constant charO:std_logic_vector(4 downto 0):= "01010"; --10
    constant charP:std_logic_vector(4 downto 0):= "01011"; --11
    constant charE:std_logic_vector(4 downto 0):= "01100"; --12
    constant charN:std_logic_vector(4 downto 0):= "01101"; --13
    constant charF:std_logic_vector(4 downto 0):= "01110"; --14
    constant charI:std_logic_vector(4 downto 0):= "01111"; --15
    constant charG:std_logic_vector(4 downto 0):= "10000"; --16
    constant charH:std_logic_vector(4 downto 0):= "10001"; --17
    constant charT:std_logic_vector(4 downto 0):= "10010"; --18
    constant charA: std_logic_vector(4 downto 0):= "10011";--19
    constant char0:std_logic_vector(4 downto 0):= "00000";
    constant char1:std_logic_vector(4 downto 0):= "00001";
    constant char2:std_logic_vector(4 downto 0):= "00010";
    constant char3:std_logic_vector(4 downto 0):= "00011";
    constant char4:std_logic_vector(4 downto 0):= "00100";
    constant char5:std_logic_vector(4 downto 0):= "00101";
    constant char6:std_logic_vector(4 downto 0):= "00110";
    constant char7:std_logic_vector(4 downto 0):= "00111";
    constant char8:std_logic_vector(4 downto 0):= "01000";
    constant char9:std_logic_vector(4 downto 0):= "01001";
    constant charEmpty:std_logic_vector(4 downto 0):= "11111";
    
    begin
    
    
    --INSTANTIATIONS-------------------------------------------------------------------------------------------------------
    
        Inst_logic1: logic PORT MAP(
            A =>HP2(0) ,
            B =>ATK1(0) ,
            COld =>c0 ,
            CNew =>c1,
            NumberBit =>result(0) ,
            S =>S, 
            AG => AG,
            BG => BG,
            negativeSign => negativeSign(0)
        );
    
            Inst_logic2: logic PORT MAP(
            A =>HP2(1) ,
            B =>ATK1(1) ,
            COld =>c1 ,
            CNew =>c2,
            NumberBit =>result(1) ,
            S =>S, 
            AG => AG,
            BG => BG,
            negativeSign => negativeSign(1)
        );
    
            Inst_logic3: logic PORT MAP(
            A =>HP2(2) ,
            B =>ATK1(2) ,
            COld =>c2 ,
            CNew =>c3,
            NumberBit =>result(2) ,
            S =>S, 
            AG => AG,
            BG => BG,
            negativeSign => negativeSign(2)
        );
    
            Inst_logic4: logic PORT MAP(
            A =>HP2(3) ,
            B =>ATK1(3) ,
            COld =>c3 ,
            CNew =>c4,
            NumberBit =>result(3) ,
            S =>S, 
            AG => AG,
            BG => BG,
            negativeSign => negativeSign(3)
        );
    
            Inst_logic5: logic PORT MAP(
            A =>HP2(4) ,
            B =>ATK1(4) ,
            COld =>c4 ,
            CNew =>Cout,
            NumberBit =>result(4) ,
            S =>S, 
            AG => AG,
            BG => BG,
            negativeSign => negativeSign(4)
        );  
    
            Inst_comparator: comparator PORT MAP(
            A => HP2,
            B => ATK1,
            AG => AG,
            BG => BG
        );
    
    --GAME LOGIC-------------------------------------------------------------------------------------------------------
    
    
    
    process(StartGameSwitch)
    begin
    
    
    --DEFAULT VALUES FOR HP1,HP2,ATK1,ATK2--
    -- I WANT HP1 AND HP2 TO CHANGE ACCORDING TO SWITCHES --
        HP2 <= char9;
        ATK1 <= char2;   
       HP1 <= char9;
        ATK2 <= char3;
    
        if(StartGameSwitch = '0') then -- OPEN P35 ON FPGA
    
                sA <= charO; -- s_ are the signals for the seven segment 
                sB <= charP;
                sC <= charE;
                sD <= charN;
                sE <= charEmpty;
                sF <= charP;
                sG <= char7;
                sH <= char8;
    
        else -- WHEN P35 IS OPENED ( WHEN THE GAME STARTS) 
    
    
                sA <= HP1; --HP POINT FOR P1
                sB <= charEmpty; 
                sC <= ATK1; --Attack POINT FOR P1
                sD <= charEmpty; 
    
                sE <= charEmpty; 
                sF <= ATK2; --Attack POINT FOR P2
                sG <= charEmpty; 
                sH <= HP2; --HP POINT FOR P2
    
                if(S = "01") then -- WHEN PLAYER 1 ATTACKS PLAYER 2 ( HP2 - ATK1 = RESULT (i.e 9-2 = 7))
                    HP2 <= result; 
                    sH <= HP2; -- When SWITCH IS OPEN, IT SHOWS 7 WITHOUT ANY PROBLEM
                end if;
        -- HOWEVER, WHEN THE SWITCH IS AGAIN BACK TO 00, sH displays 9 instead of 7, HOW CAN I SAVE THE VALUE OF HP2?
    
        end if;
    end process;
    
    -- SIGNALS ASSIGNED TO DISPLAY 
    Inst_sevenSegment: sevenSegment PORT MAP(
            A =>sA, --1ST PLAYER HEALTH 
            B =>sB, -- 1ST PLAYER DMG
            C =>sC ,
            D =>sD ,
            E =>sE ,
            F =>sF ,
            G =>sG , -- 2ND PLAYER DMG
            H =>sH , --2ND PLAYER HEALTH
            SevenSegControl =>SevenSegControl ,
            clk => clk,
            SevenSegBus => SevenSegBus
        );
    
    end Behavioral;
    
    IEEE库;
    使用IEEE.STD_LOGIC_1164.ALL;
    --如果使用,请取消注释以下库声明
    --具有有符号或无符号值的算术函数
    --使用IEEE.NUMERIC_STD.ALL;
    --如果正在实例化,请取消对以下库声明的注释
    --此代码中的任何Xilinx原语。
    --UNISIM图书馆;
    --使用UNISIM.VComponents.all;
    实体主体是
    港口(
    S:标准逻辑向量(1到0);
    Cin:标准逻辑;
    Cout:输出标准逻辑;
    STARTGAMESSWITCH:标准逻辑中;
    七级控制:输出标准逻辑向量(7到0):=x“ff”;
    SevenSegBus:OUT标准逻辑向量(7到0);
    时钟:在标准逻辑中);
    端干管;
    主要信息系统的架构
    --组成部分-------------------------------------------------------------------------------------------------------
    成分七聚
    港口(
    A:标准逻辑向量(4到0);
    B:标准逻辑向量(4到0);
    C:标准逻辑向量(4到0);
    D:标准逻辑向量(4到0);
    E:标准逻辑向量(4到0);
    F:标准逻辑向量(4到0);
    G:标准逻辑向量(4到0);
    H:标准逻辑向量(4到0);
    七级控制:输出标准逻辑向量(7到0);
    SevenSegBus:OUT标准逻辑向量(7到0);
    时钟:在标准逻辑中
    );
    端部元件;
    组件逻辑
    港口(
    答:标准逻辑;
    B:标准逻辑;
    冷:在标准逻辑中;
    S:标准逻辑向量(1到0);
    AG:IN-std_逻辑;
    BG:标准逻辑中;
    CNew:输出标准逻辑;
    NumberBit:输出标准逻辑;
    否定设计:输出标准逻辑
    );
    端部元件;
    分量比较器
    港口(
    A:标准逻辑向量(4到0);
    B:标准逻辑向量(4到0);
    AG:输出标准逻辑;
    BG:输出标准逻辑
    );
    端部元件;
    --信号-------------------------------------------------------------------------------------------------------
    信号sA、sB、sC、sD、sE、sF、sG、sH:std_逻辑_向量(4到0);
    信号逻辑LED、结果、否定设计、七段结果、七段否定设计:标准逻辑向量(4到0);
    信号c0、c1、c2、c3、c4:std_逻辑;
    信号AG、BG:std_逻辑;
    信号HP1、HP2、ATK1、ATK2:std_逻辑_向量(4到0);
    --常数-------------------------------------------------------------------------------------------------------
    常数charO:std_逻辑_向量(4到0):=“01010”--10
    常量字符:标准逻辑向量(4到0):=“01011”--11
    常量字符:标准逻辑向量(4到0):=“01100”--12
    常量字符:标准逻辑向量(4到0):=“01101”--13
    常量字符:标准逻辑向量(4到0):=“01110”--14
    常数charI:std_逻辑_向量(4到0):=“01111”--15
    常量字符:标准逻辑向量(4到0):=“10000”--16
    常量字符:标准逻辑向量(4到0):=“10001”--17
    常数图:标准逻辑向量(4到0):=“10010”--18
    常数字符:标准逻辑向量(4到0):=“10011”--19
    常数char0:std_逻辑_向量(4到0):=“00000”;
    常数char1:std_逻辑_向量(4到0):=“00001”;
    常数char2:std_逻辑_向量(4到0):=“00010”;
    常数char3:std_逻辑_向量(4到0):=“00011”;
    常数char4:std_逻辑_向量(4到0):=“00100”;
    常数char5:std_逻辑_向量(4到0):=“00101”;
    常数char6:std_逻辑_向量(4到0):=“00110”;
    常数char7:std_逻辑_向量(4到0):=“00111”;
    常数char8:std_逻辑_向量(4到0):=“01000”;
    常数char9:std_逻辑_向量(4到0):=“01001”;
    常数charEmpty:std_逻辑_向量(4到0):=“11111”;
    开始
    --实例-------------------------------------------------------------------------------------------------------
    Inst_logic1:逻辑端口映射(
    A=>HP2(0),
    B=>ATK1(0),
    冷=>c0,
    CNew=>c1,
    NumberBit=>结果(0),
    S=>S,
    AG=>AG,
    BG=>BG,
    negativeSign=>negativeSign(0)
    );
    Inst_logic2:逻辑端口映射(
    A=>HP2(1),
    B=>ATK1(1),
    冷=>c1,
    CNew=>c2,
    NumberBit=>结果(1),
    S=>S,
    AG=>AG,
    BG=>BG,
    negativeSign=>negativeSign(1)
    );
    Inst_logic3:逻辑端口映射(
    A=>HP2(2),
    B=>ATK1(2),
    冷=>c2,
    CNew=>c3,
    NumberBit=>结果(2),
    S=>S,
    AG=>AG,
    BG=>BG,
    negativeSign=>negativeSign(2)
    );
    仪器逻辑4:逻辑端口M
    
    process(StartGameSwitch,S)
    begin
    
    
    --DEFAULT VALUES FOR HP1,HP2,ATK1,ATK2--
    -- I WANT HP1 AND HP2 TO CHANGE ACCORDING TO SWITCHES --
        --HP2 <= char9;
        --ATK1 <= char2;   
        --HP1 <= char9;
        --ATK2 <= char3;
    
        if(StartGameSwitch = '0') then -- OPEN P35 ON FPGA
    
                HP2 <= char9;  --Assign default values here instead
                ATK1 <= char2;   
                HP1 <= char9;
                ATK2 <= char3;
    
                sA <= charO; -- s_ are the signals for the seven segment 
                sB <= charP;
                sC <= charE;
                sD <= charN;
                sE <= charEmpty;
                sF <= charP;
                sG <= char7;
                sH <= char8;
    
        else -- WHEN P35 IS OPENED ( WHEN THE GAME STARTS)