megadrive_test2

annotate src/io.h @ 11:302bcd73edc4

added 6button support
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 Jun 2017 03:47:37 +0300
parents ca7108a82867
children
rev   line source
nuclear@6 1 #ifndef MEGADRIVE_IO_H_
nuclear@6 2 #define MEGADRIVE_IO_H_
nuclear@6 3
nuclear@7 4 #include <stdint.h>
nuclear@7 5
nuclear@7 6 #define IO_REG_VER *((volatile uint8_t*)0xa10001)
nuclear@7 7 #define IO_REG_DATA1 *((volatile uint8_t*)0xa10003)
nuclear@7 8 #define IO_REG_DATA2 *((volatile uint8_t*)0xa10005)
nuclear@7 9 #define IO_REG_DATA3 *((volatile uint8_t*)0xa10007)
nuclear@7 10 #define IO_REG_CTRL1 *((volatile uint8_t*)0xa10009)
nuclear@7 11 #define IO_REG_CTRL2 *((volatile uint8_t*)0xa1000b)
nuclear@7 12 #define IO_REG_CTRL3 *((volatile uint8_t*)0xa1000d)
nuclear@7 13 #define IO_REG_TXDATA1 *((volatile uint8_t*)0xa1000f)
nuclear@7 14 #define IO_REG_RXDATA1 *((volatile uint8_t*)0xa10011)
nuclear@7 15 #define IO_REG_S_CTRL1 *((volatile uint8_t*)0xa10013)
nuclear@7 16 #define IO_REG_TXDATA2 *((volatile uint8_t*)0xa10015)
nuclear@7 17 #define IO_REG_RXDATA2 *((volatile uint8_t*)0xa10017)
nuclear@7 18 #define IO_REG_S_CTRL2 *((volatile uint8_t*)0xa10013)
nuclear@7 19 #define IO_REG_TXDATA3 *((volatile uint8_t*)0xa1001b)
nuclear@7 20 #define IO_REG_RXDATA3 *((volatile uint8_t*)0xa1001d)
nuclear@7 21 #define IO_REG_S_CTRL3 *((volatile uint8_t*)0xa1001f)
nuclear@7 22 #define IO_REG_LOCK *((volatile uint8_t*)0xa14000)
nuclear@7 23 #define IO_REG_TMSS *((volatile uint8_t*)0xa14101)
nuclear@7 24
nuclear@8 25 #define IO_REG_DATA(x) *((volatile uint8_t*)0xa10003 + ((x) << 1))
nuclear@8 26 #define IO_REG_CTRL(x) *((volatile uint8_t*)0xa10009 + ((x) << 1))
nuclear@8 27
nuclear@6 28
nuclear@6 29 enum {
nuclear@6 30 IO_VER_VERSION_MASK = 0x0f,
nuclear@6 31 IO_VER_EXP = 0x20,
nuclear@6 32 IO_VER_PAL = 0x40,
nuclear@6 33 IO_VER_NONJP = 0x80
nuclear@6 34 };
nuclear@6 35
nuclear@8 36 enum {
nuclear@11 37 IO_PAD_UP = 0x001,
nuclear@11 38 IO_PAD_DOWN = 0x002,
nuclear@11 39 IO_PAD_LEFT = 0x004,
nuclear@11 40 IO_PAD_RIGHT = 0x008,
nuclear@11 41 IO_PAD_B = 0x010,
nuclear@11 42 IO_PAD_C = 0x020,
nuclear@11 43 IO_PAD_A = 0x040,
nuclear@11 44 IO_PAD_START = 0x080,
nuclear@11 45
nuclear@11 46 IO_PAD_Z = 0x100,
nuclear@11 47 IO_PAD_Y = 0x200,
nuclear@11 48 IO_PAD_X = 0x400
nuclear@8 49 };
nuclear@10 50 #define IO_PAD_LOW_MASK 0x3f
nuclear@10 51 #define IO_PAD_HIGH_MASK (IO_PAD_A | IO_PAD_START)
nuclear@8 52
nuclear@8 53 static inline void io_setdir(int port, uint8_t dir)
nuclear@8 54 {
nuclear@8 55 IO_REG_CTRL(port) = dir;
nuclear@8 56 }
nuclear@8 57
nuclear@8 58 static inline uint16_t io_readpad(int port)
nuclear@8 59 {
nuclear@8 60 uint16_t bnstate;
nuclear@8 61
nuclear@10 62 bnstate = ~(IO_REG_DATA(port) << 2) & IO_PAD_HIGH_MASK;
nuclear@10 63
nuclear@8 64 IO_REG_DATA(port) = 0x40; /* select mode 1 */
nuclear@10 65 asm volatile("nop");
nuclear@10 66 asm volatile("nop");
nuclear@10 67 bnstate |= ~IO_REG_DATA(port) & IO_PAD_LOW_MASK;
nuclear@10 68
nuclear@8 69 IO_REG_DATA(port) = 0; /* select mode 0 */
nuclear@10 70 return bnstate;
nuclear@8 71 }
nuclear@8 72
nuclear@11 73 static inline uint16_t io_readpad6(int port)
nuclear@11 74 {
nuclear@11 75 uint16_t bnstate;
nuclear@11 76
nuclear@11 77 IO_REG_DATA(port) = 0x40; /* start with mode 1 */
nuclear@11 78 asm volatile("nop");
nuclear@11 79 asm volatile("nop");
nuclear@11 80 bnstate = ~IO_REG_DATA(port) & IO_PAD_LOW_MASK;
nuclear@11 81
nuclear@11 82 IO_REG_DATA(port) = 0; /* select mode 0 */
nuclear@11 83 asm volatile("nop");
nuclear@11 84 asm volatile("nop");
nuclear@11 85 bnstate |= ~(IO_REG_DATA(port) << 2) & IO_PAD_HIGH_MASK;
nuclear@11 86
nuclear@11 87 /* then introduce two more rising edges to trigger extended mode */
nuclear@11 88 IO_REG_DATA(port) = 0x40;
nuclear@11 89 asm volatile("nop");
nuclear@11 90 asm volatile("nop");
nuclear@11 91 IO_REG_DATA(port) = 0;
nuclear@11 92 asm volatile("nop");
nuclear@11 93 asm volatile("nop");
nuclear@11 94 IO_REG_DATA(port) = 0x40;
nuclear@11 95 asm volatile("nop");
nuclear@11 96 asm volatile("nop");
nuclear@11 97
nuclear@11 98 bnstate |= (uint16_t)(~IO_REG_DATA(port) & 0x7) << 8;
nuclear@11 99 IO_REG_DATA(port) = 0; /* return mode to 0 */
nuclear@11 100 return bnstate;
nuclear@11 101 }
nuclear@11 102
nuclear@10 103 void io_init(void);
nuclear@10 104 /*uint16_t io_readpad(int port);*/
nuclear@10 105
nuclear@6 106 #endif /* MEGADRIVE_IO_H_ */