megadrive_test2

changeset 4:72ab63f262bf

tiles
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 19 Jun 2017 08:02:51 +0300
parents b22bc95f0cc0
children ea70f3da150f
files src/intr.s src/main.c src/startup.s src/vdp.h tools/Makefile tools/ppm2md.c tools/tunnel.c
diffstat 7 files changed, 291 insertions(+), 14 deletions(-) [+]
line diff
     1.1 --- a/src/intr.s	Tue Mar 14 09:11:18 2017 +0200
     1.2 +++ b/src/intr.s	Mon Jun 19 08:02:51 2017 +0300
     1.3 @@ -1,4 +1,4 @@
     1.4 -| vi:filetype=asm68k:
     1.5 +| vi:filetype=gas68k:
     1.6  | the following will go into the .vect section which will be placed at the very
     1.7  | begining of the binary at address 0 by the linker (see lnkscript).
     1.8  	.section .vect,"a"
     1.9 @@ -92,15 +92,15 @@
    1.10  	.extern palval
    1.11  
    1.12  intr_hblank:
    1.13 -	move.l #0xc0020000, VDP_PORT_CTL
    1.14 -
    1.15 -	move.w testcol, %d0
    1.16 -	move.w %d0, VDP_PORT_DATA
    1.17 -	rol.b #4, %d0
    1.18 -	move.w %d0, testcol
    1.19 -
    1.20 +|	move.l #0xc0020000, VDP_PORT_CTL
    1.21 +|
    1.22 +|	move.w testcol, %d0
    1.23 +|	move.w %d0, VDP_PORT_DATA
    1.24 +|	rol.b #4, %d0
    1.25 +|	move.w %d0, testcol
    1.26 +|
    1.27  	rte
    1.28  
    1.29  intr_vblank:
    1.30 -	jsr vblank_handler
    1.31 +	|jsr vblank_handler
    1.32  	rte
     2.1 --- a/src/main.c	Tue Mar 14 09:11:18 2017 +0200
     2.2 +++ b/src/main.c	Mon Jun 19 08:02:51 2017 +0300
     2.3 @@ -1,25 +1,100 @@
     2.4  #include <stdint.h>
     2.5  #include "vdp.h"
     2.6  
     2.7 +uint32_t pat0[] = {
     2.8 +	0x00000000,
     2.9 +	0x00000000,
    2.10 +	0x00000000,
    2.11 +	0x00010000,
    2.12 +	0x00000000,
    2.13 +	0x00000000,
    2.14 +	0x00000000,
    2.15 +	0x00000000
    2.16 +};
    2.17 +
    2.18 +uint32_t pat1[] = {
    2.19 +	0x11111111,
    2.20 +	0x11000011,
    2.21 +	0x10100101,
    2.22 +	0x10011001,
    2.23 +	0x10011001,
    2.24 +	0x10100101,
    2.25 +	0x11000011,
    2.26 +	0x11111111
    2.27 +};
    2.28 +
    2.29 +uint32_t pat2[] = {
    2.30 +	0x11111111,
    2.31 +	0x10011001,
    2.32 +	0x10011001,
    2.33 +	0x11111111,
    2.34 +	0x11111111,
    2.35 +	0x10011001,
    2.36 +	0x10011001,
    2.37 +	0x11111111
    2.38 +};
    2.39 +
    2.40 +void load_pattern(int idx, void *data);
    2.41 +void set_tile(int x, int y, int tile_idx, int palidx);
    2.42 +
    2.43  int main(void)
    2.44  {
    2.45 +	int i, j;
    2.46 +
    2.47  	vdp_init();
    2.48  
    2.49  	vdp_set_pal_entry(0, 0, 0, 0, 0);
    2.50 -	vdp_set_pal_entry(0, 1, 7, 0, 3);
    2.51 -	vdp_set_bgcolor(0, 1);
    2.52 +	vdp_set_pal_entry(0, 1, 7, 1, 3);
    2.53 +	vdp_set_bgcolor(0, 0);
    2.54  
    2.55 -	vdp_enable_hintr(12);
    2.56 -	vdp_enable_vintr();
    2.57 +	load_pattern(0, pat0);
    2.58 +	load_pattern(1, pat1);
    2.59 +	load_pattern(2, pat2);
    2.60 +
    2.61 +	vdp_set_nametab_idx(VDP_PLANE_A, 6);
    2.62 +
    2.63 +	for(i=0; i<20; i++) {
    2.64 +		for(j=0; j<20; j++) {
    2.65 +			set_tile(j, i, 1, 0);
    2.66 +		}
    2.67 +	}
    2.68 +
    2.69 +	//vdp_enable_hintr(12);
    2.70 +	//vdp_enable_vintr();
    2.71  
    2.72  	for(;;);
    2.73  
    2.74  	return 0;
    2.75  }
    2.76  
    2.77 +/*
    2.78  uint16_t testcol = 0x00c0;
    2.79  
    2.80  void vblank_handler(void)
    2.81  {
    2.82  	testcol = 0x00c0;
    2.83  }
    2.84 +*/
    2.85 +
    2.86 +void load_pattern(int idx, void *data)
    2.87 +{
    2.88 +	int i;
    2.89 +	uint32_t *ptr = data;
    2.90 +	uint16_t addr = idx << 5;
    2.91 +	vdp_setup_access(addr, VDP_MEM_WRITE, VDP_MEM_VRAM);
    2.92 +
    2.93 +	for(i=0; i<16; i++) {
    2.94 +		VDP_PORT_DATA32 = *ptr++;
    2.95 +	}
    2.96 +}
    2.97 +
    2.98 +void set_tile(int x, int y, int tile_idx, int palidx)
    2.99 +{
   2.100 +	uint16_t tile_ent, addr;
   2.101 +
   2.102 +	tile_ent = vdp_nametab_entry(tile_idx, palidx, VDP_TILE_LOW_PRIO);
   2.103 +
   2.104 +	addr = vdp_nametab_addr(6) + (y * 64 + x) * 2;
   2.105 +	vdp_setup_access(addr, VDP_MEM_WRITE, VDP_MEM_VRAM);
   2.106 +	VDP_PORT_DATA = tile_ent;
   2.107 +}
     3.1 --- a/src/startup.s	Tue Mar 14 09:11:18 2017 +0200
     3.2 +++ b/src/startup.s	Mon Jun 19 08:02:51 2017 +0300
     3.3 @@ -1,4 +1,4 @@
     3.4 -| vi:filetype=asm68k:
     3.5 +| vi:filetype=gas68k:
     3.6  	.text
     3.7  	.extern main
     3.8  
     4.1 --- a/src/vdp.h	Tue Mar 14 09:11:18 2017 +0200
     4.2 +++ b/src/vdp.h	Mon Jun 19 08:02:51 2017 +0300
     4.3 @@ -47,6 +47,19 @@
     4.4  	VDP_MEM_VSRAM	= 4		/* CD5->CD0: 0 0 0 1 0 0 */
     4.5  };
     4.6  
     4.7 +enum {
     4.8 +	VDP_PLANE_A,
     4.9 +	VDP_PLANE_B,
    4.10 +	VDP_PLANE_WIN
    4.11 +};
    4.12 +
    4.13 +enum {
    4.14 +	VDP_TILE_LOW_PRIO	= 0,
    4.15 +	VDP_TILE_HFLIP		= 0x0800,
    4.16 +	VDP_TILE_VFLIP		= 0x1000,
    4.17 +	VDP_TILE_HIGH_PRIO	= 0x8000
    4.18 +};
    4.19 +
    4.20  static inline void vdp_setup_access(uint16_t addr, int rw, int memid)
    4.21  {
    4.22  	uint32_t type;
    4.23 @@ -173,6 +186,41 @@
    4.24  	vdp_setreg(VDP_REG_MODE1, vdp_getreg(VDP_REG_MODE1) & ~VDP_MODE1_HINTR);
    4.25  }
    4.26  
    4.27 +static inline void vdp_set_nametab_addr(int plane, uint16_t addr)
    4.28 +{
    4.29 +	int reg;
    4.30 +	switch(plane) {
    4.31 +	case VDP_PLANE_A:
    4.32 +		reg = VDP_REG_NAMETAB_A;
    4.33 +		break;
    4.34 +	case VDP_PLANE_B:
    4.35 +		reg = VDP_REG_NAMETAB_B;
    4.36 +		break;
    4.37 +	case VDP_PLANE_WIN:
    4.38 +		reg = VDP_REG_NAMETAB_WIN;
    4.39 +		break;
    4.40 +	default:
    4.41 +		return;
    4.42 +	}
    4.43 +
    4.44 +	vdp_setreg(reg, (addr >> 10) & 0x38);
    4.45 +}
    4.46 +
    4.47 +static inline uint16_t vdp_nametab_addr(int idx)
    4.48 +{
    4.49 +	return (uint16_t)idx << 13;
    4.50 +}
    4.51 +
    4.52 +static inline void vdp_set_nametab_idx(int plane, int idx)
    4.53 +{
    4.54 +	vdp_set_nametab_addr(plane, vdp_nametab_addr(idx));
    4.55 +}
    4.56 +
    4.57 +static inline uint16_t vdp_nametab_entry(int tileidx, int palidx, uint16_t flags)
    4.58 +{
    4.59 +	return tileidx | (((uint16_t)palidx & 0x3) << 13) | flags;
    4.60 +}
    4.61 +
    4.62  void vdp_init(void);
    4.63  
    4.64  #endif	/* VDP_H_ */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/tools/Makefile	Mon Jun 19 08:02:51 2017 +0300
     5.3 @@ -0,0 +1,12 @@
     5.4 +CFLAGS = -pedantic -Wall -g
     5.5 +LDFLAGS = -lm
     5.6 +
     5.7 +.PHONY: all
     5.8 +all: tunnel ppm2md
     5.9 +
    5.10 +tunnel: tunnel.o
    5.11 +ppm2md: ppm2md.o
    5.12 +
    5.13 +.PHONY: clean
    5.14 +clean:
    5.15 +	rm -f *.o tunnel
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/tools/ppm2md.c	Mon Jun 19 08:02:51 2017 +0300
     6.3 @@ -0,0 +1,97 @@
     6.4 +#include <stdio.h>
     6.5 +#include <stdlib.h>
     6.6 +#include <string.h>
     6.7 +#include <ctype.h>
     6.8 +
     6.9 +unsigned char *read_image(int *xsz, int *ysz, int *maxval);
    6.10 +
    6.11 +int main(void)
    6.12 +{
    6.13 +	int i, j, width, height, maxval;
    6.14 +	unsigned char *pixels;
    6.15 +
    6.16 +	if(!(pixels = read_image(&width, &height, &maxval))) {
    6.17 +		return 1;
    6.18 +	}
    6.19 +	fprintf(stderr, "read image %dx%d (maxval: %d)\n", width, height, maxval);
    6.20 +
    6.21 +	/* reduce colors */
    6.22 +
    6.23 +	/* slice tiles */
    6.24 +	for(i=0; i<height / 8; i++) {
    6.25 +		for(j=0; j<width / 8; j++) {
    6.26 +		}
    6.27 +	}
    6.28 +
    6.29 +	return 0;
    6.30 +}
    6.31 +
    6.32 +char *clean_line(char *s)
    6.33 +{
    6.34 +	char *tmp;
    6.35 +
    6.36 +	while(*s && isspace(*s)) {
    6.37 +		++s;
    6.38 +	}
    6.39 +
    6.40 +	tmp = strchr(s, '#');
    6.41 +	if(tmp) *tmp = 0;
    6.42 +	tmp = strchr(s, '\n');
    6.43 +	if(tmp) *tmp = 0;
    6.44 +	tmp = strchr(s, '\r');
    6.45 +	if(tmp) *tmp = 0;
    6.46 +
    6.47 +	return s;
    6.48 +}
    6.49 +
    6.50 +unsigned char *read_image(int *width, int *height, int *maxval)
    6.51 +{
    6.52 +	char buf[256];
    6.53 +	int i, xsz, ysz, hdrline = 0;
    6.54 +	unsigned char *pixels;
    6.55 +
    6.56 +	while(hdrline < 3 && fgets(buf, sizeof buf, stdin)) {
    6.57 +		char *line = clean_line(buf);
    6.58 +		if(!*line) continue;
    6.59 +
    6.60 +		switch(hdrline) {
    6.61 +		case 0:
    6.62 +			if(strcmp(line, "P6") != 0) {
    6.63 +				goto inval;
    6.64 +			}
    6.65 +			break;
    6.66 +
    6.67 +		case 1:
    6.68 +			if(sscanf(line, "%d %d", &xsz, &ysz) != 2) {
    6.69 +				goto inval;
    6.70 +			}
    6.71 +			break;
    6.72 +
    6.73 +		case 2:
    6.74 +			if(sscanf(line, "%d", maxval) != 1) {
    6.75 +				goto inval;
    6.76 +			}
    6.77 +			break;
    6.78 +		}
    6.79 +		++hdrline;
    6.80 +	}
    6.81 +
    6.82 +	if(!(pixels = malloc(xsz * ysz * 3))) {
    6.83 +		fprintf(stderr, "failed to allocate image: %dx%d\n", xsz, ysz);
    6.84 +		return 0;
    6.85 +	}
    6.86 +
    6.87 +	for(i=0; i<xsz * ysz * 3; i++) {
    6.88 +		int c = getchar();
    6.89 +		if(c == -1) goto inval;
    6.90 +		pixels[i] = c;
    6.91 +	}
    6.92 +
    6.93 +	*width = xsz;
    6.94 +	*height = ysz;
    6.95 +	return pixels;
    6.96 +
    6.97 +inval:
    6.98 +	fprintf(stderr, "invalid input\n");
    6.99 +	return 0;
   6.100 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/tools/tunnel.c	Mon Jun 19 08:02:51 2017 +0300
     7.3 @@ -0,0 +1,45 @@
     7.4 +#include <stdio.h>
     7.5 +#include <stdlib.h>
     7.6 +#include <math.h>
     7.7 +
     7.8 +#define XSZ		512
     7.9 +#define YSZ		512
    7.10 +
    7.11 +int main(void)
    7.12 +{
    7.13 +	int i, j;
    7.14 +	unsigned char *pixels, *pptr;
    7.15 +
    7.16 +	pixels = malloc(XSZ * YSZ);
    7.17 +	pptr = pixels;
    7.18 +
    7.19 +	for(i=0; i<YSZ; i++) {
    7.20 +		float y = 2.0 * (float)i / (float)YSZ - 1.0;
    7.21 +		for(j=0; j<XSZ; j++) {
    7.22 +			float x = 2.0 * (float)j / (float)XSZ - 1.0;
    7.23 +			float tu = atan2(y, x) / M_PI * 0.5 + 0.5;
    7.24 +			float d = sqrt(x * x + y * y);
    7.25 +			float tv = d == 0.0 ? 0.0 : 1.0 / d;
    7.26 +
    7.27 +			int ty = (int)(tv * 64.0) & 0xf;
    7.28 +			int tx = (int)(tu * 256.0) & 0xf;
    7.29 +
    7.30 +			*pptr++ = tx ? ty : 0;
    7.31 +		}
    7.32 +	}
    7.33 +
    7.34 +	pptr = pixels;
    7.35 +	printf("P6\n%d %d\n15\n", XSZ, YSZ);
    7.36 +
    7.37 +	for(i=0; i<YSZ; i++) {
    7.38 +		for(j=0; j<XSZ; j++) {
    7.39 +			unsigned char c = *pptr++;
    7.40 +			putchar(c);
    7.41 +			putchar(c);
    7.42 +			putchar(c);
    7.43 +		}
    7.44 +	}
    7.45 +	fflush(stdout);
    7.46 +
    7.47 +	return 0;
    7.48 +}