public Word[] GetContentsOfNthEntry(int n, GameMemory zorkFile) { int entrySize = GetEntrySizeInBytes(n, zorkFile); // I have been assuming that the above call will return 4, since that // is how many bytes are in a word entry. But it think this size // allows there to be arbitrary entries after all the words, and they might // be of up to this size. // Two questions: // 1. How do I know that we are in the size-regulated, has words-in-order // part of the dictionary? // 2. How do I know the size of the entries that are after the words, // in the arbitrary-sized part of the table? int entrySizeInWords = entrySize / 2; Word[] retval = new Word[entrySizeInWords]; WordAddress ptrStartOfEntry = GetOffSetOfNthEntry(n, zorkFile); for(int i = 0; i < entrySizeInWords; i++) { WordAddress here = new WordAddress(ptrStartOfEntry.Value + (i * 2)); retval[i] = zorkFile.ReadWord(here); } return retval; }
private static string Print3ZcharsFromWord(Word word) { int test1 = Bits.FetchBits(BitNumber.Bit14, BitSize.Size5, word); int test2 = Bits.FetchBits(BitNumber.Bit9, BitSize.Size5, word); int test3 = Bits.FetchBits(BitNumber.Bit4, BitSize.Size5, word); string str1 = Convert.ToString(test1, 2); string str2 = Convert.ToString(test2, 2); string str3 = Convert.ToString(test3, 2); List<Zchar> chars = new List<Zchar> { new Zchar(test1), new Zchar(test2), new Zchar(test3) }; return Zchar.PrintFromZchar(chars); }
public void ZeroIsNotATerminalWord() { Word nonTerminal = new Word(0); Assert.False(nonTerminal.IsTerminal()); }
public void WordAllOnesIsTerminal() { Word terminal = new Word(~0); Assert.True(terminal.IsTerminal()); }
public void SettingOnlyTerminalBitMakesTerminalWord() { Word onlyTerminalBitSet = new Word(0x1000); Assert.True(onlyTerminalBitSet.IsTerminal()); }
public void WriteWord(WordAddress wordAddress, Word toWrite) { short bytes = toWrite.Value; byte high = (byte)((bytes >> 8) & 0xFF); byte low = (byte)(bytes & 0xFF); this.WriteByte(Bits.AddressOfHighByte(wordAddress), high); this.WriteByte(Bits.AddressOfLowByte(wordAddress), low); }
public static IEnumerable<Zchar> ReadWordAsZchars(Word word) { Zchar low = new Zchar(Bits.FetchBits(BitNumber.Bit14, BitSize.Size5, word)); Zchar mid = new Zchar(Bits.FetchBits(BitNumber.Bit9, BitSize.Size5, word)); Zchar high = new Zchar(Bits.FetchBits(BitNumber.Bit4, BitSize.Size5, word)); return new Zchar[] { low, mid, high }; }
public static string PrintWordAsZchars(Word word) { return Zchar.PrintFromZchar(ReadWordAsZchars(word)); }
private static void TestBitTwiddling() { int myWord = 0xBEEF; int otherWord = 0xF000; Word negativeOne = new Word(0xFFFF); Word everyOther = new Word(0 + 2 + 8 + 32 + 128 + 512); Console.WriteLine(Print3ZcharsFromWord(negativeOne)); Console.WriteLine(Print3ZcharsFromWord(everyOther)); Word word = new Word(myWord); Word theOtherWord = new Word(otherWord); string firstBinary = Convert.ToString(myWord, 2); string otherWords = Convert.ToString(otherWord, 2); var toPrint = Bits.FetchBits(BitNumber.Bit15, BitSize.Size4, word); var otherToPrint = Bits.FetchBits(BitNumber.Bit15, BitSize.Size4, theOtherWord); string binary = Convert.ToString(toPrint, 2); string secondBinary = Convert.ToString(otherToPrint, 2); Console.WriteLine($"{binary} obtained from {firstBinary}"); Console.WriteLine($"{secondBinary} obtained from {otherWords}"); }
public static int FetchBits(BitNumber high, BitSize length, Word word) { int mask = ~(-1 << length.Size); int retval = (word.Value >> (high.Value - length.Size + 1)) & mask; return retval; }