1 /*
2 * Copyright © 2021 Google, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "emu.h"
30 #include "util.h"
31
32 /*
33 * Emulation for draw-state (ie. CP_SET_DRAW_STATE) related control registers:
34 */
35
36 EMU_CONTROL_REG(DRAW_STATE_SET_HDR);
37 EMU_CONTROL_REG(DRAW_STATE_SEL);
38 EMU_CONTROL_REG(DRAW_STATE_ACTIVE_BITMASK);
39 EMU_CONTROL_REG(DRAW_STATE_HDR);
40 EMU_CONTROL_REG(DRAW_STATE_BASE);
41 EMU_CONTROL_REG(SDS_BASE);
42 EMU_CONTROL_REG(SDS_DWORDS);
43
44 uint32_t
emu_get_draw_state_reg(struct emu * emu,unsigned n)45 emu_get_draw_state_reg(struct emu *emu, unsigned n)
46 {
47 // TODO maybe we don't need to do anything here
48 return emu->control_regs.val[n];
49 }
50
51 void
emu_set_draw_state_base(struct emu * emu,unsigned n,uint32_t val)52 emu_set_draw_state_base(struct emu *emu, unsigned n, uint32_t val)
53 {
54 struct emu_draw_state *ds = &emu->draw_state;
55
56 unsigned cur_idx = (emu_get_reg32(emu, &DRAW_STATE_SET_HDR) >> 24) & 0x1f;
57 ds->state[cur_idx].base_lohi[n] = val;
58 }
59
60 void
emu_set_draw_state_reg(struct emu * emu,unsigned n,uint32_t val)61 emu_set_draw_state_reg(struct emu *emu, unsigned n, uint32_t val)
62 {
63 struct emu_draw_state *ds = &emu->draw_state;
64 unsigned cur_idx = emu_get_reg32(emu, &DRAW_STATE_SEL);
65
66 if (n == emu_reg_offset(&DRAW_STATE_SET_HDR)) {
67 cur_idx = (val >> 24) & 0x1f;
68 ds->state[cur_idx].count = val & 0xffff;
69 ds->state[cur_idx].mode_mask = (val >> 20) & 0x7;
70
71 unsigned active_mask = emu_get_reg32(emu, &DRAW_STATE_ACTIVE_BITMASK);
72 active_mask |= (1 << cur_idx);
73
74 emu_set_reg32(emu, &DRAW_STATE_ACTIVE_BITMASK, active_mask);
75 } else if (n == emu_reg_offset(&DRAW_STATE_SEL)) {
76 emu_set_reg32(emu, &DRAW_STATE_HDR, ds->state[val].hdr);
77 emu_set_reg64(emu, &DRAW_STATE_BASE, ds->state[val].base);
78
79 /* It seems that SDS_BASE/SDS_DWORDS is also per draw-state group,
80 * and that when a new state-group is selected, SQE compares to
81 * the previous values to new DRAW_STATE_BASE & count to detect
82 * that new state has been appended to existing draw-state group:
83 */
84 unsigned prev_idx = ds->prev_draw_state_sel;
85 ds->state[prev_idx].sds_base = emu_get_reg64(emu, &SDS_BASE);
86 ds->state[prev_idx].sds_dwords = emu_get_reg32(emu, &SDS_DWORDS);
87
88 emu_set_reg64(emu, &SDS_BASE, ds->state[val].sds_base);
89 emu_set_reg32(emu, &SDS_DWORDS, ds->state[val].sds_dwords);
90
91 ds->prev_draw_state_sel = val;
92 }
93 }
94