Skip to content

Commit 62c4b22

Browse files
basic powered ps2 mouse driver
1 parent 7f64793 commit 62c4b22

6 files changed

Lines changed: 225 additions & 69 deletions

File tree

include/basic/sockets.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,8 @@ void udpbind_statement(struct basic_ctx* ctx);
9898

9999
void udpunbind_statement(struct basic_ctx* ctx);
100100

101-
char* basic_udpread(struct basic_ctx* ctx);
101+
char* basic_udpread(struct basic_ctx* ctx);
102+
103+
int64_t basic_udplastsourceport(struct basic_ctx* ctx);
104+
105+
char* basic_udplastip(struct basic_ctx* ctx);
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
MOUSE_UDP_PORT = 14501
2+
3+
UDPBIND "127.0.0.1", MOUSE_UDP_PORT
4+
5+
done = 0
6+
spins = 0
7+
WHILE spins < 64 AND done = 0
8+
s = INPORT(&64)
9+
IF (s AND 1) <> 0 AND (s AND &20) <> 0 THEN
10+
discard = INPORT(&60)
11+
ELSE
12+
done = 1
13+
ENDIF
14+
spins = spins + 1
15+
ENDWHILE
16+
17+
OUTPORT &64, &20
18+
cmd = INPORT(&60)
19+
cmd = cmd OR &10
20+
OUTPORT &64, &60
21+
OUTPORT &60, cmd
22+
23+
OUTPORT &64, &A8
24+
25+
ok = FNmouse_send(&FF)
26+
ok = FNmouse_send(&F6)
27+
ok = FNmouse_send(&F4)
28+
29+
x = 0
30+
y = 0
31+
buttons = 0
32+
DIM pkt,3
33+
idx = 0
34+
35+
WHILE TRUE
36+
b = FNread_mouse_byte
37+
IF b >= 0 THEN
38+
valid_first = NOT (idx = 0 AND (b AND 8) = 0)
39+
IF valid_first = 1 THEN
40+
pkt(idx) = b
41+
idx = idx + 1
42+
43+
IF idx = 3 THEN
44+
b0 = pkt(0)
45+
b1 = pkt(1)
46+
b2 = pkt(2)
47+
48+
dx = b1
49+
dy = b2
50+
IF (b0 AND &10) <> 0 THEN
51+
dx = dx - 256
52+
ENDIF
53+
IF (b0 AND &20) <> 0 THEN
54+
dy = dy - 256
55+
ENDIF
56+
57+
x = x + dx
58+
y = y - dy
59+
60+
buttons = b0 AND 7
61+
62+
idx = 0
63+
ENDIF
64+
ENDIF
65+
ENDIF
66+
67+
PROCudp_handle_request
68+
ENDWHILE
69+
70+
DEF PROCudp_handle_request
71+
buf$ = UDPREAD$(port)
72+
IF buf$ = "" THEN
73+
ENDPROC
74+
ENDIF
75+
76+
from_ip$ = UDPLASTIP$()
77+
from_port = UDPLASTSOURCEPORT()
78+
79+
IF buf$ = "GET" THEN
80+
resp$ = STR$(x) + " " + STR$(y) + " " + STR$(buttons)
81+
UDPWRITE from_ip$, MOUSE_UDP_PORT, from_port, resp$
82+
ENDIF
83+
ENDPROC
84+
85+
DEF PROCwait_ibf_clear(max_spins)
86+
spins = 0
87+
WHILE (INPORT(&64) AND 2) <> 0 AND spins < max_spins
88+
spins = spins + 1
89+
ENDWHILE
90+
ENDPROC
91+
92+
DEF FNread_mouse_byte
93+
s = INPORT(&64)
94+
IF (s AND 1) = 0 THEN
95+
= -1
96+
ENDIF
97+
IF (s AND &20) = 0 THEN
98+
= -1
99+
ENDIF
100+
= INPORT(&60)
101+
102+
DEF FNmouse_send(b)
103+
tries = 0
104+
WHILE tries < 3
105+
PROCwait_ibf_clear(64)
106+
OUTPORT &64, &D4
107+
PROCwait_ibf_clear(64)
108+
OUTPORT &60, b
109+
110+
ack = 0
111+
resend = 0
112+
spins = 0
113+
WHILE spins < 128 AND ack = 0 AND resend = 0
114+
r = FNread_mouse_byte
115+
IF r = &FA THEN
116+
ack = 1
117+
ENDIF
118+
IF r = &FE THEN
119+
resend = 1
120+
ENDIF
121+
spins = spins + 1
122+
ENDWHILE
123+
124+
IF ack = 1 THEN
125+
= 1
126+
ENDIF
127+
128+
tries = tries + 1
129+
ENDWHILE
130+
= 0

src/basic/function.c

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,68 @@ extern bool debug;
88

99
struct basic_int_fn builtin_int[] =
1010
{
11-
{ basic_abs, "ABS" },
12-
{ basic_altkey, "ALTKEY" },
13-
{ basic_asc, "ASC" },
14-
{ basic_capslock, "CAPSLOCK" },
15-
{ basic_cpuid, "CPUID" },
16-
{ basic_ctrlkey, "CTRLKEY" },
17-
{ basic_get_text_cur_x, "CURRENTX" },
18-
{ basic_get_text_cur_y, "CURRENTY" },
19-
{ basic_eof, "EOF" },
20-
{ basic_existsvar_int, "EXISTSVARI" },
21-
{ basic_existsvar_real, "EXISTSVARR" },
22-
{ basic_existsvar_string, "EXISTSVARS" },
23-
{ basic_getnamecount, "GETNAMECOUNT" },
24-
{ basic_getproccount, "GETPROCCOUNT" },
25-
{ basic_getproccpuid, "GETPROCCPUID" },
26-
{ basic_getprocid, "GETPROCID" },
27-
{ basic_getprocparent, "GETPROCPARENT" },
28-
{ basic_getprocmem, "GETPROCMEM" },
29-
{ basic_getvar_int, "GETVARI" },
30-
{ basic_getsize, "GETSIZE" },
31-
{ basic_hexval, "HEXVAL" },
32-
{ basic_instr, "INSTR" },
33-
{ basic_legacy_cpuid, "LCPUID" },
34-
{ basic_len, "LEN" },
35-
{ basic_legacy_getlastcpuid, "LGETLASTCPUID" },
36-
{ basic_get_free_mem, "MEMFREE" },
37-
{ basic_get_used_mem, "MEMUSED" },
38-
{ basic_get_total_mem, "MEMORY" },
39-
{ basic_get_program_peak_mem,"MEMPEAK" },
40-
{ basic_get_program_cur_mem, "MEMPROGRAM" },
41-
{ basic_octval, "OCTVAL" },
42-
{ basic_openin, "OPENIN" },
43-
{ basic_openout, "OPENOUT" },
44-
{ basic_openup, "OPENUP" },
45-
{ basic_atoi, "RADIX" },
46-
{ basic_read, "READ" },
47-
{ basic_rgb, "RGB" },
48-
{ basic_random, "RND" },
49-
{ basic_shl, "SHL" },
50-
{ basic_shiftkey, "SHIFTKEY" },
51-
{ basic_shr, "SHR" },
52-
{ basic_sockstatus, "SOCKSTATUS" },
53-
{ basic_get_text_max_y, "TERMHEIGHT" },
54-
{ basic_get_text_max_x, "TERMWIDTH" },
55-
{ basic_val, "VAL" },
56-
{ basic_sgn, "SGN" },
57-
{ basic_int, "INT" },
58-
{ basic_gethour, "HOUR" },
59-
{ basic_getupsecs, "UPSECS" },
60-
{ basic_getminute, "MINUTE" },
61-
{ basic_getsecond, "SECOND" },
62-
{ basic_getepoch, "EPOCH" },
63-
{ basic_getmonth, "MONTH" },
64-
{ basic_getday, "DAY" },
65-
{ basic_getweekday, "WEEKDAY" },
66-
{ basic_getyear, "YEAR" },
67-
{ basic_get_day_of_year, "YDAY" },
68-
{ basic_inportw, "INPORTW" },
69-
{ basic_inportd, "INPORTD" },
70-
{ basic_inport, "INPORT" },
71-
{ NULL, NULL },
11+
{ basic_abs, "ABS" },
12+
{ basic_altkey, "ALTKEY" },
13+
{ basic_asc, "ASC" },
14+
{ basic_capslock, "CAPSLOCK" },
15+
{ basic_cpuid, "CPUID" },
16+
{ basic_ctrlkey, "CTRLKEY" },
17+
{ basic_get_text_cur_x, "CURRENTX" },
18+
{ basic_get_text_cur_y, "CURRENTY" },
19+
{ basic_eof, "EOF" },
20+
{ basic_existsvar_int, "EXISTSVARI" },
21+
{ basic_existsvar_real, "EXISTSVARR" },
22+
{ basic_existsvar_string, "EXISTSVARS" },
23+
{ basic_getnamecount, "GETNAMECOUNT" },
24+
{ basic_getproccount, "GETPROCCOUNT" },
25+
{ basic_getproccpuid, "GETPROCCPUID" },
26+
{ basic_getprocid, "GETPROCID" },
27+
{ basic_getprocparent, "GETPROCPARENT" },
28+
{ basic_getprocmem, "GETPROCMEM" },
29+
{ basic_getvar_int, "GETVARI" },
30+
{ basic_getsize, "GETSIZE" },
31+
{ basic_hexval, "HEXVAL" },
32+
{ basic_instr, "INSTR" },
33+
{ basic_legacy_cpuid, "LCPUID" },
34+
{ basic_len, "LEN" },
35+
{ basic_legacy_getlastcpuid, "LGETLASTCPUID" },
36+
{ basic_get_free_mem, "MEMFREE" },
37+
{ basic_get_used_mem, "MEMUSED" },
38+
{ basic_get_total_mem, "MEMORY" },
39+
{ basic_get_program_peak_mem,"MEMPEAK" },
40+
{ basic_get_program_cur_mem, "MEMPROGRAM" },
41+
{ basic_octval, "OCTVAL" },
42+
{ basic_openin, "OPENIN" },
43+
{ basic_openout, "OPENOUT" },
44+
{ basic_openup, "OPENUP" },
45+
{ basic_atoi, "RADIX" },
46+
{ basic_read, "READ" },
47+
{ basic_rgb, "RGB" },
48+
{ basic_random, "RND" },
49+
{ basic_shl, "SHL" },
50+
{ basic_shiftkey, "SHIFTKEY" },
51+
{ basic_shr, "SHR" },
52+
{ basic_sockstatus, "SOCKSTATUS" },
53+
{ basic_get_text_max_y, "TERMHEIGHT" },
54+
{ basic_get_text_max_x, "TERMWIDTH" },
55+
{ basic_val, "VAL" },
56+
{ basic_sgn, "SGN" },
57+
{ basic_int, "INT" },
58+
{ basic_gethour, "HOUR" },
59+
{ basic_getupsecs, "UPSECS" },
60+
{ basic_getminute, "MINUTE" },
61+
{ basic_getsecond, "SECOND" },
62+
{ basic_getepoch, "EPOCH" },
63+
{ basic_getmonth, "MONTH" },
64+
{ basic_getday, "DAY" },
65+
{ basic_getweekday, "WEEKDAY" },
66+
{ basic_getyear, "YEAR" },
67+
{ basic_get_day_of_year, "YDAY" },
68+
{ basic_inportw, "INPORTW" },
69+
{ basic_inportd, "INPORTD" },
70+
{ basic_inport, "INPORT" },
71+
{ basic_udplastsourceport, "UDPLASTSOURCEPORT" },
72+
{ NULL, NULL },
7273
};
7374

7475
struct basic_double_fn builtin_double[] = {
@@ -133,6 +134,7 @@ struct basic_str_fn builtin_str[] =
133134
{ basic_time, "TIME$" },
134135
{ basic_get_upstr, "UPTIME$" },
135136
{ basic_udpread, "UDPREAD$" },
137+
{ basic_udplastip, "UDPLASTIP$" },
136138
{ NULL, NULL },
137139
};
138140

src/basic/lowlevel.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ int64_t basic_inport(struct basic_ctx* ctx)
165165
PARAMS_GET_ITEM(BIP_INT);
166166
int64_t port = intval;
167167
PARAMS_END("INPORT", -1);
168-
return inb(port);
168+
return inb(port & 0xFFFF);
169169
}
170170

171171
int64_t basic_inportw(struct basic_ctx* ctx)
@@ -174,7 +174,7 @@ int64_t basic_inportw(struct basic_ctx* ctx)
174174
PARAMS_GET_ITEM(BIP_INT);
175175
int64_t port = intval;
176176
PARAMS_END("INPORTW", -1);
177-
return inw(port);
177+
return inw(port & 0xFFFF);
178178
}
179179

180180
int64_t basic_inportd(struct basic_ctx* ctx)
@@ -183,7 +183,7 @@ int64_t basic_inportd(struct basic_ctx* ctx)
183183
PARAMS_GET_ITEM(BIP_INT);
184184
int64_t port = intval;
185185
PARAMS_END("INPORTD", -1);
186-
return inl(port);
186+
return inl(port & 0xFFFF);
187187
}
188188

189189
void outport_statement(struct basic_ctx* ctx) {
@@ -192,7 +192,7 @@ void outport_statement(struct basic_ctx* ctx) {
192192
accept_or_return(COMMA, ctx);
193193
int64_t value = expr(ctx) & 0xFF;
194194
accept_or_return(NEWLINE, ctx);
195-
outb(port, value);
195+
outb(port & 0xFFFF, value);
196196
}
197197

198198
void outportw_statement(struct basic_ctx* ctx) {
@@ -201,7 +201,7 @@ void outportw_statement(struct basic_ctx* ctx) {
201201
accept_or_return(COMMA, ctx);
202202
int64_t value = expr(ctx) & 0xFFFF;
203203
accept_or_return(NEWLINE, ctx);
204-
outw(port, value);
204+
outw(port & 0xFFFF, value);
205205
}
206206

207207
void outportd_statement(struct basic_ctx* ctx) {
@@ -210,6 +210,5 @@ void outportd_statement(struct basic_ctx* ctx) {
210210
accept_or_return(COMMA, ctx);
211211
int64_t value = expr(ctx) & 0xFFFFFFFF;
212212
accept_or_return(NEWLINE, ctx);
213-
outl(port, value);
213+
outl(port & 0xFFFF, value);
214214
}
215-

src/basic/sockets.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,18 @@ void udpunbind_statement(struct basic_ctx* ctx) {
312312
udp_unregister_daemon(port, &basic_udp_handle_packet);
313313
}
314314

315+
int64_t basic_udplastsourceport(struct basic_ctx* ctx) {
316+
PARAMS_START;
317+
PARAMS_END("UDPLASTSOURCEPORT","");
318+
return ctx->last_packet.source_port;
319+
}
320+
321+
char* basic_udplastip(struct basic_ctx* ctx) {
322+
PARAMS_START;
323+
PARAMS_END("UDPLASTAIP$","");
324+
return ctx->last_packet.ip ? (char*)ctx->last_packet.ip : "";
325+
}
326+
315327
char* basic_udpread(struct basic_ctx* ctx) {
316328
PARAMS_START;
317329
PARAMS_GET_ITEM(BIP_INT);

src/keyboard.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ static void push_to_buffer(char x) {
313313
}
314314

315315
void keyboard_handler(uint8_t isr, uint64_t errorcode, uint64_t irq, void *opaque) {
316+
317+
uint8_t st = inb(0x64);
318+
if ((st & 0x01) == 0) {
319+
return; /* no data */
320+
}
321+
if (st & 0x20) {
322+
return; /* AUX (mouse) byte pending: leave it */
323+
}
324+
316325
uint8_t sc = inb(0x60);
317326

318327
if (sc == 0xE0) {

0 commit comments

Comments
 (0)