Skip to content

Instantly share code, notes, and snippets.

@iGlitch
Created May 29, 2019 01:31
Show Gist options
  • Save iGlitch/ec468b740d018b699aef2e4c02c2b63e to your computer and use it in GitHub Desktop.
Save iGlitch/ec468b740d018b699aef2e4c02c2b63e to your computer and use it in GitHub Desktop.
Code Build Tool
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
namespace CodeBuildTool
{
class Program
{
static bool RunExe(string exe, string arg)
{
//assembler
var pi = new ProcessStartInfo(exe, arg);
pi.CreateNoWindow = true;
pi.RedirectStandardOutput = true;
pi.RedirectStandardError = true;
pi.UseShellExecute = false;
var p = Process.Start(pi);
if (!p.StandardError.EndOfStream)
{
Console.WriteLine("Some errors occurred while compiling the source.");
var str = "";
Console.WriteLine(str = p.StandardError.ReadToEnd());
if (str.Contains("error")) return false;
}
return true;
}
static void ConcatText(string dir)
{
if(!Directory.Exists(dir))
{
Console.WriteLine("No such directory : {0}", dir);
return;
}
var text = new StringBuilder();
foreach(var f in Directory.GetFiles(dir))
{
if (Path.GetFileName(f) != "Result.txt" && Path.GetExtension(f) == ".txt")
{
text.AppendLine(File.ReadAllText(f));
text.AppendLine();
}
}
File.WriteAllText("Output\\Result.txt", text.ToString());
Console.WriteLine("Concatinating files done");
}
static void Main(string[] args)
{
var removefiles = new Action(() =>
{
//remove temporary files
foreach (var file in new[] { "tmp.o", "tmp2.o", "tmp.bin" })
if (File.Exists(file))
File.Delete(file);
});
removefiles();
if (args.Length == 1)
{
ConcatText(args[0]);
return;
}
else if (args.Length < 3)
{
Console.WriteLine("Usage : CodeBuildTool [.c file] [code type(06,C2)] [address] (optional)[emit assemble list]");
return;
}
if (!Directory.Exists("Output"))
Directory.CreateDirectory("Output");
var source = args[0];
var c2 = args[1].ToLower() == "c2";
var address = Convert.ToUInt32(args[2], 16);
var asm = args.Length == 4 && args[3] == "1";
//check build tools
const string gcc = "C://devkitPro\\devkitPPC\\bin\\powerpc-eabi-gcc.exe";
//const string objdump = "C://devkitPro\\devkitPPC\\bin\\powerpc-eabi-objdump.exe";
const string strip = "C://devkitPro\\devkitPPC\\bin\\powerpc-eabi-strip.exe";
const string objcopy = "C://devkitPro\\devkitPPC\\bin\\powerpc-eabi-objcopy.exe";
foreach (var file in new[] { gcc,/*objdump,*/strip, objcopy })
if (!File.Exists(file))
{
Console.WriteLine("{0} doesn't exist.", file);
return;
}
//assembler
if (!RunExe(gcc, String.Format("-c -std=c99 -O3 -S -mregnames -o \"Output\\{0}.s\" \"{1}\"", Path.GetFileNameWithoutExtension(source), source)))
return;
//compile
if (!RunExe(gcc, String.Format("-c -std=c99 -O3 -mregnames -o tmp.o \"{0}\"", source)))
return;
//strip
if (!RunExe(strip, "--strip-debug --strip-all --discard-all -o tmp.o -F elf32-powerpc tmp.o"))
return;
//objcopy
if (!RunExe(objcopy, "-I elf32-powerpc -O binary tmp.o tmp.bin"))
return;
//create code file
var codearray = File.ReadAllBytes("tmp.bin");
var sb = new StringBuilder(Path.GetFileNameWithoutExtension(source) + Environment.NewLine);
var reverse = new Func<uint, uint>(val => ((val & 0xFF) << 24 | ((val & 0xFF00) << 8) | (val & 0xFF0000) >> 8) | (val >> 24));
if (c2)
{
var padding = 8 - codearray.Length % 8;
var lines = (codearray.Length + padding) / 8;
sb.AppendFormat("* {0:X8} {1:X8}", 0xC2000000 | address, lines);
sb.AppendLine();
for (int i = 0, j = 0; j < codearray.Length / 8; i += 8, j += 1)
{
sb.AppendFormat("* {0:X8} {1:X8}", reverse(BitConverter.ToUInt32(codearray, i)), reverse(BitConverter.ToUInt32(codearray, i + 4)));
sb.AppendLine();
}
for (int i = (codearray.Length / 8) * 8, j = 0; j < 8; i++, j++)
{
if (j == 4)
sb.Append(" ");
else if (j == 0)
sb.Append("* ");
if (i < codearray.Length)
sb.AppendFormat("{0:X2}", codearray[i]);
else if (padding == 8 && j == 0)
sb.Append("60");
else
sb.Append("00");
}
}
else//06
{
var padding = (8 - codearray.Length % 8) % 8;
sb.AppendFormat("* {0:X8} {1:X8}", 0x06000000 | address, codearray.Length);
sb.AppendLine();
for (int i = 0, j = 0; j < codearray.Length / 8; i += 8, j++)
{
sb.AppendFormat("* {0:X8} {1:X8}", reverse(BitConverter.ToUInt32(codearray, i)), reverse(BitConverter.ToUInt32(codearray, i + 4)));
sb.AppendLine();
}
for (int i = (codearray.Length / 8) * 8, j = 0; j < 8; i++, j++)
{
if (j == 4)
sb.Append(" ");
else if (j == 0)
sb.Append("* ");
if (i < codearray.Length)
sb.AppendFormat("{0:X2}", codearray[i]);
else
sb.Append("00");
}
}
File.WriteAllText("Output\\" + Path.GetFileNameWithoutExtension(source) + ".txt", sb.ToString());
removefiles();
Console.WriteLine("Successfully finished {0}.", source);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment