! Hellow world program for SH7045F(Alpha Project)
! Satofumi KAMIMURA
! $I$

	.global start
	.text
! main rutine
	nop
start:
	bsr	sci_init
	nop

	! print "READY.\r\n"
	bsr	put1ch
	mov	#'H', r0
	bsr	put1ch
	mov	#'e', r0
	bsr	put1ch
	mov	#'l', r0
	bsr	put1ch
	mov	#'l', r0
	bsr	put1ch
	mov	#'o', r0
	bsr	put1ch
	mov	#'.', r0
	bsr	put1ch
	mov	#'\r', r0
	bsr	put1ch
	mov	#'\n', r0

	! infinity loop
	nop
inf_loop:
	bra	inf_loop
	nop
	
! Put a character
	nop
put1ch:
	mov	r0, r3
	mov.l	SSR1, r4	! while (!(SSR & 0x80)) !
put1ch_begin:	
	mov.b	@r4, r0
	and	#0x80, r0
	cmp/pl  r0
	bf	put1ch_begin

	mov.l	TDR1, r2	! TDR = r0
	mov.b	r3, @r2
	
!	mov.l	SSR1, r4	! SSR &= ~0x80
	mov.b	@r4, r0
	and	#~0x80, r0
	mov.b	r0, @r4
	
	rts
	nop

	.align 4
TDR1:		.long	0xffff81b3
!SSR1:		.long	0xffff81b4

	.text
! SCI1 Initialize
	nop
sci_init:
	mov.l	PACRL2, r2	! enable RxD1, TxD1
	mov.w	PACRL2_D, r0
	mov.w	r0, @r2

	mov.l	SMR1, r2	! 38400bps, 8bit, 1stop bit, no parity
	mov	#0, r0
	mov.b	r0, @r2

	mov.l	BRR1, r2	! set BBR
!	mov	#22, r0
	mov	#7, r0
	mov.b	r0, @r2

	! wait 1bit time

	mov.l	SCR1, r2	! TE = 1, RE = 1
	mov	#0x30, r0
	mov.b	r0, @r2
	
	mov.l	SSR1, r2	! SSR = 0x80
	mov.b	@r2, r0
	mov	#0x80, r0
	mov.b	r0, @r2

	rts
	nop

	.align 4
PACRL2:		.long	0xffff838e
SMR1:		.long	0xffff81b0
BRR1:		.long	0xffff81b1
SCR1:		.long	0xffff81b2
SSR1:		.long	0xffff81b4
	
	.align 2
PACRL2_D:	.word	0x0140
