public static Opcode_ByteData compile_line(string userline, int bpointer)
        {
            userline.Replace("/r/n", "");
            bool            found_opcode = false;
            int             num          = getopcode(userline);
            Opcode_ByteData byte_data    = new Opcode_ByteData();

            IEnumerable <Opcode> thislist = doublecheckopcode(userline, num);


            if (thislist.Count() > 0)
            {
                found_opcode = true;
            }
            if (found_opcode == true)
            {
                Opcode_Data userdata = opcode_matchtest(thislist, userline);
                string      opstring = make_opcode_string(userdata.byte_index);
                if (userdata.success)
                {
                    byte_data        = get_bytes(userdata, bpointer);
                    byte_data.param1 = userdata.param1;
                    byte_data.param2 = userdata.param2;

                    //Next
                    //For now just print out the byte data, later this will turn into streaming the data to a file
                    int    length        = byte_data.total_instr_length;
                    string opcode_string = "Opcode Instruction was " + byte_data.memnonic + ": Opcode #" + byte_data.bytes[0] + " ";
                    for (int t = 1; t < length + 1; t++)
                    {
                        opcode_string = opcode_string + "|" + byte_data.bytes[t];
                        if (t == length)
                        {
                            opcode_string = opcode_string + "|";
                        }
                    }
                    Console.WriteLine(opcode_string);
                }
            }


            return(byte_data);
        }
Exemplo n.º 2
0
        public static Opcode_ByteData get_bytes(Opcode_Data data)
        {
            int[] bytes       = new int[30]; //We'll use extra just to be safe
            int   firstbyte   = data.byte_index;
            int   currentbyte = 1;

            bytes[0] = firstbyte;
            int param1length     = data.param1.Length;
            int param2length     = data.param2.Length;
            int param1bytelength = 1;
            int param2bytelength = 1;

            //This takes care of only two special cases where the value may not be 4 bytes long
            if (data.memnonic == "ST8")
            {
                param1bytelength = 1;
            }
            if (data.memnonic == "ST16")
            {
                param1bytelength = 2;
            }

            if (data.paramtype1 == Opcode.paramtype.REGISTER || data.paramtype1 == Opcode.paramtype.REGISTERADDRESS)
            {
                //If the first parameter is a register then the first byte is just the register's value
                bytes[currentbyte] = data.reg1value;
                currentbyte        = currentbyte + 1;
            }

            if (data.paramtype1 == Opcode.paramtype.VALUE || data.paramtype1 == Opcode.paramtype.ADDRESS)
            {
                bool writtenvalue = false;
                int  value        = System.Convert.ToInt32(Right(data.param1, param1length - 1));
                //Console.WriteLine("The value was " + secondbyte);
                byte[] results = INT2LE(value);
                //Console.WriteLine("Bytes are " + results[0] + "," + results[1] + "," + results[2] + "," + results[3]);
                int[] r = new int[10];
                get(value, 8, 0, r);
                //Console.WriteLine(" for comparison other bytes are " + r[0] + "," + r[1] + "," + r[2] + "," +r[3]);
                //Pad the value bytes to two
                if (param1bytelength == 2 && data.memnonic == "ST16")
                {
                    value = value & 65535;
                    int highbyte = value & 255; // high byte (0x12)
                    int lowbyte  = value >> 8;  // low byte  (0x34)
                    bytes[currentbyte]     = lowbyte;
                    bytes[currentbyte + 1] = highbyte;
                    currentbyte            = currentbyte + 1;
                    currentbyte            = currentbyte + 1;
                    writtenvalue           = true;
                    //Console.WriteLine("highbyte was " + highbyte + " and lowbyte was " + lowbyte);
                }

                //Pad the value bytes to 1
                if (param1bytelength == 1 && data.memnonic == "ST8")
                {
                    value = value & 255;
                    bytes[currentbyte] = value;
                    currentbyte        = currentbyte + 1;
                    writtenvalue       = true;
                }

                if (writtenvalue == false)
                {
                    if (data.memnonic != "ST16" && data.memnonic != "ST8")
                    {
                        {
                            results            = INT2LE(value);
                            bytes[currentbyte] = results[0];
                            currentbyte++;
                            bytes[currentbyte] = results[1];
                            currentbyte++;
                            bytes[currentbyte] = results[2];
                            currentbyte++;
                            bytes[currentbyte] = results[3];
                            currentbyte++;
                        }
                    }
                }
            }


            if (data.paramtype2 == Opcode.paramtype.REGISTER || data.paramtype2 == Opcode.paramtype.REGISTERADDRESS)
            {
                int value = data.reg2value;
                //Write the current value
                bytes[currentbyte] = value;
            }

            if (data.paramtype2 == Opcode.paramtype.VALUE || data.paramtype2 == Opcode.paramtype.ADDRESS)
            {
                int    value   = System.Convert.ToInt32(Right(data.param2, param2length - 1));
                byte[] results = INT2LE(value);
                // Console.WriteLine("Bytes are " + results[0] + "," + results[1] + "," + results[2] + "," + results[3]);
                int[] r = new int[10];
                get(value, 8, 0, r);
                //Console.WriteLine(" for comparison other bytes are " + r[0] + "," + r[1] + "," + r[2] + "," + r[3]);
                //put the bytes into the opcode array stream
                bytes[currentbyte] = results[0];
                currentbyte++;
                bytes[currentbyte] = results[1];
                currentbyte++;
                bytes[currentbyte] = results[2];
                currentbyte++;
                bytes[currentbyte] = results[3];
            }

            //Console.WriteLine("Just to confirm the bytes written were " + "opcode byte " + bytes[0] + " , " + bytes[1] + "," + bytes[2] + "," + bytes[3] + "," + bytes[4] + "," + bytes[5]+","+bytes[6]);

            Opcode_ByteData opcode_bytes = new Opcode_ByteData();

            opcode_bytes.total_instr_length = currentbyte;
            opcode_bytes.bytes    = bytes;
            opcode_bytes.memnonic = data.memnonic;
            return(opcode_bytes);
        }
Exemplo n.º 3
0
        //This returns a structure that contains everything the user entered, and the opcode information
        //From the internal opcode table
        static Opcode_Data opcode_matchtest(List <Opcode> opcodelist, string inputline)
        {
            //this should again patch the pesky issue
            string      userp1  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            Opcode_Data outdata = new Opcode_Data();

            Opcode.paramtype userparam1;
            Opcode.paramtype userparam2;
            userparam1 = Opcode.paramtype.DEFAULT;
            userparam2 = Opcode.paramtype.DEFAULT;
            string[] userline = toarray(inputline);
            if (!inputline.Contains("NOP"))
            {
                userp1 = userline[1];
            }
            //This little patch allows the if check later on userp2 as an array even if the userp2 string isn't filled
            //by a conversion from userp2 to char array.
            string userp2 = "ABCDEFGHIJKLKMNOPQRSTUVWXYZ";

            if (userline.Count() > 2 && userline[2].Length > 0)
            {
                userp2 = userline[2];
            }
            int    param_amount  = 0;
            bool   match         = false;
            int    matched_index = 0;
            Opcode oc            = new Opcode();

            //check param1 and 2 if registers
            for (int t = 0; t < 12 + 1; t++)
            {
                if (userp1 == validparams.RegisterParams[t])
                {
                    userparam1        = Opcode.paramtype.REGISTER;
                    outdata.reg1value = t;
                }
                if (userp1 == validparams.RegisterAddressParams[t])
                {
                    userparam1        = Opcode.paramtype.REGISTERADDRESS;
                    outdata.reg1value = t;
                }
                if (userp2 == validparams.RegisterParams[t])
                {
                    userparam2        = Opcode.paramtype.REGISTER;
                    outdata.reg2value = t;
                }
                if (userp2 == validparams.RegisterAddressParams[t])
                {
                    userparam2        = Opcode.paramtype.REGISTERADDRESS;
                    outdata.reg2value = t;
                }
            }
            char[] line1 = userp1.ToCharArray();
            char[] line2 = userp2.ToCharArray();
            if (line1[0] == 'V')
            {
                userparam1 = Opcode.paramtype.VALUE;
            }
            if (line2[0] == 'V')
            {
                userparam2 = Opcode.paramtype.VALUE;
            }
            if (line1[0] == '_')
            {
                userparam1 = Opcode.paramtype.ADDRESS;
            }
            if (line2[0] == '_')
            {
                userparam2 = Opcode.paramtype.ADDRESS;
            }

            if (userp2.Length > 0)
            {
                param_amount = 2;
            }

            if (param_amount == 1)
            {
                foreach (Opcode n in opcodelist)
                {
                    if (userparam1 == n.param1)
                    {
                        match = true; matched_index = n.byteindex; oc = n;
                        break;
                    }
                }
            }

            if (param_amount == 2)
            {
                foreach (Opcode n in opcodelist)
                {
                    if (userparam1 == n.param1 && userparam2 == n.param2)
                    {
                        match = true; matched_index = n.byteindex; oc = n;
                        break;
                    }
                }
            }

            if (match == true)
            {
                outdata.byte_index   = oc.byteindex;
                outdata.param1       = userp1;
                outdata.param2       = userp2;
                outdata.param_amount = param_amount;
                outdata.success      = true;
                outdata.paramtype1   = oc.param1;
                outdata.paramtype2   = oc.param2;
                outdata.memnonic     = oc.memnonic;
            }
            if (match == false)
            {
                outdata.success = false;
            }
            return(outdata);
        }
        public static Opcode_ByteData get_bytes(Opcode_Data data, int bpointer)
        {
            int[] bytes     = new int[30]; //We'll use extra just to be safe
            int   firstbyte = data.byte_index;

            //handle replace goto with JMP because it's basically the same thing but with label
            if (firstbyte == 42)
            {
                firstbyte = 33;
            }


            int currentbyte = 1;

            bytes[0] = firstbyte;
            int param1length     = data.param1.Length;
            int param2length     = data.param2.Length;
            int param1bytelength = 1;
            int param2bytelength = 1;
            int value            = 0;



            //This takes care of only two special cases where the value may not be 4 bytes long
            if (data.memnonic == "ST8")
            {
                param1bytelength = 1;
            }
            if (data.memnonic == "ST16")
            {
                param1bytelength = 2;
            }

            if (data.paramtype1 == Opcode.paramtype.REGISTER || data.paramtype1 == Opcode.paramtype.REGISTERADDRESS)
            {
                //If the first parameter is a register then the first byte is just the register's value
                bytes[currentbyte] = data.reg1value;
                currentbyte        = currentbyte + 1;
            }

            if (data.paramtype1 == Opcode.paramtype.VALUE || data.paramtype1 == Opcode.paramtype.ADDRESS)
            {
                bool writtenvalue = false;
                value = System.Convert.ToInt32(Right(data.param1, param1length - 1));
                //Console.WriteLine("The value was " + secondbyte);
                byte[] results = INT2LE(value);
                //Console.WriteLine("Bytes are " + results[0] + "," + results[1] + "," + results[2] + "," + results[3]);
                int[] r = new int[10];
                get(value, 8, 0, r);
                //Console.WriteLine(" for comparison other bytes are " + r[0] + "," + r[1] + "," + r[2] + "," +r[3]);
                //Pad the value bytes to two
                if (param1bytelength == 2 && data.memnonic == "ST16")
                {
                    value = value & 65535;
                    int highbyte = value & 255; // high byte (0x12)
                    int lowbyte  = value >> 8;  // low byte  (0x34)
                    bytes[currentbyte]     = lowbyte;
                    bytes[currentbyte + 1] = highbyte;
                    currentbyte            = currentbyte + 1;
                    currentbyte            = currentbyte + 1;
                    writtenvalue           = true;
                    //Console.WriteLine("highbyte was " + highbyte + " and lowbyte was " + lowbyte);
                }

                //Pad the value bytes to 1
                if (param1bytelength == 1 && data.memnonic == "ST8")
                {
                    value = value & 255;
                    bytes[currentbyte] = value;
                    currentbyte        = currentbyte + 1;
                    writtenvalue       = true;
                }

                if (writtenvalue == false)
                {
                    if (data.memnonic != "ST16" && data.memnonic != "ST8")
                    {
                        {
                            results            = INT2LE(value);
                            bytes[currentbyte] = results[0];
                            currentbyte++;
                            bytes[currentbyte] = results[1];
                            currentbyte++;
                            bytes[currentbyte] = results[2];
                            currentbyte++;
                            bytes[currentbyte] = results[3];
                            currentbyte++;
                        }
                    }
                }
            }


            if (data.paramtype2 == Opcode.paramtype.REGISTER || data.paramtype2 == Opcode.paramtype.REGISTERADDRESS)
            {
                value = data.reg2value;
                //Write the current value
                bytes[currentbyte] = value;
            }



            if (data.paramtype2 == Opcode.paramtype.STRING)
            {
                if (data.paramtype1 != Opcode.paramtype.GOTO)
                {
                    value = 0;
                    //if (data.paramtype2 == Opcode.paramtype.VALUE || data.paramtype2 == Opcode.paramtype.ADDRESS) { value = System.Convert.ToInt32(Right(data.param2, param2length - 1)); }
                    //patch in checking for byte pointers, only if not label type.

                    if (firstrun)
                    {
                        value = bpointer;
                        if (data.paramtype1 == Opcode.paramtype.LABEL)
                        {
                            labelbytepointers.Add(data.param2, value);
                        }
                    }
                }

                if (data.paramtype1 == Opcode.paramtype.GOTO)
                {
                    value = 0; //we don't know what it could be until recompile :)

                    {
                        //A bit redundant since we declare 'value' 0 later for goto but. whatever.
                        //in theory this should compile ALL the needed bytes and still get the correct pointer
                        if (!firstrun)
                        {
                            if (data.param1 == "goto" && (data.param1 != "label"))
                            {
                                value = labelbytepointers[data.param2];
                            }
                            Console.WriteLine("Value injection found for goto statement " + data.param2 + " and the injection value was " + value);
                        }
                        byte[] results = INT2LE(value);
                        bytes[currentbyte] = results[0];
                        currentbyte++;
                        bytes[currentbyte] = results[1];
                        currentbyte++;
                        bytes[currentbyte] = results[2];
                        currentbyte++;
                        bytes[currentbyte] = results[3];
                        currentbyte++;
                    }
                }
            }

            //Console.WriteLine("Just to confirm the bytes written were " + "opcode byte " + bytes[0] + " , " + bytes[1] + "," + bytes[2] + "," + bytes[3] + "," + bytes[4] + "," + bytes[5]+","+bytes[6]);

            Opcode_ByteData opcode_bytes = new Opcode_ByteData();

            opcode_bytes.total_instr_length = currentbyte;
            opcode_bytes.bytes    = bytes;
            opcode_bytes.memnonic = data.memnonic;
            return(opcode_bytes);
        }