megadrive_test2

view src/main.c @ 6:df2c6b3c6f2e

added NTSC/PAL detection
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 21 Jun 2017 06:32:10 +0300
parents ea70f3da150f
children 403367d5df5a
line source
1 #include <stdint.h>
2 #include "vdp.h"
3 #include "io.h"
4 #include "tun_data.h"
6 #define NAMETAB_A 6
7 #define NAMETAB_B 6
9 void load_pattern(int idx, void *data);
10 void set_tile(int nametab_idx, int x, int y, int tile_idx, int palidx);
12 #define CYCLE_BEG 1
13 #define CYCLE_END 14
14 static uint16_t pal[16] = {
15 VDP_PACK_RGB(0, 0, 0), /* 0: fixed */
16 VDP_PACK_RGB(0, 0, 0), /* 1: cycle start */
17 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
18 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
19 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
20 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
21 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
22 VDP_PACK_RGB(0, 0, 0), /* 12: \ */
23 VDP_PACK_RGB(7, 0, 3), /* 13: > beam */
24 VDP_PACK_RGB(0, 0, 0), /* 14: / cycle end */
25 VDP_PACK_RGB(7, 0, 3) /* 15: fixed */
26 };
29 int main(void)
30 {
31 int i, j;
33 vdp_init();
35 if(IO_REG_VER & IO_VER_PAL) {
36 vdp_setreg(VDP_REG_MODE2, vdp_getreg(VDP_REG_MODE2) | VDP_MODE2_V30CELL);
37 }
39 vdp_begin_palette(0, 0);
40 for(i=0; i<16; i++) {
41 VDP_PORT_DATA = pal[i];
42 }
43 vdp_set_bgcolor(0, 0);
45 for(i=0; i<tun_xtiles * tun_ytiles; i++) {
46 load_pattern(i, tun_tiles[i]);
47 }
49 vdp_set_nametab_idx(VDP_PLANE_A, NAMETAB_A);
50 vdp_set_nametab_idx(VDP_PLANE_B, NAMETAB_B);
52 for(i=0; i<tun_ytiles; i++) {
53 for(j=0; j<tun_xtiles; j++) {
54 set_tile(NAMETAB_A, j, i, i * tun_xtiles + j, 0);
55 }
56 }
58 //vdp_enable_hintr(12);
59 vdp_enable_vintr();
61 for(;;);
63 return 0;
64 }
66 void load_pattern(int idx, void *data)
67 {
68 int i;
69 uint32_t *ptr = data;
70 uint16_t addr = idx << 5;
71 vdp_setup_access(addr, VDP_MEM_WRITE, VDP_MEM_VRAM);
73 for(i=0; i<16; i++) {
74 VDP_PORT_DATA32 = *ptr++;
75 }
76 }
78 void set_tile(int nametab_idx, int x, int y, int tile_idx, int palidx)
79 {
80 uint16_t tile_ent, addr;
82 tile_ent = vdp_nametab_entry(tile_idx, palidx, VDP_TILE_LOW_PRIO);
84 addr = vdp_nametab_addr(nametab_idx) + (y * 64 + x) * 2;
85 vdp_setup_access(addr, VDP_MEM_WRITE, VDP_MEM_VRAM);
86 VDP_PORT_DATA = tile_ent;
87 }
89 void vblank_handler(void)
90 {
91 int idx = CYCLE_BEG;
92 uint16_t first = pal[idx];
94 vdp_begin_palette(0, idx);
95 while(idx < CYCLE_END) {
96 pal[idx] = pal[idx + 1];
97 VDP_PORT_DATA = pal[idx];
98 ++idx;
99 }
100 pal[idx] = first;
101 VDP_PORT_DATA = pal[idx];
102 }