nds_test2

annotate src/main.c @ 0:abcaf667f2bd

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