Exemplo n.º 1
0
        }/* z_print_char */

        /*
         * z_print_form, print a formatted table.
         *
         *	zargs[0] = address of formatted table to be printed
         *
         */

        internal static void z_print_form()
        {
            zword count;
            zword addr = Process.zargs[0];

            bool first = true;

            for (;;)
            {
                FastMem.LOW_WORD(addr, out count);
                addr += 2;

                if (count == 0)
                {
                    break;
                }

                if (!first)
                {
                    Buffer.new_line();
                }

                while (count-- > 0)
                {
                    zbyte c;

                    FastMem.LOW_BYTE(addr, out c);
                    addr++;

                    Buffer.print_char(translate_from_zscii(c));
                }

                first = false;
            }
        }/* z_print_form */
Exemplo n.º 2
0
        }/* memory_open */

        /*
         * memory_new_line
         *
         * Redirect a newline to the memory of the Z-machine.
         *
         */

        internal static void memory_new_line()
        {
            zword size;
            zword addr;

            redirect[depth].total += redirect[depth].width;
            redirect[depth].width  = 0;

            addr = redirect[depth].table;

            FastMem.LOW_WORD(addr, out size);
            addr += 2;

            if (redirect[depth].xsize != 0xffff)
            {
                redirect[depth].table = (zword)(addr + size);
                size = 0;
            }
            else
            {
                FastMem.storeb((zword)(addr + (size++)), 13);
            }

            FastMem.storew(redirect[depth].table, size);
        }/* memory_new_line */
Exemplo n.º 3
0
        }/* z_print_num */

        /*
         * print_object
         *
         * Print an object description.
         *
         */

        internal static void print_object(zword object_var)
        {
            zword addr = CObject.object_name(object_var);
            zword code = 0x94a5;
            zbyte length;

            FastMem.LOW_BYTE(addr, out length);
            addr++;

            if (length != 0)
            {
                FastMem.LOW_WORD(addr, out code);
            }

            if (code == 0x94a5)         /* encoded text 0x94a5 == empty string */

            {
                print_string("object#");        /* supply a generic name */
                print_num(object_var);          /* for anonymous objects */
            }
            else
            {
                decode_text(string_type.LOW_STRING, addr);
            }
        }/* print_object */
Exemplo n.º 4
0
        internal static void z_make_menu()
        {
            /* This opcode was only used for the Macintosh version of Journey.
             * It controls menus with numbers greater than 2 (menus 0, 1 and 2
             * are system menus). */

            if (Process.zargs[0] < 3)
            {
                Process.branch(false);
                return;
            }

            if (Process.zargs[1] != 0)
            {
                zword items;
                int   i;

                FastMem.LOW_WORD(Process.zargs[1], out items);

                for (i = 0; i < items; i++)
                {
                    zword item;
                    zbyte length;
                    zbyte c;
                    int   j;

                    FastMem.LOW_WORD(Process.zargs[1] + 2 + (2 * i), out item);
                    FastMem.LOW_BYTE(item, out length);

                    if (length > 31)
                    {
                        length = 31;
                    }
                    menu[length] = 0;

                    for (j = 0; j < length; j++)
                    {
                        FastMem.LOW_BYTE(item + j + 1, out c);
                        menu[j] = Text.translate_from_zscii(c);
                    }

                    if (i == 0)
                    {
                        os_.menu(ZMachine.MENU_NEW, Process.zargs[0], menu);
                    }
                    else
                    {
                        os_.menu(ZMachine.MENU_ADD, Process.zargs[0], menu);
                    }
                }
            }
            else
            {
                os_.menu(ZMachine.MENU_REMOVE, Process.zargs[0], new zword[0]);
            }

            Process.branch(true);
        }/* z_make_menu */
Exemplo n.º 5
0
        }/* z_loadb */

        /*
         * z_loadw, store a value from a table of words.
         *
         *	zargs[0] = address of table
         *	zargs[1] = index of table entry to store
         *
         */

        internal static void z_loadw()
        {
            zword addr = (zword)(Process.zargs[0] + 2 * Process.zargs[1]);
            zword value;

            FastMem.LOW_WORD(addr, out value);

            Process.store(value);
        }/* z_loadw */
Exemplo n.º 6
0
        /*
         * translate_from_zscii
         *
         * Map a ZSCII character into Unicode.
         *
         */

        internal static zword translate_from_zscii(zbyte c)
        {
            if (c == 0xfc)
            {
                return(CharCodes.ZC_MENU_CLICK);
            }
            if (c == 0xfd)
            {
                return(CharCodes.ZC_DOUBLE_CLICK);
            }
            if (c == 0xfe)
            {
                return(CharCodes.ZC_SINGLE_CLICK);
            }

            if (c >= 0x9b && main.story_id != Story.BEYOND_ZORK)
            {
                if (main.hx_unicode_table != 0)         /* game has its own Unicode table */

                {
                    zbyte N;

                    FastMem.LOW_BYTE(main.hx_unicode_table, out N);

                    if (c - 0x9b < N)
                    {
                        zword addr = (zword)(main.hx_unicode_table + 1 + 2 * (c - 0x9b));
                        zword unicode;

                        FastMem.LOW_WORD(addr, out unicode);

                        if (unicode < 0x20)
                        {
                            return('?');
                        }

                        return(unicode);
                    }
                    else
                    {
                        return('?');
                    }
                }
                else                            /* game uses standard set */

                if (c <= 0xdf)
                {
                    return(zscii_to_latin1[c - 0x9b]);
                }
                else
                {
                    return('?');
                }
            }

            return((zword)c);
        }/* translate_from_zscii */
Exemplo n.º 7
0
        }/* z_jin */

        /*
         * z_get_child, store the child of an object.
         *
         *	Process.zargs[0] = object
         *
         */

        internal static void z_get_child()
        {
            zword obj_addr;

            /* If we are monitoring object locating display a short note */

            if (main.option_object_locating == true)
            {
                Stream.stream_mssg_on();
                Text.print_string("@get_child ");
                Text.print_object(Process.zargs[0]);
                Stream.stream_mssg_off();
            }

            if (Process.zargs[0] == 0)
            {
                Err.runtime_error(ErrorCodes.ERR_GET_CHILD_0);
                Process.store(0);
                Process.branch(false);
                return;
            }

            obj_addr = object_address(Process.zargs[0]);

            if (main.h_version <= ZMachine.V3)
            {
                zbyte child;

                /* Get child id from object */

                obj_addr += O1_CHILD;
                FastMem.LOW_BYTE(obj_addr, out child);

                /* Store child id and branch */

                Process.store(child);
                Process.branch(child > 0);
            }
            else
            {
                zword child;

                /* Get child id from object */

                obj_addr += O4_CHILD;
                FastMem.LOW_WORD(obj_addr, out child);

                /* Store child id and branch */

                Process.store(child);
                Process.branch(child > 0);
            }
        }/* z_get_child */
Exemplo n.º 8
0
        }/* z_clear_attr */

        /*
         * z_jin, branch if the first object is inside the second.
         *
         *	Process.zargs[0] = first object
         *	Process.zargs[1] = second object
         *
         */

        internal static void z_jin()
        {
            zword obj_addr;

            /* If we are monitoring object locating display a short note */

            if (main.option_object_locating == true)
            {
                Stream.stream_mssg_on();
                Text.print_string("@jin ");
                Text.print_object(Process.zargs[0]);
                Text.print_string(" ");
                Text.print_object(Process.zargs[1]);
                Stream.stream_mssg_off();
            }

            if (Process.zargs[0] == 0)
            {
                Err.runtime_error(ErrorCodes.ERR_JIN_0);
                Process.branch(0 == Process.zargs[1]);
                return;
            }

            obj_addr = object_address(Process.zargs[0]);

            if (main.h_version <= ZMachine.V3)
            {
                zbyte parent;

                /* Get parent id from object */

                obj_addr += O1_PARENT;
                FastMem.LOW_BYTE(obj_addr, out parent);

                /* Branch if the parent is obj2 */

                Process.branch(parent == Process.zargs[1]);
            }
            else
            {
                zword parent;

                /* Get parent id from object */

                obj_addr += O4_PARENT;
                FastMem.LOW_WORD(obj_addr, out parent);

                /* Branch if the parent is obj2 */

                Process.branch(parent == Process.zargs[1]);
            }
        }/* z_jin */
Exemplo n.º 9
0
        }/* z_loadw */

        /*
         * z_scan_table, find and store the address of a target within a table.
         *
         *	zargs[0] = target value to be searched for
         *	zargs[1] = address of table
         *	zargs[2] = number of table entries to check value against
         *	zargs[3] = type of table (optional, defaults to 0x82)
         *
         * Note: The table is a word array if bit 7 of zargs[3] is set; otherwise
         *       it's a byte array. The lower bits hold the address step.
         *
         */

        internal static void z_scan_table()
        {
            zword addr = Process.zargs[1];
            int   i;

            /* Supply default arguments */

            if (Process.zargc < 4)
            {
                Process.zargs[3] = 0x82;
            }

            /* Scan byte or word array */

            for (i = 0; i < Process.zargs[2]; i++)
            {
                if ((Process.zargs[3] & 0x80) > 0)      /* scan word array */

                {
                    zword wvalue;

                    FastMem.LOW_WORD(addr, out wvalue);

                    if (wvalue == Process.zargs[0])
                    {
                        goto finished;
                    }
                }
                else            /* scan byte array */

                {
                    zbyte bvalue;

                    FastMem.LOW_BYTE(addr, out bvalue);

                    if (bvalue == Process.zargs[0])
                    {
                        goto finished;
                    }
                }

                addr += (zword)(Process.zargs[3] & 0x7f);
            }

            addr = 0;

finished:

            Process.store(addr);
            Process.branch(addr > 0);
        }/* z_scan_table */
Exemplo n.º 10
0
        }/* z_pop_stack */

        /*
         * z_pull, pop a value off...
         *
         * a) ...the game or a user stack and store it (V6)
         *
         *	zargs[0] = address of user stack (optional)
         *
         * b) ...the game stack and write it to a variable (other than V6)
         *
         *	zargs[0] = variable to write value to
         *
         */

        internal static void z_pull()
        {
            zword value;

            if (main.h_version != ZMachine.V6)          /* not a V6 game, pop stack and write */

            {
                value = main.stack[main.sp++];

                if (Process.zargs[0] == 0)
                {
                    main.stack[main.sp] = value;
                }
                else if (Process.zargs[0] < 16)
                {
                    // *(fp - Process.zargs[0]) = value;
                    main.stack[main.fp - Process.zargs[0]] = value;
                }
                else
                {
                    zword addr = (zword)(main.h_globals + 2 * (Process.zargs[0] - 16));
                    FastMem.SET_WORD(addr, value);
                }
            }
            else                        /* it's V6, but is there a user stack? */

            {
                if (Process.zargc == 1)         /* it's a user stack */

                {
                    zword size;
                    zword addr = Process.zargs[0];

                    FastMem.LOW_WORD(addr, out size);

                    size++;
                    FastMem.storew(addr, size);

                    addr += (zword)(2 * size);
                    FastMem.LOW_WORD(addr, out value);
                }
                else
                {
                    value = main.stack[main.sp++];   // value = *sp++;	/* it's the game stack */
                }
                Process.store(value);
            }
        }/* z_pull */
Exemplo n.º 11
0
        }/* z_push */

        /*
         * z_push_stack, push a value onto a user stack then branch if successful.
         *
         *	zargs[0] = value to push onto the stack
         *	zargs[1] = address of user stack
         *
         */

        internal static void z_push_stack()
        {
            zword size;
            zword addr = Process.zargs[1];

            FastMem.LOW_WORD(addr, out size);

            if (size != 0)
            {
                FastMem.storew((zword)(addr + 2 * size), Process.zargs[0]);

                size--;
                FastMem.storew(addr, size);
            }

            Process.branch(size > 0);  // TODO I think that's what's expected here
        }/* z_push_stack */
Exemplo n.º 12
0
        }/* translate_from_zscii */

        /*
         * unicode_to_zscii
         *
         * Convert a Unicode character to ZSCII, returning 0 on failure.
         *
         */

        internal static zbyte unicode_to_zscii(zword c)
        {
            int i;

            if (c >= CharCodes.ZC_LATIN1_MIN)
            {
                if (main.hx_unicode_table != 0)         /* game has its own Unicode table */

                {
                    zbyte N;

                    FastMem.LOW_BYTE(main.hx_unicode_table, out N);

                    for (i = 0x9b; i < 0x9b + N; i++)
                    {
                        zword addr = (zword)(main.hx_unicode_table + 1 + 2 * (i - 0x9b));
                        zword unicode;

                        FastMem.LOW_WORD(addr, out unicode);

                        if (c == unicode)
                        {
                            return((zbyte)i);
                        }
                    }

                    return(0);
                }
                else                            /* game uses standard set */

                {
                    for (i = 0x9b; i <= 0xdf; i++)
                    {
                        if (c == zscii_to_latin1[i - 0x9b])
                        {
                            return((zbyte)i);
                        }
                    }

                    return(0);
                }
            }

            return((zbyte)c);
        }/* unicode_to_zscii */
Exemplo n.º 13
0
        }/* z_pop */

        /*
         * z_pop_stack, pop n values off the game or user stack and discard them.
         *
         *	zargs[0] = number of values to discard
         *	zargs[1] = address of user stack (optional)
         *
         */

        internal static void z_pop_stack()
        {
            if (Process.zargc == 2)             /* it's a user stack */

            {
                zword size;
                zword addr = Process.zargs[1];

                FastMem.LOW_WORD(addr, out size);

                size += Process.zargs[0];
                FastMem.storew(addr, size);
            }
            else
            {
                main.sp += Process.zargs[0];    /* it's the game stack */
            }
        }/* z_pop_stack */
Exemplo n.º 14
0
        }/* z_get_prop_len */

        /*
         * z_get_sibling, store the sibling of an object.
         *
         *	Process.zargs[0] = object
         *
         */

        internal static void z_get_sibling()
        {
            zword obj_addr;

            if (Process.zargs[0] == 0)
            {
                Err.runtime_error(ErrorCodes.ERR_GET_SIBLING_0);
                Process.store(0);
                Process.branch(false);
                return;
            }

            obj_addr = object_address(Process.zargs[0]);

            if (main.h_version <= ZMachine.V3)
            {
                zbyte sibling;

                /* Get sibling id from object */

                obj_addr += O1_SIBLING;
                FastMem.LOW_BYTE(obj_addr, out sibling);

                /* Store sibling and branch */

                Process.store(sibling);
                Process.branch(sibling > 0); // TODO I'm not sure about this logic Process.branch (sibling);
                // I think it means if the sibling isn't zero, jump..
            }
            else
            {
                zword sibling;

                /* Get sibling id from object */

                obj_addr += O4_SIBLING;
                FastMem.LOW_WORD(obj_addr, out sibling);

                /* Store sibling and branch */

                Process.store(sibling);
                Process.branch(sibling > 0);
            }
        }/* z_get_sibling */
Exemplo n.º 15
0
        }/* z_dec_chk */

        /*
         * z_inc, increment a variable.
         *
         *  zargs[0] = variable to increment
         *
         */

        internal static void z_inc()
        {
            zword value;

            if (Process.zargs[0] == 0)
            {
                main.stack[main.sp]++; // (*sp)++;
            }
            else if (Process.zargs[0] < 16)
            {
                (main.stack[main.fp - Process.zargs[0]])++;
            }
            else
            {
                zword addr = (zword)(main.h_globals + 2 * (Process.zargs[0] - 16));
                FastMem.LOW_WORD(addr, out value);
                value++;
                FastMem.SET_WORD(addr, value);
            }
        }/* z_inc */
Exemplo n.º 16
0
        }/* z_inc_chk */

        /*
         * z_load, store the value of a variable.
         *
         *	zargs[0] = variable to store
         *
         */

        internal static void z_load()
        {
            zword value;

            if (Process.zargs[0] == 0)
            {
                value = main.stack[main.sp];
            }
            else if (Process.zargs[0] < 16)
            {
                value = main.stack[main.fp - Process.zargs[0]];
            }
            else
            {
                zword addr = (zword)(main.h_globals + 2 * (Process.zargs[0] - 16));
                FastMem.LOW_WORD(addr, out value);
            }

            Process.store(value);
        }/* z_load */
Exemplo n.º 17
0
        }/* memory_new_line */

        /*
         * memory_word
         *
         * Redirect a string of characters to the memory of the Z-machine.
         *
         */

        internal static void memory_word(zword[] s)
        {
            zword size;
            zword addr;
            zword c;

            int pos = 0;

            if (main.h_version == ZMachine.V6)
            {
                int width = os_.string_width(s);

                if (redirect[depth].xsize != 0xffff)
                {
                    if (redirect[depth].width + width > redirect[depth].xsize)
                    {
                        if (s[pos] == ' ' || s[pos] == CharCodes.ZC_INDENT || s[pos] == CharCodes.ZC_GAP)
                        {
                            width = os_.string_width(s, ++pos);
                        }

                        memory_new_line();
                    }
                }

                redirect[depth].width += (zword)width;
            }

            addr = redirect[depth].table;

            FastMem.LOW_WORD(addr, out size);
            addr += 2;

            while ((c = s[pos++]) != 0)
            {
                FastMem.storeb((zword)(addr + (size++)), Text.translate_to_zscii(c));
            }

            FastMem.storew(redirect[depth].table, size);
        }/* memory_word */
Exemplo n.º 18
0
        }/* z_dec */

        /*
         * z_dec_chk, decrement a variable and branch if now less than value.
         *
         *  zargs[0] = variable to decrement
         *  zargs[1] = value to check variable against
         *
         */

        internal static void z_dec_chk()
        {
            zword value;

            if (Process.zargs[0] == 0)
            {
                value = --(main.stack[main.sp]);
            }
            else if (Process.zargs[0] < 16)
            {
                value = --(main.stack[main.fp - Process.zargs[0]]);
            }
            else
            {
                zword addr = (zword)(main.h_globals + 2 * (Process.zargs[0] - 16));
                FastMem.LOW_WORD(addr, out value);
                value--;
                FastMem.SET_WORD(addr, value);
            }

            Process.branch((short)value < (short)Process.zargs[1]);
        }/* z_dec_chk */
Exemplo n.º 19
0
        /*
         * load_operand
         *
         * Load an operand, either a variable or a constant.
         *
         */

        static void load_operand(zbyte type)
        {
            zword value;

            if ((type & 2) > 0)
            {                   /* variable */
                zbyte variable;

                FastMem.CODE_BYTE(out variable);

                if (variable == 0)
                {
                    value = main.stack[main.sp++];
                }
                else if (variable < 16)
                {
                    value = main.stack[main.fp - variable];
                }
                else
                {
                    zword addr = (zword)(main.h_globals + 2 * (variable - 16)); // TODO Make sure this logic
                    FastMem.LOW_WORD(addr, out value);
                }
            }
            else if ((type & 1) > 0)
            {           /* small constant */
                zbyte bvalue;

                FastMem.CODE_BYTE(out bvalue);
                value = bvalue;
            }
            else
            {
                FastMem.CODE_WORD(out value);           /* large constant */
            }
            zargs[zargc++] = value;

            DebugState.Output("  Storing operand: {0} -> {1}", zargc - 1, value);
        }/* load_operand */
Exemplo n.º 20
0
        }/* object_address */

        /*
         * object_name
         *
         * Return the address of the given object's name.
         *
         */

        internal static zword object_name(zword object_var)
        {
            zword obj_addr;
            zword name_addr;

            obj_addr = object_address(object_var);

            /* The object name address is found at the start of the properties */

            if (main.h_version <= ZMachine.V3)
            {
                obj_addr += O1_PROPERTY_OFFSET;
            }
            else
            {
                obj_addr += O4_PROPERTY_OFFSET;
            }

            FastMem.LOW_WORD(obj_addr, out name_addr);

            return(name_addr);
        }/* object_name */
Exemplo n.º 21
0
        static void decode_text(string_type st, zword addr)
        {
            // zword* ptr;
            long  byte_addr;
            zword c2;
            zword code;
            zbyte c, prev_c = 0;
            int   shift_state = 0;
            int   shift_lock  = 0;
            int   status      = 0;

            // ptr = NULL;		/* makes compilers shut up */
            byte_addr = 0;

            if (resolution == 0)
            {
                find_resolution();
            }

            /* Calculate the byte address if necessary */

            if (st == string_type.ABBREVIATION)
            {
                byte_addr = (long)addr << 1;
            }

            else if (st == string_type.HIGH_STRING)
            {
                if (main.h_version <= ZMachine.V3)
                {
                    byte_addr = (long)addr << 1;
                }
                else if (main.h_version <= ZMachine.V5)
                {
                    byte_addr = (long)addr << 2;
                }
                else if (main.h_version <= ZMachine.V7)
                {
                    byte_addr = ((long)addr << 2) + ((long)main.h_strings_offset << 3);
                }
                else /* (h_version <= V8) */
                {
                    byte_addr = (long)addr << 3;
                }

                if (byte_addr >= main.story_size)
                {
                    Err.runtime_error(ErrorCodes.ERR_ILL_PRINT_ADDR);
                }
            }

            /* Loop until a 16bit word has the highest bit set */
            if (st == string_type.VOCABULARY)
            {
                ptrDt = 0;
            }

            do
            {
                int i;

                /* Fetch the next 16bit word */

                if (st == string_type.LOW_STRING || st == string_type.VOCABULARY)
                {
                    FastMem.LOW_WORD(addr, out code);
                    addr += 2;
                }
                else if (st == string_type.HIGH_STRING || st == string_type.ABBREVIATION)
                {
                    FastMem.HIGH_WORD(byte_addr, out code);
                    byte_addr += 2;
                }
                else
                {
                    FastMem.CODE_WORD(out code);
                }

                /* Read its three Z-characters */

                for (i = 10; i >= 0; i -= 5)
                {
                    zword abbr_addr;
                    zword ptr_addr;
                    zword zc;

                    c = (zbyte)((code >> i) & 0x1f);

                    switch (status)
                    {
                    case 0:     /* normal operation */

                        if (shift_state == 2 && c == 6)
                        {
                            status = 2;
                        }

                        else if (main.h_version == ZMachine.V1 && c == 1)
                        {
                            Buffer.new_line();
                        }

                        else if (main.h_version >= ZMachine.V2 && shift_state == 2 && c == 7)
                        {
                            Buffer.new_line();
                        }

                        else if (c >= 6)
                        {
                            outchar(st, alphabet(shift_state, c - 6));
                        }

                        else if (c == 0)
                        {
                            outchar(st, ' ');
                        }

                        else if (main.h_version >= ZMachine.V2 && c == 1)
                        {
                            status = 1;
                        }

                        else if (main.h_version >= ZMachine.V3 && c <= 3)
                        {
                            status = 1;
                        }

                        else
                        {
                            shift_state = (shift_lock + (c & 1) + 1) % 3;

                            if (main.h_version <= ZMachine.V2 && c >= 4)
                            {
                                shift_lock = shift_state;
                            }

                            break;
                        }

                        shift_state = shift_lock;

                        break;

                    case 1:     /* abbreviation */

                        ptr_addr = (zword)(main.h_abbreviations + 64 * (prev_c - 1) + 2 * c);

                        FastMem.LOW_WORD(ptr_addr, out abbr_addr);
                        decode_text(string_type.ABBREVIATION, abbr_addr);

                        status = 0;
                        break;

                    case 2:     /* ZSCII character - first part */

                        status = 3;
                        break;

                    case 3:     /* ZSCII character - second part */

                        zc = (zword)((prev_c << 5) | c);

                        c2 = translate_from_zscii((zbyte)zc);     // TODO This doesn't seem right
                        outchar(st, c2);

                        status = 0;
                        break;
                    }

                    prev_c = c;
                }
            } while (!((code & 0x8000) > 0));

            if (st == string_type.VOCABULARY)
            {
                ptrDt = 0;
            }
        }/* decode_text */
Exemplo n.º 22
0
        }/* z_print_unicode */

        /*
         * lookup_text
         *
         * Scan a dictionary searching for the given word. The first argument
         * can be
         *
         * 0x00 - find the first word which is >= the given one
         * 0x05 - find the word which exactly matches the given one
         * 0x1f - find the last word which is <= the given one
         *
         * The return value is 0 if the search fails.
         *
         */

        internal static zword lookup_text(int padding, zword dct)
        {
            zword entry_addr;
            zword entry_count;
            zword entry;
            zword addr;
            zbyte entry_len;
            zbyte sep_count;
            int   entry_number;
            int   lower, upper;
            int   i;
            bool  sorted;

            if (resolution == 0)
            {
                find_resolution();
            }

            Text.encode_text(padding);

            FastMem.LOW_BYTE(dct, out sep_count);               /* skip word separators */
            dct += (zword)(1 + sep_count);
            FastMem.LOW_BYTE(dct, out entry_len);               /* get length of entries */
            dct += 1;
            FastMem.LOW_WORD(dct, out entry_count);             /* get number of entries */
            dct += 2;

            if ((short)entry_count < 0)         /* bad luck, entries aren't sorted */

            {
                entry_count = (zword)(-(short)entry_count);
                sorted      = false;
            }
            else
            {
                sorted = true;                  /* entries are sorted */
            }
            lower = 0;
            upper = entry_count - 1;

            while (lower <= upper)
            {
                if (sorted)                             /* binary search */
                {
                    entry_number = (lower + upper) / 2;
                }
                else                                    /* linear search */
                {
                    entry_number = lower;
                }

                entry_addr = (zword)(dct + entry_number * entry_len);

                /* Compare word to dictionary entry */

                addr = entry_addr;

                for (i = 0; i < resolution; i++)
                {
                    FastMem.LOW_WORD(addr, out entry);
                    if (encoded[i] != entry)
                    {
                        goto continuing;
                    }
                    addr += 2;
                }

                return(entry_addr);              /* exact match found, return now */

continuing:

                if (sorted)                             /* binary search */

                {
                    if (encoded[i] > entry)
                    {
                        lower = entry_number + 1;
                    }
                    else
                    {
                        upper = entry_number - 1;
                    }
                }

                else
                {
                    lower++;                            /* linear search */
                }
            }

            /* No exact match has been found */

            if (padding == 0x05)
            {
                return(0);
            }

            entry_number = (padding == 0x00) ? lower : upper;

            if (entry_number == -1 || entry_number == entry_count)
            {
                return(0);
            }

            return((zword)(dct + entry_number * entry_len));
        }/* lookup_text */
Exemplo n.º 23
0
        }/* next_property */

        /*
         * unlink_object
         *
         * Unlink an object from its parent and siblings.
         *
         */

        static void unlink_object(zword object_var)
        {
            zword obj_addr;
            zword parent_addr;
            zword sibling_addr;

            if (object_var == 0)
            {
                Err.runtime_error(ErrorCodes.ERR_REMOVE_OBJECT_0);
                return;
            }

            obj_addr = object_address(object_var);

            if (main.h_version <= ZMachine.V3)
            {
                zbyte parent;
                zbyte younger_sibling;
                zbyte older_sibling;
                zbyte zero = 0;

                /* Get parent of object, and return if no parent */

                obj_addr += O1_PARENT;
                FastMem.LOW_BYTE(obj_addr, out parent);
                if (parent == 0)
                {
                    return;
                }

                /* Get (older) sibling of object and set both parent and sibling
                 * pointers to 0 */

                FastMem.SET_BYTE(obj_addr, zero);
                obj_addr += (zword)(O1_SIBLING - O1_PARENT);
                FastMem.LOW_BYTE(obj_addr, out older_sibling);
                FastMem.SET_BYTE(obj_addr, zero);

                /* Get first child of parent (the youngest sibling of the object) */

                parent_addr = (zword)(object_address(parent) + O1_CHILD);
                FastMem.LOW_BYTE(parent_addr, out younger_sibling);

                /* Remove object from the list of siblings */

                if (younger_sibling == object_var)
                {
                    FastMem.SET_BYTE(parent_addr, older_sibling);
                }
                else
                {
                    do
                    {
                        sibling_addr = (zword)(object_address(younger_sibling) + O1_SIBLING);
                        FastMem.LOW_BYTE(sibling_addr, out younger_sibling);
                    } while (younger_sibling != object_var);
                    FastMem.SET_BYTE(sibling_addr, older_sibling);
                }
            }
            else
            {
                zword parent;
                zword younger_sibling;
                zword older_sibling;
                zword zero = 0;

                /* Get parent of object, and return if no parent */

                obj_addr += O4_PARENT;
                FastMem.LOW_WORD(obj_addr, out parent);
                if (parent == 0)
                {
                    return;
                }

                /* Get (older) sibling of object and set both parent and sibling
                 * pointers to 0 */

                FastMem.SET_WORD(obj_addr, zero);
                obj_addr += (zword)(O4_SIBLING - O4_PARENT);
                FastMem.LOW_WORD(obj_addr, out older_sibling);
                FastMem.SET_WORD(obj_addr, zero);

                /* Get first child of parent (the youngest sibling of the object) */

                parent_addr = (zword)(object_address(parent) + O4_CHILD);
                FastMem.LOW_WORD(parent_addr, out younger_sibling);

                /* Remove object from the list of siblings */

                if (younger_sibling == object_var)
                {
                    FastMem.SET_WORD(parent_addr, older_sibling);
                }
                else
                {
                    do
                    {
                        sibling_addr = (zword)(object_address(younger_sibling) + O4_SIBLING);
                        FastMem.LOW_WORD(sibling_addr, out younger_sibling);
                    } while (younger_sibling != object_var);
                    FastMem.SET_WORD(sibling_addr, older_sibling);
                }
            }
        }/* unlink_object */
Exemplo n.º 24
0
        }/* z_get_parent */

        /*
         * z_get_prop, store the value of an object property.
         *
         *	Process.zargs[0] = object
         *	Process.zargs[1] = number of property to be examined
         *
         */

        internal static void z_get_prop()
        {
            zword prop_addr;
            zword wprop_val;
            zbyte bprop_val;
            zbyte value;
            zbyte mask;

            if (Process.zargs[0] == 0)
            {
                Err.runtime_error(ErrorCodes.ERR_GET_PROP_0);
                Process.store(0);
                return;
            }

            /* Property id is in bottom five (six) bits */

            mask = (zbyte)((main.h_version <= ZMachine.V3) ? 0x1f : 0x3f);

            /* Load address of first property */

            prop_addr = first_property(Process.zargs[0]);

            /* Scan down the property list */

            for (; ;)
            {
                FastMem.LOW_BYTE(prop_addr, out value);
                if ((value & mask) <= Process.zargs[1])
                {
                    break;
                }
                prop_addr = next_property(prop_addr);
            }

            if ((value & mask) == Process.zargs[1])     /* property found */

            /* Load property (byte or word sized) */

            {
                prop_addr++;

                if ((main.h_version <= ZMachine.V3 && !((value & 0xe0) > 0)) || (main.h_version >= ZMachine.V4 && !((value & 0xc0) > 0)))
                {
                    FastMem.LOW_BYTE(prop_addr, out bprop_val);
                    wprop_val = bprop_val;
                }
                else
                {
                    FastMem.LOW_WORD(prop_addr, out wprop_val);
                }
            }
            else        /* property not found */

            /* Load default value */

            {
                prop_addr = (zword)(main.h_objects + 2 * (Process.zargs[1] - 1));
                FastMem.LOW_WORD(prop_addr, out wprop_val);
            }

            /* Store the property value */

            Process.store(wprop_val);
        }/* z_get_prop */
Exemplo n.º 25
0
        }/* z_get_sibling */

        /*
         * z_insert_obj, make an object the first child of another object.
         *
         *	Process.zargs[0] = object to be moved
         *	Process.zargs[1] = destination object
         *
         */

        internal static void z_insert_obj()
        {
            zword obj1 = Process.zargs[0];
            zword obj2 = Process.zargs[1];
            zword obj1_addr;
            zword obj2_addr;

            /* If we are monitoring object movements display a short note */

            if (main.option_object_movement == true)
            {
                Stream.stream_mssg_on();
                Text.print_string("@move_obj ");
                Text.print_object(obj1);
                Text.print_string(" ");
                Text.print_object(obj2);
                Stream.stream_mssg_off();
            }

            if (obj1 == 0)
            {
                Err.runtime_error(ErrorCodes.ERR_MOVE_OBJECT_0);
                return;
            }

            if (obj2 == 0)
            {
                Err.runtime_error(ErrorCodes.ERR_MOVE_OBJECT_TO_0);
                return;
            }

            /* Get addresses of both objects */

            obj1_addr = object_address(obj1);
            obj2_addr = object_address(obj2);

            /* Remove object 1 from current parent */

            unlink_object(obj1);

            /* Make object 1 first child of object 2 */

            if (main.h_version <= ZMachine.V3)
            {
                zbyte child;

                obj1_addr += O1_PARENT;
                FastMem.SET_BYTE(obj1_addr, (zbyte)obj2);
                obj2_addr += O1_CHILD;
                FastMem.LOW_BYTE(obj2_addr, out child);
                FastMem.SET_BYTE(obj2_addr, (zbyte)obj1);
                obj1_addr += (zword)(O1_SIBLING - O1_PARENT);
                FastMem.SET_BYTE(obj1_addr, child);
            }
            else
            {
                zword child;

                obj1_addr += O4_PARENT;
                FastMem.SET_WORD(obj1_addr, obj2);
                obj2_addr += O4_CHILD;
                FastMem.LOW_WORD(obj2_addr, out child);
                FastMem.SET_WORD(obj2_addr, obj1);
                obj1_addr += (zword)(O4_SIBLING - O4_PARENT);
                FastMem.SET_WORD(obj1_addr, child);
            }
        }/* z_insert_obj */