-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(with subtraction flag)
- Loading branch information
Showing
6 changed files
with
123 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
-- Inspired by the pinout of DM74LS283, 4-bit binary full adder with fast carry | ||
|
||
library ieee; | ||
use ieee.std_logic_1164.all; | ||
use ieee.numeric_std.all; | ||
|
||
entity ALU is | ||
port ( | ||
i_A, i_B : in std_logic_vector(7 downto 0); -- 8-bit input registers | ||
o_Out : out std_logic_vector(7 downto 0); -- 8-bit output register | ||
o_Carry : out std_logic; -- Carry output | ||
i_Subtract : in std_logic -- Subtract flag (if 1, subtract, if 0, add) | ||
); | ||
end ALU; | ||
|
||
architecture rtl of ALU is | ||
signal w_Carry_Tmp : std_logic_vector(8 downto 0); | ||
signal w_B: std_logic_vector(7 downto 0); | ||
begin | ||
|
||
w_B <= std_logic_vector(unsigned(not i_B) + 1) when i_Subtract = '1' else i_B; -- Two's complement | ||
|
||
o_Out <= std_logic_vector(unsigned(i_A) + unsigned(w_B)); | ||
|
||
-- o_Out <= std_logic_vector(unsigned(i_A) + unsigned(i_B)) when i_Subtract = '0' else std_logic_vector(unsigned(i_A) - unsigned(i_B)); | ||
|
||
w_Carry_Tmp <= std_logic_vector(unsigned('0' & i_A) + unsigned('0' & i_B)); | ||
o_Carry <= w_Carry_Tmp(8); | ||
end rtl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
library ieee; | ||
use ieee.std_logic_1164.all; | ||
use ieee.numeric_std.all; | ||
|
||
library vunit_lib; | ||
context vunit_lib.vunit_context; | ||
|
||
entity ALU_TB is | ||
generic (runner_cfg : string); | ||
end entity; | ||
|
||
architecture tb of ALU_TB is | ||
|
||
function num(val : in integer) return std_logic_vector is | ||
begin | ||
return std_logic_vector(to_unsigned(val, 8)); | ||
end function num; | ||
|
||
signal w_A, w_B, w_Out : std_logic_vector(7 downto 0); | ||
signal w_Carry : std_logic; | ||
signal r_Subtract : std_logic := '0'; | ||
begin | ||
-- Instantiate the UUT | ||
UUT : entity work.ALU | ||
port map( | ||
i_A => w_A, | ||
i_B => w_B, | ||
o_Out => w_Out, | ||
o_Carry => w_Carry, | ||
i_Subtract => r_Subtract | ||
); | ||
|
||
main : process | ||
begin | ||
test_runner_setup(runner, runner_cfg); | ||
|
||
-- Simulation starts here | ||
w_A <= num(1); | ||
w_B <= num(1); | ||
wait for 10 ns; | ||
assert w_Out = num(2) report "Error: 1 + 1 != 2" severity failure; | ||
assert w_Carry = '0' report "Error: carry set" severity failure; | ||
|
||
w_A <= num(3); -- Dec -1 | ||
w_B <= num(5); -- Dec 3 | ||
wait for 10 ns; | ||
assert w_Out = num(8) report "Error: 3 + 5 != 8" severity failure; | ||
assert w_Carry = '0' report "Error: carry set" severity failure; | ||
|
||
w_A <= num(255); | ||
w_B <= num(1); | ||
wait for 10 ns; | ||
assert w_Out = num(0) report "Error: 0xFF + 0x01 != 0x00" severity failure; | ||
assert w_Carry = '1' report "Error: carry not set" severity failure; | ||
|
||
w_A <= num(255); | ||
w_B <= num(255); | ||
wait for 10 ns; | ||
assert w_Out = num(254) report "Error: 0xFF + 0xFF != 0xFE" severity failure; | ||
assert w_Carry = '1' report "Error: carry not set" severity failure; | ||
|
||
w_A <= "01111111"; -- Dec 127 | ||
w_B <= "00000001"; -- Dec 1 | ||
wait for 10 ns; | ||
assert w_Out = "10000000" report "Error: 127 + 1 != 128" severity failure; | ||
assert w_Carry = '0' report "Error: carry set" severity failure; | ||
|
||
-- Subtraction | ||
r_Subtract <= '1'; | ||
wait for 10 ns; | ||
|
||
w_A <= "00000011"; | ||
w_B <= "00000010"; | ||
wait for 10 ns; | ||
assert w_Out = "00000001" report "Error: 3 - 2 != 1" severity failure; | ||
assert w_Carry = '0' report "Error: carry set" severity failure; | ||
|
||
w_A <= "11000000"; | ||
w_B <= "11111110"; | ||
wait for 10 ns; | ||
assert w_Out = "11000010" report "Error: (-64) - (-2) != (-62)" severity failure; | ||
assert w_Carry = '1' report "Error: carry set" severity failure; | ||
|
||
wait for 10 ns; | ||
|
||
test_runner_cleanup(runner); -- Simulation ends here | ||
end process; | ||
end architecture; |