]> git.sur5r.net Git - cc65/blob - libsrc/atari/mouse.s
Documented the new mouse_setbox() and mouse_getbox functions.
[cc65] / libsrc / atari / mouse.s
1 ;--------------------------------------------------------------------
2 ; Atari 8-bit mouse routines -- 05/07/2000 Freddy Offenga
3 ; Some changes by Christian Groessler, Ullrich von Bassewitz
4 ;
5 ; The following devices are supported:
6 ; - Atari trak-ball
7 ; - ST mouse
8 ; - Amiga mouse
9 ;
10 ; Mouse checks are done in the timer 1 IRQ and the mouse arrow is
11 ; drawn in player 0 during the vertical blank
12 ;--------------------------------------------------------------------
13
14         .export         _mouse_init, _mouse_done, _mouse_box
15         .export         _mouse_show, _mouse_hide, _mouse_move
16         .export         _mouse_buttons, _mouse_pos, _mouse_info
17         .constructor    initmouse,27
18
19         .import         popax
20         .importzp       ptr1
21
22         .include "atari.inc"
23
24 TRAK_BALL       = 0     ; device Atari trak-ball
25 ST_MOUSE        = 1     ; device ST mouse
26 AMIGA_MOUSE     = 2     ; device Amiga mouse
27 MAX_TYPE        = 3     ; first illegal device type
28
29 ; the default values force the mouse cursor inside the test screen (no access to border)
30 defxmin = 48            ; default x minimum
31 defymin = 31            ; default y minimum
32 defxmax = 204           ; default x maximum
33 defymax = 211           ; default y maximum
34
35 pmsize  = 16            ; y size pm shape
36
37 xinit   = defxmin       ; init. x pos.
38 yinit   = defymin       ; init. y pos.
39
40 ;--------------------------------------------------------------------
41 ; reserve memory for the mouse pointer
42
43 initmouse:
44         lda     APPMHI+1
45         and     #%11111000      ; make 2k aligned
46         sec
47         sbc     #%00001000      ; reserve 2k
48         tax
49         adc     #3              ; add 4 (C = 1)
50         sta     mouse_pm0
51         lda     #0
52         sta     APPMHI
53         stx     APPMHI+1
54         rts
55
56
57 ;--------------------------------------------------------------------
58 ; Initialize mouse routines
59 ; void __fastcall__ mouse_init (unsigned char type);
60
61 _mouse_init:
62         cmp     #MAX_TYPE+1     ; Check for a valid type
63         bcc     setup
64
65 ifail:  lda     #0              ; init. failed
66         tax
67         rts
68
69 setup:  tax
70         lda     lvectab,x
71         sta     mouse_vec+1
72         lda     hvectab,x
73         sta     mouse_vec+2
74
75         jsr     pminit
76
77         lda     VTIMR1
78         sta     old_t1
79         lda     VTIMR1+1
80         sta     old_t1+1
81
82         lda     #<t1_vec
83         sta     VTIMR1
84         lda     #>t1_vec
85         sta     VTIMR1+1
86
87         lda     #%00000001
88         sta     AUDCTL
89
90         lda     #0
91         sta     AUDC1
92
93         lda     #15
94         sta     AUDF1
95         sta     STIMER
96
97         sei
98         lda     POKMSK
99         ora     #%00000001              ; timer 1 enable
100         sta     POKMSK
101         sta     IRQEN
102         cli
103
104         lda     VVBLKI
105         sta     vbi_jmp+1
106         lda     VVBLKI+1
107         sta     vbi_jmp+2
108
109         lda     #6
110         ldy     #<vbi
111         ldx     #>vbi
112         jsr     SETVBV
113
114         lda     #$C0
115         sta     NMIEN
116
117         ldx     #0
118         lda     #1
119         sta     mouse_off
120         rts
121
122 ;--------------------------------------------------------------------
123 ; Finish mouse routines
124 ; void mouse_done(void)
125
126 _mouse_done:
127         sei
128         lda     POKMSK
129         and     #%11111110              ; timer 1 disable
130         sta     IRQEN
131         sta     POKMSK
132         cli
133
134         lda     old_t1
135         sta     VTIMR1
136         lda     old_t1+1
137         sta     VTIMR1+1
138
139         lda     #$40
140         sta     NMIEN
141
142         lda     #6
143         ldy     vbi_jmp+1
144         ldx     vbi_jmp+2
145         jsr     SETVBV
146
147         ldx     #0
148         stx     GRACTL
149         stx     HPOSP0
150         inx
151         stx     mouse_off
152         rts
153
154 ;--------------------------------------------------------------------
155 ; Set mouse limits
156 ; void __fastcall__ mouse_box(int xmin, int ymin, int xmax, int ymax)
157
158 _mouse_box:
159         sta     ymax
160         jsr     popax           ; always ignore high byte
161         sta     xmax
162         jsr     popax
163         sta     ymin
164         jsr     popax
165         sta     xmin
166         rts
167
168 ;--------------------------------------------------------------------
169 ; Set mouse position
170 ; void __fastcall__ mouse_move(int xpos, int ypos)
171
172 _mouse_move:
173         sta     mousey          ; always ignore high byte
174         jsr     popax
175         sta     mousex
176         rts
177
178 ;--------------------------------------------------------------------
179 ; Show mouse arrow
180 ; void mouse_show(void)
181
182 _mouse_show:
183         lda     mouse_off       ; Already on?
184         beq     @L1
185         dec     mouse_off
186 @L1:    rts
187
188 ;--------------------------------------------------------------------
189 ; Hide mouse arrow
190 ; void mouse_hide(void)
191
192 _mouse_hide:
193         inc     mouse_off
194         rts
195
196 ;--------------------------------------------------------------------
197 ; Ask mouse button
198 ; unsigned char mouse_buttons(void)
199
200 _mouse_buttons:
201         ldx     #0
202         lda     STRIG0
203         bne     nobut
204 ;       lda     #14
205 ;???    sta     COLOR1
206         lda     #1
207         rts
208 nobut:  txa
209         rts
210
211 ;--------------------------------------------------------------------
212 ; Get the mouse position
213 ; void mouse_pos (struct mouse_pos* pos);
214
215 _mouse_pos:
216         sta     ptr1
217         stx     ptr1+1                  ; Store argument pointer
218         ldy     #0
219         lda     mousex                  ; X position
220         sta     (ptr1),y
221         lda     #0
222         iny
223         sta     (ptr1),y
224         lda     mousey                  ; Y position
225         iny
226         sta     (ptr1),y
227         lda     #0
228         iny
229         sta     (ptr1),y
230         rts
231
232 ;--------------------------------------------------------------------
233 ; Get the mouse position and button information
234 ; void mouse_info (struct mouse_info* info);
235
236 _mouse_info:
237
238 ; We're cheating here to keep the code smaller: The first fields of the
239 ; mouse_info struct are identical to the mouse_pos struct, so we will just
240 ; call _mouse_pos to initialize the struct pointer and fill the position
241 ; fields.
242
243         jsr     _mouse_pos
244
245 ; Fill in the button state
246
247         jsr     _mouse_buttons          ; Will not touch ptr1
248         ldy     #4
249         sta     (ptr1),y
250
251         rts
252
253 ;--------------------------------------------------------------------
254 ; Atari trak-ball check, A,Y = 4-bit port value
255
256 trak_check:
257         eor     oldval
258         and     #%00001000
259         beq     horiz
260
261         tya
262         and     #%00000100
263         beq     mmup
264
265         inc     mousey
266         bne     horiz
267
268 mmup:   dec     mousey
269
270 horiz:  tya
271         eor     oldval
272         and     #%00000010
273         beq     mmexit
274
275         tya
276         and     #%00000001
277         beq     mmleft
278
279         inc     mousex
280         bne     mmexit
281
282 mmleft: dec     mousex
283
284 mmexit: sty     oldval
285         rts
286
287 ;--------------------------------------------------------------------
288 ; ST mouse check, A,Y = 4-bit port value
289
290 st_check:
291         and     #%00000011
292         ora     dumx
293         tax
294         lda     sttab,x
295         bmi     nxst
296
297         beq     xist
298         dec     mousex              ; 1 = left
299         bne     nxst
300 xist:   inc     mousex              ; 0 = right
301
302 nxst:   tya
303         and     #%00001100
304         ora     dumy
305         tax
306         lda     sttab,x
307         bmi     nyst
308
309         bne     yst
310         dec     mousey              ; 0 = up
311         bne     nyst
312 yst:    inc     mousey              ; 1 = down
313
314 ; store old readings
315
316 nyst:   tya
317         and     #%00000011
318         asl
319         asl
320         sta     dumx
321         tya
322         and     #%00001100
323         lsr
324         lsr
325         sta     dumy
326         rts
327
328 ;--------------------------------------------------------------------
329 ; Amiga mouse check, A,Y = 4-bit port value
330
331 amiga_check:
332
333         lsr
334         and     #%00000101
335         ora     dumx
336         tax
337         lda     amitab,x
338         bmi     nxami
339
340         bne     xiami
341         dec     mousex              ; 0 = left
342         bne     nxami
343 xiami:  inc     mousex              ; 1 = right
344
345 nxami:  tya
346
347         and     #%00000101
348         ora     dumy
349         tax
350         lda     amitab,x
351         bmi     nyami
352
353         bne     yiami
354         dec     mousey              ; 0 =  up
355         bne     nyami
356 yiami:  inc     mousey              ; 1 = down
357
358 ; store old readings
359
360 nyami:  tya
361         and     #%00001010
362         sta     dumx
363         tya
364         and     #%00000101
365         asl
366         sta     dumy
367         rts
368
369 ;--------------------------------------------------------------------
370 ; timer 1 IRQ routine - check mouse
371
372 t1_vec: tya
373         pha
374         txa
375         pha     
376
377 .ifdef DEBUG
378         lda     RANDOM
379         sta     COLBK           ; debug
380 .endif
381
382         lda     PORTA
383         tay
384
385 mouse_vec:
386         jsr     st_check        ; will be modified; won't be ROMmable
387
388         pla
389         tax
390         pla
391         tay
392         pla
393         rti
394
395 ;--------------------------------------------------------------------
396 ; VBI - check mouse limits and display mouse arrow
397
398 vbi:    lda     mousex
399         cmp     xmin
400         bcs     ok1             ; xmin <= mousex
401         lda     xmin
402         sta     mousex
403
404 ok1:    lda     mousey
405         cmp     ymin
406         bcs     ok2             ; ymin <= mousey
407         lda     ymin
408         sta     mousey
409
410 ok2:    lda     xmax
411         cmp     mousex
412         bcs     ok3             ; xmax >= mousex
413         lda     xmax
414         sta     mousex
415
416 ok3:    lda     ymax
417         cmp     mousey
418         bcs     ok4             ; ymax >= mousey
419         lda     ymax
420         sta     mousey
421
422 ok4:    jsr     clrpm
423
424         lda     mouse_off
425         beq     mon
426         lda     #0
427         sta     HPOSP0
428         beq     moff
429
430 mon:    jsr     drwpm
431         lda     mousey
432         sta     omy
433
434         lda     #3
435 moff:   sta     GRACTL
436
437 vbi_jmp:
438         jmp     SYSVBV          ; will be modified; won't be ROMmable
439
440 ;--------------------------------------------------------------------
441 ; initialize mouse pm
442
443 pminit: lda     mouse_pm0
444         sta     mpatch1+2
445         sta     mpatch2+2
446         sta     mpatch3+2
447
448         ldx     #0
449         txa
450 mpatch1:
451 clpm:   sta     $1000,x         ; will be patched
452         inx
453         bne     clpm
454
455         lda     mouse_pm0
456         sec
457         sbc     #4
458         sta     PMBASE
459
460         lda     #62
461         sta     SDMCTL
462
463         lda     #1
464         sta     GPRIOR
465
466         lda     #0
467         sta     PCOLR0
468         sta     SIZEP0
469         rts
470
471 ;--------------------------------------------------------------------
472 ; draw new mouse pm
473
474 drwpm:  lda     mousex
475         sta     HPOSP0
476
477         lda     mousey
478         tax
479
480         ldy     #0
481 fmp2:   lda     mskpm,y
482 mpatch2:
483         sta     $1000,x         ; will be patched
484         inx
485         iny
486         cpy     #pmsize
487         bne     fmp2
488         rts
489
490 ;--------------------------------------------------------------------
491 ; clear old mouse pm
492
493 clrpm:  lda     omy
494         tax
495
496         ldy     #0
497         tya
498 mpatch3:
499 fmp1:   sta     $1000,x         ; will be patched
500         inx
501         iny
502         cpy     #pmsize
503         bne     fmp1
504         rts
505
506 ;--------------------------------------------------------------------
507         .rodata
508
509 ; mouse arrow - pm shape
510
511 mskpm:  .byte %00000000
512         .byte %10000000
513         .byte %11000000
514         .byte %11000000
515
516         .byte %11100000
517         .byte %11100000
518         .byte %11110000
519         .byte %11100000
520
521         .byte %11100000
522         .byte %00100000
523         .byte %00100000
524         .byte %00110000
525
526         .byte %00110000
527         .byte %00000000
528         .byte %00000000
529         .byte %00000000
530
531 ; ST mouse lookup table
532
533 sttab:  .byte $FF,$01,$00,$01
534         .byte $00,$FF,$00,$01
535         .byte $01,$00,$FF,$00
536         .byte $01,$00,$01,$FF
537
538 ; Amiga mouse lookup table
539
540 amitab: .byte $FF,$01,$00,$FF
541         .byte $00,$FF,$FF,$01
542         .byte $01,$FF,$FF,$00
543         .byte $FF,$00,$01,$FF
544
545 ; Device vectors
546
547 lvectab:
548         .byte <trak_check, <st_check, <amiga_check
549 hvectab:
550         .byte >trak_check, >st_check, >amiga_check
551
552 ; default values
553
554 xmin:   .byte   defxmin
555 ymin:   .byte   defymin
556 xmax:   .byte   defxmax
557 ymax:   .byte   defymax
558
559 mousex: .byte   xinit
560 mousey: .byte   yinit
561
562 ;--------------------------------------------------------------------
563         .bss
564
565 ; Misc. vars
566
567 old_t1: .res 2          ; old timer interrupt vector
568 oldval: .res 1          ; used by trakball routines
569 dumx:   .res 1
570 dumy:   .res 1
571 omy:    .res 1          ; old y pos
572
573 mouse_off:
574         .res 1
575 mouse_pm0:
576         .res 1