Skip to content

Commit

Permalink
Conditional compilation of extension sets
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed May 4, 2024
1 parent 27ba65e commit 9bd3acb
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 40 deletions.
51 changes: 48 additions & 3 deletions AAPFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,63 @@ namespace AssEmbly
public enum AAPFeatures : ulong
{
None = 0,
#if V1_CALL_STACK_COMPAT
V1CallStack = 0b1,
#endif
#if EXTENSION_SET_SIGNED
ExtensionSigned = 0b10,
#endif
#if EXTENSION_SET_FLOATING_POINT
ExtensionFloat = 0b100,
#endif
#if EXTENSION_SET_EXTENDED_BASE
ExtensionExtendedBase = 0b1000,
#endif
#if GZIP_COMPRESSION
GZipCompressed = 0b10000,
#endif
#if EXTENSION_SET_EXTERNAL_ASM
ExtensionExternalAssembly = 0b100000,
#endif
#if EXTENSION_SET_HEAP_ALLOCATE
ExtensionMemoryAllocation = 0b1000000,
#endif
#if EXTENSION_SET_FILE_SYSTEM
ExtensionFileSystem = 0b10000000,
#endif
#if EXTENSION_SET_TERMINAL
ExtensionTerminal = 0b100000000,
#endif

All = V1CallStack
All = None
#if V1_CALL_STACK_COMPAT
| V1CallStack
#endif
#if EXTENSION_SET_SIGNED
| ExtensionSigned
#endif
#if EXTENSION_SET_FLOATING_POINT
| ExtensionFloat
#endif
#if EXTENSION_SET_EXTENDED_BASE
| ExtensionExtendedBase
#endif
#if GZIP_COMPRESSION
| GZipCompressed
#endif
#if EXTENSION_SET_EXTERNAL_ASM
| ExtensionExternalAssembly
#endif
#if EXTENSION_SET_HEAP_ALLOCATE
| ExtensionMemoryAllocation
#endif
#if EXTENSION_SET_FILE_SYSTEM
| ExtensionFileSystem
| ExtensionTerminal,
Incompatible = ~All
#endif
#if EXTENSION_SET_TERMINAL
| ExtensionTerminal
#endif
, Incompatible = ~All
}

public class AAPFile
Expand Down Expand Up @@ -71,6 +108,7 @@ public AAPFile(byte[] executable)
Features = (AAPFeatures)BinaryPrimitives.ReadUInt64LittleEndian(byteSpan[20..]);
EntryPoint = BinaryPrimitives.ReadUInt64LittleEndian(byteSpan[28..]);

#if GZIP_COMPRESSION
if (Features.HasFlag(AAPFeatures.GZipCompressed))
{
using MemoryStream compressedProgram = new(executable[36..]);
Expand All @@ -81,13 +119,17 @@ public AAPFile(byte[] executable)
}
else
{
#endif
Program = executable[36..];
#if GZIP_COMPRESSION
}
#endif
}

public byte[] GetBytes()
{
byte[] programBytes;
#if GZIP_COMPRESSION
if (Features.HasFlag(AAPFeatures.GZipCompressed))
{
using MemoryStream compressedProgram = new();
Expand All @@ -99,8 +141,11 @@ public byte[] GetBytes()
}
else
{
#endif
programBytes = Program;
#if GZIP_COMPRESSION
}
#endif

byte[] bytes = new byte[HeaderSize + programBytes.Length];
Span<byte> byteSpan = bytes.AsSpan();
Expand Down
4 changes: 4 additions & 0 deletions AssEmbly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
</PropertyGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);V1_CALL_STACK_COMPAT;EXTENSION_SET_SIGNED;EXTENSION_SET_FLOATING_POINT;EXTENSION_SET_EXTENDED_BASE;GZIP_COMPRESSION;EXTENSION_SET_EXTERNAL_ASM;EXTENSION_SET_HEAP_ALLOCATE;EXTENSION_SET_FILE_SYSTEM;EXTENSION_SET_TERMINAL;</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Example Programs\C# Interop\**" />
<Compile Remove="Test\**" />
Expand Down
56 changes: 56 additions & 0 deletions Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,62 @@ public Assembler(bool usingV1Format, bool usingV1Stack,
{ "CURRENT_ADDRESS", () => (ulong)program.Count },
{ "OBSOLETE_DIRECTIVES", () => EnableObsoleteDirectives ? 1UL : 0UL },
{ "ESCAPE_SEQUENCES", () => EnableEscapeSequences ? 1UL : 0UL },
{
"EXTENSION_SET_SIGNED_AVAIL", () =>
#if EXTENSION_SET_SIGNED
1
#else
0
#endif
},
{
"EXTENSION_SET_FLOATING_POINT_AVAIL", () =>
#if EXTENSION_SET_FLOATING_POINT
1
#else
0
#endif
},
{
"EXTENSION_SET_EXTENDED_BASE_AVAIL", () =>
#if EXTENSION_SET_EXTENDED_BASE
1
#else
0
#endif
},
{
"EXTENSION_SET_EXTERNAL_ASM_AVAIL", () =>
#if EXTENSION_SET_EXTERNAL_ASM
1
#else
0
#endif
},
{
"EXTENSION_SET_HEAP_ALLOCATE_AVAIL", () =>
#if EXTENSION_SET_HEAP_ALLOCATE
1
#else
0
#endif
},
{
"EXTENSION_SET_FILE_SYSTEM_AVAIL", () =>
#if EXTENSION_SET_FILE_SYSTEM
1
#else
0
#endif
},
{
"EXTENSION_SET_TERMINAL_AVAIL", () =>
#if EXTENSION_SET_TERMINAL
1
#else
0
#endif
},
};

InitializeStateDirectives(out stateDirectives, out obsoleteStateDirectives);
Expand Down
37 changes: 34 additions & 3 deletions Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ public enum Register
[Flags]
public enum StatusFlags
{
// Base
Zero = 0b1,
Carry = 0b10,
FileEnd = 0b100,
// Signed
#if EXTENSION_SET_SIGNED
Sign = 0b1000,
Overflow = 0b10000,
#endif
#if EXTENSION_SET_TERMINAL
AutoEcho = 0b100000,
#endif

// Base
ZeroAndCarry = Zero | Carry,
#if EXTENSION_SET_SIGNED
// Signed
SignAndOverflow = Sign | Overflow,
#endif
}

/// <summary>
Expand All @@ -53,13 +56,27 @@ public static class Data
{
public static readonly Dictionary<byte, AAPFeatures> ExtensionSetFeatureFlags = new()
{
#if EXTENSION_SET_SIGNED
{ 0x01, AAPFeatures.ExtensionSigned },
#endif
#if EXTENSION_SET_FLOATING_POINT
{ 0x02, AAPFeatures.ExtensionFloat },
#endif
#if EXTENSION_SET_EXTENDED_BASE
{ 0x03, AAPFeatures.ExtensionExtendedBase },
#endif
#if EXTENSION_SET_EXTERNAL_ASM
{ 0x04, AAPFeatures.ExtensionExternalAssembly },
#endif
#if EXTENSION_SET_HEAP_ALLOCATE
{ 0x05, AAPFeatures.ExtensionMemoryAllocation },
#endif
#if EXTENSION_SET_FILE_SYSTEM
{ 0x06, AAPFeatures.ExtensionFileSystem },
#endif
#if EXTENSION_SET_TERMINAL
{ 0x07, AAPFeatures.ExtensionTerminal },
#endif
};

/// <summary>
Expand Down Expand Up @@ -350,6 +367,7 @@ public static class Data
// RFC (Read Character from Open File as a Byte)
{ ("RFC", new OperandType[1] { OperandType.Register }), new Opcode(0x00, 0xF1) },

#if EXTENSION_SET_SIGNED
// SIGNED EXTENSION SET

// Signed Conditional Jumps
Expand Down Expand Up @@ -458,7 +476,9 @@ public static class Data

// SIGN_NEG (Two's Complement Negation)
{ ("SIGN_NEG", new OperandType[1] { OperandType.Register }), new Opcode(0x01, 0x80) },
#endif

#if EXTENSION_SET_FLOATING_POINT
// FLOATING POINT EXTENSION SET

// Math (All operations store result in the first register operand)
Expand Down Expand Up @@ -575,7 +595,9 @@ public static class Data
{ ("FLPT_CMP", new OperandType[2] { OperandType.Register, OperandType.Literal }), new Opcode(0x02, 0xD1) }, // (reg - lit)
{ ("FLPT_CMP", new OperandType[2] { OperandType.Register, OperandType.Address }), new Opcode(0x02, 0xD2) }, // (reg - adr)
{ ("FLPT_CMP", new OperandType[2] { OperandType.Register, OperandType.Pointer }), new Opcode(0x02, 0xD3) }, // (reg - ptr)
#endif

#if EXTENSION_SET_EXTENDED_BASE
// EXTENDED BASE SET

// EXTD_BSW (Reverse Byte Order)
Expand All @@ -601,7 +623,9 @@ public static class Data
{ ("EXTD_MPA", new OperandType[2] { OperandType.Register, OperandType.Pointer }), new Opcode(0x03, 0x30) },
{ ("EXTD_MPA", new OperandType[2] { OperandType.Address, OperandType.Pointer }), new Opcode(0x03, 0x31) },
{ ("EXTD_MPA", new OperandType[2] { OperandType.Pointer, OperandType.Pointer }), new Opcode(0x03, 0x32) },
#endif

#if EXTENSION_SET_EXTERNAL_ASM
// EXTERNAL ASSEMBLY EXTENSION SET

// ASMX_LDA (Load Assembly)
Expand Down Expand Up @@ -632,7 +656,9 @@ public static class Data
{ ("ASMX_CAL", new OperandType[1] { OperandType.Literal }), new Opcode(0x04, 0x32) },
{ ("ASMX_CAL", new OperandType[1] { OperandType.Address }), new Opcode(0x04, 0x33) },
{ ("ASMX_CAL", new OperandType[1] { OperandType.Pointer }), new Opcode(0x04, 0x34) },
#endif

#if EXTENSION_SET_HEAP_ALLOCATE
// MEMORY ALLOCATION EXTENSION SET

// HEAP_ALC (Allocate Heap Memory - Error if Fail)
Expand Down Expand Up @@ -661,7 +687,9 @@ public static class Data

// HEAP_FRE (Free Allocated Heap Memory)
{ ("HEAP_FRE", new OperandType[1] { OperandType.Register }), new Opcode(0x05, 0x20) },
#endif

#if EXTENSION_SET_FILE_SYSTEM
// FILE SYSTEM EXTENSION SET

// FSYS_CWD (Change Working Directory to Path Specified by 0x00 Terminated String in Memory)
Expand Down Expand Up @@ -742,7 +770,9 @@ public static class Data
{ ("FSYS_SAT", new OperandType[2] { OperandType.Pointer, OperandType.Register }), new Opcode(0x06, 0x89) },
{ ("FSYS_SAT", new OperandType[2] { OperandType.Address, OperandType.Literal }), new Opcode(0x06, 0x8A) },
{ ("FSYS_SAT", new OperandType[2] { OperandType.Pointer, OperandType.Literal }), new Opcode(0x06, 0x8B) },
#endif

#if EXTENSION_SET_TERMINAL
// TERMINAL EXTENSION SET

// TERM_CLS (Clear Screen)
Expand Down Expand Up @@ -795,6 +825,7 @@ public static class Data

// TERM_RSC (Reset Color)
{ ("TERM_RSC", Array.Empty<OperandType>()), new Opcode(0x07, 0x58) },
#endif
};

/// <summary>
Expand Down
28 changes: 24 additions & 4 deletions Debugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ public class Debugger

public List<(Register Register, ulong Value)> Breakpoints { get; } = new();

#if V1_CALL_STACK_COMPAT
public bool UseV1CallStack => DebuggingProcessor.UseV1CallStack;
private ulong stackCallSize => UseV1CallStack ? 24UL : 16UL;
private Register[] registerPushOrder => UseV1CallStack
? new Register[3] { Register.rso, Register.rsb, Register.rpo }
: new Register[2] { Register.rsb, Register.rpo };
#else
private const ulong stackCallSize = 16;
#endif

private Register[] registerPushOrder =>
#if V1_CALL_STACK_COMPAT
UseV1CallStack ? new Register[3] { Register.rso, Register.rsb, Register.rpo } :
#endif
new Register[2] { Register.rsb, Register.rpo };

private readonly ulong[] replPreviousRegisters = new ulong[Enum.GetNames(typeof(Register)).Length];
private readonly Dictionary<string, ulong> replLabels = new() { { "START", 0 } };
Expand Down Expand Up @@ -286,9 +293,11 @@ public void StartDebugger()
case "breakpoint":
CommandBreakpointManage(command);
break;
#if EXTENSION_SET_HEAP_ALLOCATE
case "heap":
CommandHeapStats();
break;
#endif
case "help":
CommandDebugHelp();
break;
Expand Down Expand Up @@ -513,11 +522,13 @@ private void CommandMapMemory(string[] command)
for (ulong i = rowStartAdr; i < rowStartAdr + 16; i++)
{
string valueStr = string.Format(Strings.Debugger_MemoryMap_Cell, DebuggingProcessor.Memory[i]);
#if EXTENSION_SET_HEAP_ALLOCATE
// Being unmapped is the lowest priority colour, and should be completely replaced by any register colours
if (DebuggingProcessor.MappedMemoryRanges.All(mappedRange => !mappedRange.Contains((long)i)))
{
Console.ForegroundColor = ConsoleColor.DarkGray;
}
#endif
// Write both characters separately so that if multiple registers point to the same address, the cell becomes multi-coloured
if (i == DebuggingProcessor.Registers[(int)Register.rso])
{
Expand Down Expand Up @@ -649,13 +660,20 @@ private void CommandFormatStack(string[] command)
Console.WriteLine(Strings.Debugger_Stack_Box_Top);
Console.WriteLine(Strings.Debugger_Stack_ReturnInfo_First, currentStackBase, registerPushOrder[0], DebuggingProcessor.ReadMemoryQWord(currentStackBase));
Console.WriteLine(Strings.Debugger_Stack_ReturnInfo_Second, currentStackBase + 8, registerPushOrder[1], DebuggingProcessor.ReadMemoryQWord(currentStackBase + 8));
#if V1_CALL_STACK_COMPAT
if (UseV1CallStack)
{
Console.WriteLine(Strings.Debugger_Stack_ReturnInfo_Third, currentStackBase + 16, registerPushOrder[2], DebuggingProcessor.ReadMemoryQWord(currentStackBase + 16));
}
#endif
Console.WriteLine(Strings.Debugger_Stack_Box_Bottom);

ulong parentStackBase = DebuggingProcessor.ReadMemoryQWord(currentStackBase + (UseV1CallStack ? 8UL : 0UL));
ulong parentStackBase = DebuggingProcessor.ReadMemoryQWord(currentStackBase
#if V1_CALL_STACK_COMPAT
+ (UseV1CallStack ? 8UL : 0UL)
#endif
);

if (currentStackBase + stackCallSize >= parentStackBase)
{
// Parent stack is empty
Expand Down Expand Up @@ -798,6 +816,7 @@ private void CommandBreakpointManage(string[] command)
}
}

#if EXTENSION_SET_HEAP_ALLOCATE
private void CommandHeapStats()
{
long memorySize = DebuggingProcessor.Memory.LongLength;
Expand Down Expand Up @@ -877,6 +896,7 @@ private void CommandHeapStats()
}
Console.WriteLine();
}
#endif

private static void CommandDebugHelp()
{
Expand Down
Loading

0 comments on commit 9bd3acb

Please sign in to comment.