;
; "Mullet Rhythm 9090"
; Roland X0X style sequencer project
;
; 40MHz 18F4x2 PIC
;
; see http://www.mulletronic.com/diy/mr9090/
;
; Ports:
;   RA0:       tempo pot, a/d conversion
;   RA1:       key circuit bus b, input
;   RA2 - RA3: trigger out, outputs
;   RA4:       lcd display bus, enable output, 10k pull-up
;   RA5:       key circuit bus a, input
;	RB0 - RB7: trigger out / lcd display bus, outputs
;   RC1:       24ppqn start/stop in
;   RC2:       24ppqn clock in (uses capture1 interrupt for detection)
;   RC3 - RC4: flash memory bus, i2c
;   RC5:       24ppqn sync out (connecter to tr-9090 RB7)
;   RC6:       midi output pin 5, output?
;   RC7:       midi input, input
;   RE0:       led circuit bus a, output (active low)
;   RE1:       led circuit bus b, output (active low)
;	RE2:       pattern led display mux inhibit, output
;   RD0 - RD3: pattern led display mux / key+led circuit mux, outputs
;   RD4 - RD7: pattern led display second mux, outputs
;
; FSR0 - dedicated to low priority interrupt code
; FSR1 - dedicated to busy loop code
; FSR2 - dedicated to high priority interrupt code
; TBLPTR - shared
;
; timer 0 - din sync output pulse width timer
; timer 1 - internal clock timer (48ppqn)
; timer 2 - key/led multiplexer timer
; timer 3 - trigger output pulse width timer
;
; 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).
;

	LIST P=PIC18F442, R=DEC
#include <p18f442.inc>
BEGIN

; our microcontroller configuration bits
#include <config.h>

; our macros
#include <xoxmacros.h>

; our data variables & constants
#include <datavariables.h>
#include <constants.h>

; our global application settings
#include <_settings.h>

; see bootloader.asm
#define BOOTLOADER	0x3F00

	org 0x0000
;	goto	start
	goto 	BOOTLOADER	
	org 0x0004
	goto 	start	
	org	0x0008 
	goto 	hp_irq
	org	0x0018 
	goto 	lp_irq

#include <utility.inc>

start:
	; call startup initialization
	call	startupinit

	; default to play mode
	SETMODE	MODE_PPLAY
	call	mode_pattplay_enter
mainloop:
	; loop and call the appropriate mode function
	clrwdt
	IFEVENT		EVENT_SYSEX, main_sysex

	; note: settings a special mode when none of the
	;       mode bits are set
	movf		mode, W
	bnz			mainloop_notsettings
	call		mode_settings

mainloop_notsettings:
	IFMODECALL	MODE_PPLAY, mode_pattplay
	IFMODECALL	MODE_PWRITE, mode_pattwrite
	IFMODECALL	MODE_TPLAY, mode_trackplay
	IFMODECALL	MODE_TWRITE, mode_trackwrite
	bra 	mainloop

; call midi_receiving_sysex in continuous loop
;  while receiving sysex
main_sysex:
	clrwdt
	call	midi_receiving_sysex
	IFNOEVENT	EVENT_SYSEX, mainloop
	bra		main_sysex

; our sysex id
sysex_id:	db	0x71, 0x55, 0x17

; include the implementation
#include <startupinit.inc>
#include <irq_hipri.inc>
#include <irq_lopri.inc>
#include <keysleds.inc>
#include <patterndisplay.inc>
#include <mode_pattwrite.inc>
#include <mode_pattplay.inc>
#include <mode_trawrite.inc>
#include <mode_traplay.inc>
#include <mode_settings.inc>
#include <player.inc>
#include <adc.inc>
#include <lcd.inc>
#include <i2c.inc>
#include <bpmtable.inc>
#include <lcdtexts.inc>
#include <9090.inc>
#include <startupreset.inc>
#include <midi.inc>
#include <din.inc>
#include <copypasteetc.inc>
#include <keyboard.inc>
#include <eeprom.inc>

#include <bootloader.inc>
	END

External Labels :

BOOTLOADER
start
hp_irq
lp_irq
startupinit
mode_pattplay_enter
mode_settings
midi_receiving_sysex