Debian Codegraph @

src/VramBitMappedView.cpp

raw symbols SCIP
1 #include "VramBitMappedView.h"
2 #include "ranges.h"
3 #include <QPainter>
4 #include <algorithm>
5 #include <cstdio>
6
7 VramBitMappedView::VramBitMappedView(QWidget* parent)
8 : QWidget(parent)
9 , image(512, 512, QImage::Format_RGB32)
10 {
11 ranges::fill(msxPalette, qRgb(80, 80, 80));
12 setZoom(1.0f);
13
14 // Mouse update events when mouse is moved over the image, Quibus likes this
15 // better than my preferred click-on-the-image.
16 setMouseTracking(true);
17 }
18
19 void VramBitMappedView::setZoom(float zoom)
20 {
21 zoomFactor = std::max(1.0f, zoom);
22 setFixedSize(int(512.0f * zoomFactor), int(float(lines) * 2.0f * zoomFactor));
23 update();
24 }
25
26 void VramBitMappedView::decode()
27 {
28 if (!vramBase) return;
29
30 printf("\n"
31 "screenMode: %i\n"
32 "vram to start decoding: %i\n",
33 screenMode, vramAddress);
34 switch (screenMode) {
35 case 5: decodeSCR5(); break;
36 case 6: decodeSCR6(); break;
37 case 7: decodeSCR7(); break;
38 case 8: decodeSCR8(); break;
39 case 10:
40 case 11: decodeSCR10(); break;
41 case 12: decodeSCR12(); break;
42 }
43 pixImage = QPixmap::fromImage(image);
44 update();
45 }
46
47 void VramBitMappedView::decodePallet()
48 {
49 if (!palette) return;
50
51 for (int i = 0; i < 16; ++i) {
52 int r = (palette[2 * i + 0] & 0xf0) >> 4;
53 int b = (palette[2 * i + 0] & 0x0f);
54 int g = (palette[2 * i + 1] & 0x0f);
55 auto scale = [](int x) { return (x >> 1) | (x << 2) | (x << 5); };
56 msxPalette[i] = qRgb(scale(r), scale(g), scale(b));
57 }
58 }
59
60 static unsigned interleave(unsigned x)
61 {
62 return (x >> 1) | ((x & 1) << 16);
63 }
64
65 void VramBitMappedView::setPixel2x2(int x, int y, QRgb c)
66 {
67 image.setPixel(2 * x + 0, 2 * y + 0, c);
68 image.setPixel(2 * x + 1, 2 * y + 0, c);
69 image.setPixel(2 * x + 0, 2 * y + 1, c);
70 image.setPixel(2 * x + 1, 2 * y + 1, c);
71 }
72
73 void VramBitMappedView::setPixel1x2(int x, int y, QRgb c)
74 {
75 image.setPixel(x, 2 * y + 0, c);
76 image.setPixel(x, 2 * y + 1, c);
77 }
78
79 static QRgb decodeYJK(int y, int j, int k)
80 {
81 int r = std::clamp(y + j, 0, 31);
82 int g = std::clamp(y + k, 0, 31);
83 int b = std::clamp((5 * y - 2 * j - k) / 4, 0, 31);
84 auto scale = [](int x) { return (x << 3) | (x >> 2); };
85 return qRgb(scale(r), scale(g), scale(b));
86 }
87
88 void VramBitMappedView::decodeSCR12()
89 {
90 auto offset = vramAddress;
91 for (int y = 0; y < lines; ++y) {
92 for (int x = 0; x < 256; x += 4) {
93 uint8_t p[4];
94 p[0] = vramBase[interleave(offset++)];
95 p[1] = vramBase[interleave(offset++)];
96 p[2] = vramBase[interleave(offset++)];
97 p[3] = vramBase[interleave(offset++)];
98 int j = (p[2] & 7) + ((p[3] & 3) << 3) - ((p[3] & 4) << 3);
99 int k = (p[0] & 7) + ((p[1] & 3) << 3) - ((p[1] & 4) << 3);
100 for (int n = 0; n < 4; ++n) {
101 setPixel2x2(x + n, y, decodeYJK(p[n] >> 3, j, k));
102 }
103 }
104 }
105 }
106
107 void VramBitMappedView::decodeSCR10()
108 {
109 auto offset = vramAddress;
110 for (int y = 0; y < lines; ++y) {
111 for (int x = 0; x < 256; x += 4) {
112 uint8_t p[4];
113 p[0] = vramBase[interleave(offset++)];
114 p[1] = vramBase[interleave(offset++)];
115 p[2] = vramBase[interleave(offset++)];
116 p[3] = vramBase[interleave(offset++)];
117 int j = (p[2] & 7) + ((p[3] & 3) << 3) - ((p[3] & 4) << 3);
118 int k = (p[0] & 7) + ((p[1] & 3) << 3) - ((p[1] & 4) << 3);
119 for (int n = 0; n < 4; ++n) {
120 QRgb c = (p[n] & 0x08) ? msxPalette[p[n] >> 4] // YAE
121 : decodeYJK(p[n] >> 3, j, k); // YJK
122 setPixel2x2(x + n, y, c);
123 }
124 }
125 }
126 }
127
128 void VramBitMappedView::decodeSCR8()
129 {
130 auto offset = vramAddress;
131 for (int y = 0; y < lines; ++y) {
132 for (int x = 0; x < 256; ++x) {
133 uint8_t val = vramBase[interleave(offset++)];
134 int b = val & 0x03;
135 int r = val & 0x1C;
136 int g = val & 0xE0;
137
138 b = b | (b << 2) | (b << 4) | (b << 6);
139 r = (r >> 2) | r | (r << 3);
140 g = g | (g >> 3) | (g >> 6);
141
142 setPixel2x2(x, y, qRgb(r, g, b));
143 }
144 }
145 }
146
147 QRgb VramBitMappedView::getColor(int c)
148 {
149 // TODO do we need to look at the TP bit???
150 return msxPalette[c ? c : borderColor];
151 }
152
153 void VramBitMappedView::decodeSCR7()
154 {
155 auto offset = vramAddress;
156 for (int y = 0; y < lines; ++y) {
157 for (int x = 0; x < 512; x += 2) {
158 int val = vramBase[interleave(offset++)];
159 setPixel1x2(x + 0, y, getColor((val >> 4) & 15));
160 setPixel1x2(x + 1, y, getColor((val >> 0) & 15));
161 }
162 }
163 }
164
165 void VramBitMappedView::decodeSCR6()
166 {
167 auto offset = vramAddress;
168 for (int y = 0; y < lines; ++y) {
169 for (int x = 0; x < 512; x += 4) {
170 int val = vramBase[offset++];
171 setPixel1x2(x + 0, y, getColor((val >> 6) & 3));
172 setPixel1x2(x + 1, y, getColor((val >> 4) & 3));
173 setPixel1x2(x + 2, y, getColor((val >> 2) & 3));
174 setPixel1x2(x + 3, y, getColor((val >> 0) & 3));
175 }
176 }
177 }
178
179 void VramBitMappedView::decodeSCR5()
180 {
181 auto offset = vramAddress;
182 for (int y = 0; y < lines; ++y) {
183 for (int x = 0; x < 256; x += 2) {
184 int val = vramBase[offset++];
185 setPixel2x2(x + 0, y, getColor((val >> 4) & 15));
186 setPixel2x2(x + 1, y, getColor((val >> 0) & 15));
187 }
188 }
189 }
190
191 void VramBitMappedView::paintEvent(QPaintEvent* /*event*/)
192 {
193 QRect srcRect(0, 0, 512, 2 * lines);
194 QRect dstRect(0, 0, int(512.0f * zoomFactor), int(2.0f * float(lines) * zoomFactor));
195 QPainter qp(this);
196 //qp.drawImage(rect(),image,srcRect);
197 qp.drawPixmap(dstRect, pixImage, srcRect);
198 }
199
200 void VramBitMappedView::refresh()
201 {
202 decodePallet();
203 decode();
204 update();
205 }
206
207 void VramBitMappedView::mouseMoveEvent(QMouseEvent* e)
208 {
209 static const unsigned bytesPerLine[] = {
210 0, //screen 0
211 1, //screen 1
212 2, // 2
213 3, // 3
214 4,
215 128, // 5
216 128,
217 256, // 7
218 256,
219 256, // 9
220 256,
221 256,
222 256
223 };
224 static const unsigned pixelsPerByte[] = {
225 0,1,2,3,4,
226 2, // 5
227 4,
228 2, // 7
229 1,
230 1, //9
231 1,
232 1,
233 1
234 };
235
236 int x = int(float(e->x()) / zoomFactor);
237 int y = int(float(e->y()) / zoomFactor) / 2;
238
239 // I see negative y-coords sometimes, so for safety clip the coords
240 x = std::clamp(x, 0, 511);
241 y = std::clamp(y, 0, 255);
242
243 if ((screenMode != 6) && (screenMode != 7)) {
244 x /= 2;
245 }
246
247 unsigned offset = bytesPerLine[screenMode] * y
248 + x / pixelsPerByte[screenMode];
249 unsigned addr = offset + vramAddress;
250 int val = (screenMode >= 7) ? vramBase[interleave(addr)]
251 : vramBase[addr];
252
253 int color = [&] {
254 switch (screenMode) {
255 case 5: case 7:
256 return ((x & 1) ? val : (val >> 4)) & 15;
257 case 6:
258 return (val >> (2 * (3 - (x & 3)))) & 3;
259 case 8: case 10: case 11: case 12:
260 return val;
261 default:
262 return 0; // avoid warning
263 }
264 }();
265 emit imageHovered(x, y, color, addr, val);
266 }
267
268 void VramBitMappedView::mousePressEvent(QMouseEvent* e)
269 {
270 // since mouseMove only emits the correct signal we reuse/abuse that method
271 mouseMoveEvent(e);
272
273 decodePallet();
274 decode();
275 update();
276 }
277
278 void VramBitMappedView::setBorderColor(int value)
279 {
280 borderColor = std::clamp(value, 0, 15);
281 decodePallet();
282 decode();
283 update();
284 }
285
286 void VramBitMappedView::setScreenMode(int mode)
287 {
288 screenMode = mode;
289 decode();
290 update();
291 }
292
293 void VramBitMappedView::setLines(int nrLines)
294 {
295 lines = nrLines;
296 decode();
297 setFixedSize(int(512.0f * zoomFactor), int(float(lines) * 2.0f * zoomFactor));
298 update();
299 //setZoom(zoomFactor);
300 }
301
302 void VramBitMappedView::setVramAddress(int adr)
303 {
304 vramAddress = adr;
305 decodePallet();
306 decode();
307 update();
308 }
309
310 void VramBitMappedView::setVramSource(const uint8_t* adr)
311 {
312 vramBase = adr;
313 decodePallet();
314 decode();
315 update();
316 }
317
318 void VramBitMappedView::setPaletteSource(const uint8_t* adr)
319 {
320 palette = adr;
321 decodePallet();
322 decode();
323 update();
324 }