nds_test2

view src/main.c @ 1:d625ba001a62

more stuff in ds3
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Jan 2018 03:48:05 +0200
parents abcaf667f2bd
children dd8c9847bae9
line source
1 #include <stdint.h>
2 #include <math.h>
3 #include "dsregs.h"
4 #include "ds3.h"
6 static void xorpat(void *addr, int xsz, int ysz);
8 static void *vram = VRAM_LCDC_PTR;
9 static uint16_t *bgmem = VRAM_BGB_PTR;
11 #define SIN_TAB_SZ 256
12 static int32_t sintab[SIN_TAB_SZ];
13 static int32_t costab[SIN_TAB_SZ];
15 int main(void)
16 {
17 int i;
18 uint32_t frame;
19 int32_t m[16] = {
20 0x10000, 0, 0, 0,
21 0, 0x10000, 0, 0,
22 0, 0, 0x10000, 0,
23 0, 0, 0, 0x10000
24 };
26 REG_POWCNT1 = POWCNT1_LCD | POWCNT1_3DREND | POWCNT1_3DGEOM | POWCNT1_2DA | POWCNT1_2DB | POWCNT1_DSWAP;
28 REG_DISPCNT = DISPCNT_MODE(1) | DISPCNT_BG0 | DISPCNT_BG0_3D;
29 REG_B_DISPCNT = DISPCNT_MODE(1) | DISPCNT_BG2 | 5;
31 REG_B_BG2CNT = BGXCNT_BM_256X256 | BGXCNT_BM16 | BGXCNT_OVF_WRAP;
32 REG_B_BG2PA = 0x100;
33 REG_B_BG2PB = 0;
34 REG_B_BG2PC = 0;
35 REG_B_BG2PD = 0x100;
37 REG_VRAMCNT_A = VRAM_ENABLE;
38 REG_VRAMCNT_C = VRAM_ENABLE | 4;
40 xorpat(bgmem, 256, 256);
42 for(i=0; i<SIN_TAB_SZ; i++) {
43 float theta = (float)i * M_PI * 2.0 / (float)SIN_TAB_SZ;
44 float s = sin(theta);
45 float c = cos(theta);
46 sintab[i] = (int32_t)(s * 65536.0);
47 costab[i] = (int32_t)(c * 65536.0);
48 }
50 ds3_clear_color(RGB15(4, 4, 4), 31);
51 ds3_clear_depth(0x7fff);
52 ds3_viewport(0, 0, 256, 192);
54 ds3_enable(DS3_POLYGON_SMOOTH);
56 REG_POLYGON_ATTR = 0x001f00c0; /* alpha = 31, cull none */
58 ds3_matrix_mode(DS3_PROJECTION);
59 ds3_load_identity();
60 ds3_scale(49152, 65536, 65536);
62 for(;;) {
63 int idx = frame & 0xff;
64 int32_t scale = (sintab[(frame >> 1) & 0xff] >> 9) + 204;
65 int32_t sa = ((sintab[idx] >> 8) * scale) >> 8;
66 int32_t ca = ((costab[idx] >> 8) * scale) >> 8;
68 int32_t x = ca * -128 + sa * -96 + (128 << 8);
69 int32_t y = -sa * -128 + ca * -96 + (96 << 8);
71 m[0] = ca << 8; m[1] = sa << 8;
72 m[4] = -sa << 8; m[5] = ca << 8;
74 ds3_matrix_mode(DS3_MODELVIEW);
75 ds3_load_matrix(m);
77 ds3_begin(DS3_QUADS);
78 ds3_color(RGB15(31, 0, 0));
79 ds3_vertex3(-0x8000, -0x8000, 0);
80 ds3_color(RGB15(0, 31, 0));
81 ds3_vertex3(0x8000, -0x8000, 0);
82 ds3_color(RGB15(0, 0, 31));
83 ds3_vertex3(0x8000, 0x8000, 0);
84 ds3_color(RGB15(31, 0, 31));
85 ds3_vertex3(-0x8000, 0x8000, 0);
86 ds3_end();
88 ds3_swap_buffers();
89 while(REG_VCOUNT < 192);
91 REG_B_BG2PA = ca;
92 REG_B_BG2PB = sa;
93 REG_B_BG2PC = -sa;
94 REG_B_BG2PD = ca;
95 REG_B_BG2X = x;
96 REG_B_BG2Y = y;
98 ++frame;
99 }
100 return 0;
101 }
103 static void xorpat(void *addr, int xsz, int ysz)
104 {
105 int i, j;
106 uint16_t *p = addr;
108 for(i=0; i<ysz; i++) {
109 for(j=0; j<xsz; j++) {
110 int xor = i^j;
111 uint16_t red = xor >> 2;
112 uint16_t green = xor >> 1;
113 uint16_t blue = xor;
114 *p++ = 0x8000 | red | ((green & 0x1f) << 5) | ((blue & 0x1f) << 10);
115 }
116 }
117 }