17ec681f3Smrg/*
27ec681f3Smrg * Copyright © 2021 Google, Inc.
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 FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include <assert.h>
257ec681f3Smrg#include <ctype.h>
267ec681f3Smrg#include <stdio.h>
277ec681f3Smrg#include <stdlib.h>
287ec681f3Smrg
297ec681f3Smrg#include "emu.h"
307ec681f3Smrg#include "util.h"
317ec681f3Smrg
327ec681f3Smrg/*
337ec681f3Smrg * Emulation for draw-state (ie. CP_SET_DRAW_STATE) related control registers:
347ec681f3Smrg */
357ec681f3Smrg
367ec681f3SmrgEMU_CONTROL_REG(DRAW_STATE_SET);
377ec681f3SmrgEMU_CONTROL_REG(DRAW_STATE_SEL);
387ec681f3SmrgEMU_CONTROL_REG(DRAW_STATE_ACTIVE_BITMASK);
397ec681f3SmrgEMU_CONTROL_REG(DRAW_STATE_HDR);
407ec681f3SmrgEMU_CONTROL_REG(DRAW_STATE_BASE);
417ec681f3SmrgEMU_CONTROL_REG(SDS_BASE);
427ec681f3SmrgEMU_CONTROL_REG(SDS_DWORDS);
437ec681f3Smrg
447ec681f3Smrguint32_t
457ec681f3Smrgemu_get_draw_state_reg(struct emu *emu, unsigned n)
467ec681f3Smrg{
477ec681f3Smrg   // TODO maybe we don't need to do anything here
487ec681f3Smrg   return emu->control_regs.val[n];
497ec681f3Smrg}
507ec681f3Smrg
517ec681f3Smrgvoid
527ec681f3Smrgemu_set_draw_state_reg(struct emu *emu, unsigned n, uint32_t val)
537ec681f3Smrg{
547ec681f3Smrg   struct emu_draw_state *ds = &emu->draw_state;
557ec681f3Smrg   unsigned cur_idx = emu_get_reg32(emu, &DRAW_STATE_SEL);
567ec681f3Smrg
577ec681f3Smrg   if (n == emu_reg_offset(&DRAW_STATE_SET)) {
587ec681f3Smrg      if (ds->write_idx == 0) {
597ec681f3Smrg         cur_idx = (val >> 24) & 0x1f;
607ec681f3Smrg         ds->state[cur_idx].count = val & 0xffff;
617ec681f3Smrg         ds->state[cur_idx].mode_mask = (val >> 20) & 0x7;
627ec681f3Smrg
637ec681f3Smrg         unsigned active_mask = emu_get_reg32(emu, &DRAW_STATE_ACTIVE_BITMASK);
647ec681f3Smrg         active_mask |= (1 << cur_idx);
657ec681f3Smrg
667ec681f3Smrg         emu_set_reg32(emu, &DRAW_STATE_ACTIVE_BITMASK, active_mask);
677ec681f3Smrg         emu_set_reg32(emu, &DRAW_STATE_SEL, cur_idx);
687ec681f3Smrg      } else {
697ec681f3Smrg         ds->state[cur_idx].base_lohi[ds->write_idx - 1] = val;
707ec681f3Smrg      }
717ec681f3Smrg
727ec681f3Smrg      ds->write_idx = (ds->write_idx + 1) % 3;
737ec681f3Smrg   } else if (n == emu_reg_offset(&DRAW_STATE_SEL)) {
747ec681f3Smrg      emu_set_reg32(emu, &DRAW_STATE_HDR, ds->state[val].hdr);
757ec681f3Smrg      emu_set_reg64(emu, &DRAW_STATE_BASE, ds->state[val].base);
767ec681f3Smrg
777ec681f3Smrg      /* It seems that SDS_BASE/SDS_DWORDS is also per draw-state group,
787ec681f3Smrg       * and that when a new state-group is selected, SQE compares to
797ec681f3Smrg       * the previous values to new DRAW_STATE_BASE & count to detect
807ec681f3Smrg       * that new state has been appended to existing draw-state group:
817ec681f3Smrg       */
827ec681f3Smrg      unsigned prev_idx = ds->prev_draw_state_sel;
837ec681f3Smrg      ds->state[prev_idx].sds_base = emu_get_reg64(emu, &SDS_BASE);
847ec681f3Smrg      ds->state[prev_idx].sds_dwords = emu_get_reg32(emu, &SDS_DWORDS);
857ec681f3Smrg
867ec681f3Smrg      emu_set_reg64(emu, &SDS_BASE, ds->state[val].sds_base);
877ec681f3Smrg      emu_set_reg32(emu, &SDS_DWORDS, ds->state[val].sds_dwords);
887ec681f3Smrg
897ec681f3Smrg      ds->prev_draw_state_sel = val;
907ec681f3Smrg   }
917ec681f3Smrg}
92