	.module Mavric-II.C
	.area data(ram, con, rel)
_ulTimerTick::
	.blkb 4
	.area idata
	.word 0,0
	.area data(ram, con, rel)
_ucTimerFlag::
	.blkb 1
	.area idata
	.byte 1
	.area data(ram, con, rel)
	.area text(rom, con, rel)
	.even
_port_init::
; /*
; **  File:       MAVRIC-II.C
; **
; **  Purpose:    Demo Program in ICCAVR for BDMICO's MAVRIC-II board.
; **              http://www.bdmicro.com
; **
; **  Chip:		ATMEGA128 running at 16MHz
; **
; **  Version:    1.0.0, 3:rd of December 2003
; **
; **  Author:     Lars Wictorsson
; **              LAWICEL / SWEDEN
; **              http://www.lawicel.com   lars@lawicel.com
; **
; **  Copyright:  The copyright to the computer program(s) herein is the
; **              property of LAWICEL HB, Sweden. The program(s) may be used
; **              and/or copied only with the written permission of LAWICEL HB
; **              in accordance with the terms and conditions stipulated in
; **              the agreement/contract under which the program(s) have been
; **              supplied.
; **
; **  Support:    No Support is given on this free demo program.
; **
; **	Remarks:	This program is tested with ICCAVR version 6.29.
; **
; **  History:    2003-12-03  1.0.0   Created (LWI)
; */
; 
; #include <iom128v.h>
; #include <macros.h>
; 
; #define LED_ON      PORTB |= 0x01
; #define LED_OFF     PORTB &= ~0x01
; 
; unsigned long ulTimerTick = 0;
; unsigned char ucTimerFlag = 1;
; 
; //
; // All ports are set to inputs, except PB0 which is set to output and off.
; //
; void port_init(void)
; {
;  PORTA = 0xFF;
	ldi R24,255
	out 0x1b,R24
;  DDRA  = 0x00;
	clr R2
	out 0x1a,R2
;  PORTB = 0xFE;
	ldi R24,254
	out 0x18,R24
;  DDRB  = 0x01;
	ldi R24,1
	out 0x17,R24
;  PORTC = 0xFF; //m103 output only
	ldi R24,255
	out 0x15,R24
;  DDRC  = 0x00;
	out 0x14,R2
;  PORTD = 0xFF;
	out 0x12,R24
;  DDRD  = 0x00;
	out 0x11,R2
;  PORTE = 0xFF;
	out 0x3,R24
;  DDRE  = 0x00;
	out 0x2,R2
;  PORTF = 0xFF;
	sts 98,R24
;  DDRF  = 0x00;
	sts 97,R2
;  PORTG = 0x1F;
	ldi R24,31
	sts 101,R24
;  DDRG  = 0x00;
	sts 100,R2
; }
L1:
	.dbline 0 ; func end
	ret
	.even
_timer1_init::
; 
; // TIMER1 initialisation - prescale:1
; // WGM: 0) Normal, TOP=0xFFFF
; // desired value: 1000Hz
; // actual value: 1000,000Hz (0,0%)
; void timer1_init(void)
; {
;  TCCR1B = 0x00; //stop
	clr R2
	out 0x2e,R2
;  TCNT1H = 0xC1; //setup
	ldi R24,193
	out 0x2d,R24
;  TCNT1L = 0x80;
	ldi R24,128
	out 0x2c,R24
;  OCR1AH = 0x3E;
	ldi R24,62
	out 0x2b,R24
;  OCR1AL = 0x80;
	ldi R24,128
	out 0x2a,R24
;  OCR1BH = 0x3E;
	ldi R24,62
	out 0x29,R24
;  OCR1BL = 0x80;
	ldi R24,128
	out 0x28,R24
;  OCR1CH = 0x3E;
	ldi R24,62
	sts 121,R24
;  OCR1CL = 0x80;
	ldi R24,128
	sts 120,R24
;  ICR1H  = 0x3E;
	ldi R24,62
	out 0x27,R24
;  ICR1L  = 0x80;
	ldi R24,128
	out 0x26,R24
;  TCCR1A = 0x00;
	out 0x2f,R2
;  TCCR1B = 0x01; //start Timer
	ldi R24,1
	out 0x2e,R24
; }
L2:
	.dbline 0 ; func end
	ret
	.area vector(rom, abs)
	.org 56
	jmp _timer1_ovf_isr
	.area text(rom, con, rel)
	.even
_timer1_ovf_isr::
	st -y,R0
	st -y,R1
	st -y,R2
	st -y,R3
	st -y,R4
	st -y,R5
	st -y,R24
	st -y,R30
	in R0,0x3f
	st -y,R0
	xcall push_gset2
; 
; #pragma interrupt_handler timer1_ovf_isr:15
; void timer1_ovf_isr(void)
; {
;  //TIMER1 has overflowed
;  TCNT1H = 0xC1; //reload counter high value
	ldi R24,193
	out 0x2d,R24
;  TCNT1L = 0x80; //reload counter low value
	ldi R24,128
	out 0x2c,R24
;  if (ulTimerTick > 0) {
	ldi R20,0
	ldi R21,0
	ldi R22,0
	ldi R23,0
	lds R4,_ulTimerTick+2
	lds R5,_ulTimerTick+2+1
	lds R2,_ulTimerTick
	lds R3,_ulTimerTick+1
	cp R2,R20
	cpc R3,R21
	cpc R4,R22
	cpc R5,R23
	breq L4
;   ulTimerTick--;
	ldi R20,1
	ldi R21,0
	ldi R22,0
	ldi R23,0
	sub R2,R20
	sbc R3,R21
	sbc R4,R22
	sbc R5,R23
	sts _ulTimerTick+1,R3
	sts _ulTimerTick,R2
	sts _ulTimerTick+2+1,R5
	sts _ulTimerTick+2,R4
;  }
	xjmp L5
L4:
;  else {
;   ucTimerFlag = 1;
	ldi R24,1
	sts _ucTimerFlag,R24
;  }
L5:
; }
L3:
	xcall pop_gset2
	ld R0,y+
	out 0x3f,R0
	ld R30,y+
	ld R24,y+
	ld R5,y+
	ld R4,y+
	ld R3,y+
	ld R2,y+
	ld R1,y+
	ld R0,y+
	.dbline 0 ; func end
	reti
;         ulTime -> y+0
	.even
_WaitMS::
	xcall push_arg4
; 
; //
; // Routine waits n Milliseconds from Timer 1.
; //
; void WaitMS(unsigned long ulTime)
; {
;  CLI();
	cli
;  ulTimerTick = ulTime;
	movw R30,R28
	ldd R2,z+0
	ldd R3,z+1
	ldd R4,z+2
	ldd R5,z+3
	sts _ulTimerTick+1,R3
	sts _ulTimerTick,R2
	sts _ulTimerTick+2+1,R5
	sts _ulTimerTick+2,R4
;  ucTimerFlag = 0;
	clr R2
	sts _ucTimerFlag,R2
;  SEI();
	sei
L7:
L8:
;  while(ucTimerFlag == 0);
	lds R2,_ucTimerFlag
	tst R2
	breq L7
; }
L6:
	adiw R28,4
	.dbline 0 ; func end
	ret
	.even
_uart0_init::
; 
; // UART0 initialisation
; // desired baud rate: 9600
; // actual: baud rate:9615 (0,2%)
; // char size: 8 bit
; // parity: Disabled
; void uart0_init(void)
; {
;  UCSR0B = 0x00; //disable while setting baud rate
	clr R2
	out 0xa,R2
;  UCSR0A = 0x00;
	out 0xb,R2
;  UCSR0C = 0x06;
	ldi R24,6
	sts 149,R24
;  UBRR0L = 0x67; //set baud rate lo
	ldi R24,103
	out 0x9,R24
;  UBRR0H = 0x00; //set baud rate hi
	sts 144,R2
;  UCSR0B = 0x18;
	ldi R24,24
	out 0xa,R24
; }
L10:
	.dbline 0 ; func end
	ret
	.even
_init_devices::
; 
; //call this routine to initialise all peripherals
; void init_devices(void)
; {
;  //stop errant interrupts until set up
;  CLI(); //disable all interrupts
	cli
;  XDIV  = 0x00; //xtal divider
	clr R2
	out 0x3c,R2
;  XMCRA = 0x00; //external memory
	sts 109,R2
;  port_init();
	xcall _port_init
;  timer1_init();
	xcall _timer1_init
;  uart0_init();
	xcall _uart0_init
; 
;  MCUCR = 0x00;
	clr R2
	out 0x35,R2
;  EICRA = 0x00; //extended ext ints
	sts 106,R2
;  EICRB = 0x00; //extended ext ints
	out 0x3a,R2
;  EIMSK = 0x00;
	out 0x39,R2
;  TIMSK = 0x04; //timer interrupt sources
	ldi R24,4
	out 0x37,R24
;  ETIMSK = 0x00; //extended timer interrupt sources
	sts 125,R2
;  SEI(); //re-enable interrupts
	sei
;  //all peripherals are now initialised
; }
L11:
	.dbline 0 ; func end
	ret
	.even
_main::
; 
; //
; void main(void)
; {
;  init_devices();
	xcall _init_devices
;  //insert your functional code here...
;  
;  puts("Testing MAVRIC-II Board from BDMICRO www.bdmicro.com\r");
	ldi R16,<L13
	ldi R17,>L13
	xcall _puts
	xjmp L15
L14:
	sbi 0x18,0
	ldi R16,250
	ldi R17,0
	ldi R18,0
	ldi R19,0
	xcall _WaitMS
	cbi 0x18,0
	ldi R16,238
	ldi R17,2
	ldi R18,0
	ldi R19,0
	xcall _WaitMS
L15:
;  
;  while(1) { // A forever loop
	xjmp L14
X0:
;   LED_ON;
;   WaitMS(250L);
;   LED_OFF;
;   WaitMS(750L);
;  }
; }
L12:
	.dbline 0 ; func end
	ret
	.area data(ram, con, rel)
L13:
	.blkb 54
	.area idata
	.byte 'T,'e,'s,'t,'i,'n,'g,32,'M,'A,'V,'R,'I,'C,45,'I
	.byte 'I,32,'B,'o,'a,'r,'d,32,'f,'r,'o,'m,32,'B,'D,'M
	.byte 'I,'C,'R,'O,32,'w,'w,'w,46,'b,'d,'m,'i,'c,'r,'o
	.byte 46,'c,'o,'m,13,0
	.area data(ram, con, rel)
