/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished proprietary information of BroadOn Communications Corp.,
 *  and are protected by Federal copyright law. They may not be disclosed
 *  to third parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#include <sc/sc.h>
#include <sc/addr_map.h>

/*
 * SC test startup.
 * config is 0, stack setup by boot0;
 * ROM/DA already flipped;
 */
	.text
	.code 32
	.align 0
	.global _start

_start:
	ldr		pc, _Reset
	ldr		pc, _Undef
	ldr		pc, _Swi
	ldr		pc, _InstAbort
	ldr		pc, _DataAbort
	ldr		pc, _Reserved
	ldr		pc, _Irq
	ldr		pc, _Fiq

	.global	_sim
_sim:		.word Sim			// sim structure

_Reset:		.word xReset
_Undef:		.word xUndef
_Swi:		.word xSwi
_InstAbort:	.word xInstAbort
_DataAbort:	.word xDataAbort
_Reserved:	.word xReserved
_Irq:		.word xIrq
_Fiq:		.word xFiq

// write region with 32-bit value;

word_set:
	cmp		r0, r1
	bcs		1f
	str		r2, [r0], r3
	b		word_set
1:
	bx		lr

// reset exception;

xReset:

	// invalidate L1 I and D caches;

	ldr		r0, =4096               // Size of L1 caches
	ldr		r1, =L1_II_START        // L1 I Cache op
	ldr		r2, =L1_DI_START        // L1 D Cache op
	ldr		r3, =1
1:
	str		r3, [r1], #16
	str		r3, [r2], #16
	subs	r0, r0, #16
	bne		1b

	// branch away if on cpu1;

	ldr		r0,=SYS_SMP_REG
	ldr		r1, [r0], #0
	cmp		r1, #0
	bge		2f						// branch if cpu0

	ldr		r0, =main1
	ldr		sp, =stack1+1024
	mov		lr, pc
	bx		r0
1:	b		1b

	// make text segment read/execute;

2:	ldr		r0, =(DAX_START+__text_start)
	ldr		r1, =(DAX_START+__text_end)
	ldr		r2, =(PT_ACCESS_EXEC << PT_SC_PROT_SHIFT)
	ldr		r3, =16
	bl		word_set

	// make rodata segment read-only;

	ldr		r0, =(DAX_START+__rodata_start)
	ldr		r1, =(DAX_START+__rodata_end)
	ldr		r2, =(PT_ACCESS_READ << PT_SC_PROT_SHIFT)
	bl		word_set

	// make data segment r/w;

	ldr		r0, =(DAX_START+__data_start)
	ldr		r1, =(DAX_START+__data_end)
	ldr		r2, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
	bl		word_set

	// make bss segment r/w;
	// stacks are part of bss segment;

	ldr		r0, =(DAX_START+__bss_start)
	ldr		r1, =(DAX_START+__bss_end)
	ldr		r2, =(PT_ACCESS_RW << PT_SC_PROT_SHIFT)
	bl		word_set

	// zero bss;

	ldr		r0, =__bss_start
	ldr		r1, =__bss_end
	ldr		r2, =0
	ldr		r3, =4
	bl		word_set

	// jump to C code;
	// spin if it returns;

	ldr		r0, =main0
	ldr		sp, =stack0+1024
	mov		lr, pc
	bx		r0
1:	b		1b

// other exceptions;

xUndef:
xSwi:
xInstAbort:
xDataAbort:
xReserved:
xIrq:
xFiq:
1:
	b		1b

    .end

