// ********************************************************************/
// Actel Corporation Proprietary and Confidential
// Copyright 2010 Actel Corporation.  All rights reserved.
//
// ANY USE OR REDISTRIBUTION IN PART OR IN WHOLE MUST BE HANDLED IN
// ACCORDANCE WITH THE ACTEL LICENSE AGREEMENT AND MUST BE APPROVED
// IN ADVANCE IN WRITING.
//
// Description:	CoreAHBLite slave arbiter logic for
//				matrix (2 masters by 16 slaves)
//
// Revision Information:
// Date			Description
// ----			-----------------------------------------
// 10Feb10		Production Release Version 3.1
//
// SVN Revision Information:
// SVN $Revision: 11955 $
// SVN $Date: 2010-01-30 23:35:13 +0000 (Sat, 30 Jan 2010) $
//
// Resolved SARs
// SAR      Date     Who   Description
//
// Notes:
// 1. best viewed with tabstops set to "4" (tabs used throughout file)
//
// *********************************************************************/
`timescale 1ns/1ps
module COREAHBLITE_SLAVEARBITER
(
input						HCLK,
input						HRESETN,
input			[1:0]		MADDRSEL,
input						ADDRPHEND,
input						M0GATEDHMASTLOCK,
input						M1GATEDHMASTLOCK,
output	reg		[1:0]		MASTERADDRINPROG
);
localparam M1DONE			= 3'b000 ;
localparam M0EXTEND			= 3'b001 ;
localparam M1EXTEND			= 3'b010 ;
localparam M0DONE			= 3'b011 ;
localparam M0LOCK			= 3'b100 ;
localparam M1LOCK			= 3'b101 ;
localparam M0LOCKEXTEND		= 3'b110 ;
localparam M1LOCKEXTEND		= 3'b111 ;
localparam MASTER_0			= 2'b01;
localparam MASTER_1			= 2'b10;
localparam MASTER_NONE		= 2'b00;
reg				[2:0]		arbRegSMNextState;
reg				[2:0]		arbRegSMCurrentState;
    always @ ( * )
     begin
        MASTERADDRINPROG = MASTER_NONE;
        case ( arbRegSMCurrentState )
        M1DONE:
        begin
          if ( MADDRSEL[0] )
            begin
              if ( M0GATEDHMASTLOCK )
                   arbRegSMNextState = M0LOCK;
              else
               begin
                MASTERADDRINPROG = MASTER_0;
                if ( ADDRPHEND )
                    arbRegSMNextState = M0DONE;
                else
                    arbRegSMNextState = M0EXTEND;
               end
            end
            else
             if ( MADDRSEL[1] )
              begin
                if ( M1GATEDHMASTLOCK )
                   arbRegSMNextState = M1LOCK;
                else
                 begin
                  MASTERADDRINPROG = MASTER_1;
                  if ( ADDRPHEND )
                   arbRegSMNextState = M1DONE;
                  else
                   arbRegSMNextState = M1EXTEND;
                 end
                end
            else
                arbRegSMNextState = M1DONE;
        end
        M0DONE:
        begin
          if  ( MADDRSEL[1] )
           begin
             if ( M1GATEDHMASTLOCK )
                 arbRegSMNextState = M1LOCK;
             else
              begin
                MASTERADDRINPROG = MASTER_1;
                if ( ADDRPHEND )
                 arbRegSMNextState = M1DONE;
                else
                 arbRegSMNextState = M1EXTEND;
              end
            end
            else
             if ( MADDRSEL[0] )
              begin
                if ( M0GATEDHMASTLOCK )
                   arbRegSMNextState = M0LOCK;
                else
                 begin
                   MASTERADDRINPROG = MASTER_0;
                   if ( ADDRPHEND )
                    arbRegSMNextState = M0DONE;
                   else
                    arbRegSMNextState = M0EXTEND;
                 end
              end
            else
                arbRegSMNextState = M0DONE;
        end
        M0EXTEND:
        begin
            MASTERADDRINPROG = MASTER_0;
            if ( ADDRPHEND )
                arbRegSMNextState = M0DONE;
            else
                arbRegSMNextState = M0EXTEND;
        end
        M1EXTEND:
        begin
            MASTERADDRINPROG = MASTER_1;
            if ( ADDRPHEND )
                arbRegSMNextState = M1DONE;
            else
                arbRegSMNextState = M1EXTEND;
        end
        M0LOCK:
        begin
            if ( M0GATEDHMASTLOCK )
                if ( MADDRSEL[0] )
                begin
                    MASTERADDRINPROG = MASTER_0;
                    if ( ADDRPHEND )
                        arbRegSMNextState = M0LOCK;
                    else
                        arbRegSMNextState = M0LOCKEXTEND;
                end
                else
                    arbRegSMNextState = M0LOCK;
            else
                arbRegSMNextState = M0DONE;
        end
        M0LOCKEXTEND:
        begin
            MASTERADDRINPROG = MASTER_0;
            if ( ADDRPHEND )
                arbRegSMNextState = M0LOCK;
            else
                arbRegSMNextState = M0LOCKEXTEND;
        end
        M1LOCK:
        begin
            if ( M1GATEDHMASTLOCK )
                if ( MADDRSEL[1] )
                begin
                    MASTERADDRINPROG = MASTER_1;
                    if ( ADDRPHEND )
                        arbRegSMNextState = M1LOCK;
                    else
                        arbRegSMNextState = M1LOCKEXTEND;
                end
                else
                    arbRegSMNextState = M1LOCK;
            else
                arbRegSMNextState = M1DONE;
        end
        M1LOCKEXTEND:
        begin
            MASTERADDRINPROG = MASTER_1;
            if ( ADDRPHEND )
                arbRegSMNextState = M1LOCK;
            else
                arbRegSMNextState = M1LOCKEXTEND;
        end
        default:
            arbRegSMNextState = M1DONE;
	
       endcase
     end
    always @ ( posedge HCLK or negedge HRESETN )
    begin
        if ( !HRESETN )
            arbRegSMCurrentState <= M1DONE;
        else
            arbRegSMCurrentState <= arbRegSMNextState;
    end
endmodule 
