]> git.sur5r.net Git - cc65/blob - libsrc/joystick/joy-kernel.s
Moved the CAPS LOCK code out of the startup file, and into its own file.
[cc65] / libsrc / joystick / joy-kernel.s
1 ;
2 ; Ullrich von Bassewitz, 2002-12-20
3 ;
4 ; Common functions of the joystick API.
5 ;
6
7         .import         joy_libref
8         .importzp       ptr1
9         .interruptor    joy_irq         ; Export as IRQ handler
10
11         .include        "joy-kernel.inc"
12         .include        "joy-error.inc"
13
14
15 ;----------------------------------------------------------------------------
16 ; Variables
17
18
19 .bss
20 _joy_drv:       .res    2               ; Pointer to driver
21
22 _joy_masks:     .res    .sizeof(JOY_HDR::MASKS)
23
24 ; Jump table for the driver functions.
25 .data
26 joy_vectors:
27 joy_install:    jmp     $0000
28 joy_uninstall:  jmp     $0000
29 joy_count:      jmp     $0000
30 joy_read:       jmp     $0000
31 joy_irq:        .byte   $60, $00, $00   ; RTS plus two dummy bytes
32
33 ; Driver header signature
34 .rodata
35 joy_sig:        .byte   $6A, $6F, $79, JOY_API_VERSION  ; "joy", version
36
37
38 .code
39 ;----------------------------------------------------------------------------
40 ; unsigned char __fastcall__ joy_install (void* driver);
41 ; /* Install the driver once it is loaded */
42
43
44 _joy_install:
45         sta     _joy_drv
46         sta     ptr1
47         stx     _joy_drv+1
48         stx     ptr1+1
49
50 ; Check the driver signature
51
52         ldy     #.sizeof(joy_sig)-1
53 @L0:    lda     (ptr1),y
54         cmp     joy_sig,y
55         bne     inv_drv
56         dey
57         bpl     @L0
58
59 ; Set the library reference
60
61         ldy     #JOY_HDR::LIBREF
62         lda     #<joy_libref
63         sta     (ptr1),y
64         iny
65         lda     #>joy_libref
66         sta     (ptr1),y
67
68 ; Copy the mask array
69
70         ldy     #JOY_HDR::MASKS + .sizeof(JOY_HDR::MASKS) - 1
71         ldx     #.sizeof(JOY_HDR::MASKS)-1
72 @L1:    lda     (ptr1),y
73         sta     _joy_masks,x
74         dey
75         dex
76         bpl     @L1
77
78 ; Copy the jump vectors
79
80         ldy     #JOY_HDR::JUMPTAB
81         ldx     #0
82 @L2:    inx                             ; Skip the JMP opcode
83         jsr     copy                    ; Copy one byte
84         jsr     copy                    ; Copy one byte
85         cpy     #(JOY_HDR::JUMPTAB + .sizeof(JOY_HDR::JUMPTAB))
86         bne     @L2
87
88         jsr     joy_install             ; Call driver install routine
89         tay                             ; Test error code
90         bne     @L3                     ; Bail out if install had errors
91
92 ; Install the IRQ vector if the driver needs it. A/X contains the error code
93 ; from joy_install, so don't use it.
94
95         ldy     joy_irq+2               ; Check high byte of IRQ vector
96         beq     @L3                     ; Jump if vector invalid
97         ldy     #$4C                    ; JMP opcode
98         sty     joy_irq                 ; Activate IRQ routine
99 @L3:    rts
100
101 ; Driver signature invalid
102
103 inv_drv:
104         lda     #JOY_ERR_INV_DRIVER
105         ldx     #0
106         rts
107
108 ; Copy one byte from the jump vectors
109
110 copy:   lda     (ptr1),y
111         iny
112 set:    sta     joy_vectors,x
113         inx
114         rts
115
116 ;----------------------------------------------------------------------------
117 ; unsigned char joy_uninstall (void);
118 ; /* Uninstall the currently loaded driver. Note: This call does not free
119 ; ** allocated memory.
120 ; */
121
122 _joy_uninstall:
123         lda     #$60                    ; RTS opcode
124         sta     joy_irq                 ; Disable IRQ entry point
125
126         jsr     joy_uninstall           ; Call the driver routine
127
128 _joy_clear_ptr:                         ; External entry point
129         lda     #0
130         sta     _joy_drv
131         sta     _joy_drv+1              ; Clear the driver pointer
132
133         tax                             ; Return zero
134         rts