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