shithub: riscv

Download patch

ref: 8061f30e559569943324a666ee2fcc74048785cd
parent: bf0d5c8abbe9d4e3a145df29bee8ef2758d01884
author: ftrvxmtrx <devnull@localhost>
date: Sat May 31 21:21:00 EDT 2014

games/nes: support Battle City two players mode with joypads

--- a/sys/src/games/nes/dat.h
+++ b/sys/src/games/nes/dat.h
@@ -4,7 +4,7 @@
 extern uchar mem[32768], ppuram[16384], oam[256];
 extern u16int pput, ppuv;
 extern u8int ppusx, vrambuf;
-extern int mirr, ppux, ppuy, odd, vramlatch, keylatch;
+extern int mirr, ppux, ppuy, odd, vramlatch, keylatch, keylatch2;
 
 extern int map, scale, mmc3hack, oflag;
 extern uchar *prg, *chr;
@@ -13,7 +13,7 @@
 extern u8int apuseq, apuctr[13];
 extern u16int dmcaddr, dmccnt;
 
-extern int keys, clock, ppuclock, apuclock, dmcclock, dmcfreq, saveclock, paused;
+extern int keys, keys2, clock, ppuclock, apuclock, dmcclock, dmcfreq, saveclock, paused;
 
 extern void (*mapper[])(int, u8int);
 
--- /dev/null
+++ b/sys/src/games/nes/joynes
@@ -1,0 +1,33 @@
+#!/bin/rc
+# run as "nusb/joy epX.Y | joynes N", where N=[1,2]
+
+awk -safe -v 'joy='$1 '
+	BEGIN { k[0] = "" }
+
+	/^axis 0 0/   { k[0] = "left " }
+	/^axis 0 255/ { k[0] = "right " }
+	/^axis 0 128/ { k[0] = "" }
+
+	/^axis 1 0/   { k[1] = "up " }
+	/^axis 1 255/ { k[1] = "down " }
+	/^axis 1 128/ { k[1] = "" }
+
+	/^down 7$/ { k[3] = "b " }
+	/^down 8$/ { k[2] = "a " }
+	/^down 1$/ { k[4] = "control " }
+	/^down 2$/ { k[5] = "start " }
+
+	/^up 7$/ { k[3] = "" }
+	/^up 8$/ { k[2] = "" }
+	/^up 1$/ { k[4] = "" }
+	/^up 2$/ { k[5] = "" }
+
+	{
+		if(joy != "")
+			printf "joy%s ", joy
+		for(i = 0; i <= 5; i++)
+			printf k[i]
+		printf "\n"
+		fflush
+	}
+'
--- a/sys/src/games/nes/mem.c
+++ b/sys/src/games/nes/mem.c
@@ -11,7 +11,7 @@
 uchar *prgb[16], *chrb[16];
 u16int pput, ppuv;
 u8int ppusx, vrambuf;
-int vramlatch = 1, keylatch = 0xFF;
+int vramlatch = 1, keylatch = 0xFF, keylatch2 = 0xFF;
 int prgsh, chrsh, mmc3hack;
 
 static void
@@ -395,7 +395,11 @@
 			keylatch = (keylatch >> 1) | 0x80;
 			return v | 0x40;
 		case 0x4017:
-			return 0x40;
+			if((mem[p] & 1) != 0)
+				return keys2 & 1;
+			v = keylatch2 & 1;
+			keylatch2 = (keylatch2 >> 1) | 0x80;
+			return v | 0x40;
 		}
 	}
 	if(p >= 0x8000){
@@ -489,8 +493,10 @@
 			irq &= ~IRQDMC;
 			break;
 		case 0x4016:
-			if((mem[p] & 1) != 0 && (v & 1) == 0)
+			if((mem[p] & 1) != 0 && (v & 1) == 0){
 				keylatch = keys;
+				keylatch2 = keys2;
+			}
 			break;
 		case APUFRAME:
 			apuseq = 0;
--- a/sys/src/games/nes/nes.c
+++ b/sys/src/games/nes/nes.c
@@ -15,7 +15,7 @@
 Image *tmp, *bg;
 int clock, ppuclock, apuclock, dmcclock, dmcfreq, sampclock, msgclock, saveclock;
 Mousectl *mc;
-int keys, paused, savereq, loadreq, oflag, savefd = -1;
+int keys, keys2, paused, savereq, loadreq, oflag, savefd = -1;
 int mirr;
 QLock pauselock;
 
@@ -125,6 +125,51 @@
 extern int trace;
 
 void
+joyproc(void *)
+{
+	char *s, *down[9];
+	static char buf[64];
+	int n, k, j;
+
+	j = 1;
+	for(;;){
+		n = read(0, buf, sizeof(buf) - 1);
+		if(n <= 0)
+			sysfatal("read: %r");
+		buf[n] = 0;
+		n = getfields(buf, down, nelem(down), 1, " ");
+		k = 0;
+		for(n--; n >= 0; n--){
+			s = down[n];
+			if(strcmp(s, "joy1") == 0)
+				j = 1;
+			else if(strcmp(s, "joy2") == 0)
+				j = 2;
+			else if(strcmp(s, "a") == 0)
+				k |= 1<<0;
+			else if(strcmp(s, "b") == 0)
+				k |= 1<<1;
+			else if(strcmp(s, "control") == 0)
+				k |= 1<<2;
+			else if(strcmp(s, "start") == 0)
+				k |= 1<<3;
+			else if(strcmp(s, "up") == 0)
+				k |= 1<<4;
+			else if(strcmp(s, "down") == 0)
+				k |= 1<<5;
+			else if(strcmp(s, "left") == 0)
+				k |= 1<<6;
+			else if(strcmp(s, "right") == 0)
+				k |= 1<<7;
+		}
+		if(j == 2)
+			keys2 = k;
+		else
+			keys = k;
+	}
+}
+
+void
 keyproc(void *)
 {
 	int fd, k;
@@ -220,6 +265,7 @@
 	mc = initmouse(nil, screen);
 	if(mc == nil)
 		sysfatal("initmouse: %r");
+	proccreate(joyproc, nil, 8192);
 	proccreate(keyproc, nil, 8192);
 	originwindow(screen, Pt(0, 0), screen->r.min);
 	p = divpt(addpt(screen->r.min, screen->r.max), 2);
--- a/sys/src/games/nes/state.c
+++ b/sys/src/games/nes/state.c
@@ -89,6 +89,7 @@
 	odd = get8();
 	vramlatch = get8();
 	keylatch = get8();
+	keylatch2 = get8();
 	vrambuf = get8();
 	clock = get32();
 	ppuclock = get32();
@@ -130,6 +131,7 @@
 	put8(odd);
 	put8(vramlatch);
 	put8(keylatch);
+	put8(keylatch2);
 	put8(vrambuf);
 	put32(clock);
 	put32(ppuclock);
--