/*

FILE: spisendreceive.s
DESCRIPTION: SPI block initialization to ~1MHz serial clock, then
  boot from EEPROM. Compressed records are decompressed into memory.

*/

#include <vsrv.h>

#define SPI_WREN  0x06
#define SPI_WRDI  0x04
#define SPI_RDSR  0x05
#define SPI_WRSR  0x01
#define SPI_READ  0x03
#define SPI_WRITE 0x02


	.sect code,spiboot
	.export _SpiSendReceive
_SpiSendReceive:
	ldc SPI0_DATA,i7
	add null,ones,a0 ; STP a0,(i7)+(SPI0_STATUS-SPI0_DATA)	// send data
	// for first round Z = 0
#ifdef PERIP_IN_X
	sty a1,(i6)
#else
	stx a1,(i6)
#endif
	ldc SPI_ST_TXRUNNING,a1
$0:	LDP (i7),a0
	jzc $0			// wait until not running (and RX data ready)
	and a0,a1,a0	// 3-cycle loop, max 3-cycle latency
	ldc SPI0_DATA,i7
#ifdef PERIP_IN_X
	jr
	LDP (i7),a0	; ldy (i6)-1,a1
#else
	jr
	LDP (i7),a0	; ldx (i6)-1,a1
#endif

	.end
