|
1 | | -using Chip8.Instructions; |
2 | | -using Moq; |
3 | | -using NUnit.Framework; |
4 | | -using NUnit.Framework.Legacy; |
5 | | -using System.Collections.Generic; |
6 | | -using System.IO; |
7 | | -using System.Linq; |
8 | | - |
9 | | -namespace Chip8.Tests; |
10 | | - |
11 | | -public class CompilationAndDecompilationOfSampleApplications |
12 | | -{ |
13 | | - [Test] |
14 | | - [Ignore("Executed manually")] |
15 | | - public void LoadingThenRetrievingApplicationInEmulator_ReturnsSameByteArray() |
16 | | - { |
17 | | - var directory = new DirectoryInfo(@"..\..\..\..\..\games"); |
18 | | - foreach (var file in Directory.EnumerateFiles(directory.FullName).Where(x => !x.EndsWith(".c8"))) |
19 | | - { |
20 | | - var originalGame = File.ReadAllBytes(file); |
21 | | - |
22 | | - var emulator = new Emulator(new Cpu(), new Mock<IDisplay>().Object, new Mock<IKeyboard>().Object, new Mock<ISound>().Object); |
23 | | - emulator.LoadApplication(originalGame); |
24 | | - |
25 | | - var gameFromEmulator = emulator.GetApplication(); |
26 | | - |
27 | | - CollectionAssert.AreEqual(originalGame, gameFromEmulator, $"File: {file}"); |
28 | | - //File.WriteAllBytes(file, gameFromEmulator); |
29 | | - } |
30 | | - } |
31 | | - |
32 | | - [Test] |
33 | | - [Ignore("Executed manually")] |
34 | | - public void DecompiledApplication_IsSameAs_AlreadyDecompiledApplication() |
35 | | - { |
36 | | - var directory = new DirectoryInfo(@"..\..\..\..\..\games"); |
37 | | - foreach (var file in Directory.EnumerateFiles(directory.FullName).Where(x => !x.EndsWith(".c8"))) |
38 | | - { |
39 | | - var application = File.ReadAllBytes(file); |
40 | | - var instructionDecoder = new InstructionDecoder(); |
41 | | - |
42 | | - var instructions = new List<string>(); |
43 | | - for (int i = 0; i < application.Length - 1; i += 2) |
44 | | - { |
45 | | - var instruction = GetInstruction(application[i], application[i + 1], instructionDecoder); |
46 | | - instructions.Add(instruction); |
47 | | - } |
48 | | - |
49 | | - // if length of the application is not an even number, that means that the last instruction |
50 | | - // was skipped (in the preceding for loop) because it did not start on an even address |
51 | | - if (application.Length % 2 != 0) |
52 | | - { |
53 | | - var instruction = GetInstruction(application[application.Length - 1], 0x0, instructionDecoder); |
54 | | - instructions.Add(instruction); |
55 | | - } |
56 | | - |
57 | | - var codeFilePath = $"{file}.c8"; |
58 | | - var alreadyDecompiledApplication = File.ReadAllLines(codeFilePath); |
59 | | - CollectionAssert.AreEqual(alreadyDecompiledApplication, instructions, $"File: {codeFilePath}"); |
60 | | - //File.WriteAllLines(codeFilePath, instructions); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - [Test] |
65 | | - [Ignore("Executed manually")] |
66 | | - public void CompiledApplication_IsSameAs_AlreadyCompiledApplication() |
67 | | - { |
68 | | - var directory = new DirectoryInfo(@"..\..\..\..\..\games"); |
69 | | - foreach (var file in Directory.EnumerateFiles(directory.FullName).Where(x => x.EndsWith(".c8"))) |
70 | | - { |
71 | | - var decompiledApplication = File.ReadAllLines(file); |
72 | | - var instructionEncoder = new InstructionEncoder(); |
73 | | - |
74 | | - // Convert all lines of code into CPU instructions |
75 | | - var cpuInstructions = new List<CpuInstruction>(); |
76 | | - for (int i = 0; i < decompiledApplication.Length; i++) |
77 | | - { |
78 | | - var cpuInstruction = instructionEncoder.GetCpuInstruction(decompiledApplication[i]); |
79 | | - cpuInstructions.Add(cpuInstruction); |
80 | | - } |
81 | | - |
82 | | - var application = new List<byte>(); |
83 | | - foreach (var cpuInstruction in cpuInstructions) |
84 | | - { |
85 | | - // First add most significant byte |
86 | | - application.Add((byte)((cpuInstruction.Decoded.InstructionCode & 0xFF00) >> 8)); |
87 | | - |
88 | | - // Then least significant byte |
89 | | - application.Add(cpuInstruction.Decoded.kk); |
90 | | - } |
91 | | - |
92 | | - if (application[application.Count - 1] == 0x0) |
93 | | - { |
94 | | - application.RemoveAt(application.Count - 1); |
95 | | - } |
96 | | - |
97 | | - var binaryFilePath = file.Substring(0, file.Length - 3); |
98 | | - var alreadyCompiledApplication = File.ReadAllBytes(binaryFilePath); |
99 | | - CollectionAssert.AreEqual(alreadyCompiledApplication, application, $"File: {binaryFilePath}"); |
100 | | - //File.WriteAllBytes(binaryFilePath, application.ToArray()); |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - private string GetInstruction(byte msb, byte lsb, InstructionDecoder instructionDecoder) |
105 | | - { |
106 | | - var decodedInstruction = instructionDecoder.Decode(msb, lsb); |
107 | | - var cpuInstruction = instructionDecoder.GetCpuInstruction(decodedInstruction); |
108 | | - return cpuInstruction.Mnemonic; |
109 | | - } |
110 | | -} |
| 1 | +using Chip8.Instructions; |
| 2 | +using Moq; |
| 3 | +using NUnit.Framework; |
| 4 | +using NUnit.Framework.Legacy; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | + |
| 9 | +namespace Chip8.Tests; |
| 10 | + |
| 11 | +public class CompilationAndDecompilationOfSampleApplications |
| 12 | +{ |
| 13 | + [Test] |
| 14 | + [Ignore("Executed manually")] |
| 15 | + public void LoadingThenRetrievingApplicationInEmulator_ReturnsSameByteArray() |
| 16 | + { |
| 17 | + var directory = new DirectoryInfo(@"..\..\..\..\..\games"); |
| 18 | + foreach (var file in Directory.EnumerateFiles(directory.FullName).Where(x => !x.EndsWith(".c8"))) |
| 19 | + { |
| 20 | + var originalGame = File.ReadAllBytes(file); |
| 21 | + |
| 22 | + var emulator = new Emulator(new Cpu(), new Mock<IDisplay>().Object, new Mock<IKeyboard>().Object, new Mock<ISound>().Object); |
| 23 | + emulator.LoadApplication(originalGame); |
| 24 | + |
| 25 | + var gameFromEmulator = emulator.GetApplication(); |
| 26 | + |
| 27 | + CollectionAssert.AreEqual(originalGame, gameFromEmulator, $"File: {file}"); |
| 28 | + //File.WriteAllBytes(file, gameFromEmulator); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + [Test] |
| 33 | + [Ignore("Executed manually")] |
| 34 | + public void DecompiledApplication_IsSameAs_AlreadyDecompiledApplication() |
| 35 | + { |
| 36 | + var directory = new DirectoryInfo(@"..\..\..\..\..\games"); |
| 37 | + foreach (var file in Directory.EnumerateFiles(directory.FullName).Where(x => !x.EndsWith(".c8"))) |
| 38 | + { |
| 39 | + var application = File.ReadAllBytes(file); |
| 40 | + var instructionDecoder = new InstructionDecoder(); |
| 41 | + |
| 42 | + var instructions = new List<string>(); |
| 43 | + for (int i = 0; i < application.Length - 1; i += 2) |
| 44 | + { |
| 45 | + var instruction = GetInstruction(application[i], application[i + 1], instructionDecoder); |
| 46 | + instructions.Add(instruction); |
| 47 | + } |
| 48 | + |
| 49 | + // if length of the application is not an even number, that means that the last instruction |
| 50 | + // was skipped (in the preceding for loop) because it did not start on an even address |
| 51 | + if (application.Length % 2 != 0) |
| 52 | + { |
| 53 | + var instruction = GetInstruction(application[application.Length - 1], 0x0, instructionDecoder); |
| 54 | + instructions.Add(instruction); |
| 55 | + } |
| 56 | + |
| 57 | + var codeFilePath = $"{file}.c8"; |
| 58 | + var alreadyDecompiledApplication = File.ReadAllLines(codeFilePath); |
| 59 | + CollectionAssert.AreEqual(alreadyDecompiledApplication, instructions, $"File: {codeFilePath}"); |
| 60 | + //File.WriteAllLines(codeFilePath, instructions); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + [Test] |
| 65 | + [Ignore("Executed manually")] |
| 66 | + public void CompiledApplication_IsSameAs_AlreadyCompiledApplication() |
| 67 | + { |
| 68 | + var directory = new DirectoryInfo(@"..\..\..\..\..\games"); |
| 69 | + foreach (var file in Directory.EnumerateFiles(directory.FullName).Where(x => x.EndsWith(".c8"))) |
| 70 | + { |
| 71 | + var decompiledApplication = File.ReadAllLines(file); |
| 72 | + var instructionEncoder = new InstructionEncoder(); |
| 73 | + |
| 74 | + // Convert all lines of code into CPU instructions |
| 75 | + var cpuInstructions = new List<CpuInstruction>(); |
| 76 | + for (int i = 0; i < decompiledApplication.Length; i++) |
| 77 | + { |
| 78 | + var cpuInstruction = instructionEncoder.GetCpuInstruction(decompiledApplication[i]); |
| 79 | + cpuInstructions.Add(cpuInstruction); |
| 80 | + } |
| 81 | + |
| 82 | + var application = new List<byte>(); |
| 83 | + foreach (var cpuInstruction in cpuInstructions) |
| 84 | + { |
| 85 | + // First add most significant byte |
| 86 | + application.Add((byte)((cpuInstruction.Decoded.InstructionCode & 0xFF00) >> 8)); |
| 87 | + |
| 88 | + // Then least significant byte |
| 89 | + application.Add(cpuInstruction.Decoded.kk); |
| 90 | + } |
| 91 | + |
| 92 | + if (application[application.Count - 1] == 0x0) |
| 93 | + { |
| 94 | + application.RemoveAt(application.Count - 1); |
| 95 | + } |
| 96 | + |
| 97 | + var binaryFilePath = file.Substring(0, file.Length - 3); |
| 98 | + var alreadyCompiledApplication = File.ReadAllBytes(binaryFilePath); |
| 99 | + CollectionAssert.AreEqual(alreadyCompiledApplication, application, $"File: {binaryFilePath}"); |
| 100 | + //File.WriteAllBytes(binaryFilePath, application.ToArray()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private string GetInstruction(byte msb, byte lsb, InstructionDecoder instructionDecoder) |
| 105 | + { |
| 106 | + var decodedInstruction = instructionDecoder.Decode(msb, lsb); |
| 107 | + var cpuInstruction = instructionDecoder.GetCpuInstruction(decodedInstruction); |
| 108 | + return cpuInstruction.Mnemonic; |
| 109 | + } |
| 110 | +} |
0 commit comments