;*******************************************************************************
; Title:	IfPodule
; Author:	Dave Lawrence
;		Converted to ObjAsm source code by Philip Ludlam
; Copyright:	(C) Dave Lawrence 1993
; Version:	1.01, 7 May 2006
;
; Changes:	Updated for the A9home by Philip Ludlam
;
;*******************************************************************************
; This program is free software; you can redistribute it and/or modify it
; under the terms of the GNU General Public License as published by the Free
; Software Foundation; either version 2 of the License, or (at your option)
; any later version.
;
; This program is distributed in the hope that it will be useful, but WITHOUT
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
; FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more
; details.
;
; You should have received a copy of the GNU General Public License along with
; this program; if not, write to the Free Software Foundation, Inc., 59 Temple
; Place - Suite 330, Boston, MA 02111-1307, USA
;
;*******************************************************************************


		TTL	> IfPodule

		GET	OSLib:oslib.hdr.OS
		GET	OSLib:oslib.hdr.Podule
		GET	AsmLib2:hdr.RegsBoth

;*******************************************************************************
; LC
;
; if I shove 'A' in $reg ends up as 'A', 'a' ends up as 'A'
; 'Z' ends up as 'Z' and 'z' ends up as 'Z'
;
; So it's a badly named UpperCase macro
;*******************************************************************************

		MACRO
$label		LC	$reg
$label		SUB	$reg, $reg, #"a"	; $reg = $reg - 'a' (#97)
		CMP	$reg, #"z" - "a" + 1	; 'z' (#122) - 'a' (#97) +1 = 26
						; if 0 <= $reg < 26 (i.e between 'a' and 'z' inclusive)
		SUBCC	$reg, $reg, #" "	; then $reg = $reg - ' ' (#32)
		ADD	$reg, $reg, #"a"	; $reg = $reg + 'a' (#97)
		CMP	$reg, #" "		; if it's a Ctrl char, zero it
		MOVLT	$reg, #0
		MEND

arg_podule	*	&0
arg_command	*	&4
arg_yes		*	&8
arg_no		*	&C

		AREA	|main|, CODE, READONLY

		ENTRY

;-------------------------------------------------------------------------------

Start		B	Entry			; Entry instruction
		DCD	&79766748		; Magic value 1
		DCD	&216C6776		; Magic value 2
		DCD	End-Start		; Read only size
		DCD	0			; Read/write size
		DCD	32			; 26 or 32 bit build

Entry

		MOV	r11, lr			; Store lr in r11

		ADR	r0, args
		MOV	r2, wp
		MOV	r3, #1024
		SWI	XOS_ReadArgs
		ADRVS	r0, syntax
		BVS	error

		LDR	r0, [wp, #arg_command]
		TEQ	r0, #0
		ADREQ	r0, syntax
		BEQ	error

		SUB	r8, r13, r3		; r8 = next free block in workspace

do
		LDR	r0, [wp, #arg_podule]
		MOV	r1, #0
		MOV	r2, #0
		SWI	XOS_EvaluateExpression
		MOVVC	r0, #1
		STRVC	r0, [wp, #arg_podule]
		MOVVC	r10, r2

		SWI	XPodule_ReturnNumber
		BVS	error
		SUBS	r3, r0, #1
		BMI	no_podules

pod_loop
		SWI	XPodule_ReadID
		BVS	skip_pod
		ANDS	r0, r0, #&78
		BEQ	extended_pod

		LDR	r1, [wp, #arg_podule]
		TEQ	r1, #1
		BNE	skip_pod

		CMP	r10, r0, LSR #3
		BEQ	found_pod
		B	skip_pod

extended_pod
		MOV	r2, r8
		SWI	XPodule_ReadHeader
		BVS	skip_pod

		LDR	r1, [wp, #arg_podule]
		TEQ	r1, #1
		BNE	check_string

		LDRB	r0, [r8, #3]
		LDRB	r1, [r8, #4]
		ORR	r0, r0, r1, LSL #8
		LDRB	r1, [r8, #5]
		ORR	r0, r0, r1, LSL #16
		LDRB	r1, [r8, #6]
		ORR	r0, r0, r1, LSL #24
		CMP	r0, r10
		BEQ	found_pod
		B	skip_pod

check_string
		LDRB	r0, [r8, #1]
		TST	r0, #1
		BEQ	skip_pod

		MOV	r0, #0

enum_chunk
		SWI	XPodule_EnumerateChunks
		BVS	error
		CMP	r0, #0
		BEQ	skip_pod
		CMP	r2, #&F5
		BNE	enum_chunk

		SUB	r0, r0, #1
		MOV	r2, r8
		SWI	XPodule_ReadChunk
		BVS	error

		MOV	r9, #0
		LDR	r4, [wp, #arg_podule]

instr
		LDRB	r5, [r2, r9]
		LC	r5
		LDRB	r6, [r4, r9]
		LC	r6

		TEQ	r5, #0
		TEQNE	r6, #0
		BEQ	got0
		TEQ	r5, r6
		ADDEQ	r9, r9, #1
		MOVNE	r9, #0
		ADDNE	r2, r2, #1
		B	instr

got0
		CMP	r5, r6
		BLT	skip_pod

found_pod
		LDMIA	wp, {r0, r5, r6, r7}
		TEQ	r7, #0
		MOVEQ	r6, #0
		B	do_command

skip_pod
		SUBS	r3, r3, #1
		BPL	pod_loop

no_podules
		LDMIA	wp, {r0, r5, r6, r7}
		TEQ	r6, #0
		TEQNE	r7, #0
		MOVNE	r6, r7
		BNE	do_command
		TEQ	r6, #0
		BEQ	exit
		MOV	r5, r6
		MOV	r6, #0

do_command
		MOV	r1, r8
		MOV	r0, r5
		MOV	r2, #32
		BL	copy
		MOVS	r0, r6
		MOV	r2, #0
		BLNE	copy
		STREQB	r2, [r1, #-1]

os_cli
		MOV	r0, r8
		SWI	XOS_CLI

exit
		MOV	pc, r11

error
		SetV
		MOV	pc, r11

copy
		LDRB	r3, [r0], #1
		STRB	r3, [r1], #1
		CMP	r3, #32
		BGE	copy
		STRB	r2, [r1, #-1]
		MOV	pc, lr

syntax
		DCD	0
		DCB	"Syntax: *IfPodule <part podule name> <yes command> [<no command>]", 10, 13
		DCB	"or:     *IfPodule <part podule name> <command> <yes param> <no param>"
		DCB	0
		ALIGN

args
		DCB	",,,"
		DCB	0

licence		DCB	" dL, 1993. Licence: GPL."
		ALIGN

End

		END
