'Include "FuncioneStack_Push&Pop.bas"
'Funciones para trabajar software stack (Push, Pop, Push_Single y Pop_Single)
'02/09/2015, 09/2014, Basic del PSI v. 7.39_02
'************************************************************************
'Funciones _Push y _PoP (Byte, Word y Long), último en entrar primero en salir:
	'Inicialización: parámetros de entrada
		'Dim _Stack_Zize(x) As Byte 'Establece longitud del software stack
		'Dim _Stack_Max as Byte 'Establece longitud del software stack
		'_Stack_Max = x
		'Call _setup_stack()  'Inicializa contador de la pila (_Stack_Indice = 0)
	'Devuelve en _Stack_Buffer los Bytes libres de la pila
		'Call _Stack_Buffer()
	'Push:
		'Call _Push(Variable, Tipo)
		'Pone Variable en la pila, Tipo: tipo de variable
		'Tipo = _asbyte, _asword, _aslong
	'Pop:
		'Call _Pop(Tipo)
		'Extrae una variable de la pila, Tipo: tipo de variable a extraer
		'Tipo = _asbyte, _asword, _aslong
	'Push_Single:
		'Call _Push_Single(Variable as Single)
		'Pone Variable en la pila
	'Pop_Simgle:
		'Call _Pop_Single()
		'Extrae una variable de la pila (_Pop_Single = Variable as Single)
'*************************************************************************
'Parámetros de entrada y configuración
Const _asbyte = 0  'Tipo Byte
Const _asword = 1  'Tipo Word
Const _aslong = 2  'Tipo Long
Dim _stack_zize(20) As Byte  'Define la longitud de la pila
Const _stack_max = 20
Dim _stack_indice As Byte
'Función de configuración
Proc _setup_stack()
	_stack_indice = 0
End Proc                                          
'Devuelve en _Stack_Buffer los bytes libres que quedan en la pila
Function _stack_buffer() As Byte
	_stack_buffer = _stack_max - _stack_indice
End Function                                      
'Función Push para numeros enteros sin signo (Byte, Work y Long)
'hay que especificar el tipo de numero a depositar en la pila
Proc _push(_dato As Long, _tipo As Byte)
	'PUSH
	_stack_buffer = _stack_max - _stack_indice
	If _tipo = _asbyte And _stack_buffer < 1 Then Exit
	If _tipo = _asword And _stack_buffer < 2 Then Exit
	If _tipo = _aslong And _stack_buffer < 4 Then Exit
	
	_stack_zize(_stack_indice) = _dato.LB
	_stack_indice = _stack_indice + 1
	If _tipo >= 1 Then
		_stack_zize(_stack_indice) = _dato.HB
		_stack_indice = _stack_indice + 1
	Endif
	If _tipo >= 2 Then
		_stack_zize(_stack_indice) = _dato.3B
		_stack_indice = _stack_indice + 1
		_stack_zize(_stack_indice) = _dato.4B
		_stack_indice = _stack_indice + 1
	Endif
End Proc                                          
'Función Push para numeros reales de simple presición (Single)
'deposita un numero tipo Single (Float 4bytes) en la pila
Proc _push_single(_dato As Single)
	'PUSH
	_stack_buffer = _stack_max - _stack_indice
	If _stack_buffer < 4 Then Exit
	
	_stack_zize(_stack_indice) = _dato.LB
	_stack_indice = _stack_indice + 1
	_stack_zize(_stack_indice) = _dato.HB
	_stack_indice = _stack_indice + 1
	_stack_zize(_stack_indice) = _dato.3B
	_stack_indice = _stack_indice + 1
	_stack_zize(_stack_indice) = _dato.4B
	_stack_indice = _stack_indice + 1
End Proc                                          
'Función Pop para numeros enteros sin signo (Byte, Work y Long)
'hay que especificar el tipo de numero a extraer de la pila
Function _pop(_tipo As Byte) As Long
	'POP
	_pop = 0
	If _tipo >= 2 Then
		If _stack_indice < 4 Then Exit
		_stack_indice = _stack_indice - 1
		_pop.4B = _stack_zize(_stack_indice)
		_stack_indice = _stack_indice - 1
		_pop.3B = _stack_zize(_stack_indice)
	Endif
	If _tipo >= 1 Then
		If _stack_indice < 2 Then Exit
		_stack_indice = _stack_indice - 1
		_pop.HB = _stack_zize(_stack_indice)
	Endif
	If _stack_indice < 1 Then Exit
	_stack_indice = _stack_indice - 1
	_pop.LB = _stack_zize(_stack_indice)
End Function                                      
'Función Pop para numeros reales de simple presición (Single)
'extrae un numero tipo Single (Float 4bytes) de la pila
Function _pop_single() As Single
	'POP
	_pop_single = 0
	If _stack_indice < 4 Then Exit
	_stack_indice = _stack_indice - 1
	_pop_single.4B = _stack_zize(_stack_indice)
	_stack_indice = _stack_indice - 1
	_pop_single.3B = _stack_zize(_stack_indice)
	_stack_indice = _stack_indice - 1
	_pop_single.HB = _stack_zize(_stack_indice)
	_stack_indice = _stack_indice - 1
	_pop_single.LB = _stack_zize(_stack_indice)
End Function                                      
'****************************************************************************