tgingold-cern / cheby

GNU General Public License v3.0
7 stars 4 forks source link

Field Presets do not show up in exported C-Headers #2

Closed nmolinski closed 5 months ago

nmolinski commented 1 year ago

This is based of the upstream found at https://gitlab.cern.ch/be-cem-edl/common/cheby

Python: 3.11 cheby: 1.6.dev0

Problem

With the following cheby file below, running cheby --gen-c=export/c/header_block.h -i header_block.cheby.yaml does not export a define macro for the field presets, but does for the register presets. Setting x-hdl attributes at reg or field level do not seem to affect the header generation

Chebyfile

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

Additionally, if I set the preset in the reg area, the following error raises preset is not allowed for register /header_block/reg_001_version_revision with fields. Which makes sense since it's expected behavior, but then I'm not sure what I'm missing to get the preset to show.

Result

/* Identifier for the Drawing Number */
#define HEADER_BLOCK_REG_000_DRAWING_NUMBER 0x0UL
#define HEADER_BLOCK_REG_000_DRAWING_NUMBER_PRESET 0x8000101UL // <-- Drawing Number Preset shows fine

/* Version, Revision and Build Date */
#define HEADER_BLOCK_REG_001_VERSION_REVISION 0x4UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_MASK 0xfUL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_SHIFT 0
#define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_MASK 0xff0UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_SHIFT 4
#define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_MASK 0xfffff000UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_SHIFT 12

Expected Result

/* Identifier for the Drawing Number */
#define HEADER_BLOCK_REG_000_DRAWING_NUMBER 0x0UL
#define HEADER_BLOCK_REG_000_DRAWING_NUMBER_PRESET 0x8000101UL

/* Version, Revision and Build Date */
#define HEADER_BLOCK_REG_001_VERSION_REVISION 0x4UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_MASK 0xfUL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_SHIFT 0
#define HEADER_BLOCK_REG_001_VERSION_REVISION_VERSION_PRESET 1  // <-- Added
#define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_MASK 0xff0UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_SHIFT 4
#define HEADER_BLOCK_REG_001_VERSION_REVISION_REVISION_PRESET 0  // <-- Added
#define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_MASK 0xfffff000UL
#define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_SHIFT 12
#define HEADER_BLOCK_REG_001_VERSION_REVISION_BUILD_DATE_PRESET 0x3840F // <-- Added

Solution

I modified pr_field in ConstsPrinterC (print_consts.py) to include presets if they exist.

    def pr_field(self, f):
        if f.hi is None:
            # A single bit
            self.pr_hex_const(self.pr_name(f), self.compute_mask(f))
            if f.preset is not None:
                self.pr_hex_const(self.pr_name(f) + "_PRESET", f.preset)

        else:
            # A multi-bit field
            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.preset is not None:
                self.pr_hex_const(self.pr_name(f) + "_PRESET", f.preset)

If you are busy and need me to submit a PR, I could do it here, but I assume you'll want to mirror with upstream first.

tgingold-cern commented 1 year ago

Yes, please submit a PR - I have updated this mirror.