Wnk.asm

From Intellivision Wiki
Jump to: navigation, search


Functions Provided

Entry pointFunction providedNotes
WAITKEYWaits for a tap on either controllerSpecifically, waits for there to be no input on either controller, followed by an input from either controller, and then no input again. Debounces the input to avoid glitches.
WAITNOKEYWaits for there to be no input on either controller.Debounced to avoid glitches.



These functions take no arguments.

Examples

The following example program counts the number of taps of the controller one has made and displays that count on screen. It uses several library files in addition to this one, including gimini.asm, print.asm, prnum16.asm, prnum32.asm, and fillmem.asm.


;;==========================================================================;;
;; Joe Zbiciak's Tap Counting Demo                                          ;;
;; Copyright 2008, Joe Zbiciak, intvnut AT gmail.com.                       ;;
;; http://spatula-city.org/~im14u2c/intv/                                   ;;
;;==========================================================================;;

;* ======================================================================== *;
;*  TO BUILD IN BIN+CFG FORMAT:                                             *;
;*      as1600 -i ../library -o counttap.bin -l counttap.lst counttap.asm   *;
;*                                                                          *;
;*  TO BUILD IN ROM FORMAT:                                                 *;
;*      as1600 -i ../library -o counttap.rom -l counttap.lst counttap.asm   *;
;* ======================================================================== *;

;* ======================================================================== *;
;*  These routines are placed into the public domain by their author.  All  *;
;*  copyright rights are hereby relinquished on the routines and data in    *;
;*  this file.  -- Joseph Zbiciak, 2008                                     *;
;* ======================================================================== *;


        ROMW    16              ; Use 16-bit ROM width
        ORG     $5000           ; Use default memory map

;------------------------------------------------------------------------------
; Include system information
;------------------------------------------------------------------------------
        INCLUDE "gimini.asm"

;------------------------------------------------------------------------------
; EXEC-friendly ROM header.
;------------------------------------------------------------------------------
ROMHDR: BIDECLE ZERO            ; MOB picture base   (points to NULL list)
        BIDECLE ZERO            ; Process table      (points to NULL list)
        BIDECLE MAIN            ; Program start address
        BIDECLE ZERO            ; Bkgnd picture base (points to NULL list)
        BIDECLE ONES            ; GRAM pictures      (points to NULL list)
        BIDECLE TITLE           ; Cartridge title/date
        DECLE   $03C0           ; No ECS title, run code after title,
                                ; ... no clicks
ZERO:   DECLE   $0000           ; Screen border control
        DECLE   $0000           ; 0 = color stack, 1 = f/b mode
ONES:   DECLE   C_BLU, C_BLU    ; Initial color stack 0 and 1: Blue
        DECLE   C_BLU, C_BLU    ; Initial color stack 2 and 3: Blue
        DECLE   C_BLU           ; Initial border color: Blue
;------------------------------------------------------------------------------


;; ======================================================================== ;;
;;  TITLE  -- Display our modified title screen & copyright date.           ;;
;; ======================================================================== ;;
TITLE:  PROC
        BYTE    108, 'Tap Counting Demo', 0

;; ======================================================================== ;;
;;  MAIN   -- Where our program starts (immediately after title string).    ;;
;; ======================================================================== ;;
MAIN:   MVII    #ISR,   R0
        MVO     R0,     $100
        SWAP    R0
        MVO     R0,     $101
        EIS

        CALL    CLRSCR          ; Clear the screen

        CALL    PRINT.FLS       ; Display our message.
        DECLE   C_YEL           ; Yellow
        DECLE   $200 + 4*20 + 2 ; Row #4, colukmn #2 on screen
              ;01234567890123456789
        STRING  'Controller taps:'
        BYTE    0

        CLRR    R0              ; \
        PSHR    R0              ;  |- Put a 32-bit count of 0 on the 
        PSHR    R0              ; /   top of stack

@@loop:
        CALL    WAITKEY         ; Wait for a tap on the controller

        PULR    R1              ; \_ Get our current count from top of stack
        PULR    R0              ; /

        INCR    R0              ; Add 1 to lower half
        BNEQ    @@nocarry
        INCR    R1              ; Add 1 to upper half if lower half wrapped
@@nocarry

        PSHR    R0              ; \_ Remember current count on stack
        PSHR    R1              ; /

        ; To print the number, we must set up the following:
        ; R1:R0 contains 32-bit count
        ; R2 contains the width of the field 
        ; R3 contains the "format word" (generally just the color)
        ; R4 contains where to display it
        MVII    #8,     R2              ; A nice, ridiculously wide field.
        MVII    #C_WHT, R3              ; Display it in white
        MVII    #$200 + 6*20 + 6, R4    ; Row 6, column 6
        CALL    PRNUM32.z               ; Display with leading zeroes.

        B       @@loop          ; Wait for the next tap

        ENDP

;; ======================================================================== ;;
;;  ISR   -- A simple ISR to keep the screen enabled.                       ;;
;; ======================================================================== ;;
ISR     PROC
        MVO     R0,     $20     ; STIC handshake to keep display enabled

        MVII    #C_BLU, R0
        MVO     R0,     $28     ; Set color stack 0 to blue
        MVO     R0,     $2C     ; Set border to blue

        JR      R5              ; Return
        ENDP

;; ======================================================================== ;;
;;  LIBRARY INCLUDES                                                        ;;
;; ======================================================================== ;;
        INCLUDE "print.asm"     ; PRINT.xxx routines
        INCLUDE "prnum32.asm"   ; PRNUM32.x routines (include before PRNUM16)
        INCLUDE "prnum16.asm"   ; PRNUM16.x routines
        INCLUDE "wnk.asm"       ; WAITKEY routine
        INCLUDE "fillmem.asm"   ; CLRSCR/FILLZERO/FILLMEM

Notes

The code reads both controllers by XORing the inputs of the two together. Thus, it is possible to fool this code into thinking nothing is pressed if the exact same thing is pressed on both controllers. This isn't seen as a big deal.

Despite "key" appearing in their name, WAITKEY and WAITNOKEY really look for any input. Disc, action buttons and keypad all have the same effect to this code.

Source Code

;* ======================================================================== *;
;*  These routines are placed into the public domain by their author.  All  *;
;*  copyright rights are hereby relinquished on the routines and data in    *;
;*  this file.  -- Joseph Zbiciak, 2008                                     *;
;* ======================================================================== *;

;; ======================================================================== ;;
;;  WAITKEY     Wait for release/press/release                              ;;
;;  WAITNOKEY   Wait for key release                                        ;;
;; ======================================================================== ;;
WAITKEY     PROC
            PSHR    R5
@@1:        CALL    WAITNOKEY   ; Wait for release-press-release
@@wk        MVI     $1FE,   R0
            XOR     $1FF,   R0
            BEQ     @@wk
            INCR    PC          ; skip PSHR R5 
WAITNOKEY   PSHR    R5

@@waitnokey MVII    #200,   R1  ; Debounce counter

@@stillnk   MVI     $1FE,   R0
            XOR     $1FF,   R0
            BNEQ    @@waitnokey

            DECR    R1          ; Avoid glitches:  Make sure it's good and gone
            BPL     @@stillnk

            PULR    PC
            ENDP
        
;; ======================================================================== ;;
;;  End of File:  wnk.asm                                                   ;;
;; ======================================================================== ;;