------------------------------------------------------------
-- Copyright Mentor Graphic Corporation 1991. 
-- All rights reserved.  
------------------------------------------------------------
--
--  Model Title: package for cpu description 
--  Date Created: 94/09/06 (TUE)
--  Author:       T. Ohtsuka
--
------------------------------------------------------------
-- Model Description: 
--
-----------------------------------------------------------
--
LIBRARY IEEE,work ;
USE IEEE.STD_LOGIC_1164.ALL ;
USE IEEE.STD_LOGIC_1164_EXTENSIONS.ALL ;


PACKAGE cpu_package IS

   CONSTANT Cycle       : TIME := 200 ns ;
   CONSTANT Word_width  : POSITIVE := 8 ;
   CONSTANT Addr_size   : POSITIVE := 8 ;
   CONSTANT Mem_size    : POSITIVE := 8 ;
   CONSTANT Z           : STD_LOGIC_VECTOR(Word_width-1 DOWNTO 0) := "ZZZZZZZZ" ;
   CONSTANT Alu_ctl_width : POSITIVE := 5 ;
   CONSTANT C_ctl_width : POSITIVE := 3 ;

   SUBTYPE Wordtype   IS STD_LOGIC_VECTOR(Word_width-1 DOWNTO 0) ;
   SUBTYPE Bustype    IS STD_LOGIC_VECTOR(Word_width-1 DOWNTO 0) ;
   SUBTYPE Regtype    IS STD_LOGIC_VECTOR(Word_width-1 DOWNTO 0) ;
   SUBTYPE Aluctltype IS STD_LOGIC_VECTOR(Alu_ctl_width-1 DOWNTO 0) ;
   SUBTYPE Cctltype   IS STD_LOGIC_VECTOR(C_ctl_width-1 DOWNTO 0) ;

   FUNCTION P_rising(SIGNAL clk : STD_LOGIC) RETURN BOOLEAN ;
   FUNCTION P_falling(SIGNAL clk : STD_LOGIC) RETURN BOOLEAN ;
   FUNCTION Csar_int(CONSTANT csar : STD_LOGIC_VECTOR(11 DOWNTO 0)) RETURN integer ;
   FUNCTION Mar_int(CONSTANT addr : STD_LOGIC_VECTOR(7 DOWNTO 0)) RETURN integer ;
   FUNCTION Sw_rising(SIGNAL sw : STD_LOGIC) RETURN BOOLEAN ;

END cpu_package ;

-- ---------------------------------------------------------
--Copyright Mentor Graphic Corporation 1991.
--All rights reserved.
-- ---------------------------------------------------------
--Arch. Body for entity declared in 
------------------------------------------------------------
--
LIBRARY IEEE,ARITHMETIC,work ;
USE IEEE.STD_LOGIC_1164.ALL ;
USE IEEE.STD_LOGIC_1164_EXTENSIONS.ALL ;

USE work.ALL ;

 
PACKAGE BODY cpu_package IS
   
   FUNCTION P_rising(SIGNAL clk : STD_LOGIC) RETURN BOOLEAN IS
   BEGIN
    IF clk = '1' AND clk'LAST_VALUE = '0' AND clk'EVENT THEN
      RETURN TRUE ;
    ELSE
      RETURN FALSE ;
    END IF ;
   END P_rising ;

   FUNCTION P_falling(SIGNAL clk : STD_LOGIC) RETURN BOOLEAN IS
   BEGIN
    IF clk = '0' AND clk'LAST_VALUE = '1' AND clk'EVENT THEN
      RETURN TRUE ;
    ELSE
      RETURN FALSE ;
    END IF ;
   END P_falling ;

   FUNCTION Csar_int(CONSTANT    csar : IN STD_LOGIC_VECTOR(11 DOWNTO 0))
      RETURN integer IS
   VARIABLE    i_out : integer := 0 ;
   BEGIN
     IF csar(11) = '1' THEN i_out := i_out + 2048 ; END IF ;
     IF csar(10) = '1' THEN i_out := i_out + 1024 ; END IF ;
     IF csar( 9) = '1' THEN i_out := i_out +  512 ; END IF ;
     IF csar( 8) = '1' THEN i_out := i_out +  256 ; END IF ;
     IF csar( 7) = '1' THEN i_out := i_out +  128 ; END IF ;
     IF csar( 6) = '1' THEN i_out := i_out +   64 ; END IF ;
     IF csar( 5) = '1' THEN i_out := i_out +   32 ; END IF ;
     IF csar( 4) = '1' THEN i_out := i_out +   16 ; END IF ;
     IF csar( 3) = '1' THEN i_out := i_out +    8 ; END IF ;
     IF csar( 2) = '1' THEN i_out := i_out +    4 ; END IF ;
     IF csar( 1) = '1' THEN i_out := i_out +    2 ; END IF ;
     IF csar( 0) = '1' THEN i_out := i_out +    1 ; END IF ;
     RETURN i_out ;
   END Csar_int ;

   FUNCTION Mar_int(CONSTANT     addr : IN STD_LOGIC_VECTOR(7 DOWNTO 0))
      RETURN integer IS
   VARIABLE    i_out : integer := 0 ;
   BEGIN
     IF addr( 7) = '1' THEN i_out := i_out +  128 ; END IF ;
     IF addr( 6) = '1' THEN i_out := i_out +   64 ; END IF ;
     IF addr( 5) = '1' THEN i_out := i_out +   32 ; END IF ;
     IF addr( 4) = '1' THEN i_out := i_out +   16 ; END IF ;
     IF addr( 3) = '1' THEN i_out := i_out +    8 ; END IF ;
     IF addr( 2) = '1' THEN i_out := i_out +    4 ; END IF ;
     IF addr( 1) = '1' THEN i_out := i_out +    2 ; END IF ;
     IF addr( 0) = '1' THEN i_out := i_out +    1 ; END IF ;
     RETURN i_out ;
   END Mar_int ;

-- Detect the SW turning.
FUNCTION Sw_rising(SIGNAL sw : STD_LOGIC) RETURN BOOLEAN IS
BEGIN
  IF sw = '1' AND sw'EVENT THEN
   RETURN TRUE ;
  ELSE
   RETURN FALSE ;
  END IF ;
END Sw_rising ;

END cpu_package ;