DRUID Development Docs: gutil.inc

Gadget Sourcecode: gutil.inc

; Justice Unlimited Gadget Microcode
; (C) 2004 Nathan (Acorn) Pooley 
;
; gutil.inc
;
;@DOC@ Various utility functions
;

;############################################
;############### WAIT #######################
;############################################

wait_1sec:
    rcall   wait_333ms

wait_666ms:
    rcall   wait_333ms

wait_333ms:
    movlw   228

    ;
    ; wait approx 12 cycles * w = 1.464ms * w
    ;
wait:
    movwf   v_wait_cnt
wait_loop:
    rcall   time
    decfsz  v_wait_cnt
    bra     wait_loop

    return

;############################################
;############### TABLE SETUP ################
;############################################

    ;
    ; setup to read from program memory
    ;
setup_progread:
    bcf     b_cfgs      ; access program mem (not config regs)
    bsf     b_eepgd     ; access program mem (not eeprom)
    clrf    TBLPTRU     ; zero the pointer
    clrf    TBLPTRH     ; zero the pointer
    clrf    TBLPTRL     ; zero the pointer
    return

;############################################
;############### PUT NUMBER #################
;############################################

    ;
    ; draw 2 digit number (decimal value)
    ;    w should hold number (must be less than 100)
    ;
putnum_2d:
    clrf    v_num_hi
    movwf   v_num_lo
    bsf     b_num_started
    bra     putnum_10

    ;
    ; draw byte (decimal value)
    ;    w should hold number
    ;
putnum_w:
    clrf    v_num_hi
    movwf   v_num_lo

    ;
    ; draw number (decimal value)
    ;    v_num_lo, v_num_hi should hold number
    ;
putnum:
    bcf     b_num_started   ; clear this until 1st nonzero digit

    movlw   HIGH 10000      ; 10000 digit
    movwf   v_num_digit_hi
    movlw   LOW 10000
    movwf   v_num_digit_lo
    rcall   putnum_digit

    movlw   HIGH 1000       ; 1000 digit
    movwf   v_num_digit_hi
    movlw   LOW 1000
    movwf   v_num_digit_lo
    rcall   putnum_digit

    clrf    v_num_digit_hi  ; 100 digit
    movlw   100
    movwf   v_num_digit_lo
    rcall   putnum_digit

putnum_10:
    clrf    v_num_digit_hi
    movlw   10              ; 10 digit
    movwf   v_num_digit_lo
    rcall   putnum_digit

    bsf     b_num_started   ; draw 1 digit even if zero
    movlw   1               ; 1 digit
    movwf   v_num_digit_lo
    bra     putnum_digit

    ; DONE - returns to caller


    ;
    ; draw digit (if not a leading 0)
    ; remove that digit from v_num_lo, v_num_hi
    ; v_num_digit_lo, v_num_digit_hi indicate which digit
    ;
putnum_digit:
    setf    v_tmp

    ;
    ; find value of digit (into v_tmp)
    ;
putnum_loop:
    incf    v_tmp,f             ; find digit (starts at -1)
    movf    v_num_digit_lo,w
    subwf   v_num_lo,f
    movf    v_num_digit_hi,w
    subwfb  v_num_hi,f
    bc      putnum_loop         ; cross 0?

    ;
    ; add digit back into number 
    ;
    movf    v_num_digit_lo,w
    addwf   v_num_lo,f
    movf    v_num_digit_hi,w
    addwfc  v_num_hi,f

    ;
    ; print a digit
    ;
putnum_sub:
    movf    v_tmp,w
    btfss   STATUS,Z
    bsf     b_num_started   ; if nonzero, it is started
    addlw   '0'
    btfsc   b_num_started
    bra     putc_raw        ; draw digit & return to putnum
    return

;############################################
;############### PUT TIME ###################
;############################################

    ;
    ; draw a time value
    ;   w            - holds hours
    ;   v_ptime_min  - holds minutes
    ;   v_ptime_sec  - holds seconds
    ;
put_time:
    rcall   putnum_w        ; hours
    movlw   ':'
    rcall   putc_raw
    movf    v_ptime_min,w
    rcall   putnum_2d       ; minutes
    movlw   ':'
    rcall   putc_raw
    movf    v_ptime_sec,w
    bra     putnum_2d       ; seconds

    ; DONE - return to caller


;############################################
;############### PARSE NUM ##################
;############################################
    ;
    ; parse decimal number from entry buf 
    ;  put result in v_num_lo, v_num_hi
    ;
parse_dec:
    lfsr    FSR0,BUF_ENTRY
parse_dec_frombuf:
    clrf    v_num_lo
    clrf    v_num_hi
    clrf    v_num_bits

parse_dec_loop:
    ;
    ; get next decimal digit
    ;
    movf    POSTINC0,w
    bz      parse_end
    addlw   LOW -'0'        ; is digit 0-9?
    bn      parse_bad_digit
    movwf   v_tmp           ; store digit in v_tmp
    addlw   LOW -10
    bnn     parse_bad_digit

    ;
    ; multiply by 10
    ;
    rcall   num_mul2
    movff   v_num_lo,v_num_digit_lo
    movff   v_num_hi,v_num_digit_hi
    rcall   num_mul2
    rcall   num_mul2
    rcall   num_add_digit
    ;
    ; add new digit
    ;
    movff   v_tmp,v_num_digit_lo
    clrf    v_num_digit_hi
    rcall   num_add_digit

    bsf     b_num_started
    bra     parse_dec_loop


num_mul2:
    bcf     STATUS,C
    rlcf    v_num_lo,f
    rlcf    v_num_hi,f
num_checkc:
    btfsc   STATUS,C
    bsf     b_num_overflow
    return

num_add_digit:
    movf    v_num_digit_lo,w
    addwf   v_num_lo,f
    movf    v_num_digit_hi,w
    addwfc  v_num_hi,f
    bra     num_checkc

parse_bad_digit:
    bsf     b_num_bad_digit

    ;
    ; end of parse
    ;  b_num_overflow  is set if overflowed
    ;  b_num_bad_digit is set if lat digit was bad
    ;  b_num_started   is set if any good digits were parsed
    ;
parse_end:
    return



This file Copyright (C) 2004 by Nathan (Acorn) Pooley
Go to DRUID Development page
Go to DRUID page
Go to JU Gadgets page
Go to Justice Unlimited homepage
Go to Acorn's personal webpage
Contact Acorn
See comments from others
Post your own comments
File created by do_doc at Wed Aug 4 18:03:35 2004