rggen-duh 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ RgGen.define_value_extractor(:register_map, :duh, :bit_field, :bit_assignment) do
4
+ extract { |duh| { lsb: duh['bitOffset'], width: duh['bitWidth'] } }
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ {
4
+ byte_size: ['range', [:register_block]],
5
+ comment: ['description', [:register, :bit_field]],
6
+ initial_value: ['resetValue', [:bit_field]],
7
+ name: ['name'],
8
+ offset_address: ['addressOffset', [:register_file, :register]]
9
+ }.each do |value, (key, layers)|
10
+ RgGen.define_value_extractor(:register_map, :duh, *[layers, value].compact) do
11
+ extract { |duh| duh[key] }
12
+ end
13
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ RgGen.define_value_extractor(:register_map, :duh, :bit_field, :type) do
4
+ extract { |duh| find_type(duh) }
5
+
6
+ private
7
+
8
+ TYPE_MAP = {
9
+ rw: {
10
+ access: 'read-write', modified_write_value: 'default',
11
+ read_action: 'default', reserved: false
12
+ },
13
+ ro: {
14
+ access: 'read-only', read_action: 'default', reserved: false
15
+ },
16
+ wo: {
17
+ access: 'write-only', modified_write_value: 'default', reserved: false
18
+ },
19
+ rc: {
20
+ access: 'read-only', read_action: 'clear', reserved: false
21
+ },
22
+ w0c: {
23
+ access: 'read-write', modified_write_value: 'zeroToClear',
24
+ read_action: 'default', reserved: false
25
+ },
26
+ w1c: {
27
+ access: 'read-write', modified_write_value: 'oneToClear',
28
+ read_action: 'default', reserved: false
29
+ },
30
+ rs: {
31
+ access: 'read-only', read_action: 'set', reserved: false
32
+ },
33
+ w0s: {
34
+ access: 'read-write', modified_write_value: 'zeroToSet',
35
+ read_action: 'default', reserved: false
36
+ },
37
+ w1s: {
38
+ access: 'read-write', modified_write_value: 'oneToSet',
39
+ read_action: 'default', reserved: false
40
+ },
41
+ w0t: {
42
+ access: 'read-write', modified_write_value: 'zeroToToggle',
43
+ read_action: 'default', reserved: false
44
+ },
45
+ w1t: {
46
+ access: 'read-write', modified_write_value: 'oneToToggle',
47
+ read_action: 'default', reserved: false
48
+ },
49
+ w0crs: {
50
+ access: 'read-write', modified_write_value: 'zeroToClear',
51
+ read_action: 'set', reserved: false
52
+ },
53
+ w1crs: {
54
+ access: 'read-write', modified_write_value: 'oneToClear',
55
+ read_action: 'set', reserved: false
56
+ },
57
+ w0src: {
58
+ access: 'read-write', modified_write_value: 'zeroToSet',
59
+ read_action: 'clear', reserved: false
60
+ },
61
+ w1src: {
62
+ access: 'read-write', modified_write_value: 'oneToSet',
63
+ read_action: 'clear', reserved: false
64
+ },
65
+ w1: {
66
+ access: 'read-writeOnce', modified_write_value: 'default',
67
+ read_action: 'default', reserved: false
68
+ },
69
+ wo1: {
70
+ access: 'writeOnce', modified_write_value: 'default', reserved: false
71
+ },
72
+ reserved: {
73
+ reserved: true
74
+ }
75
+ }.freeze
76
+
77
+ def find_type(duh)
78
+ TYPE_MAP.find { |_, properties| match_properties?(duh, properties) }&.first
79
+ end
80
+
81
+ def match_properties?(duh, properties)
82
+ properties.all? { |name, value| __send__(name, duh) == value }
83
+ end
84
+
85
+ def access(duh)
86
+ if duh.key?('access')
87
+ duh['access']
88
+ elsif duh.layer == :register_block
89
+ 'read-write'
90
+ else
91
+ access(duh.parent)
92
+ end
93
+ end
94
+
95
+ def modified_write_value(duh)
96
+ duh.fetch('modifiedWriteValue', 'default')
97
+ end
98
+
99
+ def read_action(duh)
100
+ duh.fetch('readAction', 'default')
101
+ end
102
+
103
+ def reserved(duh)
104
+ duh.fetch('reserved', false)
105
+ end
106
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module DUH
5
+ class Loader < Core::RegisterMap::Loader
6
+ support_types [:json5]
7
+
8
+ def disable_validation
9
+ @disable_validation = true
10
+ end
11
+
12
+ private
13
+
14
+ def read_file(file_name)
15
+ duh = JsonRefs.call(RbJSON5.load_file(file_name))
16
+ validation? && validate(duh, file_name)
17
+ duh
18
+ end
19
+
20
+ def validation?
21
+ !@disable_validation
22
+ end
23
+
24
+ def validate(duh, file_name)
25
+ errors = Schema.validate(duh)
26
+ errors.empty? || (raise ValidationFailed.new(file_name, errors))
27
+ end
28
+
29
+ SUB_LAYERS = {
30
+ root: [:register_block],
31
+ register_block: [:register_file, :register],
32
+ register_file: [:register],
33
+ register: [:bit_field]
34
+ }.freeze
35
+
36
+ def format_sub_layer_data(read_data, layer, _file)
37
+ SUB_LAYERS[layer]&.each_with_object({}) do |sub_layer, sub_layer_data|
38
+ data = __send__("collect_#{sub_layer}_data", read_data)
39
+ data && (sub_layer_data[sub_layer] = data)
40
+ end
41
+ end
42
+
43
+ def collect_register_block_data(read_data)
44
+ collect_address_blocks(read_data)
45
+ &.select { |address_block| address_block['usage'] == 'register' }
46
+ &.map { |data| add_parent_and_layer_properties(data, nil, :register_block) }
47
+ end
48
+
49
+ def collect_address_blocks(read_data)
50
+ read_data
51
+ .dig('component', 'memoryMaps')
52
+ &.flat_map { |memory_map| memory_map['addressBlocks'] }
53
+ &.compact
54
+ end
55
+
56
+ def collect_register_file_data(read_data)
57
+ read_data['registerFiles']&.map do |data|
58
+ add_parent_and_layer_properties(data, read_data, :register_file)
59
+ end
60
+ end
61
+
62
+ def collect_register_data(read_data)
63
+ read_data['registers']&.map do |data|
64
+ add_parent_and_layer_properties(data, read_data, :register)
65
+ end
66
+ end
67
+
68
+ def collect_bit_field_data(read_data)
69
+ read_data['fields']&.map do |data|
70
+ add_parent_and_layer_properties(data, read_data, :bit_field)
71
+ end
72
+ end
73
+
74
+ def add_parent_and_layer_properties(data, parent, layer)
75
+ data.instance_variable_set(:@parent, parent)
76
+ data.instance_variable_set(:@layer, layer)
77
+ class << data
78
+ attr_reader :parent
79
+ attr_reader :layer
80
+ end
81
+ data
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module DUH
5
+ module Schema
6
+ class << self
7
+ def validate(duh)
8
+ schema.validate(duh).to_a
9
+ end
10
+
11
+ private
12
+
13
+ def schema
14
+ @schema ||= build_schema
15
+ end
16
+
17
+ def build_schema
18
+ schema_data = read_schema
19
+ resolver = ->(uri) { uri.path == 'defs' && schema_data['defs'] || nil }
20
+ JSONSchemer.schema(schema_data['component'], ref_resolver: resolver)
21
+ end
22
+
23
+ def read_schema
24
+ path = File.join(__dir__, 'duh-schema', 'schema.json')
25
+ JSON.parse(File.read(path))
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rggen/duh'
4
+ RgGen.setup RgGen::DUH
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module DUH
5
+ class ValidationFailed < RgGen::Core::LoadError
6
+ def initialize(file_name, errors)
7
+ super("input DUH file is invalid: #{file_name}")
8
+ @errors = errors
9
+ end
10
+
11
+ attr_reader :errors
12
+
13
+ def to_s
14
+ [
15
+ super,
16
+ *errors.map { |error| " - #{JSONSchemer::Errors.pretty(error)}" }
17
+ ].join("\n")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RgGen
4
+ module DUH
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rggen-duh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Taichi Ishitani
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-08-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json_refs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: json_schemer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.12
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.12
41
+ - !ruby/object:Gem::Dependency
42
+ name: rb_json5
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: DUH support for RgGen
70
+ email:
71
+ - rggen@googlegroups.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - CODE_OF_CONDUCT.md
77
+ - LICENSE
78
+ - README.md
79
+ - lib/rggen/duh.rb
80
+ - lib/rggen/duh/duh-schema/LICENSE
81
+ - lib/rggen/duh/duh-schema/README.md
82
+ - lib/rggen/duh/duh-schema/schema.json
83
+ - lib/rggen/duh/extractor/bit_assignment.rb
84
+ - lib/rggen/duh/extractor/simple_extractors.rb
85
+ - lib/rggen/duh/extractor/type.rb
86
+ - lib/rggen/duh/loader.rb
87
+ - lib/rggen/duh/schema.rb
88
+ - lib/rggen/duh/setup.rb
89
+ - lib/rggen/duh/validation_failed.rb
90
+ - lib/rggen/duh/version.rb
91
+ homepage: https://github.com/rggen/rggen-dut
92
+ licenses:
93
+ - MIT
94
+ metadata:
95
+ bug_tracker_uri: https://github.com/rggen/rggen-duh/issues
96
+ mailing_list_uri: https://groups.google.com/d/forum/rggen
97
+ source_code_uri: https://github.com/rggen/rggen-duh
98
+ wiki_uri: https://github.com/rggen/rggen/wiki
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '2.5'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.1.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: rggen-dut-0.1.0
118
+ test_files: []