eos.c 159 KB
Newer Older
Antony Pavlov's avatar
Antony Pavlov committed
1
2
3
4
5
6
#include "hw/hw.h"
#include "hw/loader.h"
#include "sysemu/sysemu.h"
#include "hw/devices.h"
#include "hw/boards.h"
#include "exec/address-spaces.h"
7
#include "exec/memory-internal.h"
a1ex's avatar
a1ex committed
8
#include "exec/ram_addr.h"
Antony Pavlov's avatar
Antony Pavlov committed
9
#include "hw/sysbus.h"
a1ex's avatar
a1ex committed
10
11
12
#include "ui/console.h"
#include "ui/pixel_ops.h"
#include "hw/display/framebuffer.h"
Niklas Fejes's avatar
Niklas Fejes committed
13
#include "hw/sd/sd.h"
14
#include "sysemu/char.h"
15
#include <hw/ide/internal.h>
16
#include "hw/arm/arm.h"
Antony Pavlov's avatar
Antony Pavlov committed
17
#include "eos.h"
18
#include "dbi/logging.h"
Antony Pavlov's avatar
Antony Pavlov committed
19

20
#include "hw/eos/model_list.h"
Niklas Fejes's avatar
Niklas Fejes committed
21
22
23
24
#include "hw/eos/eos_ml_helpers.h"
#include "hw/eos/mpu.h"
#include "hw/eos/serial_flash.h"
#include "hw/eos/eos_utils.h"
Niklas Fejes's avatar
Niklas Fejes committed
25
#include "eos_bufcon_100D.h"
26
#include "hw/eos/engine.h"
27

Niklas Fejes's avatar
Niklas Fejes committed
28
#define IGNORE_CONNECT_POLL
Niklas Fejes's avatar
Niklas Fejes committed
29

30
31
32
#define DIGIC_TIMER_STEP 0x100
#define DIGIC_TIMER_MASK (0xFFFFF & ~(DIGIC_TIMER_STEP-1))

Niklas Fejes's avatar
Niklas Fejes committed
33
34
35
36
/* Machine class */

typedef struct {
    MachineClass parent;
37
    struct eos_model_desc * model;
Niklas Fejes's avatar
Niklas Fejes committed
38
39
} EosMachineClass;

Niklas Fejes's avatar
Niklas Fejes committed
40
41
#define EOS_DESC_BASE    "Canon EOS"
#define TYPE_EOS_MACHINE "eos"
Niklas Fejes's avatar
Niklas Fejes committed
42
43
44
45
46
#define EOS_MACHINE_GET_CLASS(obj) \
    OBJECT_GET_CLASS(EosMachineClass, obj, TYPE_EOS_MACHINE)
#define EOS_MACHINE_CLASS(klass) \
    OBJECT_CLASS_CHECK(EosMachineClass, klass, TYPE_EOS_MACHINE)

47
static void eos_init_common(MachineState *machine);
48

Niklas Fejes's avatar
Niklas Fejes committed
49
50
51
static void eos_class_init(ObjectClass *oc, void *data)
{
    MachineClass *mc = MACHINE_CLASS(oc);
52
    mc->desc = EOS_DESC_BASE;
53
    mc->init = eos_init_common;
Niklas Fejes's avatar
Niklas Fejes committed
54
}
55
static const TypeInfo canon_eos_info = {
Niklas Fejes's avatar
Niklas Fejes committed
56
57
58
59
60
61
62
63
64
    .name = TYPE_EOS_MACHINE,
    .parent = TYPE_MACHINE,
    .abstract = true,
//  .instance_size = sizeof(MachineState), // Could probably be used for something
//  .instance_init = vexpress_instance_init,
    .class_size = sizeof(EosMachineClass),
    .class_init = eos_class_init,
};

65
66
67
68
69

static void eos_cam_class_init(ObjectClass *oc, void *data)
{
    MachineClass *mc = MACHINE_CLASS(oc);
    EosMachineClass *emc = EOS_MACHINE_CLASS(oc);
70
71
    struct eos_model_desc * model = (struct eos_model_desc*) data;
    emc->model = model;
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

    /* Create description from name */
    int desc_size = sizeof(EOS_DESC_BASE) + strlen(model->name) + 1;
    char * desc = (char*)malloc(desc_size * sizeof(char));
    if (desc) {
        snprintf(desc, desc_size, EOS_DESC_BASE " %s", model->name);
    }
    mc->desc = desc;
}

static void eos_cam_class_finalize(ObjectClass *oc, void *data)
{
    MachineClass *mc = MACHINE_CLASS(oc);
    if (mc->desc) {
        free((char*)mc->desc);
        mc->desc = NULL;
    }
}
Niklas Fejes's avatar
Niklas Fejes committed
90
91
92
93


static void eos_machine_init(void)
{
94
    /* Register base type */
95
    type_register_static(&canon_eos_info);
96
97
98
99
100
101
102
103
104

    /* Base info for camera models */
    char name[32]; // "XXXX-machine"
    TypeInfo info = {
        .name = name,
        .class_init = eos_cam_class_init,
        .class_finalize = eos_cam_class_finalize,
        .parent = TYPE_EOS_MACHINE,
    };
105
    
106
    /* Loop over all models listed in model_list.c */
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    
    /* fill in the defaults from generic entries */
    /* note: generic entries don't have a name */
    for (const struct eos_model_desc * generic = eos_model_list; generic->digic_version; generic++)
    {
        if (!generic->name)
        {
            for (struct eos_model_desc * model = eos_model_list; model->digic_version; model++)
            {
                if (model->name && model->digic_version == generic->digic_version)
                {
                    /* copy settings from generic to model */
                    for (int i = 0; i < COUNT(model->params); i++)
                    {
                        if (model->params[i] == 0)
                        {
123
                            // fprintf(stderr, "%s: params[%d] = %x\n", model->name, i, generic->params[i]);
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
                            model->params[i] = generic->params[i];
                        }
                    }
                }
            }
        }
    }

    /* then register every supported camera model */
    for (struct eos_model_desc * model = eos_model_list; model->digic_version; model++)
    {
        if (model->name)
        {
            snprintf(name, 32, "%s" TYPE_MACHINE_SUFFIX, model->name);
            info.class_data = (void*)model;
            type_register(&info);
        }
141
    }
Niklas Fejes's avatar
Niklas Fejes committed
142
143
}

Niklas Fejes's avatar
Niklas Fejes committed
144
machine_init(eos_machine_init);
Niklas Fejes's avatar
Niklas Fejes committed
145
146
147



Antony Pavlov's avatar
Antony Pavlov committed
148
149
150
151
152
EOSRegionHandler eos_handlers[] =
{
    { "FlashControl", 0xC0000000, 0xC0001FFF, eos_handle_flashctrl, 0 },
    { "ROM0",         0xF8000000, 0xFFFFFFFF, eos_handle_rom, 0 },
    { "ROM1",         0xF0000000, 0xF7FFFFFF, eos_handle_rom, 1 },
153
154
155
156
157
158
    { "Interrupt",    0xC0200000, 0xC02000FF, eos_handle_intengine_vx, 0 }, /* mostly used on D2/3, but also 60D */
    { "Interrupt",    0xC0201000, 0xC0201FFF, eos_handle_intengine, 0 },    /* <= D5 */
    { "Interrupt",    0xD4011000, 0xD4011FFF, eos_handle_intengine, 1 },    /* D6; first core in D7 */
    { "Interrupt",    0xD5011000, 0xD5011FFF, eos_handle_intengine, 2 },    /* second core in D7 */
    { "Interrupt",    0xD02C0200, 0xD02C02FF, eos_handle_intengine, 3 },    /* 5D3 eeko */
    { "Interrupt",    0xC1000000, 0xC100FFFF, eos_handle_intengine_gic, 7 },/* D7 */
Antony Pavlov's avatar
Antony Pavlov committed
159
    { "Timers",       0xC0210000, 0xC0210FFF, eos_handle_timers, 0 },
160
    { "Timers",       0xD4000240, 0xD4000410, eos_handle_timers, 1 },
161
    { "Timers",       0xD02C1500, 0xD02C15FF, eos_handle_timers, 2 },
162
    { "Timer",        0xC0242014, 0xC0242014, eos_handle_digic_timer, 0 },
163
    { "Timer",        0xD400000C, 0xD400000C, eos_handle_digic_timer, 1 },  /* not sure */
164
    { "HPTimer",      0xC0243000, 0xC0243FFF, eos_handle_hptimer, 0 },
Antony Pavlov's avatar
Antony Pavlov committed
165
    { "GPIO",         0xC0220000, 0xC022FFFF, eos_handle_gpio, 0 },
166
    { "Basic",        0xC0100000, 0xC0100FFF, eos_handle_basic, 0 },
167
    { "Basic",        0xC0400000, 0xC0400FFF, eos_handle_basic, 1 },
168
    { "Basic",        0xC0720000, 0xC0720FFF, eos_handle_basic, 2 },
169
    { "SDIO0",        0xC0C00000, 0xC0C00FFF, eos_handle_sdio, 0 },
a1ex's avatar
a1ex committed
170
171
    { "SDIO1",        0xC0C10000, 0xC0C10FFF, eos_handle_sdio, 1 },
    { "SDIO2",        0xC0C20000, 0xC0C20FFF, eos_handle_sdio, 2 },
172
    { "SFIO4",        0xC0C40000, 0xC0C40FFF, eos_handle_sdio, 4 },
173
    { "SDIO6",        0xC8060000, 0xC8060FFF, eos_handle_sdio, 6 },
174
    { "SFIO7",        0xC8070000, 0xC8070FFF, eos_handle_sdio, 7 },
175
    { "CFDMA0",       0xC0500000, 0xC0500FFF, eos_handle_cfdma, 0 },
176
    { "SDDMA1",       0xC0510000, 0xC05100FF, eos_handle_sddma, 1 },
177
    { "SDDMA3",       0xC0530000, 0xC0530FFF, eos_handle_sddma, 3 },
178
    { "SDDMA6",       0xC8020000, 0xC80200FF, eos_handle_sddma, 6 },
179
180
    { "CFATA0",       0xC0600000, 0xC060FFFF, eos_handle_cfata, 0 },
    { "CFATA2",       0xC0620000, 0xC062FFFF, eos_handle_cfata, 2 },
181
    { "UART",         0xC0800000, 0xC08000FF, eos_handle_uart, 0 },
182
    { "UART",         0xC0810000, 0xC08100FF, eos_handle_uart, 1 },
183
    { "UART",         0xC0270000, 0xC027000F, eos_handle_uart, 2 },
alex@thinkpad's avatar
alex@thinkpad committed
184
    { "I2C",          0xC0090000, 0xC00900FF, eos_handle_i2c, 0 },
Antony Pavlov's avatar
Antony Pavlov committed
185
186
187
    { "SIO0",         0xC0820000, 0xC08200FF, eos_handle_sio, 0 },
    { "SIO1",         0xC0820100, 0xC08201FF, eos_handle_sio, 1 },
    { "SIO2",         0xC0820200, 0xC08202FF, eos_handle_sio, 2 },
a1ex's avatar
a1ex committed
188
    { "SIO3",         0xC0820300, 0xC08203FF, eos_handle_sio3, 3 },
189
    { "SIO4",         0xC0820400, 0xC08204FF, eos_handle_sio, 4 },
alex@thinkpad's avatar
alex@thinkpad committed
190
    { "SIO6",         0xC0820600, 0xC08206FF, eos_handle_sio, 6 },
191
    { "SIO7",         0xC0820700, 0xC08207FF, eos_handle_sio, 7 },
192
    { "SIO8",         0xC0820800, 0xC08208FF, eos_handle_sio, 8 },
a1ex's avatar
a1ex committed
193
    { "MREQ",         0xC0203000, 0xC02030FF, eos_handle_mreq, 0 },
Antony Pavlov's avatar
Antony Pavlov committed
194
195
196
197
    { "DMA1",         0xC0A10000, 0xC0A100FF, eos_handle_dma, 1 },
    { "DMA2",         0xC0A20000, 0xC0A200FF, eos_handle_dma, 2 },
    { "DMA3",         0xC0A30000, 0xC0A300FF, eos_handle_dma, 3 },
    { "DMA4",         0xC0A40000, 0xC0A400FF, eos_handle_dma, 4 },
198
199
200
201
    { "DMA5",         0xC0A50000, 0xC0A500FF, eos_handle_dma, 5 },
    { "DMA6",         0xC0A60000, 0xC0A600FF, eos_handle_dma, 6 },
    { "DMA7",         0xC0A70000, 0xC0A700FF, eos_handle_dma, 7 },
    { "DMA8",         0xC0A80000, 0xC0A800FF, eos_handle_dma, 8 },
202
203
204
205
    { "CHSW",         0xC0F05000, 0xC0F05FFF, eos_handle_edmac_chsw, 0 },
    { "EDMAC",        0xC0F04000, 0xC0F04FFF, eos_handle_edmac, 0 },
    { "EDMAC",        0xC0F26000, 0xC0F26FFF, eos_handle_edmac, 1 },
    { "EDMAC",        0xC0F30000, 0xC0F30FFF, eos_handle_edmac, 2 },
206
    { "PREPRO",       0xC0F08000, 0xC0F08FFF, eos_handle_prepro, 0 },
207
208
209
210
    { "HEAD",         0xC0F07048, 0xC0F0705B, eos_handle_head, 1 },
    { "HEAD",         0xC0F0705C, 0xC0F0706F, eos_handle_head, 2 },
    { "HEAD",         0xC0F07134, 0xC0F07147, eos_handle_head, 3 },
    { "HEAD",         0xC0F07148, 0xC0F0715B, eos_handle_head, 4 },
Antony Pavlov's avatar
Antony Pavlov committed
211
    { "CARTRIDGE",    0xC0F24000, 0xC0F24FFF, eos_handle_cartridge, 0 },
a1ex's avatar
a1ex committed
212
    { "ASIF",         0xC0920000, 0xC0920FFF, eos_handle_asif, 4 },
213
    { "Display",      0xC0F14000, 0xC0F14FFF, eos_handle_display, 0 },
214
    { "Display",      0xC0F31000, 0xC0F31FFF, eos_handle_display, 1 },
215
    { "Power",        0xC0F01000, 0xC0F010FF, eos_handle_power_control, 1 },
216
    { "ADC",          0xD9800000, 0xD9800068, eos_handle_adc, 0 },
217
218
219
    { "JP51",         0xC0E00000, 0xC0E0FFFF, eos_handle_jpcore, 0 },
    { "JP62",         0xC0E10000, 0xC0E1FFFF, eos_handle_jpcore, 1 },
    { "JP57",         0xC0E20000, 0xC0E2FFFF, eos_handle_jpcore, 2 },
220

221
222
    { "EEKO",         0xD02C2000, 0xD02C243F, eos_handle_eeko_comm, 0 },

223
224
    /* generic catch-all for everything unhandled from this range */
    { "ENGIO",        0xC0F00000, 0xC0FFFFFF, eos_handle_engio, 0 },
225

226
227
228
229
    { "XDMAC",        0xD6030000, 0xD603002F, eos_handle_xdmac, 0 },
    { "XDMAC",        0xD6030030, 0xD603005F, eos_handle_xdmac, 1 },
    { "XDMAC",        0xD6030060, 0xD603008F, eos_handle_xdmac, 2 },
    { "XDMAC",        0xD6030090, 0xD60300BF, eos_handle_xdmac, 3 },
230
    { "MEMDIV",       0xD9001600, 0xD9003FFF, eos_handle_memdiv, 0 },
231
232
    { "DIGIC6",       0xD0000000, 0xDFFFFFFF, eos_handle_digic6, 0 },
    { "DIGIC6",       0xC8100000, 0xC8100FFF, eos_handle_digic6, 1 },
233
    
234
    { "ML helpers",   0xCF123000, 0xCF123EFF, eos_handle_ml_helpers, 0 },
235
};
Antony Pavlov's avatar
Antony Pavlov committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

/* io range access */
static uint64_t eos_io_read(void *opaque, hwaddr addr, uint32_t size)
{
    addr += IO_MEM_START;

    uint32_t type = MODE_READ;

    return eos_handler ( opaque, addr, type, 0 );
}

static void eos_io_write(void *opaque, hwaddr addr, uint64_t val, uint32_t size)
{
    addr += IO_MEM_START;

    uint32_t type = MODE_WRITE;

    eos_handler ( opaque, addr, type, val );
}

256
257
258
259
static const MemoryRegionOps iomem_ops = {
    .read = eos_io_read,
    .write = eos_io_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
260
261
262
263
    .valid = {
        .min_access_size = 4,
        .max_access_size = 4,
    },
264
265
};

266
267
268
269
270
271
272
273
274
275
/* fixme: how to get this called? */
/* no luck with memory_region_rom_device_set_romd... */
static uint64_t eos_rom_read(void * opaque, hwaddr addr, uint32_t size)
{
    fprintf(stderr, "ROM read: %x %x\n", (int)addr, (int)size);
    return 0;
}

static void eos_rom_write(void * opaque, hwaddr addr, uint64_t value, uint32_t size)
{
276
    const char * msg = 0;
277
278
279
280
281
    EOSState * s = (EOSState *)((intptr_t) opaque & ~1);
    uint32_t rom_id = (intptr_t) opaque & 1;
    uint32_t rom_addr = (rom_id) ? ROM1_ADDR : ROM0_ADDR;;
    uint32_t address = rom_addr + addr;

282
283
284
285
286
287
288
289
290
291
292
293
294
295
    if (strcmp(s->model->name, "1300D") == 0)
    {
        if (address == 0xF8000000 && size == 1 && value == 6)
        {
            /* Reading flash model ID? */
            /* Startup code writes to this address, but expects to read
             * different values: C2 25 39, 20 BB 19 or 01 02 19. */
            msg = "Flash model ID?";
            uint32_t model_id = 0x003925C2;
            MEM_WRITE_ROM(address, (uint8_t *) &model_id, 4);
            goto end;
        }
    }

296
297
298
299
300
301
302
303
304
305
306
307
308
    switch(size)
    {
        case 1:
            MEM_WRITE_ROM(address, (uint8_t *) &value, 1);
            break;
        case 2:
            MEM_WRITE_ROM(address, (uint8_t *) &value, 2);
            break;
        case 4:
            MEM_WRITE_ROM(address, (uint8_t *) &value, 4);
            break;
    }

309
end:;
310
311
312
    /* log all ROM writes */
    char name[16];
    snprintf(name, sizeof(name), "ROM%d:%d", rom_id, size);
313
    io_log(name, s, address, MODE_WRITE, value, 0, msg, 0, 0);
314
315
316
317

    /* make sure we execute the latest code */
    /* fixme: shouldn't this be handled internally by QEMU?! */
    tb_invalidate_phys_addr(&address_space_memory, address);
318
319
320
321
322
323
324
}

static const MemoryRegionOps rom_ops = {
    .read = eos_rom_read,
    .write = eos_rom_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
};
325

326
327
328
329
330
331
void eos_mem_read(EOSState *s, hwaddr addr, void * buf, int size)
{
    cpu_physical_memory_read(addr, buf, size);

    if (qemu_loglevel_mask(EOS_LOG_MEM_R))
    {
332
333
        /* fixme: can be optimized */
        for (int i = 0; i < size; i++)
334
        {
335
            eos_log_mem(s, addr + i, *(uint8_t*)(buf + i), 1, NOCHK_LOG);
336
337
338
339
340
341
342
343
        }
    }
}

void eos_mem_write(EOSState *s, hwaddr addr, void * buf, int size)
{
    if (qemu_loglevel_mask(EOS_LOG_MEM_W))
    {
344
345
        /* fixme: can be optimized */
        for (int i = 0; i < size; i ++)
346
        {
347
            eos_log_mem(s, addr + i, *(uint8_t*)(buf + i), 1, 1 | NOCHK_LOG);
348
349
350
351
        }
    }

    cpu_physical_memory_write(addr, buf, size);
352
}
353

354
const char * eos_get_cam_path(EOSState *s, const char * file_rel)
Antony Pavlov's avatar
Antony Pavlov committed
355
{
356
    /* all files are loaded from $QEMU_EOS_WORKDIR/CAM/ */
357
    /* or $QEMU_EOS_WORKDIR/CAM/FIRM_VER/ if specified */
358
    static char file[1024];
359
360
361

    if (s->model->firmware_version)
    {
362
        /* load from the firmware version directory, if specified */
363
364
365
366
        snprintf(file, sizeof(file), "%s/%s/%d/%s", s->workdir, s->model->name, s->model->firmware_version, file_rel);
    }
    else
    {
367
        /* or from the camera directory, if no firmware version is specified */
368
369
        snprintf(file, sizeof(file), "%s/%s/%s", s->workdir, s->model->name, file_rel);
    }
370

371
372
373
374
375
376
377
    return file;
}

void eos_load_image(EOSState *s, const char * file_rel, int offset, int max_size, uint32_t addr, int swap_endian)
{
    const char * file = eos_get_cam_path(s, file_rel);

Antony Pavlov's avatar
Antony Pavlov committed
378
379
380
    int size = get_image_size(file);
    if (size < 0)
    {
381
        fprintf(stderr, "%s: file not found '%s'\n", __func__, file);
Antony Pavlov's avatar
Antony Pavlov committed
382
383
384
        abort();
    }

385
386
387
388
389
390
    if (size < offset) {
        fprintf(stderr, "%s: file '%s': offset '%d' is too big\n", __func__,
            file, offset);
        abort();
    }

391
    fprintf(stderr, "[EOS] loading '%s'", file);
Antony Pavlov's avatar
Antony Pavlov committed
392

Antony Pavlov's avatar
Antony Pavlov committed
393
394
395
    uint8_t* buf = malloc(size);
    if (!buf)
    {
396
        fprintf(stderr, "%s: malloc error loading '%s'\n", __func__, file);
Antony Pavlov's avatar
Antony Pavlov committed
397
398
        abort();
    }
Antony Pavlov's avatar
Antony Pavlov committed
399

Antony Pavlov's avatar
Antony Pavlov committed
400
401
    if (load_image(file, buf) != size)
    {
402
        fprintf(stderr, "%s: error loading '%s'\n", __func__, file);
Antony Pavlov's avatar
Antony Pavlov committed
403
404
        abort();
    }
Antony Pavlov's avatar
Antony Pavlov committed
405

406
407
408
409
    if ((max_size > 0) && (size + offset != max_size)) {
        fprintf(stderr, " (expected size 0x%08X, got 0x%08X)", max_size, size);
    }

410
411
412
413
414
    size = size - offset;

    if ((max_size > 0) && (size > max_size)) {
        size = max_size;
    }
415
    
416
417
418
419
420
421
422
423
424
    fprintf(stderr, " to 0x%08X-0x%08X", addr, size + addr - 1);
    
    if (offset)
    {
        fprintf(stderr, " (offset 0x%X)", offset);
    }
    
    fprintf(stderr, "\n");
    
425
426
427
    if (swap_endian) {
        reverse_bytes_order(buf + offset, size);
    }
428

a1ex's avatar
a1ex committed
429
    MEM_WRITE_ROM(addr, buf + offset, size);
Antony Pavlov's avatar
Antony Pavlov committed
430

Antony Pavlov's avatar
Antony Pavlov committed
431
432
433
    free(buf);
}

434
435
436
437
static int cfdma_read_data(EOSState *s, CFState *cf);
static int cfdma_write_data(EOSState *s, CFState *cf);
static void cfdma_trigger_interrupt(EOSState *s);

Antony Pavlov's avatar
Antony Pavlov committed
438

439
440
static void eos_interrupt_timer_body(EOSState *s)
{
Antony Pavlov's avatar
Antony Pavlov committed
441
442
443
    {
        uint32_t pos;

Niklas Fejes's avatar
Niklas Fejes committed
444
        /* don't loop thread if cpu stopped in gdb */
445
        if (s->cpu0 && cpu_is_stopped(CPU(s->cpu0))) {
446
            return;
447
448
449
        }

        if (s->cpu1 && cpu_is_stopped(CPU(s->cpu1))) {
450
            return;
Niklas Fejes's avatar
Niklas Fejes committed
451
452
        }

453
454
        s->digic_timer += DIGIC_TIMER_STEP;
        s->digic_timer &= DIGIC_TIMER_MASK;
455
        
456
        for (pos = 0; pos < COUNT(s->timer_enabled); pos++)
457
458
459
        {
            if (s->timer_enabled[pos])
            {
460
                s->timer_current_value[pos] += DIGIC_TIMER_STEP;
461
462
463
464
465

                if (s->timer_current_value[pos] > s->timer_reload_value[pos])
                {
                    s->timer_current_value[pos] = 0;
                }
466
467
            }
        }
Antony Pavlov's avatar
Antony Pavlov committed
468
469

        /* go through all interrupts and check if they are pending/scheduled */
470
        for(pos = INT_ENTRIES-1; pos > 0; pos--)
Antony Pavlov's avatar
Antony Pavlov committed
471
472
473
474
475
476
477
478
        {
            /* it is pending, so trigger int and set to 0 */
            if(s->irq_schedule[pos] == 1)
            {
                /* wait, its not enabled. keep it pending */
                if(s->irq_enabled[pos] && !s->irq_id)
                {
                    /* timer interrupt will re-fire periodically */
479
                    if(pos == TIMER_INTERRUPT)
Antony Pavlov's avatar
Antony Pavlov committed
480
                    {
481
482
483
484
                        if (qemu_loglevel_mask(CPU_LOG_INT) &&
                            qemu_loglevel_mask(EOS_LOG_VERBOSE))
                        {
                            /* timer interrupt, quiet */
485
                            fprintf(stderr, "[EOS] trigger int 0x%02X (delayed)\n", pos);    /* quiet */
486
                        }
487
                        s->irq_schedule[pos] = s->timer_reload_value[DRYOS_TIMER_ID] >> 8;
Antony Pavlov's avatar
Antony Pavlov committed
488
489
490
                    }
                    else
                    {
491
                        if (qemu_loglevel_mask(CPU_LOG_INT)) {
492
                            fprintf(stderr, "[EOS] trigger int 0x%02X (delayed)\n", pos);
493
                        }
Antony Pavlov's avatar
Antony Pavlov committed
494
495
496
497
                        s->irq_schedule[pos] = 0;
                    }

                    s->irq_id = pos;
498
                    s->irq_enabled[s->irq_id] = 0;
499

500
                    cpu_interrupt(CPU(CURRENT_CPU), CPU_INTERRUPT_HARD);
Antony Pavlov's avatar
Antony Pavlov committed
501
502
503
504
505
506
507
508
509
                }
            }

            /* still counting down? */
            if(s->irq_schedule[pos] > 1)
            {
                s->irq_schedule[pos]--;
            }
        }
510

511
512
        /* also check all HPTimers */
        /* note: we can trigger multiple HPTimers on a single interrupt */
513
514
        int trigger_hptimers[64] = {0};
        int hptimer_interrupts[COUNT(s->HPTimers)] = {
515
            0x18, 0x1A, 0x1C, 0x1E, 0, 0,
516
517
518
519
520
            HPTIMER_INTERRUPT, HPTIMER_INTERRUPT, HPTIMER_INTERRUPT, HPTIMER_INTERRUPT,
            HPTIMER_INTERRUPT, HPTIMER_INTERRUPT, HPTIMER_INTERRUPT, HPTIMER_INTERRUPT,
        };
        
        for (pos = 0; pos < COUNT(s->HPTimers); pos++)
521
522
523
        {
            if (s->HPTimers[pos].active && s->HPTimers[pos].output_compare == s->digic_timer)
            {
524
                if (qemu_loglevel_mask(EOS_LOG_IO)) {
525
                    fprintf(stderr, "[HPTimer] Firing HPTimer #%d\n", pos);
526
                }
527
                s->HPTimers[pos].triggered = 1;
528
529
530
531
                int interrupt = hptimer_interrupts[pos];
                assert(interrupt > 0);
                assert(interrupt < COUNT(trigger_hptimers));
                trigger_hptimers[hptimer_interrupts[pos]] = 1;
532
533
534
            }
        }

535
        for (int i = 1; i < COUNT(trigger_hptimers); i++)
536
        {
537
538
539
540
            if (trigger_hptimers[i])
            {
                eos_trigger_int(s, i, 0);
            }
541
        }
542

543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
        if (s->cf.dma_read_request)
        {
            s->cf.dma_read_request = cfdma_read_data(s, &s->cf);
        }

        if (s->cf.dma_write_request)
        {
            s->cf.dma_write_request = cfdma_write_data(s, &s->cf);
        }
        
        if (s->cf.pending_interrupt && s->cf.interrupt_enabled == 1)
        {
            cfdma_trigger_interrupt(s);
            s->cf.pending_interrupt = 0;
        }
Antony Pavlov's avatar
Antony Pavlov committed
558
559
    }

560
561
562
563
564
565
566
}

static void eos_interrupt_timer_cb(void *parm)
{
    EOSState *s = (EOSState *)parm;
    eos_interrupt_timer_body(s);
    int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
567
    timer_mod_anticipate_ns(s->interrupt_timer, now + DIGIC_TIMER_STEP*1000); // DIGIC_TIMER_STEP us
Antony Pavlov's avatar
Antony Pavlov committed
568
569
}

Niklas Fejes's avatar
Niklas Fejes committed
570
571
572
573
574
575
576
577



/** FRAMEBUFFER & DISPLAY (move to separate file?) **/




578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// precompute some parts of YUV to RGB computations
static int yuv2rgb_RV[256];
static int yuv2rgb_GU[256];
static int yuv2rgb_GV[256];
static int yuv2rgb_BU[256];

/** http://www.martinreddy.net/gfx/faqs/colorconv.faq
 * BT 601:
 * R'= Y' + 0.000*U' + 1.403*V'
 * G'= Y' - 0.344*U' - 0.714*V'
 * B'= Y' + 1.773*U' + 0.000*V'
 * 
 * BT 709:
 * R'= Y' + 0.0000*Cb + 1.5701*Cr
 * G'= Y' - 0.1870*Cb - 0.4664*Cr
 * B'= Y' - 1.8556*Cb + 0.0000*Cr
 */

static void precompute_yuv2rgb(int rec709)
{
    int u, v;
    if (rec709)
    {
        /*
        *R = *Y + 1608 * V / 1024;
        *G = *Y -  191 * U / 1024 - 478 * V / 1024;
        *B = *Y + 1900 * U / 1024;
        */
        for (u = 0; u < 256; u++)
        {
            int8_t U = u;
            yuv2rgb_GU[u] = (-191 * U) >> 10;
            yuv2rgb_BU[u] = (1900 * U) >> 10;
        }

        for (v = 0; v < 256; v++)
        {
            int8_t V = v;
            yuv2rgb_RV[v] = (1608 * V) >> 10;
            yuv2rgb_GV[v] = (-478 * V) >> 10;
        }
    }
    else // REC 601
    {
        /*
        *R = *Y + ((1437 * V) >> 10);
        *G = *Y -  ((352 * U) >> 10) - ((731 * V) >> 10);
        *B = *Y + ((1812 * U) >> 10);
        */
        for (u = 0; u < 256; u++)
        {
            int8_t U = u;
            yuv2rgb_GU[u] = (-352 * U) >> 10;
            yuv2rgb_BU[u] = (1812 * U) >> 10;
        }

        for (v = 0; v < 256; v++)
        {
            int8_t V = v;
            yuv2rgb_RV[v] = (1437 * V) >> 10;
            yuv2rgb_GV[v] = (-731 * V) >> 10;
        }
    }
}

#define COERCE(x,lo,hi) MAX(MIN((x),(hi)),(lo))

static void yuv2rgb(int Y, int U, int V, int* R, int* G, int* B)
{
    const int v_and_ff = V & 0xFF;
    const int u_and_ff = U & 0xFF;
    int v = Y + yuv2rgb_RV[v_and_ff];
    *R = COERCE(v, 0, 255);
    v = Y + yuv2rgb_GU[u_and_ff] + yuv2rgb_GV[v_and_ff];
    *G = COERCE(v, 0, 255);
    v = Y + yuv2rgb_BU[u_and_ff];
    *B = COERCE(v, 0, 255);
}

#define UYVY_GET_Y1(uyvy) (((uyvy) >>  8) & 0xFF)
#define UYVY_GET_Y2(uyvy) (((uyvy) >> 24) & 0xFF)
#define UYVY_GET_U(uyvy)  (((uyvy)      ) & 0xFF)
#define UYVY_GET_V(uyvy)  (((uyvy) >> 16) & 0xFF)


a1ex's avatar
a1ex committed
663
664
/* todo: supoort other bith depths */

665
typedef void (*drawfn_bmp_yuv)(void *, uint8_t *, const uint8_t *, const uint8_t*, int, int, int);
666

667
668
669
670
static void draw_line8_32(void *opaque,
                uint8_t *d, const uint8_t *s, int width, int deststep)
{
    uint8_t v, r, g, b;
a1ex's avatar
a1ex committed
671
    EOSState* ws = (EOSState*) opaque;
672
673
    
    do {
a1ex's avatar
a1ex committed
674
        v = ldub_p((void *) s);
675
676
        if (v)
        {
677
678
679
            r = ws->disp.palette_8bit[v].R;
            g = ws->disp.palette_8bit[v].G;
            b = ws->disp.palette_8bit[v].B;
680
681
682
683
            ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
        }
        else
        {
684
            r = g = b = 128;
685
686
687
688
689
690
691
            ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
        }
        s ++;
        d += 4;
    } while (-- width != 0);
}

692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
static uint8_t clip_yuv(int v) {
    if (v<0) return 0;
    if (v>255) return 255;
    return v;
}

static uint8_t yuv_to_r(uint8_t y, int8_t v) {
    return clip_yuv(((y<<12) +          v*5743 + 2048)>>12);
}

static uint8_t yuv_to_g(uint8_t y, int8_t u, int8_t v) {
    return clip_yuv(((y<<12) - u*1411 - v*2925 + 2048)>>12);
}

static uint8_t yuv_to_b(uint8_t y, int8_t u) {
    return clip_yuv(((y<<12) + u*7258          + 2048)>>12);
}

static void draw_line_YUV8B_32(void *opaque,
                uint8_t *d, const uint8_t *s, int width, int deststep)
{
    uint8_t v, r, g, b;
    width = width / 2;
    do {
        v = ldub_p((void *) s);
        if (v)
        {
            uint8_t p2 = s[2] - 0x80;
            uint8_t p0 = s[0] - 0x80;
            r = yuv_to_r(s[1],p2);
            g = yuv_to_g(s[1],p0,p2);
            b = yuv_to_b(s[1],p0);
            ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
                        
            d += 4;
            r = yuv_to_r(s[3],p2);
            g = yuv_to_g(s[3],p0,p2);
            b = yuv_to_b(s[3],p0);
            ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
            d += 4;
        }
        else
        {
            r = g = b = 128;
            ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
            d += 4;
            ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
            d += 4;
        }
        s +=4;
    } while (-- width != 0);
}

745
746
747
748
static void draw_line4_32(void *opaque,
                uint8_t *d, const uint8_t *s, int width, int deststep)
{
    uint8_t v, r, g, b;
749
    EOSState* ws = (EOSState*) opaque;
750
    void * d0 = d;
751
752
    
    do {
a1ex's avatar
a1ex committed
753
        v = ldub_p((void *) s);
754
755
        v = ((uintptr_t)d/4 % 2) ? (v >> 4) & 0xF : v & 0xF;
        
756
757
758
        r = ws->disp.palette_4bit[v].R;
        g = ws->disp.palette_4bit[v].G;
        b = ws->disp.palette_4bit[v].B;
759
        ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
760
761
762
763

        if ((uintptr_t)d/4 % 2) s ++;
        d += 4;
    } while (-- width != 0);
764
765
766
767
768
769

    if (ws->model->digic_version < 4)
    {
        /* double each line */
        memcpy(d, d0, (void *) d - d0);
    }
770
771
}

772
static void draw_line8_32_bmp_yuv(void *opaque,
773
                uint8_t *d, const uint8_t *bmp, const uint8_t *yuv, int width, int deststep, int yuvstep)
a1ex's avatar
a1ex committed
774
775
{
    uint8_t v, r, g, b;
a1ex's avatar
a1ex committed
776
    EOSState* ws = (EOSState*) opaque;
777
    
a1ex's avatar
a1ex committed
778
    do {
a1ex's avatar
a1ex committed
779
        v = ldub_p((void *) bmp);
780
781
        if (v)
        {
782
783
784
            r = ws->disp.palette_8bit[v].R;
            g = ws->disp.palette_8bit[v].G;
            b = ws->disp.palette_8bit[v].B;
785
786
787
788
            ((uint32_t *) d)[0] = rgb_to_pixel32(r, g, b);
        }
        else
        {
a1ex's avatar
a1ex committed
789
            uint32_t uyvy =  ldl_p((void*)((uintptr_t)yuv & ~3));
790
            int Y = (uintptr_t)yuv & 3 ? UYVY_GET_Y2(uyvy) : UYVY_GET_Y1(uyvy);
791
792
793
794
795
            int U = UYVY_GET_U(uyvy);
            int V = UYVY_GET_V(uyvy);
            int R, G, B;
            yuv2rgb(Y, U, V, &R, &G, &B);
            ((uint32_t *) d)[0] = rgb_to_pixel32(R, G, B);
796
        }
797
        bmp ++;
798
        yuv += yuvstep;
a1ex's avatar
a1ex committed
799
800
801
802
        d += 4;
    } while (-- width != 0);
}

803
804
/* similar to QEMU's framebuffer_update_display, but with two image planes */
/* main plane is BMP (8-bit, same size as output), secondary plane is YUV (scaled to match the BMP one) */
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
static void framebuffer_update_display_bmp_yuv(
    DisplaySurface *ds,
    MemoryRegion *address_space,
    hwaddr base_bmp,
    hwaddr base_yuv,
    int cols, /* Width in pixels.  */
    int rows_bmp, /* Height in pixels.  */
    int rows_yuv,
    int src_width_bmp, /* Length of source line, in bytes.  */
    int src_width_yuv,
    int dest_row_pitch, /* Bytes between adjacent horizontal output pixels.  */
    int dest_col_pitch, /* Bytes between adjacent vertical output pixels.  */
    int invalidate, /* nonzero to redraw the whole image.  */
    drawfn_bmp_yuv fn,
    void *opaque,
    int *first_row, /* Input and output.  */
    int *last_row /* Output only */)
{
    hwaddr src_len_bmp;
    hwaddr src_len_yuv;
    uint8_t *dest;
    uint8_t *src_bmp;
    uint8_t *src_yuv;
    uint8_t *src_base_bmp;
    uint8_t *src_base_yuv;
    int first, last = 0;
    int dirty;
    int i;
    ram_addr_t addr_bmp;
    ram_addr_t addr_yuv;
835
    ram_addr_t addr_base_yuv;
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
    MemoryRegionSection mem_section_bmp;
    MemoryRegionSection mem_section_yuv;
    MemoryRegion *mem_bmp;
    MemoryRegion *mem_yuv;

    i = *first_row;
    *first_row = -1;
    src_len_bmp = src_width_bmp * rows_bmp;
    src_len_yuv = src_width_yuv * rows_yuv;

    mem_section_bmp = memory_region_find(address_space, base_bmp, src_len_bmp);
    mem_section_yuv = memory_region_find(address_space, base_yuv, src_len_yuv);
    mem_bmp = mem_section_bmp.mr;
    mem_yuv = mem_section_yuv.mr;
    if (int128_get64(mem_section_bmp.size) != src_len_bmp ||
            !memory_region_is_ram(mem_section_bmp.mr)) {
        goto out;
    }
    assert(mem_bmp);
    assert(mem_section_bmp.offset_within_address_space == base_bmp);

    if (int128_get64(mem_section_yuv.size) != src_len_yuv ||
            !memory_region_is_ram(mem_section_yuv.mr)) {
        goto out;
    }
    assert(mem_yuv);
    assert(mem_section_yuv.offset_within_address_space == base_yuv);

    memory_region_sync_dirty_bitmap(mem_bmp);
    memory_region_sync_dirty_bitmap(mem_yuv);
    src_base_bmp = cpu_physical_memory_map(base_bmp, &src_len_bmp, 0);
    src_base_yuv = cpu_physical_memory_map(base_yuv, &src_len_yuv, 0);
    /* If we can't map the framebuffer then bail.  We could try harder,
       but it's not really worth it as dirty flag tracking will probably
       already have failed above.  */
    if (!src_base_bmp)
        goto out;
    if (!src_base_yuv)
        goto out;
    if (src_len_bmp != src_width_bmp * rows_bmp) {
        cpu_physical_memory_unmap(src_base_bmp, src_len_bmp, 0, 0);
        goto out;
    }
    if (src_len_yuv != src_width_yuv * rows_yuv) {
        cpu_physical_memory_unmap(src_base_yuv, src_len_yuv, 0, 0);
        goto out;
    }
    src_bmp = src_base_bmp;
    src_yuv = src_base_yuv;
    dest = surface_data(ds);
    if (dest_col_pitch < 0)
        dest -= dest_col_pitch * (cols - 1);
    if (dest_row_pitch < 0) {
        dest -= dest_row_pitch * (rows_bmp - 1);
    }
    first = -1;
    addr_bmp = mem_section_bmp.offset_within_region;
    addr_yuv = mem_section_yuv.offset_within_region;
894
    addr_base_yuv = addr_yuv;
895

896
    int j = i * rows_yuv / rows_bmp;
897
898
    addr_bmp += i * src_width_bmp;
    src_bmp += i * src_width_bmp;
899
900
    addr_yuv = addr_base_yuv + j * src_width_yuv;
    src_yuv = src_base_yuv + j * src_width_yuv;
901
    dest += i * dest_row_pitch;
902
903
904
    
    /* fixme: only works for integer factors */
    int src_yuv_pitch = src_width_yuv / cols;
905
906
907
908
909
910
911

    for (; i < rows_bmp; i++) {
        dirty = memory_region_get_dirty(mem_bmp, addr_bmp, src_width_bmp,
                                             DIRTY_MEMORY_VGA);
        dirty |= memory_region_get_dirty(mem_yuv, addr_yuv, src_width_yuv,
                                             DIRTY_MEMORY_VGA);
        if (dirty || invalidate) {
912
            fn(opaque, dest, src_bmp, src_yuv, cols, dest_col_pitch, src_yuv_pitch);
913
914
915
916
            if (first == -1)
                first = i;
            last = i;
        }
917
918

        int j = i * rows_yuv / rows_bmp;
919
920
        addr_bmp += src_width_bmp;
        src_bmp += src_width_bmp;
921
922
        addr_yuv = addr_base_yuv + j * src_width_yuv;
        src_yuv = src_base_yuv + j * src_width_yuv;
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
        dest += dest_row_pitch;
    }
    cpu_physical_memory_unmap(src_base_bmp, src_len_bmp, 0, 0);
    cpu_physical_memory_unmap(src_base_yuv, src_len_yuv, 0, 0);
    if (first < 0) {
        goto out;
    }
    memory_region_reset_dirty(mem_bmp, mem_section_bmp.offset_within_region, src_len_bmp,
                              DIRTY_MEMORY_VGA);
    memory_region_reset_dirty(mem_yuv, mem_section_yuv.offset_within_region, src_len_yuv,
                              DIRTY_MEMORY_VGA);
    *first_row = first;
    *last_row = last;
out:
    memory_region_unref(mem_bmp);
    memory_region_unref(mem_yuv);
}

a1ex's avatar
a1ex committed
941
942
943
944
static void eos_update_display(void *parm)
{
    EOSState *s = (EOSState *)parm;

945
    DisplaySurface *surface = qemu_console_surface(s->disp.con);
946
947
948
    
    /* these numbers need double-checking */
    /*                  LCD    HDMI-1080   HDMI-480    SD-PAL      SD-NTSC */
949
950
951
952
    int widths[]      = {   720,   960,        720,        720,        720     };
    int heights[]     = {   480,   540,        480,        576,        480     };
    int yuv_widths[]  = {   720,  1920,        720,        540,        540     };
    int yuv_heights[] = {   480,  1080,        480,        572,        480     };
953
    
954
955
956
957
    int width       = widths     [s->disp.type];
    int height      = heights    [s->disp.type];
    int yuv_width   = yuv_widths [s->disp.type];
    int yuv_height  = yuv_heights[s->disp.type];
958
959
960
961
962

    int height_multiplier = 1;
    int out_height = height;

    /* VxWorks models have 720x240 screens stretched vertically */
963
964
    if (s->model->digic_version < 4)
    {
965
966
967
        height_multiplier = 2;
        height /= height_multiplier;
        assert(out_height == height * height_multiplier);
968
    }
969

970
971
972
973
    if (s->disp.width && s->disp.height)
    {
        /* did we manage to get them from registers? override the above stuff */
        width = s->disp.width;
974
        out_height = height = s->disp.height;
975
    }
a1ex's avatar
a1ex committed
976

977
    if (width != surface_width(surface) || out_height != surface_height(surface))
a1ex's avatar
a1ex committed
978
    {
979
        qemu_console_resize(s->disp.con, width, out_height);
980
981
        surface = qemu_console_surface(s->disp.con);
        s->disp.invalidate = 1;
a1ex's avatar
a1ex committed
982
    }
alex@thinkpad's avatar
alex@thinkpad committed
983
984
985
986
987
988
989

    if (s->card_led)
    {
        /* fixme: inefficient (redraws non-dirty areas) */
        s->disp.invalidate = 1;
    }

a1ex's avatar
a1ex committed
990
991
992
    int first, last;
    
    first = 0;
993
994
    int linesize = surface_stride(surface) * height_multiplier;

995
    if (s->disp.is_4bit)
996
    {
a1ex's avatar
a1ex committed
997
        /* bootloader config, 4 bpp */
998
        uint64_t size = height * width / 2;
999
1000
        MemoryRegionSection section = memory_region_find(
            s->system_mem,
1001
            s->disp.bmp_vram ? s->disp.bmp_vram : 0x08000000,
1002
1003
            size
        );
1004
1005
        framebuffer_update_display(
            surface,
Niklas Fejes's avatar
Niklas Fejes committed
1006
            &section,
1007
            width, height,
1008
            s->disp.bmp_pitch, linesize, 0, 1,
1009
            draw_line4_32, s,
1010
1011
1012
            &first, &last
        );
    }
1013
    else if (s->disp.img_vram)
1014
1015
1016
1017
    {
        framebuffer_update_display_bmp_yuv(
            surface,
            s->system_mem,
1018
1019
            s->disp.bmp_vram,
            s->disp.img_vram,
1020
            width, height, yuv_height,
1021
            s->disp.bmp_pitch, yuv_width*2, linesize, 0, s->disp.invalidate,
a1ex's avatar
a1ex committed
1022
            draw_line8_32_bmp_yuv, s,
1023
1024
1025
            &first, &last
        );
    }
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
    else if (strcmp(s->model->name, "EOSM3") == 0)
    {
        uint64_t size = height * s->disp.bmp_pitch;
        MemoryRegionSection section = memory_region_find(
            s->system_mem,
            s->disp.bmp_vram ? s->disp.bmp_vram : 0x08000000,
            size
        );
        framebuffer_update_display(
            surface,
            &section,
            width , height,
            s->disp.bmp_pitch, linesize, 0, 1,
            draw_line_YUV8B_32, s,
            &first, &last
        );
    }
1043
    else
1044
    {
1045
        uint64_t size = height * width;
1046
1047
        MemoryRegionSection section = memory_region_find(
            s->system_mem,
1048
            s->disp.bmp_vram ? s->disp.bmp_vram : 0x08000000,
1049
1050
            size
        );
1051
1052
        framebuffer_update_display(
            surface,
Niklas Fejes's avatar
Niklas Fejes committed
1053
            &section,
1054
            width, height,
1055
            s->disp.bmp_pitch, linesize, 0, 1,
a1ex's avatar
a1ex committed
1056
            draw_line8_32, s,
1057
1058
1059
            &first, &last
        );
    }
1060
1061
1062
1063
1064

    if (s->card_led)
    {
        /* draw the LED at the bottom-right corner of the screen */
        int x_led = width - 8;
1065
        int y_led = out_height - 8;
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
        uint8_t * dest = surface_data(surface);
        for (int dy = -5; dy <= 5; dy++)
        {
            for (int dx = -5; dx <= 5; dx++)
            {
                int r2 = dx*dx + dy*dy;
                if (r2 < 5*5)
                {
                    ((uint32_t *) dest)[x_led+dx + width*(y_led+dy)] =
                        (r2 >= 4*4)         ? rgb_to_pixel32(0, 0, 0)       :
                        (s->card_led == 1)  ? rgb_to_pixel32(255, 0, 0)     :
                                              rgb_to_pixel32(64, 64, 64) ;
                }
            }
        }
    }

1083
1084
1085
    first *= height_multiplier;
    last *= height_multiplier;

a1ex's avatar
a1ex committed
1086
    if (first >= 0) {
1087
        dpy_gfx_update(s->disp.con, 0, first, width, last - first + 1);
a1ex's avatar
a1ex committed
1088
1089
    }
    
1090
    s->disp.invalidate = 0;
a1ex's avatar
a1ex committed
1091
1092
1093
1094
1095
}

static void eos_invalidate_display(void *parm)
{
    EOSState *s = (EOSState *)parm;
1096
    s->disp.invalidate = 1;
a1ex's avatar
a1ex committed
1097
1098
1099
1100
1101
1102
1103
}

static const GraphicHwOps eos_display_ops = {
    .invalidate  = eos_invalidate_display,
    .gfx_update  = eos_update_display,
};

1104
1105
static void eos_key_event(void *parm, int keycode)
{
1106
    /* keys sent to guest machine */
1107
    EOSState *s = (EOSState *)parm;
1108
1109
    mpu_send_keypress(s, keycode);
    //s->keyb.buf[(s->keyb.tail++) & 15] = keycode;
1110
1111
}

1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
/**
 * UART code taken from hw/char/digic-uart.c
 * (sorry, couldn't figure out how to reuse it...)
 */

enum {
    ST_RX_RDY = (1 << 0),
    ST_TX_RDY = (1 << 1),
};

static int eos_uart_can_rx(void *opaque)
{
    DigicUartState *s = opaque;
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
    EOSState *es = (EOSState *)(opaque - offsetof(EOSState, uart));

    /* fixme: make it work without this workaround */
    if (es->uart_just_received)
    {
        /* extra wait states to work around buffer issues; test code follows */
        /* ( sleep 5; echo "akashimorino" ) | ./run_canon_fw.sh 750D -serial stdio */
        es->uart_just_received--;
        return 0;
    }
Niklas Fejes's avatar
Niklas Fejes committed
1135

1136
1137
1138
1139
1140
1141
1142
    return !(s->reg_st & ST_RX_RDY);
}

static void eos_uart_rx(void *opaque, const uint8_t *buf, int size)
{
    DigicUartState *s = opaque;

1143
    assert(size == 1);
1144
1145
1146
1147
    assert(eos_uart_can_rx(opaque));

    s->reg_st |= ST_RX_RDY;
    s->reg_rx = *buf;
1148

1149
1150
    assert(!eos_uart_can_rx(opaque));

1151
    EOSState *es = (EOSState *)(opaque - offsetof(EOSState, uart));
1152
1153
1154
1155
    assert(es->model->uart_rx_interrupt);

    /* fixme: why it locks up without a delay? */
    eos_trigger_int(es, es->model->uart_rx_interrupt, 10);
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
}

static void eos_uart_event(void *opaque, int event)
{
}

static void eos_uart_reset(DigicUartState *s)
{
    s->reg_rx = 0;
    s->reg_st = ST_TX_RDY;
}
Niklas Fejes's avatar
Niklas Fejes committed
1167
1168
1169

/** EOS CPU SETUP **/