started implementing some GPU abstractions and moved test to C
[psx_test1] / src / gpu.c
1 #include "gpu.h"
2 #include "psxregs.h"
3
4 void gpu_reset(void)
5 {
6         REG_GP1 = 0;    /* GPCMD(GP1_RESET) */
7 }
8
9 void gpu_setmode(int xsz, int ysz, int bpp, int rate)
10 {
11         unsigned int x1, y0, y1;
12         unsigned int mode = (ysz >= 480) ? DISP_V480 | DISP_ILACE : 0;
13
14         switch(xsz) {
15         case 320:
16                 mode |= DISP_H320;
17                 break;
18         case 368:
19                 mode |= DISP_H368;
20                 break;
21         case 512:
22                 mode |= DISP_H512;
23                 break;
24         case 640:
25                 mode |= DISP_H640;
26                 break;
27         default:
28                 break;
29         }
30
31         if(bpp >= 24) mode |= DISP_24BPP;
32         if(rate == 50) mode |= DISP_PAL;
33
34         REG_GP1 = GPCMD(GP1_DISPMODE) | mode;
35
36         /* also set display ranges */
37         x1 = 0x260 + 320 * 8;
38         REG_GP1 = GPCMD(GP1_HDISP) | 0x260 | (x1 << 12);
39         if(mode & DISP_PAL) {
40                 y0 = 0xa3 - 264 / 2;
41                 y1 = 0xa3 + 264 / 2;
42         } else {
43                 y0 = 0x88 - 224 / 2;
44                 y1 = 0x88 + 224 / 2;
45         }
46         REG_GP1 = GPCMD(GP1_VDISP) | y0 | (y1 << 10);
47 }
48
49 void gpu_display(int en)
50 {
51         REG_GP1 = GPCMD(GP1_DISPEN) | (en ? 0 : 1);
52 }
53
54 void gpu_cliprect(int x, int y, int w, int h)
55 {
56         REG_GP0 = GPCMD(GP0_CLIP_TL) | x | (y << 10);
57         REG_GP0 = GPCMD(GP0_CLIP_BR) | (w - 1) | ((h - 1) << 10);
58 }
59
60 void gpu_origin(int x, int y)
61 {
62         REG_GP0 = GPCMD(GP0_ORIGIN) | x | (y << 11);
63 }
64
65 /* ---- drawing ---- */
66
67 void gpu_fillrect(int x, int y, int w, int h, unsigned int col)
68 {
69         REG_GP0 = GPCMD(GP0_FILLRECT) | col;
70         REG_GP0 = x | (y << 16);
71         REG_GP0 = w | (h << 16);
72 }
73
74 void gpu_draw_flat(int cmd, uint32_t color, struct gpu_vert *data, int vcount)
75 {
76         REG_GP0 = GPCMD(cmd) | color;
77         while(vcount-- > 0) {
78                 REG_GP0 = *(uint32_t*)data++;
79         }
80 }
81
82 void gpu_draw_gouraud(int cmd, struct gpu_gvert *data, int vcount)
83 {
84         REG_GP0 = GPCMD(cmd) | data->color;
85         REG_GP0 = *(uint32_t*)&data->x;
86         data++;
87
88         while(--vcount > 0) {
89                 REG_GP0 = data->color;
90                 REG_GP0 = *(uint32_t*)&data->x;
91                 data++;
92         }
93 }