3
3
using System . Text ;
4
4
using SAP1EMU . Lib ;
5
5
6
+ using Serilog ;
7
+
6
8
using SAP1EMU . Lib . Registers ;
7
9
using SAP1EMU . Lib . Components ;
8
-
10
+ using System . IO ;
11
+ using System . Diagnostics ;
9
12
10
13
namespace SAP1EMU . Engine
11
14
{
12
15
public class EngineProc
13
16
{
14
-
17
+
15
18
string OutPutRegContents = "" ;
16
19
private readonly List < Frame > _FrameStack = new List < Frame > ( ) ;
17
20
private RAMProgram Program { get ; set ; }
@@ -23,7 +26,29 @@ public class EngineProc
23
26
// *************************************************************************
24
27
public void Init ( RAMProgram program , string InstructionSetName = DefaultInstructionSetName )
25
28
{
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
+
26
50
// Get Instruction Set
51
+ Log . Information ( $ "SAP1Emu: Using Instruction Set: \" { InstructionSetName } \" ") ;
27
52
InstructionSet = OpCodeLoader . GetSet ( InstructionSetName ) ;
28
53
29
54
// Init RAM
@@ -32,6 +57,9 @@ public void Init(RAMProgram program, string InstructionSetName = DefaultInstruct
32
57
this . Program = new RAMProgram ( new List < string > ( ) ) ;
33
58
}
34
59
this . Program = program ;
60
+
61
+ Log . Information ( "SAP1Emu: Finished Engine Initialization" ) ;
62
+
35
63
}
36
64
// *************************************************************************
37
65
@@ -41,7 +69,9 @@ public void Init(RAMProgram program, string InstructionSetName = DefaultInstruct
41
69
// *************************************************************************
42
70
public void Run ( )
43
71
{
72
+ Log . Information ( "SAP1Emu: Begin Engine Run" ) ;
44
73
74
+ Log . Verbose ( "SAP1Emu: Initializing Registers" ) ;
45
75
Clock clock = new Clock ( ) ;
46
76
TicTok tictok = new TicTok ( ) ;
47
77
@@ -72,13 +102,18 @@ public void Run()
72
102
ram . Subscribe ( clock ) ;
73
103
74
104
105
+ Log . Verbose ( "SAP1Emu: Initialized Registers" ) ;
75
106
76
107
108
+
109
+ Log . Information ( "SAP1Emu: Loading Ram" ) ;
77
110
// Load the program into the RAM
78
111
ram . LoadProgram ( Program ) ;
112
+ Log . Information ( "SAP1Emu: RAM:\n {RAM}" , Program . RamContents ) ;
79
113
80
114
// Load the intsructionSet into the SEQ
81
115
seq . Load ( InstructionSet ) ;
116
+ Log . Information ( $ "SAP1Emu: Loaded Instruction Set: \" { InstructionSet . SetName } \" ") ;
82
117
83
118
84
119
@@ -90,10 +125,15 @@ public void Run()
90
125
clock . IsEnabled = true ;
91
126
92
127
// 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 ;
94
133
int loop_counter = 0 ;
95
134
96
135
int TState = 1 ;
136
+ Log . Information ( "SAP1Emu: Start Program Execution" ) ;
97
137
while ( clock . IsEnabled )
98
138
{
99
139
if ( TState <= 3 )
@@ -105,6 +145,16 @@ public void Run()
105
145
seq . UpdateControlWordReg ( TState , ireg . ToString ( ) ) ;
106
146
}
107
147
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
+
108
158
clock . SendTicTok ( tictok ) ;
109
159
tictok . ToggleClockState ( ) ;
110
160
clock . SendTicTok ( tictok ) ;
@@ -114,13 +164,35 @@ public void Run()
114
164
115
165
if ( ireg . ToString ( ) == "1111" && TState == 6 )
116
166
{
167
+ Log . Information ( "SAP1Emu: HLT Detected" ) ;
117
168
clock . IsEnabled = false ;
118
169
}
119
170
120
171
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
+
122
192
if ( loop_counter >= max_loop_count )
123
193
{
194
+ Log . Fatal ( $ "SAP1Emu: Infinite Loop Fatal Error: Infinite Loop Detected") ;
195
+ Log . CloseAndFlush ( ) ;
124
196
throw new EngineRuntimeException ( "Engine Error: Infinite Loop Detected" ) ;
125
197
}
126
198
else
@@ -137,8 +209,14 @@ public void Run()
137
209
TState = 1 ;
138
210
}
139
211
}
212
+ Log . Information ( "SAP1Emu: End Program Execution" ) ;
213
+
140
214
141
215
OutPutRegContents = oreg . ToString ( ) ;
216
+ Log . Information ( "SAP1Emu: End Engine Run" ) ;
217
+
218
+ Log . CloseAndFlush ( ) ;
219
+
142
220
#endregion
143
221
}
144
222
// *************************************************************************
0 commit comments