// ********************************************************************
// Actel Corporation Proprietary and Confidential
//  Copyright 2010 Actel Corporation.  All rights reserved.
//
// ANY USE OR REDISTRIBUTION IN PART OR IN WHOLE MUST BE HANDLED IN
// ACCORDANCE WITH THE ACTEL LICENSE AGREEMENT AND MUST BE APPROVED
// IN ADVANCE IN WRITING.
//
// Description:	User testbench APB3 master BFM script for CoreAPB3
//
// Revision Information:
// Date     Description
// 05Feb10		Production Release Version 3.0
//
// SVN Revision Information:
// SVN $Revision: 12087 $
// SVN $Date: 2010-02-03 20:00:28 -0800 (Wed, 03 Feb 2010) $
//
// Resolved SARs
// SAR      Date     Who   Description
//
// Notes:
// 1. best viewed with tabstops set to "4"
// 2. Most of the behavior is driven from the BFM script for the APB master.
//    Consult the Actel AMBA BFM documentation for more information.
// 3. All procedures, variables, and constants used by the 'main' procedure
//    are declared in the include file "coreapb3_usertb_include.bfm"
//
// History:		1/29/10  - TFB created
//
// *********************************************************************

// include constants, and miscellaneous procedures used in this main file
include "coreapb3_usertb_include.bfm"


procedure main
	int i
	int j
	int k
	int dtmp[4]
	int dmask[4]
	int atmp
	int cond[2]

	header "User Testbench for CoreAPB3: BFM APB Master Test Harness"
	print "(c) Copyright 2010 Actel Corporation. All rights reserved."
	call pr_underscores

	debug 1			// only text strings printed
	timeout 2000	// timeout in cycles, in case BFM stalls
	wait 1

	// initialize local variables passed from testbench HDL to the
	// ARGVALUE* BFM parameters
	call init_parameter_vars

	wait 2

# test writes and reads for all enabled slots for direct addressing mode
set cond[0] IADDR_ENABLE == 0
if cond[0]
	call pr_underscores
	print "Direct Addressing (Legacy mode) testing beginning at: %0d ns." $TIME
	call pr_underscores
	loop i 0 15 1
		if APBSLOTENABLE[i]
			call pr_underscores
			print "Master testing APB3 slot:%0d at: %0d ns." i $TIME
			call pr_underscores
			// write/read byte, halfword or word, depending on APB_DWIDTH
			case APB_DWIDTH
			// APB_DWIDTH=8
			when 8
				// write/read 8-bit bytes
				set dtmp[0] i + 0 ^ 0xff
				set dtmp[1] i + 1 ^ 0xff
				set dtmp[2] i + 2 ^ 0xff
				set dtmp[3] i + 3 ^ 0xff
				loop j 0 3 1
					set k j * 4
					set atmp i * RANGESIZE + k
					print "Master writes:%02x to APB3 slave:%0d at: %0d ns." dtmp[j] i $TIME
					write b BASE atmp dtmp[j]
				endloop
				loop j 0 3 1
					set k j * 4
					set atmp i * RANGESIZE + k
					print "Master verifying (reading):%02x from APB3 slave:%0d at: %0d ns." dtmp[j] i $TIME
					readcheck b BASE atmp dtmp[j]
				endloop
			// APB_DWIDTH=16
			when 16
				// write/read 16-bit halfwords
				set dtmp[0] 1 << i ^ 0xffff
				set dtmp[1] 1 << i
				loop j 0 1 1
					set k j * 4
					set atmp i * RANGESIZE + k
					print "Master writes:%04x to APB3 slave:%0d at: %0d ns." dtmp[j] i $TIME
					write h BASE atmp dtmp[j]
				endloop
				loop j 0 1 1
					set k j * 4
					set atmp i * RANGESIZE + k
					print "Master verifying (reading):%04x from APB3 slave:%0d at: %0d ns." dtmp[j] i $TIME
					readcheck h BASE atmp dtmp[j]
				endloop
			// APB_DWIDTH=32
			default
				// write/read 32-bit word
				set atmp i * RANGESIZE + 0x0
				set dtmp[0] 0x00010001 << i ^ 0xffff0000
				print "Master writes:%08x to APB3 slave:%0d at: %0d ns." dtmp[0] i $TIME
				write w BASE atmp dtmp[0]
				print "Master verifying (reading):%08x from APB3 slave:%0d at: %0d ns." dtmp[0] i $TIME
				readcheck w BASE atmp dtmp[0]
				wait 8
			endcase
			print "Master done testing APB3 slot:%0d at: %0d ns." i $TIME
			call pr_underscores
		else
			call pr_underscores
			print "Master 0 skipping test of APB3 slot:%0d at: %0d ns." i $TIME
			call pr_underscores
		endif
	endloop
endif

# test writes and reads for all enabled slots for indirect addressing mode
set cond[0] IADDR_ENABLE == 1
if cond[0]
	call pr_underscores
	print "Indirect Addressing testing beginning at: %0d ns." $TIME
	call pr_underscores

	if APBSLOTENABLE[0]
		call pr_underscores
		print "Master testing APB3 slot:0 at: %0d ns." $TIME
		call pr_underscores
		// write/read byte, halfword or word, depending on APB_DWIDTH
		case APB_DWIDTH
		// APB_DWIDTH=8
		when 8
			// set base address of peripheral 0 to 0x76543210
			// lower log2(RANGESIZE) bits ignored
			set dtmp[0] 0x10
			set dtmp[1] 0x32
			set dtmp[2] 0x54
			set dtmp[3] 0x76

			set dmask[0] RANGESIZE - 1 ^ 0xffffffff
			set dmask[1] dmask[0] >> 8
			set dmask[1] dmask[1] & 0xff
			set dmask[2] dmask[0] >> 16
			set dmask[2] dmask[2] & 0xff
			set dmask[3] dmask[0] >> 24
			set dmask[3] dmask[3] & 0xff
			set dmask[0] dmask[0] & 0xff

			set dtmp[0] dtmp[0] & dmask[0]
			set dtmp[1] dtmp[1] & dmask[1]
			set dtmp[2] dtmp[2] & dmask[2]
			set dtmp[3] dtmp[3] & dmask[3]

			loop j 0 3 1
				set k j * 4
				set atmp 1 * RANGESIZE + k
				print "Master writes:%02x to APB3 address:%08x at: %0d ns." dtmp[j] atmp $TIME
				write b BASE atmp dtmp[j]
			endloop
			loop j 0 3 1
				set k j * 4
				set atmp 1 * RANGESIZE + k
				print "Master verifying (reading):%02x from APB3 address:%08x at: %0d ns." dtmp[j] atmp $TIME
				readcheck b BASE atmp dtmp[j]
			endloop
			// write/read 8-bit bytes to peripheral address 0x76543210
			set dtmp[0] 0xef
			set dtmp[1] 0xbe
			set dtmp[2] 0xad
			set dtmp[3] 0xde
			loop j 0 3 1
				set k j * 4
				set atmp 0 * RANGESIZE + k
				print "Master writes:%02x to APB3 slave:0, address:%08x at: %0d ns." dtmp[j] atmp $TIME
				write b BASE atmp dtmp[j]
			endloop
			loop j 0 3 1
				set k j * 4
				set atmp 0 * RANGESIZE + k
				print "Master verifying (reading):%02x from APB3 slave:0, address:%08x at: %0d ns." dtmp[j] atmp $TIME
				readcheck b BASE atmp dtmp[j]
			endloop
		// APB_DWIDTH=16
		when 16
			// set base address of peripheral 0 to 0x76543210
			// lower log2(RANGESIZE) bits ignored
			set dtmp[0] 0x3210
			set dtmp[1] 0x7654

			set dmask[0] RANGESIZE - 1 ^ 0xffffffff
			set dmask[1] dmask[0] >> 16
			set dmask[0] dmask[0] & 0xffff

			set dtmp[0] dtmp[0] & dmask[0]
			set dtmp[1] dtmp[1] & dmask[1]

			loop j 0 1 1
				set k j * 4
				set atmp 1 * RANGESIZE + k
				print "Master writes:%04x to APB3 address:%08x at: %0d ns." dtmp[j] atmp $TIME
				write h BASE atmp dtmp[j]
			endloop
			loop j 0 1 1
				set k j * 4
				set atmp 1 * RANGESIZE + k
				print "Master verifying (reading):%04x from APB3 address:%08x at: %0d ns." dtmp[j] atmp $TIME
				readcheck h BASE atmp dtmp[j]
			endloop
			// write/read 16-bit halfwords to peripheral address 0x76543210
			set dtmp[0] 0xbeef
			set dtmp[1] 0xdead
			loop j 0 1 1
				set k j * 4
				set atmp 0 * RANGESIZE + k
				print "Master writes:%04x to APB3 slave:0, address:%08x at: %0d ns." dtmp[j] atmp $TIME
				write h BASE atmp dtmp[j]
			endloop
			loop j 0 1 1
				set k j * 4
				set atmp 0 * RANGESIZE + k
				print "Master verifying (reading):%04x from APB3 slave:0, address:%08x at: %0d ns." dtmp[j] atmp $TIME
				readcheck h BASE atmp dtmp[j]
			endloop
		// APB_DWIDTH=32
		default
			// set base address of peripheral 0 to 0x76543210
			// lower log2(RANGESIZE) bits ignored
			set dtmp[0] 0x76543210
			set dmask[0] RANGESIZE - 1 ^ 0xffffffff
			set dtmp[0] dtmp[0] & dmask[0]
			set atmp 1 * RANGESIZE + 0x0
			print "Master writes:%08x to APB3 address:%08x at: %0d ns." dtmp[0] atmp $TIME
			write w BASE atmp dtmp[0]
			print "Master verifying (reading):%08x from APB3 address:%08x at: %0d ns." dtmp[0] atmp $TIME
			readcheck w BASE atmp dtmp[0]

			// write/read 32-bit word to peripheral address 0x76543210
			set dtmp[0] 0xdeadbeef
			set atmp 0 * RANGESIZE + 0x0
			print "Master writes:%08x to APB3 slave:0, address:%08x at: %0d ns." dtmp[0] atmp $TIME
			write w BASE atmp dtmp[0]
			print "Master verifying (reading):%08x from APB3 slave:0, address:%08x at: %0d ns." dtmp[0] atmp $TIME
			readcheck w BASE atmp dtmp[0]
		endcase
		print "Master done testing APB3 slot:0 at: %0d ns." $TIME
		call pr_underscores
	else
		call pr_underscores
		print "Master 0 skipping test of APB3 slot:0 at: %0d ns." $TIME
		call pr_underscores
	endif

	loop i 2 15 1
		if APBSLOTENABLE[i]
			call pr_underscores
			print "Master testing APB3 slot:%0d at: %0d ns." i $TIME
			call pr_underscores
			// write/read byte, halfword or word, depending on APB_DWIDTH
			case APB_DWIDTH
			// APB_DWIDTH=8
			when 8
				// write/read 8-bit bytes
				set dtmp[0] i + 0 ^ 0xff
				set dtmp[1] i + 1 ^ 0xff
				set dtmp[2] i + 2 ^ 0xff
				set dtmp[3] i + 3 ^ 0xff
				loop j 0 3 1
					set k j * 4
					set atmp i * RANGESIZE + k
					print "Master writes:%02x to APB3 slave:%0d at: %0d ns." dtmp[j] i $TIME
					write b BASE atmp dtmp[j]
				endloop
				loop j 0 3 1
					set k j * 4
					set atmp i * RANGESIZE + k
					print "Master verifying (reading):%02x from APB3 slave:%0d at: %0d ns." dtmp[j] i $TIME
					readcheck b BASE atmp dtmp[j]
				endloop
			// APB_DWIDTH=16
			when 16
				// write/read 16-bit halfwords
				set dtmp[0] 1 << i ^ 0xffff
				set dtmp[1] 1 << i
				loop j 0 1 1
					set k j * 4
					set atmp i * RANGESIZE + k
					print "Master writes:%04x to APB3 slave:%0d at: %0d ns." dtmp[j] i $TIME
					write h BASE atmp dtmp[j]
				endloop
				loop j 0 1 1
					set k j * 4
					set atmp i * RANGESIZE + k
					print "Master verifying (reading):%04x from APB3 slave:%0d at: %0d ns." dtmp[j] i $TIME
					readcheck h BASE atmp dtmp[j]
				endloop
			// APB_DWIDTH=32
			default
				// write/read 32-bit word
				set atmp i * RANGESIZE + 0x0
				set dtmp[0] 0x00010001 << i ^ 0xffff0000
				print "Master writes:%08x to APB3 slave:%0d at: %0d ns." dtmp[0] i $TIME
				write w BASE atmp dtmp[0]
				print "Master verifying (reading):%08x from APB3 slave:%0d at: %0d ns." dtmp[0] i $TIME
				readcheck w BASE atmp dtmp[0]
				wait 8
			endcase
			print "Master done testing APB3 slot:%0d at: %0d ns." i $TIME
			call pr_underscores
		else
			call pr_underscores
			print "Master 0 skipping test of APB3 slot:%0d at: %0d ns." i $TIME
			call pr_underscores
		endif
	endloop
endif

return

