Statistics
| Branch: | Revision:

root / hw / sm501.c @ 61d3cf93

History | View | Annotate | Download (34 kB)

1
/*
2
 * QEMU SM501 Device
3
 *
4
 * Copyright (c) 2008 Shin-ichiro KAWASAKI
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
#include <stdio.h>
26
#include "hw.h"
27
#include "pc.h"
28
#include "console.h"
29
#include "devices.h"
30
#include "sysbus.h"
31
#include "qdev-addr.h"
32

    
33
/*
34
 * Status: 2008/11/02
35
 *   - Minimum implementation for Linux console : mmio regs and CRT layer.
36
 *   - Always updates full screen.
37
 *
38
 * TODO:
39
 *   - Panel support
40
 *   - Hardware cursor support
41
 *   - Touch panel support
42
 *   - USB support
43
 *   - UART support
44
 *   - Performance tuning
45
 */
46

    
47
//#define DEBUG_SM501
48
//#define DEBUG_BITBLT
49

    
50
#ifdef DEBUG_SM501
51
#define SM501_DPRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
52
#else
53
#define SM501_DPRINTF(fmt, ...) do {} while(0)
54
#endif
55

    
56

    
57
#define MMIO_BASE_OFFSET 0x3e00000
58

    
59
/* SM501 register definitions taken from "linux/include/linux/sm501-regs.h" */
60

    
61
/* System Configuration area */
62
/* System config base */
63
#define SM501_SYS_CONFIG                (0x000000)
64

    
65
/* config 1 */
66
#define SM501_SYSTEM_CONTROL                 (0x000000)
67

    
68
#define SM501_SYSCTRL_PANEL_TRISTATE        (1<<0)
69
#define SM501_SYSCTRL_MEM_TRISTATE        (1<<1)
70
#define SM501_SYSCTRL_CRT_TRISTATE        (1<<2)
71

    
72
#define SM501_SYSCTRL_PCI_SLAVE_BURST_MASK (3<<4)
73
#define SM501_SYSCTRL_PCI_SLAVE_BURST_1        (0<<4)
74
#define SM501_SYSCTRL_PCI_SLAVE_BURST_2        (1<<4)
75
#define SM501_SYSCTRL_PCI_SLAVE_BURST_4        (2<<4)
76
#define SM501_SYSCTRL_PCI_SLAVE_BURST_8        (3<<4)
77

    
78
#define SM501_SYSCTRL_PCI_CLOCK_RUN_EN        (1<<6)
79
#define SM501_SYSCTRL_PCI_RETRY_DISABLE        (1<<7)
80
#define SM501_SYSCTRL_PCI_SUBSYS_LOCK        (1<<11)
81
#define SM501_SYSCTRL_PCI_BURST_READ_EN        (1<<15)
82

    
83
/* miscellaneous control */
84

    
85
#define SM501_MISC_CONTROL                (0x000004)
86

    
87
#define SM501_MISC_BUS_SH                (0x0)
88
#define SM501_MISC_BUS_PCI                (0x1)
89
#define SM501_MISC_BUS_XSCALE                (0x2)
90
#define SM501_MISC_BUS_NEC                (0x6)
91
#define SM501_MISC_BUS_MASK                (0x7)
92

    
93
#define SM501_MISC_VR_62MB                (1<<3)
94
#define SM501_MISC_CDR_RESET                (1<<7)
95
#define SM501_MISC_USB_LB                (1<<8)
96
#define SM501_MISC_USB_SLAVE                (1<<9)
97
#define SM501_MISC_BL_1                        (1<<10)
98
#define SM501_MISC_MC                        (1<<11)
99
#define SM501_MISC_DAC_POWER                (1<<12)
100
#define SM501_MISC_IRQ_INVERT                (1<<16)
101
#define SM501_MISC_SH                        (1<<17)
102

    
103
#define SM501_MISC_HOLD_EMPTY                (0<<18)
104
#define SM501_MISC_HOLD_8                (1<<18)
105
#define SM501_MISC_HOLD_16                (2<<18)
106
#define SM501_MISC_HOLD_24                (3<<18)
107
#define SM501_MISC_HOLD_32                (4<<18)
108
#define SM501_MISC_HOLD_MASK                (7<<18)
109

    
110
#define SM501_MISC_FREQ_12                (1<<24)
111
#define SM501_MISC_PNL_24BIT                (1<<25)
112
#define SM501_MISC_8051_LE                (1<<26)
113

    
114

    
115

    
116
#define SM501_GPIO31_0_CONTROL                (0x000008)
117
#define SM501_GPIO63_32_CONTROL                (0x00000C)
118
#define SM501_DRAM_CONTROL                (0x000010)
119

    
120
/* command list */
121
#define SM501_ARBTRTN_CONTROL                (0x000014)
122

    
123
/* command list */
124
#define SM501_COMMAND_LIST_STATUS        (0x000024)
125

    
126
/* interrupt debug */
127
#define SM501_RAW_IRQ_STATUS                (0x000028)
128
#define SM501_RAW_IRQ_CLEAR                (0x000028)
129
#define SM501_IRQ_STATUS                (0x00002C)
130
#define SM501_IRQ_MASK                        (0x000030)
131
#define SM501_DEBUG_CONTROL                (0x000034)
132

    
133
/* power management */
134
#define SM501_POWERMODE_P2X_SRC                (1<<29)
135
#define SM501_POWERMODE_V2X_SRC                (1<<20)
136
#define SM501_POWERMODE_M_SRC                (1<<12)
137
#define SM501_POWERMODE_M1_SRC                (1<<4)
138

    
139
#define SM501_CURRENT_GATE                (0x000038)
140
#define SM501_CURRENT_CLOCK                (0x00003C)
141
#define SM501_POWER_MODE_0_GATE                (0x000040)
142
#define SM501_POWER_MODE_0_CLOCK        (0x000044)
143
#define SM501_POWER_MODE_1_GATE                (0x000048)
144
#define SM501_POWER_MODE_1_CLOCK        (0x00004C)
145
#define SM501_SLEEP_MODE_GATE                (0x000050)
146
#define SM501_POWER_MODE_CONTROL        (0x000054)
147

    
148
/* power gates for units within the 501 */
149
#define SM501_GATE_HOST                        (0)
150
#define SM501_GATE_MEMORY                (1)
151
#define SM501_GATE_DISPLAY                (2)
152
#define SM501_GATE_2D_ENGINE                (3)
153
#define SM501_GATE_CSC                        (4)
154
#define SM501_GATE_ZVPORT                (5)
155
#define SM501_GATE_GPIO                        (6)
156
#define SM501_GATE_UART0                (7)
157
#define SM501_GATE_UART1                (8)
158
#define SM501_GATE_SSP                        (10)
159
#define SM501_GATE_USB_HOST                (11)
160
#define SM501_GATE_USB_GADGET                (12)
161
#define SM501_GATE_UCONTROLLER                (17)
162
#define SM501_GATE_AC97                        (18)
163

    
164
/* panel clock */
165
#define SM501_CLOCK_P2XCLK                (24)
166
/* crt clock */
167
#define SM501_CLOCK_V2XCLK                (16)
168
/* main clock */
169
#define SM501_CLOCK_MCLK                (8)
170
/* SDRAM controller clock */
171
#define SM501_CLOCK_M1XCLK                (0)
172

    
173
/* config 2 */
174
#define SM501_PCI_MASTER_BASE                (0x000058)
175
#define SM501_ENDIAN_CONTROL                (0x00005C)
176
#define SM501_DEVICEID                        (0x000060)
177
/* 0x050100A0 */
178

    
179
#define SM501_DEVICEID_SM501                (0x05010000)
180
#define SM501_DEVICEID_IDMASK                (0xffff0000)
181
#define SM501_DEVICEID_REVMASK                (0x000000ff)
182

    
183
#define SM501_PLLCLOCK_COUNT                (0x000064)
184
#define SM501_MISC_TIMING                (0x000068)
185
#define SM501_CURRENT_SDRAM_CLOCK        (0x00006C)
186

    
187
#define SM501_PROGRAMMABLE_PLL_CONTROL        (0x000074)
188

    
189
/* GPIO base */
190
#define SM501_GPIO                        (0x010000)
191
#define SM501_GPIO_DATA_LOW                (0x00)
192
#define SM501_GPIO_DATA_HIGH                (0x04)
193
#define SM501_GPIO_DDR_LOW                (0x08)
194
#define SM501_GPIO_DDR_HIGH                (0x0C)
195
#define SM501_GPIO_IRQ_SETUP                (0x10)
196
#define SM501_GPIO_IRQ_STATUS                (0x14)
197
#define SM501_GPIO_IRQ_RESET                (0x14)
198

    
199
/* I2C controller base */
200
#define SM501_I2C                        (0x010040)
201
#define SM501_I2C_BYTE_COUNT                (0x00)
202
#define SM501_I2C_CONTROL                (0x01)
203
#define SM501_I2C_STATUS                (0x02)
204
#define SM501_I2C_RESET                        (0x02)
205
#define SM501_I2C_SLAVE_ADDRESS                (0x03)
206
#define SM501_I2C_DATA                        (0x04)
207

    
208
/* SSP base */
209
#define SM501_SSP                        (0x020000)
210

    
211
/* Uart 0 base */
212
#define SM501_UART0                        (0x030000)
213

    
214
/* Uart 1 base */
215
#define SM501_UART1                        (0x030020)
216

    
217
/* USB host port base */
218
#define SM501_USB_HOST                        (0x040000)
219

    
220
/* USB slave/gadget base */
221
#define SM501_USB_GADGET                (0x060000)
222

    
223
/* USB slave/gadget data port base */
224
#define SM501_USB_GADGET_DATA                (0x070000)
225

    
226
/* Display controller/video engine base */
227
#define SM501_DC                        (0x080000)
228

    
229
/* common defines for the SM501 address registers */
230
#define SM501_ADDR_FLIP                        (1<<31)
231
#define SM501_ADDR_EXT                        (1<<27)
232
#define SM501_ADDR_CS1                        (1<<26)
233
#define SM501_ADDR_MASK                        (0x3f << 26)
234

    
235
#define SM501_FIFO_MASK                        (0x3 << 16)
236
#define SM501_FIFO_1                        (0x0 << 16)
237
#define SM501_FIFO_3                        (0x1 << 16)
238
#define SM501_FIFO_7                        (0x2 << 16)
239
#define SM501_FIFO_11                        (0x3 << 16)
240

    
241
/* common registers for panel and the crt */
242
#define SM501_OFF_DC_H_TOT                (0x000)
243
#define SM501_OFF_DC_V_TOT                (0x008)
244
#define SM501_OFF_DC_H_SYNC                (0x004)
245
#define SM501_OFF_DC_V_SYNC                (0x00C)
246

    
247
#define SM501_DC_PANEL_CONTROL                (0x000)
248

    
249
#define SM501_DC_PANEL_CONTROL_FPEN        (1<<27)
250
#define SM501_DC_PANEL_CONTROL_BIAS        (1<<26)
251
#define SM501_DC_PANEL_CONTROL_DATA        (1<<25)
252
#define SM501_DC_PANEL_CONTROL_VDD        (1<<24)
253
#define SM501_DC_PANEL_CONTROL_DP        (1<<23)
254

    
255
#define SM501_DC_PANEL_CONTROL_TFT_888        (0<<21)
256
#define SM501_DC_PANEL_CONTROL_TFT_333        (1<<21)
257
#define SM501_DC_PANEL_CONTROL_TFT_444        (2<<21)
258

    
259
#define SM501_DC_PANEL_CONTROL_DE        (1<<20)
260

    
261
#define SM501_DC_PANEL_CONTROL_LCD_TFT        (0<<18)
262
#define SM501_DC_PANEL_CONTROL_LCD_STN8        (1<<18)
263
#define SM501_DC_PANEL_CONTROL_LCD_STN12 (2<<18)
264

    
265
#define SM501_DC_PANEL_CONTROL_CP        (1<<14)
266
#define SM501_DC_PANEL_CONTROL_VSP        (1<<13)
267
#define SM501_DC_PANEL_CONTROL_HSP        (1<<12)
268
#define SM501_DC_PANEL_CONTROL_CK        (1<<9)
269
#define SM501_DC_PANEL_CONTROL_TE        (1<<8)
270
#define SM501_DC_PANEL_CONTROL_VPD        (1<<7)
271
#define SM501_DC_PANEL_CONTROL_VP        (1<<6)
272
#define SM501_DC_PANEL_CONTROL_HPD        (1<<5)
273
#define SM501_DC_PANEL_CONTROL_HP        (1<<4)
274
#define SM501_DC_PANEL_CONTROL_GAMMA        (1<<3)
275
#define SM501_DC_PANEL_CONTROL_EN        (1<<2)
276

    
277
#define SM501_DC_PANEL_CONTROL_8BPP        (0<<0)
278
#define SM501_DC_PANEL_CONTROL_16BPP        (1<<0)
279
#define SM501_DC_PANEL_CONTROL_32BPP        (2<<0)
280

    
281

    
282
#define SM501_DC_PANEL_PANNING_CONTROL        (0x004)
283
#define SM501_DC_PANEL_COLOR_KEY        (0x008)
284
#define SM501_DC_PANEL_FB_ADDR                (0x00C)
285
#define SM501_DC_PANEL_FB_OFFSET        (0x010)
286
#define SM501_DC_PANEL_FB_WIDTH                (0x014)
287
#define SM501_DC_PANEL_FB_HEIGHT        (0x018)
288
#define SM501_DC_PANEL_TL_LOC                (0x01C)
289
#define SM501_DC_PANEL_BR_LOC                (0x020)
290
#define SM501_DC_PANEL_H_TOT                (0x024)
291
#define SM501_DC_PANEL_H_SYNC                (0x028)
292
#define SM501_DC_PANEL_V_TOT                (0x02C)
293
#define SM501_DC_PANEL_V_SYNC                (0x030)
294
#define SM501_DC_PANEL_CUR_LINE                (0x034)
295

    
296
#define SM501_DC_VIDEO_CONTROL                (0x040)
297
#define SM501_DC_VIDEO_FB0_ADDR                (0x044)
298
#define SM501_DC_VIDEO_FB_WIDTH                (0x048)
299
#define SM501_DC_VIDEO_FB0_LAST_ADDR        (0x04C)
300
#define SM501_DC_VIDEO_TL_LOC                (0x050)
301
#define SM501_DC_VIDEO_BR_LOC                (0x054)
302
#define SM501_DC_VIDEO_SCALE                (0x058)
303
#define SM501_DC_VIDEO_INIT_SCALE        (0x05C)
304
#define SM501_DC_VIDEO_YUV_CONSTANTS        (0x060)
305
#define SM501_DC_VIDEO_FB1_ADDR                (0x064)
306
#define SM501_DC_VIDEO_FB1_LAST_ADDR        (0x068)
307

    
308
#define SM501_DC_VIDEO_ALPHA_CONTROL        (0x080)
309
#define SM501_DC_VIDEO_ALPHA_FB_ADDR        (0x084)
310
#define SM501_DC_VIDEO_ALPHA_FB_OFFSET        (0x088)
311
#define SM501_DC_VIDEO_ALPHA_FB_LAST_ADDR        (0x08C)
312
#define SM501_DC_VIDEO_ALPHA_TL_LOC        (0x090)
313
#define SM501_DC_VIDEO_ALPHA_BR_LOC        (0x094)
314
#define SM501_DC_VIDEO_ALPHA_SCALE        (0x098)
315
#define SM501_DC_VIDEO_ALPHA_INIT_SCALE        (0x09C)
316
#define SM501_DC_VIDEO_ALPHA_CHROMA_KEY        (0x0A0)
317
#define SM501_DC_VIDEO_ALPHA_COLOR_LOOKUP        (0x0A4)
318

    
319
#define SM501_DC_PANEL_HWC_BASE                (0x0F0)
320
#define SM501_DC_PANEL_HWC_ADDR                (0x0F0)
321
#define SM501_DC_PANEL_HWC_LOC                (0x0F4)
322
#define SM501_DC_PANEL_HWC_COLOR_1_2        (0x0F8)
323
#define SM501_DC_PANEL_HWC_COLOR_3        (0x0FC)
324

    
325
#define SM501_HWC_EN                        (1<<31)
326

    
327
#define SM501_OFF_HWC_ADDR                (0x00)
328
#define SM501_OFF_HWC_LOC                (0x04)
329
#define SM501_OFF_HWC_COLOR_1_2                (0x08)
330
#define SM501_OFF_HWC_COLOR_3                (0x0C)
331

    
332
#define SM501_DC_ALPHA_CONTROL                (0x100)
333
#define SM501_DC_ALPHA_FB_ADDR                (0x104)
334
#define SM501_DC_ALPHA_FB_OFFSET        (0x108)
335
#define SM501_DC_ALPHA_TL_LOC                (0x10C)
336
#define SM501_DC_ALPHA_BR_LOC                (0x110)
337
#define SM501_DC_ALPHA_CHROMA_KEY        (0x114)
338
#define SM501_DC_ALPHA_COLOR_LOOKUP        (0x118)
339

    
340
#define SM501_DC_CRT_CONTROL                (0x200)
341

    
342
#define SM501_DC_CRT_CONTROL_TVP        (1<<15)
343
#define SM501_DC_CRT_CONTROL_CP                (1<<14)
344
#define SM501_DC_CRT_CONTROL_VSP        (1<<13)
345
#define SM501_DC_CRT_CONTROL_HSP        (1<<12)
346
#define SM501_DC_CRT_CONTROL_VS                (1<<11)
347
#define SM501_DC_CRT_CONTROL_BLANK        (1<<10)
348
#define SM501_DC_CRT_CONTROL_SEL        (1<<9)
349
#define SM501_DC_CRT_CONTROL_TE                (1<<8)
350
#define SM501_DC_CRT_CONTROL_PIXEL_MASK (0xF << 4)
351
#define SM501_DC_CRT_CONTROL_GAMMA        (1<<3)
352
#define SM501_DC_CRT_CONTROL_ENABLE        (1<<2)
353

    
354
#define SM501_DC_CRT_CONTROL_8BPP        (0<<0)
355
#define SM501_DC_CRT_CONTROL_16BPP        (1<<0)
356
#define SM501_DC_CRT_CONTROL_32BPP        (2<<0)
357

    
358
#define SM501_DC_CRT_FB_ADDR                (0x204)
359
#define SM501_DC_CRT_FB_OFFSET                (0x208)
360
#define SM501_DC_CRT_H_TOT                (0x20C)
361
#define SM501_DC_CRT_H_SYNC                (0x210)
362
#define SM501_DC_CRT_V_TOT                (0x214)
363
#define SM501_DC_CRT_V_SYNC                (0x218)
364
#define SM501_DC_CRT_SIGNATURE_ANALYZER        (0x21C)
365
#define SM501_DC_CRT_CUR_LINE                (0x220)
366
#define SM501_DC_CRT_MONITOR_DETECT        (0x224)
367

    
368
#define SM501_DC_CRT_HWC_BASE                (0x230)
369
#define SM501_DC_CRT_HWC_ADDR                (0x230)
370
#define SM501_DC_CRT_HWC_LOC                (0x234)
371
#define SM501_DC_CRT_HWC_COLOR_1_2        (0x238)
372
#define SM501_DC_CRT_HWC_COLOR_3        (0x23C)
373

    
374
#define SM501_DC_PANEL_PALETTE                (0x400)
375

    
376
#define SM501_DC_VIDEO_PALETTE                (0x800)
377

    
378
#define SM501_DC_CRT_PALETTE                (0xC00)
379

    
380
/* Zoom Video port base */
381
#define SM501_ZVPORT                        (0x090000)
382

    
383
/* AC97/I2S base */
384
#define SM501_AC97                        (0x0A0000)
385

    
386
/* 8051 micro controller base */
387
#define SM501_UCONTROLLER                (0x0B0000)
388

    
389
/* 8051 micro controller SRAM base */
390
#define SM501_UCONTROLLER_SRAM                (0x0C0000)
391

    
392
/* DMA base */
393
#define SM501_DMA                        (0x0D0000)
394

    
395
/* 2d engine base */
396
#define SM501_2D_ENGINE                        (0x100000)
397
#define SM501_2D_SOURCE                        (0x00)
398
#define SM501_2D_DESTINATION                (0x04)
399
#define SM501_2D_DIMENSION                (0x08)
400
#define SM501_2D_CONTROL                (0x0C)
401
#define SM501_2D_PITCH                        (0x10)
402
#define SM501_2D_FOREGROUND                (0x14)
403
#define SM501_2D_BACKGROUND                (0x18)
404
#define SM501_2D_STRETCH                (0x1C)
405
#define SM501_2D_COLOR_COMPARE                (0x20)
406
#define SM501_2D_COLOR_COMPARE_MASK         (0x24)
407
#define SM501_2D_MASK                        (0x28)
408
#define SM501_2D_CLIP_TL                (0x2C)
409
#define SM501_2D_CLIP_BR                (0x30)
410
#define SM501_2D_MONO_PATTERN_LOW        (0x34)
411
#define SM501_2D_MONO_PATTERN_HIGH        (0x38)
412
#define SM501_2D_WINDOW_WIDTH                (0x3C)
413
#define SM501_2D_SOURCE_BASE                (0x40)
414
#define SM501_2D_DESTINATION_BASE        (0x44)
415
#define SM501_2D_ALPHA                        (0x48)
416
#define SM501_2D_WRAP                        (0x4C)
417
#define SM501_2D_STATUS                        (0x50)
418

    
419
#define SM501_CSC_Y_SOURCE_BASE                (0xC8)
420
#define SM501_CSC_CONSTANTS                (0xCC)
421
#define SM501_CSC_Y_SOURCE_X                (0xD0)
422
#define SM501_CSC_Y_SOURCE_Y                (0xD4)
423
#define SM501_CSC_U_SOURCE_BASE                (0xD8)
424
#define SM501_CSC_V_SOURCE_BASE                (0xDC)
425
#define SM501_CSC_SOURCE_DIMENSION        (0xE0)
426
#define SM501_CSC_SOURCE_PITCH                (0xE4)
427
#define SM501_CSC_DESTINATION                (0xE8)
428
#define SM501_CSC_DESTINATION_DIMENSION        (0xEC)
429
#define SM501_CSC_DESTINATION_PITCH        (0xF0)
430
#define SM501_CSC_SCALE_FACTOR                (0xF4)
431
#define SM501_CSC_DESTINATION_BASE        (0xF8)
432
#define SM501_CSC_CONTROL                (0xFC)
433

    
434
/* 2d engine data port base */
435
#define SM501_2D_ENGINE_DATA                (0x110000)
436

    
437
/* end of register definitions */
438

    
439
#define SM501_HWC_WIDTH                       (64)
440
#define SM501_HWC_HEIGHT                      (64)
441

    
442
/* SM501 local memory size taken from "linux/drivers/mfd/sm501.c" */
443
static const uint32_t sm501_mem_local_size[] = {
444
        [0]        = 4*1024*1024,
445
        [1]        = 8*1024*1024,
446
        [2]        = 16*1024*1024,
447
        [3]        = 32*1024*1024,
448
        [4]        = 64*1024*1024,
449
        [5]        = 2*1024*1024,
450
};
451
#define get_local_mem_size(s) sm501_mem_local_size[(s)->local_mem_size_index]
452

    
453
typedef struct SM501State {
454
    /* graphic console status */
455
    DisplayState *ds;
456

    
457
    /* status & internal resources */
458
    target_phys_addr_t base;
459
    uint32_t local_mem_size_index;
460
    uint8_t * local_mem;
461
    ram_addr_t local_mem_offset;
462
    uint32_t last_width;
463
    uint32_t last_height;
464

    
465
    /* mmio registers */
466
    uint32_t system_control;
467
    uint32_t misc_control;
468
    uint32_t gpio_31_0_control;
469
    uint32_t gpio_63_32_control;
470
    uint32_t dram_control;
471
    uint32_t irq_mask;
472
    uint32_t misc_timing;
473
    uint32_t power_mode_control;
474

    
475
    uint32_t uart0_ier;
476
    uint32_t uart0_lcr;
477
    uint32_t uart0_mcr;
478
    uint32_t uart0_scr;
479

    
480
    uint8_t dc_palette[0x400 * 3];
481

    
482
    uint32_t dc_panel_control;
483
    uint32_t dc_panel_panning_control;
484
    uint32_t dc_panel_fb_addr;
485
    uint32_t dc_panel_fb_offset;
486
    uint32_t dc_panel_fb_width;
487
    uint32_t dc_panel_fb_height;
488
    uint32_t dc_panel_tl_location;
489
    uint32_t dc_panel_br_location;
490
    uint32_t dc_panel_h_total;
491
    uint32_t dc_panel_h_sync;
492
    uint32_t dc_panel_v_total;
493
    uint32_t dc_panel_v_sync;
494

    
495
    uint32_t dc_panel_hwc_addr;
496
    uint32_t dc_panel_hwc_location;
497
    uint32_t dc_panel_hwc_color_1_2;
498
    uint32_t dc_panel_hwc_color_3;
499

    
500
    uint32_t dc_crt_control;
501
    uint32_t dc_crt_fb_addr;
502
    uint32_t dc_crt_fb_offset;
503
    uint32_t dc_crt_h_total;
504
    uint32_t dc_crt_h_sync;
505
    uint32_t dc_crt_v_total;
506
    uint32_t dc_crt_v_sync;
507

    
508
    uint32_t dc_crt_hwc_addr;
509
    uint32_t dc_crt_hwc_location;
510
    uint32_t dc_crt_hwc_color_1_2;
511
    uint32_t dc_crt_hwc_color_3;
512

    
513
} SM501State;
514

    
515
static uint32_t get_local_mem_size_index(uint32_t size)
516
{
517
    uint32_t norm_size = 0;
518
    int i, index = 0;
519

    
520
    for (i = 0; i < ARRAY_SIZE(sm501_mem_local_size); i++) {
521
        uint32_t new_size = sm501_mem_local_size[i];
522
        if (new_size >= size) {
523
            if (norm_size == 0 || norm_size > new_size) {
524
                norm_size = new_size;
525
                index = i;
526
            }
527
        }
528
    }
529

    
530
    return index;
531
}
532

    
533
/**
534
 * Check the availability of hardware cursor.
535
 * @param crt  0 for PANEL, 1 for CRT.
536
 */
537
static inline int is_hwc_enabled(SM501State *state, int crt)
538
{
539
    uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
540
    return addr & 0x80000000;
541
}
542

    
543
/**
544
 * Get the address which holds cursor pattern data.
545
 * @param crt  0 for PANEL, 1 for CRT.
546
 */
547
static inline uint32_t get_hwc_address(SM501State *state, int crt)
548
{
549
    uint32_t addr = crt ? state->dc_crt_hwc_addr : state->dc_panel_hwc_addr;
550
    return (addr & 0x03FFFFF0)/* >> 4*/;
551
}
552

    
553
/**
554
 * Get the cursor position in y coordinate.
555
 * @param crt  0 for PANEL, 1 for CRT.
556
 */
557
static inline uint32_t get_hwc_y(SM501State *state, int crt)
558
{
559
    uint32_t location = crt ? state->dc_crt_hwc_location
560
                            : state->dc_panel_hwc_location;
561
    return (location & 0x07FF0000) >> 16;
562
}
563

    
564
/**
565
 * Get the cursor position in x coordinate.
566
 * @param crt  0 for PANEL, 1 for CRT.
567
 */
568
static inline uint32_t get_hwc_x(SM501State *state, int crt)
569
{
570
    uint32_t location = crt ? state->dc_crt_hwc_location
571
                            : state->dc_panel_hwc_location;
572
    return location & 0x000007FF;
573
}
574

    
575
/**
576
 * Get the cursor position in x coordinate.
577
 * @param crt  0 for PANEL, 1 for CRT.
578
 * @param index  0, 1, 2 or 3 which specifies color of corsor dot.
579
 */
580
static inline uint16_t get_hwc_color(SM501State *state, int crt, int index)
581
{
582
    uint16_t color_reg = 0;
583
    uint16_t color_565 = 0;
584

    
585
    if (index == 0) {
586
        return 0;
587
    }
588

    
589
    switch (index) {
590
    case 1:
591
    case 2:
592
        color_reg = crt ? state->dc_crt_hwc_color_1_2
593
                        : state->dc_panel_hwc_color_1_2;
594
        break;
595
    case 3:
596
        color_reg = crt ? state->dc_crt_hwc_color_3
597
                        : state->dc_panel_hwc_color_3;
598
        break;
599
    default:
600
        printf("invalid hw cursor color.\n");
601
        abort();
602
    }
603

    
604
    switch (index) {
605
    case 1:
606
    case 3:
607
        color_565 = (uint16_t)(color_reg & 0xFFFF);
608
        break;
609
    case 2:
610
        color_565 = (uint16_t)((color_reg >> 16) & 0xFFFF);
611
        break;
612
    }
613
    return color_565;
614
}
615

    
616
static int within_hwc_y_range(SM501State *state, int y, int crt)
617
{
618
    int hwc_y = get_hwc_y(state, crt);
619
    return (hwc_y <= y && y < hwc_y + SM501_HWC_HEIGHT);
620
}
621

    
622
static uint32_t sm501_system_config_read(void *opaque, target_phys_addr_t addr)
623
{
624
    SM501State * s = (SM501State *)opaque;
625
    uint32_t ret = 0;
626
    SM501_DPRINTF("sm501 system config regs : read addr=%x\n", (int)addr);
627

    
628
    switch(addr) {
629
    case SM501_SYSTEM_CONTROL:
630
        ret = s->system_control;
631
        break;
632
    case SM501_MISC_CONTROL:
633
        ret = s->misc_control;
634
        break;
635
    case SM501_GPIO31_0_CONTROL:
636
        ret = s->gpio_31_0_control;
637
        break;
638
    case SM501_GPIO63_32_CONTROL:
639
        ret = s->gpio_63_32_control;
640
        break;
641
    case SM501_DEVICEID:
642
        ret = 0x050100A0;
643
        break;
644
    case SM501_DRAM_CONTROL:
645
        ret = (s->dram_control & 0x07F107C0) | s->local_mem_size_index << 13;
646
        break;
647
    case SM501_IRQ_MASK:
648
        ret = s->irq_mask;
649
        break;
650
    case SM501_MISC_TIMING:
651
        /* TODO : simulate gate control */
652
        ret = s->misc_timing;
653
        break;
654
    case SM501_CURRENT_GATE:
655
        /* TODO : simulate gate control */
656
        ret = 0x00021807;
657
        break;
658
    case SM501_CURRENT_CLOCK:
659
        ret = 0x2A1A0A09;
660
        break;
661
    case SM501_POWER_MODE_CONTROL:
662
        ret = s->power_mode_control;
663
        break;
664

    
665
    default:
666
        printf("sm501 system config : not implemented register read."
667
               " addr=%x\n", (int)addr);
668
        abort();
669
    }
670

    
671
    return ret;
672
}
673

    
674
static void sm501_system_config_write(void *opaque,
675
                                      target_phys_addr_t addr, uint32_t value)
676
{
677
    SM501State * s = (SM501State *)opaque;
678
    SM501_DPRINTF("sm501 system config regs : write addr=%x, val=%x\n",
679
                  addr, value);
680

    
681
    switch(addr) {
682
    case SM501_SYSTEM_CONTROL:
683
        s->system_control = value & 0xE300B8F7;
684
        break;
685
    case SM501_MISC_CONTROL:
686
        s->misc_control = value & 0xFF7FFF20;
687
        break;
688
    case SM501_GPIO31_0_CONTROL:
689
        s->gpio_31_0_control = value;
690
        break;
691
    case SM501_GPIO63_32_CONTROL:
692
        s->gpio_63_32_control = value;
693
        break;
694
    case SM501_DRAM_CONTROL:
695
        s->local_mem_size_index = (value >> 13) & 0x7;
696
        /* rODO : check validity of size change */
697
        s->dram_control |=  value & 0x7FFFFFC3;
698
        break;
699
    case SM501_IRQ_MASK:
700
        s->irq_mask = value;
701
        break;
702
    case SM501_MISC_TIMING:
703
        s->misc_timing = value & 0xF31F1FFF;
704
        break;
705
    case SM501_POWER_MODE_0_GATE:
706
    case SM501_POWER_MODE_1_GATE:
707
    case SM501_POWER_MODE_0_CLOCK:
708
    case SM501_POWER_MODE_1_CLOCK:
709
        /* TODO : simulate gate & clock control */
710
        break;
711
    case SM501_POWER_MODE_CONTROL:
712
        s->power_mode_control = value & 0x00000003;
713
        break;
714

    
715
    default:
716
        printf("sm501 system config : not implemented register write."
717
               " addr=%x, val=%x\n", (int)addr, value);
718
        abort();
719
    }
720
}
721

    
722
static CPUReadMemoryFunc * const sm501_system_config_readfn[] = {
723
    NULL,
724
    NULL,
725
    &sm501_system_config_read,
726
};
727

    
728
static CPUWriteMemoryFunc * const sm501_system_config_writefn[] = {
729
    NULL,
730
    NULL,
731
    &sm501_system_config_write,
732
};
733

    
734
static uint32_t sm501_palette_read(void *opaque, target_phys_addr_t addr)
735
{
736
    SM501State * s = (SM501State *)opaque;
737
    SM501_DPRINTF("sm501 palette read addr=%x\n", (int)addr);
738

    
739
    /* TODO : consider BYTE/WORD access */
740
    /* TODO : consider endian */
741

    
742
    assert(0 <= addr && addr < 0x400 * 3);
743
    return *(uint32_t*)&s->dc_palette[addr];
744
}
745

    
746
static void sm501_palette_write(void *opaque,
747
                                target_phys_addr_t addr, uint32_t value)
748
{
749
    SM501State * s = (SM501State *)opaque;
750
    SM501_DPRINTF("sm501 palette write addr=%x, val=%x\n",
751
                  (int)addr, value);
752

    
753
    /* TODO : consider BYTE/WORD access */
754
    /* TODO : consider endian */
755

    
756
    assert(0 <= addr && addr < 0x400 * 3);
757
    *(uint32_t*)&s->dc_palette[addr] = value;
758
}
759

    
760
static uint32_t sm501_disp_ctrl_read(void *opaque, target_phys_addr_t addr)
761
{
762
    SM501State * s = (SM501State *)opaque;
763
    uint32_t ret = 0;
764
    SM501_DPRINTF("sm501 disp ctrl regs : read addr=%x\n", (int)addr);
765

    
766
    switch(addr) {
767

    
768
    case SM501_DC_PANEL_CONTROL:
769
        ret = s->dc_panel_control;
770
        break;
771
    case SM501_DC_PANEL_PANNING_CONTROL:
772
        ret = s->dc_panel_panning_control;
773
        break;
774
    case SM501_DC_PANEL_FB_ADDR:
775
        ret = s->dc_panel_fb_addr;
776
        break;
777
    case SM501_DC_PANEL_FB_OFFSET:
778
        ret = s->dc_panel_fb_offset;
779
        break;
780
    case SM501_DC_PANEL_FB_WIDTH:
781
        ret = s->dc_panel_fb_width;
782
        break;
783
    case SM501_DC_PANEL_FB_HEIGHT:
784
        ret = s->dc_panel_fb_height;
785
        break;
786
    case SM501_DC_PANEL_TL_LOC:
787
        ret = s->dc_panel_tl_location;
788
        break;
789
    case SM501_DC_PANEL_BR_LOC:
790
        ret = s->dc_panel_br_location;
791
        break;
792

    
793
    case SM501_DC_PANEL_H_TOT:
794
        ret = s->dc_panel_h_total;
795
        break;
796
    case SM501_DC_PANEL_H_SYNC:
797
        ret = s->dc_panel_h_sync;
798
        break;
799
    case SM501_DC_PANEL_V_TOT:
800
        ret = s->dc_panel_v_total;
801
        break;
802
    case SM501_DC_PANEL_V_SYNC:
803
        ret = s->dc_panel_v_sync;
804
        break;
805

    
806
    case SM501_DC_CRT_CONTROL:
807
        ret = s->dc_crt_control;
808
        break;
809
    case SM501_DC_CRT_FB_ADDR:
810
        ret = s->dc_crt_fb_addr;
811
        break;
812
    case SM501_DC_CRT_FB_OFFSET:
813
        ret = s->dc_crt_fb_offset;
814
        break;
815
    case SM501_DC_CRT_H_TOT:
816
        ret = s->dc_crt_h_total;
817
        break;
818
    case SM501_DC_CRT_H_SYNC:
819
        ret = s->dc_crt_h_sync;
820
        break;
821
    case SM501_DC_CRT_V_TOT:
822
        ret = s->dc_crt_v_total;
823
        break;
824
    case SM501_DC_CRT_V_SYNC:
825
        ret = s->dc_crt_v_sync;
826
        break;
827

    
828
    case SM501_DC_CRT_HWC_ADDR:
829
        ret = s->dc_crt_hwc_addr;
830
        break;
831
    case SM501_DC_CRT_HWC_LOC:
832
        ret = s->dc_crt_hwc_location;
833
        break;
834
    case SM501_DC_CRT_HWC_COLOR_1_2:
835
        ret = s->dc_crt_hwc_color_1_2;
836
        break;
837
    case SM501_DC_CRT_HWC_COLOR_3:
838
        ret = s->dc_crt_hwc_color_3;
839
        break;
840

    
841
    case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400*3 - 4:
842
        ret = sm501_palette_read(opaque, addr - SM501_DC_PANEL_PALETTE);
843
        break;
844

    
845
    default:
846
        printf("sm501 disp ctrl : not implemented register read."
847
               " addr=%x\n", (int)addr);
848
        abort();
849
    }
850

    
851
    return ret;
852
}
853

    
854
static void sm501_disp_ctrl_write(void *opaque,
855
                                           target_phys_addr_t addr,
856
                                           uint32_t value)
857
{
858
    SM501State * s = (SM501State *)opaque;
859
    SM501_DPRINTF("sm501 disp ctrl regs : write addr=%x, val=%x\n",
860
                  addr, value);
861

    
862
    switch(addr) {
863
    case SM501_DC_PANEL_CONTROL:
864
        s->dc_panel_control = value & 0x0FFF73FF;
865
        break;
866
    case SM501_DC_PANEL_PANNING_CONTROL:
867
        s->dc_panel_panning_control = value & 0xFF3FFF3F;
868
        break;
869
    case SM501_DC_PANEL_FB_ADDR:
870
        s->dc_panel_fb_addr = value & 0x8FFFFFF0;
871
        break;
872
    case SM501_DC_PANEL_FB_OFFSET:
873
        s->dc_panel_fb_offset = value & 0x3FF03FF0;
874
        break;
875
    case SM501_DC_PANEL_FB_WIDTH:
876
        s->dc_panel_fb_width = value & 0x0FFF0FFF;
877
        break;
878
    case SM501_DC_PANEL_FB_HEIGHT:
879
        s->dc_panel_fb_height = value & 0x0FFF0FFF;
880
        break;
881
    case SM501_DC_PANEL_TL_LOC:
882
        s->dc_panel_tl_location = value & 0x07FF07FF;
883
        break;
884
    case SM501_DC_PANEL_BR_LOC:
885
        s->dc_panel_br_location = value & 0x07FF07FF;
886
        break;
887

    
888
    case SM501_DC_PANEL_H_TOT:
889
        s->dc_panel_h_total = value & 0x0FFF0FFF;
890
        break;
891
    case SM501_DC_PANEL_H_SYNC:
892
        s->dc_panel_h_sync = value & 0x00FF0FFF;
893
        break;
894
    case SM501_DC_PANEL_V_TOT:
895
        s->dc_panel_v_total = value & 0x0FFF0FFF;
896
        break;
897
    case SM501_DC_PANEL_V_SYNC:
898
        s->dc_panel_v_sync = value & 0x003F0FFF;
899
        break;
900

    
901
    case SM501_DC_PANEL_HWC_ADDR:
902
        s->dc_panel_hwc_addr = value & 0x8FFFFFF0;
903
        break;
904
    case SM501_DC_PANEL_HWC_LOC:
905
        s->dc_panel_hwc_location = value & 0x0FFF0FFF;
906
        break;
907
    case SM501_DC_PANEL_HWC_COLOR_1_2:
908
        s->dc_panel_hwc_color_1_2 = value;
909
        break;
910
    case SM501_DC_PANEL_HWC_COLOR_3:
911
        s->dc_panel_hwc_color_3 = value & 0x0000FFFF;
912
        break;
913

    
914
    case SM501_DC_CRT_CONTROL:
915
        s->dc_crt_control = value & 0x0003FFFF;
916
        break;
917
    case SM501_DC_CRT_FB_ADDR:
918
        s->dc_crt_fb_addr = value & 0x8FFFFFF0;
919
        break;
920
    case SM501_DC_CRT_FB_OFFSET:
921
        s->dc_crt_fb_offset = value & 0x3FF03FF0;
922
        break;
923
    case SM501_DC_CRT_H_TOT:
924
        s->dc_crt_h_total = value & 0x0FFF0FFF;
925
        break;
926
    case SM501_DC_CRT_H_SYNC:
927
        s->dc_crt_h_sync = value & 0x00FF0FFF;
928
        break;
929
    case SM501_DC_CRT_V_TOT:
930
        s->dc_crt_v_total = value & 0x0FFF0FFF;
931
        break;
932
    case SM501_DC_CRT_V_SYNC:
933
        s->dc_crt_v_sync = value & 0x003F0FFF;
934
        break;
935

    
936
    case SM501_DC_CRT_HWC_ADDR:
937
        s->dc_crt_hwc_addr = value & 0x8FFFFFF0;
938
        break;
939
    case SM501_DC_CRT_HWC_LOC:
940
        s->dc_crt_hwc_location = value & 0x0FFF0FFF;
941
        break;
942
    case SM501_DC_CRT_HWC_COLOR_1_2:
943
        s->dc_crt_hwc_color_1_2 = value;
944
        break;
945
    case SM501_DC_CRT_HWC_COLOR_3:
946
        s->dc_crt_hwc_color_3 = value & 0x0000FFFF;
947
        break;
948

    
949
    case SM501_DC_PANEL_PALETTE ... SM501_DC_PANEL_PALETTE + 0x400*3 - 4:
950
        sm501_palette_write(opaque, addr - SM501_DC_PANEL_PALETTE, value);
951
        break;
952

    
953
    default:
954
        printf("sm501 disp ctrl : not implemented register write."
955
               " addr=%x, val=%x\n", (int)addr, value);
956
        abort();
957
    }
958
}
959

    
960
static CPUReadMemoryFunc * const sm501_disp_ctrl_readfn[] = {
961
    NULL,
962
    NULL,
963
    &sm501_disp_ctrl_read,
964
};
965

    
966
static CPUWriteMemoryFunc * const sm501_disp_ctrl_writefn[] = {
967
    NULL,
968
    NULL,
969
    &sm501_disp_ctrl_write,
970
};
971

    
972
/* draw line functions for all console modes */
973

    
974
#include "pixel_ops.h"
975

    
976
typedef void draw_line_func(uint8_t *d, const uint8_t *s,
977
                            int width, const uint32_t *pal);
978

    
979
typedef void draw_hwc_line_func(SM501State * s, int crt, uint8_t * palette,
980
                                int c_y, uint8_t *d, int width);
981

    
982
#define DEPTH 8
983
#include "sm501_template.h"
984

    
985
#define DEPTH 15
986
#include "sm501_template.h"
987

    
988
#define BGR_FORMAT
989
#define DEPTH 15
990
#include "sm501_template.h"
991

    
992
#define DEPTH 16
993
#include "sm501_template.h"
994

    
995
#define BGR_FORMAT
996
#define DEPTH 16
997
#include "sm501_template.h"
998

    
999
#define DEPTH 32
1000
#include "sm501_template.h"
1001

    
1002
#define BGR_FORMAT
1003
#define DEPTH 32
1004
#include "sm501_template.h"
1005

    
1006
static draw_line_func * draw_line8_funcs[] = {
1007
    draw_line8_8,
1008
    draw_line8_15,
1009
    draw_line8_16,
1010
    draw_line8_32,
1011
    draw_line8_32bgr,
1012
    draw_line8_15bgr,
1013
    draw_line8_16bgr,
1014
};
1015

    
1016
static draw_line_func * draw_line16_funcs[] = {
1017
    draw_line16_8,
1018
    draw_line16_15,
1019
    draw_line16_16,
1020
    draw_line16_32,
1021
    draw_line16_32bgr,
1022
    draw_line16_15bgr,
1023
    draw_line16_16bgr,
1024
};
1025

    
1026
static draw_line_func * draw_line32_funcs[] = {
1027
    draw_line32_8,
1028
    draw_line32_15,
1029
    draw_line32_16,
1030
    draw_line32_32,
1031
    draw_line32_32bgr,
1032
    draw_line32_15bgr,
1033
    draw_line32_16bgr,
1034
};
1035

    
1036
static draw_hwc_line_func * draw_hwc_line_funcs[] = {
1037
    draw_hwc_line_8,
1038
    draw_hwc_line_15,
1039
    draw_hwc_line_16,
1040
    draw_hwc_line_32,
1041
    draw_hwc_line_32bgr,
1042
    draw_hwc_line_15bgr,
1043
    draw_hwc_line_16bgr,
1044
};
1045

    
1046
static inline int get_depth_index(DisplayState *s)
1047
{
1048
    switch(ds_get_bits_per_pixel(s)) {
1049
    default:
1050
    case 8:
1051
        return 0;
1052
    case 15:
1053
        return 1;
1054
    case 16:
1055
        return 2;
1056
    case 32:
1057
        if (is_surface_bgr(s->surface))
1058
            return 4;
1059
        else
1060
            return 3;
1061
    }
1062
}
1063

    
1064
static void sm501_draw_crt(SM501State * s)
1065
{
1066
    int y;
1067
    int width = (s->dc_crt_h_total & 0x00000FFF) + 1;
1068
    int height = (s->dc_crt_v_total & 0x00000FFF) + 1;
1069

    
1070
    uint8_t  * src = s->local_mem;
1071
    int src_bpp = 0;
1072
    int dst_bpp = ds_get_bytes_per_pixel(s->ds) + (ds_get_bits_per_pixel(s->ds) % 8 ? 1 : 0);
1073
    uint32_t * palette = (uint32_t *)&s->dc_palette[SM501_DC_CRT_PALETTE
1074
                                                    - SM501_DC_PANEL_PALETTE];
1075
    uint8_t hwc_palette[3 * 3];
1076
    int ds_depth_index = get_depth_index(s->ds);
1077
    draw_line_func * draw_line = NULL;
1078
    draw_hwc_line_func * draw_hwc_line = NULL;
1079
    int full_update = 0;
1080
    int y_start = -1;
1081
    int page_min = 0x7fffffff;
1082
    int page_max = -1;
1083
    ram_addr_t offset = s->local_mem_offset;
1084

    
1085
    /* choose draw_line function */
1086
    switch (s->dc_crt_control & 3) {
1087
    case SM501_DC_CRT_CONTROL_8BPP:
1088
        src_bpp = 1;
1089
        draw_line = draw_line8_funcs[ds_depth_index];
1090
        break;
1091
    case SM501_DC_CRT_CONTROL_16BPP:
1092
        src_bpp = 2;
1093
        draw_line = draw_line16_funcs[ds_depth_index];
1094
        break;
1095
    case SM501_DC_CRT_CONTROL_32BPP:
1096
        src_bpp = 4;
1097
        draw_line = draw_line32_funcs[ds_depth_index];
1098
        break;
1099
    default:
1100
        printf("sm501 draw crt : invalid DC_CRT_CONTROL=%x.\n",
1101
               s->dc_crt_control);
1102
        abort();
1103
        break;
1104
    }
1105

    
1106
    /* set up to draw hardware cursor */
1107
    if (is_hwc_enabled(s, 1)) {
1108
        int i;
1109

    
1110
        /* get cursor palette */
1111
        for (i = 0; i < 3; i++) {
1112
            uint16_t rgb565 = get_hwc_color(s, 1, i + 1);
1113
            hwc_palette[i * 3 + 0] = (rgb565 & 0xf800) >> 8; /* red */
1114
            hwc_palette[i * 3 + 1] = (rgb565 & 0x07e0) >> 3; /* green */
1115
            hwc_palette[i * 3 + 2] = (rgb565 & 0x001f) << 3; /* blue */
1116
        }
1117

    
1118
        /* choose cursor draw line function */
1119
        draw_hwc_line = draw_hwc_line_funcs[ds_depth_index];
1120
    }
1121

    
1122
    /* adjust console size */
1123
    if (s->last_width != width || s->last_height != height) {
1124
        qemu_console_resize(s->ds, width, height);
1125
        s->last_width = width;
1126
        s->last_height = height;
1127
        full_update = 1;
1128
    }
1129

    
1130
    /* draw each line according to conditions */
1131
    for (y = 0; y < height; y++) {
1132
        int update_hwc = draw_hwc_line ? within_hwc_y_range(s, y, 1) : 0;
1133
        int update = full_update || update_hwc;
1134
        ram_addr_t page0 = offset & TARGET_PAGE_MASK;
1135
        ram_addr_t page1 = (offset + width * src_bpp - 1) & TARGET_PAGE_MASK;
1136
        ram_addr_t page;
1137

    
1138
        /* check dirty flags for each line */
1139
        for (page = page0; page <= page1; page += TARGET_PAGE_SIZE)
1140
            if (cpu_physical_memory_get_dirty(page, VGA_DIRTY_FLAG))
1141
                update = 1;
1142

    
1143
        /* draw line and change status */
1144
        if (update) {
1145
            uint8_t * d = &(ds_get_data(s->ds)[y * width * dst_bpp]);
1146

    
1147
            /* draw graphics layer */
1148
            draw_line(d, src, width, palette);
1149

    
1150
            /* draw haredware cursor */
1151
            if (update_hwc) {
1152
                draw_hwc_line(s, 1, hwc_palette, y - get_hwc_y(s, 1), d, width);
1153
            }
1154

    
1155
            if (y_start < 0)
1156
                y_start = y;
1157
            if (page0 < page_min)
1158
                page_min = page0;
1159
            if (page1 > page_max)
1160
                page_max = page1;
1161
        } else {
1162
            if (y_start >= 0) {
1163
                /* flush to display */
1164
                dpy_update(s->ds, 0, y_start, width, y - y_start);
1165
                y_start = -1;
1166
            }
1167
        }
1168

    
1169
        src += width * src_bpp;
1170
        offset += width * src_bpp;
1171
    }
1172

    
1173
    /* complete flush to display */
1174
    if (y_start >= 0)
1175
        dpy_update(s->ds, 0, y_start, width, y - y_start);
1176

    
1177
    /* clear dirty flags */
1178
    if (page_max != -1)
1179
        cpu_physical_memory_reset_dirty(page_min, page_max + TARGET_PAGE_SIZE,
1180
                                        VGA_DIRTY_FLAG);
1181
}
1182

    
1183
static void sm501_update_display(void *opaque)
1184
{
1185
    SM501State * s = (SM501State *)opaque;
1186

    
1187
    if (s->dc_crt_control & SM501_DC_CRT_CONTROL_ENABLE)
1188
        sm501_draw_crt(s);
1189
}
1190

    
1191
void sm501_init(uint32_t base, uint32_t local_mem_bytes, qemu_irq irq,
1192
                CharDriverState *chr)
1193
{
1194
    SM501State * s;
1195
    DeviceState *dev;
1196
    int sm501_system_config_index;
1197
    int sm501_disp_ctrl_index;
1198

    
1199
    /* allocate management data region */
1200
    s = (SM501State *)qemu_mallocz(sizeof(SM501State));
1201
    s->base = base;
1202
    s->local_mem_size_index
1203
        = get_local_mem_size_index(local_mem_bytes);
1204
    SM501_DPRINTF("local mem size=%x. index=%d\n", get_local_mem_size(s),
1205
                  s->local_mem_size_index);
1206
    s->system_control = 0x00100000;
1207
    s->misc_control = 0x00001000; /* assumes SH, active=low */
1208
    s->dc_panel_control = 0x00010000;
1209
    s->dc_crt_control = 0x00010000;
1210

    
1211
    /* allocate local memory */
1212
    s->local_mem_offset = qemu_ram_alloc(local_mem_bytes);
1213
    s->local_mem = qemu_get_ram_ptr(s->local_mem_offset);
1214
    cpu_register_physical_memory(base, local_mem_bytes, s->local_mem_offset);
1215

    
1216
    /* map mmio */
1217
    sm501_system_config_index
1218
        = cpu_register_io_memory(sm501_system_config_readfn,
1219
                                 sm501_system_config_writefn, s);
1220
    cpu_register_physical_memory(base + MMIO_BASE_OFFSET,
1221
                                 0x6c, sm501_system_config_index);
1222
    sm501_disp_ctrl_index = cpu_register_io_memory(sm501_disp_ctrl_readfn,
1223
                                                   sm501_disp_ctrl_writefn, s);
1224
    cpu_register_physical_memory(base + MMIO_BASE_OFFSET + SM501_DC,
1225
                                 0x1000, sm501_disp_ctrl_index);
1226

    
1227
    /* bridge to usb host emulation module */
1228
    dev = qdev_create(NULL, "sysbus-ohci");
1229
    qdev_prop_set_uint32(dev, "num-ports", 2);
1230
    qdev_prop_set_taddr(dev, "dma-offset", base);
1231
    qdev_init_nofail(dev);
1232
    sysbus_mmio_map(sysbus_from_qdev(dev), 0,
1233
                    base + MMIO_BASE_OFFSET + SM501_USB_HOST);
1234
    sysbus_connect_irq(sysbus_from_qdev(dev), 0, irq);
1235

    
1236
    /* bridge to serial emulation module */
1237
    if (chr) {
1238
#ifdef TARGET_WORDS_BIGENDIAN
1239
        serial_mm_init(base + MMIO_BASE_OFFSET + SM501_UART0, 2,
1240
                       NULL, /* TODO : chain irq to IRL */
1241
                       115200, chr, 1, 1);
1242
#else
1243
        serial_mm_init(base + MMIO_BASE_OFFSET + SM501_UART0, 2,
1244
                       NULL, /* TODO : chain irq to IRL */
1245
                       115200, chr, 1, 0);
1246
#endif
1247
    }
1248

    
1249
    /* create qemu graphic console */
1250
    s->ds = graphic_console_init(sm501_update_display, NULL,
1251
                                 NULL, NULL, s);
1252
}