code for signed alu unit
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.numeric_std.ALL;
use work.dsp_pac.all;
use work.alu_types.all;
entity alu_L is
port(s1: in DM_data; -- dm_data is unsigned(15 downto 0);
s2: in DM_data;
result : out DM_data;
func : in alu_func;
zero, negative,carry_out: out bit);
end entity alu_L;
architecture Behavioral of alu_L is
begin
alu_op: process(s1,s2,func) is
procedure add ( L,R : in DM_data;
result: out DM_data;
carry_out : out bit;
carry_in : in bit;
signed : in boolean) is
variable carry : bit := carry_in;
variable carry_prev : bit;
begin
for index in result'reverse_range loop
carry_prev := carry;
result(index) := L(index) xor R(index) xor carry;
carry := (L(index) and R(index)) or (carry and (L(index) xor R(index)));
end loop;
if signed then
carry_out:= carry xor carry_prev;
else
carry_out := carry;
end if;
end procedure add;
variable temp_result : DM_data;
variable temp_carry: bit;
begin
temp_carry := '0';
case func is
when alu_add =>
add(s1, s2, temp_result, temp_carry,
carry_in => '0', signed =>true);
when alu_addc =>
add(s1, s2, temp_result, temp_carry,
carry_in => '1', signed => true);
when alu_sub =>
add(s1, not s2, temp_result, temp_carry,
carry_in => '0', signed => true);
when alu_subc =>
add(s1,not s2, temp_result, temp_carry,
carry_in => '1', signed => true);
when others =>
report "illegal function code" severity error;
temp_result := x"0000";
end case;
result <= temp_result;
zero <= bit'val (boolean'pos(temp_result=X"0000")) ;
negative <=temp_result(15);
carry_out<= temp_carry;
end process alu_op;
end architecture Behavioral;