megadrive_test2

annotate src/main.c @ 10:ca7108a82867

fixed gamepad input on real megadrive
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 Jun 2017 02:33:52 +0300
parents 6ecf2f3ff05a
children
rev   line source
nuclear@1 1 #include <stdint.h>
nuclear@0 2 #include "vdp.h"
nuclear@6 3 #include "io.h"
nuclear@9 4 #include "pad.h"
nuclear@9 5 #include "intr.h"
nuclear@10 6 #include "debug.h"
nuclear@5 7 #include "tun_data.h"
nuclear@0 8
nuclear@5 9 #define NAMETAB_A 6
nuclear@5 10 #define NAMETAB_B 6
nuclear@5 11
nuclear@5 12 void load_pattern(int idx, void *data);
nuclear@5 13 void set_tile(int nametab_idx, int x, int y, int tile_idx, int palidx);
nuclear@5 14
nuclear@5 15 #define CYCLE_BEG 1
nuclear@5 16 #define CYCLE_END 14
nuclear@5 17 static uint16_t pal[16] = {
nuclear@5 18 VDP_PACK_RGB(0, 0, 0), /* 0: fixed */
nuclear@5 19 VDP_PACK_RGB(0, 0, 0), /* 1: cycle start */
nuclear@5 20 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
nuclear@5 21 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
nuclear@5 22 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
nuclear@5 23 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
nuclear@5 24 VDP_PACK_RGB(0, 0, 0), VDP_PACK_RGB(0, 0, 0),
nuclear@5 25 VDP_PACK_RGB(0, 0, 0), /* 12: \ */
nuclear@5 26 VDP_PACK_RGB(7, 0, 3), /* 13: > beam */
nuclear@5 27 VDP_PACK_RGB(0, 0, 0), /* 14: / cycle end */
nuclear@5 28 VDP_PACK_RGB(7, 0, 3) /* 15: fixed */
nuclear@4 29 };
nuclear@4 30
nuclear@10 31 static uint16_t dbg;
nuclear@10 32
nuclear@4 33
nuclear@0 34 int main(void)
nuclear@0 35 {
nuclear@4 36 int i, j;
nuclear@4 37
nuclear@0 38 vdp_init();
nuclear@10 39 io_init();
nuclear@10 40 dbg_init();
nuclear@0 41
nuclear@6 42 if(IO_REG_VER & IO_VER_PAL) {
nuclear@6 43 vdp_setreg(VDP_REG_MODE2, vdp_getreg(VDP_REG_MODE2) | VDP_MODE2_V30CELL);
nuclear@6 44 }
nuclear@5 45
nuclear@5 46 vdp_begin_palette(0, 0);
nuclear@5 47 for(i=0; i<16; i++) {
nuclear@5 48 VDP_PORT_DATA = pal[i];
nuclear@5 49 }
nuclear@0 50
nuclear@5 51 for(i=0; i<tun_xtiles * tun_ytiles; i++) {
nuclear@5 52 load_pattern(i, tun_tiles[i]);
nuclear@5 53 }
nuclear@4 54
nuclear@5 55 vdp_set_nametab_idx(VDP_PLANE_A, NAMETAB_A);
nuclear@5 56 vdp_set_nametab_idx(VDP_PLANE_B, NAMETAB_B);
nuclear@4 57
nuclear@5 58 for(i=0; i<tun_ytiles; i++) {
nuclear@5 59 for(j=0; j<tun_xtiles; j++) {
nuclear@5 60 set_tile(NAMETAB_A, j, i, i * tun_xtiles + j, 0);
nuclear@4 61 }
nuclear@4 62 }
nuclear@4 63
nuclear@4 64 //vdp_enable_hintr(12);
nuclear@5 65 vdp_enable_vintr();
nuclear@1 66
nuclear@8 67 for(;;) {
nuclear@9 68 pad_update(0);
nuclear@8 69
nuclear@9 70 if(pad_pressed(0, IO_PAD_C)) {
nuclear@9 71 disable_intr();
nuclear@9 72 vdp_setreg(VDP_REG_MODE2, vdp_getreg(VDP_REG_MODE2) ^ VDP_MODE2_V30CELL);
nuclear@10 73 vdp_set_bgcolor(DBG_PALIDX, ++dbg & 1);
nuclear@9 74 enable_intr();
nuclear@8 75 }
nuclear@8 76
nuclear@8 77 vdp_wait_vblank();
nuclear@8 78 }
nuclear@0 79
nuclear@0 80 return 0;
nuclear@0 81 }
nuclear@1 82
nuclear@4 83 void load_pattern(int idx, void *data)
nuclear@4 84 {
nuclear@4 85 int i;
nuclear@4 86 uint32_t *ptr = data;
nuclear@4 87 uint16_t addr = idx << 5;
nuclear@4 88 vdp_setup_access(addr, VDP_MEM_WRITE, VDP_MEM_VRAM);
nuclear@4 89
nuclear@4 90 for(i=0; i<16; i++) {
nuclear@4 91 VDP_PORT_DATA32 = *ptr++;
nuclear@4 92 }
nuclear@4 93 }
nuclear@4 94
nuclear@5 95 void set_tile(int nametab_idx, int x, int y, int tile_idx, int palidx)
nuclear@4 96 {
nuclear@4 97 uint16_t tile_ent, addr;
nuclear@4 98
nuclear@4 99 tile_ent = vdp_nametab_entry(tile_idx, palidx, VDP_TILE_LOW_PRIO);
nuclear@4 100
nuclear@5 101 addr = vdp_nametab_addr(nametab_idx) + (y * 64 + x) * 2;
nuclear@4 102 vdp_setup_access(addr, VDP_MEM_WRITE, VDP_MEM_VRAM);
nuclear@4 103 VDP_PORT_DATA = tile_ent;
nuclear@4 104 }
nuclear@5 105
nuclear@5 106 void vblank_handler(void)
nuclear@5 107 {
nuclear@5 108 int idx = CYCLE_BEG;
nuclear@8 109 uint16_t first;
nuclear@8 110
nuclear@8 111 first = pal[idx];
nuclear@5 112
nuclear@5 113 vdp_begin_palette(0, idx);
nuclear@5 114 while(idx < CYCLE_END) {
nuclear@5 115 pal[idx] = pal[idx + 1];
nuclear@5 116 VDP_PORT_DATA = pal[idx];
nuclear@5 117 ++idx;
nuclear@5 118 }
nuclear@5 119 pal[idx] = first;
nuclear@5 120 VDP_PORT_DATA = pal[idx];
nuclear@5 121 }