Skip to content

Commit

Permalink
support bit field label (#8, rggen/rggen#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
taichi-ishitani authored Apr 26, 2023
1 parent f3f8baf commit d26ef01
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/rggen/c_header/bit_field/c_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
define_macro("#{full_name}_bit_width", width)
define_macro("#{full_name}_bit_mask", mask)
define_offset_macro
define_label_macros
end

private
Expand All @@ -27,5 +28,13 @@ def define_offset_macro
define_macro("#{full_name}_bit_offset", bit_field.lsb)
end
end

def define_label_macros
bit_field.labels.each do |label|
name = "#{full_name}_#{label.name}"
value = hex(label.value, bit_field.width)
define_macro(name, value)
end
end
end
end
37 changes: 31 additions & 6 deletions spec/rggen/c_header/bit_field/c_header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
RgGen.enable(:register_file, [:name, :offset_address, :size])
RgGen.enable(:register, [:name, :offset_address, :size, :type])
RgGen.enable(:register, :type, [:external, :indirect])
RgGen.enable(:bit_field, [:name, :bit_assignment, :type, :initial_value, :reference])
RgGen.enable(:bit_field, [:name, :bit_assignment, :type, :initial_value, :reference, :labels])
RgGen.enable(:bit_field, :type, [:rw])
RgGen.enable(:bit_field, :c_header)
end
Expand All @@ -30,7 +30,17 @@

register do
name 'register_1'
bit_field { bit_assignment lsb: 0, width: 64; type :rw; initial_value 0 }
bit_field {
bit_assignment lsb: 0, width: 64
type :rw
initial_value 0
labels [
{ name: 'FOO', value: 0 },
{ name: 'BAR', value: 2**16 - 1 },
{ name: 'BAZ', value: 2**32 - 1 },
{ name: 'QUX', value: 2**64 - 1 }
]
}
end

register do
Expand Down Expand Up @@ -66,15 +76,24 @@
register do
name 'register_4_2_1'
size [2, 2]
bit_field { name 'bit_field_0'; bit_assignment lsb: 0, width: 1; type :rw; initial_value 0 }
bit_field {
name 'bit_field_0'
bit_assignment lsb: 0, width: 1
type :rw
initial_value 0
labels [
{ name: 'FIZZ', value: 0 },
{ name: 'BUZZ', value: 1 }
]
}
end
end
end
end
c_header.bit_fields
end

it 'ビット幅/ビットマスク/ビット位置を示すマクロが定義される' do
it 'ビット幅/ビットマスク/ビット位置/ラベルを示すマクロが定義される' do
expect(c_header[0].macro_definitions).to match([
match_macro_definition('BLOCK_0_REGISTER_0_BIT_FIELD_0_BIT_WIDTH', 1),
match_macro_definition('BLOCK_0_REGISTER_0_BIT_FIELD_0_BIT_MASK', '0x1'),
Expand All @@ -96,7 +115,11 @@
expect(c_header[3].macro_definitions).to match([
match_macro_definition('BLOCK_0_REGISTER_1_BIT_WIDTH', 64),
match_macro_definition('BLOCK_0_REGISTER_1_BIT_MASK', '0xffffffffffffffff'),
match_macro_definition('BLOCK_0_REGISTER_1_BIT_OFFSET', 0)
match_macro_definition('BLOCK_0_REGISTER_1_BIT_OFFSET', 0),
match_macro_definition('BLOCK_0_REGISTER_1_FOO', '0x0000000000000000'),
match_macro_definition('BLOCK_0_REGISTER_1_BAR', '0x000000000000ffff'),
match_macro_definition('BLOCK_0_REGISTER_1_BAZ', '0x00000000ffffffff'),
match_macro_definition('BLOCK_0_REGISTER_1_QUX', '0xffffffffffffffff')
])

expect(c_header[4].macro_definitions).to match([
Expand Down Expand Up @@ -144,7 +167,9 @@
expect(c_header[10].macro_definitions).to match([
match_macro_definition('BLOCK_0_REGISTER_FILE_4_REGISTER_FILE_4_2_REGISTER_4_2_1_BIT_FIELD_0_BIT_WIDTH', 1),
match_macro_definition('BLOCK_0_REGISTER_FILE_4_REGISTER_FILE_4_2_REGISTER_4_2_1_BIT_FIELD_0_BIT_MASK', '0x1'),
match_macro_definition('BLOCK_0_REGISTER_FILE_4_REGISTER_FILE_4_2_REGISTER_4_2_1_BIT_FIELD_0_BIT_OFFSET', 0)
match_macro_definition('BLOCK_0_REGISTER_FILE_4_REGISTER_FILE_4_2_REGISTER_4_2_1_BIT_FIELD_0_BIT_OFFSET', 0),
match_macro_definition('BLOCK_0_REGISTER_FILE_4_REGISTER_FILE_4_2_REGISTER_4_2_1_BIT_FIELD_0_FIZZ', '0x0'),
match_macro_definition('BLOCK_0_REGISTER_FILE_4_REGISTER_FILE_4_2_REGISTER_4_2_1_BIT_FIELD_0_BUZZ', '0x1')
])
end
end
Expand Down

0 comments on commit d26ef01

Please sign in to comment.