; 21 July 97 chetan
; Duplication of reset and Reset
; Now calling Reset from reset
; See comments NEW_BOOT

	include ..\devdrvrs\include\a360def.h

;C calling convention : write_to_flash(char *src, char *dest, int size)

	xdef	_write_to_flash
_write_to_flash:
	movem.l	a4,-(a7)	;save a4
	move.l	8(a7),a0	;get source pointer into a0
	move.l	12(a7),a4	;get destination pointer into a4
	move.l	16(a7),d0	;get count into d0
	beq	MoveOver	;exit if count is 0
	subq.l	#1,d0		;count-- to take care of dbf
	move.w	sr,-(a7)	;store sr before we disable interrupts
	ori.w	#$700,sr

; The protect sequence to enable writing 1 Flash page is specified by offsets
; from the base address. Simply writing to 5555 or 2aaa fails when a write
; is attempted to the 2nd Flash

        move.l a1,-(a7)       ; Save register a1
        move.l a2,-(a7)       ; Save register a2
        
        move.l d0,-(a7)       ; Save d0
        move.l a4,d0          ; Take destination pointer in d0
        andi.l #$FFF80000,d0  ; Get base address in d0
        move.l d0,a1          ; Move base address to a1
        move.l d0,a2          ; Move base address to a2
        adda.l #$5555,a1      ; Get one address to a1
        adda.l #$2AAA,a2      ; Get the other address to a2
        move.l (a7)+,d0       ; Restore d0

	move.b	#$aa,(a1)     ;protect sequence to enable writing 1 page
	move.b	#$55,(a2)
	move.b	#$a0,(a1)

        move.l (a7)+,a2       ; Restore register a2
        move.l (a7)+,a1       ; Restore register a1

MoveAgain:			;actual writing takes place here
	move.b	(a0)+,(a4)+	;move the data into dest
	dbf	d0,MoveAgain	;use DBcc with FFFC=-4 to enter loop mode

;interrupts must be disabled till the programming is completed
	move.l	#$FFFF,d0
Wait:
	dbf	d0,Wait
	
	move.w	(a7)+,sr	;restore sr

MoveOver:
	movem.l	(a7)+,a4	;restore a4
	rts

; This is different from write_to_flash in the sense that interrupts are
; disabled only when loading the device. There is no 10ms wait.


	xdef	_new_write_to_flash
_new_write_to_flash:
	movem.l	a4,-(a7)	;save a4
	move.l	8(a7),a0	;get source pointer into a0
	move.l	12(a7),a4	;get destination pointer into a4
	move.l	16(a7),d0	;get count into d0
	beq	move_over      ;exit if count is 0
	subq.l	#1,d0		;count-- to take care of dbf
	move.w	sr,-(a7)	;store sr before we disable interrupts
	ori.w	#$700,sr

; The protect sequence to enable writing 1 Flash page is specified by offsets
; from the base address. Simply writing to 5555 or 2aaa fails when a write
; is attempted to the 2nd Flash

        move.l a1,-(a7)       ; Save register a1
        move.l a2,-(a7)       ; Save register a2
        
        move.l d0,-(a7)       ; Save d0
        move.l a4,d0          ; Take destination pointer in d0
        andi.l #$FFF80000,d0  ; Get base address in d0
        move.l d0,a1          ; Move base address to a1
        move.l d0,a2          ; Move base address to a2
        adda.l #$5555,a1      ; Get one address to a1
        adda.l #$2AAA,a2      ; Get the other address to a2
        move.l (a7)+,d0       ; Restore d0

	move.b	#$aa,(a1)     ;protect sequence to enable writing 1 page
	move.b	#$55,(a2)
	move.b	#$a0,(a1)

        move.l (a7)+,a2       ; Restore register a2
        move.l (a7)+,a1       ; Restore register a1

move_again:             ;actual writing takes place here
	move.b	(a0)+,(a4)+	;move the data into dest
	dbf	d0,move_again  ;use DBcc with FFFC=-4 to enter loop mode

	move.w	(a7)+,sr	;restore sr

move_over:
	movem.l	(a7)+,a4	;restore a4
	rts


	xref _Reset	
	xdef	_reset
_reset:
	jsr _Reset
	rts

;----------------------------------------------------------------------------
;	function	_asmSectorErase
; C calling convention is SectorErase(char *src)
; return a non-zero value if it could erase the sector

	xdef _asmSectorErase
_asmSectorErase:
	move.l	a1,-(a7)
	move.l	8(a7),a1	;get sector address into a1
	addq.l	#1,a1		;write at proper place
	move.w	sr,-(a7)	;store sr before we disable interrupts
	ori.w	#$700,sr	;disable interrupts

	;construct a "trampoline-subroutine" on the stack
	suba.l	#100,a7			;make space on stack

	move.l	#$13fc00aa,(a7)
	move.l	#$0000aaab,4(a7)	;MOVE.B	#$aa,$aaab

	move.l	#$11fc0055,8(a7)
	move.w	#$5555,12(a7)		;MOVE.B	#$55,$5555

	move.l	#$13fc0080,14(a7)
	move.l	#$0000aaab,18(a7)	;MOVE.B	#$80,$aaab

	move.l	#$13fc00aa,22(a7)
	move.l	#$0000aaab,26(a7)	;MOVE.B	#$aa,$aaab

	move.l	#$11fc0055,30(a7)
	move.w	#$5555,34(a7)		;MOVE.B	#$55,$5555

	move.l	#$12bc0030,36(a7)	;MOVE.B	#$30,(A1)

	; sit back for erase cycle to complete

;-----  Data Polling with loop limit count to see end of erase
	move.w	#$203c,40(a7)		; move.l #$count,d0
	move.l	#$000FFFFF,42(a7)	;

	move.w	#$1211,46(a7)		;move.b (a1),d1
	move.l	#$b23c00ff,48(a7)	;cmp.b #$ff,d1

	move.w	#$6708,52(a7)		;beq <return>

	move.w	#$90BC,54(a7)		; sub.l #$1,d0
	move.l	#$00000001,56(a7)

	move.w  #$66F0,60(a7)		; bne to previous instruction
;------
	move.w	#$4E75,62(a7)		; rts

	; Code assembly on stack over. Now trampoline jump into stack
	jsr (a7)

	adda.l #100,a7		; fix up stack

	move.w	(a7)+,sr
	move.l	(a7)+,a1
	rts

;----------------------------------------------------------------------------
;	function	asmCopyToSector
;	asmCopyToSector(word *SectorAddress, word *Source, dword length)
;

	xdef	_asmCopyToSector
_asmCopyToSector:
	move.l	a1,-(a7)
	move.l	8(a7),a1	;get dest pointer into a1
	move.l	12(a7),a0	;source pointer in a0
	move.l	16(a7),d0	;count in d0
	
	move.w	sr,-(a7)	;store sr before we disable interrupts
	ori.w	#$700,sr	;disable interrupts

	;construct a "trampoline-subroutine" on the stack
	suba.l	#100,a7			;make space on stack

	;check if count has expired, exit if expired
; <begin loop>
	move.w	#$4a80,(a7)		;tst.l	d0

;BEWARE-- THERE IS AN OFFSET IN THIS instruction-
	move.w	#$6726,2(a7)		;beq	<over loop>

	;enable one write cycle
	move.l	#$13fc00aa,4(a7)
	move.l	#$0000aaab,8(a7)	;MOVE.B	#$aa,$aaab

	move.l	#$11fc0055,12(a7)
	move.w	#$5555,16(a7)		;MOVE.B	#$55,$5555

	move.l	#$13fc00A0,18(a7)
	move.l	#$0000aaab,22(a7)	;MOVE.B	#$A0,$aaab

	;move the word
	move.w	#$3218,26(a7)		;move.w (a0)+,d1
	move.w	#$3281,28(a7)		;move.w d1,(a1)

	;poll for write completion
	move.w	#$b251,30(a7)		;cmp.w (a1),d1
	move.w	#$66fc,32(a7)		;bne <to compare>

	;decrement d0 and increment a1 by two (a0 allready incremented)
	move.w	#$5580,34(a7)		;subq.l #$2,d0
	move.l	#$d2fc0002,36(a7)	;adda.w #$2,a1

	;branch back to see if all bytes copied
;BEWARE-- THERE IS AN OFFSET IN THIS instruction-
	move.w	#$60d6,40(a7)		;bra <begin loop>

;<over loop>
	move.w	#$4E75,42(a7)		; rts

	; Code assembly on stack over. Now trampoline jump into stack
	jsr (a7)

	adda.l #100,a7		; fix up stack

	move.w	(a7)+,sr
	move.l	(a7)+,a1
	rts

END

;--------------------------- last line ---------------------------------
