
#include "regdef.h"
#include "asm.h"
#include <R4300.h>
#include <rcp.h>

#define game_start	0x80000400
#define rd_rams		0x80000000	
#define game_size	(0x400000-0x400)

LEAF (Boot)                       # Address = 0xB0000400.
        .set    noreorder
        li      t1, SR_BEV|SR_CU0	# Initialize SR
        mtc0    t1, C0_SR		# Clear Status Register.

        li	t1, CONFIG_BE|CONFIG_UNCACHED		# Disable Cache.
        li	t1, CONFIG_BE|CONFIG_NONCOHRNT		# Enable Cache.
        mtc0	t1, C0_CONFIG       	# Enable Cache in KSeg0.

	mtc0	zero,C0_CAUSE	# initialize cause register


	# Initialize RD-RAM 

	la	t0,PHYS_TO_K1(RI_BASE_REG)
	lui	t2, 0xa3f8
	lui	t3, 0xa3f0
	la	t4, PHYS_TO_K1(MI_BASE_REG)
	ori	t1, zero,  146
	sw	t1, 0x0004(t0)		# Enable RAC auto-calibration control
        				# Delay 4096 RCP clock cycles.
	li	s1,6
wait_rac:
	nop
	addi	s1, 0xffff		# s1 = s1 - 1
	bne	s1,zero,wait_rac
	nop
	sw	zero,  0x0008(t0)	# Load current control regs.
	ori	t1, zero,  0x0014
	sw	t1, 0x000c(t0)		# Set transmit and receive selects
	sw	zero,0(t0)		# Reset all RDRAM devices
					# Delay 100 RCP clock cycle
	li	s1,4
wait_rac1:
	nop
	addi	s1, 0xffff              # s1 = s1 - 1
	bne     s1,zero,wait_rac1
	nop
	ori	t1, zero,  0x000e
	sw	t1,0(t0)		# Enable RDRAM access
	ori	t1, zero, 0x010f
	sw	t1, 0x0000(t4)		# Initialize the delay register
	li	t1, 0x18082838
	sw	t1, 0x0008(t2)			
	lui	t1, 0x8000
	sw	t1, 0x000c(t2)		# Enable RDRAM auto-calibration	
	sw	zero, 0x14(t2)		# Initialize Refrow regs.
	lui	t1, 0x800
	sw	t1, 4(t2)		# move all RDRAMs to top of address
	sw	zero, 0x404(t3)		# Move the first RDRAM to bottom addr
	li	t1, 0x0240c0c0
	sw	t1, 0xc(t3)		# activate the first RDRAM
	sw	t1, 0x40c(t3)		# activate the second RDRAM
	li	t1, 0x2000
	sw	t1, 0(t4)		# determine first RDRAM type
	lw	t5, 0x24(t3)
	li	t1, 0x1000
	sw	t1, 0(t4)
	li	t1, 0x101c0a04
	sw	t1, 0x18(t3)		# select a rasinterval value
	li	t1, 0x2000
	sw	t1, 0(t4)		# determine second RDRAM type
	lw	t5, 0x424(t3)
	li	t1, 0x1000
	sw	t1, 0(t4)
	beqz	t5, 1f
	nop
	li	t1, 0x101c0a04
	sw	t1, 0x418(t3)		# select a rasinterval value
1:
	lw	t0, 0xa0000000		# tickle the first RDRAM
	lw	t0, 0xa0080000
	lw	t0, 0xa0100000
	lw	t0, 0xa0180000
	lw	t0, 0xa0000000
	lw	t0, 0xa0080000
	lw	t0, 0xa0100000
	beqz	t5, done
	nop
	lw	t0, 0xa0180000
	lw	t0, 0xa0200000		# tickle the second RDRAM
	lw	t0, 0xa0280000
	lw	t0, 0xa0300000
	lw	t0, 0xa0380000
	lw	t0, 0xa0200000		
	lw	t0, 0xa0280000
	lw	t0, 0xa0300000
	lw	t0, 0xa0380000
done:
	lui	t2, 0xa470			# Enable refresh
	li	t1, 0x00063634
	sw	t1,  0x0010(t2)

	# Invalidate I cache

	.set	reorder
        la	t0,K0BASE    	
	addu	t1,t0,ICACHE_SIZE
	subu	t1,ICACHE_LINESIZE

        .set    noreorder
	mtc0	zero,C0_TAGLO
	mtc0	zero,C0_TAGHI
init_icache:
        cache   CACH_PI|C_IST,0(t0) 	#  use index invalidate
        bltu    t0,t1,init_icache       #  on entire cache
        addu    t0,ICACHE_LINESIZE

        la	t0,K0BASE    	
	addu	t1,t0,DCACHE_SIZE
	subu	t1,DCACHE_LINESIZE
	
	# Invalidate D cache

init_dcache:
        cache   CACH_PD|C_IST,0(t0)  #  use index (writeback) invalidate
        bltu    t0,t1,init_dcache       #  on entire cache, so we can save
        addu    t0,DCACHE_LINESIZE	#  some cache data for NMI case

        .set    reorder
load_ipl3:
	# relocate the current running program to RD-RAM

	la	t0,block17s		# the beginning of relocated program

	la	t3,pifipl3e		# the end of relocated program

	la	t1,K0_TO_K1(rd_rams)	# load RD-RAM start address for IPL3
send2:
	lw	t5,0(t0)		# load Boot from ROM
	sw	t5,0(t1)		# store Boot to RD-RAM
	addiu	t0,4			# add Boot address
	addiu	t1,4			# add RD-RAM adress
	bltu	t0,t3,send2

	#********<16 Jump to IPL3 in RD-RAM (cache enable)>

	la	t4,rd_rams
	jr	t4

	#*******<17 Load game program to RD-RAM>
block17s:
	# Initialize ROM
	lui	t0,0xb000	# get source address of ROM
	lw	t1,0(t0)	# fetch 32-bit word from ROM address
	la	t4,PHYS_TO_K1(PI_BASE_REG)	#load PI register address

	andi	t0,t1,0xff	#mask except dom1 latency data
	sw	t0,0x14(t4)	#store dom1 latency data to GrE
	srl	t0,t1,0x08	#t0 = t1 >> 8
	sw	t0,0x18(t4)	#store dom1 pulse width to GrE
	srl	t0,t1,0x10	#t0 = t1 >> 16
	sw	t0,0x1c(t4)	#store dom1 page size data to GrE
	srl	t0,t1,0x14	#t0 = t1 >> 20
	sw	t0,0x20(t4)	#store dom1 release data to GrE


 # Send 1M byte game program from cartridge ROM 
 # ($19+game_offset ~ 19+game_offset+0xffffc) to RDRAM (0x80000400 ~ 0x801003fc)
 # We need to reserve the first 1k for exception vector
	
	lui	t3,0xb000		#set source address of cartridge ROM
	lw	t0,0x8(t3)		# get rom address of the game
cart:
	add	t0,t3
	li	t2,0x1fffffff
	and	t0,t2
	sw	t0,PHYS_TO_K1(PI_CART_ADDR_REG)
	la	t1,K0_TO_PHYS(game_start)		# game's boot address
	sw	t1,PHYS_TO_K1(PI_DRAM_ADDR_REG)
	la	t2,game_size			# DMA size
	sw	t2,PHYS_TO_K1(PI_WR_LEN_REG)	# start DMA

	# wait for DMA to finish
	
waitdma:
        .set noreorder
        .repeat 10
        nop
        .endr
        .set reorder
	lw	t3,PHYS_TO_K1(PI_STATUS_REG)
	andi	t3,PI_STATUS_DMA_BUSY
	bnez	t3,waitdma

	# Make sure DMA works
	#get checksum for RDRAM
	li	t2,game_size
	li	t1,game_start
	add	t3,t2,t1
	lui	t4,0xb000		#set source address of cartridge ROM
	lw	t0,0x8(t4)		# get rom address of the game
	add	t4,t0			# the address of rom image
check1:
	lw	t5,0(t1)		
	lw	t6,0(t4)

	# compare data

	bne	t5,t6,fail
	addiu	t1,4			
	addiu	t4,4			
	bltu	t1,t3,check1

 #********<21 Jump to game program in RD-RAM>
	la	t1,game_start
	jr	t1
        .set    noreorder
fail:
	lui	s0,0xb000
1:
	lw	t3,PHYS_TO_K1(PI_STATUS_REG)
	andi	t3,(PI_STATUS_IO_BUSY|PI_STATUS_DMA_BUSY)
	bnez	t3,1b
	sw	t5,0x20(s0)

1:
	lw	t3,PHYS_TO_K1(PI_STATUS_REG)
	andi	t3,(PI_STATUS_IO_BUSY|PI_STATUS_DMA_BUSY)
	bnez	t3,1b
	sw	t6,0x24(s0)

1:
	lw	t3,PHYS_TO_K1(PI_STATUS_REG)
	andi	t3,(PI_STATUS_IO_BUSY|PI_STATUS_DMA_BUSY)
	bnez	t3,1b
	sw	t1,0x28(s0)
1:
	lw	t3,PHYS_TO_K1(PI_STATUS_REG)
	andi	t3,(PI_STATUS_IO_BUSY|PI_STATUS_DMA_BUSY)
	bnez	t3,1b
	sw	t4,0x2c(s0)
loop:
	b	loop

pifipl3e:
	nop
        .set noreorder
        .repeat 11
        nop
        .endr
	li      k0,0xa0102000
	mfc0    k1,C0_CAUSE
	sw      k1,0(k0)
	mfc0    k1,C0_SR
	sw      k1,4(k0)
	mfc0    k1,C0_EPC
	sw      k1,4(k0)
	sw      ra,4(k0)
1:
	b	1b
	nop
END (Boot)
