Clean up sunSpcBinary

This commit is contained in:
arookas 2017-04-15 20:52:05 -04:00
parent c556939972
commit 74fc59f232

View file

@ -87,301 +87,319 @@ namespace arookas {
sealed class sunSpcBinary : sunBinary { sealed class sunSpcBinary : sunBinary {
aBinaryWriter mWriter; aBinaryWriter mWriter;
sunSpcBinarySection mText, mData, mDataString, mSymbol, mSymbolString; sunSpcStringTable mStringTable;
uint mTextOffset, mDataOffset, mSymbolOffset;
int mDataCount, mSymbolCount, mVarCount; int mDataCount, mSymbolCount, mVarCount;
public override uint Offset { public override uint Offset {
get { return mText.Offset; } get { return (uint)mWriter.Position; }
} }
public sunSpcBinary(Stream output) { public sunSpcBinary(Stream output) {
mWriter = new aBinaryWriter(output, Endianness.Big, Encoding.GetEncoding(932)); mWriter = new aBinaryWriter(output, Endianness.Big, Encoding.GetEncoding(932));
mText = new sunSpcBinarySection(); mStringTable = new sunSpcStringTable(Encoding.GetEncoding(932));
mData = new sunSpcBinarySection();
mDataString = new sunSpcBinarySection();
mSymbol = new sunSpcBinarySection();
mSymbolString = new sunSpcBinarySection();
mWriter.PushAnchor();
} }
public override void Open() {
mWriter.PushAnchor();
WriteHeader();
}
public override void Close() { public override void Close() {
// header WriteHeader();
mWriter.WriteString("SPCB"); mWriter.PopAnchor();
mWriter.Write32(0x1C); }
mWriter.Write32(0x1C + mText.Size);
void WriteHeader() {
mWriter.Goto(0);
mWriter.Write8(0x53); // 'S'
mWriter.Write8(0x50); // 'P'
mWriter.Write8(0x43); // 'C'
mWriter.Write8(0x42); // 'B'
mWriter.Write32(mTextOffset);
mWriter.Write32(mDataOffset);
mWriter.WriteS32(mDataCount); mWriter.WriteS32(mDataCount);
mWriter.Write32(0x1C + mText.Size + mData.Size + mDataString.Size); mWriter.Write32(mSymbolOffset);
mWriter.WriteS32(mSymbolCount); mWriter.WriteS32(mSymbolCount);
mWriter.WriteS32(mVarCount); mWriter.WriteS32(mVarCount);
// sections
mText.Copy(mWriter);
mData.Copy(mWriter);
mDataString.Copy(mWriter);
mSymbol.Copy(mWriter);
mSymbolString.Copy(mWriter);
} }
// text // text
public override void Keep() { public override void Keep() {
mText.Writer.Keep(); mWriter.Keep();
} }
public override void Back() { public override void Back() {
mText.Writer.Back(); mWriter.Back();
} }
public override void Goto(uint offset) { public override void Goto(uint offset) {
mText.Writer.Goto(offset); mWriter.Goto(offset);
} }
public override void BeginText() {
mTextOffset = Offset;
mWriter.PushAnchor();
}
public override void WriteINT(int value) { public override void WriteINT(int value) {
switch (value) { // shortcut commands switch (value) { // shortcut commands
case 0: WriteINT0(); return; case 0: WriteINT0(); return;
case 1: WriteINT1(); return; case 1: WriteINT1(); return;
} }
TraceInstruction("int {0} # ${0:X}", value); TraceInstruction("int {0} # ${0:X}", value);
mText.Writer.Write8(0x00); mWriter.Write8(0x00);
mText.Writer.WriteS32(value); mWriter.WriteS32(value);
} }
public override void WriteFLT(float value) { public override void WriteFLT(float value) {
TraceInstruction("flt {0}", value); TraceInstruction("flt {0}", value);
mText.Writer.Write8(0x01); mWriter.Write8(0x01);
mText.Writer.WriteF32(value); mWriter.WriteF32(value);
} }
public override void WriteSTR(int index) { public override void WriteSTR(int index) {
TraceInstruction("str {0}", index); TraceInstruction("str {0}", index);
mText.Writer.Write8(0x02); mWriter.Write8(0x02);
mText.Writer.WriteS32(index); mWriter.WriteS32(index);
} }
public override void WriteADR(uint value) { public override void WriteADR(uint value) {
TraceInstruction("adr ${0:X8}", value); TraceInstruction("adr ${0:X8}", value);
mText.Writer.Write8(0x03); mWriter.Write8(0x03);
mText.Writer.Write32(value); mWriter.Write32(value);
} }
public override void WriteVAR(int display, int index) { public override void WriteVAR(int display, int index) {
TraceInstruction("var {0} {1}", display, index); TraceInstruction("var {0} {1}", display, index);
mText.Writer.Write8(0x04); mWriter.Write8(0x04);
mText.Writer.WriteS32(display); mWriter.WriteS32(display);
mText.Writer.WriteS32(index); mWriter.WriteS32(index);
} }
public override void WriteNOP() { public override void WriteNOP() {
TraceInstruction("nop"); TraceInstruction("nop");
mText.Writer.Write8(0x05); mWriter.Write8(0x05);
} }
public override void WriteINC(int display, int index) { public override void WriteINC(int display, int index) {
TraceInstruction("inc {0} {1}", display, index); TraceInstruction("inc {0} {1}", display, index);
mText.Writer.Write8(0x06); mWriter.Write8(0x06);
mText.Writer.WriteS32(display); mWriter.WriteS32(display);
mText.Writer.WriteS32(index); mWriter.WriteS32(index);
} }
public override void WriteDEC(int display, int index) { public override void WriteDEC(int display, int index) {
TraceInstruction("dec {0} {1}", display, index); TraceInstruction("dec {0} {1}", display, index);
mText.Writer.Write8(0x07); mWriter.Write8(0x07);
mText.Writer.WriteS32(display); mWriter.WriteS32(display);
mText.Writer.WriteS32(index); mWriter.WriteS32(index);
} }
public override void WriteADD() { public override void WriteADD() {
TraceInstruction("add"); TraceInstruction("add");
mText.Writer.Write8(0x08); mWriter.Write8(0x08);
} }
public override void WriteSUB() { public override void WriteSUB() {
TraceInstruction("sub"); TraceInstruction("sub");
mText.Writer.Write8(0x09); mWriter.Write8(0x09);
} }
public override void WriteMUL() { public override void WriteMUL() {
TraceInstruction("mul"); TraceInstruction("mul");
mText.Writer.Write8(0x0A); mWriter.Write8(0x0A);
} }
public override void WriteDIV() { public override void WriteDIV() {
TraceInstruction("div"); TraceInstruction("div");
mText.Writer.Write8(0x0B); mWriter.Write8(0x0B);
} }
public override void WriteMOD() { public override void WriteMOD() {
TraceInstruction("mod"); TraceInstruction("mod");
mText.Writer.Write8(0x0C); mWriter.Write8(0x0C);
} }
public override void WriteASS(int display, int index) { public override void WriteASS(int display, int index) {
TraceInstruction("ass {0} {1}", display, index); TraceInstruction("ass {0} {1}", display, index);
mText.Writer.Write8(0x0D); mWriter.Write8(0x0D);
mText.Writer.Write8(0x04); // unused (skipped over by TSpcInterp) mWriter.Write8(0x04); // unused (skipped over by TSpcInterp)
mText.Writer.WriteS32(display); mWriter.WriteS32(display);
mText.Writer.WriteS32(index); mWriter.WriteS32(index);
} }
public override void WriteEQ() { public override void WriteEQ() {
TraceInstruction("eq"); TraceInstruction("eq");
mText.Writer.Write8(0x0E); mWriter.Write8(0x0E);
} }
public override void WriteNE() { public override void WriteNE() {
TraceInstruction("ne"); TraceInstruction("ne");
mText.Writer.Write8(0x0F); mWriter.Write8(0x0F);
} }
public override void WriteGT() { public override void WriteGT() {
TraceInstruction("gt"); TraceInstruction("gt");
mText.Writer.Write8(0x10); mWriter.Write8(0x10);
} }
public override void WriteLT() { public override void WriteLT() {
TraceInstruction("lt"); TraceInstruction("lt");
mText.Writer.Write8(0x11); mWriter.Write8(0x11);
} }
public override void WriteGE() { public override void WriteGE() {
TraceInstruction("ge"); TraceInstruction("ge");
mText.Writer.Write8(0x12); mWriter.Write8(0x12);
} }
public override void WriteLE() { public override void WriteLE() {
TraceInstruction("le"); TraceInstruction("le");
mText.Writer.Write8(0x13); mWriter.Write8(0x13);
} }
public override void WriteNEG() { public override void WriteNEG() {
TraceInstruction("neg"); TraceInstruction("neg");
mText.Writer.Write8(0x14); mWriter.Write8(0x14);
} }
public override void WriteNOT() { public override void WriteNOT() {
TraceInstruction("not"); TraceInstruction("not");
mText.Writer.Write8(0x15); mWriter.Write8(0x15);
} }
public override void WriteAND() { public override void WriteAND() {
TraceInstruction("and"); TraceInstruction("and");
mText.Writer.Write8(0x16); mWriter.Write8(0x16);
} }
public override void WriteOR() { public override void WriteOR() {
TraceInstruction("or"); TraceInstruction("or");
mText.Writer.Write8(0x17); mWriter.Write8(0x17);
} }
public override void WriteBAND() { public override void WriteBAND() {
TraceInstruction("band"); TraceInstruction("band");
mText.Writer.Write8(0x18); mWriter.Write8(0x18);
} }
public override void WriteBOR() { public override void WriteBOR() {
TraceInstruction("bor"); TraceInstruction("bor");
mText.Writer.Write8(0x19); mWriter.Write8(0x19);
} }
public override void WriteSHL() { public override void WriteSHL() {
TraceInstruction("shl"); TraceInstruction("shl");
mText.Writer.Write8(0x1A); mWriter.Write8(0x1A);
} }
public override void WriteSHR() { public override void WriteSHR() {
TraceInstruction("shr"); TraceInstruction("shr");
mText.Writer.Write8(0x1B); mWriter.Write8(0x1B);
} }
public override void WriteCALL(uint offset, int count) { public override void WriteCALL(uint offset, int count) {
TraceInstruction("call ${0:X8} {1}", offset, count); TraceInstruction("call ${0:X8} {1}", offset, count);
mText.Writer.Write8(0x1C); mWriter.Write8(0x1C);
mText.Writer.Write32(offset); mWriter.Write32(offset);
mText.Writer.WriteS32(count); mWriter.WriteS32(count);
} }
public override void WriteFUNC(int index, int count) { public override void WriteFUNC(int index, int count) {
TraceInstruction("func {0} {1}", index, count); TraceInstruction("func {0} {1}", index, count);
mText.Writer.Write8(0x1D); mWriter.Write8(0x1D);
mText.Writer.WriteS32(index); mWriter.WriteS32(index);
mText.Writer.WriteS32(count); mWriter.WriteS32(count);
} }
public override void WriteMKFR(int count) { public override void WriteMKFR(int count) {
#if DEBUG
TraceInstruction("mkfr {0}", count); TraceInstruction("mkfr {0}", count);
#endif mWriter.Write8(0x1E);
mText.Writer.Write8(0x1E); mWriter.WriteS32(count);
mText.Writer.WriteS32(count);
} }
public override void WriteMKDS(int display) { public override void WriteMKDS(int display) {
TraceInstruction("mkds {0}", display); TraceInstruction("mkds {0}", display);
mText.Writer.Write8(0x1F); mWriter.Write8(0x1F);
mText.Writer.WriteS32(display); mWriter.WriteS32(display);
} }
public override void WriteRET() { public override void WriteRET() {
TraceInstruction("ret"); TraceInstruction("ret");
mText.Writer.Write8(0x20); mWriter.Write8(0x20);
} }
public override void WriteRET0() { public override void WriteRET0() {
TraceInstruction("ret0"); TraceInstruction("ret0");
mText.Writer.Write8(0x21); mWriter.Write8(0x21);
} }
public override void WriteJNE(uint offset) { public override void WriteJNE(uint offset) {
TraceInstruction("jne ${0:X8}", offset); TraceInstruction("jne ${0:X8}", offset);
mText.Writer.Write8(0x22); mWriter.Write8(0x22);
mText.Writer.Write32(offset); mWriter.Write32(offset);
} }
public override void WriteJMP(uint offset) { public override void WriteJMP(uint offset) {
TraceInstruction("jmp ${0:X8}", offset); TraceInstruction("jmp ${0:X8}", offset);
mText.Writer.Write8(0x23); mWriter.Write8(0x23);
mText.Writer.Write32(offset); mWriter.Write32(offset);
} }
public override void WritePOP() { public override void WritePOP() {
TraceInstruction("pop"); TraceInstruction("pop");
mText.Writer.Write8(0x24); mWriter.Write8(0x24);
} }
public override void WriteINT0() { public override void WriteINT0() {
TraceInstruction("int0"); TraceInstruction("int0");
mText.Writer.Write8(0x25); mWriter.Write8(0x25);
} }
public override void WriteINT1() { public override void WriteINT1() {
TraceInstruction("int1"); TraceInstruction("int1");
mText.Writer.Write8(0x26); mWriter.Write8(0x26);
} }
public override void WriteEND() { public override void WriteEND() {
TraceInstruction("end"); TraceInstruction("end");
mText.Writer.Write8(0x27); mWriter.Write8(0x27);
}
public override void EndText() {
mWriter.PopAnchor();
} }
[Conditional("DEBUG")] [Conditional("DEBUG")]
void TraceInstruction(string format, params object[] args) { void TraceInstruction(string format, params object[] args) {
var instruction = String.Format(format, args); var instruction = String.Format(format, args);
Debug.WriteLine("{0:X8} {1}", mText.Size, instruction); Debug.WriteLine("{0:X8} {1}", mWriter.Position, instruction);
} }
// data // data
public override void BeginData() {
mDataCount = 0;
mDataOffset = Offset;
mWriter.PushAnchor();
mStringTable.Clear();
}
public override void WriteData(string data) { public override void WriteData(string data) {
if (data == null) { if (data == null) {
throw new ArgumentNullException("data"); throw new ArgumentNullException("data");
} }
mData.Writer.Write32(mDataString.Size); mWriter.Write32(mStringTable.Add(data));
mDataString.Writer.WriteString<aZSTR>(data);
++mDataCount; ++mDataCount;
} }
public override void EndData() {
mWriter.WriteString(mStringTable.ToString());
mWriter.PopAnchor();
mStringTable.Clear();
}
// symbol // symbol
public override void BeginSymbol() {
mSymbolCount = 0;
mSymbolOffset = Offset;
mWriter.PushAnchor();
mStringTable.Clear();
}
public override void WriteSymbol(sunSymbolType type, string name, uint data) { public override void WriteSymbol(sunSymbolType type, string name, uint data) {
mSymbol.Writer.WriteS32((int)type); mWriter.WriteS32((int)type);
mSymbol.Writer.Write32(mSymbolString.Size); mWriter.Write32(mStringTable.Add(name));
mSymbol.Writer.Write32(data); mWriter.Write32(data);
mSymbol.Writer.Write32(0u); // runtime field (hash) mWriter.Write32(0u); // runtime field (hash)
mSymbol.Writer.Write32(0u); // runtime field (funcptr) mWriter.Write32(0u); // runtime field (funcptr)
mSymbolString.Writer.WriteString<aZSTR>(name);
++mSymbolCount; ++mSymbolCount;
if (type == sunSymbolType.Variable) { if (type == sunSymbolType.Variable) {
++mVarCount; ++mVarCount;
} }
} }
public override void EndSymbol() {
mWriter.WriteString(mStringTable.ToString());
mWriter.PopAnchor();
mStringTable.Clear();
}
class sunSpcBinarySection : IDisposable { class sunSpcStringTable {
readonly aBinaryWriter mWriter; StringBuilder mBuilder;
readonly MemoryStream mStream; Encoding mEncoding;
uint mSize;
public aBinaryWriter Writer { public sunSpcStringTable(Encoding encoding) {
get { return mWriter; } mBuilder = new StringBuilder(1024);
} mEncoding = encoding;
public MemoryStream Stream {
get { return mStream; }
} }
public uint Offset { public uint Add(string value) {
get { return (uint)mWriter.Position; } var size = mSize;
mBuilder.Append(value);
mBuilder.Append('\0');
mSize += (uint)mEncoding.GetByteCount(value) + 1u; // + null terminator
return size;
} }
public uint Size { public override string ToString() {
get { return (uint)mWriter.Length; } return mBuilder.ToString();
} }
public void Clear() {
public sunSpcBinarySection() { mBuilder.Clear();
mStream = new MemoryStream(1024); mSize = 0;
mWriter = new aBinaryWriter(mStream, Endianness.Big, Encoding.GetEncoding(932));
}
public void Dispose() {
mStream.Dispose();
}
public void Copy(aBinaryWriter writer) {
if (writer == null) {
throw new ArgumentNullException("writer");
}
writer.Write8s(mStream.GetBuffer(), (int)Size);
} }
} }
} }