Skip to content

Commit 005bbad

Browse files
authored
Merge pull request #46 from rbaker26/infinite_loop_fix
fixed infinite loop and added debug info to log file
2 parents 4ac18bc + a947768 commit 005bbad

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

SAP1EMU.Engine-CLI/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ static void Main(string[] args)
223223
{
224224
Console.ForegroundColor = ConsoleColor.Red;
225225
}
226+
227+
226228
//Console.SetOut(new StreamWriter(Console.OpenStandardOutput()));
227229
//Console.SetError(new StreamWriter(Console.OpenStandardError()));
228230

229-
Console.Error.WriteLine($"SAP1EMU: fatal error: " + ere.Message + " " + ere.InnerException.Message);
231+
Console.Error.WriteLine($"SAP1EMU: fatal error: " + ere.Message);
230232
Console.ForegroundColor = tempColor;
231233
Console.Error.WriteLine("emulation terminated");
232234

SAP1EMU.Engine/EngineProc.cs

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
using System.Text;
44
using SAP1EMU.Lib;
55

6+
using Serilog;
7+
68
using SAP1EMU.Lib.Registers;
79
using SAP1EMU.Lib.Components;
8-
10+
using System.IO;
11+
using System.Diagnostics;
912

1013
namespace SAP1EMU.Engine
1114
{
1215
public class EngineProc
1316
{
14-
17+
1518
string OutPutRegContents = "";
1619
private readonly List<Frame> _FrameStack = new List<Frame>();
1720
private RAMProgram Program { get; set; }
@@ -23,7 +26,29 @@ public class EngineProc
2326
// *************************************************************************
2427
public void Init(RAMProgram program, string InstructionSetName = DefaultInstructionSetName)
2528
{
29+
string log_name = "runtime_log.txt";
30+
// Clear Old Log
31+
if (File.Exists(log_name))
32+
{
33+
File.Delete(log_name);
34+
}
35+
File.Create(log_name).Close(); // must close the file or the handle will stay open and be locked
36+
37+
38+
// Init Logger
39+
Log.Logger = new LoggerConfiguration()
40+
.WriteTo.File(log_name)
41+
// .WriteTo.Console()
42+
.CreateLogger();
43+
44+
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
45+
Serilog.Debugging.SelfLog.Enable(Console.Error);
46+
47+
Log.Information("SAP1Emu: Begin Engine Initialization");
48+
49+
2650
// Get Instruction Set
51+
Log.Information($"SAP1Emu: Using Instruction Set: \"{InstructionSetName}\"");
2752
InstructionSet = OpCodeLoader.GetSet(InstructionSetName);
2853

2954
// Init RAM
@@ -32,6 +57,9 @@ public void Init(RAMProgram program, string InstructionSetName = DefaultInstruct
3257
this.Program = new RAMProgram(new List<string>());
3358
}
3459
this.Program = program;
60+
61+
Log.Information("SAP1Emu: Finished Engine Initialization");
62+
3563
}
3664
// *************************************************************************
3765

@@ -41,7 +69,9 @@ public void Init(RAMProgram program, string InstructionSetName = DefaultInstruct
4169
// *************************************************************************
4270
public void Run()
4371
{
72+
Log.Information("SAP1Emu: Begin Engine Run");
4473

74+
Log.Verbose("SAP1Emu: Initializing Registers");
4575
Clock clock = new Clock();
4676
TicTok tictok = new TicTok();
4777

@@ -72,13 +102,18 @@ public void Run()
72102
ram.Subscribe(clock);
73103

74104

105+
Log.Verbose("SAP1Emu: Initialized Registers");
75106

76107

108+
109+
Log.Information("SAP1Emu: Loading Ram");
77110
// Load the program into the RAM
78111
ram.LoadProgram(Program);
112+
Log.Information("SAP1Emu: RAM:\n{RAM}", Program.RamContents);
79113

80114
// Load the intsructionSet into the SEQ
81115
seq.Load(InstructionSet);
116+
Log.Information($"SAP1Emu: Loaded Instruction Set: \"{InstructionSet.SetName}\"");
82117

83118

84119

@@ -90,10 +125,15 @@ public void Run()
90125
clock.IsEnabled = true;
91126

92127
// These vars will make sure that the engine will not hang if there is an infinite loop
93-
int max_loop_count = 5000;
128+
int warning1_loop_counter = 3000;
129+
int warning2_loop_counter = 5000;
130+
int warning3_loop_counter = 7000;
131+
int warning4_loop_counter = 9000;
132+
int max_loop_count = 10000;
94133
int loop_counter = 0;
95134

96135
int TState = 1;
136+
Log.Information("SAP1Emu: Start Program Execution");
97137
while(clock.IsEnabled)
98138
{
99139
if(TState <= 3)
@@ -105,6 +145,16 @@ public void Run()
105145
seq.UpdateControlWordReg(TState, ireg.ToString());
106146
}
107147

148+
149+
// Log the Instruction
150+
if(TState == 4)
151+
{
152+
string iname = InstructionSet.instructions.Find(x => x.BinCode.Equals(ireg.ToString())).OpCode;
153+
int operandVal = Convert.ToInt32(ireg.ToString_Frame_Use().Substring(4, 4), 2);
154+
string hexOperand = "0x" + operandVal.ToString("X");
155+
Log.Information($"SAP1Emu: Instruction: {iname}, Operand: {hexOperand}");
156+
}
157+
108158
clock.SendTicTok(tictok);
109159
tictok.ToggleClockState();
110160
clock.SendTicTok(tictok);
@@ -114,13 +164,35 @@ public void Run()
114164

115165
if (ireg.ToString() == "1111" && TState == 6)
116166
{
167+
Log.Information("SAP1Emu: HLT Detected");
117168
clock.IsEnabled = false;
118169
}
119170

120171

121-
// Infinite Loop Check
172+
// Infinite Loop Checks
173+
if(loop_counter == warning1_loop_counter)
174+
{
175+
Log.Warning($"SAP1Emu: Infinite Loop Warning: interations count: {warning1_loop_counter} ");
176+
}
177+
else if (loop_counter == warning2_loop_counter)
178+
{
179+
Log.Warning($"SAP1Emu: Infinite Loop Warning: interations count: {warning2_loop_counter} ");
180+
}
181+
else if (loop_counter == warning3_loop_counter)
182+
{
183+
Log.Warning($"SAP1Emu: Infinite Loop Warning: interations count: {warning3_loop_counter} ");
184+
}
185+
else if (loop_counter == warning4_loop_counter)
186+
{
187+
Log.Warning($"SAP1Emu: Infinite Loop Warning: interations count: {warning4_loop_counter} ");
188+
}
189+
190+
191+
122192
if (loop_counter >= max_loop_count)
123193
{
194+
Log.Fatal($"SAP1Emu: Infinite Loop Fatal Error: Infinite Loop Detected");
195+
Log.CloseAndFlush();
124196
throw new EngineRuntimeException("Engine Error: Infinite Loop Detected");
125197
}
126198
else
@@ -137,8 +209,14 @@ public void Run()
137209
TState = 1;
138210
}
139211
}
212+
Log.Information("SAP1Emu: End Program Execution");
213+
140214

141215
OutPutRegContents = oreg.ToString();
216+
Log.Information("SAP1Emu: End Engine Run");
217+
218+
Log.CloseAndFlush();
219+
142220
#endregion
143221
}
144222
// *************************************************************************

SAP1EMU.Engine/SAP1EMU.Engine.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
<Company />
1111
</PropertyGroup>
1212

13+
<ItemGroup>
14+
<PackageReference Include="Serilog" Version="2.9.0" />
15+
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
16+
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
17+
</ItemGroup>
18+
1319
<ItemGroup>
1420
<ProjectReference Include="..\SAP1EMU.Lib\SAP1EMU.Lib.csproj" />
1521
</ItemGroup>

SAP1EMU.Lib/Frame.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public Frame(string instruction, int TState, AReg areg, BReg breg, IReg ireg, MR
5858

5959
}
6060

61+
62+
// TODO - Repleace with something in the LIB OpCodeLoader
6163
private string InstuctionDecode(string BinInstruction, int TState)
6264
{
6365
List<string> KnownInstructions = new List<string> { "LDA", "ADD", "SUB", "STA", "JMP", "JEQ", "", "", "", "JIC", "", "", "", "", "OUT", "HLT" };

0 commit comments

Comments
 (0)