发表于
2007-4-17 22:52:27
网上找来了ALU的VHDL代码:
alu 1.1 vhd
--
-- Project
-- pAVR (pipelined AVR) is an 8 bit RISC controller, compatible with Atmel's
-- AVR core, but about 3x faster in terms of both clock frequency and MIPS.
-- The increase in speed comes from a relatively deep pipeline. The original
-- AVR core has only two pipeline stages (fetch and execute), while pAVR has
-- 6 pipeline stages:
-- 1. PM (read Program Memory)
-- 2. INSTR (load Instruction)
-- 3. RFRD (decode Instruction and read Register File)
-- 4. OPS (load Operands)
-- 5. ALU (execute ALU opcode or access Unified Memory)
-- 6. RFWR (write Register File)
-- Version
-- 0.32
-- Date
-- 2002 August 07
-- Author
-- Doru Cuturela, doruu@yahoo.com
-- License
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--
--
-- This is pAVR's ALU.
-- The ALU asychronousely computes:
-- - output
-- - output flags,
-- based on:
-- - input 1
-- - input 2
-- - input flags
-- Flags:
-- - C (cary)
-- - Z (zero)
-- - N (negative)
-- - V (two's complement overflow)
-- - S (N xor V, for signed tests)
-- - H (half carry)
-- *** The half carry is computed as specified in the AVR instruction set.
-- However, Atmel's AVRStudio computes it differently. To see where is the
-- bug, in the AVR instruction set document or in AVRStudio.
--
--
library work;
use work.std_util.all;
use work.pavr_util.all;
use work.pavr_constants.all;
library ieee;
use ieee.std_logic_1164.all;
entity pavr_alu is
port(
pavr_alu_op1: in std_logic_vector(15 downto 0);
pavr_alu_op2: in std_logic_vector(7 downto 0);
pavr_alu_out: out std_logic_vector(15 downto 0);
pavr_alu_opcode: in std_logic_vector(pavr_alu_opcode_w - 1 downto 0);
pavr_alu_flagsin: in std_logic_vector(5 downto 0);
pavr_alu_flagsout: out std_logic_vector(5 downto 0)
);
end;
architecture pavr_alu_arch of pavr_alu is
-- Wires
signal tmp10_1, tmp10_2, tmp10_3 : std_logic_vector(9 downto 0);
signal tmp18_1, tmp18_2, tmp18_3 : std_logic_vector(17 downto 0);
signal pavr_alu_h_sel: std_logic_vector(pavr_alu_h_sel_w - 1 downto 0);
signal pavr_alu_s_sel: std_logic;
signal pavr_alu_v_sel: std_logic_vector(pavr_alu_v_sel_w - 1 downto 0);
signal pavr_alu_n_sel: std_logic_vector(pavr_alu_n_sel_w - 1 downto 0);
signal pavr_alu_z_sel: std_logic_vector(pavr_alu_z_sel_w - 1 downto 0);
signal pavr_alu_c_sel: std_logic_vector(pavr_alu_c_sel_w - 1 downto 0);
signal pavr_alu_out_int: std_logic_vector(15 downto 0);
signal pavr_alu_flagsout_int: std_logic_vector(5 downto 0);
-- Registers
-- No registers
begin
-- Compute ALU output and selectors for flags muxers.
alu_out:
process(pavr_alu_op1, pavr_alu_op2, pavr_alu_out_int, pavr_alu_opcode, pavr_alu_flagsin,
tmp10_1, tmp10_2, tmp10_3,
tmp18_1, tmp18_2, tmp18_3
)
begin
-- Default ALU output to 0.
pavr_alu_out_int <= int_to_std_logic_vector(0, pavr_alu_out_int'length);
-- Default 8 bit adders's operands to ls8bits(operand1), operand2, carry in and carry out to 0.
tmp10_1(0) <= '0';
tmp10_2(0) <= '0';
tmp10_1(8 downto 1) <= pavr_alu_op1(7 downto 0);
tmp10_2(8 downto 1) <= pavr_alu_op2(7 downto 0);
tmp10_1(9) <= '0';
tmp10_2(9) <= '0';
-- Default 16 bit adders's operands to operand1, signExtendTo16bits(operand2), carry in and carry out to 0.
tmp18_1(0) <= '0';
tmp18_2(0) <= '0';
tmp18_1(16 downto 1) <= pavr_alu_op1(15 downto 0);
tmp18_2(16 downto 1) <= sign_extend(pavr_alu_op2, 16);
tmp18_1(17) <= '0';
tmp18_2(17) <= '0';
-- Default adders's outputs
tmp10_3 <= int_to_std_logic_vector(0, tmp10_3'length);
tmp18_3 <= int_to_std_logic_vector(0, tmp18_3'length);
-- Default flags out to flags in.
pavr_alu_h_sel <= pavr_alu_h_sel_same;
pavr_alu_s_sel <= pavr_alu_s_sel_same;
pavr_alu_v_sel <= pavr_alu_v_sel_same;
pavr_alu_n_sel <= pavr_alu_n_sel_same;
pavr_alu_z_sel <= pavr_alu_z_sel_same;
pavr_alu_c_sel <= pavr_alu_c_sel_same;
-- Build ALU output.
case std_logic_vector_to_nat(pavr_alu_opcode) is
when pavr_alu_opcode_add8 =>
tmp10_3 <= tmp10_1 + tmp10_2;
pavr_alu_out_int(7 downto 0) <= tmp10_3(8 downto 1);
pavr_alu_h_sel <= pavr_alu_h_sel_add8;
pavr_alu_s_sel <= pavr_alu_s_sel_nxorv;
pavr_alu_v_sel <= pavr_alu_v_sel_add8;
pavr_alu_n_sel <= pavr_alu_n_sel_msb8;
pavr_alu_z_sel <= pavr_alu_z_sel_z8;
pavr_alu_c_sel <= pavr_alu_c_sel_add8;
when pavr_alu_opcode_adc8 =>
tmp10_1(0) <= pavr_alu_flagsin(0);
tmp10_2(0) <= pavr_alu_flagsin(0);
tmp10_3 <= tmp10_1 + tmp10_2;
pavr_alu_out_int(7 downto 0) <= tmp10_3(8 downto 1);
pavr_alu_h_sel <= pavr_alu_h_sel_a