blizzard: fix for non-32bpp host display
[qemu] / hw / blizzard.c
1 /*
2  * Epson S1D13744/S1D13745 (Blizzard/Hailstorm/Tornado) LCD/TV controller.
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  * Written by Andrzej Zaborowski <andrew@openedhand.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 or
10  * (at your option) version 3 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "qemu-common.h"
23 #include "sysemu.h"
24 #include "console.h"
25 #include "devices.h"
26 #include "vga_int.h"
27 #include "pixel_ops.h"
28
29 typedef void (*blizzard_fn_t)(uint8_t *, const uint8_t *, unsigned int);
30
31 struct blizzard_s {
32     uint8_t reg;
33     uint32_t addr;
34     int swallow;
35
36     int pll;
37     int pll_range;
38     int pll_ctrl;
39     uint8_t pll_mode;
40     uint8_t clksel;
41     int memenable;
42     int memrefresh;
43     uint8_t timing[3];
44     int priority;
45
46     uint8_t lcd_config;
47     int x;
48     int y;
49     int skipx;
50     int skipy;
51     uint8_t hndp;
52     uint8_t vndp;
53     uint8_t hsync;
54     uint8_t vsync;
55     uint8_t pclk;
56     uint8_t u;
57     uint8_t v;
58     uint8_t yrc[2];
59     int ix[2];
60     int iy[2];
61     int ox[2];
62     int oy[2];
63
64     int enable;
65     int blank;
66     int bpp;
67     int invalidate;
68     int mx[2];
69     int my[2];
70     uint8_t mode;
71     uint8_t effect;
72     uint8_t iformat;
73     uint8_t source;
74     DisplayState *state;
75     void *fb;
76
77     uint8_t hssi_config[3];
78     uint8_t tv_config;
79     uint8_t tv_timing[4];
80     uint8_t vbi;
81     uint8_t tv_x;
82     uint8_t tv_y;
83     uint8_t tv_test;
84     uint8_t tv_filter_config;
85     uint8_t tv_filter_idx;
86     uint8_t tv_filter_coeff[0x20];
87     uint8_t border_r;
88     uint8_t border_g;
89     uint8_t border_b;
90     uint8_t gamma_config;
91     uint8_t gamma_idx;
92     uint8_t gamma_lut[0x100];
93     uint8_t matrix_ena;
94     uint8_t matrix_coeff[0x12];
95     uint8_t matrix_r;
96     uint8_t matrix_g;
97     uint8_t matrix_b;
98     uint8_t pm;
99     uint8_t status;
100     uint8_t rgbgpio_dir;
101     uint8_t rgbgpio;
102     uint8_t gpio_dir;
103     uint8_t gpio;
104     uint8_t gpio_edge[2];
105     uint8_t gpio_irq;
106     uint8_t gpio_pdown;
107
108     struct {
109         int x;
110         int y;
111         int dx;
112         int dy;
113         int len;
114         int buflen;
115         void *buf;
116         void *data;
117         uint16_t *ptr;
118         int angle;
119         int pitch;
120     } data;
121 };
122
123 /* Bytes(!) per pixel */
124 static const int blizzard_iformat_bpp[0x10] = {
125     0,
126     2,  /* RGB 5:6:5*/
127     3,  /* RGB 6:6:6 mode 1 */
128     3,  /* RGB 8:8:8 mode 1 */
129     0, 0,
130     4,  /* RGB 6:6:6 mode 2 */
131     4,  /* RGB 8:8:8 mode 2 */
132     0,  /* YUV 4:2:2 */
133     0,  /* YUV 4:2:0 */
134     0, 0, 0, 0, 0, 0,
135 };
136
137 #define DEPTH 8
138 #include "blizzard_template.h"
139 #define DEPTH 15
140 #include "blizzard_template.h"
141 #define DEPTH 16
142 #include "blizzard_template.h"
143 #define DEPTH 24
144 #include "blizzard_template.h"
145 #define DEPTH 32
146 #include "blizzard_template.h"
147
148 static inline void blizzard_rgb2yuv(int r, int g, int b,
149                 int *y, int *u, int *v)
150 {
151     *y = 0x10 + ((0x838 * r + 0x1022 * g + 0x322 * b) >> 13);
152     *u = 0x80 + ((0xe0e * b - 0x04c1 * r - 0x94e * g) >> 13);
153     *v = 0x80 + ((0xe0e * r - 0x0bc7 * g - 0x247 * b) >> 13);
154 }
155
156 static void blizzard_window(struct blizzard_s *s)
157 {
158     uint8_t *src, *dst;
159     int bypp[2];
160     int bypl[3];
161     int y;
162     blizzard_fn_t fn = 0;
163
164     /* FIXME: this is a hack - but nseries.c will use this function
165      * before correct DisplayState is initialized so we need a way to
166      * avoid drawing something when we actually have no clue about host bpp */
167         if (!s->state->listeners)
168                 return;
169                 
170     switch (ds_get_bits_per_pixel(s->state)) {
171         case 8:
172             fn = s->data.angle
173                 ? blizzard_draw_fn_r_8[s->iformat]
174                 : blizzard_draw_fn_8[s->iformat];
175             break;
176         case 15:
177             fn = s->data.angle
178                 ? blizzard_draw_fn_r_15[s->iformat]
179                 : blizzard_draw_fn_15[s->iformat];
180             break;
181         case 16:
182             fn = s->data.angle
183                 ? blizzard_draw_fn_r_16[s->iformat]
184                 : blizzard_draw_fn_16[s->iformat];
185             break;
186         case 24:
187             fn = s->data.angle
188                 ? blizzard_draw_fn_r_24[s->iformat]
189                 : blizzard_draw_fn_24[s->iformat];
190             break;
191         case 32:
192             fn = s->data.angle
193                 ? blizzard_draw_fn_r_32[s->iformat]
194                 : blizzard_draw_fn_32[s->iformat];
195             break;
196         default:
197                 break;
198     }
199     if (!fn)
200         return;
201     if (s->mx[0] > s->data.x)
202         s->mx[0] = s->data.x;
203     if (s->my[0] > s->data.y)
204         s->my[0] = s->data.y;
205     if (s->mx[1] < s->data.x + s->data.dx)
206         s->mx[1] = s->data.x + s->data.dx;
207     if (s->my[1] < s->data.y + s->data.dy)
208         s->my[1] = s->data.y + s->data.dy;
209
210     bypp[0] = s->bpp;
211     bypp[1] = (ds_get_bits_per_pixel(s->state) + 7) >> 3;
212     bypl[0] = bypp[0] * s->data.pitch;
213     bypl[1] = bypp[1] * s->x;
214     bypl[2] = bypp[0] * s->data.dx;
215
216     src = s->data.data;
217     dst = s->fb + bypl[1] * s->data.y + bypp[1] * s->data.x;
218     for (y = s->data.dy; y > 0; y --, src += bypl[0], dst += bypl[1])
219         fn(dst, src, bypl[2]);
220 }
221
222 static int blizzard_transfer_setup(struct blizzard_s *s)
223 {
224     if (s->source > 3 || !s->bpp ||
225                     s->ix[1] < s->ix[0] || s->iy[1] < s->iy[0])
226         return 0;
227
228     s->data.angle = s->effect & 3;
229     s->data.x = s->ix[0];
230     s->data.y = s->iy[0];
231     s->data.dx = s->ix[1] - s->ix[0] + 1;
232     s->data.dy = s->iy[1] - s->iy[0] + 1;
233     s->data.len = s->bpp * s->data.dx * s->data.dy;
234     s->data.pitch = s->data.dx;
235     if (s->data.len > s->data.buflen) {
236         s->data.buf = qemu_realloc(s->data.buf, s->data.len);
237         s->data.buflen = s->data.len;
238     }
239     s->data.ptr = s->data.buf;
240     s->data.data = s->data.buf;
241     s->data.len /= 2;
242     return 1;
243 }
244
245 static void blizzard_reset(struct blizzard_s *s)
246 {
247     s->reg = 0;
248     s->swallow = 0;
249
250     s->pll = 9;
251     s->pll_range = 1;
252     s->pll_ctrl = 0x14;
253     s->pll_mode = 0x32;
254     s->clksel = 0x00;
255     s->memenable = 0;
256     s->memrefresh = 0x25c;
257     s->timing[0] = 0x3f;
258     s->timing[1] = 0x13;
259     s->timing[2] = 0x21;
260     s->priority = 0;
261
262     s->lcd_config = 0x74;
263     s->x = 8;
264     s->y = 1;
265     s->skipx = 0;
266     s->skipy = 0;
267     s->hndp = 3;
268     s->vndp = 2;
269     s->hsync = 1;
270     s->vsync = 1;
271     s->pclk = 0x80;
272
273     s->ix[0] = 0;
274     s->ix[1] = 0;
275     s->iy[0] = 0;
276     s->iy[1] = 0;
277     s->ox[0] = 0;
278     s->ox[1] = 0;
279     s->oy[0] = 0;
280     s->oy[1] = 0;
281
282     s->yrc[0] = 0x00;
283     s->yrc[1] = 0x30;
284     s->u = 0;
285     s->v = 0;
286
287     s->iformat = 3;
288     s->source = 0;
289     s->bpp = blizzard_iformat_bpp[s->iformat];
290
291     s->hssi_config[0] = 0x00;
292     s->hssi_config[1] = 0x00;
293     s->hssi_config[2] = 0x01;
294     s->tv_config = 0x00;
295     s->tv_timing[0] = 0x00;
296     s->tv_timing[1] = 0x00;
297     s->tv_timing[2] = 0x00;
298     s->tv_timing[3] = 0x00;
299     s->vbi = 0x10;
300     s->tv_x = 0x14;
301     s->tv_y = 0x03;
302     s->tv_test = 0x00;
303     s->tv_filter_config = 0x80;
304     s->tv_filter_idx = 0x00;
305     s->border_r = 0x10;
306     s->border_g = 0x80;
307     s->border_b = 0x80;
308     s->gamma_config = 0x00;
309     s->gamma_idx = 0x00;
310     s->matrix_ena = 0x00;
311     memset(&s->matrix_coeff, 0, sizeof(s->matrix_coeff));
312     s->matrix_r = 0x00;
313     s->matrix_g = 0x00;
314     s->matrix_b = 0x00;
315     s->pm = 0x02;
316     s->status = 0x00;
317     s->rgbgpio_dir = 0x00;
318     s->gpio_dir = 0x00;
319     s->gpio_edge[0] = 0x00;
320     s->gpio_edge[1] = 0x00;
321     s->gpio_irq = 0x00;
322     s->gpio_pdown = 0xff;
323 }
324
325 static inline void blizzard_invalidate_display(void *opaque) {
326     struct blizzard_s *s = (struct blizzard_s *) opaque;
327
328     s->invalidate = 1;
329 }
330
331 static uint16_t blizzard_reg_read(void *opaque, uint8_t reg)
332 {
333     struct blizzard_s *s = (struct blizzard_s *) opaque;
334
335     switch (reg) {
336     case 0x00:  /* Revision Code */
337         return 0xa5;
338
339     case 0x02:  /* Configuration Readback */
340         return 0x83;    /* Macrovision OK, CNF[2:0] = 3 */
341
342     case 0x04:  /* PLL M-Divider */
343         return (s->pll - 1) | (1 << 7);
344     case 0x06:  /* PLL Lock Range Control */
345         return s->pll_range;
346     case 0x08:  /* PLL Lock Synthesis Control 0 */
347         return s->pll_ctrl & 0xff;
348     case 0x0a:  /* PLL Lock Synthesis Control 1 */
349         return s->pll_ctrl >> 8;
350     case 0x0c:  /* PLL Mode Control 0 */
351         return s->pll_mode;
352
353     case 0x0e:  /* Clock-Source Select */
354         return s->clksel;
355
356     case 0x10:  /* Memory Controller Activate */
357     case 0x14:  /* Memory Controller Bank 0 Status Flag */
358         return s->memenable;
359
360     case 0x18:  /* Auto-Refresh Interval Setting 0 */
361         return s->memrefresh & 0xff;
362     case 0x1a:  /* Auto-Refresh Interval Setting 1 */
363         return s->memrefresh >> 8;
364
365     case 0x1c:  /* Power-On Sequence Timing Control */
366         return s->timing[0];
367     case 0x1e:  /* Timing Control 0 */
368         return s->timing[1];
369     case 0x20:  /* Timing Control 1 */
370         return s->timing[2];
371
372     case 0x24:  /* Arbitration Priority Control */
373         return s->priority;
374
375     case 0x28:  /* LCD Panel Configuration */
376         return s->lcd_config;
377
378     case 0x2a:  /* LCD Horizontal Display Width */
379         return s->x >> 3;
380     case 0x2c:  /* LCD Horizontal Non-display Period */
381         return s->hndp;
382     case 0x2e:  /* LCD Vertical Display Height 0 */
383         return s->y & 0xff;
384     case 0x30:  /* LCD Vertical Display Height 1 */
385         return s->y >> 8;
386     case 0x32:  /* LCD Vertical Non-display Period */
387         return s->vndp;
388     case 0x34:  /* LCD HS Pulse-width */
389         return s->hsync;
390     case 0x36:  /* LCd HS Pulse Start Position */
391         return s->skipx >> 3;
392     case 0x38:  /* LCD VS Pulse-width */
393         return s->vsync;
394     case 0x3a:  /* LCD VS Pulse Start Position */
395         return s->skipy;
396
397     case 0x3c:  /* PCLK Polarity */
398         return s->pclk;
399
400     case 0x3e:  /* High-speed Serial Interface Tx Configuration Port 0 */
401         return s->hssi_config[0];
402     case 0x40:  /* High-speed Serial Interface Tx Configuration Port 1 */
403         return s->hssi_config[1];
404     case 0x42:  /* High-speed Serial Interface Tx Mode */
405         return s->hssi_config[2];
406     case 0x44:  /* TV Display Configuration */
407         return s->tv_config;
408     case 0x46 ... 0x4c: /* TV Vertical Blanking Interval Data bits */
409         return s->tv_timing[(reg - 0x46) >> 1];
410     case 0x4e:  /* VBI: Closed Caption / XDS Control / Status */
411         return s->vbi;
412     case 0x50:  /* TV Horizontal Start Position */
413         return s->tv_x;
414     case 0x52:  /* TV Vertical Start Position */
415         return s->tv_y;
416     case 0x54:  /* TV Test Pattern Setting */
417         return s->tv_test;
418     case 0x56:  /* TV Filter Setting */
419         return s->tv_filter_config;
420     case 0x58:  /* TV Filter Coefficient Index */
421         return s->tv_filter_idx;
422     case 0x5a:  /* TV Filter Coefficient Data */
423         if (s->tv_filter_idx < 0x20)
424             return s->tv_filter_coeff[s->tv_filter_idx ++];
425         return 0;
426
427     case 0x60:  /* Input YUV/RGB Translate Mode 0 */
428         return s->yrc[0];
429     case 0x62:  /* Input YUV/RGB Translate Mode 1 */
430         return s->yrc[1];
431     case 0x64:  /* U Data Fix */
432         return s->u;
433     case 0x66:  /* V Data Fix */
434         return s->v;
435
436     case 0x68:  /* Display Mode */
437         return s->mode;
438
439     case 0x6a:  /* Special Effects */
440         return s->effect;
441
442     case 0x6c:  /* Input Window X Start Position 0 */
443         return s->ix[0] & 0xff;
444     case 0x6e:  /* Input Window X Start Position 1 */
445         return s->ix[0] >> 3;
446     case 0x70:  /* Input Window Y Start Position 0 */
447         return s->ix[0] & 0xff;
448     case 0x72:  /* Input Window Y Start Position 1 */
449         return s->ix[0] >> 3;
450     case 0x74:  /* Input Window X End Position 0 */
451         return s->ix[1] & 0xff;
452     case 0x76:  /* Input Window X End Position 1 */
453         return s->ix[1] >> 3;
454     case 0x78:  /* Input Window Y End Position 0 */
455         return s->ix[1] & 0xff;
456     case 0x7a:  /* Input Window Y End Position 1 */
457         return s->ix[1] >> 3;
458     case 0x7c:  /* Output Window X Start Position 0 */
459         return s->ox[0] & 0xff;
460     case 0x7e:  /* Output Window X Start Position 1 */
461         return s->ox[0] >> 3;
462     case 0x80:  /* Output Window Y Start Position 0 */
463         return s->oy[0] & 0xff;
464     case 0x82:  /* Output Window Y Start Position 1 */
465         return s->oy[0] >> 3;
466     case 0x84:  /* Output Window X End Position 0 */
467         return s->ox[1] & 0xff;
468     case 0x86:  /* Output Window X End Position 1 */
469         return s->ox[1] >> 3;
470     case 0x88:  /* Output Window Y End Position 0 */
471         return s->oy[1] & 0xff;
472     case 0x8a:  /* Output Window Y End Position 1 */
473         return s->oy[1] >> 3;
474
475     case 0x8c:  /* Input Data Format */
476         return s->iformat;
477     case 0x8e:  /* Data Source Select */
478         return s->source;
479     case 0x90:  /* Display Memory Data Port */
480         return 0;
481
482     case 0xa8:  /* Border Color 0 */
483         return s->border_r;
484     case 0xaa:  /* Border Color 1 */
485         return s->border_g;
486     case 0xac:  /* Border Color 2 */
487         return s->border_b;
488
489     case 0xb4:  /* Gamma Correction Enable */
490         return s->gamma_config;
491     case 0xb6:  /* Gamma Correction Table Index */
492         return s->gamma_idx;
493     case 0xb8:  /* Gamma Correction Table Data */
494         return s->gamma_lut[s->gamma_idx ++];
495
496     case 0xba:  /* 3x3 Matrix Enable */
497         return s->matrix_ena;
498     case 0xbc ... 0xde: /* Coefficient Registers */
499         return s->matrix_coeff[(reg - 0xbc) >> 1];
500     case 0xe0:  /* 3x3 Matrix Red Offset */
501         return s->matrix_r;
502     case 0xe2:  /* 3x3 Matrix Green Offset */
503         return s->matrix_g;
504     case 0xe4:  /* 3x3 Matrix Blue Offset */
505         return s->matrix_b;
506
507     case 0xe6:  /* Power-save */
508         return s->pm;
509     case 0xe8:  /* Non-display Period Control / Status */
510         return s->status | (1 << 5);
511     case 0xea:  /* RGB Interface Control */
512         return s->rgbgpio_dir;
513     case 0xec:  /* RGB Interface Status */
514         return s->rgbgpio;
515     case 0xee:  /* General-purpose IO Pins Configuration */
516         return s->gpio_dir;
517     case 0xf0:  /* General-purpose IO Pins Status / Control */
518         return s->gpio;
519     case 0xf2:  /* GPIO Positive Edge Interrupt Trigger */
520         return s->gpio_edge[0];
521     case 0xf4:  /* GPIO Negative Edge Interrupt Trigger */
522         return s->gpio_edge[1];
523     case 0xf6:  /* GPIO Interrupt Status */
524         return s->gpio_irq;
525     case 0xf8:  /* GPIO Pull-down Control */
526         return s->gpio_pdown;
527
528     default:
529         fprintf(stderr, "%s: unknown register %02x\n", __FUNCTION__, reg);
530         return 0;
531     }
532 }
533
534 static void blizzard_reg_write(void *opaque, uint8_t reg, uint16_t value)
535 {
536     struct blizzard_s *s = (struct blizzard_s *) opaque;
537
538     switch (reg) {
539     case 0x04:  /* PLL M-Divider */
540         s->pll = (value & 0x3f) + 1;
541         break;
542     case 0x06:  /* PLL Lock Range Control */
543         s->pll_range = value & 3;
544         break;
545     case 0x08:  /* PLL Lock Synthesis Control 0 */
546         s->pll_ctrl &= 0xf00;
547         s->pll_ctrl |= (value << 0) & 0x0ff;
548         break;
549     case 0x0a:  /* PLL Lock Synthesis Control 1 */
550         s->pll_ctrl &= 0x0ff;
551         s->pll_ctrl |= (value << 8) & 0xf00;
552         break;
553     case 0x0c:  /* PLL Mode Control 0 */
554         s->pll_mode = value & 0x77;
555         if ((value & 3) == 0 || (value & 3) == 3)
556             fprintf(stderr, "%s: wrong PLL Control bits (%i)\n",
557                     __FUNCTION__, value & 3);
558         break;
559
560     case 0x0e:  /* Clock-Source Select */
561         s->clksel = value & 0xff;
562         break;
563
564     case 0x10:  /* Memory Controller Activate */
565         s->memenable = value & 1;
566         break;
567     case 0x14:  /* Memory Controller Bank 0 Status Flag */
568         break;
569
570     case 0x18:  /* Auto-Refresh Interval Setting 0 */
571         s->memrefresh &= 0xf00;
572         s->memrefresh |= (value << 0) & 0x0ff;
573         break;
574     case 0x1a:  /* Auto-Refresh Interval Setting 1 */
575         s->memrefresh &= 0x0ff;
576         s->memrefresh |= (value << 8) & 0xf00;
577         break;
578
579     case 0x1c:  /* Power-On Sequence Timing Control */
580         s->timing[0] = value & 0x7f;
581         break;
582     case 0x1e:  /* Timing Control 0 */
583         s->timing[1] = value & 0x17;
584         break;
585     case 0x20:  /* Timing Control 1 */
586         s->timing[2] = value & 0x35;
587         break;
588
589     case 0x24:  /* Arbitration Priority Control */
590         s->priority = value & 1;
591         break;
592
593     case 0x28:  /* LCD Panel Configuration */
594         s->lcd_config = value & 0xff;
595         if (value & (1 << 7))
596             fprintf(stderr, "%s: data swap not supported!\n", __FUNCTION__);
597         break;
598
599     case 0x2a:  /* LCD Horizontal Display Width */
600         s->x = value << 3;
601         break;
602     case 0x2c:  /* LCD Horizontal Non-display Period */
603         s->hndp = value & 0xff;
604         break;
605     case 0x2e:  /* LCD Vertical Display Height 0 */
606         s->y &= 0x300;
607         s->y |= (value << 0) & 0x0ff;
608         break;
609     case 0x30:  /* LCD Vertical Display Height 1 */
610         s->y &= 0x0ff;
611         s->y |= (value << 8) & 0x300;
612         break;
613     case 0x32:  /* LCD Vertical Non-display Period */
614         s->vndp = value & 0xff;
615         break;
616     case 0x34:  /* LCD HS Pulse-width */
617         s->hsync = value & 0xff;
618         break;
619     case 0x36:  /* LCD HS Pulse Start Position */
620         s->skipx = value & 0xff;
621         break;
622     case 0x38:  /* LCD VS Pulse-width */
623         s->vsync = value & 0xbf;
624         break;
625     case 0x3a:  /* LCD VS Pulse Start Position */
626         s->skipy = value & 0xff;
627         break;
628
629     case 0x3c:  /* PCLK Polarity */
630         s->pclk = value & 0x82;
631         /* Affects calculation of s->hndp, s->hsync and s->skipx.  */
632         break;
633
634     case 0x3e:  /* High-speed Serial Interface Tx Configuration Port 0 */
635         s->hssi_config[0] = value;
636         break;
637     case 0x40:  /* High-speed Serial Interface Tx Configuration Port 1 */
638         s->hssi_config[1] = value;
639         if (((value >> 4) & 3) == 3)
640             fprintf(stderr, "%s: Illegal active-data-links value\n",
641                             __FUNCTION__);
642         break;
643     case 0x42:  /* High-speed Serial Interface Tx Mode */
644         s->hssi_config[2] = value & 0xbd;
645         break;
646
647     case 0x44:  /* TV Display Configuration */
648         s->tv_config = value & 0xfe;
649         break;
650     case 0x46 ... 0x4c: /* TV Vertical Blanking Interval Data bits 0 */
651         s->tv_timing[(reg - 0x46) >> 1] = value;
652         break;
653     case 0x4e:  /* VBI: Closed Caption / XDS Control / Status */
654         s->vbi = value;
655         break;
656     case 0x50:  /* TV Horizontal Start Position */
657         s->tv_x = value;
658         break;
659     case 0x52:  /* TV Vertical Start Position */
660         s->tv_y = value & 0x7f;
661         break;
662     case 0x54:  /* TV Test Pattern Setting */
663         s->tv_test = value;
664         break;
665     case 0x56:  /* TV Filter Setting */
666         s->tv_filter_config = value & 0xbf;
667         break;
668     case 0x58:  /* TV Filter Coefficient Index */
669         s->tv_filter_idx = value & 0x1f;
670         break;
671     case 0x5a:  /* TV Filter Coefficient Data */
672         if (s->tv_filter_idx < 0x20)
673             s->tv_filter_coeff[s->tv_filter_idx ++] = value;
674         break;
675
676     case 0x60:  /* Input YUV/RGB Translate Mode 0 */
677         s->yrc[0] = value & 0xb0;
678         break;
679     case 0x62:  /* Input YUV/RGB Translate Mode 1 */
680         s->yrc[1] = value & 0x30;
681         break;
682     case 0x64:  /* U Data Fix */
683         s->u = value & 0xff;
684         break;
685     case 0x66:  /* V Data Fix */
686         s->v = value & 0xff;
687         break;
688
689     case 0x68:  /* Display Mode */
690         if ((s->mode ^ value) & 3)
691             s->invalidate = 1;
692         s->mode = value & 0xb7;
693         s->enable = value & 1;
694         s->blank = (value >> 1) & 1;
695         if (value & (1 << 4))
696             fprintf(stderr, "%s: Macrovision enable attempt!\n", __FUNCTION__);
697         break;
698
699     case 0x6a:  /* Special Effects */
700         s->effect = value & 0xfb;
701         break;
702
703     case 0x6c:  /* Input Window X Start Position 0 */
704         s->ix[0] &= 0x300;
705         s->ix[0] |= (value << 0) & 0x0ff;
706         break;
707     case 0x6e:  /* Input Window X Start Position 1 */
708         s->ix[0] &= 0x0ff;
709         s->ix[0] |= (value << 8) & 0x300;
710         break;
711     case 0x70:  /* Input Window Y Start Position 0 */
712         s->iy[0] &= 0x300;
713         s->iy[0] |= (value << 0) & 0x0ff;
714         break;
715     case 0x72:  /* Input Window Y Start Position 1 */
716         s->iy[0] &= 0x0ff;
717         s->iy[0] |= (value << 8) & 0x300;
718         break;
719     case 0x74:  /* Input Window X End Position 0 */
720         s->ix[1] &= 0x300;
721         s->ix[1] |= (value << 0) & 0x0ff;
722         break;
723     case 0x76:  /* Input Window X End Position 1 */
724         s->ix[1] &= 0x0ff;
725         s->ix[1] |= (value << 8) & 0x300;
726         break;
727     case 0x78:  /* Input Window Y End Position 0 */
728         s->iy[1] &= 0x300;
729         s->iy[1] |= (value << 0) & 0x0ff;
730         break;
731     case 0x7a:  /* Input Window Y End Position 1 */
732         s->iy[1] &= 0x0ff;
733         s->iy[1] |= (value << 8) & 0x300;
734         break;
735     case 0x7c:  /* Output Window X Start Position 0 */
736         s->ox[0] &= 0x300;
737         s->ox[0] |= (value << 0) & 0x0ff;
738         break;
739     case 0x7e:  /* Output Window X Start Position 1 */
740         s->ox[0] &= 0x0ff;
741         s->ox[0] |= (value << 8) & 0x300;
742         break;
743     case 0x80:  /* Output Window Y Start Position 0 */
744         s->oy[0] &= 0x300;
745         s->oy[0] |= (value << 0) & 0x0ff;
746         break;
747     case 0x82:  /* Output Window Y Start Position 1 */
748         s->oy[0] &= 0x0ff;
749         s->oy[0] |= (value << 8) & 0x300;
750         break;
751     case 0x84:  /* Output Window X End Position 0 */
752         s->ox[1] &= 0x300;
753         s->ox[1] |= (value << 0) & 0x0ff;
754         break;
755     case 0x86:  /* Output Window X End Position 1 */
756         s->ox[1] &= 0x0ff;
757         s->ox[1] |= (value << 8) & 0x300;
758         break;
759     case 0x88:  /* Output Window Y End Position 0 */
760         s->oy[1] &= 0x300;
761         s->oy[1] |= (value << 0) & 0x0ff;
762         break;
763     case 0x8a:  /* Output Window Y End Position 1 */
764         s->oy[1] &= 0x0ff;
765         s->oy[1] |= (value << 8) & 0x300;
766         break;
767
768     case 0x8c:  /* Input Data Format */
769         s->iformat = value & 0xf;
770         s->bpp = blizzard_iformat_bpp[s->iformat];
771         if (!s->bpp)
772             fprintf(stderr, "%s: Illegal or unsupported input format %x\n",
773                             __FUNCTION__, s->iformat);
774         break;
775     case 0x8e:  /* Data Source Select */
776         s->source = value & 7;
777         /* Currently all windows will be "destructive overlays".  */
778         if ((!(s->effect & (1 << 3)) && (s->ix[0] != s->ox[0] ||
779                                         s->iy[0] != s->oy[0] ||
780                                         s->ix[1] != s->ox[1] ||
781                                         s->iy[1] != s->oy[1])) ||
782                         !((s->ix[1] - s->ix[0]) & (s->iy[1] - s->iy[0]) &
783                           (s->ox[1] - s->ox[0]) & (s->oy[1] - s->oy[0]) & 1))
784             fprintf(stderr, "%s: Illegal input/output window positions\n",
785                             __FUNCTION__);
786
787         blizzard_transfer_setup(s);
788         break;
789
790     case 0x90:  /* Display Memory Data Port */
791         if (!s->data.len && !blizzard_transfer_setup(s))
792             break;
793
794         *s->data.ptr ++ = value;
795         if (-- s->data.len == 0)
796             blizzard_window(s);
797         break;
798
799     case 0xa8:  /* Border Color 0 */
800         s->border_r = value;
801         break;
802     case 0xaa:  /* Border Color 1 */
803         s->border_g = value;
804         break;
805     case 0xac:  /* Border Color 2 */
806         s->border_b = value;
807         break;
808
809     case 0xb4:  /* Gamma Correction Enable */
810         s->gamma_config = value & 0x87;
811         break;
812     case 0xb6:  /* Gamma Correction Table Index */
813         s->gamma_idx = value;
814         break;
815     case 0xb8:  /* Gamma Correction Table Data */
816         s->gamma_lut[s->gamma_idx ++] = value;
817         break;
818
819     case 0xba:  /* 3x3 Matrix Enable */
820         s->matrix_ena = value & 1;
821         break;
822     case 0xbc ... 0xde: /* Coefficient Registers */
823         s->matrix_coeff[(reg - 0xbc) >> 1] = value & ((reg & 2) ? 0x80 : 0xff);
824         break;
825     case 0xe0:  /* 3x3 Matrix Red Offset */
826         s->matrix_r = value;
827         break;
828     case 0xe2:  /* 3x3 Matrix Green Offset */
829         s->matrix_g = value;
830         break;
831     case 0xe4:  /* 3x3 Matrix Blue Offset */
832         s->matrix_b = value;
833         break;
834
835     case 0xe6:  /* Power-save */
836         s->pm = value & 0x83;
837         if (value & s->mode & 1)
838             fprintf(stderr, "%s: The display must be disabled before entering "
839                             "Standby Mode\n", __FUNCTION__);
840         break;
841     case 0xe8:  /* Non-display Period Control / Status */
842         s->status = value & 0x1b;
843         break;
844     case 0xea:  /* RGB Interface Control */
845         s->rgbgpio_dir = value & 0x8f;
846         break;
847     case 0xec:  /* RGB Interface Status */
848         s->rgbgpio = value & 0xcf;
849         break;
850     case 0xee:  /* General-purpose IO Pins Configuration */
851         s->gpio_dir = value;
852         break;
853     case 0xf0:  /* General-purpose IO Pins Status / Control */
854         s->gpio = value;
855         break;
856     case 0xf2:  /* GPIO Positive Edge Interrupt Trigger */
857         s->gpio_edge[0] = value;
858         break;
859     case 0xf4:  /* GPIO Negative Edge Interrupt Trigger */
860         s->gpio_edge[1] = value;
861         break;
862     case 0xf6:  /* GPIO Interrupt Status */
863         s->gpio_irq &= value;
864         break;
865     case 0xf8:  /* GPIO Pull-down Control */
866         s->gpio_pdown = value;
867         break;
868
869     default:
870         fprintf(stderr, "%s: unknown register %02x\n", __FUNCTION__, reg);
871         break;
872     }
873 }
874
875 uint16_t s1d13745_read(void *opaque, int dc)
876 {
877     struct blizzard_s *s = (struct blizzard_s *) opaque;
878     uint16_t value = blizzard_reg_read(s, s->reg);
879
880     if (s->swallow -- > 0)
881         return 0;
882     if (dc)
883         s->reg ++;
884
885     return value;
886 }
887
888 void s1d13745_write(void *opaque, int dc, uint16_t value)
889 {
890     struct blizzard_s *s = (struct blizzard_s *) opaque;
891
892     if (s->swallow -- > 0)
893         return;
894     if (dc) {
895         blizzard_reg_write(s, s->reg, value);
896
897         if (s->reg != 0x90 && s->reg != 0x5a && s->reg != 0xb8)
898             s->reg += 2;
899     } else
900         s->reg = value & 0xff;
901 }
902
903 void s1d13745_write_block(void *opaque, int dc,
904                 void *buf, size_t len, int pitch)
905 {
906     struct blizzard_s *s = (struct blizzard_s *) opaque;
907
908     while (len > 0) {
909         if (s->reg == 0x90 && dc &&
910                         (s->data.len || blizzard_transfer_setup(s)) &&
911                         len >= (s->data.len << 1)) {
912             len -= s->data.len << 1;
913             s->data.len = 0;
914             s->data.data = buf;
915             if (pitch)
916                 s->data.pitch = pitch;
917             blizzard_window(s);
918             s->data.data = s->data.buf;
919             continue;
920         }
921
922         s1d13745_write(opaque, dc, *(uint16_t *) buf);
923         len -= 2;
924         buf += 2;
925     }
926
927     return;
928 }
929
930 static void blizzard_update_display(void *opaque)
931 {
932     struct blizzard_s *s = (struct blizzard_s *) opaque;
933     int y, bypp, bypl, bwidth;
934     uint8_t *src, *dst;
935
936     if (!s->enable)
937         return;
938
939     if (s->x != ds_get_width(s->state) || s->y != ds_get_height(s->state)) {
940         s->invalidate = 1;
941         qemu_console_resize(s->state, s->x, s->y);
942     }
943
944     if (s->invalidate) {
945         s->invalidate = 0;
946
947         if (s->blank) {
948             bypp = (ds_get_bits_per_pixel(s->state) + 7) >> 3;
949             memset(ds_get_data(s->state), 0, bypp * s->x * s->y);
950             return;
951         }
952
953         s->mx[0] = 0;
954         s->mx[1] = s->x;
955         s->my[0] = 0;
956         s->my[1] = s->y;
957     }
958
959     if (s->mx[1] <= s->mx[0])
960         return;
961
962     bypp = (ds_get_bits_per_pixel(s->state) + 7) >> 3;
963     bypl = bypp * s->x;
964     bwidth = bypp * (s->mx[1] - s->mx[0]);
965     y = s->my[0];
966     src = s->fb + bypl * y + bypp * s->mx[0];
967     dst = ds_get_data(s->state) + bypl * y + bypp * s->mx[0];
968     for (; y < s->my[1]; y ++, src += bypl, dst += bypl)
969         memcpy(dst, src, bwidth);
970
971     dpy_update(s->state, s->mx[0], s->my[0],
972                     s->mx[1] - s->mx[0], y - s->my[0]);
973
974     s->mx[0] = s->x;
975     s->mx[1] = 0;
976     s->my[0] = s->y;
977     s->my[1] = 0;
978 }
979
980 static void blizzard_screen_dump(void *opaque, const char *filename) {
981     struct blizzard_s *s = (struct blizzard_s *) opaque;
982
983     blizzard_update_display(opaque);
984     if (s && ds_get_data(s->state))
985         ppm_save(filename, s->state->surface);
986 }
987
988 void *s1d13745_init(qemu_irq gpio_int)
989 {
990     struct blizzard_s *s = (struct blizzard_s *) qemu_mallocz(sizeof(*s));
991
992     s->fb = qemu_malloc(0x180000);
993     /* Fill the framebuffer with white color here because the corresponding
994      * code in nseries.c is broken since the DisplayState change in QEMU.
995      * This is supposedly ok since nseries.c is the only user of blizzard.c */
996     memset(s->fb, 0xff, 0x180000);
997     
998     s->state = graphic_console_init(blizzard_update_display,
999                                  blizzard_invalidate_display,
1000                                  blizzard_screen_dump, NULL, s);
1001     blizzard_reset(s);
1002     return s;
1003 }