1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * $Id: lcd-as-video.S 26756 2010-06-11 04:41:36Z funman $
10 * Copyright (C) 2010 by Andree Buschmann
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /****************************************************************************
23 * #define FORCE_FIFO_WAIT
25 * This is not needed in YUV blitting when the LCD IF is fast enough. In this
26 * case YUV-to-RGB conversion per pixel needs longer than the transfer of a
27 * pixel via the LCD IF. For iPod nano 2G this is true if the LCD IF is
28 * configured to use LCD_PHTIME = 0x00 (see lcd-nano2g.c).
29 ****************************************************************************/
33 .section .icode, "ax", %progbits
35 /****************************************************************************
36 * void lcd_write_line(const fb_data *addr,
38 * const unsigned int lcd_base_addr);
40 * Writes pixelcount pixels from src-pointer (lcd_framebuffer) to LCD dataport.
43 .global lcd_write_line
44 .type lcd_write_line, %function
45 /* r0 = addr, must be aligned */
46 /* r1 = pixel count, must be even */
47 lcd_write_line: /* r2 = LCD_BASE */
48 stmfd sp!, {r4-r6, lr} /* save non-scratch registers */
49 add r12, r2, #0x40 /* LCD_WDATA = LCD data port */
52 ldmia r0!, {r3, r5} /* read 4 pixels (=8 byte) */
54 /* wait for FIFO half full */
56 ldr lr, [r2, #0x1C] /* while (LCD_STATUS & 0x08); */
60 mov r4, r3, asr #16 /* r3 = 1st pixel, r4 = 2nd pixel */
61 mov r6, r5, asr #16 /* r5 = 3rd pixel, r6 = 4th pixel */
62 stmia r12, {r3-r6} /* write pixels (lowest 16 bit used) */
69 /****************************************************************************
70 * extern void lcd_write_yuv420_lines(unsigned char const * const src[3],
71 * const unsigned LCD_BASE,
75 * Conversion from Motion JPEG and MPEG Y'PbPr to RGB is:
76 * |R| |1.164 0.000 1.596| |Y' - 16|
77 * |G| = |1.164 -0.391 -0.813| |Pb - 128|
78 * |B| |1.164 2.018 0.000| |Pr - 128|
80 * Scaled, normalized, rounded and tweaked to yield RGB 565:
81 * |R| |74 0 101| |Y' - 16| >> 9
82 * |G| = |74 -24 -51| |Cb - 128| >> 8
83 * |B| |74 128 0| |Cr - 128| >> 9
85 * Converts two lines from YUV to RGB565 and writes to LCD at once. First loop
86 * loads Cb/Cr, calculates the chroma offset and saves them to buffer. Within
87 * the second loop these chroma offset are reloaded from buffer. Within each
88 * loop two pixels are calculated and written to LCD.
91 .global lcd_write_yuv420_lines
92 .type lcd_write_yuv420_lines, %function
93 lcd_write_yuv420_lines:
94 /* r0 = src = yuv_src */
95 /* r1 = dst = LCD_BASE */
98 stmfd sp!, { r4-r10, lr } /* save non-scratch */
99 ldmia r0, { r9, r10, r12 } /* r9 = yuv_src[0] = Y'_p */
100 /* r10 = yuv_src[1] = Cb_p */
101 /* r12 = yuv_src[2] = Cr_p */
102 add r3, r9, r3 /* r3 = &ysrc[stride] */
103 add r4, r2, r2, asr #1 /* chroma buffer lenght = width/2 *3 */
104 mov r4, r4, asl #2 /* use words for str/ldm possibility */
105 add r4, r4, #19 /* plus room for 4 additional words, */
106 bic r4, r4, #3 /* rounded up to multiples of 4 byte */
107 sub sp, sp, r4 /* and allocate on stack */
108 stmia sp, {r1-r4} /* LCD_BASE, width, &ysrc[stride], stack_alloc */
110 mov r7, r2 /* r7 = loop count */
111 add r8, sp, #16 /* chroma buffer */
112 add lr, r1, #0x40 /* LCD data port = LCD_BASE + 0x40 */
117 ldrb r0, [r10], #1 /* r0 = *usrc++ = *Cb_p++ */
118 ldrb r1, [r12], #1 /* r1 = *vsrc++ = *Cr_p++ */
120 sub r0, r0, #128 /* r0 = Cb-128 */
121 sub r1, r1, #128 /* r1 = Cr-128 */
123 add r2, r1, r1, asl #1 /* r2 = Cr*51 + Cb*24 */
124 add r2, r2, r2, asl #4
125 add r2, r2, r0, asl #3
126 add r2, r2, r0, asl #4
128 add r4, r1, r1, asl #2 /* r1 = Cr*101 */
129 add r4, r4, r1, asl #5
130 add r1, r4, r1, asl #6
132 add r1, r1, #256 /* r1 = rv = (r1 + 256) >> 9 */
134 rsb r2, r2, #128 /* r2 = guv = (-r2 + 128) >> 8 */
136 add r0, r0, #2 /* r0 = bu = (Cb*128 + 256) >> 9 */
138 stmia r8!, {r0-r2} /* store r0, r1 and r2 to chroma buffer */
140 /* 1st loop, first pixel */
141 ldrb r5, [r9], #1 /* r5 = *ysrc++ = *Y'_p++ */
142 sub r5, r5, #16 /* r5 = (Y'-16) * 74 */
143 add r3, r5, r5, asl #2
144 add r5, r3, r5, asl #5
146 add r6, r1, r5, asr #8 /* r6 = r = (Y >> 9) + rv */
147 add r3, r2, r5, asr #7 /* r3 = g = (Y >> 8) + guv */
148 add r4, r0, r5, asr #8 /* r4 = b = (Y >> 9) + bu */
150 orr r5, r6, r4 /* check if clamping is needed... */
151 orr r5, r5, r3, asr #1 /* ...at all */
153 bls 15f /* -> no clamp */
154 cmp r6, #31 /* clamp r */
155 mvnhi r6, r6, asr #31
157 cmp r3, #63 /* clamp g */
158 mvnhi r3, r3, asr #31
160 cmp r4, #31 /* clamp b */
161 mvnhi r4, r4, asr #31
165 /* calculate pixel_1 and save to r4 for later pixel packing */
166 orr r4, r4, r3, lsl #5 /* pixel_1 = r<<11 | g<<5 | b */
167 orr r4, r4, r6, lsl #11 /* r4 = pixel_1 */
169 /* 1st loop, second pixel */
170 ldrb r5, [r9], #1 /* r5 = *ysrc++ = *Y'_p++ */
171 sub r5, r5, #16 /* r5 = (Y'-16) * 74 */
172 add r3, r5, r5, asl #2
173 add r5, r3, r5, asl #5
175 add r6, r1, r5, asr #8 /* r6 = r = (Y >> 9) + rv */
176 add r3, r2, r5, asr #7 /* r3 = g = (Y >> 8) + guv */
177 add r5, r0, r5, asr #8 /* r5 = b = (Y >> 9) + bu */
179 orr r0, r6, r5 /* check if clamping is needed... */
180 orr r0, r0, r3, asr #1 /* ...at all */
182 bls 15f /* -> no clamp */
183 cmp r6, #31 /* clamp r */
184 mvnhi r6, r6, asr #31
186 cmp r3, #63 /* clamp g */
187 mvnhi r3, r3, asr #31
189 cmp r5, #31 /* clamp b */
190 mvnhi r5, r5, asr #31
194 /* calculate pixel_2 and pack with pixel_1 before writing */
195 orr r5, r5, r3, lsl #5 /* pixel_2 = r<<11 | g<<5 | b */
196 orr r5, r5, r6, lsl #11 /* r5 = pixel_2 */
197 #ifdef FORCE_FIFO_WAIT
198 /* wait for FIFO half full */
200 ldr r3, [lr, #-0x24] /* while (LCD_STATUS & 0x08); */
204 stmia lr, {r4,r5} /* write pixel_1 and pixel_2 */
206 subs r7, r7, #2 /* check for loop end */
207 bgt 10b /* back to beginning */
210 /* Reload several registers for pointer rewinding for next loop */
211 add r8, sp, #16 /* chroma buffer */
212 ldmia sp, { r1, r7, r9} /* r1 = LCD_BASE */
213 /* r7 = loop count */
214 /* r9 = &ysrc[stride] */
218 /* restore r0 (bu), r1 (rv) and r2 (guv) from chroma buffer */
221 /* 2nd loop, first pixel */
222 ldrb r5, [r9], #1 /* r5 = *ysrc++ = *Y'_p++ */
223 sub r5, r5, #16 /* r5 = (Y'-16) * 74 */
224 add r3, r5, r5, asl #2
225 add r5, r3, r5, asl #5
227 add r6, r1, r5, asr #8 /* r6 = r = (Y >> 9) + rv */
228 add r3, r2, r5, asr #7 /* r3 = g = (Y >> 8) + guv */
229 add r4, r0, r5, asr #8 /* r4 = b = (Y >> 9) + bu */
231 orr r5, r6, r4 /* check if clamping is needed... */
232 orr r5, r5, r3, asr #1 /* ...at all */
234 bls 15f /* -> no clamp */
235 cmp r6, #31 /* clamp r */
236 mvnhi r6, r6, asr #31
238 cmp r3, #63 /* clamp g */
239 mvnhi r3, r3, asr #31
241 cmp r4, #31 /* clamp b */
242 mvnhi r4, r4, asr #31
245 /* calculate pixel_1 and save to r4 for later pixel packing */
246 orr r4, r4, r3, lsl #5 /* pixel_1 = r<<11 | g<<5 | b */
247 orr r4, r4, r6, lsl #11 /* r4 = pixel_1 */
249 /* 2nd loop, second pixel */
250 ldrb r5, [r9], #1 /* r5 = *ysrc++ = *Y'_p++ */
251 sub r5, r5, #16 /* r5 = (Y'-16) * 74 */
252 add r3, r5, r5, asl #2
253 add r5, r3, r5, asl #5
255 add r6, r1, r5, asr #8 /* r6 = r = (Y >> 9) + rv */
256 add r3, r2, r5, asr #7 /* r3 = g = (Y >> 8) + guv */
257 add r5, r0, r5, asr #8 /* r5 = b = (Y >> 9) + bu */
259 orr r0, r6, r5 /* check if clamping is needed... */
260 orr r0, r0, r3, asr #1 /* ...at all */
262 bls 15f /* -> no clamp */
263 cmp r6, #31 /* clamp r */
264 mvnhi r6, r6, asr #31
266 cmp r3, #63 /* clamp g */
267 mvnhi r3, r3, asr #31
269 cmp r5, #31 /* clamp b */
270 mvnhi r5, r5, asr #31
274 /* calculate pixel_2 and pack with pixel_1 before writing */
275 orr r5, r5, r3, lsl #5 /* pixel_2 = r<<11 | g<<5 | b */
276 orr r5, r5, r6, lsl #11 /* r5 = pixel_2 */
277 #ifdef FORCE_FIFO_WAIT
278 /* wait for FIFO half full */
280 ldr r3, [lr, #-0x24] /* while (LCD_STATUS & 0x08); */
284 stmia lr, {r4,r5} /* write pixel_1 and pixel_2 */
286 subs r7, r7, #2 /* check for loop end */
287 bgt 20b /* back to beginning */
291 add sp, sp, r3 /* deallocate buffer */
292 ldmpc regs=r4-r10 /* restore registers */
295 .size lcd_write_yuv420_lines, .-lcd_write_yuv420_lines