Compare commits
15 Commits
9b8db3343b
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| c7f395339f | |||
| 47236203cc | |||
| 740b4c2243 | |||
| 04c3c8c5c2 | |||
| f680f7c425 | |||
| 9be2aacab0 | |||
| d3d7da3d6b | |||
| e51a86a726 | |||
| 436b89d5bb | |||
| b0789a7bc8 | |||
| f18727a49c | |||
| 4b5ae0e516 | |||
| 5e1ba9d749 | |||
| 3f43fc65b2 | |||
| fdcb75c454 |
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;
|
||||
|
||||
// Чтение мыши с порта
|
||||
void read_mouse(MouseData* dst, volatile u8* port);
|
||||
|
||||
#endif
|
||||
BIN
res/fluttershy.png
Normal file
BIN
res/fluttershy.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 202 B |
BIN
res/pinkie_pie.png
Normal file
BIN
res/pinkie_pie.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 198 B |
9
res/resources.h
Normal file
9
res/resources.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <genesis.h>
|
||||
|
||||
#ifndef _RES_RESOURCES_H_
|
||||
#define _RES_RESOURCES_H_
|
||||
|
||||
extern const SpriteDefinition FLUTTERSHY;
|
||||
extern const SpriteDefinition PINKIE_PIE;
|
||||
|
||||
#endif // _RES_RESOURCES_H_
|
||||
2
res/resources.res
Normal file
2
res/resources.res
Normal file
@@ -0,0 +1,2 @@
|
||||
SPRITE FLUTTERSHY "fluttershy.png" 2 2 NONE 0
|
||||
SPRITE PINKIE_PIE "pinkie_pie.png" 2 2 NONE 0
|
||||
182
src/main.c
182
src/main.c
@@ -1,82 +1,134 @@
|
||||
#include "genesis.h"
|
||||
#include "mouse.h"
|
||||
#include "resources.h"
|
||||
|
||||
#define MOUSE_PORT ((volatile u8*)0xA10003)
|
||||
#define Z80_HALT *((volatile u16*)0xA11100)
|
||||
|
||||
#define MOUSE_DELAY 8
|
||||
|
||||
|
||||
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)
|
||||
|
||||
Sprite* ptr1;
|
||||
Sprite* ptr2;
|
||||
|
||||
int main() {
|
||||
VDP_setTextPlane(0);
|
||||
VDP_drawText("Sega Mega Mouse RAW reader", 7, 27);
|
||||
VDP_drawText("RAW nibbles:", 12,0);
|
||||
u8 nibbles[16] = {0};
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
VDP_setTextPlane(0);
|
||||
VDP_drawText("Sega Mega Mouse RAW reader", 7, 27);
|
||||
VDP_drawText("RAW nibbles:", 14,0);
|
||||
|
||||
char buf[40]; //буфер строки для вывода на экран
|
||||
SPR_init();
|
||||
PAL_setPalette(PAL1, FLUTTERSHY.palette->data, DMA);
|
||||
|
||||
s16 x1=0,y1=0, x2=0,y2=0;
|
||||
u8 frame = 0;
|
||||
char buf[40]; //буфер строки для вывода на экран
|
||||
MouseData m1, m2;
|
||||
|
||||
while(TRUE) {
|
||||
u8 code1[6] = {0};
|
||||
u8 code2[6] = {0};
|
||||
|
||||
SYS_doVBlankProcess();
|
||||
|
||||
//опрос мыши
|
||||
Z80_HALT = 0x0100;
|
||||
*MOUSE_PORT = 0x60; delay();
|
||||
|
||||
nibbles[0] = *MOUSE_PORT & 0x0F;
|
||||
u8 ptr_active1=0,ptr_active2=0;
|
||||
|
||||
|
||||
for(int i=1;i<10;i++) {
|
||||
*MOUSE_PORT = (i&1)?0x20:0x00;
|
||||
delay();
|
||||
nibbles[i] = *MOUSE_PORT & 0x0F;
|
||||
delay();
|
||||
}
|
||||
while(TRUE) {
|
||||
//считывание портов и вывод информации
|
||||
u8 btnFront1=m1.buttons, btnFront2=m2.buttons;
|
||||
read_mouse(&m1, PORT1);
|
||||
read_mouse(&m2, PORT2);
|
||||
|
||||
*MOUSE_PORT = 0x60; delay();
|
||||
Z80_HALT = 0x0000;
|
||||
//конец опроса мыши
|
||||
btnFront1^=m1.buttons;
|
||||
btnFront2^=m2.buttons;
|
||||
|
||||
// выводим нибблы на экран
|
||||
VDP_clearTextArea(0, 1, 40, 1);
|
||||
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_drawText(buf, 13,1);
|
||||
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);
|
||||
|
||||
//считаем dx dy
|
||||
int dx = (nibbles[6] << 4) | nibbles[7];
|
||||
int dy = (nibbles[8] << 4) | nibbles[9];
|
||||
if (nibbles[4]&8) dy = 255;
|
||||
if (nibbles[4]&4) dx = 255;
|
||||
if (nibbles[4]&2) dy = -dy;
|
||||
if (nibbles[4]&1) dx = -dx;
|
||||
x+=dx;
|
||||
y+=dy;
|
||||
if(m1.valid) {
|
||||
x1 += m1.dx; y1 += m1.dy;
|
||||
if(x1>304) x1=304; else if(x1<0) x1=0;
|
||||
if(y1>208) y1=208; else if(y1<0) y1=0;
|
||||
|
||||
//выводим на экран полезные данные
|
||||
VDP_clearTextArea(0, 10, 40, 3);
|
||||
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, 10);
|
||||
sprintf(buf, "dx: %+04d dy: %+04d x: %+04d y: %+04d", dx,dy,x/64,y/64);
|
||||
VDP_drawText(buf, 2, 12);
|
||||
if(!ptr_active1 && btnFront1) {
|
||||
u8* code = code1;
|
||||
for(int i=1;i<6;i++) code[i-1]=code[i];
|
||||
code[5]=btnFront1;
|
||||
if(code[0]==0x4&&code[1]==0x1&&code[2]==0x1&&code[3]==0x2&&code[4]==0x2&&code[5]==0x4){
|
||||
ptr_active1=1;
|
||||
ptr1 = SPR_addSprite(&FLUTTERSHY, 0, 0, TILE_ATTR(PAL1, TRUE, FALSE, TRUE));
|
||||
}
|
||||
}
|
||||
|
||||
if(ptr_active1) SPR_setPosition(ptr1, x1, 208-y1);
|
||||
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
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);
|
||||
|
||||
return 0;
|
||||
if(m2.valid) {
|
||||
x2 += m2.dx; y2 += m2.dy;
|
||||
if(x2>304) x2=304; else if(x2<0) x2=0;
|
||||
if(y2>208) y2=208; else if(y2<0) y2=0;
|
||||
|
||||
|
||||
if(!ptr_active2 && btnFront2) {
|
||||
u8* code = code2;
|
||||
for(int i=1;i<6;i++) code[i-1]=code[i];
|
||||
code[5]=btnFront2;
|
||||
if(code[0]==0x4&&code[1]==0x1&&code[2]==0x1&&code[3]==0x2&&code[4]==0x2&&code[5]==0x4){
|
||||
ptr_active2=1;
|
||||
ptr2 = SPR_addSprite(&PINKIE_PIE, 0, 0, TILE_ATTR(PAL1, TRUE, FALSE, FALSE));
|
||||
}
|
||||
}
|
||||
|
||||
if(ptr_active2) SPR_setPosition(ptr2, x2, 208-y2);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
//спрайты
|
||||
|
||||
SPR_update();
|
||||
|
||||
//счётчик кадров для вывода истории нибблов
|
||||
frame++;
|
||||
|
||||
//конец кадра
|
||||
|
||||
//Я не уверен почему, но SYS_doVBlankProcess в какой-то момент вызывал странное поведение. После добавления спрайтов, кажется, всё в порядке.
|
||||
|
||||
//VDP_waitVSync();
|
||||
SYS_doVBlankProcess();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
47
src/mouse.c
Normal file
47
src/mouse.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#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"
|
||||
);
|
||||
}
|
||||
|
||||
void read_mouse(MouseData* dst, volatile u8* port) {
|
||||
|
||||
dst->valid = 0;
|
||||
|
||||
Z80_HALT = 0x0100;
|
||||
*port = 0x60; delay();
|
||||
|
||||
dst->nibbles[0] = *port & 0x0F;
|
||||
for(int i=1; i<10; i++) {
|
||||
*port = (i&1) ? 0x20 : 0x00;
|
||||
delay();
|
||||
dst->nibbles[i] = *port & 0x0F;
|
||||
delay();
|
||||
}
|
||||
|
||||
*port = 0x60; delay();
|
||||
Z80_HALT = 0x0000;
|
||||
|
||||
// проверка сигнатуры 0BFF
|
||||
if (dst->nibbles[0]==0x0 && dst->nibbles[1]==0xB && dst->nibbles[2]==0xF && dst->nibbles[3]==0xF) {
|
||||
dst->valid = 1;
|
||||
dst->dx = (dst->nibbles[6] << 4) | dst->nibbles[7];
|
||||
dst->dy = (dst->nibbles[8] << 4) | dst->nibbles[9];
|
||||
if (dst->nibbles[4] & 8) dst->dy = 255;
|
||||
if (dst->nibbles[4] & 4) dst->dx = 255;
|
||||
if (dst->nibbles[4] & 2) dst->dy |= 0xFF00; // sign-extend
|
||||
if (dst->nibbles[4] & 1) dst->dx |= 0xFF00;
|
||||
|
||||
dst->buttons = dst->nibbles[5];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user