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