1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2021 Google, Inc.
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 FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <assert.h>
25bf215546Sopenharmony_ci#include <ctype.h>
26bf215546Sopenharmony_ci#include <stdio.h>
27bf215546Sopenharmony_ci#include <stdlib.h>
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "emu.h"
30bf215546Sopenharmony_ci#include "util.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci/*
33bf215546Sopenharmony_ci * Emulation for draw-state (ie. CP_SET_DRAW_STATE) related control registers:
34bf215546Sopenharmony_ci */
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ciEMU_CONTROL_REG(DRAW_STATE_SET);
37bf215546Sopenharmony_ciEMU_CONTROL_REG(DRAW_STATE_SEL);
38bf215546Sopenharmony_ciEMU_CONTROL_REG(DRAW_STATE_ACTIVE_BITMASK);
39bf215546Sopenharmony_ciEMU_CONTROL_REG(DRAW_STATE_HDR);
40bf215546Sopenharmony_ciEMU_CONTROL_REG(DRAW_STATE_BASE);
41bf215546Sopenharmony_ciEMU_CONTROL_REG(SDS_BASE);
42bf215546Sopenharmony_ciEMU_CONTROL_REG(SDS_DWORDS);
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ciuint32_t
45bf215546Sopenharmony_ciemu_get_draw_state_reg(struct emu *emu, unsigned n)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   // TODO maybe we don't need to do anything here
48bf215546Sopenharmony_ci   return emu->control_regs.val[n];
49bf215546Sopenharmony_ci}
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_civoid
52bf215546Sopenharmony_ciemu_set_draw_state_reg(struct emu *emu, unsigned n, uint32_t val)
53bf215546Sopenharmony_ci{
54bf215546Sopenharmony_ci   struct emu_draw_state *ds = &emu->draw_state;
55bf215546Sopenharmony_ci   unsigned cur_idx = emu_get_reg32(emu, &DRAW_STATE_SEL);
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   if (n == emu_reg_offset(&DRAW_STATE_SET)) {
58bf215546Sopenharmony_ci      if (ds->write_idx == 0) {
59bf215546Sopenharmony_ci         cur_idx = (val >> 24) & 0x1f;
60bf215546Sopenharmony_ci         ds->state[cur_idx].count = val & 0xffff;
61bf215546Sopenharmony_ci         ds->state[cur_idx].mode_mask = (val >> 20) & 0x7;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci         unsigned active_mask = emu_get_reg32(emu, &DRAW_STATE_ACTIVE_BITMASK);
64bf215546Sopenharmony_ci         active_mask |= (1 << cur_idx);
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci         emu_set_reg32(emu, &DRAW_STATE_ACTIVE_BITMASK, active_mask);
67bf215546Sopenharmony_ci         emu_set_reg32(emu, &DRAW_STATE_SEL, cur_idx);
68bf215546Sopenharmony_ci      } else {
69bf215546Sopenharmony_ci         ds->state[cur_idx].base_lohi[ds->write_idx - 1] = val;
70bf215546Sopenharmony_ci      }
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci      ds->write_idx = (ds->write_idx + 1) % 3;
73bf215546Sopenharmony_ci   } else if (n == emu_reg_offset(&DRAW_STATE_SEL)) {
74bf215546Sopenharmony_ci      emu_set_reg32(emu, &DRAW_STATE_HDR, ds->state[val].hdr);
75bf215546Sopenharmony_ci      emu_set_reg64(emu, &DRAW_STATE_BASE, ds->state[val].base);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci      /* It seems that SDS_BASE/SDS_DWORDS is also per draw-state group,
78bf215546Sopenharmony_ci       * and that when a new state-group is selected, SQE compares to
79bf215546Sopenharmony_ci       * the previous values to new DRAW_STATE_BASE & count to detect
80bf215546Sopenharmony_ci       * that new state has been appended to existing draw-state group:
81bf215546Sopenharmony_ci       */
82bf215546Sopenharmony_ci      unsigned prev_idx = ds->prev_draw_state_sel;
83bf215546Sopenharmony_ci      ds->state[prev_idx].sds_base = emu_get_reg64(emu, &SDS_BASE);
84bf215546Sopenharmony_ci      ds->state[prev_idx].sds_dwords = emu_get_reg32(emu, &SDS_DWORDS);
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci      emu_set_reg64(emu, &SDS_BASE, ds->state[val].sds_base);
87bf215546Sopenharmony_ci      emu_set_reg32(emu, &SDS_DWORDS, ds->state[val].sds_dwords);
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci      ds->prev_draw_state_sel = val;
90bf215546Sopenharmony_ci   }
91bf215546Sopenharmony_ci}
92