Beispiel #1
0
        public static void TestOpcodes()
        {
            List <OpcodeCompiler.Op> ops = OpcodeCompiler.GenerateOpcodes(OpcodesPath);

            System.IO.File.WriteAllText(OpcodesCsv, OpcodeCompiler.MakeCSV(ops));
            Debug.LogFormat("Wrote {0} opcodes to {1}", ops.Count, OpcodesCsv);
        }
Beispiel #2
0
        public static void Decompile400()
        {
            List <OpcodeCompiler.Op> ops = OpcodeCompiler.GenerateOpcodes(OpcodesPath);

            string path = EditorUtility.OpenFilePanel("Open ROM file", @"C:\dev\dx8\ROMS", "bin");

            byte[] data = System.IO.File.ReadAllBytes(path);

            System.IO.File.WriteAllText(path + ".s", OpcodeCompiler.Decompile(ops, data, 0x400));
        }
Beispiel #3
0
        public static void MakeMacros()
        {
            List <OpcodeCompiler.Op> ops       = OpcodeCompiler.GenerateOpcodes(OpcodesPath);
            Dictionary <string, int> registers = OpcodeCompiler.Generate_Registers(Config);
            Dictionary <string, int> constants = OpcodeCompiler.Generate_Constants(Config);
            Dictionary <string, KeyValuePair <int, string> > scancodes  = OpcodeCompiler.Generate_Scancodes(Config);
            Dictionary <string, KeyValuePair <int, string> > interrupts = OpcodeCompiler.Generate_Interrupts(Config);

            System.IO.File.WriteAllText(OpcodesInc, OpcodeCompiler.MakeMacros(ops, registers, constants, interrupts, scancodes));
            Debug.LogFormat("Wrote {0} macros to {1}", ops.Count, OpcodesInc);
        }
Beispiel #4
0
        public static void ScanCodesToCStringArray()
        {
            Dictionary <string, KeyValuePair <int, string> > scancodes = OpcodeCompiler.Generate_Scancodes(Config);
            StringBuilder sb = new StringBuilder();

            foreach (var sc in scancodes)
            {
                sb.AppendFormat("\"{0}\",", ToLiteral(sc.Value.Value));
                sb.AppendLine();
            }
            Debug.Log(sb.ToString());
        }
Beispiel #5
0
        public static void MakeRegisters()
        {
            Dictionary <string, int> registers = OpcodeCompiler.Generate_Registers(Config);
            Dictionary <string, int> constants = OpcodeCompiler.Generate_Constants(Config);
            Dictionary <string, KeyValuePair <int, string> > scancodes  = OpcodeCompiler.Generate_Scancodes(Config);
            Dictionary <string, KeyValuePair <int, string> > interrupts = OpcodeCompiler.Generate_Interrupts(Config);

            System.IO.File.WriteAllText(Registers, OpcodeCompiler.MakeRegisters(registers));
            System.IO.File.WriteAllText(Constants, OpcodeCompiler.MakeConstants(constants));
            System.IO.File.WriteAllText(Interrupts, OpcodeCompiler.MakeInterrupts(interrupts));
            System.IO.File.WriteAllText(Scancodes, OpcodeCompiler.MakeScancodes(scancodes));

            Debug.Log("Wrote dx__??.inc");
        }
Beispiel #6
0
        public static void MakeSDK()
        {
            List <OpcodeCompiler.Op> ops       = OpcodeCompiler.GenerateOpcodes(OpcodesPath);
            Dictionary <string, int> registers = OpcodeCompiler.Generate_Registers(Config);
            Dictionary <string, int> constants = OpcodeCompiler.Generate_Constants(Config);
            Dictionary <string, KeyValuePair <int, string> > scancodes  = OpcodeCompiler.Generate_Scancodes(Config);
            Dictionary <string, KeyValuePair <int, string> > interrupts = OpcodeCompiler.Generate_Interrupts(Config);

            System.IO.File.WriteAllText(SdkCpuPath, OpcodeCompiler.MakeMacros(ops, null, null, null, null));
            Debug.LogFormat("Wrote {0} instructions to {1}", ops.Count, SdkCpuPath);

            System.IO.File.WriteAllText(SdkKeyboardPath, OpcodeCompiler.MakeMacros(null, null, null, null, scancodes));
            Debug.LogFormat("Wrote {0} keyboards to {1}", scancodes.Count, SdkKeyboardPath);

            System.IO.File.WriteAllText(SdkRegistersPath, OpcodeCompiler.MakeMacros(null, registers, constants, null, null));
            Debug.LogFormat("Wrote {0} registers/constants to {1}", registers.Count + constants.Count, SdkRegistersPath);
        }
Beispiel #7
0
        public static void ScanCodesToAssemblyDbArray()
        {
            Dictionary <string, KeyValuePair <int, string> > scancodes = OpcodeCompiler.Generate_Scancodes(Config);
            StringBuilder sb = new StringBuilder();

            foreach (var sc in scancodes)
            {
                char m = sc.Value.Value[0];
                int  n = 0x7F;

                if (m != '^')
                {
                    n = (int)m;
                }

                sb.AppendFormat("db ${0:X2} ; ${1:X2} {2}", n, sc.Value.Key, sc.Key);
                sb.AppendLine();
            }
            Debug.Log(sb.ToString());
        }
Beispiel #8
0
        public static void GenerateSwitchStatement()
        {
            List <OpcodeCompiler.Op> ops = OpcodeCompiler.GenerateOpcodes(OpcodesPath);

            System.Text.StringBuilder opcodeSwitch = new System.Text.StringBuilder(16384);

            opcodeSwitch.AppendLine("#include \"dx8.h\"");
            opcodeSwitch.AppendLine("#include \"dx8_Cpu_Common.inc\"");
            opcodeSwitch.AppendLine("#include \"dx8_Cpu_Tables.inc\"");
            opcodeSwitch.AppendLine();

            string[] includes = System.IO.Directory.GetFiles(OpcodesFunctions, "dx8_Cpu_Op_*.inc");

            HashSet <string> functions = new HashSet <string>();

            //
            foreach (var inc in includes)
            {
                var lines = System.IO.File.ReadAllLines(inc);
                foreach (var line in lines)
                {
                    Match match = Regex.Match(line, @"inline\s+(\w+)\s+(.*)", RegexOptions.IgnoreCase);

                    if (!match.Success)
                    {
                        continue;
                    }

                    string fn = string.Format("{0} {1}", match.Groups[1], match.Groups[2].Value.Trim(new [] { ';' })).Trim();

                    functions.Add(fn);
                }
            }

            foreach (var fn in functions)
            {
                opcodeSwitch.AppendFormat("inline {0};", fn);
                opcodeSwitch.AppendLine();
            }

            opcodeSwitch.AppendLine();

            foreach (var inc in System.IO.Directory.GetFiles(OpcodesFunctions, "dx8_Cpu_Op_*.inc"))
            {
                opcodeSwitch.AppendFormat("#include \"{0}\"", System.IO.Path.GetFileName(inc));
                opcodeSwitch.AppendLine();
            }

            opcodeSwitch.AppendLine();

            opcodeSwitch.AppendLine("void Cpu_StepOnce()");
            opcodeSwitch.AppendLine("{");
            opcodeSwitch.AppendLine("  Opcode opcode = Mmu_Get(cpu.pc.w);");

            opcodeSwitch.AppendLine("#if defined(DX8_DEBUG_INSTRUCTIONS) && DX8_DEBUG_INSTRUCTIONS == 1");
            opcodeSwitch.AppendFormat("      sDebugInstruction.opcode = opcode;");
            opcodeSwitch.AppendLine();
            opcodeSwitch.AppendFormat("      sDebugInstruction.pc = cpu.pc.w;");
            opcodeSwitch.AppendLine();
            opcodeSwitch.AppendFormat("      sDebugInstruction.context1Type = 0;");
            opcodeSwitch.AppendLine();
            opcodeSwitch.AppendFormat("      sDebugInstruction.context2Type = 0;");
            opcodeSwitch.AppendLine();
            opcodeSwitch.AppendLine("      sDebugInstruction.operand.w = 0;");
            opcodeSwitch.AppendLine("#endif");

            opcodeSwitch.AppendLine("  switch (opcode)");
            opcodeSwitch.AppendLine("  {");

            foreach (var op in ops)
            {
                opcodeSwitch.AppendFormat("    // ");
                op.ToAssemblerFormat(ref opcodeSwitch);
                opcodeSwitch.AppendLine();
                opcodeSwitch.AppendFormat("    case ");

                op.ToCEnumName(ref opcodeSwitch);
                opcodeSwitch.AppendFormat(" : // 0x{0:X2}", op.Index);
                opcodeSwitch.AppendLine();
                opcodeSwitch.AppendLine("    {");

                // m = m.Replace(

                if (op.Opcode == OpcodeCompiler.Opcode.Nop)
                {
                    opcodeSwitch.AppendLine("#if defined(DX8_DEBUG_INSTRUCTIONS) && DX8_DEBUG_INSTRUCTIONS == 1");
                    opcodeSwitch.AppendFormat("      sDebugInstruction.length = {0};", op.Length);
                    opcodeSwitch.AppendLine();
                    opcodeSwitch.AppendLine("#endif");

                    if (op.Index != 0)
                    {
                        opcodeSwitch.AppendLine("      Cpu_Halt(HALT_UNKNOWN_OPCODE);");
                    }
                }
                else
                {
                    opcodeSwitch.AppendLine("#if defined(DX8_DEBUG_INSTRUCTIONS) && DX8_DEBUG_INSTRUCTIONS == 1");
                    opcodeSwitch.AppendFormat("      sDebugInstruction.length = {0};", op.Length);
                    opcodeSwitch.AppendLine();

                    if (op.Length == 2)
                    {
                        opcodeSwitch.AppendLine("#endif");
                        opcodeSwitch.AppendLine("      Byte imm  = Mmu_Get(cpu.pc.w + 1);");
                        opcodeSwitch.AppendLine("#if defined(DX8_DEBUG_INSTRUCTIONS) && DX8_DEBUG_INSTRUCTIONS == 1");
                        opcodeSwitch.AppendLine("      sDebugInstruction.operand.lo = imm;");
                        opcodeSwitch.AppendLine("#endif");
                    }
                    else if (op.Length == 3)
                    {
                        opcodeSwitch.AppendLine("#endif");
                        opcodeSwitch.AppendLine("      Word imm = Mmu_GetWord(cpu.pc.w + 1);");
                        opcodeSwitch.AppendLine("#if defined(DX8_DEBUG_INSTRUCTIONS) && DX8_DEBUG_INSTRUCTIONS == 1");
                        opcodeSwitch.AppendLine("      sDebugInstruction.operand.w = imm;");
                        opcodeSwitch.AppendLine("#endif");
                    }
                    else
                    {
                        opcodeSwitch.AppendLine("#endif");
                    }

                    string m = op.Code.TrimStart(new char[] { '{' });
                    m = m.TrimEnd(new char[] { '}', ')' });
                    m = m.Trim();

                    if (m.EndsWith(";") == false)
                    {
                        m = m + ";";
                    }

                    m = m.Replace("REG_IMM", "imm");
                    m = m.Replace("REG_BYTE", "imm");
                    m = m.Replace("REG_WORD", "imm");
                    m = m.Replace("REG_PC", "cpu.pc.w");
                    m = m.Replace("REG_A", "cpu.a");
                    m = m.Replace("REG_X", "cpu.I.x");
                    m = m.Replace("REG_Y", "cpu.I.y");
                    m = m.Replace("REG_I", "cpu.I.I");
                    m = m.Replace("REG_Z", "cpu.J.z");
                    m = m.Replace("REG_W", "cpu.J.w");
                    m = m.Replace("REG_J", "cpu.J.J");
                    m = m.Replace("FL_Z", "cpu.flags.bZero");
                    m = m.Replace("FL_G", "cpu.flags.bGreater");
                    m = m.Replace("FL_N", "cpu.flags.bNegative");
                    m = m.Replace("FL_C", "cpu.flags.bCarry");
                    m = m.Replace("Opf_Single", "1");
                    m = m.Replace("Opf_Byte", "2");
                    m = m.Replace("Opf_Word", "3");
                    m = m.Replace("Opf_Address", "3");

                    opcodeSwitch.Append("      ");
                    opcodeSwitch.AppendLine(m);
                }

                if (op.PcMod == OpcodeCompiler.PcMod.Length)
                {
                    opcodeSwitch.AppendFormat("      cpu.pc.w += {0};", op.Length);
                    opcodeSwitch.AppendLine();
                }

                opcodeSwitch.AppendLine("#if defined(DX8_DEBUG_INSTRUCTIONS) && DX8_DEBUG_INSTRUCTIONS == 1");
                opcodeSwitch.AppendLine("      Cpu_Debug_PushDebugInstruction();");
                opcodeSwitch.AppendLine("#endif");

                opcodeSwitch.AppendLine("    }");
                opcodeSwitch.AppendLine("    break;");
            }

            opcodeSwitch.AppendLine("  }");
            opcodeSwitch.AppendLine("}");

            System.IO.File.WriteAllText(StepPath, opcodeSwitch.ToString());

            Debug.Log("Wrote switch file to " + StepPath);

            StringBuilder tables = new StringBuilder();

            tables.AppendLine("const char* kOpStrs[] = {");

            foreach (var op in ops)
            {
                tables.AppendFormat("\"");
                op.ToAssemblerFormatName(ref tables);
                tables.AppendFormat("\",");
                tables.AppendLine();
            }

            tables.AppendLine("};");

            tables.AppendLine();

            tables.AppendLine("int Cpu_FormatInstruction(char* str, Byte opcode, Word imm)");
            tables.AppendLine("{");

            tables.AppendLine("    switch(opcode)");
            tables.AppendLine("    {");

            foreach (var op in ops)
            {
                tables.AppendFormat("      case {0}: ", op.Index);
                if (op.Length == 1)
                {
                    tables.AppendFormat("return sprintf(str, \"");
                    op.ToSprintfFormat(ref tables);
                    tables.AppendLine("\");");
                }
                else
                {
                    tables.AppendFormat("return sprintf(str, \"");
                    op.ToSprintfFormat(ref tables);
                    tables.AppendLine("\", imm);");
                }
            }

            tables.AppendLine("  }");
            tables.AppendLine("  return 0;");
            tables.AppendLine("}");

            tables.AppendLine();

            tables.AppendLine("typedef enum");
            tables.AppendLine("{");

            foreach (var op in ops)
            {
                tables.Append("  ");
                op.ToCEnumName(ref tables);
                tables.AppendFormat(" = 0x{0:X2},", op.Index);
                tables.AppendLine();
            }
            tables.AppendLine("  OP_COUNT");
            tables.AppendLine("} Opcode;");

            System.IO.File.WriteAllText(TablesPath, tables.ToString());
        }
Beispiel #9
0
        public static void GenerateApi()
        {
            Dictionary <string, KeyValuePair <int, string> > scancodes = OpcodeCompiler.Generate_Scancodes(Config);

            System.IO.File.WriteAllText(ApiCsPath, OpcodeCompiler.GenerateCsApi(ApiPath, scancodes));
        }