megadrive_test2

changeset 9:6ecf2f3ff05a

- better pad input handling - switch between 28 and 30 vertical tiles by pressing C
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 Jun 2017 00:33:10 +0300
parents 403367d5df5a
children ca7108a82867
files Makefile src/intr.h src/io.h src/main.c src/pad.h src/vdp.h
diffstat 6 files changed, 81 insertions(+), 10 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Thu Jun 22 07:44:48 2017 +0300
     1.2 +++ b/Makefile	Sat Jun 24 00:33:10 2017 +0300
     1.3 @@ -2,6 +2,7 @@
     1.4  asrc = $(wildcard src/*.s)
     1.5  aSsrc = $(wildcard src/*.S)
     1.6  obj = $(asrc:.s=.o) $(aSsrc:.S=.o) $(csrc:.c=.o)
     1.7 +dep = $(csrc:.c=.d)
     1.8  
     1.9  name = test2
    1.10  elf = $(name).elf
    1.11 @@ -38,10 +39,19 @@
    1.12  tunnel.ppm:
    1.13  	tools/tunnel >$@
    1.14  
    1.15 +-include $(dep)
    1.16 +
    1.17 +%.d: %.c
    1.18 +	@$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@
    1.19 +
    1.20  .PHONY: clean
    1.21  clean:
    1.22  	rm -f $(obj) $(elf) $(bin) tun_data.h tunnel.ppm
    1.23  
    1.24 +.PHONY: cleandep
    1.25 +cleandep:
    1.26 +	rm -f $(dep)
    1.27 +
    1.28  .PHONY: run
    1.29  run: $(bin)
    1.30  	gens-sdl $<
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/intr.h	Sat Jun 24 00:33:10 2017 +0300
     2.3 @@ -0,0 +1,7 @@
     2.4 +#ifndef INTR_H_
     2.5 +#define INTR_H_
     2.6 +
     2.7 +void enable_intr(void);
     2.8 +void disable_intr(void);
     2.9 +
    2.10 +#endif	/* INTR_H_ */
     3.1 --- a/src/io.h	Thu Jun 22 07:44:48 2017 +0300
     3.2 +++ b/src/io.h	Sat Jun 24 00:33:10 2017 +0300
     3.3 @@ -58,7 +58,7 @@
     3.4  	bnstate = IO_REG_DATA(port);
     3.5  	IO_REG_DATA(port) = 0;		/* select mode 0 */
     3.6  	bnstate |= (IO_REG_DATA(port) << 2) & (IO_PAD_A | IO_PAD_START);
     3.7 -	return bnstate;
     3.8 +	return ~bnstate;
     3.9  }
    3.10  
    3.11  #endif	/* MEGADRIVE_IO_H_ */
     4.1 --- a/src/main.c	Thu Jun 22 07:44:48 2017 +0300
     4.2 +++ b/src/main.c	Sat Jun 24 00:33:10 2017 +0300
     4.3 @@ -1,6 +1,8 @@
     4.4  #include <stdint.h>
     4.5  #include "vdp.h"
     4.6  #include "io.h"
     4.7 +#include "pad.h"
     4.8 +#include "intr.h"
     4.9  #include "tun_data.h"
    4.10  
    4.11  #define NAMETAB_A	6
    4.12 @@ -25,8 +27,6 @@
    4.13  	VDP_PACK_RGB(7, 0, 3)		/* 15: fixed */
    4.14  };
    4.15  
    4.16 -static int running = 1;
    4.17 -
    4.18  
    4.19  int main(void)
    4.20  {
    4.21 @@ -42,7 +42,6 @@
    4.22  	for(i=0; i<16; i++) {
    4.23  		VDP_PORT_DATA = pal[i];
    4.24  	}
    4.25 -	vdp_set_bgcolor(0, 0);
    4.26  
    4.27  	for(i=0; i<tun_xtiles * tun_ytiles; i++) {
    4.28  		load_pattern(i, tun_tiles[i]);
    4.29 @@ -61,10 +60,12 @@
    4.30  	vdp_enable_vintr();
    4.31  
    4.32  	for(;;) {
    4.33 -		uint16_t bnstate = io_readpad(0);
    4.34 +		pad_update(0);
    4.35  
    4.36 -		if(bnstate & IO_PAD_START) {
    4.37 -			running = !running;
    4.38 +		if(pad_pressed(0, IO_PAD_C)) {
    4.39 +			disable_intr();
    4.40 +			vdp_setreg(VDP_REG_MODE2, vdp_getreg(VDP_REG_MODE2) ^ VDP_MODE2_V30CELL);
    4.41 +			enable_intr();
    4.42  		}
    4.43  
    4.44  		vdp_wait_vblank();
    4.45 @@ -101,8 +102,6 @@
    4.46  	int idx = CYCLE_BEG;
    4.47  	uint16_t first;
    4.48  
    4.49 -	if(!running) return;
    4.50 -
    4.51  	first = pal[idx];
    4.52  
    4.53  	vdp_begin_palette(0, idx);
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/pad.h	Sat Jun 24 00:33:10 2017 +0300
     5.3 @@ -0,0 +1,30 @@
     5.4 +#ifndef PAD_H_
     5.5 +#define PAD_H_
     5.6 +
     5.7 +#include "io.h"
     5.8 +
     5.9 +static uint16_t pad_bnstate[2], pad_prev_bnstate[2], pad_bndiff[2];
    5.10 +
    5.11 +static inline void pad_update(int pad)
    5.12 +{
    5.13 +	pad_prev_bnstate[pad] = pad_bnstate[pad];
    5.14 +	pad_bnstate[pad] = io_readpad(pad);
    5.15 +	pad_bndiff[pad] = pad_bnstate[pad] ^ pad_prev_bnstate[pad];
    5.16 +}
    5.17 +
    5.18 +static inline int pad_pressed(int pad, uint16_t mask)
    5.19 +{
    5.20 +	return (pad_bnstate[pad] & mask & pad_bndiff[pad]) != 0;
    5.21 +}
    5.22 +
    5.23 +static inline int pad_released(int pad, uint16_t mask)
    5.24 +{
    5.25 +	return (~pad_bnstate[pad] & mask & pad_bndiff[pad]) != 0;
    5.26 +}
    5.27 +
    5.28 +static inline uint16_t pad_getstate(int pad)
    5.29 +{
    5.30 +	return pad_bnstate[pad];
    5.31 +}
    5.32 +
    5.33 +#endif	/* PAD_H_ */
     6.1 --- a/src/vdp.h	Thu Jun 22 07:44:48 2017 +0300
     6.2 +++ b/src/vdp.h	Sat Jun 24 00:33:10 2017 +0300
     6.3 @@ -60,6 +60,20 @@
     6.4  	VDP_TILE_HIGH_PRIO	= 0x8000
     6.5  };
     6.6  
     6.7 +enum {
     6.8 +	VDP_STAT_PAL		= 0x0001,
     6.9 +	VDP_STAT_DMABUSY	= 0x0002,
    6.10 +	VDP_STAT_HBLANK		= 0x0004,
    6.11 +	VDP_STAT_VBLANK		= 0x0008,
    6.12 +	VDP_STAT_ODDFIELD	= 0x0010,
    6.13 +	VDP_STAT_SPRITE_COL	= 0x0020,
    6.14 +	VDP_STAT_SPRITE_OVF = 0x0040,
    6.15 +	VDP_STAT_VINTR		= 0x0080,
    6.16 +	VDP_STAT_FIFO_FULL	= 0x0100,
    6.17 +	VDP_STAT_FIFO_EMPTY	= 0x0200
    6.18 +};
    6.19 +
    6.20 +
    6.21  static inline void vdp_setup_access(uint16_t addr, int rw, int memid)
    6.22  {
    6.23  	uint32_t type;
    6.24 @@ -247,9 +261,20 @@
    6.25  	vdp_setreg(VDP_REG_SCROLL_TAB, addr >> 10);
    6.26  }
    6.27  
    6.28 +static inline void vdp_wait_vblank_start(void)
    6.29 +{
    6.30 +	while(!(VDP_PORT_STATUS & VDP_STAT_VBLANK));
    6.31 +}
    6.32 +
    6.33 +static inline void vdp_wait_vblank_end(void)
    6.34 +{
    6.35 +	while(VDP_PORT_STATUS & VDP_STAT_VBLANK);
    6.36 +}
    6.37 +
    6.38  static inline void vdp_wait_vblank(void)
    6.39  {
    6.40 -	while(!(VDP_PORT_STATUS & 0x0008));
    6.41 +	vdp_wait_vblank_start();
    6.42 +	vdp_wait_vblank_end();
    6.43  }
    6.44  
    6.45  void vdp_init(void);