ALU¶
This ALU was designed with hope as the main rules; I don’t even know how to look at it two years later. But let’s document it anyway !
alu¶
alu.vhd
alu module
Imports¶
Imports 4 elements
IEEE std_logic_1164 numeric_std
work common records
Parameters¶
Parameters 1 element
- • XLEN Type : INTEGER Default : 32
! @brief Configure the data width in the core. DOES NOT configure the instruction lenght, which is fixed to 32 bits.
Ports¶
Ports list 5 elements
Name |
Direction |
Description |
Type |
Size |
Kind |
Polarity |
Fanout |
|---|---|---|---|---|---|---|---|
arg1 |
Input |
! @brief Argument 2 input for the ALU logic. |
|
|
Combinatorial |
- |
Fanout : 0 |
arg2 |
Input |
! @brief Output of the ALU logic. |
|
|
Combinatorial |
- |
Fanout : 0 |
result |
Output |
|
|
|
Combinatorial |
- |
Fanout : 0 |
command |
Input |
|
|
- |
Combinatorial |
- |
Fanout : 0 |
status |
Output |
|
|
- |
Combinatorial |
- |
Fanout : 0 |
Processes¶
Process list 1 element
• P1 Combinatorial
- -
Signals¶
Signals list 4 elements
Name |
Description |
Type |
Size |
Kind |
Fanout |
|---|---|---|---|---|---|
tmp |
|
|
|
Combinatorial |
Fanout : 0 |
res |
|
|
|
Combinatorial |
Fanout : 0 |
v_ovf |
|
|
- |
Combinatorial |
Fanout : 0 |
highz_out |
|
|
|
Combinatorial |
Fanout : 0 |
Instantiation templates¶
Instantiation templates 2 elements
alu <name> #(
.XLEN ( 32)
)(
.arg1 (i_arg1),
.arg2 (i_arg2),
.result (i_result),
.command (i_command),
.status (i_status)
);
<name>: entity work.alu
generic map (
XLEN => 32
)
port map (
arg1 => i_arg1,
arg2 => i_arg2,
result => i_result,
command => i_command,
status => i_status
);
Source¶
Raw source Full file
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;
USE work.common.ALL;
USE work.records.ALL;
ENTITY alu IS
GENERIC (
--! @brief Configure the data width in the core. DOES NOT configure the instruction lenght, which is fixed to 32 bits.
XLEN : INTEGER := 32
);
PORT (
--------------------------------------------------------------------------------------------------------
-- Data I/O
--------------------------------------------------------------------------------------------------------
--! @brief Argument 1 input for the ALU logic.
arg1 : IN STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Argument 2 input for the ALU logic.
arg2 : IN STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0);
--! @brief Output of the ALU logic.
result : OUT STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0) := (OTHERS => 'Z');
--------------------------------------------------------------------------------------------------------
-- Controls
--------------------------------------------------------------------------------------------------------
--! @brief Command of the ALU, which operation need to be done.
command : IN commands;
--------------------------------------------------------------------------------------------------------
-- Status
--------------------------------------------------------------------------------------------------------
--! @brief ALU status (zero, overflow...) output.
status : OUT alu_feedback
);
END ENTITY;
ARCHITECTURE behavioral OF alu IS
BEGIN
--========================================================================================
--! @brief Process that is executed on changed of the input arg, and trigger a new
--! calculation.
--! Most of the operations are based on bare VHDL implementations.
--========================================================================================
P1 : PROCESS (arg1, arg2, command)
VARIABLE tmp : signed(XLEN - 1 DOWNTO 0);
VARIABLE res : STD_LOGIC_VECTOR(XLEN - 1 DOWNTO 0);
VARIABLE v_ovf : STD_LOGIC := '0';
VARIABLE highz_out : STD_LOGIC_VECTOR((XLEN - 1) DOWNTO 0) := (OTHERS => 'Z');
BEGIN
res := (OTHERS => '0');
v_ovf := '0';
CASE command IS
WHEN c_ADD =>
tmp := signed(arg1) + signed(arg2);
res := STD_LOGIC_VECTOR(tmp);
-- signed overflow detection
IF (arg1(XLEN - 1) = arg2(XLEN - 1)) AND (res(XLEN - 1) /= arg1(XLEN - 1)) THEN
v_ovf := '1';
END IF;
WHEN c_SUB =>
tmp := signed(arg1) - signed(arg2);
res := STD_LOGIC_VECTOR(tmp);
-- signed overflow detection
IF (arg1(XLEN - 1) /= arg2(XLEN - 1)) AND (res(XLEN - 1) /= arg1(XLEN - 1)) THEN
v_ovf := '1';
END IF;
WHEN c_SLL =>
res := STD_LOGIC_VECTOR(shift_left(unsigned(arg1), to_integer(unsigned(arg2(4 DOWNTO 0)))));
WHEN c_SRL =>
res := STD_LOGIC_VECTOR(shift_right(unsigned(arg1), to_integer(unsigned(arg2(4 DOWNTO 0)))));
WHEN c_SRA =>
res := STD_LOGIC_VECTOR(shift_right(signed(arg1), to_integer(unsigned(arg2(4 DOWNTO 0)))));
WHEN c_AND =>
res := arg1 AND arg2;
WHEN c_OR =>
res := arg1 OR arg2;
WHEN c_XOR =>
res := arg1 XOR arg2;
WHEN c_SLT =>
IF signed(arg1) < signed(arg2) THEN
res := (OTHERS => '0');
res(0) := '1';
ELSE
res := (OTHERS => '0');
END IF;
WHEN c_SLTU =>
IF unsigned(arg1) < unsigned(arg2) THEN
res := (OTHERS => '0');
res(0) := '1';
ELSE
res := (OTHERS => '0');
END IF;
WHEN OTHERS =>
res := (OTHERS => '0');
END CASE;
-- outputs assignment
result <= res;
status.overflow <= v_ovf;
IF (unsigned(res) = 0) AND (command /= c_None) THEN -- This make c_NONE be used for branchs evaluations.
status.zero <= '1';
ELSE
status.zero <= '0';
END IF;
-- Jumps condition checks
IF (unsigned(arg1) = unsigned(arg2)) THEN
status.beq <= '1';
status.bne <= '0';
ELSE
status.beq <= '0';
status.bne <= '1';
END IF;
IF (unsigned(arg1) < unsigned(arg2)) THEN
status.bltu <= '1';
status.bgeu <= '0';
ELSE
status.bltu <= '0';
status.bgeu <= '1';
END IF;
IF (signed(arg1) < signed(arg2)) THEN
status.blt <= '1';
status.bge <= '0';
ELSE
status.blt <= '0';
status.bge <= '1';
END IF;
END PROCESS;
END ARCHITECTURE;
alu_tb¶
alu_tb.vhd
alu_tb testbench
Imports¶
Imports 3 elements
IEEE std_logic_1164 numeric_std
work common
Signals¶
Signals list 12 elements
Name |
Description |
Type |
Size |
Kind |
Fanout |
|---|---|---|---|---|---|
arg1_t |
|
|
|
Combinatorial |
Fanout : 0 |
arg2_t |
|
|
|
Combinatorial |
Fanout : 0 |
result_t |
|
|
|
Combinatorial |
Fanout : 0 |
command_t |
|
|
- |
Combinatorial |
Fanout : 0 |
outen_t |
|
|
- |
Combinatorial |
Fanout : 0 |
overflow_t |
|
|
- |
Combinatorial |
Fanout : 0 |
beq_t |
|
|
- |
Combinatorial |
Fanout : 0 |
bne_t |
|
|
- |
Combinatorial |
Fanout : 0 |
blt_t |
|
|
- |
Combinatorial |
Fanout : 0 |
bge_t |
|
|
- |
Combinatorial |
Fanout : 0 |
bltu_t |
|
|
- |
Combinatorial |
Fanout : 0 |
bgeu_t |
|
|
- |
Combinatorial |
Fanout : 0 |
Modules¶
Processes¶
Process list 3 elements
• P1
Instructions cycles
• P2
Input controls
• P3
Output enable control
Source¶
Raw source Full file
-- recommended sim lenght : 5 us
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.numeric_std.ALL;
USE work.common.ALL;
ENTITY alu_tb IS
END ENTITY;
ARCHITECTURE behavioral OF alu_tb IS
SIGNAL arg1_t : STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '0');
SIGNAL arg2_t : STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '0');
SIGNAL result_t : STD_LOGIC_VECTOR(31 DOWNTO 0) := (OTHERS => '0');
SIGNAL command_t : commands := c_NONE;
SIGNAL outen_t : STD_LOGIC := '0';
SIGNAL overflow_t : STD_LOGIC := '0';
SIGNAL beq_t : STD_LOGIC := '0';
SIGNAL bne_t : STD_LOGIC := '0';
SIGNAL blt_t : STD_LOGIC := '0';
SIGNAL bge_t : STD_LOGIC := '0';
SIGNAL bltu_t : STD_LOGIC := '0';
SIGNAL bgeu_t : STD_LOGIC := '0';
BEGIN
U1 : ENTITY work.alu(behavioral)
GENERIC MAP(
XLEN => 32
)
PORT MAP(
arg1 => arg1_t,
arg2 => arg2_t,
result => result_t,
command => command_t,
outen => outen_t,
overflow => overflow_t,
beq => beq_t,
bne => bne_t,
bge => bge_t,
blt => blt_t,
bltu => bltu_t,
bgeu => bgeu_t
);
-- Instructions cycles
P1 : PROCESS
BEGIN
command_t <= c_ADD;
WAIT FOR 140 ns;
command_t <= c_SUB;
WAIT FOR 140 ns;
command_t <= c_AND;
WAIT FOR 140 ns;
command_t <= c_OR;
WAIT FOR 140 ns;
command_t <= c_XOR;
WAIT FOR 140 ns;
command_t <= c_SLL;
WAIT FOR 140 ns;
command_t <= c_SRL;
WAIT FOR 140 ns;
command_t <= c_SRA;
WAIT FOR 140 ns;
command_t <= c_SLT;
WAIT FOR 140 ns;
command_t <= c_SLTU;
WAIT FOR 140 ns;
command_t <= c_NONE;
WAIT FOR 140 ns;
END PROCESS;
-- Input controls
P2 : PROCESS
BEGIN
-- Standard operations
arg1_t <= STD_LOGIC_VECTOR(to_signed(12, arg1_t'length));
arg2_t <= STD_LOGIC_VECTOR(to_signed(5, arg2_t'length));
WAIT FOR 20 ns;
arg1_t <= STD_LOGIC_VECTOR(to_signed(-10, arg1_t'length));
arg2_t <= STD_LOGIC_VECTOR(to_signed(-5, arg2_t'length));
WAIT FOR 20 ns;
-- Zero crossing
arg1_t <= STD_LOGIC_VECTOR(to_signed(3, arg1_t'length));
arg2_t <= STD_LOGIC_VECTOR(to_signed(-5, arg2_t'length));
WAIT FOR 20 ns;
arg1_t <= STD_LOGIC_VECTOR(to_signed(-3, arg1_t'length));
arg2_t <= STD_LOGIC_VECTOR(to_signed(-5, arg2_t'length));
WAIT FOR 20 ns;
-- Overflows
arg1_t <= STD_LOGIC_VECTOR(to_signed(2147483640, arg1_t'length));
arg2_t <= STD_LOGIC_VECTOR(to_signed(64, arg2_t'length));
WAIT FOR 20 ns;
arg1_t <= STD_LOGIC_VECTOR(to_signed(-64, arg1_t'length));
arg2_t <= STD_LOGIC_VECTOR(to_signed(2147483640, arg2_t'length));
WAIT FOR 20 ns;
arg1_t <= STD_LOGIC_VECTOR(to_signed(128, arg1_t'length));
arg2_t <= STD_LOGIC_VECTOR(to_signed(128, arg2_t'length));
WAIT FOR 20 ns;
END PROCESS;
-- Output enable control
P3 : PROCESS
BEGIN
outen_t <= '1';
WAIT FOR 1540 ns;
outen_t <= '0';
WAIT FOR 1540 ns;
END PROCESS;
END ARCHITECTURE;