Skip to content

Commit

Permalink
Merge pull request #36 from stefanlippuner/c-preset-field
Browse files Browse the repository at this point in the history
Consts - Add Preset Values of Fields
  • Loading branch information
tgingold-cern authored Jan 11, 2024
2 parents 6ff3a4b + a33b70a commit 6217d65
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 11 deletions.
37 changes: 27 additions & 10 deletions proto/cheby/print_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def pr_hex_const(self, name, val):
"Unsized hex constant"
pass

def pr_hex_data(self, name, val, reg):
def pr_hex_data(self, name, val, width):
"Hex constant using the width of the data"
self.pr_hex_const(name, val)

Expand Down Expand Up @@ -63,7 +63,7 @@ def pr_reg(self, n):
return
f = n.children[0]
if f.c_preset is not None:
self.pr_hex_data(self.pr_name(n) + '_PRESET', f.c_preset, n)
self.pr_preset(self.pr_name(n), f.c_preset, n.width)

def pr_field_offset(self, f):
self.pr_dec_const(self.pr_name(f) + "_OFFSET", f.lo)
Expand All @@ -76,12 +76,19 @@ def compute_mask(self, f):
return mask << f.lo

def pr_field_mask(self, f):
self.pr_hex_data(self.pr_name(f), self.compute_mask(f), f._parent)
self.pr_hex_data(self.pr_name(f), self.compute_mask(f), f._parent.width)

def pr_field(self, f):
self.pr_field_offset(f)
self.pr_field_mask(f)

if f.c_preset is not None:
self.pr_preset(self.pr_name(f), f.c_preset, f.c_rwidth)

def pr_preset(self, name, preset, width):
name = name + '_PRESET'
self.pr_hex_data(name, preset, width)

def pr_enum(self, name, val, wd):
self.pr_hex_const(name, val)

Expand Down Expand Up @@ -114,8 +121,8 @@ def pr_const(self, name, val):
def pr_hex_const(self, name, val):
self.pr_const(name, "'h{:x}".format(val))

def pr_hex_data(self, name, val, reg):
self.pr_const(name, "{}'h{:x}".format(reg.width, val))
def pr_hex_data(self, name, val, width):
self.pr_const(name, "{}'h{:x}".format(width, val))

def pr_enum(self, name, val, wd):
self.pr_const (name, "{}'h{:x}".format(wd, val))
Expand Down Expand Up @@ -146,11 +153,18 @@ def pr_const_width(self, name, val, width):
def pr_hex_const(self, name, val):
self.pr_const(name, "16#{:x}#".format(val))

def pr_hex_data(self, name, val, reg):
hex_width = round(reg.width / 4)
assert(4*hex_width == reg.width)
hex_val = "{:x}".format(val).zfill(hex_width)
self.pr_const_width(name, "x\"{}\"".format(hex_val), reg.width)
def pr_bin_data(self, name, val, width):
bin_val = "{:b}".format(val).zfill(width)
self.pr_const_width(name, "\"{}\"".format(bin_val), width)

def pr_hex_data(self, name, val, width):
if width % 4 == 0:
hex_width = round(width / 4)
hex_val = "{:x}".format(val).zfill(hex_width)
self.pr_const_width(name, "x\"{}\"".format(hex_val), width)
else:
# Fall back to a binary constant for improved tool compatibility
self.pr_bin_data(name, val, width)

def pr_field_mask(self, f):
# Not printed as a mask may overflow a natural.
Expand Down Expand Up @@ -211,6 +225,9 @@ def pr_field(self, f):
self.pr_hex_const(self.pr_name(f) + '_MASK', self.compute_mask(f))
self.pr_dec_const(self.pr_name(f) + "_SHIFT", f.lo)

if f.c_preset is not None:
self.pr_preset(self.pr_name(f), f.c_preset, f.c_rwidth)


class ConstsPrinterPython(ConstsPrinter):
def pr_const(self, name, val):
Expand Down
2 changes: 1 addition & 1 deletion proto/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ def test_consts():
global nbr_tests

for f in ['demo_all', 'features/semver1', 'features/mapinfo1',
'issue64/simple_reg1', 'bug-consts/blkpfx',
'issue64/simple_reg1', 'issue_g2/reg', 'bug-consts/blkpfx',
'features/enums1', 'features/enums2', 'bug-const-range/const_range']:
if args.verbose:
print('test consts: {}'.format(f))
Expand Down
1 change: 1 addition & 0 deletions testfiles/demo_all-consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define DEMO_ALL_REG0_FIELD01 0xf0UL
#define DEMO_ALL_REG0_FIELD02_OFFSET 8
#define DEMO_ALL_REG0_FIELD02 0x700UL
#define DEMO_ALL_REG0_FIELD02_PRESET 0x2UL
#define ADDR_DEMO_ALL_REG1 0x4UL
#define DEMO_ALL_REG1_PRESET 0x123UL
#define ADDR_DEMO_ALL_REG2 0x8UL
Expand Down
1 change: 1 addition & 0 deletions testfiles/demo_all-consts.sv
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package demo_all_Consts;
localparam DEMO_ALL_REG0_FIELD01 = 32'hf0;
localparam DEMO_ALL_REG0_FIELD02_OFFSET = 8;
localparam DEMO_ALL_REG0_FIELD02 = 32'h700;
localparam DEMO_ALL_REG0_FIELD02_PRESET = 3'h2;
localparam ADDR_DEMO_ALL_REG1 = 'h4;
localparam DEMO_ALL_REG1_PRESET = 32'h123;
localparam ADDR_DEMO_ALL_REG2 = 'h8;
Expand Down
1 change: 1 addition & 0 deletions testfiles/demo_all-consts.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
`define DEMO_ALL_REG0_FIELD01 'hf0
`define DEMO_ALL_REG0_FIELD02_OFFSET 8
`define DEMO_ALL_REG0_FIELD02 'h700
`define DEMO_ALL_REG0_FIELD02_PRESET 'h2
`define ADDR_DEMO_ALL_REG1 'h4
`define DEMO_ALL_REG1_PRESET 'h123
`define ADDR_DEMO_ALL_REG2 'h8
Expand Down
1 change: 1 addition & 0 deletions testfiles/demo_all-consts.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package demo_all_Consts is
constant DEMO_ALL_REG0_FIELD00_OFFSET : Natural := 1;
constant DEMO_ALL_REG0_FIELD01_OFFSET : Natural := 4;
constant DEMO_ALL_REG0_FIELD02_OFFSET : Natural := 8;
constant DEMO_ALL_REG0_FIELD02_PRESET : std_logic_vector(3-1 downto 0) := "010";
constant ADDR_DEMO_ALL_REG1 : Natural := 16#4#;
constant DEMO_ALL_REG1_PRESET : std_logic_vector(32-1 downto 0) := x"00000123";
constant ADDR_DEMO_ALL_REG2 : Natural := 16#8#;
Expand Down
1 change: 1 addition & 0 deletions testfiles/demo_all-consts.vhdl-ohwr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package demo_all_consts_pkg is
constant c_DEMO_ALL_REG0_FIELD00_OFFSET : Natural := 1;
constant c_DEMO_ALL_REG0_FIELD01_OFFSET : Natural := 4;
constant c_DEMO_ALL_REG0_FIELD02_OFFSET : Natural := 8;
constant c_DEMO_ALL_REG0_FIELD02_PRESET : std_logic_vector(3-1 downto 0) := "010";
constant c_DEMO_ALL_REG1_ADDR : Natural := 16#4#;
constant c_DEMO_ALL_REG1_PRESET : std_logic_vector(32-1 downto 0) := x"00000123";
constant c_DEMO_ALL_REG2_ADDR : Natural := 16#8#;
Expand Down
13 changes: 13 additions & 0 deletions testfiles/issue_g2/reg-consts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#define HEADER_BLOCK_SIZE 8
#define ADDR_HEADER_BLOCK_REG_000_DRAWING_NUMBER 0x0UL
#define HEADER_BLOCK_REG_000_DRAWING_NUMBER_PRESET 0x8000101UL
#define ADDR_HEADER_BLOCK_REG_001_VERSION_REVISION 0x4UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_OFFSET 0
#define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION 0xfUL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_PRESET 0x1UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_OFFSET 4
#define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION 0xff0UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_PRESET 0x0UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_OFFSET 12
#define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE 0xfffff000UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_PRESET 0x3840fUL
15 changes: 15 additions & 0 deletions testfiles/issue_g2/reg-consts.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package header_block_Consts;
localparam HEADER_BLOCK_SIZE = 8;
localparam ADDR_HEADER_BLOCK_REG_000_DRAWING_NUMBER = 'h0;
localparam HEADER_BLOCK_REG_000_DRAWING_NUMBER_PRESET = 32'h8000101;
localparam ADDR_HEADER_BLOCK_REG_001_VERSION_REVISION = 'h4;
localparam HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_OFFSET = 0;
localparam HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION = 32'hf;
localparam HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_PRESET = 4'h1;
localparam HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_OFFSET = 4;
localparam HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION = 32'hff0;
localparam HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_PRESET = 8'h0;
localparam HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_OFFSET = 12;
localparam HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE = 32'hfffff000;
localparam HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_PRESET = 20'h3840f;
endpackage
13 changes: 13 additions & 0 deletions testfiles/issue_g2/reg-consts.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
`define HEADER_BLOCK_SIZE 8
`define ADDR_HEADER_BLOCK_REG_000_DRAWING_NUMBER 'h0
`define HEADER_BLOCK_REG_000_DRAWING_NUMBER_PRESET 'h8000101
`define ADDR_HEADER_BLOCK_REG_001_VERSION_REVISION 'h4
`define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_OFFSET 0
`define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION 'hf
`define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_PRESET 'h1
`define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_OFFSET 4
`define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION 'hff0
`define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_PRESET 'h0
`define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_OFFSET 12
`define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE 'hfffff000
`define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_PRESET 'h3840f
15 changes: 15 additions & 0 deletions testfiles/issue_g2/reg-consts.vhdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
library ieee;
use ieee.std_logic_1164.all;

package header_block_Consts is
constant HEADER_BLOCK_SIZE : Natural := 8;
constant ADDR_HEADER_BLOCK_REG_000_DRAWING_NUMBER : Natural := 16#0#;
constant HEADER_BLOCK_REG_000_DRAWING_NUMBER_PRESET : std_logic_vector(32-1 downto 0) := x"08000101";
constant ADDR_HEADER_BLOCK_REG_001_VERSION_REVISION : Natural := 16#4#;
constant HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_OFFSET : Natural := 0;
constant HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_PRESET : std_logic_vector(4-1 downto 0) := x"1";
constant HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_OFFSET : Natural := 4;
constant HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_PRESET : std_logic_vector(8-1 downto 0) := x"00";
constant HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_OFFSET : Natural := 12;
constant HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_PRESET : std_logic_vector(20-1 downto 0) := x"3840f";
end package header_block_Consts;
15 changes: 15 additions & 0 deletions testfiles/issue_g2/reg-consts.vhdl-ohwr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
library ieee;
use ieee.std_logic_1164.all;

package header_block_consts_pkg is
constant c_HEADER_BLOCK_SIZE : Natural := 8;
constant c_HEADER_BLOCK_REG_000_DRAWING_NUMBER_ADDR : Natural := 16#0#;
constant c_HEADER_BLOCK_REG_000_DRAWING_NUMBER_PRESET : std_logic_vector(32-1 downto 0) := x"08000101";
constant c_HEADER_BLOCK_REG_001_VERSION_REVISION_ADDR : Natural := 16#4#;
constant c_HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_OFFSET : Natural := 0;
constant c_HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_PRESET : std_logic_vector(4-1 downto 0) := x"1";
constant c_HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_OFFSET : Natural := 4;
constant c_HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_PRESET : std_logic_vector(8-1 downto 0) := x"00";
constant c_HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_OFFSET : Natural := 12;
constant c_HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_PRESET : std_logic_vector(20-1 downto 0) := x"3840f";
end package header_block_consts_pkg;
45 changes: 45 additions & 0 deletions testfiles/issue_g2/reg.cheby
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
memory-map:
bus: axi4-lite-32
name: header_block
description: A Header Block
x-hdl:
reg-prefix: true
children:
# This preset exports to header fine
- reg:
name: reg_000_drawing_number
address: 0x000
description: Identifier for the Drawing Number
width: 32
access: ro
preset: 0x08000101
x-hdl:
type: const
# These field presets does not export to header
- reg:
name: reg_001_version_revision
address: 0x004
description: Version, Revision and Build Date
width: 32
access: ro
x-hdl:
type: const
children:
- field:
name: version
range: 3-0
preset: 1
x-hdl:
type: const
- field:
name: revision
range: 11-4
preset: 0
x-hdl:
type: const
- field:
name: build_date
range: 31-12
preset: 0x3840f
x-hdl:
type: const

0 comments on commit 6217d65

Please sign in to comment.