diff --git a/register_printer/VERSION b/register_printer/VERSION index d7f1518..0437331 100644 --- a/register_printer/VERSION +++ b/register_printer/VERSION @@ -1 +1 @@ -1.1.12 \ No newline at end of file +1.1.13 \ No newline at end of file diff --git a/register_printer/parser/register_parser.py b/register_printer/parser/register_parser.py index 32759b4..a2a5ef9 100644 --- a/register_printer/parser/register_parser.py +++ b/register_printer/parser/register_parser.py @@ -22,23 +22,23 @@ def is_field_row(row): return False -def validate_register_row_empty_field(row, previous_context): +def validate_register_row_empty_field(row, register_table_column_mapping, previous_context): """ row can be obtained by xlrd sheet.row() method. It's a sequence of cell objects. """ context = previous_context.copy() - field_map = [ - (2, "msb"), - (3, "lsb"), - (4, "field"), - (5, "access"), - (6, "default") + fields = [ + "msb", + "lsb", + "field name", + "access", + "default" ] - for (col, field_name) in field_map: - context.column = col - if row[col].value != "": - msg = "Field '%s' must be emtpy." % field_name + for field in fields: + context.column = register_table_column_mapping[field] + if row[context.column].value != "": + msg = "Field '%s' must be emtpy." % field raise ExcelParseException(msg, context) return @@ -49,7 +49,7 @@ def parse_register_row(row, register_table_column_mapping, previous_context): a sequence of cells. """ context = previous_context.copy() - validate_register_row_empty_field(row, context) + validate_register_row_empty_field(row, register_table_column_mapping, context) context.column = register_table_column_mapping["offset"] try: diff --git a/register_printer/tests/dataset/dataset3/abc.xlsx b/register_printer/tests/dataset/dataset3/abc.xlsx new file mode 100644 index 0000000..8011bb0 Binary files /dev/null and b/register_printer/tests/dataset/dataset3/abc.xlsx differ diff --git a/register_printer/tests/dataset/dataset3/excels/block1.xlsx b/register_printer/tests/dataset/dataset3/excels/block1.xlsx new file mode 100644 index 0000000..6b0fa59 Binary files /dev/null and b/register_printer/tests/dataset/dataset3/excels/block1.xlsx differ diff --git a/register_printer/tests/dataset/dataset3/output/register_printer.json b/register_printer/tests/dataset/dataset3/output/register_printer.json new file mode 100644 index 0000000..0b8f05a --- /dev/null +++ b/register_printer/tests/dataset/dataset3/output/register_printer.json @@ -0,0 +1,255 @@ +{ + "name": "Top_Module", + "addressWidth": 12, + "dataWidth": 32, + "version": "2.1.234", + "author": "Yi Zhang", + "blockInstances": [ + { + "blockType": "Type2", + "name": "Instance2", + "baseAddress": 65536, + "blockSize": 16, + "addressWidth": 24, + "dataWidth": 64 + }, + { + "blockType": "Type1", + "name": "Instance3", + "baseAddress": 131072, + "blockSize": 65536, + "addressWidth": null, + "dataWidth": null + } + ], + "blockTemplates": [ + { + "blockType": "Type2", + "arrayTemplates": [], + "registerTemplates": [ + { + "name": "reg1", + "offset": 0, + "description": "register 1", + "fieldTemplates": [ + { + "name": "Field1", + "msb": 7, + "lsb": 1, + "defaultValue": 1, + "access": "RW", + "description": "Register1-Field1" + }, + { + "name": "Field2", + "msb": 10, + "lsb": 8, + "defaultValue": 1, + "access": "RW", + "description": "Register1-Field2" + } + ] + }, + { + "name": "reg2", + "offset": 8, + "description": "register 2", + "fieldTemplates": [ + { + "name": "Field2", + "msb": 7, + "lsb": 1, + "defaultValue": 1, + "access": "RW", + "description": "Register2-Field1" + }, + { + "name": "Field3", + "msb": 12, + "lsb": 9, + "defaultValue": 1, + "access": "RW", + "description": "Register2-Field3" + } + ] + } + ] + }, + { + "blockType": "Type1", + "arrayTemplates": [ + { + "name": "reg_array1", + "length": 16, + "offset": 4, + "startAddress": 16, + "endAddress": 16, + "description": "" + }, + { + "name": "reg_array2", + "length": 4, + "offset": 32, + "startAddress": 256, + "endAddress": 284, + "description": "" + } + ], + "registerTemplates": [ + { + "name": "reg1", + "offset": 0, + "description": "register 1", + "fieldTemplates": [ + { + "name": "Field1", + "msb": 7, + "lsb": 1, + "defaultValue": 1, + "access": "RW", + "description": "Register1-Field1" + }, + { + "name": "Field2", + "msb": 10, + "lsb": 8, + "defaultValue": 1, + "access": "RW", + "description": "Register1-Field2" + } + ] + }, + { + "name": "reg2", + "offset": 4, + "description": "register 2", + "fieldTemplates": [ + { + "name": "Field2", + "msb": 7, + "lsb": 1, + "defaultValue": 1, + "access": "RW", + "description": "Register2-Field1" + }, + { + "name": "Field3", + "msb": 12, + "lsb": 9, + "defaultValue": 1, + "access": "RW", + "description": "Register2-Field3" + } + ] + }, + { + "name": "reg_a1", + "offset": 16, + "description": "register aa", + "fieldTemplates": [ + { + "name": "Field4", + "msb": 3, + "lsb": 0, + "defaultValue": 1, + "access": "RO", + "description": "registeraa_field4" + }, + { + "name": "Field5", + "msb": 14, + "lsb": 4, + "defaultValue": 2, + "access": "RW", + "description": "registeraa_field5" + } + ] + }, + { + "name": "reg_a2", + "offset": 256, + "description": "register a2", + "fieldTemplates": [ + { + "name": "Field6", + "msb": 10, + "lsb": 0, + "defaultValue": 4, + "access": "RW", + "description": "registera2_field6" + }, + { + "name": "Field7", + "msb": 31, + "lsb": 11, + "defaultValue": 4, + "access": "RW", + "description": "registera2_field7" + } + ] + }, + { + "name": "reg_a3", + "offset": 280, + "description": "register a3", + "fieldTemplates": [ + { + "name": "Field8", + "msb": 11, + "lsb": 0, + "defaultValue": 5, + "access": "RW", + "description": "registera3_field8" + }, + { + "name": "Field9", + "msb": 15, + "lsb": 12, + "defaultValue": 7, + "access": "RW", + "description": "registera3_field9" + } + ] + }, + { + "name": "reg_a2", + "offset": 288, + "description": "register a2", + "fieldTemplates": [ + { + "name": "Field6", + "msb": 10, + "lsb": 0, + "defaultValue": 5, + "access": "RW", + "description": "registera2_field6" + }, + { + "name": "Field7", + "msb": 31, + "lsb": 11, + "defaultValue": 6, + "access": "RW", + "description": "registera2_field7" + } + ] + }, + { + "name": "regtt", + "offset": 416, + "description": "", + "fieldTemplates": [ + { + "name": "Field9", + "msb": 10, + "lsb": 0, + "defaultValue": 5, + "access": "RW", + "description": "registera2_field6" + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/register_printer/tests/test_register_printer.py b/register_printer/tests/test_register_printer.py index 8f69b5e..b02db37 100644 --- a/register_printer/tests/test_register_printer.py +++ b/register_printer/tests/test_register_printer.py @@ -258,3 +258,57 @@ def test_json_input_and_output(self): "JSON file content is not correct" ) return + + +class TestRegisterPrinterWithExtraField(TestCase): + + DATASET_PATH = os.path.join(DATASET_ROOT_PATH, "dataset3") + + def setUp(self): + self.config_file = os.path.join( + TestRegisterPrinterWithExtraField.DATASET_PATH, + "abc.xlsx" + ) + self.excel_path = os.path.join( + TestRegisterPrinterWithExtraField.DATASET_PATH, + "excels" + ) + return + + def test_extra_field_in_registers(self): + with TemporaryDirectory() as tmp_dir: + output_path = tmp_dir + register_printer = RegisterPrinter( + config_file=self.config_file, + excel_path=self.excel_path, + output_path=output_path + ) + register_printer.generate_json() + + json_file_path = tmp_dir + baseline_json_file_path = os.path.join( + TestRegisterPrinterWithExtraField.DATASET_PATH, + "output" + ) + + json_filename = os.path.join( + json_file_path, + "register_printer.json" + ) + baseline_json_filename = os.path.join( + baseline_json_file_path, + "register_printer.json" + ) + self.assertTrue( + os.path.exists(json_filename), + "JSON file is not generated." + ) + self.assertTrue( + filecmp.cmp( + json_filename, + baseline_json_filename + ), + "JSON file content is not correct" + ) + + return