Skip to content

Commit 8dbacaf

Browse files
authored
Merge pull request #3 from RonnyA/ImplementAssembler
Added support for unit testing
2 parents 454b13e + 7af84af commit 8dbacaf

File tree

7 files changed

+288
-57
lines changed

7 files changed

+288
-57
lines changed

Program.cs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static void Main(string[] args)
1313

1414
string defaultS8File = @"s8.s8";
1515

16-
S8Dissasembler s8d = new S8Dissasembler();
16+
S8Dissasembler s8d = new S8Dissasembler();
1717

1818
s8d.Init(defaultS8File);
1919

@@ -40,7 +40,7 @@ static void Main(string[] args)
4040
{
4141
Console.Write("s8 [");
4242
}
43-
Console.Write(currentAddress.ToString("X4") + "] ");
43+
Console.Write(currentAddress.ToString("X3") + "] ");
4444
string input = Console.ReadLine();
4545

4646
try
@@ -87,7 +87,7 @@ static void Main(string[] args)
8787
{
8888
Console.WriteLine("Assembly FAILED!!");
8989
}
90-
else
90+
else
9191
{
9292
s8d = new S8Dissasembler();
9393
s8d.InitFromMemory(s8prog);
@@ -140,7 +140,7 @@ static void Main(string[] args)
140140
}
141141
}
142142
break;
143-
143+
144144
case "INPUT":
145145
case "FØDE":
146146
if (cmd.Length > 1)
@@ -157,6 +157,19 @@ static void Main(string[] args)
157157
currentAddress = s8d.SetPC(start);
158158
break;
159159

160+
case "PC!": // set PC
161+
currentAddress = s8d.SetPC(start, true);
162+
break;
163+
164+
165+
case "UNITTEST":
166+
if (cmd.Length > 1)
167+
{
168+
S8UnitTest s8unit = new S8UnitTest();
169+
currentAddress = s8unit.RunUnitTest(s8d, cmd[1]);
170+
}
171+
break;
172+
160173
case "R":
161174
case "RUN":
162175
currentAddress = s8d.Run();
@@ -176,7 +189,7 @@ static void Main(string[] args)
176189
case "S":
177190
case "STEP":
178191
if (start > 0)
179-
{
192+
{
180193
currentAddress = s8d.Step(start);
181194
}
182195
else
@@ -191,7 +204,7 @@ static void Main(string[] args)
191204
{
192205
s8d.SetMaxTicks(start);
193206
}
194-
207+
195208
Console.WriteLine("MaxTicks is set to " + s8d.GetMaxTicks().ToString());
196209
break;
197210
case ":":
@@ -203,9 +216,16 @@ static void Main(string[] args)
203216
case "D":
204217
currentAddress = s8d.Dissasemble(start, length, showAddress);
205218
break;
219+
case "D!":
220+
currentAddress = s8d.Dissasemble(start, length, showAddress, true);
221+
break;
206222
case "M":
207223
currentAddress = s8d.MemoryDump(start, length, showAddress);
208224
break;
225+
case "M!":
226+
currentAddress = s8d.MemoryDump(start, length, showAddress, true);
227+
break;
228+
209229
case "H":
210230
case "HELP":
211231
case "?":
@@ -266,7 +286,7 @@ private static byte[] Asm(string sledeFile)
266286
Console.WriteLine("Can't find SLEDE8 file " + sledeFile);
267287
}
268288
S8Assembler s8 = new S8Assembler();
269-
289+
270290
return s8.AssembleFile(sledeFile);
271291
}
272292

@@ -294,18 +314,25 @@ static void hard()
294314
static void PrintHelp()
295315
{
296316
Console.WriteLine("D - Dissassemble [start] [length]");
297-
Console.WriteLine("M - Memory Dump [start] [length]");
317+
Console.WriteLine("M - Memory Dump [start] [length]");
318+
Console.WriteLine("Limits itself to inside loaded image");
319+
Console.WriteLine();
320+
321+
Console.WriteLine("D!- Dissassemble [start] [length]");
322+
Console.WriteLine("M!- Memory Dump [start] [length]");
323+
Console.WriteLine("Enables access to memory ourside loaded image"); ;
298324
Console.WriteLine();
299325

300326
Console.WriteLine("");
301327

302-
Console.WriteLine("FØDE - SET INPUT hexhexhex");
303-
Console.WriteLine("PC - SET pc = 0xNNNN");
328+
Console.WriteLine("INPUT - SET INPUT hexhexhex");
329+
Console.WriteLine("PC - SET pc = xxx");
304330
Console.WriteLine("RUN - Run program from 0");
305331
Console.WriteLine("REGS - Dump registers");
306332
Console.WriteLine("RESET - Reset registers");
307333
Console.WriteLine("STEP - Step PC [steps]");
308334
Console.WriteLine("TICKS - Set Max Ticks 0xNN");
335+
Console.WriteLine("UNITTEST [filename] - Run unit tests agains [filename]");
309336
Console.WriteLine("! = Change showaddress flag");
310337
Console.WriteLine("");
311338

ReadMe-release.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dotnet publish -r win-x64 -c release -p:PublishSingleFile=true --self-contained true

S8CPU.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public S8CPU()
8282
public void SetMaxTicks(int Ticks)
8383
{
8484
state.maxTicks = Ticks;
85-
ERROR_MESSAGE[(int)ERROR_MESSAGE_ID.resourcesExhausted] = ERROR_MESSAGE[(int)ERROR_MESSAGE_ID.resourcesExhausted].Replace("${maxTicks}", Ticks.ToString());
85+
8686
}
8787

8888
//const memory = load(executable);
@@ -91,8 +91,13 @@ public void SetMaxTicks(int Ticks)
9191

9292
public byte[] Load(byte[] executable, bool skipMagicHeader)
9393
{
94+
int oldMaxTicks = DEFAULT_MAX_STEPS;
95+
if (state is not null)
96+
{
97+
oldMaxTicks = state.maxTicks;
98+
}
9499
state = new CpuState();
95-
SetMaxTicks(DEFAULT_MAX_STEPS);
100+
SetMaxTicks(oldMaxTicks);
96101

97102
ResetRegs();
98103

@@ -190,7 +195,8 @@ internal bool RunUntil(int stop_pc_at)
190195
{
191196
if (++state.tick > state.maxTicks)
192197
{
193-
Console.WriteLine(ERROR_MESSAGE[(int)ERROR_MESSAGE_ID.resourcesExhausted]);
198+
var strErr = ERROR_MESSAGE[(int)ERROR_MESSAGE_ID.resourcesExhausted].Replace("${maxTicks}", state.tick.ToString());
199+
Console.WriteLine(strErr);
194200
return false;
195201
}
196202

S8Dissasembler.cs

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace S8Debugger
1313
{
1414
public class S8Dissasembler
15-
{
16-
byte[] bytes = null;
15+
{
16+
byte[] bytes = new byte[4096];
1717
S8CPU cpu = new S8CPU();
1818

1919
public bool Init(string fname, bool force = false)
@@ -39,8 +39,8 @@ internal bool InitFromMemory(byte[] s8prog)
3939
return true;
4040
}
4141

42-
public int MemoryDump(int start, int length, bool showAddress = false)
43-
{
42+
public int MemoryDump(int start, int length, bool showAddress = false, bool allowOutsideLoadedMemory = false)
43+
{
4444
int currentAddress = start;
4545
int endAddress = currentAddress + length;
4646

@@ -51,25 +51,30 @@ public int MemoryDump(int start, int length, bool showAddress = false)
5151
//endAddress = cpu.state.memoryUsed;
5252
}
5353

54-
if (endAddress > cpu.state.memoryUsed)
54+
if (!allowOutsideLoadedMemory)
5555
{
56-
endAddress = cpu.state.memoryUsed;
56+
if (endAddress > cpu.state.memoryUsed)
57+
{
58+
endAddress = cpu.state.memoryUsed;
59+
}
5760
}
5861

59-
6062

6163
int lineCounter = 0;
6264

6365
string line1 = "";
6466
string line2 = "";
6567

68+
6669
while (currentAddress < endAddress)
6770
{
68-
71+
6972
if (lineCounter == 0)
7073
{
74+
string sHexAddress = currentAddress.ToString("X3");
75+
Console.WriteLine("m" + sHexAddress + ":");
7176
line1 = ".DATA ";
72-
line2 = ";" + currentAddress.ToString("X4") + ": ";
77+
line2 = ";" + currentAddress.ToString("X3") + ": ";
7378
}
7479
else
7580
{
@@ -104,22 +109,22 @@ public int MemoryDump(int start, int length, bool showAddress = false)
104109

105110
lineCounter = 0;
106111
Console.WriteLine();
107-
}
112+
}
108113
}
109114

110115
Console.WriteLine();
111116

112117
return currentAddress;
113118
}
114119

115-
120+
116121

117122
internal void Reset()
118123
{
119124
cpu.ResetRegs();
120125
}
121126

122-
public int Dissasemble(int start, int length, bool showAddress = false)
127+
public int Dissasemble(int start, int length, bool showAddress = false, bool allowOutsideLoadedMemory = false)
123128
{
124129
S8Instruction s8i;
125130

@@ -132,29 +137,45 @@ public int Dissasemble(int start, int length, bool showAddress = false)
132137
endAddress = currentAddress + 8;
133138
//endAddress = cpu.state.memoryUsed;
134139
}
135-
136-
if (endAddress > cpu.state.memoryUsed)
140+
if (!allowOutsideLoadedMemory)
137141
{
138-
endAddress = cpu.state.memoryUsed;
142+
if (endAddress > cpu.state.memoryUsed)
143+
{
144+
endAddress = cpu.state.memoryUsed;
145+
}
139146
}
140147

141148
while (currentAddress < endAddress)
142149
{
143-
string sHexAddress = currentAddress.ToString("X4");
150+
string sHexAddress = currentAddress.ToString("X3");
144151

145152
byte opcode = bytes[currentAddress++];
146153
byte param = bytes[currentAddress++];
147-
154+
148155
s8i = new S8Instruction(opcode, param);
149156
s8i.DecodeInstruction();
150157

158+
if (s8i.ValidInstruction)
159+
{
160+
if (!showAddress)
161+
Console.WriteLine("a" + sHexAddress + ":");
162+
}
163+
else
164+
{
165+
if (!showAddress)
166+
Console.WriteLine("m" + sHexAddress + ":");
167+
}
168+
151169
if (showAddress)
152170
{
153171
string sOpcode = opcode.ToString("X2");
154172
string sParam = param.ToString("X2");
155-
Console.Write("A["+sHexAddress + "] | I["+sOpcode + " " + sParam + "] ");
173+
Console.Write("A[" + sHexAddress + "] | I[" + sOpcode + " " + sParam + "] ");
156174
}
157-
175+
176+
177+
178+
158179
if (s8i.ValidInstruction)
159180
{
160181
Console.WriteLine(s8i.DecodedInstruction);
@@ -169,28 +190,47 @@ public int Dissasemble(int start, int length, bool showAddress = false)
169190

170191
}
171192

172-
internal int SetPC(int start)
193+
internal int SetPC(int start, bool allowOutsideLoadedMemory = false)
173194
{
174-
if (start > cpu.state.memoryUsed)
175-
{
195+
if (start > 0xFFF)
176196
start = 0;
197+
198+
if (!allowOutsideLoadedMemory)
199+
{
200+
if (start > cpu.state.memoryUsed)
201+
{
202+
start = 0;
203+
}
177204
}
178205
cpu.state.pc = start;
179206

180207
return cpu.state.pc;
181208
}
182209

210+
internal void SetInput(byte[] inputBuffer)
211+
{
212+
cpu.state.stdin = inputBuffer;
213+
}
214+
183215
internal void SetInput(string v)
184216
{
185217
string s = ConvertHex2Asii(v);
186218

187219
cpu.state.stdin = new byte[s.Length];
188-
for (int i=0;i<s.Length;i++)
220+
for (int i = 0; i < s.Length; i++)
189221
{
190222
cpu.state.stdin[i] = (byte)s[i];
191-
}
223+
}
224+
}
225+
internal string GetOutput()
226+
{
227+
return cpu.state.stdout;
192228
}
193229

230+
internal void ClearOutput()
231+
{
232+
cpu.state.stdout = "";
233+
}
194234

195235
public void SetMaxTicks(int Ticks)
196236
{
@@ -202,18 +242,20 @@ public int GetMaxTicks()
202242
return cpu.state.maxTicks;
203243
}
204244

205-
public int Run()
245+
246+
public int Run(bool ShowOppgulp = true)
206247
{
207248
var stopwatch = new Stopwatch();
208-
stopwatch.Start();
209-
249+
stopwatch.Start();
250+
210251
cpu.Run();
211252

212253
stopwatch.Stop();
213254
var elapsed_time = stopwatch.ElapsedMilliseconds;
214255

215256
Console.WriteLine("Elapsed time " + elapsed_time + "ms, Ticks " + cpu.state.tick);
216-
Oppgulp();
257+
if (ShowOppgulp)
258+
Oppgulp();
217259
return cpu.state.pc;
218260
}
219261

@@ -286,9 +328,9 @@ private void Oppgulp()
286328

287329
public void Regs()
288330
{
289-
Console.WriteLine("PC [" + cpu.state.pc.ToString("X4") +"]");
290-
Console.WriteLine("FLAG [" + cpu.state.flag +"]");
291-
for (int i=0; i<16;i++)
331+
Console.WriteLine("PC [" + cpu.state.pc.ToString("X3") + "]");
332+
Console.WriteLine("FLAG [" + cpu.state.flag + "]");
333+
for (int i = 0; i < 16; i++)
292334
{
293335
Console.Write("R" + i + "[" + cpu.state.regs[i].ToString("X2") + "] ");
294336
}

0 commit comments

Comments
 (0)