amiga_cyberspace

view src/mouse.c @ 0:e6fd57053627

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Jul 2017 08:17:34 +0300
parents
children
line source
1 #include <stdlib.h>
2 #include "mouse.h"
3 #include "inttypes.h"
6 #define REG_BASE_ADDR 0xdff000
7 #define REGNO_JOY0DAT 0x00a
8 #define REG_JOY0DAT *(volatile uint16_t*)(REG_BASE_ADDR | REGNO_JOY0DAT)
9 #define REG_CIAA_PORTA *(volatile uint8_t*)0xbfe001
11 #define CIAA_PA_FIR0 0x40
12 #define CIAA_PA_FIR1 0x80
14 static int xrng[2] = {0, 320};
15 static int yrng[2] = {0, 240};
17 void set_mouse_bounds(int x0, int y0, int x1, int y1)
18 {
19 xrng[0] = x0;
20 xrng[1] = x1;
21 yrng[0] = y0;
22 yrng[1] = y1;
23 }
25 int mouse_state(int *x, int *y)
26 {
27 mouse_pos(x, y);
28 return mouse_bnstate();
29 }
31 int mouse_bnstate(void)
32 {
33 /* TODO: handle right mouse button */
34 return (REG_CIAA_PORTA & CIAA_PA_FIR0) ? 0 : 1;
35 }
37 /* TODO: for when I feel like making a proper mouse handler
38 * use vblank interrupt vector to read the mouse register once
39 * and have mouse_pos (and friends) simply access the global data
40 */
41 void mouse_pos(int *x, int *y)
42 {
43 static int xpos, ypos;
44 static int prev_xcount, prev_ycount;
45 int xcount, ycount, dx, dy;
46 uint16_t raw = REG_JOY0DAT;
48 xcount = raw & 0xff;
49 ycount = raw >> 8;
51 dx = xcount - prev_xcount;
52 dy = ycount - prev_ycount;
54 if(abs(dx) > 127) dx = 255 - (xcount - prev_xcount);
55 if(abs(dy) > 127) dy = 255 - (ycount - prev_ycount);
56 prev_xcount = xcount;
57 prev_ycount = ycount;
59 xpos += dx;
60 ypos += dy;
62 if(xpos < xrng[0]) xpos = xrng[0];
63 if(ypos < yrng[0]) ypos = yrng[0];
64 if(xpos >= xrng[1]) xpos = xrng[1] - 1;
65 if(ypos >= yrng[1]) ypos = yrng[1] - 1;
67 *x = xpos;
68 *y = ypos;
69 }