Пример #1
0
        public static void FSUB(string fir, string sec)
        {
            SoReg rf = reg[fir] as SoReg;
            SoReg rs = reg[sec] as SoReg;

            if (rf.val < rs.val)
            {
                string s = fir;
                fir = sec;
                sec = s;
            }

            if (rf.val < 0 && rs.val > 0)
            {
                Visual.VisualCommandFAdd(1, fir, sec);
            }
            else
            if (rf.val > 0 && rs.val < 0)
            {
                Visual.VisualCommandFAdd(0, fir, sec);
            }
            else
            if (rf.val < 0 && rs.val < 0)
            {
                Visual.VisualCommandFSub(1, fir, sec);
            }
            else
            {
                Visual.VisualCommandFSub(0, fir, sec);
            }

            double ans = rf.val - rs.val;

            rf.In(ans);
        }
Пример #2
0
        public static void FMUL(string fir, string sec)
        {
            Visual.VisualCommandFMul(fir, sec);
            SoReg rf = reg[fir] as SoReg;
            SoReg rs = reg[sec] as SoReg;

            double ans = rf.val * rs.val;

            rf.In(ans);
        }
Пример #3
0
        public static void FDIV(string fir, string sec)
        {
            SoReg rf = reg[fir] as SoReg;
            SoReg rs = reg[sec] as SoReg;

            if (Math.Abs(rs.val) <= 0.00000001)
            {
                Console.WriteLine("Divide by zero!");
                return;
            }

            Visual.VisualCommandFDiv(fir, sec);

            double ans = rf.val / rs.val;

            rf.In(ans);
        }
Пример #4
0
        public static void FADD(string fir, string sec)
        {
            SoReg rf = reg[fir] as SoReg;
            SoReg rs = reg[sec] as SoReg;

            if (rf.val < 0 && rs.val > 0)
            {
                Visual.VisualCommandFSub(1, fir, sec);
            }
            else
            if (rf.val > 0 && rs.val < 0)
            {
                Visual.VisualCommandFSub(0, fir, sec);
            }
            else
            {
                Visual.VisualCommandFAdd(1, fir, sec);
            }

            double ans = rf.val + rs.val;

            rf.In(ans);
        }
Пример #5
0
        public static void VisualCommandFDiv(string a1, string a2)
        {
            SoReg fir = SCPU.reg[a1] as SoReg;
            SoReg sec = SCPU.reg[a2] as SoReg;

            string f   = fir.man;
            string s   = sec.man;
            int    exp = fir.exp - sec.exp;

            Console.WriteLine("First  value S {0} E {1} M{2}", fir.sign, fir.or, fir.man);
            Console.WriteLine("Second value S {0} E {1} M{2}", sec.sign, sec.or, sec.man);
            //Console.WriteLine(Convert.ToString(exp + 127, 2));
            //Console.WriteLine("{0, 55}", fir.man);
            //Console.WriteLine("{0, 55}", sec.man);

            int i = 0;

            f += new string('0', 23);

            bool   exit = SCPU.MExit;
            int    step = 0;
            string help = "";
            string ans  = "";
            int    u    = 0;

            Console.WriteLine(f.PadLeft(f.Length + 9));
            Console.WriteLine(s.PadLeft(s.Length + 9));

            for (; u < 23; ++u)
            {
                help += f[u];
            }
            while (true)
            {
                Console.WriteLine("Step {0}: {1}{2}", (++step).ToString("D2"), new string(' ', i++), help);
                if (u >= f.Length)
                {
                    break;
                }
                if (!exit)
                {
                    if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                    {
                        exit = true;
                    }
                }

                if (Checker.Equals(help, s))
                {
                    ans += "1";
                    int           over = 0;
                    StringBuilder A    = new StringBuilder(new string('0', help.Length));
                    s = new String('0', help.Length - s.Length) + s;
                    for (int j = help.Length - 1; j > -1; --j)
                    {
                        if (help[j] == '1' && s[j] == '1')
                        {
                            if (over == 0)
                            {
                                A[j] = '0';
                                over = 0;
                            }
                            else
                            {
                                A[j] = '1';
                                over = 1;
                            }
                        }
                        else
                        if (help[j] == '0' && s[j] == '0')
                        {
                            if (over == 0)
                            {
                                A[j] = '0';
                                over = 0;
                            }
                            else
                            {
                                A[j] = '1';
                                over = 1;
                            }
                        }
                        else
                        if (help[j] == '1' && s[j] == '0')
                        {
                            if (over == 0)
                            {
                                A[j] = '1';
                                over = 0;
                            }
                            else
                            {
                                A[j] = '0';
                                over = 0;
                            }
                        }
                        else
                        if (help[j] == '0' && s[j] == '1')
                        {
                            if (over == 0)
                            {
                                A[j] = '1';
                                over = 1;
                            }
                            else
                            {
                                A[j] = '0';
                                over = 1;
                            }
                        }
                    }

                    help  = A.ToString();
                    help  = help.TrimStart('0');
                    s     = s.TrimStart('0');
                    help += f[u++];
                }
                else
                {
                    ans  += "0";
                    help += f[u++];
                }
            }

            Console.WriteLine("Answer: " + ans);


            ans = ans.TrimStart('0');

            if (ans.Length > 22)
            {
                ans = ans.Substring(0, 23);
            }

            int v = 0;

            if (fir.val / sec.val < 0)
            {
                v = 1;
            }

            Console.WriteLine("Answer: S {0} E {1} M {2} = {3}", v, Convert.ToString(exp + 127, 2), ans + new string('0', 23 - ans.Length), fir.val / sec.val);
        }
Пример #6
0
        public static void VisualCommandFMul(string a1, string a2)
        {
            SoReg fir = SCPU.reg[a1] as SoReg;
            SoReg sec = SCPU.reg[a2] as SoReg;

            string first  = fir.man;
            string second = sec.man;
            int    exp    = fir.exp + sec.exp - 2;

            Console.WriteLine("First  value S {0} E {1} M{2}", fir.sign, fir.or, fir.man);
            Console.WriteLine("Second value S {0} E {1} M{2}", sec.sign, sec.or, sec.man);
            //Console.WriteLine(Convert.ToString(exp + 127, 2));
            Console.WriteLine("{0, 55}", fir.man);
            Console.WriteLine("{0, 55}", sec.man);
            string        help = new string('0', first.Length + second.Length);
            StringBuilder ans  = new StringBuilder(help);

            int  len  = ans.Length;
            int  i    = 23;
            int  step = 0;
            bool exit = SCPU.MExit;

            while (true)
            {
                i--;
                if (i < 0)
                {
                    break;
                }
                if (!exit)
                {
                    if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                    {
                        exit = true;
                    }
                }
                help = "";

                for (int j = 0; j < first.Length; j++)
                {
                    if (first[j] == '0' || second[i] == '0')
                    {
                        help += '0';
                    }
                    else
                    {
                        help += '1';
                    }
                }

                int k = first.Length - 1;
                int over = 0, j1 = 0;
                for (int j = len - 1; k > -1; j--, k--)
                {
                    int prval = ((ans[j] - '0') + over + (help[k] - '0'));
                    ans[j] = (char)(prval % 2 + '0');
                    over   = prval / 2;
                    j1     = j;
                }
                if (over > 0)
                {
                    ans[j1 - 1] = '1';
                }
                Console.WriteLine("step {0}| {1}", (++step).ToString("D2"), help.PadLeft(len--));
            }
            Console.WriteLine(new string('-', ans.Length + 9));
            Console.WriteLine("{0, -7}| {1}", "answer", ans);

            string Ans = ans.ToString();

            Ans = Ans.TrimStart('0');

            if (Ans.Length > 22)
            {
                Ans = Ans.Substring(0, 23);
            }

            int v = 0;

            if (fir.val * sec.val < 0)
            {
                v = 1;
            }

            Console.WriteLine("Answer: S {0} E {1} M {2} = {3}", v, Convert.ToString(exp + 127 + 1, 2), Ans + new string('0', 23 - Ans.Length), fir.val * sec.val);
        }
Пример #7
0
        public static void VisualCommandFSub(byte sign, string a1, string a2)
        {
            SoReg fir = SCPU.reg[a1] as SoReg;
            SoReg sec = SCPU.reg[a2] as SoReg;

            string f = fir.man;
            string s = sec.man;

            Console.WriteLine("First  value S {0} E {1} M{2}", fir.sign, fir.or, fir.man);
            Console.WriteLine("Second value S {0} E {1} M{2}", sec.sign, sec.or, sec.man);

            int max = Math.Max(fir.exp, sec.exp);

            if (fir.exp != max)
            {
                f = new string('0', max - fir.exp) + f;
                f = f.Substring(0, 23);
            }
            else
            {
                s = new string('0', max - sec.exp) + s;
                s = s.Substring(0, 23);
            }

            max--;
            if (fir.val < 0 && sec.val < 0)
            {
                max++;
            }

            StringBuilder ans = new StringBuilder(new string('0', 23));

            int  over = 0;
            int  i    = 23;
            int  step = 0;
            bool exit = SCPU.MExit;

            Console.WriteLine("{0, 32}", fir.man);
            Console.WriteLine("{0, 32}", sec.man);
            while (true)
            {
                i--;
                if (i < 0)
                {
                    break;
                }
                if (!exit)
                {
                    if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                    {
                        exit = true;
                    }
                }
                if (f[i] == '1' && s[i] == '1')
                {
                    if (over == 0)
                    {
                        ans[i] = '0';
                        over   = 0;
                    }
                    else
                    {
                        ans[i] = '1';
                        over   = 1;
                    }
                }
                else
                if (f[i] == '0' && s[i] == '0')
                {
                    if (over == 0)
                    {
                        ans[i] = '0';
                        over   = 0;
                    }
                    else
                    {
                        ans[i] = '1';
                        over   = 1;
                    }
                }
                else
                if (f[i] == '1' && s[i] == '0')
                {
                    if (over == 0)
                    {
                        ans[i] = '1';
                        over   = 0;
                    }
                    else
                    {
                        ans[i] = '0';
                        over   = 0;
                    }
                }
                else
                if (f[i] == '0' && s[i] == '1')
                {
                    if (over == 0)
                    {
                        ans[i] = '1';
                        over   = 1;
                    }
                    else
                    {
                        ans[i] = '0';
                        over   = 1;
                    }
                }

                Console.WriteLine("Step {0}: {1}", (++step).ToString("D2"), ans);
            }
            if (over == 1)
            {
                max--;
            }
            string Ans = ans.ToString();
            int    len = Ans.Length;

            Ans  = Ans.TrimStart('0');
            max -= len - Ans.Length;
            //if (max == 0)
            //	max = 1;
            if (Ans.Length > 22)
            {
                Ans = Ans.Substring(0, 23);
            }

            Console.WriteLine("Answer: S {0} E {1} M {2} = {3}", sign, Convert.ToString(max + 127, 2), Ans + new string('0', 23 - Ans.Length), fir.val - sec.val);
        }