17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2019 Intel Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#undef NDEBUG
257ec681f3Smrg
267ec681f3Smrg#include <stdio.h>
277ec681f3Smrg#include <stdint.h>
287ec681f3Smrg#include <stdbool.h>
297ec681f3Smrg#include "intel_decoder.h"
307ec681f3Smrg
317ec681f3Smrgstatic bool quiet = false;
327ec681f3Smrg
337ec681f3Smrgstruct test_address {
347ec681f3Smrg   uint64_t offset;
357ec681f3Smrg};
367ec681f3Smrg
377ec681f3Smrg__attribute__((unused)) static uint64_t
387ec681f3Smrg_test_combine_address(void *data, void *location,
397ec681f3Smrg                      struct test_address address, uint32_t delta)
407ec681f3Smrg{
417ec681f3Smrg   return address.offset + delta;
427ec681f3Smrg}
437ec681f3Smrg
447ec681f3Smrg#define __gen_user_data void
457ec681f3Smrg#define __gen_combine_address _test_combine_address
467ec681f3Smrg#define __gen_address_type struct test_address
477ec681f3Smrg
487ec681f3Smrg#include "gentest_pack.h"
497ec681f3Smrg
507ec681f3Smrgstatic void
517ec681f3Smrgtest_struct(struct intel_spec *spec) {
527ec681f3Smrg   /* Fill struct fields and <group> tag */
537ec681f3Smrg   struct GFX9_TEST_STRUCT test1 = {
547ec681f3Smrg      .number1 = 5,
557ec681f3Smrg      .number2 = 1234,
567ec681f3Smrg   };
577ec681f3Smrg
587ec681f3Smrg   for (int i = 0; i < 4; i++) {
597ec681f3Smrg      test1.byte[i] = i * 10 + 5;
607ec681f3Smrg   }
617ec681f3Smrg
627ec681f3Smrg   /* Pack struct into a dw array */
637ec681f3Smrg   uint32_t dw[GFX9_TEST_STRUCT_length];
647ec681f3Smrg   GFX9_TEST_STRUCT_pack(NULL, dw, &test1);
657ec681f3Smrg
667ec681f3Smrg   /* Now decode the packed struct, and make sure it matches the original */
677ec681f3Smrg   struct intel_group *group;
687ec681f3Smrg   group = intel_spec_find_struct(spec, "TEST_STRUCT");
697ec681f3Smrg
707ec681f3Smrg   assert(group != NULL);
717ec681f3Smrg
727ec681f3Smrg   if (!quiet) {
737ec681f3Smrg      printf("\nTEST_STRUCT:\n");
747ec681f3Smrg      intel_print_group(stdout, group, 0, dw, 0, false);
757ec681f3Smrg   }
767ec681f3Smrg
777ec681f3Smrg   struct intel_field_iterator iter;
787ec681f3Smrg   intel_field_iterator_init(&iter, group, dw, 0, false);
797ec681f3Smrg
807ec681f3Smrg   while (intel_field_iterator_next(&iter)) {
817ec681f3Smrg      int idx;
827ec681f3Smrg      if (strcmp(iter.name, "number1") == 0) {
837ec681f3Smrg         uint16_t number = iter.raw_value;
847ec681f3Smrg         assert(number == test1.number1);
857ec681f3Smrg      } else if (strcmp(iter.name, "number2") == 0) {
867ec681f3Smrg         uint16_t number = iter.raw_value;
877ec681f3Smrg         assert(number == test1.number2);
887ec681f3Smrg      } else if (sscanf(iter.name, "byte[%d]", &idx) == 1) {
897ec681f3Smrg         uint8_t number = iter.raw_value;
907ec681f3Smrg         assert(number == test1.byte[idx]);
917ec681f3Smrg      }
927ec681f3Smrg   }
937ec681f3Smrg}
947ec681f3Smrg
957ec681f3Smrgstatic void
967ec681f3Smrgtest_two_levels(struct intel_spec *spec) {
977ec681f3Smrg   struct GFX9_STRUCT_TWO_LEVELS test;
987ec681f3Smrg
997ec681f3Smrg   for (int i = 0; i < 4; i++) {
1007ec681f3Smrg      for (int j = 0; j < 8; j++) {
1017ec681f3Smrg         test.byte[i][j] = (i * 10 + j) % 256;
1027ec681f3Smrg      }
1037ec681f3Smrg   }
1047ec681f3Smrg
1057ec681f3Smrg   uint32_t dw[GFX9_STRUCT_TWO_LEVELS_length];
1067ec681f3Smrg   GFX9_STRUCT_TWO_LEVELS_pack(NULL, dw, &test);
1077ec681f3Smrg
1087ec681f3Smrg   struct intel_group *group;
1097ec681f3Smrg   group = intel_spec_find_struct(spec, "STRUCT_TWO_LEVELS");
1107ec681f3Smrg
1117ec681f3Smrg   assert(group != NULL);
1127ec681f3Smrg
1137ec681f3Smrg   if (!quiet) {
1147ec681f3Smrg      printf("\nSTRUCT_TWO_LEVELS\n");
1157ec681f3Smrg      intel_print_group(stdout, group, 0, dw, 0, false);
1167ec681f3Smrg   }
1177ec681f3Smrg
1187ec681f3Smrg   struct intel_field_iterator iter;
1197ec681f3Smrg   intel_field_iterator_init(&iter, group, dw, 0, false);
1207ec681f3Smrg
1217ec681f3Smrg   while (intel_field_iterator_next(&iter)) {
1227ec681f3Smrg      int i, j;
1237ec681f3Smrg
1247ec681f3Smrg      assert(sscanf(iter.name, "byte[%d][%d]", &i, &j) == 2);
1257ec681f3Smrg      uint8_t number = iter.raw_value;
1267ec681f3Smrg      assert(number == test.byte[i][j]);
1277ec681f3Smrg   }
1287ec681f3Smrg}
1297ec681f3Smrg
1307ec681f3Smrgint main(int argc, char **argv)
1317ec681f3Smrg{
1327ec681f3Smrg   struct intel_spec *spec = intel_spec_load_filename(GENXML_PATH);
1337ec681f3Smrg
1347ec681f3Smrg   if (argc > 1 && strcmp(argv[1], "-quiet") == 0)
1357ec681f3Smrg      quiet = true;
1367ec681f3Smrg
1377ec681f3Smrg   test_struct(spec);
1387ec681f3Smrg   test_two_levels(spec);
1397ec681f3Smrg
1407ec681f3Smrg   intel_spec_destroy(spec);
1417ec681f3Smrg
1427ec681f3Smrg   return 0;
1437ec681f3Smrg}
144