;
; utility.inc: general utility calls
;
; author: Marko Kanala, raato ]a t[ mulletronic.com
;
; LICENSE
; Creative Commons Attribution-NonCommercial-ShareALike 2.5
;
; You are free:
;
; * to copy, distribute, display, and perform the work
; * to make derivative works
;
; Under the following conditions:
;	
; 1. Attribution. You must attribute the work in the manner specified by the 
;    author or licensor.
; 2. Noncommercial. You may not use this work for commercial purposes.
; 3. Share Alike. If you alter, transform, or build upon this work, you may 
;    distribute the resulting work only under a license identical to this one.
;
; * For any reuse or distribution, you must make clear to others the license 
;   terms of this work.
; * Any of these conditions can be waived if you get permission from the 
;   copyright holder.
;
; Your fair use and other rights are in no way affected by the above.
;
; This is a human-readable summary of the Legal Code.
; See full license at http://creativecommons.org/licenses/by-nc-sa/2.5/legalcode
;
; Copyright: Marko Kanala (raato@mulletronic.com).
;

; decodes the number in W register to a single bit
get_bit:
	addwf	WREG
	addwf	PCL
	retlw	b'10000000'
	retlw	b'01000000'
	retlw	b'00100000'
	retlw	b'00010000'
	retlw	b'00001000'
	retlw	b'00000100'
	retlw	b'00000010'
	retlw	b'00000001'

; decodes step key to name entering character
; note: this function should be called with 
;       step key code - 1 in WREG
get_keyboard_char:
	addwf	WREG
	addwf	PCL
	retlw	"A"			; "abc"
	retlw	"D"			; "def"
	retlw	"G"			; "ghi"
	retlw	"J"			; "jkl"
	retlw	"M"			; "mno"
	retlw	"P"			; "pqrs"
	retlw	"T"			; "tuv"
	retlw	"W"			; "wxyz"
	retlw	"W" + 4		; dummy
	retlw	"9" + 1		; numbers

;
; resolves the scale value from WREG
;  to the number of 48 ppqn ticks
;
get_scaleticks:
	addwf	WREG
	addwf	PCL
	retlw	SCALE_16_TICKS
	retlw	SCALE_32_TICKS
	retlw	SCALE_8TR_TICKS
	retlw	SCALE_16TR_TICKS

;; decodes the keypress to led register
get_ledreg:
	movlw	KEY_NONE
	cpfsgt	keypress
	retlw	0				; return 0 if no step key pressed
	cpfsgt	KEY_S8
	retlw	leds_busa_1		; step key leds 1 - 8 are at bus a_1
	cpfsgt	KEY_S16
	retlw	leds_busa_2		; step key leds 9 - 16 are at bus a_2
	retlw	leds_busb_1		; other key leds are at bus b_1

; decodes the keypress to led register bit	
get_ledbit:
	movlw	1
	subwf	keypress, W

; decodes a step key press to a single bit
;  note: should be called only if sure
;        that keypress == step key
get_stepbit:
	movlw	KEY_S8
	cpfsgt	keypress
	bra		get_stepbit_firsthalf
	; second half
	subwf	keypress, W
	bra		get_stepbit_get
get_stepbit_firsthalf:
	; first half
	movf	keypress, W
get_stepbit_get:
	decf	WREG
	call	get_bit
	return

; decodes a currentstep to a single bit
get_currentbit:
	movlw	8
	cpfsgt	currentstep
	bra		get_currentbit_firsthalf
	; second half
	subwf	currentstep, W
	bra		get_currentbit_get
get_currentbit_firsthalf:
	; first half
	movf	currentstep, W
get_currentbit_get:
	decf	WREG
	call	get_bit
	return


External Labels :

get_bit