BS2SX - Use of the Scratchpad RAM

Peter H. Anderson, Baltimore, MD, Nov, '98

Introduction.

Both the BS2 and BS2SX provide 26 bytes of user RAM which may be directly referenced.

The BS2SX provides an additional 63 bytes of scratchpad RAM and this may be a big plus, depending on your application. It isn't quite as easy to manipulate, but certainly workable. A few of the 26 bytes of RAM which may be directly referenced are required in manipulating the scratchpad RAM variables.

Details.

The scratch pad addresses are 0 through 62. Address 63 is read only and is used by the BS2SX to determine which ROM memory block it is currently executing.

This scratch pad memory may be used to transfer variables from one process to another (more on this elsewhere). But, it may also be used locally much as one would use the 26 bytes which may be directly referenced.

Access the the scratch pad RAM is facilitated using the GET and PUT commands. Note that these commands are new to the BS2SX.

	GET scratch_address, variable
	PUT scratch_address, variable
Assume one wishes to perform the calculation;
	Z = 52 * X/10 + 49 * Y/100


'RAM_0.BSX
'
'

	A 	VAR WORD	' general purpose variables
	B	VAR BYTE
	C  	VAR BYTE

	Z_H_SCRATCH 	CON 0	' sratchpad pad addresses defined
	Z_L_SCRATCH	CON 1

	X_SCRATCH	CON 2
	Y_SCRATCH	CON 3


	GET X_SCRATCH, B	' fetch the values
	GET Y_SCRATCH, C

	A = 52 * B/10 + 49 * C/100	' perform the calculations

	PUT Z_H_SCRATCH, A.BYTE1	' save the result
	PUT Z_L_SCRATCH, A.BYTE0

DONE:
	GOTO DONE
Of course, in this example, four the 26 RAM locations which may be directly manipulated are used and it might appear there is no advantage in having these 63 new scratch pad locations. However, note that variables A, B and C may be thought of as temporary variables which are briefly used in the code segment and may then are free to perform other calculations.

These 63 new variables open up possibilities for arrays which simply are not practical with the 26 byte limitations. For example, assume you have previously stored 50 temperature values (bytes) in scratchpad locations 0 - 49 and desire to know the average, the maximum and the minimum. (In the following, I have not taken into account negative values).

'ARRAY_0.BSX

	SUM VAR WORD
	AVG_10 VAR WORD	' ten times the average
	_MAX VAR BYTE
	_MIN VAR BYTE

	N   VAR BYTE
	A   VAR BYTE	' temporary variable

	SCRATCH CON 0	' temperature values in locations 0 - 49

	SUM = 0
	FOR N=0 TO 49
           GET SCRATCH+N, A
           SUM = SUM + A
        NEXT

	AVG_10 = SUM * 10 / 50	
	DEBUG "AVG = ", DEC AVG_10/10, ".", DEC AVG_10//10, 
CR		   

' Now for the MIN

	_MIN = 255
	
	FOR N=0 TO 49
	   GET SCRATCH+N, A
	   IF (A>=_MIN) THEN SKIP_1
             _MIN = A	' A is less than _MIN
SKIP_1:
	NEXT
	DEBUG "MIN = ", DEC _MIN, CR

' Now for the MAX

	_MAX = 0
	
	FOR N=0 TO 49
	   GET SCRATCH+N, A
	   IF (A<=_MAX) THEN SKIP_2
             _MAX = A	' A is greater than MAX
SKIP_2:
	NEXT
	DEBUG "MAX = ", DEC _MAX, CR

DONE:
	GOTO DONE

It occurs to me that this additional RAM may provide the capability for performing matrix manipulation, perhaps for solving simultaneous equations, but this is something I will probably never find the time to do.

The following is a rather foolish routine which fetches 50 variables from scratch pad, increments them and writes them to consecutive locations in EEPROM. As, I say, foolish, but it does illustrate how 63 RAM locations may be utilized with a very minor sacrifice of the 26 RAM bytes which may be directly referenced.

'RAM_1.BSX

	A	VAR BYTE	' a general purpose variable
	B	VAR WORD
	N	VAR BYTE
	
	X_SCRATCH CON 0		' values in 0-49
	P_SCRATCH_HI CON 50	' EEPROM pointer
	P_SCRATCH_LO CON 51

	FOR N = 0 TO 49		' dummy up the scratch pad
           PUT  X_SCRATCH+N, 255-N
        NEXT
	
	FOR N= 0 TO 49		' dump the scratch pad
           GET X_SCRATCH+N, A
           DEBUG HEX N, " ", HEX A, CR
           PAUSE 100
        NEXT 

	B=$100		' write scatch pad to EEPROM
	PUT P_SCRATCH_HI, B.BYTE1	' save to scratch pad
        PUT P_SCRATCH_LO, B.BYTE0   

	FOR N = 0 TO 49
	   GET X_SCRATCH+N, A	' increment X_SCRATCH
	   A = A+1
	   PUT X_SCRATCH+N, A 
	
	   GET P_SCRATCH_HI, B.BYTE1
	   GET P_SCRATCH_LO, B.BYTE0
	   WRITE B, A		' save to EEPROM
	   B=B+1		' increment the pointer
	   PUT P_SCRATCH_HI, B.BYTE1
	   PUT P_SCRATCH_LO, B.BYTE0
	NEXT		

	B=$100		' dump EEPROM
	FOR N=0 TO 49
           READ B, A
	   DEBUG HEX B," ", HEX A, CR
           B=B+1
	   PAUSE 100
        NEXT

DONE:
	GOTO DONE