;
; startupreset.inc: resets the flash memory if
;                   requested
;
; 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).
;

check_startup_reset:
	; keypress should be caught during the intro
	;  texts
	KEYEVENT	KEY_START, startup_reset
	return

startup_reset:
	CLEAREVENT	EVENT_KEY
	TABLE_ADDR	text_resetmem
	call	lcd_send_text		
	TABLE_ADDR	text_resetmem2
	call	lcd_send_text		
	; wait for Accent/Fill/Enter key
startup_reset_waitkey:
	clrwdt
	KEYEVENT	KEY_ENTER, startup_reset_continue
	bra 	startup_reset_waitkey
startup_reset_continue:
	TABLE_ADDR	text_resetmem3
	call	lcd_send_text		
	TABLE_ADDR	text_resetmem4
	call	lcd_send_text		
		
	; prepare the data to be written
	lfsr	FSR1, sysexdata
	movlw	"E"
	movwf	POSTINC1
	movlw	"m"
	movwf	POSTINC1
	movlw	"p"
	movwf	POSTINC1
	movlw	"t"
	movwf	POSTINC1
	movlw	"y"
	movwf	POSTINC1
	movlw	" "
	movwf	POSTINC1
	movwf	POSTINC1
	movwf	POSTINC1
	movwf	POSTINC1
	movwf	POSTINC1
	movwf	POSTINC1
	movwf	POSTINC1
	movlw	15
	movwf	POSTINC1		; scale = 0, length = 16
	clrf	POSTINC1		; shuffle = 0, flam = 0
	movlw	24 + 22 + 22	; 12 triggers, 11 accents, 11 flams
startup_reset_loop1:
	clrf	POSTINC1		; clear triggers, accents, flams
	decfsz	WREG
	bra		startup_reset_loop1
	movlw	255				; set instrument pattern lengths to 16
	movwf	POSTINC1		; bd & sd
	movwf	POSTINC1		; lt & mt
	movwf	POSTINC1		; ht & rs
	movwf	POSTINC1		; hc & chh
	movwf	POSTINC1		; ohh & cc
	movwf	POSTINC1		; rc & acc
	; proceed to fill pattern
	movlw	15
	movwf	POSTINC1		; scale = 0, length = 16
	clrf	POSTINC1		; shuffle = 0, flam = 0
	movlw	24 + 22 + 22	; 12 triggers, 11 accents, 11 flams
startup_reset_loop2:
	clrf	POSTINC1		; clear fill triggers, accents, flams
	decfsz	WREG
	bra		startup_reset_loop2
	movlw	255				; set instrument pattern lengths to 16
	movwf	POSTINC1		; bd & sd
	movwf	POSTINC1		; lt & mt
	movwf	POSTINC1		; ht & rs
	movwf	POSTINC1		; hc & chh
	movwf	POSTINC1		; ohh & cc
	movwf	POSTINC1		; rc & acc

	movlw	78
startup_reset_measure_loop:
	clrf	POSTINC1		; clear 78 measures
	decfsz	WREG
	bra		startup_reset_measure_loop
	; 12 bytes of track name
	movlw	"E"
	movwf	POSTINC1
	movlw	"m"
	movwf	POSTINC1
	movlw	"p"
	movwf	POSTINC1
	movlw	"t"
	movwf	POSTINC1
	movlw	"y"
	movwf	POSTINC1
	movlw	" "
	movwf	POSTINC1
	movlw	"t"
	movwf	POSTINC1
	movlw	"r"
	movwf	POSTINC1
	movlw	"a"
	movwf	POSTINC1
	movlw	"c"
	movwf	POSTINC1
	movlw	"k"
	movwf	POSTINC1
	movlw	" "
	movwf	POSTINC1
	; track length
	clrf	POSTINC1
	movlw	1
	movwf	POSTINC1

	; write 64 dummy pages to flash memory
	clrf	currentbank
	clrf	currentpattern
startup_reset_loop:
	movlw	0
	call	i2c_flash_save_sysex_128		; write ...
	movlw	128
	call	i2c_flash_save_sysex_128		;  ... a page
	incf	currentpattern					; page += 1
	movlw	16
	cpfseq	currentpattern					; loop 16 patterns
	bra		startup_reset_loop
	clrf	currentpattern
	incf	currentbank
	movlw	4
	cpfseq	currentbank						; ... per bank
	bra		startup_reset_loop
	CLEAREVENT	EVENT_KEY
	CLEARMODE	MODE_RUN
	clrf	currentbank
	clrf	currentpattern

	; reset settings
	movlw	default_settings
	movwf	settings
	movlw	default_midi_channel
	movwf	settings_midi_channel
	call	s_save_settings

	return

External Labels :

lcd_send_text
i2c_flash_save_sysex_128
s_save_settings