2 * Copyright (C) 2011, 2012 glevand <geoffrey.levand@mail.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
37 #include <sys/ioctl.h>
40 #include <sys/consio.h>
44 #include "ps3gpu_ctl.h"
45 #include "ps3gpu_mth.h"
48 static const uint64_t cursor_bitmap
[64] = {
117 update_cursor(int fd
, int context_id
, int head
, unsigned long offset
,
118 int x
, int y
, int enable
)
120 struct ps3gpu_ctl_cursor_enable cursor_enable
;
121 struct ps3gpu_ctl_cursor_set_image cursor_set_image
;
122 struct ps3gpu_ctl_cursor_set_position cursor_set_position
;
127 cursor_enable
.context_id
= context_id
;
128 cursor_enable
.head
= head
;
129 cursor_enable
.enable
= 0;
131 err
= ioctl(fd
, PS3GPU_CTL_CURSOR_ENABLE
, &cursor_enable
);
137 /* Set cursor image */
139 cursor_set_image
.context_id
= context_id
;
140 cursor_set_image
.head
= head
;
141 cursor_set_image
.offset
= offset
;
143 err
= ioctl(fd
, PS3GPU_CTL_CURSOR_SET_IMAGE
, &cursor_set_image
);
149 /* Set cursor position */
151 cursor_set_position
.context_id
= context_id
;
152 cursor_set_position
.head
= head
;
153 cursor_set_position
.x
= x
;
154 cursor_set_position
.y
= y
;
156 err
= ioctl(fd
, PS3GPU_CTL_CURSOR_SET_POSITION
, &cursor_set_position
);
164 cursor_enable
.context_id
= context_id
;
165 cursor_enable
.head
= head
;
166 cursor_enable
.enable
= enable
;
168 err
= ioctl(fd
, PS3GPU_CTL_CURSOR_ENABLE
, &cursor_enable
);
178 main(int argc
, char **argv
)
180 struct ps3gpu_ctl_context_allocate context_allocate
;
181 struct ps3gpu_ctl_context_free context_free
;
183 volatile uint32_t *control
;
184 uint32_t *vram
, *cursor
;
185 unsigned long vram_handle
, cursor_handle
;
186 unsigned int vram_gaddr
, cursor_gaddr
;
191 /* Open GPU device */
193 fd
= open(PS3GPU_DEV_PATH
, O_RDWR
);
199 /* Create GPU context */
201 context_allocate
.vram_size
= 64; /* MB */
203 err
= ioctl(fd
, PS3GPU_CTL_CONTEXT_ALLOCATE
, &context_allocate
);
209 context_id
= context_allocate
.context_id
;
211 printf("context id %d\n", context_id
);
212 printf("control handle 0x%lx size %d\n",
213 context_allocate
.control_handle
, context_allocate
.control_size
);
215 /* Map control registers */
217 control
= mmap(NULL
, context_allocate
.control_size
,
218 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, context_allocate
.control_handle
);
219 if (control
== (void *) MAP_FAILED
) {
226 err
= memory_allocate(fd
, context_id
, PS3GPU_CTL_MEMORY_TYPE_VIDEO
,
227 ROUNDUP(DISPLAY_HEIGHT
* DISPLAY_PITCH
, 4 * 1024), 12,
228 &vram_handle
, &vram_gaddr
, (void **) &vram
);
230 perror("memory_allocate");
234 printf("VRAM handle 0x%lx gpu addr 0x%08x\n",
235 vram_handle
, vram_gaddr
);
237 memset32(vram
, 0xff404040, DISPLAY_HEIGHT
* DISPLAY_WIDTH
);
239 /* Allocate cursor */
241 err
= memory_allocate(fd
, context_id
, PS3GPU_CTL_MEMORY_TYPE_VIDEO
,
242 16 * 1024, 20, &cursor_handle
, &cursor_gaddr
, (void **) &cursor
);
244 perror("memory_allocate");
248 printf("cursor handle 0x%lx gpu addr 0x%08x\n",
249 cursor_handle
, cursor_gaddr
);
251 /* Create cursor image */
253 for (y
= 0; y
< 64; y
++) {
254 for (x
= 0; x
< 64; x
++)
255 if (cursor_bitmap
[(y
* 64 + x
) >> 6] &
256 (1ul << ((63 - (y
* 64 + x
)) & 0x3f)))
257 cursor
[y
* 64 + x
] = 0xffffff00;
259 cursor
[y
* 64 + x
] = 0x00000000;
264 err
= update_cursor(fd
, context_id
, PS3GPU_CTL_HEAD_A
,
265 cursor_handle
, 100, 100, 1);
269 err
= update_cursor(fd
, context_id
, PS3GPU_CTL_HEAD_B
,
270 cursor_handle
, 100, 100, 1);
276 err
= flip(fd
, context_id
, PS3GPU_CTL_HEAD_A
, vram_handle
);
282 err
= flip(fd
, context_id
, PS3GPU_CTL_HEAD_B
, vram_handle
);
292 err
= update_cursor(fd
, context_id
, PS3GPU_CTL_HEAD_A
,
293 cursor_handle
, 300, 300, 1);
295 fprintf(stderr
, "could not update cursor\n");
299 err
= update_cursor(fd
, context_id
, PS3GPU_CTL_HEAD_B
,
300 cursor_handle
, 300, 300, 1);
302 fprintf(stderr
, "could not update cursor\n");
308 /* Destroy GPU context */
310 context_free
.context_id
= context_id
;
312 err
= ioctl(fd
, PS3GPU_CTL_CONTEXT_FREE
, &context_free
);
323 /* Restore console */
325 ioctl(0, SW_TEXT_80x25
, NULL
);