-
Notifications
You must be signed in to change notification settings - Fork 0
/
blitter.cpp
372 lines (256 loc) · 6.62 KB
/
blitter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "blitter.h"
#include "memio.h"
#include "tangerine.h"
static uint32_t bltWrite( uint32_t addr, uint16_t data )
{
uint8_t memMask;
uint32_t memData;
uint32_t memAddr;
memData = ( data << 16 ) | data;
if( addr & 2 )
{
memMask = 0b1100;
}
else
{
memMask = 0b0011;
}
//0x24000000
memAddr = 0x20000000 | ( addr & 0x3fffffc ) ;
return storeData( memAddr, memMask, memData );
}
static uint32_t bltRead( uint32_t addr )
{
uint32_t memData;
uint32_t memAddr;
//0x20800000
memAddr = 0x20000000 | ( addr & 0x3fffffc ) ;
memData = fetchData( memAddr );
if( addr & 2 )
{
return ( memData >> 16 ) & 0xffff;
}
else
{
return memData & 0xffff;
}
}
static uint32_t bltCalcAlpha( uint32_t pixelA, uint32_t pixelB, uint32_t alpha )
{
uint32_t ra;
uint32_t ga;
uint32_t ba;
uint32_t rb;
uint32_t gb;
uint32_t bb;
uint32_t rd;
uint32_t gd;
uint32_t bd;
alpha = alpha * 8;
ra = tgColor565GetR( pixelA );
ga = tgColor565GetG( pixelA );
ba = tgColor565GetB( pixelA );
rb = tgColor565GetR( pixelB );
gb = tgColor565GetG( pixelB );
bb = tgColor565GetB( pixelB );
rd = rb * ( 255 - alpha );
gd = gb * ( 255 - alpha );
bd = bb * ( 255 - alpha );
rd += ra * alpha;
gd += ga * alpha;
bd += ba * alpha;
rd >>= 8;
gd >>= 8;
bd >>= 8;
return tgColor565( rd, gd, bd );
}
static uint32_t blitterFill( tgBlitterRegs_t *regs )
{
uint32_t destAddress;
uint32_t x;
uint32_t y;
destAddress = regs->daAddress & 0x3fffffe;
for( y = 0; y < regs->daHeight; y++ )
{
for( x = 0; x < regs->daWidth; x++ )
{
bltWrite( destAddress, regs->input0 );
destAddress += 2;
}
destAddress += regs->daColAdd * 2;
}
return RV_OK;
}
static uint32_t blitterCopy( tgBlitterRegs_t *regs, uint32_t cmd )
{
uint32_t srcAddress;
uint32_t destAddress;
uint32_t x;
uint32_t y;
uint16_t pixel;
srcAddress = regs->saAddress & 0x3fffffe;
destAddress = regs->daAddress & 0x3fffffe;
if( cmd & 1 )
{
for( y = 0; y < regs->daHeight; y++ )
{
for( x = 0; x < regs->daWidth; x++ )
{
pixel = bltRead( srcAddress );
if( pixel != ( regs->input0 & 0xffff ) )
{
bltWrite( destAddress, pixel );
}
srcAddress += 2;
destAddress += 2;
}
srcAddress += regs->saColAdd * 2;
destAddress += regs->daColAdd * 2;
}
}
else
{
for( y = 0; y < regs->daHeight; y++ )
{
for( x = 0; x < regs->daWidth; x++ )
{
bltWrite( destAddress, bltRead( srcAddress ) );
srcAddress += 2;
destAddress += 2;
}
srcAddress += regs->saColAdd * 2;
destAddress += regs->daColAdd * 2;
}
}
return RV_OK;
}
static uint32_t blitterCopyAlpha( tgBlitterRegs_t *regs, uint32_t cmd )
{
uint32_t srcAAddress;
uint32_t srcBAddress;
uint32_t destAddress;
uint32_t x;
uint32_t y;
uint16_t pixelA;
uint16_t pixelB;
srcAAddress = regs->saAddress & 0x3fffffe;
srcBAddress = regs->sbAddress & 0x3fffffe;
destAddress = regs->daAddress & 0x3fffffe;
if( cmd & 1 )
{
for( y = 0; y < regs->daHeight; y++ )
{
for( x = 0; x < regs->daWidth; x++ )
{
pixelA = bltRead( srcAAddress );
pixelB = bltRead( srcBAddress );
if( pixelA != ( regs->input1 & 0xffff ) )
{
bltWrite( destAddress, bltCalcAlpha( pixelA, pixelB, regs->input0 ) );
}
srcAAddress += 2;
srcBAddress += 2;
destAddress += 2;
}
srcAAddress += regs->saColAdd * 2;
srcBAddress += regs->sbColAdd * 2;
destAddress += regs->daColAdd * 2;
}
}
else
{
for( y = 0; y < regs->daHeight; y++ )
{
for( x = 0; x < regs->daWidth; x++ )
{
pixelA = bltRead( srcAAddress );
pixelB = bltRead( srcBAddress );
bltWrite( destAddress, bltCalcAlpha( pixelA, pixelB, regs->input0 ) );
srcAAddress += 2;
srcBAddress += 2;
destAddress += 2;
}
srcAAddress += regs->saColAdd * 2;
srcBAddress += regs->sbColAdd * 2;
destAddress += regs->daColAdd * 2;
}
}
return RV_OK;
}
static uint32_t blitterScaledCopy( tgBlitterRegs_t *regs, uint32_t cmd )
{
uint32_t srcAddress;
uint32_t destAddress;
uint32_t x;
uint32_t y;
uint16_t pixel;
uint32_t sx;
uint32_t sy;
destAddress = regs->daAddress & 0x3fffffe;
sx = 0;
sy = 0;
if( cmd & 1 )
{
for( y = 0; y < regs->daHeight; y++ )
{
for( x = 0; x < regs->daWidth; x++ )
{
srcAddress = regs->saAddress & 0x3fffffe;
srcAddress += ( sx >> 15 ) + ( sy >> 16 ) * regs->saRowWidth * 2;
sx += regs->input0;
pixel = bltRead( srcAddress );
if( pixel != ( regs->input2 & 0xffff ) )
{
bltWrite( destAddress, pixel );
}
srcAddress += 2;
destAddress += 2;
}
sx = 0;
sy += regs->input1;
destAddress += regs->daColAdd * 2;
}
}
else
{
for( y = 0; y < regs->daHeight; y++ )
{
for( x = 0; x < regs->daWidth; x++ )
{
srcAddress = regs->saAddress & 0x3fffffe;
srcAddress += ( sx >> 15 ) + ( sy >> 16 ) * regs->saRowWidth * 2;
sx += regs->input0;
bltWrite( destAddress, bltRead( srcAddress ) );
srcAddress += 2;
destAddress += 2;
}
sx = 0;
sy += regs->input1;
destAddress += regs->daColAdd * 2;
}
}
return RV_OK;
}
uint32_t blitterCommand( tgBlitterRegs_t *regs, uint32_t cmd )
{
regs->saColAdd = regs->saRowWidth - regs->saWidth;
regs->sbColAdd = regs->sbRowWidth - regs->saWidth;
regs->scColAdd = regs->scRowWidth - regs->saWidth;
regs->daColAdd = regs->daRowWidth - regs->daWidth;
regs->dbColAdd = regs->dbRowWidth - regs->daWidth;
switch( ( cmd >> 8 ) & 0xff )
{
case 0x01:
return blitterFill( regs );
break;
case 0x02:
return blitterCopy( regs, cmd );
break;
case 0x03:
return blitterCopyAlpha( regs, cmd );
break;
case 0x04:
return blitterScaledCopy( regs, cmd );
}
return RV_OK;
}