Пример #1
0
        public Processor(RealMemory realMemory, ChannelTool channelTool, VirtualMemory virtualMemory = null, Pager pager = null)
        {
            RealMemory           = realMemory;
            VirtualMemory        = virtualMemory;
            ChannelTool          = channelTool;
            Pager                = pager;
            FileManager          = new FileManager(RealMemory, this);
            CommandInterpretator = new CommandInterpretator(this, virtualMemory);
            Interruptor          = new Interruptor(this, virtualMemory, FileManager, RealMemory);

            // STOPS PROGRAM AFTER MAX STEP COUNT
            UseMaxStep = true;
            // CURRENT STEP
            CurrentStep = 0;

            registers = new Dictionary <string, Register>
            {
                { "R1", new Register(value: 0) },                                           // Word length general register
                { "R2", new Register(value: 0) },                                           // Word length general register
                { "IC", new HexRegister(2) },                                               // Current command adress in memory register
                { "PTR", new HexRegister(4) },                                              // Page table adress register
                { "SF", new StatusFlagRegister() },                                         // Aritmetic operation logic values
                { "MODE", new ChoiceRegister('N', 'S') },                                   // Processor mode "N" - user, "S" - supervisor
                { "PI", new ChoiceRegister(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) },     // Program interuptor
                { "SI", new ChoiceRegister(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) },     // Supervisor interuptor
                { "TI", new HexRegister(Utility.TIMER_VALUE, 1) }                           // Timer interuptor
            };
        }
Пример #2
0
        private Tuple <int, int> CreateTaskProgramInSupervisorMemory(int supervisorAddress, int blockSize)
        {
            string oneLine = string.Empty;

            for (int i = 0; i < blockSize; i++)
            {
                char[][] block = RealMemory.GetSupervisorMemoryBlockValues(supervisorAddress + i * Utility.BLOCK_SIZE);
                oneLine += block.CharsToString();
            }

            string[] lines = oneLine.Split('\n');

            var           segments = SegmentTask(lines);
            List <string> code     = segments.Item1;
            List <string> data     = segments.Item2;

            char[][] cleanCode      = CleanCode(code);
            int      dataStartBlock = Utility.VIRTUAL_MEMORY_BLOCKS / 2;

            char[][] cleanData = CleanData(data, ref dataStartBlock);

            char[][][] taskMemory = CreateTaskMemory(cleanCode, cleanData, dataStartBlock);

            PutTaskProgramToSupervisorMemory(taskMemory, supervisorAddress);

            return(Tuple.Create(supervisorAddress, Utility.VIRTUAL_MEMORY_BLOCKS));
        }
Пример #3
0
        private void SetBlockByIndex(int index, char[][] block)
        {
            int blockAddress = Utility.GetAddressTuple(RealFileMemoryAddresses[index]).Item1;

            for (int i = 0; i < Utility.BLOCK_SIZE; i++)
            {
                RealMemory.GetMemoryCell(blockAddress, i).SetValue(block[i]);
            }
        }
Пример #4
0
        public int GetCellRealAddress(int virtualAddress)
        {
            var addressTuple = Utility.GetAddressTuple(virtualAddress);
            int virtualBlock = addressTuple.Item1;
            int cell         = addressTuple.Item2;
            int realBlock    = RealMemory.GetUserMemoryValue(PTR * Utility.BLOCK_SIZE + virtualBlock).HexToInt();

            return(realBlock * Utility.BLOCK_SIZE + cell);
        }
Пример #5
0
 public FileManager(RealMemory realMemory, Processor processor)
 {
     RealMemory = realMemory;
     Processor  = processor;
     RealFileMemoryAddresses = new int[Utility.FILE_MANAGER_BLOCKS];
     OpenFileStreams         = new FileStream[Utility.FILE_MANAGER_BLOCKS];
     GetBlocks();
     FileReaders = new Dictionary <int, StreamReader>();
     FileWriters = new Dictionary <int, StreamWriter>();
 }
Пример #6
0
 private void GetBlocks()
 {
     for (int i = 0; i < Utility.FILE_MANAGER_BLOCKS; i++)
     {
         int blockIndex = (Utility.BLOCKS - Utility.FILE_MANAGER_BLOCKS + i);
         RealFileMemoryAddresses[i] = blockIndex * Utility.BLOCK_SIZE;
         RealMemory.TakeMemoryBlock(blockIndex);
         CleanBlock(RealFileMemoryAddresses[i]);
     }
 }
Пример #7
0
        private char[][] GetBlockByIndex(int index)
        {
            char[][] block        = new char[Utility.BLOCK_SIZE][];
            int      blockAddress = Utility.GetAddressTuple(RealFileMemoryAddresses[index]).Item1;

            for (int i = 0; i < Utility.BLOCK_SIZE; i++)
            {
                block[i] = RealMemory.GetMemoryCell(blockAddress, i).GetValue();
            }
            return(block);
        }
Пример #8
0
 public RealMachine(Input inputHandler = null, Output outputHandler = null)
 {
     RealMemory     = new RealMemory();
     ExternalMemory = new ExternalMemory();
     InputHandler   = inputHandler;
     OutputHandler  = outputHandler;
     ChannelTool    = new ChannelTool(RealMemory, ExternalMemory, InputHandler, OutputHandler);
     Processor      = new Processor(RealMemory, ChannelTool);
     ChannelTool.SetProcessor(Processor);
     VirtualMemory = null;
     Pager         = null;
 }
Пример #9
0
        private void CleanBlock(int memoryAddress)
        {
            var address = Utility.GetAddressTuple(memoryAddress);
            int block   = address.Item1;

            char[] emptyValue = string.Empty.ToCharArray().AddWhiteSpacesToSize(Utility.WORD_SIZE);

            for (int i = 0; i < Utility.BLOCK_SIZE; i++)
            {
                RealMemory.GetMemoryCell(block, i).SetValue(emptyValue);
            }
        }
Пример #10
0
 public ChannelTool(RealMemory realMemmory, ExternalMemory externalMemory, Input inputHandler, Output outputHandler, Processor processor = null)
 {
     RealMemory     = realMemmory;
     ExternalMemory = externalMemory;
     InputHandler   = inputHandler;
     OutputHandler  = outputHandler;
     Processor      = processor;
     registers      = new Dictionary <string, Register> {
         { "SB", new HexRegister() },                                    // Copy source block address
         { "DB", new HexRegister() },                                    // Copy destination block address
         { "ST", new ChoiceRegister(1, 2, 3, 4, 5, 6, 7) },              // Source object number: 1-user memory, 2-supervisor, 3-external, 4-input
         { "DT", new ChoiceRegister(1, 2, 3, 4, 5, 6, 7) },              // Destination object number: 1-user memory, 2-supervisor, 3-external, 4-output
     };
 }
Пример #11
0
        public bool LoadVirtualMachine(string filePath)
        {
            //Get all file to one string
            string uncutTask = ReadTaskFile(filePath);

            if (uncutTask == string.Empty)
            {
                return(false);
            }

            //Cut string to blocks words and bytes
            char[][][] taskCutToBlocks         = CutToBlocks(uncutTask);
            int        taskBlockLengthUnsorted = taskCutToBlocks.Length;

            //Store blocks to supervisor memory
            PutTaskToSupervisorMemory(taskCutToBlocks);

            //Create task program in supervisor memory
            Tuple <int, int> result     = CreateTaskProgramInSupervisorMemory(0, taskBlockLengthUnsorted);
            int supervisorAddressSorted = result.Item1;
            int taskBlockLengthSorted   = result.Item2;

            //Copy task program to external memory and clear supervisor memory
            CopyTaskToExternalMemory(supervisorAddressSorted, taskBlockLengthSorted);

            //Create virtual memory
            VirtualMemory = RealMemory.CreateVirtualMemory(Utility.VIRTUAL_MEMORY_BLOCKS);

            Pager = VirtualMemory.GetPager();

            //Move task program from external memory to user / virtual memory
            UploadTaskToVirtualMemory(0, Utility.VIRTUAL_MEMORY_BLOCKS);

            GetProcessor().SetVirtualMemory(VirtualMemory, Pager);

            GetProcessor().SetICRegisterValue(0);

            return(true);
        }
Пример #12
0
 private void CleanTaskMemoryFromSupervisorMemory(int supervisorAddress, int blockCount) => RealMemory.CleanDataFromSupervisorMemory(supervisorAddress, blockCount);
Пример #13
0
 private void PutTaskProgramToSupervisorMemory(char[][][] blocks, int supervisorAddress) => RealMemory.PutDataToSupervisorMemory(blocks, 0);
Пример #14
0
 private void PutTaskToSupervisorMemory(char[][][] blocks) => RealMemory.PutDataToSupervisorMemory(blocks, 0);
Пример #15
0
 public void AddBlock(int index, int blockRealAddress) => RealMemory.SetUserMemoryValue(PTR * Utility.BLOCK_SIZE + index, blockRealAddress.IntToHex());
Пример #16
0
 public Pager(RealMemory realMemory, int pagerAddress = 0)
 {
     RealMemory = realMemory;
     PTR        = 0;
 }
Пример #17
0
        public void XCHG()
        {
            char[][] sourceBlock = null;
            char[]   sourceWord  = null;

            int sourceAddress      = ((HexRegister)registers["SB"]).GetIntValue();
            int destinationAddress = ((HexRegister)registers["DB"]).GetIntValue();

            int STvalue = ((ChoiceRegister)registers["ST"]).GetIntValue();
            int DTvalue = ((ChoiceRegister)registers["DT"]).GetIntValue();

            if ((STvalue == '5' || STvalue == '6' || STvalue == '7') ^ (DTvalue == '5' || DTvalue == '6' || DTvalue == '7'))
            {
                throw new Exception("Channel tool cannot work with word and block at the same time.");
            }

            switch (STvalue)
            {
            case 1:
                sourceBlock = RealMemory.GetUserMemoryBlockValues(sourceAddress);
                break;

            case 2:
                sourceBlock = RealMemory.GetSupervisorMemoryBlockValues(sourceAddress);
                break;

            case 3:
                sourceBlock = ExternalMemory.GetBlockValues(sourceAddress);
                break;

            case 4:
                sourceBlock = InputHandler.ReadBlock();
                break;

            case 5:
                sourceWord = InputHandler.ReadWord();
                break;

            case 6:
                sourceWord = Processor.GetRegisterValue("R1");
                break;

            case 7:
                sourceWord = Processor.GetRegisterValue("R2");
                break;

            default:
                throw new Exception("Channel tool ST register value is incorrect.");
            }

            switch (DTvalue)
            {
            case 1:
                RealMemory.SetUserMemoryBlockValues(destinationAddress, sourceBlock);
                break;

            case 2:
                RealMemory.SetSupervisorMemoryBlockValues(destinationAddress, sourceBlock);
                break;

            case 3:
                ExternalMemory.SetBlockValues(destinationAddress, sourceBlock);
                break;

            case 4:
                OutputHandler.WriteBlock(sourceBlock);
                break;

            case 5:
                OutputHandler.WriteWord(sourceWord);
                break;

            case 6:
                Processor.SetRegisterValue("R1", sourceWord);
                break;

            case 7:
                Processor.SetRegisterValue("R2", sourceWord);
                break;

            default:
                throw new Exception("Channel tool ST register value is incorrect.");
            }
        }