Nugget
gpu.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2021 PCSX-Redux authors
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 
27 #pragma once
28 
29 #include <stdint.h>
30 
31 #include "common/hardware/hwregs.h"
32 
33 enum HResolution {
34  HR_EXTENDED,
35  HR_256 = 0,
36  HR_320 = 1,
37  HR_512 = 2,
38  HR_640 = 3,
39 };
40 
41 enum VResolution {
42  VR_240 = 0,
43  VR_480 = 1,
44 };
45 
46 enum VideoMode {
47  VM_NTSC = 0,
48  VM_PAL = 1,
49 };
50 
51 enum ColorDepth {
52  CD_15BITS = 0,
53  CD_24BITS = 1,
54 };
55 
56 enum VideoInterlace {
57  VI_OFF = 0,
58  VI_ON = 1,
59 };
60 
61 enum HResolutionExtended {
62  HRE_NORMAL = 0,
63  HRE_368 = 1,
64 };
65 
67  enum HResolution hResolution;
68  enum VResolution vResolution;
69  enum VideoMode videoMode;
70  enum ColorDepth colorDepth;
71  enum VideoInterlace videoInterlace;
72  enum HResolutionExtended hResolutionExtended;
73 };
74 
75 static inline void waitGPU() {
76  while ((GPU_STATUS & 0x04000000) == 0)
77  ;
78 }
79 
80 static inline void sendGPUData(uint32_t data) {
81  waitGPU();
82  GPU_DATA = data;
83 }
84 
85 static inline void sendGPUStatus(uint32_t status) {
86  GPU_STATUS = status;
87 }
88 
89 static inline uint32_t generateDisableDisplay() { return 0x03000001; }
90 static inline uint32_t generateEnableDisplay() { return 0x03000000; }
91 static inline void disableDisplay() { sendGPUStatus(generateDisableDisplay()); }
92 static inline void enableDisplay() { sendGPUStatus(generateEnableDisplay()); }
93 
94 static inline uint32_t generateDisplayMode(const struct DisplayModeConfig* config) {
95  return 0x08000000 | (config->hResolution << 0) | (config->vResolution << 2) | (config->videoMode << 3) |
96  (config->colorDepth << 4) | (config->videoInterlace << 5) | (config->hResolutionExtended << 6);
97 }
98 
99 static inline void setDisplayMode(const struct DisplayModeConfig* config) {
100  sendGPUStatus(generateDisplayMode(config));
101 }
102 
103 static inline uint32_t generateDisplayArea(int16_t x, int16_t y) { return 0x05000000 | x | (y << 10); }
104 static inline void setDisplayArea(int16_t x, int16_t y) { sendGPUStatus(generateDisplayArea(x, y)); }
105 
106 static inline uint32_t generateHorizontalRange(int16_t x1, int16_t x2) {
107  return 0x06000000 | (x1 + 0x260) | ((x1 + x2 + 0x260) << 12);
108 }
109 static inline void setHorizontalRange(int16_t x1, int16_t x2) { sendGPUStatus(generateHorizontalRange(x1, x2)); }
110 
111 static inline uint32_t generateVerticalRange(int16_t y1, int16_t y2) { return 0x07000000 | y1 | (y2 << 10); }
112 static inline void setVerticalRange(int16_t y1, int16_t y2) { sendGPUStatus(generateVerticalRange(y1, y2)); }
113 
114 union Color {
115  struct {
116  uint8_t r, g, b;
117  };
118  uint32_t packed;
119 };
120 
121 struct FastFill {
122  union Color c;
123  int16_t x, y, w, h;
124 };
125 
126 static inline void fastFill(const struct FastFill* ff) {
127  waitGPU();
128  GPU_DATA = 0x02000000 | ff->c.r | ff->c.g << 8 | ff->c.b << 16;
129  GPU_DATA = ff->x | ff->y << 16;
130  GPU_DATA = ff->w | ff->h << 16;
131 }
132 
133 static inline uint32_t generateDrawingAreaStart(int16_t x, int16_t y) { return 0xe3000000 | x | y << 10; }
134 static inline uint32_t generateDrawingAreaEnd(int16_t x, int16_t y) { return 0xe4000000 | (x - 1) | (y - 1) << 10; }
135 static inline void setDrawingArea(int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
136  sendGPUData(generateDrawingAreaStart(x1, y1));
137  sendGPUData(generateDrawingAreaEnd(x2, y2));
138 }
139 
140 static inline uint32_t generateDrawingOffset(int16_t x, int16_t y) { return 0xe5000000 | x | y << 11; }
141 static inline void setDrawingOffset(int16_t x, int16_t y) { sendGPUData(generateDrawingOffset(x, y)); }
142 
143 enum Shading {
144  S_FLAT = 0,
145  S_GOURAUD = 1,
146 };
147 
148 enum VerticesCount {
149  VC_3 = 0,
150  VC_4 = 1,
151 };
152 
153 enum Textured {
154  TEX_ON = 1,
155  TEX_OFF = 0,
156 };
157 
158 enum Transparency {
159  TRANS_ON = 1,
160  TRANS_OFF = 0,
161 };
162 
163 enum Blending {
164  BLEND_ON = 1,
165  BLEND_OFF = 0,
166 };
167 
169  enum Shading shading;
170  enum VerticesCount verticesCount;
171  enum Textured textured;
172  enum Transparency transparency;
173  enum Blending blending;
174  union Color color;
175 };
176 
177 static inline uint32_t generatePolygonCommand(const struct GPUPolygonCommand* c) {
178  return 0x20000000 | c->shading << 28 | c->verticesCount << 27 | c->textured << 26 | c->transparency << 25 |
179  c->blending << 24 | c->color.b << 16 | c->color.g << 8 | c->color.r;
180 }
181 static inline void startPolygonCommand(const struct GPUPolygonCommand* c) { sendGPUData(generatePolygonCommand(c)); }
182 
183 enum LineStyle {
184  POLY_OFF = 0,
185  POLY_ON = 1,
186 };
187 
189  enum Shading shading;
190  enum LineStyle lineStyle;
191  enum Transparency transparency;
192  union Color color;
193 };
194 
195 static inline uint32_t generateLineCommand(const struct GPULineCommand* c) {
196  return 0x40000000 | c->shading << 28 | c->lineStyle << 27 | c->transparency << 25 | c->color.b << 16 |
197  c->color.g << 8 | c->color.r;
198 }
199 static inline void startLineCommand(const struct GPULineCommand* c) { sendGPUData(generateLineCommand(c)); }
200 
201 static inline uint32_t generateFlushGPUCache() { return 0x01000000; }
202 static inline void flushGPUCache() { sendGPUData(generateFlushGPUCache()); }
Definition: gpu.h:66
Definition: gpu.h:121
Definition: gpu.h:188
Definition: gpu.h:168
Definition: gpu.h:114