}/* lookup_text */ /* * tokenise_text * * Translate a single word to a token and append it to the token * buffer. Every token consists of the address of the dictionary * entry, the length of the word and the offset of the word from * the start of the text buffer. Unknown words cause empty slots * if the flag is set (such that the text can be scanned several * times with different dictionaries); otherwise they are zero. * */ static void tokenise_text(zword text, zword length, zword from, zword parse, zword dct, bool flag) { zword addr; zbyte token_max, token_count; FastMem.LOW_BYTE(parse, out token_max); parse++; FastMem.LOW_BYTE(parse, out token_count); if (token_count < token_max) /* sufficient space left for token? */ { FastMem.storeb(parse++, (zbyte)(token_count + 1)); load_string((zword)(text + from), length); addr = lookup_text(0x05, dct); if (addr != 0 || !flag) { parse += (zword)(4 * token_count); // Will parse get updated properly? FastMem.storew((zword)(parse + 0), addr); FastMem.storeb((zword)(parse + 2), (zbyte)length); FastMem.storeb((zword)(parse + 3), (zbyte)from); } } }/* tokenise_text */
}/* 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 */
}/* 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 */
/* * z_copy_table, copy a table or fill it with zeroes. * * zargs[0] = address of table * zargs[1] = destination address or 0 for fill * zargs[2] = size of table * * Note: Copying is safe even when source and destination overlap; but * if zargs[1] is negative the table _must_ be copied forwards. * */ internal static void z_copy_table() { zword addr; zword size = Process.zargs[2]; zbyte value; int i; if (Process.zargs[1] == 0) /* zero table */ { for (i = 0; i < size; i++) { FastMem.storeb((zword)(Process.zargs[0] + i), 0); } } else if ((short)size < 0 || Process.zargs[0] > Process.zargs[1]) /* copy forwards */ { for (i = 0; i < (((short)size < 0) ? -(short)size : size); i++) { addr = (zword)(Process.zargs[0] + i); FastMem.LOW_BYTE(addr, out value); FastMem.storeb((zword)(Process.zargs[1] + i), value); } } else /* copy backwards */ { for (i = size - 1; i >= 0; i--) { addr = (zword)(Process.zargs[0] + i); FastMem.LOW_BYTE(addr, out value); FastMem.storeb((zword)(Process.zargs[1] + i), value); } } }/* z_copy_table */
}/* read_number */ /* * z_read, read a line of input and (in V5+) store the terminating key. * * zargs[0] = address of text buffer * zargs[1] = address of token buffer * zargs[2] = timeout in tenths of a second (optional) * zargs[3] = packed address of routine to be called on timeout * */ internal static void z_read() { zword[] buffer = new zword[General.INPUT_BUFFER_SIZE]; zword addr; zword key; zbyte max, size; zbyte c; int i; /* Supply default arguments */ if (Process.zargc < 3) { Process.zargs[2] = 0; } /* Get maximum input size */ addr = Process.zargs[0]; FastMem.LOW_BYTE(addr, out max); if (main.h_version <= ZMachine.V4) { max--; } if (max >= General.INPUT_BUFFER_SIZE) { max = (zbyte)(General.INPUT_BUFFER_SIZE - 1); } /* Get initial input size */ if (main.h_version >= ZMachine.V5) { addr++; FastMem.LOW_BYTE(addr, out size); } else { size = 0; } /* Copy initial input to local buffer */ for (i = 0; i < size; i++) { addr++; FastMem.LOW_BYTE(addr, out c); buffer[i] = Text.translate_from_zscii(c); } buffer[i] = 0; /* Draw status line for V1 to V3 games */ if (main.h_version <= ZMachine.V3) { Screen.z_show_status(); } /* Read input from current input stream */ key = Stream.stream_read_input( max, buffer, /* buffer and size */ Process.zargs[2], /* timeout value */ Process.zargs[3], /* timeout routine */ true, /* enable hot keys */ main.h_version == ZMachine.V6); /* no script in V6 */ if (key == CharCodes.ZC_BAD) { return; } /* Perform save_undo for V1 to V4 games */ if (main.h_version <= ZMachine.V4) { FastMem.save_undo(); } /* Copy local buffer back to dynamic memory */ for (i = 0; buffer[i] != 0; i++) { if (key == CharCodes.ZC_RETURN) { buffer[i] = Text.unicode_tolower(buffer[i]); } FastMem.storeb((zword)(Process.zargs[0] + ((main.h_version <= ZMachine.V4) ? 1 : 2) + i), Text.translate_to_zscii(buffer[i])); } /* Add null character (V1-V4) or write input length into 2nd byte */ if (main.h_version <= ZMachine.V4) { FastMem.storeb((zword)(Process.zargs[0] + 1 + i), 0); } else { FastMem.storeb((zword)(Process.zargs[0] + 1), (byte)i); } /* Tokenise line if a token buffer is present */ if (key == CharCodes.ZC_RETURN && Process.zargs[1] != 0) { Text.tokenise_line(Process.zargs[0], Process.zargs[1], 0, false); } /* Store key */ if (main.h_version >= ZMachine.V5) { Process.store(Text.translate_to_zscii(key)); } }/* z_read */
}/* tokenise_text */ /* * tokenise_line * * Split an input line into words and translate the words to tokens. * */ internal static void tokenise_line(zword text, zword token, zword dct, bool flag) { zword addr1; zword addr2; zbyte length; zbyte c; length = 0; /* makes compilers shut up */ /* Use standard dictionary if the given dictionary is zero */ if (dct == 0) { dct = main.h_dictionary; } /* Remove all tokens before inserting new ones */ FastMem.storeb((zword)(token + 1), 0); /* Move the first pointer across the text buffer searching for the * beginning of a word. If this succeeds, store the position in a * second pointer. Move the first pointer searching for the end of * the word. When it is found, "tokenise" the word. Continue until * the end of the buffer is reached. */ addr1 = text; addr2 = 0; if (main.h_version >= ZMachine.V5) { addr1++; FastMem.LOW_BYTE(addr1, out length); } do { zword sep_addr; zbyte sep_count; zbyte separator; /* Fetch next ZSCII character */ addr1++; if (main.h_version >= ZMachine.V5 && addr1 == text + 2 + length) { c = 0; } else { FastMem.LOW_BYTE(addr1, out c); } /* Check for separator */ sep_addr = dct; FastMem.LOW_BYTE(sep_addr, out sep_count); sep_addr++; do { FastMem.LOW_BYTE(sep_addr, out separator); sep_addr++; } while (c != separator && --sep_count != 0); /* This could be the start or the end of a word */ if (sep_count == 0 && c != ' ' && c != 0) { if (addr2 == 0) { addr2 = addr1; } } else if (addr2 != 0) { tokenise_text( text, (zword)(addr1 - addr2), (zword)(addr2 - text), token, dct, flag); addr2 = 0; } /* Translate separator (which is a word in its own right) */ if (sep_count != 0) { tokenise_text( text, (zword)(1), (zword)(addr1 - text), token, dct, flag); } } while (c != 0); }/* tokenise_line */
}/* z_scan_table */ /* * z_storeb, write a byte into a table of bytes. * * zargs[0] = address of table * zargs[1] = index of table entry * zargs[2] = value to be written * */ internal static void z_storeb() { FastMem.storeb((zword)(Process.zargs[0] + Process.zargs[1]), (byte)Process.zargs[2]); }/* z_storeb */