Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
13 years ago

Subtraction with numeric_Std doesn't work

Hello

What am i doing in wrong in the code below. subtraction doesn't work! I am using numeric_std and the numbers are in twos complement format.

// mattgust

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.OF_DET.all;
entity Arith is
    port(
            op_code    : in std_logic_vector(1 downto 0);
            a_in        : in std_logic_vector(7 downto 0);
            b_in        : in std_logic_vector(7 downto 0);
            zero_det    : out std_logic;
            overflow    : out std_logic;
            alu_out    : out std_logic_vector(7 downto 0)                    
        );
end entity Arith;
architecture ALU of Arith is
signal Tmp_a    :    signed(a_in'range):=(others=>'0');
signal Tmp_b    :    signed(b_in'range):=(others=>'0');
signal Tmp_out    :    signed(alu_out'range):=(others=>'0');
begin
    Tmp_a <= signed(a_in);
    Tmp_b <= signed(b_in);
    
    ALU:process(Tmp_out, Tmp_a, Tmp_b)    
    begin
    
    
        case op_code is
            when "00" =>
            
                Tmp_out <= Tmp_a and Tmp_b;
                
            when "01" =>
            
                Tmp_out <=  Tmp_a or Tmp_b;
                
            when "10" =>
            
                Tmp_out <=  Tmp_a + Tmp_b;
                overflow <= detect_overflow(Tmp_out, Tmp_a, Tmp_b);
                zero_det <= detect_zero(Tmp_out);
                
            when "11" =>        
            
                Tmp_out <=    Tmp_a - Tmp_b;
                overflow <= detect_overflow(Tmp_out, Tmp_a, Tmp_b);
                zero_det <= detect_zero(Tmp_out);
                
            when others =>    
            
                null;
                
        end case;
        
            alu_out <= std_logic_vector(Tmp_out);
        
    end process;
    
    
end architecture ALU;

-- Copyright (C) 1991-2012 Altera Corporation
-- Your use of Altera Corporation's design tools, logic functions 
-- and other software and tools, and its AMPP partner logic 
-- functions, and any output files from any of the foregoing 
-- (including device programming or simulation files), and any 
-- associated documentation or information are expressly subject 
-- to the terms and conditions of the Altera Program License 
-- Subscription Agreement, Altera MegaCore Function License 
-- Agreement, or other applicable license agreement, including, 
-- without limitation, that your use is for the sole purpose of 
-- programming logic devices manufactured by Altera and sold by 
-- Altera or its authorized distributors.  Please refer to the 
-- applicable agreement for further details.
-- ***************************************************************************
-- This file contains a Vhdl test bench template that is freely editable to   
-- suit user's needs .Comments are provided in each section to help the user  
-- fill out necessary details.                                                
-- ***************************************************************************
-- Generated on "11/17/2012 12:17:48"
                                                            
-- Vhdl Test Bench template for design  :  Arith
-- 
-- Simulation tool : ModelSim-Altera (VHDL)
-- 
LIBRARY ieee;                                               
USE ieee.std_logic_1164.all;                                
ENTITY Arith_vhd_tst IS
END Arith_vhd_tst;
ARCHITECTURE Arith_arch OF Arith_vhd_tst IS
-- constants                                                 
-- signals                                                   
SIGNAL a_in : STD_LOGIC_VECTOR(7 DOWNTO 0);
SIGNAL b_in : STD_LOGIC_VECTOR(7 DOWNTO 0);
SIGNAL alu_out : STD_LOGIC_VECTOR(7 DOWNTO 0);
SIGNAL op_code : STD_LOGIC_VECTOR(1 DOWNTO 0);
SIGNAL overflow : STD_LOGIC;
SIGNAL zero_det : STD_LOGIC;
COMPONENT Arith
    PORT (
    a_in : IN STD_LOGIC_VECTOR(7 DOWNTO 0);
    alu_out : OUT STD_LOGIC_VECTOR(7 DOWNTO 0);
    b_in : IN STD_LOGIC_VECTOR(7 DOWNTO 0);
    op_code : IN STD_LOGIC_VECTOR(1 DOWNTO 0);
    overflow : OUT STD_LOGIC;
    zero_det : OUT STD_LOGIC
    );
END COMPONENT;
BEGIN
    i1 : Arith
    PORT MAP (
-- list connections between master ports and signals
    a_in => a_in,
    alu_out => alu_out,
    b_in => b_in,
    op_code => op_code,
    overflow => overflow,
    zero_det => zero_det
    );
    process
    begin
    
        op_code <="10";    
        wait for 300 ns;
        op_code <="11";
        wait for 300 ns;
    end process;
    
    process
    begin
    
        a_in <= "01111110";
        b_in <= "00000010";
        wait for 50 ns;
        a_in <= "00000010";
        b_in <= "00000010";
        wait for 50 ns;
        a_in <= "01111111";
        b_in <= "00000000";
        wait for 50 ns;
        a_in <= "01111111";
        b_in <= "10000000";
        wait for 50 ns;
        a_in <= "00011110";
        b_in <= "11011000";
        wait for 50 ns;
        a_in <= "00000010";
        b_in <= "11111110";
        wait for 50 ns;
        a_in <= "10000000"; -- Subtraktion härifrån
        b_in <= "00000010";
        wait for 50 ns;
        a_in <= "01111111";
        b_in <= "11111110";
        wait for 50 ns;
        a_in <= "00001000";
        b_in <= "00001000";
        wait for 50 ns;
        a_in <= "01111111";
        b_in <= "10000000";
        wait for 50 ns;
        a_in <= "00011110";
        b_in <= "11011000";
        wait for 50 ns;
        a_in <= "00000010";
        b_in <= "11111110";
        wait for 50 ns;
        
    end process;
    
END Arith;

17 Replies