Blitz_Basic_Bank.bb

Name: Blitz_Basic_Bank.bb
Version: 1.1
Released: August 27th, 2007
Description: PeekString, PokeString and DumpBank. DumpBank is used to output the contents of a bank to the Blitz debugger.
Download: Download Now (2.94KB)
; ------------------------------------------------------------------------------
; -- Blitz_Basic_Bank.bb
; -- 
; -- PeekString, PokeString and DumpBank (for debugging).
; -- 
; -- Homepage: http://www.sodaware.net/dev/blitz/
; --
; -- Copyright (c) 2007 Phil Newton
; -- All rights reserved.
; -- 
; -- Redistribution and use in source and binary forms, with or without 
; -- modification, are permitted provided that the following conditions are met:
; -- 
; --     1. Redistributions of source code must retain the above copyright 
; --        notice, this list of conditions and the following disclaimer.
; --     
; --     2. Redistributions in binary form must reproduce the above copyright 
; --        notice, this list of conditions and the following disclaimer in the 
; --        documentation and/or other materials provided with the distribution.
; -- 
; -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
; -- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
; -- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
; -- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
; -- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
; -- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
; -- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
; -- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
; -- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
; -- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; -- POSSIBILITY OF SUCH DAMAGE.
; ------------------------------------------------------------------------------


;;; <summary>Pokes a NULL terminated string to a bank.</summary>
;;; <param name="bankHandle">Bank handle to poke to.</param>
;;; <param name="offset">Offset in bytes that the poke operation will be started at.</param>
;;; <param name="value">The string to poke.</param>
;;; <remarks>If the end of the bank is reached, string will still end with a NULL byte.</remarks>
;;; <subsystem>Blitz.Bank</subsystem>
Function PokeString(bankHandle, offset, value$)

	; Check inputs
	If bankHandle < 1 Then Return False
	If offset < 0 Or offset > BankSize(bankHandle) Then Return False

	Local stringPos         = 1
	Local bankPos           = offset + stringPos - 1
	Local bankLength        = BankSize(bankHandle) - 1

	While (stringPos <= Len(value)) And (bankPos < bankLength)
		PokeByte bankHandle, bankPos, Asc(Mid(value$, stringPos, 1))

		; Move to next char
		bankPos         = bankPos + 1
		stringPos       = stringPos + 1
	Wend

	; Add null byte to end of string to terminate it
	PokeByte bankHandle, bankPos, 0

	Return True

End Function

;;; <summary>Reads a string from a memory bank.</summary>
;;; <param name="bankHandle">Bank handle to peek from.</param>
;;; <param name="offset">Offset in bytes that the peek operation will be started at.</param>
;;; <returns>The Peeked string.</returns>
;;; <subsystem>Blitz.Bank</subsystem>
Function PeekString$(bankHandle, offset)

	; Check inputs
	If bankHandle < 1 Then Return 
	If offset < 0 Or offset > BankSize(bankHandle) Then Return

	Local bankPos           = offset
	Local bankLength        = BankSize(bankHandle)
	Local peekedString$     = ""
	Local endFound%         = False
	Local currentByte       = 0

	While (bankPos < bankLength) And (endFound = False)

		currentByte     = PeekByte(bankHandle, bankPos)

		If currentByte = 0 Then
			endFound = True
		Else
			peekedString = peekedString + Chr(currentByte)
		EndIf

		bankPos = bankPos + 1
	Wend

	Return peekedString

End Function

;;; <summary>Dumps the contents of a bank to the debuglog. Useful for examining the contents of a bank.</summary>
;;; <param name="bankHandle">Handle of the bank to dump.</param>
;;; <param name="rowSize">Optional value to control the length of a dumped row.</param>
;;; <subsystem>Blitz.Bank</subsystem>
Function DumpBank(bankHandle, rowSize = 16)

	Local dumpString$       = ""
	Local ascString$        = ""

	; Header
	DebugLog "Bank Size: " + BankSize(bankHandle)
	For i = 0 To BankSize(bankHandle) - 1

		; Dump is raw data (as hex), asc is the ascii data
		dumpString = dumpString + Right(Hex(PeekByte(bankHandle, i)), 2) + " "
		ascString       = ascString + Chr(PeekByte(bankHandle, i))

		; Dump the line if we're at the end of the row
		If i Mod rowSize = rowSize - 1 Then
			DebugLog dumpString + "   " + ascString
			dumpString = ""
			ascString = ""
		EndIf
	Next

	; Any extra data not dumped.
	DebugLog dumpString + "   " + ascString

End Function