z80:Miscellaneous Routines

From Learn @ Cemetech
Revision as of 21:29, 3 February 2016 by KermMartian (talk | contribs) (Created page with "A list of small routines that can help you. Feel free to add your own. = Battery Status = Directly polls the hardware for battery status. Thanks to Steve Riekeberg. ;N...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

A list of small routines that can help you. Feel free to add your own.

Battery Status

Directly polls the hardware for battery status. Thanks to Steve Riekeberg.


   ;Name: BatteryStatus
   ;Description: Returns if the batteries are good, or low and need changing.
   ;Inputs: None
   ;Outputs: Non-Zero = Good Batteries
            Zero = Batteries Low
   ;Destroys: A
   
   GetBatteryStatus:
      in a, (2)
      and 01h
      ret


Boundary Check

Checks if coordinates is inside a designated box. Thanks to Adm. Wiggin.

   ; Your box coords (will not work with 0, keep x1 < x2, and y1 < y2 [duh])
   #define x1 10
   #define y1 10
   #define x2 20
   #define y2 30
   
   check:
   ; a = x
   ; b = y
   ; returns c flag set if good
   ; returns c flag reset if no good (nc)
   ;  destorys af (quite obvious if one knows any z80 at all)
     cp x1
     jr c,nogood
     cp x2+1
     jr nc,nogood
     ld a,b
     cp y1
     jr c,nogood
     cp y2+1
     jr nc,nogood
   ;if it gets here, it's in the box!   Yay!  Hooray!  Peoples rejoice!
   ; as a side effect of the last operation, the carry flag is already set.
     ret
   nogood:
     or a;to reset the carry flag
     ret


Center Text

Displays a string centered on the screen (small font). Thanks to WikiGuru.


   ;input:hl points to string
   ;output: string displayed centered on screen
   ;destroyed: all registers
   ;other remarks: (penrow) must be set before calling this sub-routine
   ;               first byte of string must be the length of the string
   
   centertxt:
   bcall(_SStringLength)
   ld a,96        ;width of screen
   sub b          ;subtract width of string
   rra             ;divide by 2 to be centered
   ld (pencol),a
   ld b,(hl)
   inc hl
   bcall(_VPutSN)
   ret
   
   ;to call:
   
   ...
   ld hl,0
   ld (penrow),hl
   ld hl,txtTest
   call centertxt
   ...
   
   txtTest:
   .db 11,"Sample Text"


Direct Input

Used for direct input. See here to learn more about direct input. Thanks to Steve Riekeberg.


   ;Name: DirectInput
   ;Inputs: A = Key Group
   ;Outputs: A = Key Code; Zero If None
   ;Destroys: A, B
   
   DirectInput:
      ld b,a
      ld a,$ff ;Reset the keypad
      out (1),a
      ld a,b
      out (1),a
      in a,(1)
      ret


Hotspot

Checks if h>b, l>c, h<d, and l<e. Thanks to kermmartian and DarkerLine.


   ;----------Hot spot detection-----------
   ;inputs: b,c (first x and y cor)
   ;        d,e (last x and y cor)
   ;        h,l (current x,y)
   ;output: "c" flag [either true (set) or false (reset)]
   
   hdetect:
    ld a,h
    cp b
    ccf
    ret nc
    cp d
    ret nc
    ld a,l
    cp c
    ccf
    ret nc
    cp e
    ret


On/Off Calc

Turns the calculator "off" and waits for user to press on. Note: Pulling batteries out will result in Ram Clear. Thanks to Taricorp.


   ;no inputs
   	ld a,02h
   	out (10h),a		;Turn off LCD
   	ld a,08h
   	out (3),a
   	call LCD_Delay
   	ld a,36h
   	out (4),a
   	ld a,01h
   	out (3),a
   	halt		;It halts until ON is pressed
   	ld a,0Bh
   	out (3),a
   	ld a,03h
   	out (10h),a			;Turn the LCD back on
   	res onKey,(iy+keyFlags)