首页 > 解决方案 > 如何在保持向后兼容聚合的同时扩展记录类型?

问题描述

我们的许多模块使用一组配置描述符记录来配置Configurable组件。数组类型、记录类型和可配置组件在每个人都使用的全局包中声明。

type config_descriptor_type is record
    config0 : integer;
    config1 : boolean;
end record;

type config_type is array(natural range <>) of config_descriptor_type;

component Configurable is
    generic (
        g_CONFIG : config_type
    );
end component;

使用它的每个模块Configurable都有一个包含该模块配置信息的包(所有现有的此类配置必须保持不变)。

constant c_config : config_type(0 to 3) := (
    (1, true),
    (2, false),
    (8, false),
    (4, true)
);

这个每个模块的常量被传递给 Configurable 实例(实例化也必须保持不变)。

config_inst : Configurable
    generic map (
        g_CONFIG => c_config
    );

我正在添加一些需要在记录Configurable中添加额外字段的功能。config_descriptor_type

type new_config_descriptor_type is record
    config0 : integer;
    config1 : boolean;
    config2 : integer; -- this field was added
end record;

我可以自由地对全局可配置包和实体以及使用新功能的任何新模块进行任何我喜欢的更改,但我不应该触及任何使用可配置的现有模块。如果由旧模块实例化,新字段应该获得一些默认值。

有没有办法添加这样一个字段,而不必修改所有使用可配置的现有模块?

标签: vhdl

解决方案


类型不能包含仅来自对象(信号、变量、常量)的默认值。当给一个对象赋值时(比如常量的初始值),所有的字段都需要被定义。

有一种解决方法,但需要一次性更改所有先前定义的常量的代码,不需要再次更改。如果您定义了一个基本的“init”函数,其中所有参数都有一个默认值,那么如果您向基本类型添加更多项目,那么现有代码将始终返回一个分配了所有字段的合法对象。

type config_descriptor_type is record
    config0 : integer;
    config1 : boolean;
    config2 : integer;
end record;

type config_type is array(natural range <>) of config_descriptor_type;

function init_config_descriptor_type( config0 : integer := 0;
                                      config1 : boolean := true;
                                      config2 : integer := 0     ) return config_descriptor_type is
  variable r : config_descriptor_type;
begin
  r.config0 := config0;
  r.config1 := config1;
  r.config2 := config2;
  return r;
end function;


-- Now you can create configs from the function. Config2 will be default value (0)
constant c_config : config_type(0 to 3) := (
    init_config_descriptor_type(1, true),
    init_config_descriptor_type(2, false),
    init_config_descriptor_type(8, false),
    init_config_descriptor_type(4, true)
);

推荐阅读