Debian Codegraph @

examples/ddrv.a

raw
1 ;ACME 0.97
2 ;!sl "ddrv.l"
3 ; Name DuoDriver
4 ; Purpose Input driver for mouse and joystick
5 ; Author (c) Marco Baye, 1999
6 ; Licence Free software
7 ; Changes:
8 ; 23 Apr 1999 Release 2.20. Internal info:
9 ; DuoDriver v2.20 by Mac Bacon 23 Apr 1999. Freeware!
10 ; Somewhen Added self-calibration, forming release 3.00. Internal info:
11 ; Mac Bacon:DuoDrv3,PD
12 ; 21 Jul 1999 Used reverse subtraction, forming release 3.01. Internal info:
13 ; Mac Bacon:DuoDrv3,PD
14 ; 1 Aug 1999 Release 4.00.
15 ; Both 128 and 64 versions
16 ; Now supports overlay-sprite mouse pointer
17 ; Binary includes sprites
18 ; Released in GO64 8/1999 (without release number).
19 ; 3 Aug 1999 Same source file for both 128 and 64 versions. Release 4.01.
20 ; Apart from that, virtually identical to release 4.00.
21 ; 04 Feb 2003 Beautified
22 ; 05 Feb 2003 Added "SpriteLine" macro and made sprites inline
23 ; 26 May 2005 Release 4.02. All changes since release 4.00 are source-only!
24 ; The resulting binaries are identical to those of release 4.00
25 ; (which were included in GO64 magazine 8/1999)
26 ; 26 Mar 2006 Release 4.03. Adjusted source to ACME 0.91 capabilities.
27 ; 25 Nov 2007 Release 4.04. Adjusted source to ACME 0.94 capabilities.
28 ; 7 Apr 2013 Slightly reformatted.
29 ; 1 Jun 2014 Adjusted to experimental type system of ACME 0.95
30
31 ; This source code file uses conditional assembly
32 ; to decide which version to produce (C64 or C128).
33
34 ; Select type of binary to assemble (64 => c64, anything else => c128)
35 !ifndef SYSTEM {
36 !warn "Label SYSTEM not defined. Use -DSYSTEM=64 to build C64 version, -DSYSTEM=128 to build C128 version. Will now default to C64 version."
37 SYSTEM = 64
38 }
39 !if SYSTEM != 64 & SYSTEM != 128 {
40 !serious "Please use either -DSYSTEM=64 or -DSYSTEM=128 when assembling this project."
41 }
42
43
44 ; --- Configurable values
45
46 ; Start address, output file name and VIC location
47
48 !if SYSTEM = 64 {
49 * = $c000
50 ;!to "ddrv64.prg", cbm
51 !addr VIC_Base = $d000
52 }
53 !if SYSTEM = 128 {
54 * = $0c00
55 ;!to "ddrv128.prg", cbm
56 !addr VIC_Base = $11d6 ; Location of mirror registers
57 }
58
59 ; Pointer's maximum coordinates
60 MaximumCoordinateX = 319 ; VIC value
61 ; MaximumCoordinateX = 639 ; VDC value
62 MaximumCoordinateY = 199
63
64 ; Maximum pixel step size ("speed") for joystick acceleration routine.
65 MaxStep = $10 ; max $7f
66
67 ; Distance before acceleration starts, in pixels.
68 MaxTime = $04 ; max $7f
69
70 ; Sprites to use for overlay pointer
71 Sprite_A = 0
72 Sprite_B = 1
73
74 ; Coordinates of "pointer pixel" within pointer sprites; adjust these
75 ; if you use different sprites. (0,0) is sprite's upper left pixel.
76 Sprite_HotspotX = 1
77 Sprite_HotspotY = 1
78
79 ; address definitions
80 !addr {
81
82 ; Locations to store button states, $ff = pressed, $00 = not pressed.
83 ; Mouse uses both buttons, joystick only uses "LeftButton".
84 ; Location to store pointer's current character coordinates.
85 !if SYSTEM == 64 {
86 LeftButton = $a4
87 RightButton = $a5
88 CharX = $b3
89 CharY = $b4
90 tapebuf = $0340
91 spr_ptrs = 2040
92 }
93 !if SYSTEM == 128 {
94 LeftButton = $fa
95 RightButton = $ff
96 CharX = $9b
97 CharY = $9c
98 }
99
100 ; Location to store pointer's current pixel coordinates. The driver
101 ; code relies on having *four consecutive* bytes:
102 ; x low, x high, y low, y high
103 Coordinates = $fb ; $fb..$fe
104
105 ; --- System constants
106
107 ; Interrupt vector
108 sys_iirq = $0314
109
110 ; I/O registers
111 sid_pot = $d419
112 cia1_pra = $dc00
113 cia1_prb = $dc01
114 cia1_ddrb = $dc03
115 mmu_cr = $ff00 ; c128 only
116
117 ; dummy value for self mod
118 MODIFIED16 = $ffff
119
120 };addr
121
122 ; --- Label definitions
123
124 ; New names for some precalculated values, only to improve
125 ; readability. Don't change these.
126 PointerXnow = Coordinates
127 PointerYnow = Coordinates + 2
128 SpriteA_X = VIC_Base + 2 * Sprite_A
129 SpriteA_Y = VIC_Base + 2 * Sprite_A + 1
130 SpriteB_X = VIC_Base + 2 * Sprite_B
131 SpriteB_Y = VIC_Base + 2 * Sprite_B + 1
132 Sprites_OF = VIC_Base + 16 ; X Overflow
133 ; The character "^" in the following calculation means "to the power
134 ; of". It is ACME syntax - if your assembler cannot do this, you may
135 ; want to use hardcoded values here instead of calculations.
136 Sprites_Bitmask = 2 ^ Sprite_A + 2 ^ Sprite_B
137 ;alternative:
138 ; Sprites_Bitmask = 1 << Sprite_A | 1 << Sprite_B
139 SpriteOffset_X = $18 - Sprite_HotspotX
140 SpriteOffset_Y = $32 - Sprite_HotspotY
141 ; In the sprite coordinate system, the graphics pixel (0,0) has the
142 ; coordinates ($18,$32), so these are needed for converting. Blame the
143 ; VIC.
144
145
146 ; --- Entry point
147
148 ; Because this routine is the first, the file can be BOOTed on a c128.
149 ; Initialisation code, installs driver on IRQ vector.
150 ; Fetch IRQ vector and write to end
151 Init lda sys_iirq
152 ldx sys_iirq + 1
153 sta mod16
154 stx mod16 + 1
155 ; Let IRQ vector point to driver code
156 lda #<Entry
157 ldx #>Entry
158 php
159 sei
160 sta sys_iirq
161 stx sys_iirq + 1
162 plp
163 !if SYSTEM == 128 {
164 lda mmu_cr
165 tay
166 and #$fe ; activate I/O chips
167 sta mmu_cr
168 }
169
170 ; Init mouse buttons
171 lda #$11
172 sta cia1_prb
173 !if SYSTEM == 128 {
174 sty mmu_cr
175 }
176 !if SYSTEM == 64 {
177 ; Copy sprites to tape buffer
178 ldx #127
179 - lda Sprites, x
180 sta tapebuf, x
181 dex
182 bpl -
183 lda #Sprites_Bitmask
184 ; Set sprite block pointers
185 ldx #$0d
186 stx spr_ptrs + Sprite_A
187 inx
188 stx spr_ptrs + Sprite_B
189 ; Activate pointer sprites
190 ora VIC_Base + 21
191 sta VIC_Base + 21
192 }
193 rts
194
195
196 ; --- Variables
197
198 ; Pixel counter before accelerating
199 JoyWaittime !byte 0
200
201
202 ; --- Main code
203
204 Entry
205 ; The driver consists of several distinct parts. To minimise
206 ; performance wastage, you should remove all parts you don't need for
207 ; the specific application.
208
209
210 ; --- Part 0, initialisations
211
212 ; Make sure decimal mode is off
213 cld
214 ; Set button states to "not pressed", so the other parts only have to
215 ; deal with setting them to "pressed".
216 lda #$00
217 sta LeftButton
218 sta RightButton
219
220
221 ; --- Part 1, handling mouse movements
222
223 ; mouse x
224 ldx #$00 ; 0 means "x stuff"
225 jsr PotDelta
226 ; Now signed x movement is in A/Y. Add to current x value.
227 clc
228 adc PointerXnow
229 sta PointerXnow
230 tya
231 adc PointerXnow + 1
232 sta PointerXnow + 1
233 ; mouse y
234 ldx #$01 ; 1 means "y stuff"
235 jsr PotDelta
236 ; Now signed y movement is in A/Y. Mouse and computer use different y
237 ; directions, so don't add to, but subtract from current y value.
238 ; This is a reverse subtraction - it might be harder to understand,
239 ; but it is both faster and smaller than the usual way.
240 clc
241 sbc PointerYnow
242 eor #$ff
243 sta PointerYnow
244 tya
245 sbc PointerYnow + 1
246 eor #$ff
247 sta PointerYnow + 1
248
249
250 ; --- Part 2, handling mouse buttons
251
252 ; Prepare CIA by setting bits to input
253 ldy #$11
254 sty cia1_ddrb
255 ldx #$ff ; $ff means "pressed"
256 lda #$10 ; check left button
257 bit cia1_prb
258 bne +
259 stx LeftButton ; store state
260 + lda #$01 ; check right button
261 bit cia1_prb
262 bne +
263 stx RightButton ; store state
264 +
265 ; Reset CIA to normal state
266 ldy #$00
267 sty cia1_ddrb
268
269
270 ; --- Part 3, handling the joystick
271
272 ; Fetch byte holding direction flags
273 lda cia1_pra
274 tax ; ...and remember it
275 ; Check 'up' direction
276 ror
277 bcs ++
278 ; Subtract current step size from y value if needed.
279 tay
280 sec
281 lda PointerYnow
282 sbc JoyStepsize
283 sta PointerYnow
284 bcs +
285 dec PointerYnow + 1
286 + tya
287 ++ ; Check 'down' direction
288 ror
289 bcs ++
290 ; Add current step size to y value if needed.
291 tay
292 ;clc ; C is always clear here
293 lda PointerYnow
294 adc JoyStepsize
295 sta PointerYnow
296 bcc +
297 inc PointerYnow + 1
298 + tya
299 ++ ; Check 'left' direction
300 ror
301 bcs ++
302 ; Subtract current step size from x value if needed.
303 tay
304 sec
305 lda PointerXnow
306 sbc JoyStepsize
307 sta PointerXnow
308 bcs +
309 dec PointerXnow + 1
310 + tya
311 ++ ; Check 'right' direction
312 ror
313 bcs ++
314 ; Add current step size to x value if needed.
315 tay
316 ;clc ; C is always clear here
317 lda PointerXnow
318 adc JoyStepsize
319 sta PointerXnow
320 bcc +
321 inc PointerXnow + 1
322 + tya
323 ++
324
325 ; --- Part 4, handling joystick button
326
327 ror
328 bcs +
329 lda #$ff ; $ff means "pressed"
330 sta LeftButton
331 +
332
333 ; --- Part 5, joystick acceleration
334
335 ; Restore joystick direction bits and check whether to set speed to
336 ; zero.
337 txa
338 and #$0f ; Clear unneeded bits
339 cmp #$0f ; Any direction bit ?
340 bne +
341 ; No direction was used, so reset speed and wait counter to normal.
342 lda #$01
343 sta JoyStepsize
344 lda #MaxTime
345 sta JoyWaittime
346 jmp Part5End
347 +
348 ; A direction bit was used, so check whether to accelerate: If speed
349 ; is already maximum speed, don't accelerate.
350 JoyStepsize = * + 1
351 lda #$00 ; (self-modifying)
352 ; If the variable "JoyStepsize" would have been defined as a separate
353 ; location (using "!byte"), it would have taken a byte of memory. By
354 ; storing the value inside an LDA command's argument, we save that one
355 ; byte. It might make a difference. :)
356 cmp #MaxStep ; If speed is max.,
357 bcs Part5End ; don't accelerate.
358 ; Speed isn't maximum yet. Check whether
359 ; we have to wait before accelerating.
360 dec JoyWaittime
361 bpl Part5End
362 ; Counter has underrun, so accelerate.
363 inc JoyWaittime ; reset counter
364 inc JoyStepsize ; increase speed
365 Part5End
366
367 ; --- Part 6, restrict coordinate range
368
369 ; restrict x value
370 ldx #$00 ; 0 means "x stuff"
371 jsr Restrict
372 ; restrict y value
373 ldx #$02 ; 2 means "y stuff"
374 jsr Restrict
375
376 ; --- Part 7, positioning sprites
377
378 ; Set sprites' x positions
379 lda PointerXnow
380 clc
381 adc #SpriteOffset_X
382 sta SpriteA_X ; set both sprites
383 sta SpriteB_X
384 lda Sprites_OF ; get x overflow
385 bcs SetOF
386 ldx PointerXnow + 1
387 bne SetOF
388 and #Sprites_Bitmask XOR $ff
389 bcc StoreOF ; C is clear here
390 SetOF ora #Sprites_Bitmask
391 StoreOF sta Sprites_OF ; set x overflow
392
393 ; Set sprites' y positions
394 lda PointerYnow
395 clc
396 adc #SpriteOffset_Y
397 sta SpriteA_Y
398 sta SpriteB_Y
399 ; The y value's high byte is useless in this case.
400
401 ; --- Part 8, making char coordinates
402
403 ; Convert x coordinate. There are different "best" routines for
404 ; different resolutions, so I've given the VIC and VDC routines.
405 lda PointerXnow
406 lsr
407 lsr
408 lsr
409 ldx PointerXnow + 1
410 ;ora OrTable,x ; VDC only (see below for data table)
411 beq + ; VIC only
412 ora #$20 ; VIC only
413 + sta CharX
414
415 ; Convert y coordinate.
416 lda PointerYnow
417 lsr
418 lsr
419 lsr
420 sta CharY
421
422 ; --- Add further parts here
423
424 ; Here you can add further routines, for example to use the button
425 ; states to fake keypresses etc.
426
427 ; --- The end
428
429 ; The initialisation routine sets the argument to the address of the
430 ; previous IRQ routine.
431 mod16 = * + 1: jmp MODIFIED16 ; (self-modifying)
432
433 ; This table is for part 8.
434 ;OrTable !byte 0, 32, 64 ; VDC only
435
436 ; --- "Restrict" subroutine
437
438 PointerXmax !word MaximumCoordinateX
439 PointerYmax !word MaximumCoordinateY
440 ; "y" word must follow directly after "x" word in memory.
441
442 Restrict
443 ; Restrict internal coordinates to configured range. Entry conditions:
444 ; X is direction handle (0 = x, 2 = y)
445 lda PointerXnow + 1, x
446 bmi SetTo0
447 cmp PointerXmax + 1, x
448 bcc Eosr
449 bne +
450 lda PointerXmax, x
451 cmp PointerXnow, x
452 bcs Eosr
453 + lda PointerXmax, x
454 ldy PointerXmax + 1, x
455 jmp DefCo
456
457 SetTo0 lda #0
458 tay
459 DefCo sta PointerXnow, x
460 sty PointerXnow + 1, x
461 Eosr rts
462
463 ; --- "Pot" subroutine
464
465 ; This routine computes the mouse movements and therefore contains the
466 ; self-calibration stuff and the other improvements over the standard
467 ; 1351 driver.
468 PotMax !word 0 ; max. POTs yet plus 1 !
469 PotMin !word $ffff ; lowest POTs yet
470 PotOld !word 0 ; old values
471 PotWidth !word 0 ; interval width
472 HalfPotWidth !word 0 ; half width
473 ; (buffered for speed increase)
474 ; The above variables are not really words: The first byte is the x
475 ; value, the second byte is the y value respectively.
476
477
478 ; Compute the signed distance of mouse movement.
479 ; Entry conditions: X is direction handle (0 = x, 1 = y)
480 ; Exit conditions: A/Y are signed distance (low/high)
481
482 ; First, get new value and clear "recalculate signal width" flag.
483 PotDelta lda sid_pot, x
484 ldy #$00
485 ; Check whether new value is lower than lowest known.
486 cmp PotMin, x
487 bcs +
488 ; Store new "lowest" und set "recalculate signal width" flag.
489 sta PotMin, x
490 ldy #$ff
491 + ; Check whether new value is higher than highest known.
492 cmp PotMax, x
493 bcc +
494 ; Set "recalculate signal width" flag and store new "highest".
495 ldy #$ff
496 pha ; Remember current value
497 adc #$00 ; Add one (C is set)
498 sta PotMax, x
499 ; Value $ff (0 after adding) means that there is no mouse connected,
500 ; so reset min/max in that case.
501 beq ResetMM ; Stack is untidy...
502 pla ; Restore current value
503 + ; If flag is set, recalculate signal width.
504 iny ; Check flag
505 bne ++
506 tay ; Buffer current value.
507 lda PotMax,x ; Get highest + 1
508 sec ; Subtract lowest
509 sbc PotMin, x
510 bcc +
511 sta PotWidth, x ; Store signal
512 lsr ; width and half signal
513 sta HalfPotWidth, x ; width
514 + tya ; Restore current value.
515 ++ ; Calculate distance
516 tay ; Buffer current value.
517 sec
518 sbc PotOld, x
519 pha
520 tya
521 sta PotOld, x
522 pla
523 beq zero ; If not moved, exit.
524 bcc minus ; Negative difference
525
526 ; Positive difference:
527 ; Check whether movement caused a value wrap-around.
528 cmp HalfPotWidth, x
529 bcc Decrease
530 beq Decrease
531 ; It did, so calculate "real" distance and jump to exit
532 ;sec ; C is always set here
533 sbc PotWidth, x ; Fix distance
534
535 ; We now know that the (fixed) distance is really negative, so we
536 ; finally wipe out that annoying bit 0 noise by incrementing the
537 ; value.
538 Increase ;clc ; C is always clear here
539 adc #$01
540 beq zero ; If increasing gives zero, jump to zero handler.
541 ldy #$ff ; Set up high byte for negative values.
542 rts
543
544 ; Negative difference:
545 ; Check whether movement caused a value wrap-around.
546 minus eor #$ff ; Complement
547 ; If we would do a real negation (by adding "1"), then we would need
548 ; to branch using BCC *and* BEQ. So the above way might be harder to
549 ; understand, but it is both shorter *and* faster - which I like. :)
550 cmp HalfPotWidth, x
551 eor #$ff ; Restore value
552 bcc Increase
553 ; Movement caused a value wrap-around, so calculate "real" distance and exit.
554 clc
555 adc PotWidth, x ; Fix distance
556
557 ; We now know that the (fixed) distance is really positive, so we
558 ; finally wipe out that annoying bit 0 noise by decrementing the value.
559 Decrease sec
560 sbc #$01
561
562 ; No difference or positive difference; both need zero as the high byte.
563 zero ldy #0
564 rts
565
566 ; If there is no mouse, reset "lowest" ("highest" will have been reset
567 ; already) and return zero.
568 ResetMM tay ; Set Y to zero.
569 pla ; Tidy stack
570 lda #$ff ; Reset "lowest"
571 sta PotMin, x
572 tya ; Return with A/Y = 0
573 rts
574
575 ; --- Include sprites
576
577 ; Because the c64 version copies the sprite data into the tape buffer
578 ; on initialisation, the data is included right here.
579 ; In the c128 version, we skip memory until we reach $0e00 - this is
580 ; where the sprites are stored by default.
581
582 !if SYSTEM == 128 {
583 !align $ffff, $e00, $0
584 }
585
586 !macro SpriteLine .v {
587 !by .v >> 16, (.v >> 8) & 255, .v & 255
588 }
589
590 Sprites ; 765432107654321076543210
591 +SpriteLine %........................
592 +SpriteLine %.#......................
593 +SpriteLine %.##.....................
594 +SpriteLine %.###....................
595 +SpriteLine %.####...................
596 +SpriteLine %.#####..................
597 +SpriteLine %.######.................
598 +SpriteLine %.#######................
599 +SpriteLine %.########...............
600 +SpriteLine %.#########..............
601 +SpriteLine %.########...............
602 +SpriteLine %.######.................
603 +SpriteLine %.######.................
604 +SpriteLine %.##..##.................
605 +SpriteLine %.#....##................
606 +SpriteLine %......##................
607 +SpriteLine %.......##...............
608 +SpriteLine %.......##...............
609 +SpriteLine %........##..............
610 +SpriteLine %........##..............
611 +SpriteLine %........................
612 !byte 0 ; pad to 64-byte block
613 ; 765432107654321076543210
614 +SpriteLine %##......................
615 +SpriteLine %###.....................
616 +SpriteLine %####....................
617 +SpriteLine %#####...................
618 +SpriteLine %######..................
619 +SpriteLine %#######.................
620 +SpriteLine %########................
621 +SpriteLine %#########...............
622 +SpriteLine %##########..............
623 +SpriteLine %###########.............
624 +SpriteLine %###########.............
625 +SpriteLine %#########...............
626 +SpriteLine %########................
627 +SpriteLine %########................
628 +SpriteLine %###..####...............
629 +SpriteLine %##...####...............
630 +SpriteLine %......####..............
631 +SpriteLine %......####..............
632 +SpriteLine %.......####.............
633 +SpriteLine %.......####.............
634 +SpriteLine %........###.............