OpenCores
URL https://opencores.org/ocsvn/soc_maker/soc_maker/trunk

Subversion Repositories soc_maker

[/] [soc_maker/] [trunk/] [lib/] [soc_maker/] [component.rb] - Diff between revs 7 and 8

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 7 Rev 8
###############################################################
###############################################################
#
#
#  File:      component.rb
#  File:      component.rb
#
#
#  Author:    Christian Hättich
#  Author:    Christian Hättich
#
#
#  Project:   System-On-Chip Maker
#  Project:   System-On-Chip Maker
#
#
#  Target:    Linux / Windows / Mac
#  Target:    Linux / Windows / Mac
#
#
#  Language:  ruby
#  Language:  ruby
#
#
#
#
###############################################################
###############################################################
#
#
#
#
#   Copyright (C) 2014  Christian Hättich  - feddischson [ at ] opencores.org
#   Copyright (C) 2014  Christian Hättich  - feddischson [ at ] opencores.org
#
#
#   This program is free software: you can redistribute it and/or modify
#   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
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   (at your option) any later version.
#
#
#   This program is distributed in the hope that it will be useful,
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   GNU General Public License for more details.
#
#
#   You should have received a copy of the GNU General Public License
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see .
#   along with this program.  If not, see .
#
#
#
#
###############################################################
###############################################################
#
#
#   Description:
#   Description:
#     This class represents an abstract component.
#     This class represents an abstract component.
#     It is one of the central classes and holds data,
#     It is one of the central classes and holds data,
#     which is used to describe and instanciate a core or SOC.
#     which is used to describe and instanciate a core or SOC.
#
#
#     The following fields are defined and used
#     The following fields are defined and used
#     by CoreDef and SOCDef
#     by CoreDef and SOCDef
#         - name               : name of the core  (mandatory)
#         - name               : name of the core  (mandatory)
#         - version            : version of the core  (mandatory)
#         - version            : version of the core  (mandatory)
#         - toplevel           : toplevel name (mandatory)
#         - toplevel           : toplevel name (mandatory)
#         - description        : description of this core
#         - description        : description of this core
#         - date               : creation date
#         - date               : creation date
#         - license            : license of this core
#         - license            : license of this core
#         - licensefile        : location of the license file
#         - licensefile        : location of the license file
#         - author             : author of this core
#         - author             : author of this core
#         - authormail         : author-mail of this core
#         - authormail         : author-mail of this core
#         - vccmd              : a version control command, which is used
#         - vccmd              : a version control command, which is used
#                                to download the files
#                                to download the files
#         - interfaces         : interfaces which are implemented
#         - interfaces         : interfaces which are implemented
#                                see SOCMaker::IfcSpc
#                                see SOCMaker::IfcSpc
#         - inst_parameters    : hash of instantiation parameters
#         - inst_parameters    : hash of instantiation parameters
#                                see SOCMaker::Parameter
#                                see SOCMaker::Parameter
#         - static_parameters  : hash of static parameters
#         - static_parameters  : hash of static parameters
#                                see SOCMaker::SParameter
#                                see SOCMaker::SParameter
########
########
#
#
# TODO
# TODO
#
#
#
#
###############################################################
###############################################################
module SOCMaker
module SOCMaker
class Component
class Component
  include ERR
  include ERR
  include YAML_EXT
  include YAML_EXT
  attr_accessor :name
  attr_accessor :name
  attr_accessor :version
  attr_accessor :version
  attr_accessor :toplevel
  attr_accessor :toplevel
  attr_accessor :description
  attr_accessor :description
  attr_accessor :date
  attr_accessor :date
  attr_accessor :license
  attr_accessor :license
  attr_accessor :licensefile
  attr_accessor :licensefile
  attr_accessor :author
  attr_accessor :author
  attr_accessor :authormail
  attr_accessor :authormail
  attr_accessor :vccmd
  attr_accessor :vccmd
  attr_accessor :interfaces
  attr_accessor :interfaces
  attr_accessor :functions
  attr_accessor :functions
  attr_accessor :inst_parameters
  attr_accessor :inst_parameters
  attr_accessor :static_parameters
  attr_accessor :static_parameters
  def initialize( name, version, toplevel, options = {} )
  def initialize( name, version, toplevel, options = {} )
    init_with( { 'name'      => name,
    init_with( { 'name'      => name,
                 'version'   => version,
                 'version'   => version,
                 'toplevel'  => toplevel }.merge( options ) )
                 'toplevel'  => toplevel }.merge( options ) )
  end
  end
  def encode_with( coder )
  def encode_with( coder )
    %w[ name version description date license licensefile
    %w[ name version description date license licensefile
        author authormail vccmd toplevel interfaces
        author authormail vccmd toplevel interfaces
        functions inst_parameters static_parameters ].
        functions inst_parameters static_parameters ].
          each { |v| coder[ v ] = instance_variable_get "@#{v}" }
          each { |v| coder[ v ] = instance_variable_get "@#{v}" }
  end
  end
  def init_with( coder )
  def init_with( coder )
    serr_if( coder[ 'name' ] == nil,
    serr_if( coder[ 'name' ] == nil,
      'Name not defined',
      'Name not defined',
      field:    'name'    )
      field:    'name'    )
    @name = coder[ 'name' ]
    @name = coder[ 'name' ]
    verr_if( !@name.is_a?( String ),
    verr_if( !@name.is_a?( String ),
      'The name must be of type string',
      'The name must be of type string',
      field:    'name'         )
      field:    'name'         )
    serr_if( @name.size  == 0,
    serr_if( @name.size  == 0,
      'Name not defined (size == 0)',
      'Name not defined (size == 0)',
      field:    'name'    )
      field:    'name'    )
    verr_if( !!SOCMaker::conf[ :name_regex ].match( @name ) == false,
    verr_if( !!SOCMaker::conf[ :name_regex ].match( @name ) == false,
         'The core name is invalid',
         'The core name is invalid',
        instance: @name,
        instance: @name,
        field:    'name' )
        field:    'name' )
    serr_if( coder[ 'version' ] == nil,
    serr_if( coder[ 'version' ] == nil,
      'Version not defined',
      'Version not defined',
      instance: @name,
      instance: @name,
      field:    'version' )
      field:    'version' )
    @version = coder[ 'version' ]
    @version = coder[ 'version' ]
    serr_if( @version.size == 0,
    serr_if( @version.size == 0,
      'Version not defined (size == 0)',
      'Version not defined (size == 0)',
      instance: @name,
      instance: @name,
      field:    'version' )
      field:    'version' )
    # cast from numeric to string, if not given as string
    # cast from numeric to string, if not given as string
    @version = @version.to_s if @version.is_a? ( Numeric )
    @version = @version.to_s if @version.is_a? ( Numeric )
    verr_if( !@version.is_a?( String ),
    verr_if( !@version.is_a?( String ),
      'The name must be of type string or numeric',
      'The name must be of type string or numeric',
      field:    'name'         )
      field:    'name'         )
    serr_if( coder[ 'toplevel' ] == nil,
    serr_if( coder[ 'toplevel' ] == nil,
      'Toplevel not defined',
      'Toplevel not defined',
      instance: @name,
      instance: @name,
      field:    'toplevel' )
      field:    'toplevel' )
    @toplevel = coder[ 'toplevel' ]
    @toplevel = coder[ 'toplevel' ]
    verr_if( !@toplevel.is_a?( String ),
    verr_if( !@toplevel.is_a?( String ),
      "toplevel must be of type string",
      "toplevel must be of type string",
      instance: @name,
      instance: @name,
      field:    "toplevel" )
      field:    "toplevel" )
    serr_if( @toplevel.size  == 0,
    serr_if( @toplevel.size  == 0,
      'Toplevel not defined (size == 0 )',
      'Toplevel not defined (size == 0 )',
      instance: @name,
      instance: @name,
      field:    'toplevel' )
      field:    'toplevel' )
    # set non-nil values
    # set non-nil values
    #  -> we don't need to check for nil in the rest of the
    #  -> we don't need to check for nil in the rest of the
    #     processing
    #     processing
    @description       = coder[ 'description'       ] || ""
    @description       = coder[ 'description'       ] || ""
    @date              = coder[ 'date'              ] || ""
    @date              = coder[ 'date'              ] || ""
    @license           = coder[ 'license'           ] || ""
    @license           = coder[ 'license'           ] || ""
    @licensefile       = coder[ 'licensefile'       ] || ""
    @licensefile       = coder[ 'licensefile'       ] || ""
    @author            = coder[ 'author'            ] || ""
    @author            = coder[ 'author'            ] || ""
    @authormail        = coder[ 'authormail'        ] || ""
    @authormail        = coder[ 'authormail'        ] || ""
    @vccmd             = coder[ 'vccmd'             ] || ""
    @vccmd             = coder[ 'vccmd'             ] || ""
    @interfaces        = coder[ 'interfaces'        ] || {}
    @interfaces        = coder[ 'interfaces'        ] || {}
    @functions         = coder[ 'functions'         ] || {}
    @functions         = coder[ 'functions'         ] || {}
    @inst_parameters   = coder[ 'inst_parameters'   ] || {}
    @inst_parameters   = coder[ 'inst_parameters'   ] || {}
    @static_parameters = coder[ 'static_parameters' ] || {}
    @static_parameters = coder[ 'static_parameters' ] || {}
    # ensure, that these fields are of type String
    # ensure, that these fields are of type String
    %w[ description date license licensefile
    %w[ description date license licensefile
        author authormail vccmd ].each do |n|
        author authormail vccmd ].each do |n|
      verr_if( !instance_variable_get( '@'+n ).is_a?( String ),
      verr_if( !instance_variable_get( '@'+n ).is_a?( String ),
        "#{n} must be of type String",
        "#{n} must be of type String",
        instance: @name,
        instance: @name,
        field:    n )
        field:    n )
    end
    end
    # ensure, that these fields are of type Hash
    # ensure, that these fields are of type Hash
    %w[ interfaces inst_parameters
    %w[ interfaces inst_parameters
        functions  static_parameters ].each do |n|
        functions  static_parameters ].each do |n|
      verr_if( !instance_variable_get( '@'+n ).is_a?( Hash ),
      verr_if( !instance_variable_get( '@'+n ).is_a?( Hash ),
        "#{n} must be of type Hash",
        "#{n} must be of type Hash",
        instance: @name,
        instance: @name,
        field:    n )
        field:    n )
    end
    end
    # check interfaces
    # check interfaces
    @interfaces.each do |ifc_name, ifc|
    @interfaces.each do |ifc_name, ifc|
      serr_if( ifc == nil,
      serr_if( ifc == nil,
            'Interface not defined',
            'Interface not defined',
            instance:   @name+":"+ifc_name.to_s )
            instance:   @name+":"+ifc_name.to_s )
      serr_if( !ifc.is_a?( SOCMaker::IfcDef ),
      serr_if( !ifc.is_a?( SOCMaker::IfcDef ),
            'Interface definition is not SOCMaker::IfcDef (please use SOCM_IFC)',
            'Interface definition is not SOCMaker::IfcDef (please use SOCM_IFC)',
            instance: @name+":"+ifc_name.to_s )
            instance: @name+":"+ifc_name.to_s )
    end
    end
    # check instance parameters
    # check instance parameters
    @inst_parameters.each do |name, param |
    @inst_parameters.each do |name, param |
      serr_if( param == nil,
      serr_if( param == nil,
            'Instance parameter not defined',
            'Instance parameter not defined',
            instance:   @name+":"+name.to_s )
            instance:   @name+":"+name.to_s )
      serr_if( !param.is_a?( SOCMaker::Parameter ),
      serr_if( !param.is_a?( SOCMaker::Parameter ),
            'Instance parameter not SOCMaker::Parameter (please use SOCM_PARAM)',
            'Instance parameter not SOCMaker::Parameter (please use SOCM_PARAM)',
            instance: @name+":"+name.to_s )
            instance: @name+":"+name.to_s )
    end
    end
    # check instance parameters
    # check instance parameters
    @static_parameters.each do |name, sparam |
    @static_parameters.each do |name, sparam |
      serr_if( sparam == nil,
      serr_if( sparam == nil,
            'Static parameter not defined',
            'Static parameter not defined',
            instance:   @name+":"+name.to_s )
            instance:   @name+":"+name.to_s )
      serr_if( !sparam.is_a?( SOCMaker::SParameter ),
      serr_if( !sparam.is_a?( SOCMaker::SParameter ),
            'Static parameter not SOCMaker::Parameter (please use SOCM_SPARAM)',
            'Static parameter not SOCMaker::Parameter (please use SOCM_SPARAM)',
            instance: @name+":"+name.to_s )
            instance: @name+":"+name.to_s )
    end
    end
  end
  end
  def consistency_check
  def consistency_check
 
    @interfaces.values.each_with_index do | ifc, i_ifc; ifc_def|
 
 
 
      # get interface definition
 
      ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
 
 
 
 
 
      # check, if all mandatory ports are implemented by this interface
 
      ifc_def.ports.each do | port_name, port |
 
        perr_if( port[ :mandatory ] == true &&
 
                  ifc.ports.select{ |key,port_def| port_def.defn.to_sym == port_name }.size == 0,
 
          "Mandatory port #{port_name} is not implemented in interface #{ifc.name}" )
 
      end
 
    end
 
 
  end
  end
  def get_files
  def get_files
    unless self.vccmd.nil? or @vccmd.size == 0
    unless self.vccmd.nil? or @vccmd.size == 0
      #puts"cd #{@dir} && #{@vccmd}"
      #puts"cd #{@dir} && #{@vccmd}"
      system( "cd #{@dir} && #{vccmd} " )
      system( "cd #{@dir} && #{vccmd} " )
    end
    end
  end
  end
 
 
 
 
  def generics
  def generics
    @inst_parameters.each_with_index do |(name, val), i|
    @inst_parameters.each_with_index do |(name, val), i|
      yield( name.to_s, val.type, val.default, i == @inst_parameters.size-1 )
      yield( name.to_s, val.type, val.default, i == @inst_parameters.size-1 )
    end
    end
  end
  end
  def get_and_ensure_dst_dir!( core_name )
  def get_and_ensure_dst_dir!( core_name )
      dst_dir =  File.expand_path(
      dst_dir =  File.expand_path(
            File.join(
            File.join(
              SOCMaker::conf[ :build_dir ],
              SOCMaker::conf[ :build_dir ],
              SOCMaker::conf[ :hdl_dir   ],
              SOCMaker::conf[ :hdl_dir   ],
              core_name ) )
              core_name ) )
      FileUtils.mkdir_p dst_dir
      FileUtils.mkdir_p dst_dir
      return dst_dir
      return dst_dir
  end
  end
  #
  #
  # Iterates over interface list.
  # Iterates over interface list.
  # For each interface, all ports are processed.
  # For each interface, all ports are processed.
  # For each port within each interface, we lookup the port defn
  # For each port within each interface, we lookup the port defn
  # and yield the call block with
  # and yield the call block with
  #   - port-name
  #   - port-name
  #   - port-definition
  #   - port-definition
  #   - info if last
  #   - info if last
  # as argument
  # as argument
  #
  #
 
  # An xor mechanism between port_dir and ifc=>dir is used
 
  # to determine the direction of a port, for example:
 
  #  If the interface is declared as input (1) and a port is declared as input (1)
 
  #  the resulting direction will be an output 1^1 = 0.
 
  #  But if the overall interface direction is an output (0) and a port is declared
 
  #  as input, the resulting direction will an input 0^1 = 1.
 
  # This allows to define a port-direction in the interface definition,
 
  # and toggle the directions on core-definition level.
  #
  #
  #
  #
  def ports( *args )
  def ports( *args )
    if args.size == 0
    if args.size == 0
      @interfaces.values.each_with_index do | ifc, i_ifc; ifc_def|
      @interfaces.values.each_with_index do | ifc, i_ifc; ifc_def|
        # get interface definition
        # get interface definition
        ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
        ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
        # loop over ports in this interface
        # loop over ports in this interface
        ifc.ports.each_with_index do |(port_name, port_def), i_port; port_dir|
        ifc.ports.each_with_index do |(port_name, port_def), i_port; port_dir|
          # the reference to the port in the definition
          # the reference to the port in the definition
          defn_ref = port_def.defn
          defn_ref      = port_def.defn.to_sym
          port_dir = ifc_def.ports[ defn_ref.to_sym ]
          perr_if( !ifc_def.ports.has_key?( defn_ref ),
          perr_if( port_dir == nil,
              "Can't find #{port_def} in" +
                "Can't find #{defn_ref} in interface " +
              "interface definition #{ifc_def.name} " +
                "definition #{ifc_def.name} version "  +
              "version #{ifc_def.version}" )
                ifc_def.version + "==>>" + ifc_def.to_yaml )
 
 
 
          # An xor mechanism between port_dir and ifc=>dir is used
 
          # to determine the direction of a port, for example:
 
          #  If the interface is declared as input (1) and a port is declared as input (1)
 
          #  the resulting direction will be an output 1^1 = 0.
 
          #  But if the overall interface direction is an output (0) and a port is declared
 
          #  as input, the resulting direction will an input 0^1 = 1.
 
          # This allows to define a port-direction in the interface definition,
 
          # and toggle the directions on core-definition level.
 
 
 
          #  (name, direction, length, is_last)
 
          yield(  port_name.to_s,
          yield(  port_name.to_s,
                  port_dir ^ ifc.dir,
                  ifc_def.ports[ defn_ref ][:dir] ^ ifc.dir,
                  port_def.len,
                  port_def.len,
 
                  ifc_def.ports[ defn_ref ][ :default  ],
                  ( (i_port == ifc.ports.size-1 ) and (i_ifc == @interfaces.size-1 ) ) )
                  ( (i_port == ifc.ports.size-1 ) and (i_ifc == @interfaces.size-1 ) ) )
        end
        end
      end
      end
    elsif args.size == 1
    elsif args.size == 1
      # get interface (input is the name as string  )
      # get interface (input is the name as string  )
      ifc = @interfaces[ args.first.to_sym  ]
      ifc = @interfaces[ args.first.to_sym  ]
 
 
      ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
      ifc_def = SOCMaker::lib.get_ifc( ifc.name, ifc.version )
      ifc.ports.each do |port_name, port_def; port_dir|
 
        port_def = port_def.defn
      # loop over all ports of this interface
        port_dir = ifc_def.ports[ port_def.to_sym ]
      ifc.ports.each_with_index do |(port_name, port_def),i_port; port_dir|
        if port_dir == nil
        defn_ref      = port_def.defn.to_sym
          perr_if( port_dir==nil,
        perr_if( !ifc_def.ports.has_key?( defn_ref ),
              "Can't find #{port_def} in" +
            "Can't find #{defn_ref} in" +
              "interface definition #{ifc_def.name} " +
              "interface definition #{ifc_def.name} " +
              "version #{ifc_def.version}" )
              "version #{ifc_def.version}" )
        end
        yield(  port_name.to_s,
        yield(  port_def.to_s,
                ifc_def.ports[ defn_ref ][:dir] ^ ifc.dir,
                port_name.to_s,
                port_def.len,
                port_dir ^ ifc.dir )
                ifc_def.ports[ defn_ref ][ :default  ],
 
                ( (i_port == ifc.ports.size-1 ) )
 
              )
 
        #yield(  port_def.to_s,
 
                #port_name.to_s,
 
                #port_dir ^ ifc.dir,
 
                #port_default )
      end
      end
    else
    else
 
      # TODO
    end
    end
  end
  end
#  def implements_port?( ifc_name, port_spec_name )
#  def implements_port?( ifc_name, port_spec_name )
#    tmp = @interfaces[ ifc_name.to_sym ].
#    tmp = @interfaces[ ifc_name.to_sym ].
#        ports.select{ |key,hash| hash.defn == port_spec_name.to_s }.keys
#        ports.select{ |key,hash| hash.defn == port_spec_name.to_s }.keys
#    perr_if( tmp.size > 1,
#    perr_if( tmp.size > 1,
#      "The port #{port_spec_name} of interface #{ifc_name} is implemented
#      "The port #{port_spec_name} of interface #{ifc_name} is implemented
#      multiple times" )
#      multiple times" )
#    return tmp.size == 1
#    return tmp.size == 1
#  end
#  end
  def param_ok?( param_name, param_value )
  def param_ok?( param_name, param_value )
    param = inst_parameters[ param_name.to_sym ]
    param = inst_parameters[ param_name.to_sym ]
    param = static_parameters[ param_name.to_sym ] if param == nil
    param = static_parameters[ param_name.to_sym ] if param == nil
    return false if param == nil
    return false if param == nil
  end
  end
  def ==(o)
  def ==(o)
    tmp    = ( o.class   == self.class )
    tmp    = ( o.class   == self.class )
    return tmp if !tmp
    return tmp if !tmp
    %w[ name version description date license licensefile
    %w[ name version description date license licensefile
        author authormail vccmd toplevel interfaces
        author authormail vccmd toplevel interfaces
        functions inst_parameters static_parameters ].
        functions inst_parameters static_parameters ].
          each do |v|
          each do |v|
      return false if instance_variable_get( "@#{v}" ) != o.instance_variable_get( "@#{v}" )
      return false if instance_variable_get( "@#{v}" ) != o.instance_variable_get( "@#{v}" )
    end
    end
    return true
    return true
  end
  end
  def to_s
  def to_s
    "version:           #{@version}\n"              +
    "version:           #{@version}\n"              +
    "toplevel:          #{@toplevel}\n"             +
    "toplevel:          #{@toplevel}\n"             +
    "description:       #{@description}\n"          +
    "description:       #{@description}\n"          +
    "date:              #{@date}\n"                 +
    "date:              #{@date}\n"                 +
    "license:           #{@license}\n"              +
    "license:           #{@license}\n"              +
    "licensefile:       #{@licensefile}\n"          +
    "licensefile:       #{@licensefile}\n"          +
    "author:            #{@author}\n"               +
    "author:            #{@author}\n"               +
    "authormail:        #{@authormail}\n"           +
    "authormail:        #{@authormail}\n"           +
    "vccmd:             #{@vccmd}\n"                +
    "vccmd:             #{@vccmd}\n"                +
    "interfaces:        #{@interfaces}\n"           +
    "interfaces:        #{@interfaces}\n"           +
    "functions:         #{@functions}\n"            +
    "functions:         #{@functions}\n"            +
    "inst_parameters:   #{@inst_parameters}\n"      +
    "inst_parameters:   #{@inst_parameters}\n"      +
    "static_parameters: #{@static_parameters}\n"
    "static_parameters: #{@static_parameters}\n"
  end
  end
end # class CoreDef
end # class CoreDef
end # module SOCMaker
end # module SOCMaker
# vim: noai:ts=2:sw=2
# vim: noai:ts=2:sw=2
 
 
 
# vim: noai:ts=2:sw=2
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.