コード例 #1
0
        public override Task OnConnectedAsync()
        {
            Healper    ob  = new Healper();
            MessageHub hub = new MessageHub();

            //get DB MSG

            messages = ob.GetAll();

            // messages.Add(new Message() { clientuniqueid = Guid.NewGuid().ToString(), date = DateTime.Now, message = "FirstMSG", type = "sent" });
            // messages.Add(new Message() { clientuniqueid = Guid.NewGuid().ToString(), date = DateTime.Now, message = "FirstMSG2", type = "sent" });

            var comments = messages; // some sort of cache would be good here

            foreach (var comment in comments)
            {
                var id = Context.ConnectionId;
                var x  = Clients.Client(id);
                // Clients.Client(id).SendAsync(comment.clientuniqueid, comment.message);

                //Clients.All.SendAsync("MessageReceived", comment.message);



                hub.NewMessage(comment, Clients);
            }
            return(base.OnConnectedAsync());
        }
コード例 #2
0
        public async void NewMessage(Message msg, IHubCallerClients Clients2)
        {
            if (Clients == null)// Intial call
            {
                await Clients2.All.SendAsync("MessageReceived", msg);
            }
            else
            {
                await Clients.All.SendAsync("MessageReceived", msg);

                // messages.Add(msg);
                Healper ob = new Healper();
                ob.insert(msg);
            }
        }
コード例 #3
0
        private void DoSecondParse(RiscVProgramResult res,
                                   List <RiscVProgramResult> debugRes,
                                   bool DebugMode, Dictionary <uint, UncompleteParseData> uncompleteParse,
                                   Dictionary <string, Lable> labelTable, Dictionary <uint, ExeCommand> commandsToExe)
        {
            int  commandToFix    = 0;
            bool lowLabelCommand = false;

            foreach (var commandToUpdate in uncompleteParse)
            {
                try
                {
                    for (int j = 0; j < 4; j++) //read command from memory, each command (32b) needs 4 bytes
                    {
                        commandToFix |= res.Memory[(uint)(commandToUpdate.Key + j)] << 8 * j;
                    }

                    lowLabelCommand = Healper.IsLowLabelCommand(commandToUpdate.Value.optionalLabel, out var label);
                    if (!labelTable.ContainsKey(label))
                    {
                        throw new SimulatorException {
                                  ErrorMessage = $"cannot find label {label}"
                        }
                    }
                    ;

                    switch (commandToUpdate.Value.command)
                    {
                    case "addi":
                    case "slti":
                    case "sltiu":
                    case "xori":
                    case "ori":
                    case "andi":
                    case "jalr":
                        if (lowLabelCommand)
                        {
                            commandsToExe[commandToUpdate.Key].Args[2] =
                                (labelTable[label].Address & Convert.ToInt32("111111111111", 2)).ToString();
                            commandToFix |= ((int)labelTable[label].Address & Convert.ToInt32("111111111111", 2)) << 20;
                        }
                        else
                        {
                            // we cant fit 20 high bit to imm 12 bit so we cut the bits from 12-19 (the high bits) and move them to the left(20-31) to fit the imm 12 bits
                            labelTable[label].Address &= 16773120;               //CUT
                            commandToFix |= (int)labelTable[label].Address << 8; //MOVE + MERGE
                            commandsToExe[commandToUpdate.Key].Args[2] =
                                ((labelTable[label].Address &
                                  Convert.ToInt32("00000000111111111111000000000000", 2)) >> 12).ToString();
                        }

                        break;

                    case "lui":
                        if (lowLabelCommand)
                        {
                            commandsToExe[commandToUpdate.Key].Args[1] =
                                (labelTable[label].Address & Convert.ToInt32("111111111111", 2)).ToString();
                            commandToFix |= ((int)labelTable[label].Address & Convert.ToInt32("111111111111", 2)) << 12;
                        }
                        else
                        {
                            commandsToExe[commandToUpdate.Key].Args[1] =
                                (UInt32.Parse(labelTable[label].Address.ToString()) >> 12).ToString();
                            commandToFix |= (int)labelTable[label].Address &
                                            Convert.ToInt32("11111111111111111111000000000000", 2);
                        }

                        break;

                    case "auipc":
                        if (lowLabelCommand)
                        {
                            commandsToExe[commandToUpdate.Key].Args[1] =
                                ((int)labelTable[label].Address & Convert.ToInt32("111111111111", 2)).ToString();
                            commandToFix |= ((int)labelTable[label].Address & Convert.ToInt32("111111111111", 2)) << 12;
                        }
                        else
                        {
                            //commandsToExe[commandToUpdate.Key].Args[1] = (labelTable[label].Address & Convert.ToInt32("11111111111111111111000000000000", 2)).ToString();
                            commandsToExe[commandToUpdate.Key].Args[1] =
                                (UInt32.Parse(labelTable[label].Address.ToString()) >> 12).ToString();
                            commandToFix |= (int)labelTable[label].Address &
                                            Convert.ToInt32("11111111111111111111000000000000", 2);
                        }

                        break;
                    }

                    var longBytes = BitConverter.GetBytes(commandToFix); //Enter Command to Stack
                    if (DebugMode)
                    {
                        foreach (var snapshotMemory in debugRes)
                        {
                            if (debugRes.IndexOf((snapshotMemory)) >= commandToUpdate.Value.lineNumber)
                            {
                                for (int j = 0; j < 4; j++)
                                {
                                    snapshotMemory.Memory[(uint)(commandToUpdate.Key + j)] = longBytes[j];
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            res.Memory[(uint)(commandToUpdate.Key + j)] = longBytes[j];
                        }
                    }
                }

                catch (SimulatorException e)
                {
                    throw new ErrorInResult {
                              Line = commandToUpdate.Value.lineNumber, Message = e.ErrorMessage
                    };
                }
                catch (Exception e)
                {
                    throw new ErrorInResult {
                              Line = commandToUpdate.Value.lineNumber, Message = "Internal Error"
                    };
                }
            }
        }
コード例 #4
0
        public async Task <ActionResult> ProgramToRun([FromBody] RiscVProgram req)
        {
            var cursor = 0;
            var commandInBinarryFormat = 0;
            var memorySection          = MemorySection.Text;
            Dictionary <string, Lable>             labelTable      = new Dictionary <string, Lable>();
            Dictionary <uint, UncompleteParseData> uncompleteParse = new Dictionary <uint, UncompleteParseData>();
            Dictionary <uint, ExeCommand>          commandsToExe   = new Dictionary <uint, ExeCommand>();
            RiscVProgramResult        res         = new RiscVProgramResult(req);
            List <RiscVProgramResult> debugRes    = new List <RiscVProgramResult>();
            Dictionary <string, uint> stringTable = new Dictionary <string, uint>();//only for ecall 7 use
            var TextArray        = req.Program.Split('\n').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
            var programTextArray = new string[TextArray.Length][];

            for (int i = 0; i < TextArray.Length; i++)
            {
                programTextArray[i] = TextArray[i].Split('#').FirstOrDefault()?.Split(' ', '\t', '\r').Where(s => !string.IsNullOrWhiteSpace(s))
                                      .ToArray();

                /*   programTextArray[i] = TextArray[i].Split(' ', '\t', '\r').Where(s => !string.IsNullOrWhiteSpace(s))
                 *     .ToArray();
                 * if (programTextArray[i].FirstOrDefault(x => x.StartsWith('#')) != null)
                 * {
                 *     var index = programTextArray[i].IndexOf(programTextArray[i].FirstOrDefault(x => x.StartsWith('#')));
                 *     programTextArray[i] = programTextArray[i].Take(index).ToArray();
                 * }*/
            }

            bool newLabel = false;

            for (int i = 0; i < programTextArray.Length; i++)
            {
                for (int j = 0; j < programTextArray[i].Length; j++)
                {
                    try
                    {
                        if (Healper.IsLabel(programTextArray, ref i, ref j, out var label))
                        {
                            if (!labelTable.TryAdd(label, null))
                            {
                                return(BadRequest(new ErrorInResult
                                {
                                    Line = i, Message = $"The label {label} already exist."
                                }));
                            }
                            newLabel = true;
                        }

                        if (programTextArray[i][j][0] == '.')
                        {
                            var directive = Healper.GetDirective(programTextArray, ref i, ref j);
                            if (DirectiveNumber.ContainsKey(directive))
                            {
                                if (newLabel)
                                {
                                    labelTable[labelTable.Keys.Last()] = new Lable
                                    {
                                        Address = Healper.GetAddress(res, memorySection), i = i, j = j
                                    };
                                    newLabel = false;
                                }

                                if (programTextArray[i].Length > ++j == false)
                                {
                                    if (programTextArray.Length > ++i == false)
                                    {
                                        throw new SimulatorException {
                                                  ErrorMessage = $"'incomplete command"
                                        }
                                    }
                                    ;
                                    j = 0;
                                }

                                if (directive == "string")
                                {
                                    var items = Healper.PrepareString(programTextArray, ref i, ref j);
                                    stringTable.Add(items.Substring(0, items.Length - 1), Healper.GetAddress(res, memorySection));
                                    foreach (var item in items)
                                    {
                                        var longBytes = BitConverter.GetBytes(item);
                                        for (int z = 0; z < DirectiveNumber[directive]; z++)
                                        {
                                            PutIntoAddress(res, memorySection,
                                                           longBytes.Length > z
                                                ? longBytes[z]
                                                : (byte)0);  //we fill the memory with space if the number is not big enough  to fit the DirectiveNumber
                                        }
                                    }
                                }
                                else
                                {
                                    var items = Healper.GetListOfNumber(programTextArray, ref i, ref j);
                                    foreach (var item in items)
                                    {
                                        var longBytes = BitConverter.GetBytes(item);
                                        for (int z = 0; z < DirectiveNumber[directive]; z++)
                                        {
                                            PutIntoAddress(res, memorySection,
                                                           longBytes.Length > z
                                                ? longBytes[z]
                                                : (byte)0);  //we fill the memory with space if the number is not big enough  to fit the DirectiveNumber
                                        }
                                    }
                                }
                            }
                            else
                            {
                                switch (directive)
                                {
                                case "data":
                                    memorySection = MemorySection.Static;
                                    break;

                                case "text":
                                    memorySection = MemorySection.Text;
                                    break;

                                default:
                                    throw new SimulatorException
                                          {
                                              ErrorMessage = $"'{directive}' is unknown  directive"
                                          };
                                }
                            }
                        }
                        else
                        {
                            commandsToExe.Add(GetIntoAddress(res, memorySection), null);
                            commandInBinarryFormat = ParseCommandWithNoImm(programTextArray, ref i, ref j, commandsToExe);
                            if (commandInBinarryFormat == 0)
                            {
                                commandInBinarryFormat = ParseCommandWithImm(out string optionalLabel, out string command,
                                                                             programTextArray, ref i, ref j, commandsToExe);
                                if (optionalLabel != null)
                                {
                                    uncompleteParse.Add(Healper.GetAddress(res, memorySection),
                                                        new UncompleteParseData(optionalLabel, command, i));
                                }
                            }

                            if (newLabel)
                            {
                                labelTable[label] = new Lable {
                                    Address = Healper.GetAddress(res, memorySection)
                                };
                                newLabel = false;
                            }

                            var longBytes = BitConverter.GetBytes(commandInBinarryFormat); //Enter Command to Stack
                            for (int z = 0; z < 4; z++)
                            {
                                PutIntoAddress(res, memorySection, longBytes[z]);
                            }
                        }
                    }
                    catch (SimulatorException e)
                    {
                        return(BadRequest(new ErrorInResult {
                            Line = i, Message = e.ErrorMessage
                        }));
                    }
                    catch (Exception e)
                    {
                        return(BadRequest(new ErrorInResult {
                            Line = i, Message = "Internal Error"
                        }));
                    }
                }
            }

            try
            {
                DoSecondParse(res, debugRes, req.DebugMode, uncompleteParse, labelTable, commandsToExe);
                res.Register[32].Value = (int)commandsToExe.FirstOrDefault().Key;
                ExeProgram(res, debugRes, commandsToExe, req.DebugMode, stringTable);
            }
            catch (ErrorInResult e)
            {
                return(BadRequest(new ErrorInResult {
                    Line = e.Line, Message = e.Message
                }));
            }

            catch (Exception e)
            {
                return(BadRequest(new ErrorInResult {
                    Message = "Internal Error"
                }));
            }



            if (res.alphanumericData.Line == -1)
            {
                if (req.DebugMode)
                {
                    return(Ok(debugRes));
                }
                else
                {
                    return(Ok(res));
                }
            }
            return(Ok(new ContinueProgramResult
            {
                res = res,
                commandsToExe = commandsToExe,
                debugRes = debugRes,
                DebugMode = req.DebugMode,
                stringTable = stringTable
            }));
        }