Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Common settings
BasedOnStyle: WebKit
TabWidth: 4
IndentWidth: 4
UseTab: Always
ColumnLimit: 100

# Other languages JavaScript, Proto

---
Language: Cpp

# http://releases.llvm.org/6.0.1/tools/clang/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code
# int formatted_code;
# // clang-format off
# void unformatted_code ;
# // clang-format on
# void formatted_code_again;

DisableFormat: false
Standard: Cpp11

AccessModifierOffset: -4
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false

# Configure each individual brace in BraceWrapping
BreakBeforeBraces: Custom
# Control of individual brace wrapping cases
BraceWrapping: {
AfterClass: 'true'
AfterControlStatement: 'true'
AfterEnum : 'true'
AfterFunction : 'true'
AfterNamespace : 'true'
AfterStruct : 'true'
AfterUnion : 'true'
BeforeCatch : 'true'
BeforeElse : 'true'
IndentBraces : 'false'
AfterExternBlock : 'true'
SplitEmptyFunction : 'false'
SplitEmptyRecord : 'false'
SplitEmptyNamespace : 'true'
}

BreakAfterJavaFieldAnnotations: true
BreakBeforeInheritanceComma: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakStringLiterals: true

CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
SpaceBeforeCpp11BracedList: false
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IndentCaseLabels: false
FixNamespaceComments: true
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
JavaScriptQuotes: Double
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000

PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceAfterTemplateKeyword: true
SpaceBeforeInheritanceColon: true

SortUsingDeclarations: true
SortIncludes: true

# Comments are for developers, they should arrange them
ReflowComments: false

IncludeBlocks: Preserve
IndentPPDirectives: AfterHash
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*.o
*.exe

ztest.cpp
build/
28 changes: 28 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.15)
project(intel-x86-emulator LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

file(GLOB_RECURSE CPP_SOURCES "src/*.cpp")
file(GLOB_RECURSE HPP_SOURCES "src/*.hpp")

add_executable(intel-x86-emulator main.cpp ${CPP_SOURCES} ${HPP_SOURCES})

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()
include(GoogleTest)

add_executable(ie-test test/Test.cpp ${CPP_SOURCES} ${HPP_SOURCES})

target_link_libraries(ie-test GTest::gtest_main)
gtest_discover_tests(ie-test)
34 changes: 0 additions & 34 deletions input_reader.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions input_reader.hpp

This file was deleted.

19 changes: 0 additions & 19 deletions instr_utils.hpp

This file was deleted.

121 changes: 6 additions & 115 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,120 +1,11 @@
#include "input_reader.hpp"
#include "opcodes.hpp"
#include "operations/all_operations.hpp"
#include "register.hpp"
#include "memory.hpp"
#include "src/emulator.hpp"

#include <iostream>
#include <fstream>
#include <iostream>
#include <vector>

int main(int argc, char *argv[]) {
std::cout<<"----Emulation started----"<<"\n\n";
std::cout << std::hex;

InputReader ir = InputReader(argv[1]);
RegisterBank rb = RegisterBank();
Memory mem = Memory();

while (ir.fileRemaining()){

uint8_t opcode1 = ir.nextByte();

// Test for prefixes / escape opcodes

switch (opcode1){

case 0x0f: {

uint8_t opcode2 = ir.nextByte();

if (opcode2==0x3A || opcode2==0x38){
// No opcodes with three-byte encodings are implemented
std::cout << "Error: three-byte opcodes not implemented \n";
exit(0);
}
else {
// Check the opcode maps for two-byte opcodes with 0F byte

if (opcodes::bsf_ops.count(opcode2)) {
bsf(&ir, &rb, &mem, opcode2);
}

else if (opcodes::bsr_ops.count(opcode2)) {
bsr(&ir, &rb, &mem, opcode2);
}

else{
std::cout << "Error: Unsupported opcode! \n";
exit(0);
}
}
break;
}
case 0x66:
// No opcodes with 66 mandatory prefix have been implemented
std::cout << "Error: unsupported opcode prefix 66 \n";
exit(0);
break;

case 0xF2:
// No opcodes with F2 mandatory prefix have been implemented
std::cout << "Error: unsupported opcode prefix F2 \n";
exit(0);
break;

case 0xF3:
// No opcodes with F3 mandatory prefix have been implemented
std::cout << "Error: unsupported opcode prefix F3 \n";
exit(0);
break;

default:

if (opcodes::add_ops.count(opcode1)){
add(&ir, &rb, &mem, opcode1);
}

else if (opcodes::cmp_ops.count(opcode1)){
cmp(&ir, &rb, &mem, opcode1);
}

else if (opcodes::and_ops.count(opcode1)){
andOp(&ir, &rb, &mem, opcode1);
}

else if (opcodes::mov_ops.count(opcode1)){
mov(&ir, &rb, &mem, opcode1);
}

else if (opcodes::or_ops.count(opcode1)){
orOp(&ir, &rb, &mem, opcode1);
}

else if (opcodes::push_ops.count(opcode1)){
push(&ir, &rb, &mem, opcode1);
}

else if (opcodes::pop_ops.count(opcode1)){
pop(&ir, &rb, &mem, opcode1);
}

else if (opcodes::xor_ops.count(opcode1)){
xorOp(&ir, &rb, &mem, opcode1);
}

// Add other operations below after implementing

else {
std::cout << "Error: Unsupported opcode! \n";
exit(0);
}

}
std::cout << "--------------------------\n";
}

std::cout << "\n----Emulation complete----\n";
rb.dumpValues();
mem.dumpValues();
int main(int argc, char* argv[])
{
Emulator& emulator = Emulator::getInstance();
emulator.run(argv[1]);
}
Loading