MIPS32 in VHDL
Posted: Mon Dec 02, 2024 8:22 am
32 Bit Ripple Carry Chain Addierer
Subtrahierer:
19219 - 14823 = 4396 ^= 0x112C
14823 - 19219 = -4396 ^= 0xFFFFFFFFFFFFEED4
Code: Select all
-- (C) David Vajda
-- 32 Bit Ripple Carry Chain Adder / Subtrahierer / Volladdierer
-- 2024-12-02
library ieee;
use ieee.std_logic_1164.all;
entity fulladder is
port (
a: in std_logic;
b: in std_logic;
c: out std_logic;
s: in std_logic;
t: out std_logic
);
end;
architecture behaviour of fulladder is
begin
c <= a xor b xor s;
t <= (a and b) or ((a or b) and s);
end;
library ieee;
use ieee.std_logic_1164.all;
entity ripplecarrychainadder32 is
port (
a: in std_logic_vector (31 downto 0);
b: in std_logic_vector (31 downto 0);
c: out std_logic_vector (31 downto 0);
s: in std_logic;
t: out std_logic
);
end;
architecture behaviour of ripplecarrychainadder32 is
component fulladder
port (
a: in std_logic;
b: in std_logic;
c: out std_logic;
s: in std_logic;
t: out std_logic
);
end component;
signal x: std_logic_vector (32 downto 0);
begin
fa_loop: FOR i IN 31 DOWNTO 0 GENERATE
fa: fulladder PORT MAP (a=>a(i), b=>b(i), c=>c(i), s=>x(i), t=>x(i+1));
END GENERATE;
t <= x (32) ;
x (0) <= s;
--fa3: fulladder20241128 PORT MAP (a=>a3, b=>b3, c=>c3, s=>s3, t=>t);
--fa2: fulladder20241128 PORT MAP (a=>a2, b=>b2, c=>c2, s=>s2, t=>s3);
--fa1: fulladder20241128 PORT MAP (a=>a1, b=>b1, c=>c1, s=>s1, t=>s2);
--fa0: fulladder20241128 PORT MAP (a=>a0, b=>b0, c=>c0, s=>s, t=>s1);
end;
library ieee;
use ieee.std_logic_1164.all;
entity ripplecarrychainadder32testbench is
port (
c: out std_logic_vector (31 downto 0);
t: out std_logic
);
end;
architecture behaviour of ripplecarrychainadder32testbench is
component ripplecarrychainadder32
port (
a: in std_logic_vector (31 downto 0);
b: in std_logic_vector (31 downto 0);
c: out std_logic_vector (31 downto 0);
s: in std_logic;
t: out std_logic
);
end component;
signal a: std_logic_vector (31 downto 0);
signal b: std_logic_vector (31 downto 0);
signal s: std_logic;
begin
rplcadd: ripplecarrychainadder32 PORT MAP (c=>c, b=>b, a=>a, s=>s, t=>t);
-- 19219 + 14823 = 34042
-- 0b100101100010011 + 0b11100111100111 = 0b1000010011111010
a <= ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','1','0','0', '1','0','1','1', '0','0','0','1' ,'0','0','1','1') after 0 ns, ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','1','0','0', '1','0','1','1', '0','0','0','1' ,'0','0','1','1') after 20 ns;
b <= ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','1','1', '1','0','0','1', '1','1','1','0', '0','1','1','1') after 0 ns, ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','1','1', '1','0','0','1', '1','1','1','0', '0','1','1','1') after 20 ns;
s <= '0';
end;
Code: Select all
-- (C) David Vajda
-- 32 Bit Ripple Carry Chain Adder / Subtrahierer / Volladdierer
-- 2024-12-02
library ieee;
use ieee.std_logic_1164.all;
entity fulladder is
port (
a: in std_logic;
b: in std_logic;
c: out std_logic;
s: in std_logic;
t: out std_logic
);
end;
architecture behaviour of fulladder is
begin
c <= a xor b xor s;
t <= (a and b) or ((a or b) and s);
end;
library ieee;
use ieee.std_logic_1164.all;
entity ripplecarrychainadder32 is
port (
a: in std_logic_vector (31 downto 0);
b: in std_logic_vector (31 downto 0);
c: out std_logic_vector (31 downto 0);
s: in std_logic;
t: out std_logic
);
end;
architecture behaviour of ripplecarrychainadder32 is
component fulladder
port (
a: in std_logic;
b: in std_logic;
c: out std_logic;
s: in std_logic;
t: out std_logic
);
end component;
signal x: std_logic_vector (32 downto 0);
begin
fa_loop: FOR i IN 31 DOWNTO 0 GENERATE
fa: fulladder PORT MAP (a=>a(i), b=>b(i), c=>c(i), s=>x(i), t=>x(i+1));
END GENERATE;
t <= x (32) ;
x (0) <= s;
--fa3: fulladder20241128 PORT MAP (a=>a3, b=>b3, c=>c3, s=>s3, t=>t);
--fa2: fulladder20241128 PORT MAP (a=>a2, b=>b2, c=>c2, s=>s2, t=>s3);
--fa1: fulladder20241128 PORT MAP (a=>a1, b=>b1, c=>c1, s=>s1, t=>s2);
--fa0: fulladder20241128 PORT MAP (a=>a0, b=>b0, c=>c0, s=>s, t=>s1);
end;
library ieee;
use ieee.std_logic_1164.all;
entity ripplecarrychainadder32testbench is
port (
c: out std_logic_vector (31 downto 0);
t: out std_logic
);
end;
architecture behaviour of ripplecarrychainadder32testbench is
component ripplecarrychainadder32
port (
a: in std_logic_vector (31 downto 0);
b: in std_logic_vector (31 downto 0);
c: out std_logic_vector (31 downto 0);
s: in std_logic;
t: out std_logic
);
end component;
signal a: std_logic_vector (31 downto 0);
signal b: std_logic_vector (31 downto 0);
signal s: std_logic;
begin
rplcadd: ripplecarrychainadder32 PORT MAP (c=>c, b=>b, a=>a, s=>s, t=>t);
-- 19219 + 14823 = 34042
-- 0b100101100010011 + 0b11100111100111 = 0b1000010011111010
a <= ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','1','0','0', '1','0','1','1', '0','0','0','1' ,'0','0','1','1') after 0 ns, ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','1','0','0', '1','0','1','1', '0','0','0','1' ,'0','0','1','1') after 20 ns;
b <= ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','1','1', '1','0','0','1', '1','1','1','0', '0','1','1','1') after 0 ns, ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','1','1', '1','0','0','1', '1','1','1','0', '0','1','1','1') after 20 ns;
s <= '0';
end;
library ieee;
use ieee.std_logic_1164.all;
entity com32 is
port (
x: in std_logic_vector (31 downto 0);
y: out std_logic_vector (31 downto 0)
);
end;
architecture behaviour of com32 is
begin
com_connect: FOR i IN 31 DOWNTO 0 GENERATE
y (i) <= not x (i);
END GENERATE;
end;
library ieee;
use ieee.std_logic_1164.all;
entity neg32 is
port (
b: out std_logic_vector (31 downto 0);
a: in std_logic_vector (31 downto 0);
t: out std_logic
);
end;
architecture behaviour of neg32 is
component com32
port (
x: in std_logic_vector (31 downto 0);
y: out std_logic_vector (31 downto 0)
);
end component;
component ripplecarrychainadder32
port (
a: in std_logic_vector (31 downto 0);
b: in std_logic_vector (31 downto 0);
c: out std_logic_vector (31 downto 0);
s: in std_logic;
t: out std_logic
);
end component;
signal c: std_logic_vector (31 downto 0);
signal d: std_logic_vector (31 downto 0);
signal s: std_logic;
begin
neg32_generate_1: FOR i IN 31 DOWNTO 0 GENERATE
d (i) <= '0';
END GENERATE;
s <= '1';
com: com32 PORT MAP (x=>a, y=>c);
add: ripplecarrychainadder32 PORT MAP (b=>d, a=>c, s=>s, t=>t, c=>b);
end;
library ieee;
use ieee.std_logic_1164.all;
entity sub32 is
port (
a: in std_logic_vector (31 downto 0);
b: in std_logic_vector (31 downto 0);
c: out std_logic_vector (31 downto 0);
t: out std_logic
);
end;
architecture behaviour of sub32 is
component ripplecarrychainadder32
port (
a: in std_logic_vector (31 downto 0);
b: in std_logic_vector (31 downto 0);
c: out std_logic_vector (31 downto 0);
s: in std_logic;
t: out std_logic
);
end component;
component neg32
port (
b: out std_logic_vector (31 downto 0);
a: in std_logic_vector (31 downto 0);
t: out std_logic
);
end component;
signal x: std_logic_vector (31 downto 0);
signal s: std_logic;
begin
neg: neg32 PORT MAP (a=>a, b=>x);
add: ripplecarrychainadder32 PORT MAP (b=>b, a=>x, c=>c, s=>s, t=>t);
s <= '0';
end;
library ieee;
use ieee.std_logic_1164.all;
entity sub32testbench is
port (
c: out std_logic_vector (31 downto 0);
t: out std_logic
);
end;
architecture behaviour of sub32testbench is
component sub32
port (
a: in std_logic_vector (31 downto 0);
b: in std_logic_vector (31 downto 0);
c: out std_logic_vector (31 downto 0);
t: out std_logic
);
end component;
signal a: std_logic_vector (31 downto 0);
signal b: std_logic_vector (31 downto 0);
signal s: std_logic;
begin
sub: sub32 PORT MAP (a=>a, b=>b, c=>c, t=>t);
a <= ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','1','0','0', '1','0','1','1', '0','0','0','1' ,'0','0','1','1') after 0 ns, ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','1','1', '1','0','0','1', '1','1','1','0', '0','1','1','1') after 10 ns, ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','1','1', '1','0','0','1', '1','1','1','0', '0','1','1','1') after 20 ns;
b <= ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','1','1', '1','0','0','1', '1','1','1','0', '0','1','1','1') after 0 ns, ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','1','0','0', '1','0','1','1', '0','0','0','1' ,'0','0','1','1') after 10 ns, ('0','0','0','0', '0','0','0','0', '0','0','0','0', '0','0','0','0', '0','1','0','0', '1','0','1','1', '0','0','0','1' ,'0','0','1','1') after 20 ns;
s <= '0' after 0 ns, '1' after 10 ns;
-- 19219 - 14823 =
end;
14823 - 19219 = -4396 ^= 0xFFFFFFFFFFFFEED4