private void ResetAndLoadButton_Click(object sender, EventArgs e)
        {
            try
            {
                _vm       = new StackVirtualMachine(LogToOutput);
                _vm.State = SVMSStates.Idle;
                var assembler = new SVMAssembler();
                var program   = assembler.Assemble(CodeInput.Text);

                _vm.Load(program);

                _vmBackgroundThread         = new BackgroundWorker();
                _vmBackgroundThread.DoWork += (o, args) =>
                {
                    _vm.Start();
                };
                _vmBackgroundThread.RunWorkerAsync();

                LogToOutput("Loaded. VM Running in idle state");
            }
            catch (Exception ex)
            {
                if (ex.GetType() == typeof(AssemblerSyntaxErrorException))
                {
                    LogToOutput(ex.Message);
                }
                else
                {
                    LogToOutput(ex.ToString());
                }
            }
        }
Beispiel #2
0
        public void PushByStackReference()
        {
            var assembler = new SVMAssembler();
            var output    = assembler.Assemble(
                "PSH $^\n" +
                "HLT");

            AssertDeepEqual <uint>(output, new uint[]
            {
                OpCodes.Push, OpModes.StackValue,
                OpCodes.Halt
            });
        }
Beispiel #3
0
        public void PushByReference()
        {
            var assembler = new SVMAssembler();
            var output    = assembler.Assemble(
                "PSH #FF00\n" +
                "HLT");

            AssertDeepEqual <uint>(output, new uint[]
            {
                OpCodes.Push, OpModes.Reference, 0xFF00u,
                OpCodes.Halt
            });
        }
Beispiel #4
0
        public void InvalidInstructionSyntaxError()
        {
            Exception ex = null;

            var assembler = new SVMAssembler();

            try
            {
                var output = assembler.Assemble(
                    "aaaPSH $^\n" +
                    "HLT");
            }
            catch (Exception e)
            {
                ex = e;
            }

            Assert.IsInstanceOfType(ex, typeof(AssemblerSyntaxErrorException));
        }
Beispiel #5
0
        private void Assemble(string input, string dest)
        {
            uint[] output;

            using (var fs = new FileStream(input, FileMode.Open, FileAccess.Read))
            {
                var assembler = new SVMAssembler();

                byte[] buffer = new byte[input.Length];
                fs.Seek(0, SeekOrigin.Begin);
                fs.Read(buffer, 0, input.Length);

                output = assembler.Assemble(Encoding.ASCII.GetString(buffer));
            }

            using (var fs = new FileStream(dest, FileMode.CreateNew, FileAccess.Write))
            {
                fs.Write(SVMAssembler.ToByteArray(output), 0, output.Length * 4);
            }
        }