-
Notifications
You must be signed in to change notification settings - Fork 45
/
camera.c
522 lines (426 loc) · 13.6 KB
/
camera.c
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// SPDX-License-Identifier: GPL-2.0-or-later
//
// Copyright (c) 2011-2015, 2019-2020, Antonio Niño Díaz
//
// GiiBiiAdvance - GBA/GB emulator
#include <stdlib.h>
#include <string.h>
#include "../build_options.h"
#include "../config.h"
#include "../debug_utils.h"
#include "../general_utils.h"
#include "../webcam_utils.h"
#include "cpu.h"
#include "gameboy.h"
//------------------------------------------------------------------------------
extern _GB_CONTEXT_ GameBoy;
//------------------------------------------------------------------------------
// Webcam image (exposed in gc_core/camera.h, values in the range 0-255)
int gb_camera_webcam_output[GBCAM_SENSOR_W][GBCAM_SENSOR_H];
// Image processed by the retina chip
static int gb_cam_retina_output_buf[GBCAM_SENSOR_W][GBCAM_SENSOR_H];
void GB_CameraEnd(void)
{
Webcam_End();
}
int GB_CameraInit(void)
{
return Webcam_Init();
}
static int webcam_frame_delay = 0;
void GB_CameraWebcamCapture(void)
{
if (webcam_frame_delay == 0)
{
Webcam_GetFrame();
webcam_frame_delay = 5;
}
}
void GB_CameraWebcamDelayDecrease(void)
{
if (webcam_frame_delay > 0)
webcam_frame_delay--;
}
//----------------------------------------------------------------
static int gb_camera_clock_counter = 0;
void GB_CameraClockCounterReset(void)
{
gb_camera_clock_counter = 0;
}
static int GB_CameraClockCounterGet(void)
{
return gb_camera_clock_counter;
}
static void GB_CameraClockCounterSet(int new_reference_clocks)
{
gb_camera_clock_counter = new_reference_clocks;
}
void GB_CameraUpdateClocksCounterReference(int reference_clocks)
{
if (GameBoy.Emulator.MemoryController != MEM_CAMERA)
return;
int increment_clocks = reference_clocks - GB_CameraClockCounterGet();
_GB_CAMERA_CART_ *cam = &GameBoy.Emulator.CAM;
if (cam->clocks_left > 0)
{
cam->clocks_left -= increment_clocks;
if (cam->clocks_left <= 0)
{
cam->reg[0] &= ~BIT(0); // Ready
cam->clocks_left = 0;
}
}
GB_CameraClockCounterSet(reference_clocks);
}
int GB_CameraGetClocksToNextEvent(void)
{
if (GameBoy.Emulator.MemoryController != MEM_CAMERA)
return 0x7FFFFFFF;
_GB_CAMERA_CART_ *cam = &GameBoy.Emulator.CAM;
if (cam->clocks_left == 0)
return 0x7FFFFFFF;
return cam->clocks_left;
}
//----------------------------------------------------------------------------
static int gb_clamp_int(int min, int value, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static int gb_min_int(int a, int b)
{
return (a < b) ? a : b;
}
static int gb_max_int(int a, int b)
{
return (a > b) ? a : b;
}
static u32 gb_cam_matrix_process(u32 value, int x, int y)
{
_GB_CAMERA_CART_ *cam = &GameBoy.Emulator.CAM;
x = x & 3;
y = y & 3;
int base = 6 + (y * 4 + x) * 3;
u32 r0 = cam->reg[base + 0];
u32 r1 = cam->reg[base + 1];
u32 r2 = cam->reg[base + 2];
if (value < r0)
return 0x00;
if (value < r1)
return 0x40;
if (value < r2)
return 0x80;
return 0xC0;
}
static void GB_CameraTakePicture(void)
{
// Buffer for the image as seen after processed by the sensor
int (*temp_buf)[GBCAM_SENSOR_W];
temp_buf = calloc(GBCAM_SENSOR_W * GBCAM_SENSOR_H, sizeof(int));
// Buffer after controller matrix
int (*fourcolorsbuffer)[GBCAM_W];
fourcolorsbuffer = calloc(GBCAM_W * GBCAM_H, sizeof(int));
if ((temp_buf == NULL) || (fourcolorsbuffer == NULL))
{
Debug_ErrorMsgArg("%s(): Not enough memory.");
goto cleanup;
}
_GB_CAMERA_CART_ *cam = &GameBoy.Emulator.CAM;
//------------------------------------------------
// Get webcam image
// ----------------
GB_CameraWebcamCapture();
//------------------------------------------------
// Get configuration
// -----------------
// Register 0
u32 P_bits = 0;
u32 M_bits = 0;
switch ((cam->reg[0] >> 1) & 3)
{
case 0:
P_bits = 0x00;
M_bits = 0x01;
break;
case 1:
P_bits = 0x01;
M_bits = 0x00;
break;
case 2:
case 3:
P_bits = 0x01;
M_bits = 0x02;
break;
default:
break;
}
// Register 1
u32 N_bit = (cam->reg[1] & BIT(7)) >> 7;
u32 VH_bits = (cam->reg[1] & (BIT(6) | BIT(5))) >> 5;
// Registers 2 and 3
u32 EXPOSURE_bits = cam->reg[3] | (cam->reg[2] << 8);
// Register 4
const float edge_ratio_lut[8] = {
0.50, 0.75, 1.00, 1.25, 2.00, 3.00, 4.00, 5.00
};
float EDGE_alpha = edge_ratio_lut[(cam->reg[4] & 0x70) >> 4];
u32 E3_bit = (cam->reg[4] & BIT(7)) >> 7;
u32 I_bit = (cam->reg[4] & BIT(3)) >> 3;
//------------------------------------------------
// Calculate timings
// -----------------
cam->clocks_left = 4 * (32446 + (N_bit ? 0 : 512) + 16 * EXPOSURE_bits);
//------------------------------------------------
// Sensor handling
// ---------------
// Copy webcam buffer to sensor buffer applying color correction and
// exposure time
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
int value = gb_camera_webcam_output[i][j];
value = (value * EXPOSURE_bits)
/ EmulatorConfig.gbcam_exposure_reference;
value = 128 + (((value - 128) * 1) / 8); // "adapt" to "3.1"/5.0 V
gb_cam_retina_output_buf[i][j] = gb_clamp_int(0, value, 255);
}
}
if (I_bit) // Invert image
{
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
gb_cam_retina_output_buf[i][j] =
255 - gb_cam_retina_output_buf[i][j];
}
}
}
// Make signed
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
gb_cam_retina_output_buf[i][j] =
gb_cam_retina_output_buf[i][j] - 128;
}
}
u32 filtering_mode = (N_bit << 3) | (VH_bits << 1) | E3_bit;
switch (filtering_mode)
{
// 1-D filtering
case 0x0:
{
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
temp_buf[j][i] = gb_cam_retina_output_buf[i][j];
}
}
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
int ms = temp_buf[gb_min_int(j + 1, GBCAM_SENSOR_H - 1)][i];
int px = temp_buf[j][i];
int value = 0;
if (P_bits & BIT(0))
value += px;
if (P_bits & BIT(1))
value += ms;
if (M_bits & BIT(0))
value -= px;
if (M_bits & BIT(1))
value -= ms;
gb_cam_retina_output_buf[i][j] =
gb_clamp_int(-128, value, 127);
}
}
break;
}
// 1-D filtering + Horiz. enhancement : P + {2P - (MW + ME)} * alpha
case 0x2:
{
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
int mw = gb_cam_retina_output_buf[gb_max_int(0, i - 1)][j];
int me = gb_cam_retina_output_buf
[gb_min_int(i + 1, GBCAM_SENSOR_W - 1)][j];
int px = gb_cam_retina_output_buf[i][j];
temp_buf[j][i] = gb_clamp_int(0,
px + ((2 * px - mw - me) * EDGE_alpha),
255);
}
}
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
int ms = temp_buf[gb_min_int(j + 1, GBCAM_SENSOR_H - 1)][i];
int px = temp_buf[j][i];
int value = 0;
if (P_bits & BIT(0))
value += px;
if (P_bits & BIT(1))
value += ms;
if (M_bits & BIT(0))
value -= px;
if (M_bits & BIT(1))
value -= ms;
gb_cam_retina_output_buf[i][j] =
gb_clamp_int(-128, value, 127);
}
}
break;
}
// 2D enhancement : P + {4P - (MN + MS + ME + MW)} * alpha
case 0xE:
{
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
int ms = gb_cam_retina_output_buf[i]
[gb_min_int(j + 1, GBCAM_SENSOR_H - 1)];
int mn = gb_cam_retina_output_buf[i][gb_max_int(0, j - 1)];
int mw = gb_cam_retina_output_buf[gb_max_int(0, i - 1)][j];
int me = gb_cam_retina_output_buf
[gb_min_int(i + 1, GBCAM_SENSOR_W - 1)][j];
int px = gb_cam_retina_output_buf[i][j];
temp_buf[j][i] = gb_clamp_int(-128,
px + ((4 * px - mw - me - mn - ms) * EDGE_alpha),
127);
}
}
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
gb_cam_retina_output_buf[i][j] = temp_buf[j][i];
}
}
break;
}
// In my GB Camera cartridge this is always the same color. The
// datasheet of the sensor doesn't have this configuration documented.
// Maybe this is a bug?
case 0x1:
{
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
gb_cam_retina_output_buf[i][j] = 0;
}
}
break;
}
// Ignore filtering
default:
{
Debug_DebugMsgArg("Unsupported GB Cam mode: 0x%X\n%02X %02X %02X "
"%02X %02X %02X",
filtering_mode, cam->reg[0], cam->reg[1],
cam->reg[2], cam->reg[3], cam->reg[4],
cam->reg[5]);
break;
}
}
// Make unsigned
for (int j = 0; j < GBCAM_SENSOR_H; j++)
{
for (int i = 0; i < GBCAM_SENSOR_W; i++)
{
gb_cam_retina_output_buf[i][j] =
gb_cam_retina_output_buf[i][j] + 128;
}
}
//------------------------------------------------
// Controller handling
// -------------------
// Convert to Game Boy colors using the controller matrix
for (int j = 0; j < GBCAM_H; j++)
{
for (int i = 0; i < GBCAM_W; i++)
{
int v;
v = gb_cam_retina_output_buf[i][j + (GBCAM_SENSOR_EXTRA_LINES / 2)];
fourcolorsbuffer[j][i] = gb_cam_matrix_process(v, i, j);
}
}
// Convert to tiles
u8 finalbuffer[14][16][16]; // Final buffer
memset(finalbuffer, 0, sizeof(finalbuffer));
for (int j = 0; j < GBCAM_H; j++)
{
for (int i = 0; i < GBCAM_W; i++)
{
u8 outcolor = 3 - (fourcolorsbuffer[j][i] >> 6);
u8 *tile_base = finalbuffer[j >> 3][i >> 3];
tile_base = &tile_base[(j & 7) * 2];
if (outcolor & 1)
tile_base[0] |= 1 << (7 - (7 & i));
if (outcolor & 2)
tile_base[1] |= 1 << (7 - (7 & i));
}
}
// Copy to cart ram...
memcpy(&(GameBoy.Memory.ExternRAM[0][0xA100 - 0xA000]), finalbuffer,
sizeof(finalbuffer));
cleanup:
free(temp_buf);
free(fourcolorsbuffer);
}
int GB_CameraReadRegister(int address)
{
GB_CameraUpdateClocksCounterReference(GB_CPUClockCounterGet());
_GB_CAMERA_CART_ *cam = &GameBoy.Emulator.CAM;
int reg = (address & 0x7F); // All registers are mirrored
if (reg == 0)
return cam->reg[0]; // 'Ready' register
// Others are write-only and they return 0.
return 0x00;
}
void GB_CameraWriteRegister(int address, int value)
{
_GB_CAMERA_CART_ *cam = &GameBoy.Emulator.CAM;
int reg = (address & 0x7F); // Mirror
if (reg < 0x36)
{
//Debug_LogMsgArg("Cam [0x%02X]=0x%02X", (u32)(u16)(address & 0xFF),
// (u32)(u8)value);
cam->reg[reg] = value;
if (reg == 0) // Take picture...
{
cam->reg[0] &= 7;
if (value & BIT(0))
GB_CameraTakePicture();
}
}
else
{
Debug_DebugMsgArg("Camera wrote to invalid register: [%04x] = %02X",
address, value);
}
}
//----------------------------------------------------------------
int GB_MapperIsGBCamera(void)
{
return (GameBoy.Emulator.MemoryController == MEM_CAMERA);
}
int GB_CameraWebcamImageGetPixel(int x, int y)
{
return gb_camera_webcam_output[x][y + (GBCAM_SENSOR_EXTRA_LINES / 2)];
}
int GB_CameraRetinaProcessedImageGetPixel(int x, int y)
{
// 4 extra rows, 2 on each border
return gb_cam_retina_output_buf[x][y + (GBCAM_SENSOR_EXTRA_LINES / 2)];
}