public void CanTokensize() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var textBytes = new byte[37]; textBytes[0] = (byte)textBytes.Length; var textMemory = new MemoryLocation(1, textBytes.AsMemory()); var textBuffer = new TextBuffer(textMemory); textBuffer.Write("fred,go fishing"); var dictionary = new ParseDictionary(machine); textBuffer.Tokenize(dictionary); Assert.Equal(4, textBuffer.Tokens.Count); Assert.Equal("fred", textBuffer.Tokens[1]); Assert.Equal(",", textBuffer.Tokens[5]); Assert.Equal("go", textBuffer.Tokens[6]); Assert.Equal("fishing", textBuffer.Tokens[9]); }
public void CanRemoveObjectFromParentWhenNotDirectChild() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var o247 = machine.ObjectTable.GetObject(247); var o1 = machine.ObjectTable.GetObject(1); var o248 = machine.ObjectTable.GetObject(248); o1.Sibling = 33; // 247 -> 248 // | // 1 // | // 33 machine.ObjectTable.RemoveFromParent(1); Assert.Equal(248, o247.Child); Assert.Equal(0, o1.Parent); Assert.Equal(33, o248.Sibling); Assert.Equal(247, o248.Parent); }
public void CanReturnLargeValue() { var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); var operand1 = new Operand(OperandType.Large, 0xFFFF, machine); var operand2 = new Operand(OperandType.Large, 0xFFFE, machine); var operand3 = new Operand(OperandType.Large, 0xFFD7, machine); var operand4 = new Operand(OperandType.Large, 0x0001, machine); var operand5 = new Operand(OperandType.Large, 0x007F, machine); var operand6 = new Operand(OperandType.Large, 0x7FFF, machine); Assert.Equal(-1, operand1.SignedValue); Assert.Equal(65535, operand1.Value); Assert.Equal(-2, operand2.SignedValue); Assert.Equal(65534, operand2.Value); Assert.Equal(-41, operand3.SignedValue); Assert.Equal(65495, operand3.Value); Assert.Equal(1, operand4.SignedValue); Assert.Equal(1, operand4.Value); Assert.Equal(127, operand5.SignedValue); Assert.Equal(127, operand5.Value); Assert.Equal(32767, operand6.SignedValue); Assert.Equal(32767, operand6.Value); }
public void AbbreviationsMakeSense() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); logger.Error($"ABBREVIATIONS"); var decoder = new ZStringDecoder(machine); for (var index = 1; index <= 3; index++) { for (var number = 0; number < 32; number++) { var offset = (32 * (index - 1)) + (number * 2); logger.Error($"For [{index}][{number}] the offset is {offset}"); var ppAbbreviation = machine.Memory.WordAt(Header.ABBREVIATIONS); logger.Error($"For [{index}][{number}] the ppointer is {ppAbbreviation:X}"); var pAbbreviation = machine.Memory.WordAddressAt(ppAbbreviation + offset); logger.Error($"For [{index}][{number}] the pointer is {pAbbreviation:X}"); var location = machine.Memory.SpanAt(pAbbreviation); var result = decoder.Decode(location).Text; logger.Error($"Abbreviation [{index}][{number}] : {result}"); } } }
public void CanDecodePairOfHands() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var decoder = new ZStringDecoder(machine); Assert.Equal("pair of hands", decoder.Decode(machine.Memory.SpanAt(0xBB9)).Text); }
public void CanDecodeTenBit() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var decoder = new ZStringDecoder(machine); var result = decoder.Decode(machine.Memory.SpanAt(0x5908)).Text; Assert.Equal(">", result); }
public void CanPushAndPop() { var s = new FrameCollection(NullLoggerFactory.GetLogger()); s.PushFrame(new StackFrame(0x55FF, 2, 1)); s.Locals[0] = 22; s.RoutineStack.Push(42); var frame = s.PopFrame(); Assert.Equal(0x55FF, frame.ReturnPC); Assert.Equal(22, frame.Locals[0]); Assert.Equal(42, frame.RoutineStack.Pop()); }
public void CanDecodeDictionary() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var dictionary = new ParseDictionary(machine); Assert.Equal(0x2B9, dictionary.Words.Count); Assert.True(dictionary.Words.ContainsKey("altar")); Assert.False(dictionary.Words.ContainsKey("ackack")); }
public void DecodesOpeningZorkInstruction() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var decoder = new InstructionDecoder(machine); var memory = machine.Memory.SpanAt(machine.PC); var instruction = decoder.Decode(memory); instruction.Prepare(memory); instruction.Execute(memory); Assert.NotNull(instruction as VarInstruction); Assert.Equal("Call", instruction.Operation.Name); Assert.Equal(0, instruction.OpCode); Assert.Equal(0, instruction.StoreResult); Assert.Equal(3, instruction.Operands.Count); Assert.Equal(0x5479, machine.PC); memory = machine.Memory.SpanAt(machine.PC); instruction = decoder.Decode(memory); instruction.Prepare(memory); instruction.Execute(memory); Assert.NotNull(instruction as VarInstruction); Assert.Equal("Call", instruction.Operation.Name); Assert.Equal(2, instruction.Operands.Count); Assert.Equal(3, instruction.StoreResult); Assert.Equal(OperandType.Variable, instruction.Operands[1].Type); Assert.Equal(1, instruction.Operands[1].RawValue); memory = machine.Memory.SpanAt(machine.PC); instruction = decoder.Decode(memory); instruction.Prepare(memory); instruction.Execute(memory); Assert.NotNull(instruction as Op2Instruction); Assert.Equal("Add", instruction.Operation.Name); Assert.Equal(4, instruction.Size); Assert.Equal(2, instruction.Operands.Count); Assert.Equal(OperandType.Variable, instruction.Operands[0].Type); Assert.Equal(0x94, instruction.Operands[0].RawValue); Assert.Equal(OperandType.Small, instruction.Operands[1].Type); Assert.Equal(0xB4, instruction.Operands[1].RawValue); Assert.Equal(0x03, instruction.StoreResult); }
public void ReadsHeader() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var memory = machine.Memory; Assert.Equal(3, memory.Version); Assert.Equal(20023, memory.HighMemoryAddress); Assert.Equal(0x4F05, memory.StartingProgramCounter); Assert.Equal(0x02B0, memory.ObjectTableAddress); Assert.Equal(15137, memory.DictionaryAddress); }
public void Decodes_Method_At_5472() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var memory = machine.Memory.SpanAt(0x5472); var descriptor = new MethodDescriptor(memory, machine); Assert.Equal(3, descriptor.LocalsCount); Assert.Equal(0, descriptor.InitialValues.First()); Assert.Equal(0, descriptor.InitialValues.Last()); Assert.Equal(0x5479, descriptor.StartAddress); }
public void CanLoadObjectTable() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); Assert.Equal(0, machine.ObjectTable.GetDefault(10)); Assert.Equal(0, machine.ObjectTable.GetDefault(30)); Assert.Equal(0, machine.ObjectTable.GetDefault(0)); var object1 = machine.ObjectTable.GetObject(1); Assert.Equal(0xBB8, object1.PropertyPointer); Assert.Equal(247, object1.Parent); Assert.Equal(2, object1.Sibling); Assert.Equal(0, object1.Child); Assert.True(object1.ReadAttribute(14)); Assert.True(object1.ReadAttribute(28)); object1.SetAttribute(28, false); Assert.False(object1.ReadAttribute(28)); Assert.False(object1.ReadAttribute(0)); Assert.Equal("pair of hands", object1.Description); Assert.False(object1.Properties.ContainsKey(17)); Assert.True(MemoryEqual(new byte[] { 0x46, 0xDC, 0x42, 0xC2, 0x42, 0xB4 }, object1.Properties[18].Value)); Assert.True(MemoryEqual(new byte[] { 0x82 }, object1.Properties[16].Value)); var object227 = machine.ObjectTable.GetObject(227); Assert.Equal(0x205F, object227.PropertyPointer); Assert.True(object227.ReadAttribute(11)); Assert.True(object227.ReadAttribute(12)); Assert.False(object227.ReadAttribute(14)); object227.SetAttribute(14, true); Assert.True(object227.ReadAttribute(14)); Assert.Equal("basket", object227.PropertyTable.Description); var object250 = machine.ObjectTable.GetObject(250); Assert.Equal(249, object250.Parent); Assert.Equal(73, object250.Sibling); Assert.Equal(0, object250.Child); Assert.Equal(0x2263, object250.PropertyPointer); Assert.Equal("board", object250.PropertyTable.Description); Assert.True(MemoryEqual(new byte[] { 0x3C, 0xDA, 0x3C, 0xCC }, object250.Properties[18].Value)); }
public void SetsGlobalWordUnSigned() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); machine.SetVariable(20, 1); Assert.Equal(1, machine.ReadVariable(20)); machine.SetVariable(20, 0xFFFF); Assert.Equal(0xFFFF, machine.ReadVariable(20)); machine.SetVariable(20, 0xFFFF); Assert.Equal(-1, (short)machine.ReadVariable(20)); }
public void CanRemoveObjectFromParent() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var o249 = machine.ObjectTable.GetObject(249); var o250 = machine.ObjectTable.GetObject(250); // 249 - 250 // | // 73 machine.ObjectTable.RemoveFromParent(250); Assert.Equal(0, o250.Parent); Assert.Equal(73, o249.Child); }
public void CanReturnSmallValue() { var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); var operand1 = new Operand(OperandType.Small, 0xFF, machine); var operand2 = new Operand(OperandType.Small, 0xFE, machine); var operand3 = new Operand(OperandType.Small, 0xD7, machine); var operand4 = new Operand(OperandType.Small, 0x01, machine); var operand5 = new Operand(OperandType.Small, 0x7F, machine); var operand6 = new Operand(OperandType.Small, 0x10, machine); Assert.Equal(1, operand4.SignedValue); Assert.Equal(1, operand4.Value); Assert.Equal(127, operand5.SignedValue); Assert.Equal(127, operand5.Value); Assert.Equal(16, operand6.SignedValue); Assert.Equal(16, operand6.Value); }
public void CanAddObjectToNewParent() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); // 249 - 250 // | // 73 var o249 = machine.ObjectTable.GetObject(249); var o250 = machine.ObjectTable.GetObject(250); var o33 = machine.ObjectTable.GetObject(33); machine.ObjectTable.AddToParent(33, 249); Assert.Equal(33, o249.Child); Assert.Equal(250, o33.Sibling); Assert.Equal(249, o33.Parent); Assert.Equal(249, o250.Parent); }
public void CanPopulateParseBuffer() { using var file = File.OpenRead(@"Data\ZORK1.DAT"); var logger = NullLoggerFactory.GetLogger(); var machine = new Machine(logger, new SolveZorkInputStream()); machine.Load(file); var textBytes = new byte[37]; textBytes[0] = (byte)textBytes.Length; var textMemory = new MemoryLocation(1, textBytes.AsMemory()); var textBuffer = new TextBuffer(textMemory); textBuffer.Write("open mailbox"); var dictionary = new ParseDictionary(machine); textBuffer.Tokenize(dictionary); var parseBytes = new byte[128]; parseBytes[0] = (byte)parseBytes.Length; var parseMemory = new MemoryLocation(1, parseBytes.AsMemory()); var parseBuffer = new ParseBuffer(parseMemory); parseBuffer.Populate(textBuffer, dictionary); Assert.Equal(2, parseBytes[0]); Assert.Equal(0x46, parseBytes[1]); Assert.Equal(0x88, parseBytes[2]); Assert.Equal(0x04, parseBytes[3]); Assert.Equal(0x01, parseBytes[4]); }