1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#undef NDEBUG
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include <stdio.h>
27bf215546Sopenharmony_ci#include <stdint.h>
28bf215546Sopenharmony_ci#include <stdbool.h>
29bf215546Sopenharmony_ci#include "intel_decoder.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cistatic bool quiet = false;
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cistruct test_address {
34bf215546Sopenharmony_ci   uint64_t offset;
35bf215546Sopenharmony_ci};
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci__attribute__((unused)) static uint64_t
38bf215546Sopenharmony_ci_test_combine_address(void *data, void *location,
39bf215546Sopenharmony_ci                      struct test_address address, uint32_t delta)
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   return address.offset + delta;
42bf215546Sopenharmony_ci}
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci#define __gen_user_data void
45bf215546Sopenharmony_ci#define __gen_combine_address _test_combine_address
46bf215546Sopenharmony_ci#define __gen_address_type struct test_address
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci#include "gentest_pack.h"
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_cistatic void
51bf215546Sopenharmony_citest_struct(struct intel_spec *spec) {
52bf215546Sopenharmony_ci   /* Fill struct fields and <group> tag */
53bf215546Sopenharmony_ci   struct GFX9_TEST_STRUCT test1 = {
54bf215546Sopenharmony_ci      .number1 = 5,
55bf215546Sopenharmony_ci      .number2 = 1234,
56bf215546Sopenharmony_ci   };
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   for (int i = 0; i < 4; i++) {
59bf215546Sopenharmony_ci      test1.byte[i] = i * 10 + 5;
60bf215546Sopenharmony_ci   }
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   /* Pack struct into a dw array */
63bf215546Sopenharmony_ci   uint32_t dw[GFX9_TEST_STRUCT_length];
64bf215546Sopenharmony_ci   GFX9_TEST_STRUCT_pack(NULL, dw, &test1);
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   /* Now decode the packed struct, and make sure it matches the original */
67bf215546Sopenharmony_ci   struct intel_group *group;
68bf215546Sopenharmony_ci   group = intel_spec_find_struct(spec, "TEST_STRUCT");
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   assert(group != NULL);
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   if (!quiet) {
73bf215546Sopenharmony_ci      printf("\nTEST_STRUCT:\n");
74bf215546Sopenharmony_ci      intel_print_group(stdout, group, 0, dw, 0, false);
75bf215546Sopenharmony_ci   }
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   struct intel_field_iterator iter;
78bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, group, dw, 0, false);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
81bf215546Sopenharmony_ci      int idx;
82bf215546Sopenharmony_ci      if (strcmp(iter.name, "number1") == 0) {
83bf215546Sopenharmony_ci         uint16_t number = iter.raw_value;
84bf215546Sopenharmony_ci         assert(number == test1.number1);
85bf215546Sopenharmony_ci      } else if (strcmp(iter.name, "number2") == 0) {
86bf215546Sopenharmony_ci         uint16_t number = iter.raw_value;
87bf215546Sopenharmony_ci         assert(number == test1.number2);
88bf215546Sopenharmony_ci      } else if (sscanf(iter.name, "byte[%d]", &idx) == 1) {
89bf215546Sopenharmony_ci         uint8_t number = iter.raw_value;
90bf215546Sopenharmony_ci         assert(number == test1.byte[idx]);
91bf215546Sopenharmony_ci      }
92bf215546Sopenharmony_ci   }
93bf215546Sopenharmony_ci}
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_cistatic void
96bf215546Sopenharmony_citest_two_levels(struct intel_spec *spec) {
97bf215546Sopenharmony_ci   struct GFX9_STRUCT_TWO_LEVELS test;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   for (int i = 0; i < 4; i++) {
100bf215546Sopenharmony_ci      for (int j = 0; j < 8; j++) {
101bf215546Sopenharmony_ci         test.byte[i][j] = (i * 10 + j) % 256;
102bf215546Sopenharmony_ci      }
103bf215546Sopenharmony_ci   }
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   uint32_t dw[GFX9_STRUCT_TWO_LEVELS_length];
106bf215546Sopenharmony_ci   GFX9_STRUCT_TWO_LEVELS_pack(NULL, dw, &test);
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   struct intel_group *group;
109bf215546Sopenharmony_ci   group = intel_spec_find_struct(spec, "STRUCT_TWO_LEVELS");
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   assert(group != NULL);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   if (!quiet) {
114bf215546Sopenharmony_ci      printf("\nSTRUCT_TWO_LEVELS\n");
115bf215546Sopenharmony_ci      intel_print_group(stdout, group, 0, dw, 0, false);
116bf215546Sopenharmony_ci   }
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   struct intel_field_iterator iter;
119bf215546Sopenharmony_ci   intel_field_iterator_init(&iter, group, dw, 0, false);
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   while (intel_field_iterator_next(&iter)) {
122bf215546Sopenharmony_ci      int i, j;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci      assert(sscanf(iter.name, "byte[%d][%d]", &i, &j) == 2);
125bf215546Sopenharmony_ci      uint8_t number = iter.raw_value;
126bf215546Sopenharmony_ci      assert(number == test.byte[i][j]);
127bf215546Sopenharmony_ci   }
128bf215546Sopenharmony_ci}
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ciint main(int argc, char **argv)
131bf215546Sopenharmony_ci{
132bf215546Sopenharmony_ci   struct intel_spec *spec = intel_spec_load_filename(GENXML_PATH);
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   if (argc > 1 && strcmp(argv[1], "-quiet") == 0)
135bf215546Sopenharmony_ci      quiet = true;
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   test_struct(spec);
138bf215546Sopenharmony_ci   test_two_levels(spec);
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   intel_spec_destroy(spec);
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   return 0;
143bf215546Sopenharmony_ci}
144