dev #2
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -1 +1,2 @@ | ||||
| out/ | ||||
| *.bin | ||||
|  | ||||
							
								
								
									
										17
									
								
								inc/mouse.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								inc/mouse.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,17 @@ | ||||
| #ifndef MOUSE_H | ||||
| #define MOUSE_H | ||||
| 
 | ||||
| #include "genesis.h" | ||||
| 
 | ||||
| typedef struct { | ||||
|     u8  valid;       // =1 если мышь (сигнатура совпала)
 | ||||
|     u8  buttons; | ||||
|     s16 dx; | ||||
|     s16 dy; | ||||
|     u8  nibbles[10]; // для вывода RAW
 | ||||
| } MouseData; | ||||
| 
 | ||||
| // Чтение мыши с порта
 | ||||
| MouseData read_mouse(volatile u8* port); | ||||
| 
 | ||||
| #endif | ||||
							
								
								
									
										112
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										112
									
								
								src/main.c
									
									
									
									
									
								
							| @ -1,77 +1,75 @@ | ||||
| #include "genesis.h" | ||||
| #include "mouse.h" | ||||
| 
 | ||||
| #define MOUSE_PORT ((volatile u8*)0xA10003) | ||||
| #define Z80_HALT  *((volatile u16*)0xA11100) | ||||
| 
 | ||||
| #define MOUSE_DELAY 16 | ||||
| 
 | ||||
| static inline void delay(void) { | ||||
| 	__asm__ volatile ( | ||||
| 		"move.w %0,%%d0\n\t" | ||||
| 		"1: subq.w #1,%%d0\n\t" | ||||
| 		"bne.s 1b" | ||||
| 		: | ||||
| 		: "i"(MOUSE_DELAY) | ||||
| 		: "d0" | ||||
| 	); | ||||
| } | ||||
| #define PORT1 ((volatile u8*)0xA10003) | ||||
| #define PORT2 ((volatile u8*)0xA10005) | ||||
| 
 | ||||
| int main() { | ||||
| 	VDP_setTextPlane(0); | ||||
| 	VDP_drawText("Sega Mega Mouse RAW reader", 7, 27); | ||||
| 	VDP_drawText("RAW nibbles:", 14,0); | ||||
| 	u8 nibbles[16] = {0}; | ||||
| 	s16 x = 0; | ||||
| 	s16 y = 0; | ||||
| 
 | ||||
| 	 | ||||
| 	s16 x1=0,y1=0, x2=0,y2=0; | ||||
| 	u8 frame = 0; | ||||
| 
 | ||||
| 	char buf[40]; //буфер строки для вывода на экран
 | ||||
| 
 | ||||
| 	while(TRUE) {	 | ||||
| 		//опрос мыши
 | ||||
| 		Z80_HALT = 0x0100; | ||||
| 		*MOUSE_PORT = 0x60; delay(); | ||||
| 	while(TRUE) { | ||||
| 		MouseData m1 = read_mouse(PORT1); | ||||
| 		sprintf(buf, "+%X%X%X%X %X %X %X%X %X%X",  | ||||
| 				m1.nibbles[0], m1.nibbles[1], m1.nibbles[2], m1.nibbles[3], | ||||
| 				m1.nibbles[4], m1.nibbles[5], m1.nibbles[6], m1.nibbles[7], | ||||
| 				m1.nibbles[8], m1.nibbles[9]); | ||||
| 		VDP_clearTextArea(1,2,1,20); | ||||
| 		VDP_drawText(buf, 1, 2 + frame%20); | ||||
| 
 | ||||
| 		nibbles[0] = *MOUSE_PORT & 0x0F; | ||||
| 		if(m1.valid) { | ||||
| 			x1 += m1.dx; y1 += m1.dy; | ||||
| 			if(x1>9999) x1=-9999; else if(x1<-9999) x1=9999; | ||||
| 			if(y1>9999) y1=-9999; else if(y1<-9999) y1=9999; | ||||
| 
 | ||||
| 			sprintf(buf, "buttons: %c%c%c%c", | ||||
| 					((m1.buttons>>3)&1)+'0', | ||||
| 					((m1.buttons>>2)&1)+'0', | ||||
| 					((m1.buttons>>1)&1)+'0', | ||||
| 					((m1.buttons>>0)&1)+'0'); | ||||
| 			VDP_drawText(buf, 2, 23); | ||||
| 
 | ||||
| 		for(int i=1;i<10;i++) { | ||||
| 			*MOUSE_PORT = (i&1)?0x20:0x00; | ||||
| 			delay(); | ||||
| 			nibbles[i] = *MOUSE_PORT & 0x0F; | ||||
| 			delay(); | ||||
| 			sprintf(buf, "PORT1: dx:%+04d dy:%+04d x:%+05d y:%+05d", m1.dx,m1.dy,x1,y1); | ||||
| 			VDP_drawText(buf, 1, 24); | ||||
| 		} else { | ||||
| 			VDP_clearTextArea(2,23,13,1); | ||||
| 			sprintf(buf, "PORT1:            Mouse not recognized"); | ||||
| 			VDP_drawText(buf, 1, 24); | ||||
| 		} | ||||
| 		 | ||||
| 		MouseData m2 = read_mouse(PORT2); | ||||
| 		sprintf(buf, "%X%X%X%X %X %X %X%X %X%X+",  | ||||
| 				m2.nibbles[0], m2.nibbles[1], m2.nibbles[2], m2.nibbles[3], | ||||
| 				m2.nibbles[4], m2.nibbles[5], m2.nibbles[6], m2.nibbles[7], | ||||
| 				m2.nibbles[8], m2.nibbles[9]); | ||||
| 		VDP_clearTextArea(38,2,1,20); | ||||
| 		VDP_drawText(buf, 24, 2 + frame%20); | ||||
| 
 | ||||
| 		*MOUSE_PORT = 0x60; delay(); | ||||
| 		Z80_HALT = 0x0000; | ||||
| 		//конец опроса мыши
 | ||||
| 		if(m2.valid) { | ||||
| 			x2 += m2.dx; y2 += m2.dy; | ||||
| 			if(x2>9999) x2=-9999; else if(x2<-9999) x2=9999; | ||||
| 			if(y2>9999) y2=-9999; else if(y2<-9999) y2=9999; | ||||
| 
 | ||||
| 		//выводим нибблы на экран
 | ||||
| 		sprintf(buf, "+%X%X%X%X %X %X %X%X %X%X", nibbles[0],nibbles[1],nibbles[2],nibbles[3],nibbles[4],nibbles[5],nibbles[6],nibbles[7],nibbles[8],nibbles[9]); | ||||
| 		VDP_clearTextArea(12,1,1,22); | ||||
| 		VDP_drawText(buf, 12,1+frame%22); | ||||
| 
 | ||||
| 		//считаем dx dy 
 | ||||
| 		s16 dx = (nibbles[6] << 4) | nibbles[7]; | ||||
| 		s16 dy = (nibbles[8] << 4) | nibbles[9]; | ||||
| 		if (nibbles[4]&8) dy = 255; | ||||
| 		if (nibbles[4]&4) dx = 255; | ||||
| 		if (nibbles[4]&2) dy = dy|0xFF00; //sign-extend to 16-bit
 | ||||
| 		if (nibbles[4]&1) dx = dx|0xFF00; | ||||
| 		x+=dx; | ||||
| 		y+=dy; | ||||
| 
 | ||||
| 		//ограничиваем x и y
 | ||||
| 		x = (x > 9999) ? -9999 : (x < -9999 ? 9999 : x); | ||||
| 		y = (y > 9999) ? -9999 : (y < -9999 ? 9999 : y); | ||||
| 
 | ||||
| 		//выводим на экран полезные данные
 | ||||
| 		sprintf(buf, "buttons: %c%c%c%c", (nibbles[5]&8&&1)+'0',(nibbles[5]&4&&1)+'0',(nibbles[5]&2&&1)+'0',(nibbles[5]&1&&1)+'0'); | ||||
| 		VDP_drawText(buf, 2, 24); | ||||
| 		sprintf(buf, "dx: %+04d dy: %+04d x: %+05d y: %+05d", dx,dy,x,y); | ||||
| 		VDP_drawText(buf, 2, 25); | ||||
| 			sprintf(buf, "buttons: %c%c%c%c", | ||||
| 					((m2.buttons>>3)&1)+'0', | ||||
| 					((m2.buttons>>2)&1)+'0', | ||||
| 					((m2.buttons>>1)&1)+'0', | ||||
| 					((m2.buttons>>0)&1)+'0'); | ||||
| 			VDP_drawText(buf, 25, 23); | ||||
| 
 | ||||
| 			sprintf(buf, "PORT2: dx:%+04d dy:%+04d x:%+05d y:%+05d", m2.dx,m2.dy,x2,y2); | ||||
| 			VDP_drawText(buf, 1, 25); | ||||
| 		} else { | ||||
| 			sprintf(buf, "PORT2:            Mouse not recognized"); | ||||
| 			VDP_clearTextArea(25,23,13,1); | ||||
| 			VDP_drawText(buf, 1, 25); | ||||
| 		} | ||||
| 		 | ||||
| 		//счётчик кадров для вывода истории нибблов
 | ||||
| 		frame++; | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										54
									
								
								src/mouse.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/mouse.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,54 @@ | ||||
| #include "mouse.h" | ||||
| 
 | ||||
| #define MOUSE_DELAY 16 | ||||
| #define Z80_HALT *((volatile u16*)0xA11100) | ||||
| 
 | ||||
| static inline void delay(void) { | ||||
|     __asm__ volatile ( | ||||
|         "move.w %0,%%d0\n\t" | ||||
|         "1: subq.w #1,%%d0\n\t" | ||||
|         "bne.s 1b" | ||||
|         : | ||||
|         : "i"(MOUSE_DELAY) | ||||
|         : "d0" | ||||
|     ); | ||||
| } | ||||
| 
 | ||||
| MouseData read_mouse(volatile u8* port) { | ||||
|     u8 nibbles[16]; | ||||
|     MouseData md = {0}; | ||||
| 
 | ||||
|     Z80_HALT = 0x0100; | ||||
|     *port = 0x60; delay(); | ||||
| 
 | ||||
|     nibbles[0] = *port & 0x0F; | ||||
|     for(int i=1; i<10; i++) { | ||||
|         *port = (i&1) ? 0x20 : 0x00; | ||||
|         delay(); | ||||
|         nibbles[i] = *port & 0x0F; | ||||
|         delay(); | ||||
|     } | ||||
| 
 | ||||
|     *port = 0x60; delay(); | ||||
|     Z80_HALT = 0x0000; | ||||
| 
 | ||||
|     // копируем нибблы для вывода RAW
 | ||||
|     for(int i=0;i<10;i++) md.nibbles[i] = nibbles[i]; | ||||
| 
 | ||||
|     // проверка сигнатуры 0BFF
 | ||||
|     if (nibbles[0]==0x0 && nibbles[1]==0xB && nibbles[2]==0xF && nibbles[3]==0xF) { | ||||
|         md.valid = 1; | ||||
|         s16 dx = (nibbles[6] << 4) | nibbles[7]; | ||||
|         s16 dy = (nibbles[8] << 4) | nibbles[9]; | ||||
|         if (nibbles[4] & 8) dy = 255; | ||||
|         if (nibbles[4] & 4) dx = 255; | ||||
|         if (nibbles[4] & 2) dy |= 0xFF00; // sign-extend
 | ||||
|         if (nibbles[4] & 1) dx |= 0xFF00; | ||||
| 
 | ||||
|         md.dx = dx; | ||||
|         md.dy = dy; | ||||
|         md.buttons = nibbles[5]; | ||||
|     } | ||||
| 
 | ||||
|     return md; | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user