1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/*
** File: variable.c
**
** (C)opyright 1987-1992 InfoTaskforce.
*/
#include "infocom.h"
Void
get_var ( var )
word var ;
{
store ( load_var ( var ) ) ;
}
word
load_var ( var )
word var ;
{
extern byte_ptr global_ptr ;
extern word *stack_var_ptr ;
extern word *stack ;
register word value ;
register word *svp ;
register byte_ptr ptr ;
if ( var == 0 )
return ( *stack ) ;
else
{
if ( var < LOCAL_VARS )
{
/*
** In interpreters prior to VERSION_5, the "stack_var_ptr"
** pointed to the first local variable on the z_code stack.
** In VERSION_5 and subsequent interpreters, the "stack_var_ptr"
** points to the stack entry above the first local variable on
** the z_code stack.
**
** However, there is no reason for us not to adopt the VERSION_5
** method for all versions of the interpreter. This decision
** affects the following opcodes:
** gosub, std_rtn, load_var & put_var.
**
** Interpreters prior to VERSION_5 used the statement:
**
** svp = stack_var_ptr - ( var - 1 ) ;
**
** instead of:
**
** svp = stack_var_ptr - var ;
*/
svp = stack_var_ptr - var ;
return ( *svp ) ;
}
else
{
ptr = global_ptr + (( var - LOCAL_VARS ) << 1 ) ;
value = *(ptr++) << BITS_PER_BYTE ;
return ( (word)(value | *ptr) ) ;
}
}
}
Void
put_var ( var,value )
word var,value ;
{
extern byte_ptr global_ptr ;
extern word *stack_var_ptr ;
extern word *stack ;
register word *svp ;
register byte_ptr ptr ;
if ( var == 0 )
*stack = value ;
else
{
if ( var < LOCAL_VARS )
{
/*
** In interpreters prior to VERSION_5, the "stack_var_ptr"
** pointed to the first local variable on the z_code stack.
** In VERSION_5 and subsequent interpreters, the "stack_var_ptr"
** points to the stack entry above the first local variable on
** the z_code stack.
**
** However, there is no reason for us not to adopt the VERSION_5
** method for all versions of the interpreter. This decision
** affects the following opcodes:
** gosub, std_rtn, load_var & put_var.
**
** Interpreters prior to VERSION_5 used the statement:
**
** svp = stack_var_ptr - ( var - 1 ) ;
**
** instead of:
**
** svp = stack_var_ptr - var ;
*/
svp = stack_var_ptr - var ;
*svp = value ;
}
else
{
ptr = global_ptr + (( var - LOCAL_VARS ) << 1 ) ;
*ptr++ = MOST_SIGNIFICANT_BYTE ( value ) ;
*ptr = LEAST_SIGNIFICANT_BYTE ( value ) ;
}
}
}
Void
push ( value )
word value ;
{
extern word *stack ;
*(--stack) = value ;
}
Void
pop ( var )
word var ;
{
extern word *stack ;
put_var ( var,*stack++ ) ;
}
Void
inc_var ( var )
word var ;
{
put_var ( var,load_var ( var ) + 1 ) ;
}
Void
dec_var ( var )
word var ;
{
put_var ( var,load_var ( var ) - 1 ) ;
}
Void
inc_chk ( var,threshold )
word var,threshold ;
{
register word value ;
value = load_var ( var ) + 1 ;
put_var ( var,value ) ;
if ( (signed_word)value > (signed_word)threshold )
ret_value ( TRUE ) ;
else
ret_value ( FALSE ) ;
}
Void
dec_chk ( var,threshold )
word var,threshold ;
{
register word value ;
value = load_var ( var ) - 1 ;
put_var ( var,value ) ;
if ( (signed_word)value < (signed_word)threshold )
ret_value ( TRUE ) ;
else
ret_value ( FALSE ) ;
}
word
load ( mode )
int mode ;
{
/*
** Mode 0 = Immediate Word ;
** Mode 1 = Immediate Byte ;
** Mode 2 = Variable ;
*/
extern word *stack ;
register word var ;
if ( --mode < 0 )
return ( next_word () ) ;
if ( mode == 0 )
return ( (word)next_byte () ) ;
var = next_byte () ;
if ( var == 0 )
return ( *stack++ ) ;
else
return ( load_var ( var ) ) ;
}
|