megadrive_test2

diff src/vdp.h @ 0:ce1b05082ac4

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 14 Mar 2017 05:59:33 +0200
parents
children 2560a8be8cb8
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/vdp.h	Tue Mar 14 05:59:33 2017 +0200
     1.3 @@ -0,0 +1,157 @@
     1.4 +#ifndef VDP_H_
     1.5 +#define VDP_H_
     1.6 +
     1.7 +#include <stdint.h>
     1.8 +
     1.9 +#define VDP_PORT_DATA		*((volatile uint16_t*)0xc00000)
    1.10 +#define VDP_PORT_DATA32		*((volatile uint32_t*)0xc00000)
    1.11 +#define VDP_PORT_CTL		*((volatile uint16_t*)0xc00004)
    1.12 +#define VDP_PORT_CTL32		*((volatile uint32_t*)0xc00004)
    1.13 +#define VDP_PORT_HVCNT		*((volatile uint16_t*)0xc00008)
    1.14 +#define VDP_PORT_PSG		*((volatile uint16_t*)0xc00010)
    1.15 +
    1.16 +#define VDP_PORT_STATUS		*((volatile uint16_t*)0xc00004)
    1.17 +
    1.18 +enum {
    1.19 +	VDP_REG_MODE1			= 0,
    1.20 +	VDP_REG_MODE2			= 1,
    1.21 +	VDP_REG_NAMETAB_A		= 2,
    1.22 +	VDP_REG_NAMETAB_WIN		= 3,
    1.23 +	VDP_REG_NAMETAB_B		= 4,
    1.24 +	VDP_REG_SPRITE_TAB		= 5,
    1.25 +	VDP_REG_BGCOL			= 7,
    1.26 +	VDP_REG_INTR			= 10,
    1.27 +	VDP_REG_MODE3			= 11,
    1.28 +	VDP_REG_MODE4			= 12,
    1.29 +	VDP_REG_SCROLL_TAB		= 13,
    1.30 +	VDP_REG_AUTOINC			= 15,
    1.31 +	VDP_REG_SCROLL_SIZE		= 16,
    1.32 +	VDP_REG_WIN_XPOS		= 17,
    1.33 +	VDP_REG_WIN_YPOS		= 18,
    1.34 +	VDP_REG_DMA_LEN_LOW		= 19,
    1.35 +	VDP_REG_DMA_LEN_HIGH	= 20,
    1.36 +	VDP_REG_DMA_SRC_LOW		= 21,
    1.37 +	VDP_REG_DMA_SRC_MID		= 22,
    1.38 +	VDP_REG_DMA_SRC_HIGH	= 23,
    1.39 +
    1.40 +	VDP_NUM_REGS
    1.41 +};
    1.42 +
    1.43 +uint16_t vdp_reg_shadow[VDP_NUM_REGS];
    1.44 +
    1.45 +/* access VDP memory */
    1.46 +enum { VDP_MEM_READ, VDP_MEM_WRITE };
    1.47 +enum {
    1.48 +	VDP_MEM_VRAM	= 0,
    1.49 +	VDP_MEM_CRAM	= 0xa,	/* CD5->CD0: 0 0 r 0 w 0 */
    1.50 +	VDP_MEM_VSRAM	= 4		/* CD5->CD0: 0 0 0 1 0 0 */
    1.51 +};
    1.52 +
    1.53 +static inline void vdp_setup_access(uint16_t addr, int rw, int memid)
    1.54 +{
    1.55 +	uint32_t type;
    1.56 +	if(rw == VDP_MEM_WRITE) {
    1.57 +		type = (memid & 7) | 1;
    1.58 +	} else {
    1.59 +		type = memid & 0xc;
    1.60 +	}
    1.61 +
    1.62 +	VDP_PORT_CTL32 = (((uint32_t)addr & 0x3fff) << 16) | ((addr >> 14) & 3) |
    1.63 +		((type << 2) & 0xf0) | (type << 30);
    1.64 +}
    1.65 +
    1.66 +
    1.67 +/* mode register 1 */
    1.68 +enum {
    1.69 +	VDP_MODE1_BASE	= 0x4,
    1.70 +	VDP_MODE1_HVCNT = 0x2,
    1.71 +	VDP_MODE1_HINTR = 0x10
    1.72 +};
    1.73 +
    1.74 +/* mode register 2 */
    1.75 +enum {
    1.76 +	VDP_MODE2_BASE		= 0x4,
    1.77 +	VDP_MODE2_V30CELL	= 0x8,
    1.78 +	VDP_MODE2_DMA		= 0x10,
    1.79 +	VDP_MODE2_VINTR		= 0x20,
    1.80 +	VDP_MODE2_DISP		= 0x40
    1.81 +};
    1.82 +
    1.83 +/* mode register 3 */
    1.84 +enum {
    1.85 +	VDP_MODE3_BASE			= 0,
    1.86 +	VDP_MODE3_HSCROLL_CELL	= 2,
    1.87 +	VDP_MODE3_HSCROLL_LINE	= 3,
    1.88 +	VDP_MODE3_VSCROLL_2CELL	= 4,
    1.89 +	VDP_MODE3_EXTINTR		= 8
    1.90 +};
    1.91 +
    1.92 +/* mode register 4 */
    1.93 +enum {
    1.94 +	VDP_MODE4_BASE			= 0,
    1.95 +	VDP_MODE4_H40CELL		= 0x81,
    1.96 +	VDP_MODE4_ILACE			= 2,
    1.97 +	VDP_MODE4_ILACE_2XRES	= 6,
    1.98 +	VDP_MODE4_SH			= 8	/* shadow/highlight enable */
    1.99 +};
   1.100 +
   1.101 +/* scroll size register */
   1.102 +enum {
   1.103 +	VDP_SCROLL_H32		= 0,
   1.104 +	VDP_SCROLL_H64		= 1,
   1.105 +	VDP_SCROLL_H128		= 3,
   1.106 +	VDP_SCROLL_V32		= 0,
   1.107 +	VDP_SCROLL_V64		= 0x10,
   1.108 +	VDP_SCROLL_V128		= 0x30
   1.109 +};
   1.110 +
   1.111 +/* window X/Y position register */
   1.112 +enum {
   1.113 +	VDP_WIN_LEFT	= 0,
   1.114 +	VDP_WIN_UP		= 0,
   1.115 +	VDP_WIN_RIGHT	= 0x80,
   1.116 +	VDP_WIN_DOWN	= 0x80,
   1.117 +	VDP_WIN_POSMASK	= 0x1f
   1.118 +};
   1.119 +
   1.120 +#define VDP_PACK_RGB(r, g, b)	\
   1.121 +	((((uint16_t)(r) & 7) << 1) | (((uint16_t)(g) & 7) << 5) | (((uint16_t)(b) & 7) << 9))
   1.122 +
   1.123 +
   1.124 +static inline void vdp_setreg(int reg, uint8_t value)
   1.125 +{
   1.126 +	/*vdp_reg_shadow[reg] = value;*/
   1.127 +	VDP_PORT_CTL = (uint16_t)value | (reg << 8) | (uint16_t)0x8000;
   1.128 +}
   1.129 +
   1.130 +static inline uint16_t vdp_getreg(int reg)
   1.131 +{
   1.132 +	return vdp_reg_shadow[reg];
   1.133 +}
   1.134 +
   1.135 +static inline uint16_t vdp_status(void)
   1.136 +{
   1.137 +	return VDP_PORT_CTL;
   1.138 +}
   1.139 +
   1.140 +static inline void vdp_set_bgcolor(int palidx, int colidx)
   1.141 +{
   1.142 +	vdp_setreg(VDP_REG_BGCOL, (colidx & 0xf) | (palidx << 4));
   1.143 +}
   1.144 +
   1.145 +static inline void vdp_set_autoinc(int stride)
   1.146 +{
   1.147 +	vdp_setreg(VDP_REG_AUTOINC, stride);
   1.148 +}
   1.149 +
   1.150 +static inline void vdp_set_pal_entry(int pidx, int cidx, int r, int g, int b)
   1.151 +{
   1.152 +	uint16_t paddr = (pidx << 5) | (cidx << 1);
   1.153 +
   1.154 +	vdp_setup_access(paddr, VDP_MEM_WRITE, VDP_MEM_CRAM);
   1.155 +	VDP_PORT_DATA = VDP_PACK_RGB(r, g, b);
   1.156 +}
   1.157 +
   1.158 +void vdp_init(void);
   1.159 +
   1.160 +#endif	/* VDP_H_ */