/// <summary>
        /// Processes the command text
        /// </summary>
        /// <param name="commandText">The command text</param>
        /// <param name="validationMessage">
        ///     Null, if the command is valid; otherwise the validation message to show
        /// </param>
        /// <param name="topAddress">
        /// Non-null value indicates that the view should be scrolled to that address
        /// </param>
        /// <returns>
        /// True, if the command has been handled; otherwise, false
        /// </returns>
        public bool ProcessCommandline(string commandText, out string validationMessage,
                                       out ushort?topAddress)
        {
            const string INV_S48_COMMAND = "This command cannot be used for a Spectrum 48K model.";
            const string INV_RUN_COMMAND = "This command can only be used when the virtual machine is running.";

            // --- Prepare command handling
            validationMessage = null;
            topAddress        = null;
            var isSpectrum48 = SpectNetPackage.IsSpectrum48Model();
            var banks        = MachineViewModel.SpectrumVm.MemoryConfiguration.RamBanks;
            var roms         = MachineViewModel.SpectrumVm.RomConfiguration.NumberOfRoms;

            var parser = new MemoryCommandParser(commandText);

            switch (parser.Command)
            {
            case MemoryCommandType.Invalid:
                validationMessage = "Invalid command syntax";
                return(false);

            case MemoryCommandType.Goto:
                topAddress = parser.Address;
                break;

            case MemoryCommandType.GotoSymbol:
                if (CompilerOutput == null)
                {
                    validationMessage = "No compilation has been done, symbols cannot be used with the 'G' command";
                    return(false);
                }

                if (!CompilerOutput.Symbols.TryGetValue(parser.Arg1, out var symbolValue))
                {
                    validationMessage = $"Cannot find symbol '{parser.Arg1}'";
                    return(false);
                }
                topAddress = symbolValue;
                break;

            case MemoryCommandType.SetRomPage:
                if (isSpectrum48)
                {
                    validationMessage = INV_S48_COMMAND;
                    return(false);
                }
                if (parser.Address > roms - 1)
                {
                    validationMessage = $"This machine does not have a ROM bank #{parser.Address}";
                    return(false);
                }
                SetRomViewMode(parser.Address);
                topAddress = 0;
                break;

            case MemoryCommandType.SetRamBank:
                if (isSpectrum48)
                {
                    validationMessage = INV_S48_COMMAND;
                    return(false);
                }
                if (VmStopped)
                {
                    validationMessage = INV_RUN_COMMAND;
                    return(false);
                }
                if (parser.Address > banks - 1)
                {
                    validationMessage = $"This machine does not have a RAM bank #{parser.Address}";
                    return(false);
                }
                SetRamBankViewMode(parser.Address);
                topAddress = 0;
                break;

            case MemoryCommandType.MemoryMode:
                if (isSpectrum48)
                {
                    validationMessage = INV_S48_COMMAND;
                    return(false);
                }
                if (VmStopped)
                {
                    validationMessage = INV_RUN_COMMAND;
                    return(false);
                }
                SetFullViewMode();
                break;

            default:
                return(false);
            }
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Processes the command text
        /// </summary>
        /// <param name="commandText">The command text</param>
        /// <param name="validationMessage">
        ///     Null, if the command is valid; otherwise the validation message to show
        /// </param>
        /// <param name="topAddress">
        /// Non-null value indicates that the view should be scrolled to that address
        /// </param>
        /// <returns>
        /// True, if the command has been handled; otherwise, false
        /// </returns>
        public bool ProcessCommandline(string commandText, out string validationMessage,
                                       out ushort?topAddress)
        {
            // --- Prepare command handling
            validationMessage = null;
            topAddress        = null;
            var isSpectrum48 = SpectNetPackage.IsSpectrum48Model();
            var banks        = SpectrumVm.MemoryConfiguration.RamBanks;
            var roms         = SpectrumVm.RomConfiguration.NumberOfRoms;

            var command = ParseCommand(commandText);

            if (command is CompactToolCommand compactCommand)
            {
                command = ParseCommand(compactCommand.CommandText);
            }
            if (command == null || command.HasSemanticError)
            {
                validationMessage = INV_SYNTAX;
                return(false);
            }

            switch (command)
            {
            case GotoToolCommand gotoCommand:
                if (gotoCommand.Symbol != null)
                {
                    if (ResolveSymbol(gotoCommand.Symbol, out var symbolValue))
                    {
                        topAddress = symbolValue;
                        break;
                    }

                    validationMessage = string.Format(UNDEF_SYMBOL, gotoCommand.Symbol);
                    return(false);
                }
                topAddress = gotoCommand.Address;
                break;

            case RomPageToolCommand romPageCommand:
                if (isSpectrum48)
                {
                    validationMessage = INV_S48_COMMAND;
                    return(false);
                }
                if (romPageCommand.Page > roms - 1)
                {
                    validationMessage = $"This machine does not have a ROM bank #{romPageCommand.Page}";
                    return(false);
                }
                SetRomViewMode(romPageCommand.Page);
                topAddress = 0;
                break;

            case BankPageToolCommand bankPageCommand:
                if (isSpectrum48)
                {
                    validationMessage = INV_S48_COMMAND;
                    return(false);
                }
                if (MachineState == VmState.Stopped)
                {
                    validationMessage = INV_RUN_COMMAND;
                    return(false);
                }
                if (bankPageCommand.Page > banks - 1)
                {
                    validationMessage = $"This machine does not have a RAM bank #{bankPageCommand.Page}";
                    return(false);
                }
                SetRamBankViewMode(bankPageCommand.Page);
                topAddress = 0;
                break;

            case MemoryModeToolCommand _:
                if (isSpectrum48)
                {
                    validationMessage = INV_S48_COMMAND;
                    return(false);
                }
                if (MachineState == VmState.Stopped)
                {
                    validationMessage = INV_RUN_COMMAND;
                    return(false);
                }
                SetFullViewMode();
                break;

            case ExportToolCommand exportMemoryCommand:
            {
                if (!ObtainAddress(exportMemoryCommand.From, null,
                                   out var startAddress,
                                   out validationMessage))
                {
                    return(false);
                }
                if (!ObtainAddress(exportMemoryCommand.To, null,
                                   out var endAddress,
                                   out validationMessage))
                {
                    return(false);
                }

                if (DisplayExportMemoryDialog(out var vm, startAddress, endAddress))
                {
                    // --- Export cancelled
                    break;
                }

                var exporter = new MemoryExporter(vm);
                exporter.ExportMemory(EmulatorViewModel.Machine.SpectrumVm);
                ExportMemoryViewModel.LatestFolder = Path.GetDirectoryName(vm.Filename);
                break;
            }

            default:
                validationMessage = string.Format(INV_CONTEXT, "ZX Spectrum Memory window");
                return(false);
            }
            return(true);
        }