rggen-systemverilog 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a6878079652346d464c360c256e886f9225c102dd31bc8ae3ffdcc943ced8c99
4
+ data.tar.gz: 0f7f5723562004a98e2ebba3c96f51b5ca0387d46e82282f5da38d96acbf8fb2
5
+ SHA512:
6
+ metadata.gz: 670df6b890abef983d23d1a6c31861193fdeb1c3eb2a00bdc82990a23d01b043e4bd698878dbe7ef1206239414ff45c952fedd64f68ce08aaf110ea19157f759
7
+ data.tar.gz: 03e2e64799595dedea6725d38ad886aa377edac14839bed6f474ffdc6d886d5ef58bb08c489b117942eea123180ac6dbc24d713593737b16ed018a953c52d144
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at taichi730@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Taichi Ishitani
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ [![Build Status](https://travis-ci.org/rggen/rggen-systemverilog.svg?branch=master)](https://travis-ci.org/rggen/rggen-systemverilog)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/88086c5be538a1564a35/maintainability)](https://codeclimate.com/github/rggen/rggen-systemverilog/maintainability)
3
+ [![codecov](https://codecov.io/gh/rggen/rggen-systemverilog/branch/master/graph/badge.svg)](https://codecov.io/gh/rggen/rggen-systemverilog)
4
+ [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=rggen_rggen-systemverilog&metric=alert_status)](https://sonarcloud.io/dashboard?id=rggen_rggen-systemverilog)
5
+
6
+ # RgGen::SystemVerilog
7
+
8
+ RgGen::SystemVerilog provides for features listed below:
9
+
10
+ * Structure for defining SystemVerilog RTL and UVM RAL model writers
11
+ * Convience APIs for SystemVerilog code
12
+ * Declaring signals, ports and variables
13
+ * Generating code to define module, class and package
14
+ * Etc.
15
+
16
+ ## Installation
17
+
18
+ During RgGen installation, RgGen::SytemVerilog will also be installed automatically.
19
+
20
+ ```
21
+ $ gem install rggen
22
+ ```
23
+
24
+ If you want to install RgGen::SytemVerilog only, use the command below:
25
+
26
+ ```
27
+ $ gem isntall rggen-systemverilog
28
+ ```
29
+
30
+ ## Contact
31
+
32
+ Feedbacks, bug reports, questions and etc. are wellcome! You can post them by using following ways:
33
+
34
+ * [GitHub Issue Tracker](https://github.com/rggen/rggen-systemverilog/issues)
35
+ * [Mail](mailto:taichi730@gmail.com)
36
+
37
+ ## Copyright & License
38
+
39
+ Copyright © 2019 Taichi Ishitani. RgGen::SystemVerilog is licensed under the [MIT License](https://opensource.org/licenses/MIT), see [LICENSE](LICENSE) for futher details.
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the RgGen project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rggen/rggen-systemverilog/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'docile'
4
+ require 'facets/kernel/attr_singleton'
5
+
6
+ require_relative 'systemverilog/version'
7
+
8
+ require_relative 'systemverilog/utility/identifier'
9
+ require_relative 'systemverilog/utility/data_object'
10
+ require_relative 'systemverilog/utility/interface_port'
11
+ require_relative 'systemverilog/utility/interface_instance'
12
+ require_relative 'systemverilog/utility/structure_definition'
13
+ require_relative 'systemverilog/utility/class_definition'
14
+ require_relative 'systemverilog/utility/function_definition'
15
+ require_relative 'systemverilog/utility/local_scope'
16
+ require_relative 'systemverilog/utility/module_definition'
17
+ require_relative 'systemverilog/utility/package_definition'
18
+ require_relative 'systemverilog/utility/source_file'
19
+ require_relative 'systemverilog/utility'
20
+
21
+ require_relative 'systemverilog/component'
22
+ require_relative 'systemverilog/feature'
23
+ require_relative 'systemverilog/feature_rtl'
24
+ require_relative 'systemverilog/feature_ral'
25
+ require_relative 'systemverilog/factories'
26
+
27
+ module RgGen
28
+ module SystemVerilog
29
+ class << self
30
+ def setup_sv_component(builder, name, sv_feature)
31
+ builder.output_component_registry(name) do
32
+ register_component [
33
+ :register_map, :register_block, :register, :bit_field
34
+ ] do |category|
35
+ component Component, ComponentFactory
36
+ feature sv_feature, FeatureFactory if category != :register_map
37
+ end
38
+ end
39
+ end
40
+
41
+ def setup(builder)
42
+ setup_sv_component(builder, :sv_rtl, FeatureRTL)
43
+ setup_sv_component(builder, :sv_ral, FeatureRAL)
44
+ end
45
+ end
46
+ end
47
+
48
+ setup :systemverilog, SystemVerilog
49
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ class Component < Core::OutputBase::Component
6
+ def declarations(domain, type)
7
+ body = ->(r) { r.declarations(domain, type) }
8
+ [
9
+ @features.each_value.map(&body),
10
+ @children.map(&body)
11
+ ].flatten
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ class ComponentFactory < Core::OutputBase::ComponentFactory
6
+ end
7
+
8
+ class FeatureFactory < Core::OutputBase::FeatureFactory
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ class Feature < Core::OutputBase::Feature
6
+ include Utility
7
+ template_engine Core::OutputBase::ERBEngine
8
+
9
+ EntityContext =
10
+ Struct.new(:entity_type, :creation_method, :declaration_type)
11
+
12
+ class << self
13
+ private
14
+
15
+ def define_entity(entity_type, creation_method, declaration_type)
16
+ context =
17
+ EntityContext.new(entity_type, creation_method, declaration_type)
18
+ define_method(entity_type) do |domain, name, **attributes, &block|
19
+ entity =
20
+ create_entity(context, { name: name }.merge(attributes), block)
21
+ add_entity(entity, context, domain, name)
22
+ end
23
+ end
24
+ end
25
+
26
+ def declarations(domain, type)
27
+ @declarations[domain][type]
28
+ end
29
+
30
+ private
31
+
32
+ def post_initialize
33
+ super
34
+ @declarations = Hash.new do |h0, k0|
35
+ h0[k0] = Hash.new { |h1, k1| h1[k1] = [] }
36
+ end
37
+ end
38
+
39
+ def create_entity(context, attributes, block)
40
+ creation_method = context.creation_method
41
+ entity_type = context.entity_type
42
+ __send__(creation_method, entity_type, attributes, block)
43
+ end
44
+
45
+ def add_entity(entity, context, domain, name)
46
+ add_declaration(context, domain, entity.declaration)
47
+ add_identifier(name, entity.identifier)
48
+ end
49
+
50
+ def add_declaration(context, domain, declaration)
51
+ declaration_type = context.declaration_type
52
+ @declarations[domain][declaration_type] << declaration
53
+ end
54
+
55
+ def add_identifier(name, identifier)
56
+ instance_variable_set("@#{name}", identifier)
57
+ attr_singleton_reader(name)
58
+ export(name)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ class FeatureRAL < Feature
6
+ private
7
+
8
+ def create_variable(_, attributes, block)
9
+ DataObject.new(
10
+ :variable, attributes.merge(array_format: :unpacked), &block
11
+ )
12
+ end
13
+
14
+ def create_parameter(_, attributes, block)
15
+ DataObject.new(
16
+ :parameter, attributes, &block
17
+ )
18
+ end
19
+
20
+ define_entity :variable, :create_variable, :variable
21
+ define_entity :parameter, :create_parameter, :parameter
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ class FeatureRTL < Feature
6
+ private
7
+
8
+ def create_variable(data_type, attributes, block)
9
+ DataObject.new(
10
+ :variable, attributes.merge(data_type: data_type), &block
11
+ )
12
+ end
13
+
14
+ def create_interface(_, attributes, block)
15
+ InterfaceInstance.new(attributes, &block)
16
+ end
17
+
18
+ def create_argument(direction, attributes, block)
19
+ DataObject.new(
20
+ :argument, attributes.merge(direction: direction), &block
21
+ )
22
+ end
23
+
24
+ def create_interface_port(_, attributes, block)
25
+ InterfacePort.new(attributes, &block)
26
+ end
27
+
28
+ def create_parameter(_, attributes, block)
29
+ DataObject.new(
30
+ :parameter, attributes.merge(parameter_type: :parameter), &block
31
+ )
32
+ end
33
+
34
+ [
35
+ [:logic, :create_variable, :variable],
36
+ [:interface, :create_interface, :variable],
37
+ [:input, :create_argument, :port],
38
+ [:output, :create_argument, :port],
39
+ [:interface_port, :create_interface_port, :port],
40
+ [:parameter, :create_parameter, :parameter]
41
+ ].each do |entity, creation_method, declaration_type|
42
+ define_entity(entity, creation_method, declaration_type)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ include Core::Utility::CodeUtility
7
+
8
+ def create_blank_file(path)
9
+ SourceFile.new(path)
10
+ end
11
+
12
+ private
13
+
14
+ def create_identifier(name)
15
+ Identifier.new(name)
16
+ end
17
+
18
+ def assign(lhs, rhs)
19
+ "assign #{lhs} = #{rhs};"
20
+ end
21
+
22
+ def concat(expressions)
23
+ "{#{Array(expressions).join(', ')}}"
24
+ end
25
+
26
+ def array(expressions)
27
+ "'#{concat(expressions)}"
28
+ end
29
+
30
+ def function_call(name, expressions = nil)
31
+ "#{name}(#{Array(expressions).join(', ')})"
32
+ end
33
+
34
+ def macro_call(name, expressions = nil)
35
+ if (expression_array = Array(expressions)).empty?
36
+ "`#{name}"
37
+ else
38
+ "`#{name}(#{expression_array.join(', ')})"
39
+ end
40
+ end
41
+
42
+ def bin(value, width = nil)
43
+ if width
44
+ width = bit_width(value, width)
45
+ format("%d'b%0*b", width, width, value)
46
+ else
47
+ format("'b%b", value)
48
+ end
49
+ end
50
+
51
+ def dec(value, width = nil)
52
+ if width
53
+ width = bit_width(value, width)
54
+ format("%0d'd%d", width, value)
55
+ else
56
+ format("'d%d", value)
57
+ end
58
+ end
59
+
60
+ def hex(value, width = nil)
61
+ if width
62
+ width = bit_width(value, width)
63
+ print_width = (width + 3) / 4
64
+ format("%0d'h%0*x", width, print_width, value)
65
+ else
66
+ format("'h%x", value)
67
+ end
68
+ end
69
+
70
+ def bit_width(value, width)
71
+ bit_length = value.bit_length
72
+ bit_length = 1 if bit_length.zero?
73
+ [width, bit_length].max
74
+ end
75
+
76
+ def argument(name, **attribute)
77
+ DataObject.new(:argument, attribute.merge(name: name)).declaration
78
+ end
79
+
80
+ {
81
+ class_definition: ClassDefinition,
82
+ function_definition: FunctionDefinition,
83
+ local_scope: LocalScope,
84
+ module_definition: ModuleDefinition,
85
+ package_definition: PackageDefinition
86
+ }.each do |method_name, definition|
87
+ define_method(method_name) do |name, **attributes, &block|
88
+ definition.new(attributes.merge(name: name), &block).to_code
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class ClassDefinition < StructureDefinition
7
+ define_attribute :name
8
+ define_attribute :base
9
+ define_attribute :parameters
10
+ define_attribute :variables
11
+
12
+ private
13
+
14
+ def header_code(code)
15
+ code << [:class, space, name]
16
+ parameter_declarations(code)
17
+ class_inheritance(code)
18
+ code << semicolon
19
+ end
20
+
21
+ def parameter_declarations(code)
22
+ declarations = Array(parameters)
23
+ declarations.empty? || wrap(code << space, '#(', ')') do
24
+ add_declarations_to_header(code, declarations)
25
+ end
26
+ end
27
+
28
+ def class_inheritance(code)
29
+ return unless base
30
+ code << [space, :extends, space, base]
31
+ end
32
+
33
+ def pre_body_code(code)
34
+ add_declarations_to_body(code, Array(variables))
35
+ end
36
+
37
+ def footer_code
38
+ :endclass
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class DataObject
7
+ include Core::Utility::AttributeSetter
8
+
9
+ def initialize(object_type, **default_attributes)
10
+ @object_type = object_type
11
+ apply_attributes(default_attributes)
12
+ block_given? && yield(self)
13
+ end
14
+
15
+ define_attribute :name
16
+ define_attribute :direction
17
+ define_attribute :parameter_type
18
+ define_attribute :data_type
19
+ define_attribute :width
20
+ define_attribute :array_size
21
+ define_attribute :array_format, :packed
22
+ define_attribute :random
23
+ define_attribute :default
24
+
25
+ def declaration
26
+ declaration_snippets
27
+ .select(&:itself)
28
+ .reject(&:empty?)
29
+ .join(' ')
30
+ end
31
+
32
+ def identifier
33
+ Identifier.new(name) do |identifier|
34
+ identifier.__width__(width)
35
+ identifier.__array_size__(array_size)
36
+ identifier.__array_format__(array_format)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def declaration_snippets
43
+ [
44
+ rand_keyword,
45
+ argument_direction,
46
+ paraemter_keyword,
47
+ data_type,
48
+ packed_dimensions,
49
+ object_identifier,
50
+ default_value
51
+ ]
52
+ end
53
+
54
+ def rand_keyword
55
+ return unless @object_type == :variable
56
+ return unless random
57
+ return :rand if random.equal?(true)
58
+ random
59
+ end
60
+
61
+ def argument_direction
62
+ @object_type == :argument && direction
63
+ end
64
+
65
+ def paraemter_keyword
66
+ @object_type == :parameter && parameter_type
67
+ end
68
+
69
+ def packed_dimensions
70
+ (vectorized_array? ? vectorized_array_size : packed_array_size)
71
+ .map { |size| "[#{msb(size)}:0]" }
72
+ .join
73
+ end
74
+
75
+ def msb(size)
76
+ (size.is_a?(Integer) && size - 1) || "#{size}-1"
77
+ end
78
+
79
+ def array?
80
+ return false unless array_size
81
+ !array_size.empty?
82
+ end
83
+
84
+ def vectorized_array?
85
+ array? && array_format == :vectorized
86
+ end
87
+
88
+ def vectorized_array_size
89
+ size = [(width || 1), *array_size]
90
+ if size.all? { |s| s.is_a?(Integer) }
91
+ [size.inject(&:*)]
92
+ else
93
+ [size.join('*')]
94
+ end
95
+ end
96
+
97
+ def packed_array_size
98
+ size = []
99
+ size.concat(Array(array_size)) if array_format == :packed
100
+ size << width if valid_width?
101
+ size
102
+ end
103
+
104
+ def valid_width?
105
+ return false unless width
106
+ return true unless width.is_a?(Integer)
107
+ width > 1
108
+ end
109
+
110
+ def object_identifier
111
+ "#{name}#{unpacked_dimensions}"
112
+ end
113
+
114
+ def unpacked_array?
115
+ array? && array_format == :unpacked
116
+ end
117
+
118
+ def unpacked_dimensions
119
+ return unless unpacked_array?
120
+ array_size.map { |size| "[#{size}]" }.join
121
+ end
122
+
123
+ def default_value
124
+ default && "= #{default}"
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class FunctionDefinition < StructureDefinition
7
+ define_attribute :name
8
+ define_attribute :return_type
9
+ define_attribute :arguments
10
+
11
+ def return_type(**attributes)
12
+ attributes.size.zero? || (
13
+ @return_type = DataObject.new(:variable, **attributes)
14
+ )
15
+ @return_type
16
+ end
17
+
18
+ private
19
+
20
+ def header_code(code)
21
+ code << :function
22
+ return_type_declaration(code)
23
+ code << [space, name]
24
+ argument_declarations(code)
25
+ code << semicolon
26
+ end
27
+
28
+ def return_type_declaration(code)
29
+ return unless @return_type
30
+ code << [space, return_type.declaration]
31
+ end
32
+
33
+ def argument_declarations(code)
34
+ wrap(code, '(', ')') do
35
+ add_declarations_to_header(code, Array(arguments))
36
+ end
37
+ end
38
+
39
+ def footer_code
40
+ :endfunction
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class Identifier
7
+ def initialize(name)
8
+ @name = name
9
+ block_given? && yield(self)
10
+ end
11
+
12
+ def __width__(width)
13
+ @width = width
14
+ end
15
+
16
+ def __array_size__(array_size)
17
+ @array_size = array_size
18
+ end
19
+
20
+ def __array_format__(array_format)
21
+ @array_format = array_format
22
+ end
23
+
24
+ def __sub_identifiers__(sub_identifiers)
25
+ Array(sub_identifiers).each do |sub_identifier|
26
+ (@sub_identifiers ||= []) << sub_identifier
27
+ define_singleton_method(sub_identifier) do
28
+ Identifier.new("#{@name}.#{__method__}")
29
+ end
30
+ end
31
+ end
32
+
33
+ def to_s
34
+ @name.to_s
35
+ end
36
+
37
+ def [](array_index_or_lsb, width = nil)
38
+ if array_index_or_lsb
39
+ __create_new_identifier__(array_index_or_lsb, width)
40
+ else
41
+ self
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def __create_new_identifier__(array_index_or_lsb, width)
48
+ select = __create_select__(array_index_or_lsb, width)
49
+ Identifier.new("#{@name}#{select}") do |identifier|
50
+ identifier.__sub_identifiers__(@sub_identifiers)
51
+ end
52
+ end
53
+
54
+ def __create_select__(array_index_or_lsb, width)
55
+ if array_index_or_lsb.is_a?(::Array)
56
+ __array_select__(array_index_or_lsb)
57
+ elsif width
58
+ "[#{array_index_or_lsb}+:#{width}]"
59
+ else
60
+ "[#{array_index_or_lsb}]"
61
+ end
62
+ end
63
+
64
+ def __array_select__(array_index)
65
+ if @array_format == :vectorized
66
+ "[#{__vecotr_lsb__(array_index)}+:#{@width}]"
67
+ else
68
+ array_index
69
+ .map { |index| "[#{index}]" }
70
+ .join
71
+ end
72
+ end
73
+
74
+ def __vecotr_lsb__(array_index)
75
+ __reduce_array__([@width, __vecotr_index__(array_index)], :*, 1)
76
+ end
77
+
78
+ def __vecotr_index__(array_index)
79
+ index_values =
80
+ array_index
81
+ .reverse
82
+ .zip(__index_factors__)
83
+ .map { |i, f| __calc_index_value__(i, f) }
84
+ index = __reduce_array__(index_values.reverse, :+, 0)
85
+ integer?(index) ? index : "(#{index})"
86
+ end
87
+
88
+ def __index_factors__
89
+ Array.new(@array_size.size) do |i|
90
+ i.zero? ? nil : __reduce_array__(@array_size[-i..-1], :*, 1)
91
+ end
92
+ end
93
+
94
+ def __calc_index_value__(index, factor)
95
+ __reduce_array__([factor, index].compact, :*, 1)
96
+ end
97
+
98
+ def __reduce_array__(array, operator, initial_value)
99
+ if array.all?(&method(:integer?))
100
+ array.reduce(initial_value, &operator)
101
+ else
102
+ array.join(operator.to_s)
103
+ end
104
+ end
105
+
106
+ def integer?(value)
107
+ value.is_a?(Integer)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class InterfaceInstance
7
+ include Core::Utility::AttributeSetter
8
+
9
+ def initialize(**default_attributes)
10
+ apply_attributes(default_attributes)
11
+ block_given? && yield(self)
12
+ end
13
+
14
+ define_attribute :name
15
+ define_attribute :interface_type
16
+ define_attribute :parameter_values
17
+ define_attribute :port_connections
18
+ define_attribute :array_size
19
+ define_attribute :variables
20
+
21
+ def instantiation
22
+ [
23
+ interface_type,
24
+ parameter_value_assignments,
25
+ instance_identifier
26
+ ].select(&:itself).join(' ')
27
+ end
28
+
29
+ alias_method :declaration, :instantiation
30
+
31
+ def identifier
32
+ Identifier.new(name) do |identifier|
33
+ identifier.__array_size__(array_size)
34
+ identifier.__sub_identifiers__(variables)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def parameter_value_assignments
41
+ values = Array(parameter_values)
42
+ values.size.positive? && "#(#{values.join(', ')})"
43
+ end
44
+
45
+ def instance_identifier
46
+ [
47
+ name,
48
+ *Array(array_size).map { |size| "[#{size}]" },
49
+ "(#{Array(port_connections).join(', ')})"
50
+ ].join
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class InterfacePort
7
+ include Core::Utility::AttributeSetter
8
+
9
+ def initialize(**default_attributes)
10
+ apply_attributes(default_attributes)
11
+ block_given? && yield(self)
12
+ end
13
+
14
+ define_attribute :name
15
+ define_attribute :interface_type
16
+ define_attribute :modport
17
+ define_attribute :array_size
18
+
19
+ def modport(name, ports = nil)
20
+ @modport_name = name
21
+ @modport_ports = ports
22
+ end
23
+
24
+ def declaration
25
+ "#{port_type} #{port_identifier}"
26
+ end
27
+
28
+ def identifier
29
+ Identifier.new(name) do |identifier|
30
+ identifier.__array_size__(array_size)
31
+ identifier.__sub_identifiers__(@modport_ports)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def port_type
38
+ [@interface_type, @modport_name].compact.join('.')
39
+ end
40
+
41
+ def port_identifier
42
+ [
43
+ name, *Array(array_size).map { |size| "[#{size}]" }
44
+ ].join
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class LocalScope < StructureDefinition
7
+ define_attribute :name
8
+ define_attribute :variables
9
+ define_attribute :loop_size
10
+
11
+ def top_scope
12
+ @top_scope = true
13
+ end
14
+
15
+ private
16
+
17
+ def header_code(code)
18
+ code << [:generate, space] if @top_scope
19
+ code << "if (1) begin : #{name}" << nl
20
+ end
21
+
22
+ def footer_code(code)
23
+ code << :end
24
+ code << [space, :endgenerate] if @top_scope
25
+ end
26
+
27
+ def pre_body_code(code)
28
+ genvar_declarations(code)
29
+ generate_for_header(code)
30
+ variable_declarations(code)
31
+ end
32
+
33
+ def genvar_declarations(code)
34
+ genvars = Array(loop_size&.keys).map { |genvar| "genvar #{genvar}" }
35
+ add_declarations_to_body(code, genvars)
36
+ end
37
+
38
+ def generate_for_header(code)
39
+ loop_size&.each do |genvar, size|
40
+ code << generate_for(genvar, size) << nl
41
+ code.indent += 2
42
+ end
43
+ end
44
+
45
+ def generate_for(genvar, size)
46
+ "for (#{genvar} = 0;#{genvar} < #{size};++#{genvar}) begin : g"
47
+ end
48
+
49
+ def variable_declarations(code)
50
+ add_declarations_to_body(code, Array(variables))
51
+ end
52
+
53
+ def post_body_code(code)
54
+ (loop_size&.size || 0).times do
55
+ code.indent -= 2
56
+ code << :end << nl
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class ModuleDefinition < StructureDefinition
7
+ define_attribute :name
8
+ define_attribute :package_imports
9
+ define_attribute :parameters
10
+ define_attribute :ports
11
+ define_attribute :variables
12
+
13
+ def package_imports(packages)
14
+ @package_imports ||= []
15
+ @package_imports.concat(Array(packages))
16
+ end
17
+
18
+ def package_import(package)
19
+ package_imports([package])
20
+ end
21
+
22
+ private
23
+
24
+ def header_code(code)
25
+ code << [:module, space, name]
26
+ package_import_declaration(code)
27
+ parameter_declarations(code)
28
+ port_declarations(code)
29
+ code << semicolon
30
+ end
31
+
32
+ def package_import_declaration(code)
33
+ if (items = pacakge_import_items).empty?
34
+ code << space
35
+ return
36
+ end
37
+ add_declarations_to_header(code, items, semicolon)
38
+ end
39
+
40
+ def pacakge_import_items
41
+ Array(@package_imports).map.with_index do |package, i|
42
+ if i.zero?
43
+ [:import, "#{package}::*"].join(space)
44
+ else
45
+ [space(6), "#{package}::*"].join(space)
46
+ end
47
+ end
48
+ end
49
+
50
+ def parameter_declarations(code)
51
+ declarations = Array(parameters)
52
+ declarations.empty? || wrap(code, '#(', ')') do
53
+ add_declarations_to_header(code, declarations)
54
+ end
55
+ end
56
+
57
+ def port_declarations(code)
58
+ declarations = Array(ports)
59
+ wrap(code, '(', ')') do
60
+ add_declarations_to_header(code, declarations)
61
+ end
62
+ end
63
+
64
+ def pre_body_code(code)
65
+ add_declarations_to_body(code, Array(variables))
66
+ end
67
+
68
+ def footer_code
69
+ :endmodule
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class PackageDefinition < StructureDefinition
7
+ define_attribute :name
8
+ define_attribute :package_imports
9
+ define_attribute :include_files
10
+
11
+ def package_imports(packages)
12
+ @package_imports ||= []
13
+ @package_imports.concat(Array(packages))
14
+ end
15
+
16
+ def package_import(package)
17
+ package_imports([package])
18
+ end
19
+
20
+ def include_files(files)
21
+ @include_files ||= []
22
+ @include_files.concat(Array(files))
23
+ end
24
+
25
+ def include_file(file)
26
+ include_files([file])
27
+ end
28
+
29
+ private
30
+
31
+ def header_code(code)
32
+ code << [:package, space, name, semicolon]
33
+ end
34
+
35
+ def pre_body_code(code)
36
+ package_import_declaration(code)
37
+ file_include_directives(code)
38
+ end
39
+
40
+ def package_import_declaration(code)
41
+ declarations =
42
+ Array(@package_imports)
43
+ .map { |package| [:import, space, package, '::*'] }
44
+ add_declarations_to_body(code, declarations)
45
+ end
46
+
47
+ def file_include_directives(code)
48
+ Array(@include_files).each do |file|
49
+ code << [:'`include', space, string(file), nl]
50
+ end
51
+ end
52
+
53
+ def footer_code
54
+ :endpackage
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class SourceFile < Core::Utility::CodeUtility::SourceFile
7
+ ifndef_keyword :'`ifndef'
8
+ endif_keyword :'`endif'
9
+ define_keyword :'`define'
10
+ include_keyword :'`include'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ module Utility
6
+ class StructureDefinition <
7
+ Core::Utility::CodeUtility::StructureDefinition
8
+
9
+ include Core::Utility::AttributeSetter
10
+
11
+ def initialize(**default_attributes, &block)
12
+ apply_attributes(default_attributes)
13
+ super(&block)
14
+ end
15
+
16
+ private
17
+
18
+ def add_declarations_to_header(code, declarations, end_charactor = '')
19
+ declarations.empty? || indent(code, 2) do
20
+ declarations.each_with_index do |d, i|
21
+ code <<
22
+ if i < (declarations.size - 1)
23
+ [d, comma, nl]
24
+ else
25
+ [d, end_charactor]
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def add_declarations_to_body(code, declarations, end_charactor = ';')
32
+ declarations.each { |d| code << d << end_charactor << nl }
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module SystemVerilog
5
+ VERSION = '0.9.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rggen-systemverilog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Taichi Ishitani
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: docile
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: facets
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: 'Structure of SystemVerilog RTL and UVM RAL model writers for Rggen.
56
+
57
+ '
58
+ email:
59
+ - taichi730@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - CODE_OF_CONDUCT.md
65
+ - LICENSE
66
+ - README.md
67
+ - lib/rggen/systemverilog.rb
68
+ - lib/rggen/systemverilog/component.rb
69
+ - lib/rggen/systemverilog/factories.rb
70
+ - lib/rggen/systemverilog/feature.rb
71
+ - lib/rggen/systemverilog/feature_ral.rb
72
+ - lib/rggen/systemverilog/feature_rtl.rb
73
+ - lib/rggen/systemverilog/utility.rb
74
+ - lib/rggen/systemverilog/utility/class_definition.rb
75
+ - lib/rggen/systemverilog/utility/data_object.rb
76
+ - lib/rggen/systemverilog/utility/function_definition.rb
77
+ - lib/rggen/systemverilog/utility/identifier.rb
78
+ - lib/rggen/systemverilog/utility/interface_instance.rb
79
+ - lib/rggen/systemverilog/utility/interface_port.rb
80
+ - lib/rggen/systemverilog/utility/local_scope.rb
81
+ - lib/rggen/systemverilog/utility/module_definition.rb
82
+ - lib/rggen/systemverilog/utility/package_definition.rb
83
+ - lib/rggen/systemverilog/utility/source_file.rb
84
+ - lib/rggen/systemverilog/utility/structure_definition.rb
85
+ - lib/rggen/systemverilog/version.rb
86
+ homepage: https://github.com/rggen/rggen-systemverilog
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ bug_tracker_uri: https://github.com/rggen/rggen-systemverilog/issues
91
+ source_code_uri: https://github.com/rggen/rggen-systemverilog
92
+ wiki_uri: https://github.com/rggen/rggen/wiki
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '2.3'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: rggen-systemverilog-0.9.0
112
+ test_files: []