diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..46229dd --- /dev/null +++ b/.clang-format @@ -0,0 +1,98 @@ +AccessModifierOffset: -2 +AlignAfterOpenBracket: DontAlign +AlignConsecutiveAssignments: Consecutive +AlignConsecutiveDeclarations: Consecutive +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortLoopsOnASingleLine: true +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: false +BinPackParameters: false +BraceWrapping: + AfterCaseLabel: false + AfterClass: true + AfterEnum: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: true + AfterUnion: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyNamespace: true + SplitEmptyRecord: true +BreakAfterJavaFieldAnnotations: true +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: true +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakConstructorInitializersBeforeComma: false +BreakStringLiterals: true +ColumnLimit: 0 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 2 +ContinuationIndentWidth: 2 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: true +FixNamespaceComments: true +ForEachMacros: +- foreach +- Q_FOREACH +- BOOST_FOREACH +IncludeCategories: +- Priority: 2 + Regex: ^"(llvm|llvm-c|clang|clang-c)/ +- Priority: 3 + Regex: ^(<|"(gtest|gmock|isl|json)/) +- Priority: 1 + Regex: .* +IncludeIsMainRegex: (Test)?$ +IndentCaseLabels: false +IndentWidth: 2 +IndentWrappedFunctionNames: true +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +Language: Cpp +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 2 +NamespaceIndentation: Inner +ObjCBlockIndentWidth: 7 +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: false +PointerAlignment: Right +ReflowComments: true +SortIncludes: CaseInsensitive +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 0 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: true +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: c++20 +TabWidth: 8 +UseTab: Never + diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..cc98825 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,7 @@ +--- +Checks: '*,-fuchsia-*,-google-*,-zircon-*,-abseil-*,-modernize-use-trailing-return-type,-llvm*,-altera*,-misc-non-private-member-variables-in-classes,-readability-else-after-return' +WarningsAsErrors: '' +HeaderFilterRegex: '' +FormatStyle: none + + diff --git a/.cmake-format.yaml b/.cmake-format.yaml new file mode 100644 index 0000000..03c1da7 --- /dev/null +++ b/.cmake-format.yaml @@ -0,0 +1,19 @@ +additional_commands: + foo: + flags: + - BAR + - BAZ + kwargs: + DEPENDS: '*' + HEADERS: '*' + SOURCES: '*' +bullet_char: '*' +dangle_parens: false +enum_char: . +line_ending: unix +line_width: 120 +max_pargs_hwrap: 3 +separate_ctrl_name_with_space: false +separate_fn_name_with_space: false +tab_size: 2 + diff --git a/CMakeLists.txt b/CMakeLists.txt index 0040ab1..bdaad0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,82 @@ -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.15) -set (CMAKE_CXX_STANDARD 14) +# Set the project name to your project name, my project isn't very descriptive +project(6502-c++ CXX) +include(cmake/StandardProjectSettings.cmake) +include(cmake/PreventInSourceBuilds.cmake) -project(x86-to-6502) +# Link this 'library' to set the c++ standard / compile-time options requested +add_library(project_options INTERFACE) +target_compile_features(project_options INTERFACE cxx_std_20) -if(CMAKE_COMPILER_IS_GNUCC) - add_definitions(-Wall -Wextra -Wconversion -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wcast-qual -Wunused -Woverloaded-virtual -pedantic -std=c++14) +if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + option(ENABLE_BUILD_WITH_TIME_TRACE "Enable -ftime-trace to generate time tracing .json files on clang" OFF) + if(ENABLE_BUILD_WITH_TIME_TRACE) + target_compile_options(project_options INTERFACE -ftime-trace) + endif() endif() -add_executable(x86-to-6502 src/main.cpp) +# Link this 'library' to use the warnings specified in CompilerWarnings.cmake +add_library(project_warnings INTERFACE) +# enable cache system +include(cmake/Cache.cmake) +# standard compiler warnings +include(cmake/CompilerWarnings.cmake) +set_project_warnings(project_warnings) + +# sanitizer options if supported by compiler +include(cmake/Sanitizers.cmake) +enable_sanitizers(project_options) + +# enable doxygen +include(cmake/Doxygen.cmake) +enable_doxygen() + +# allow for static analysis options +include(cmake/StaticAnalyzers.cmake) + +option(BUILD_SHARED_LIBS "Enable compilation of shared libraries" OFF) +option(ENABLE_TESTING "Enable Test Builds" ON) +option(ENABLE_FUZZING "Enable Fuzzing Builds" OFF) + +# Very basic PCH example +option(ENABLE_PCH "Enable Precompiled Headers" OFF) +if(ENABLE_PCH) + # This sets a global PCH parameter, each project will build its own PCH, which is a good idea if any #define's change + # + # consider breaking this out per project as necessary + target_precompile_headers( + project_options + INTERFACE + + + + ) +endif() + +# Set up some extra Conan dependencies based on our needs before loading Conan +set(CONAN_EXTRA_REQUIRES "") +set(CONAN_EXTRA_OPTIONS "") + +if(CPP_STARTER_USE_SDL) + set(CONAN_EXTRA_REQUIRES ${CONAN_EXTRA_REQUIRES} sdl2/2.0.10@bincrafters/stable) + # set(CONAN_EXTRA_OPTIONS ${CONAN_EXTRA_OPTIONS} sdl2:wayland=True) +endif() + +include(cmake/Conan.cmake) +run_conan() + +if(ENABLE_TESTING) + enable_testing() + message("Building Tests. Be sure to check out test/constexpr_tests for constexpr testing") + add_subdirectory(test) +endif() + +if(ENABLE_FUZZING) + message("Building Fuzz Tests, using fuzzing sanitizer https://www.llvm.org/docs/LibFuzzer.html") + add_subdirectory(fuzz_test) +endif() + +add_subdirectory(src) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fdddb29 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/README.md b/README.md new file mode 100644 index 0000000..f826489 --- /dev/null +++ b/README.md @@ -0,0 +1,467 @@ +# cpp_starter_project + +[![codecov](https://codecov.io/gh/lefticus/cpp_starter_project/branch/master/graph/badge.svg)](https://codecov.io/gh/lefticus/cpp_starter_project) + +[![Build Status](https://travis-ci.org/lefticus/cpp_starter_project.svg?branch=master)](https://travis-ci.org/lefticus/cpp_starter_project) + +[![Build status](https://ci.appveyor.com/api/projects/status/ro4lbfoa7n0sy74c/branch/master?svg=true)](https://ci.appveyor.com/project/lefticus/cpp-starter-project/branch/master) + +![CMake](https://github.com/lefticus/cpp_starter_project/workflows/CMake/badge.svg) + + +## Getting Started + +### Use the Github template +First, click the green `Use this template` button near the top of this page. +This will take you to Github's ['Generate Repository'](https://github.com/lefticus/cpp_starter_project/generate) page. +Fill in a repository name and short description, and click 'Create repository from template'. +This will allow you to create a new repository in your Github account, +prepopulated with the contents of this project. +Now you can clone the project locally and get to work! + + $ git clone https://github.com//.git + +### Remove frameworks you're not going to use +If you know you're not going to use one or more of the optional gui/graphics +frameworks (fltk, gtkmm, imgui, etc.), you can remove them with `git rm`: + + $ git rm -r src/ + +## Dependencies + +Note about install commands: +- for Windows, we use [choco](https://chocolatey.org/install). +- for MacOS, we use [brew](https://brew.sh/). +- In case of an error in cmake, make sure that the dependencies are on the PATH. + +### Necessary Dependencies +1. A C++ compiler that supports C++17. +See [cppreference.com](https://en.cppreference.com/w/cpp/compiler_support) +to see which features are supported by each compiler. +The following compilers should work: + + * [gcc 7+](https://gcc.gnu.org/) +
+ Install command + + - Debian/Ubuntu: + + sudo apt install build-essential + + - Windows: + + choco install mingw -y + + - MacOS: + + brew install gcc +
+ + * [clang 6+](https://clang.llvm.org/) +
+ Install command + + - Debian/Ubuntu: + + bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" + + - Windows: + + Visual Studio 2019 ships with LLVM (see the Visual Studio section). However, to install LLVM separately: + + choco install llvm -y + + llvm-utils for using external LLVM with Visual Studio generator: + + git clone https://github.com/zufuliu/llvm-utils.git + cd llvm-utils/VS2017 + .\install.bat + + - MacOS: + + brew install llvm +
+ + * [Visual Studio 2019 or higher](https://visualstudio.microsoft.com/) +
+ Install command + Environment setup + + On Windows, you need to install Visual Studio 2019 because of the SDK and libraries that ship with it. + + Visual Studio IDE - 2019 Community (installs Clang too): + + choco install -y visualstudio2019community --package-parameters "add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended --includeOptional --passive --locale en-US" + + Put MSVC compiler, Clang compiler, and vcvarsall.bat on the path: + + choco install vswhere -y + refreshenv + + # change to x86 for 32bit + $clpath = vswhere -products * -latest -prerelease -find **/Hostx64/x64/* + $clangpath = vswhere -products * -latest -prerelease -find **/Llvm/bin/* + $vcvarsallpath = vswhere -products * -latest -prerelease -find **/Auxiliary/Build/* + + $path = [System.Environment]::GetEnvironmentVariable("PATH", "User") + [Environment]::SetEnvironmentVariable("Path", $path + ";$clpath" + ";$clangpath" + ";$vcvarsallpath", "User") + refreshenv + +
+ + +2. [Conan](https://conan.io/) +
+ Install Command + + - Via pip - https://docs.conan.io/en/latest/installation.html#install-with-pip-recommended + + pip install --user conan + + - Windows: + + choco install conan -y + + - MacOS: + + brew install conan + +
+ +3. [CMake 3.15+](https://cmake.org/) +
+ Install Command + + - Debian/Ubuntu: + + sudo apt-get install cmake + + - Windows: + + choco install cmake -y + + - MacOS: + + brew install cmake + +
+ +### Optional Dependencies +#### C++ Tools + * [Doxygen](http://doxygen.nl/) +
+ Install Command + + - Debian/Ubuntu: + + sudo apt-get install doxygen + sudo apt-get install graphviz + + - Windows: + + choco install doxygen.install -y + choco install graphviz -y + + - MacOS: + + brew install doxygen + brew install graphviz + +
+ + + * [ccache](https://ccache.dev/) +
+ Install Command + + - Debian/Ubuntu: + + sudo apt-get install ccache + + - Windows: + + choco install ccache -y + + - MacOS: + + brew install ccache + +
+ + + * [Cppcheck](http://cppcheck.sourceforge.net/) +
+ Install Command + + - Debian/Ubuntu: + + sudo apt-get install cppcheck + + - Windows: + + choco install cppcheck -y + + - MacOS: + + brew install cppcheck + +
+ + + * [include-what-you-use](https://include-what-you-use.org/) +
+ Install Command + + Follow instructions here: + https://github.com/include-what-you-use/include-what-you-use#how-to-install +
+ +#### GUI libraries +This project can be made to work with several optional GUI frameworks. + +If desired, you should install the following optional dependencies as +directed by their documentation, linked here: + +- [FLTK](https://www.fltk.org/doc-1.4/index.html) +- [GTKMM](https://www.gtkmm.org/en/documentation.html) +- [QT](https://doc.qt.io/) + +The following dependencies can be downloaded automatically by CMake and Conan. +All you need to do to install them is to turn on a CMake flag during +configuration. +If you run into difficulty using them, please refer to their documentation, +linked here: + +- [NANA](http://nanapro.org/en-us/documentation/) +- [SDL](http://wiki.libsdl.org/FrontPage) +- [IMGUI](https://github.com/ocornut/imgui/tree/master/docs): + This framework depends on SFML, and if you are using Linux, you may need + to install several of SFML's dependencies using your package manager. See + [the SFML build tutorial](https://www.sfml-dev.org/tutorials/2.5/compile-with-cmake.php) + for specifics. + +## Build Instructions + +### Build directory +Make a build directory: +``` +mkdir build +``` +### Specify the compiler using environment variables + +By default (if you don't set environment variables `CC` and `CXX`), the system default compiler will be used. + +Conan and CMake use the environment variables CC and CXX to decide which compiler to use. So to avoid the conflict issues only specify the compilers using these variables. + +CMake will detect which compiler was used to build each of the Conan targets. If you build all of your Conan targets with one compiler, and then build your CMake targets with a different compiler, the project may fail to build. + +
+Commands for setting the compilers + +- Debian/Ubuntu/MacOS: + + Set your desired compiler (`clang`, `gcc`, etc): + + - Temporarily (only for the current shell) + + Run one of the followings in the terminal: + + - clang + + CC=clang CXX=clang++ + + - gcc + + CC=gcc CXX=g++ + + - Permanent: + + Open `~/.bashrc` using your text editor: + + gedit ~/.bashrc + + Add `CC` and `CXX` to point to the compilers: + + export CC=clang + export CXX=clang++ + + Save and close the file. + +- Windows: + + - Permanent: + + Run one of the followings in PowerShell: + + - Visual Studio generator and compiler (cl) + + [Environment]::SetEnvironmentVariable("CC", "cl.exe", "User") + [Environment]::SetEnvironmentVariable("CXX", "cl.exe", "User") + refreshenv + + Set the architecture using [vsvarsall](https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=vs-2019#vcvarsall-syntax): + + vsvarsall.bat x64 + + - clang + + [Environment]::SetEnvironmentVariable("CC", "clang.exe", "User") + [Environment]::SetEnvironmentVariable("CXX", "clang++.exe", "User") + refreshenv + + - gcc + + [Environment]::SetEnvironmentVariable("CC", "gcc.exe", "User") + [Environment]::SetEnvironmentVariable("CXX", "g++.exe", "User") + refreshenv + + + - Temporarily (only for the current shell): + + $Env:CC="clang.exe" + $Env:CXX="clang++.exe" + +
+ +### Configure your build + +To configure the project and write makefiles, you could use `cmake` with a bunch of command line options. +The easier option is to run cmake interactively: + +#### **Configure via cmake-gui**: + +1) Open cmake-gui from the project directory: +``` +cmake-gui . +``` +2) Set the build directory: + +![build_dir](https://user-images.githubusercontent.com/16418197/82524586-fa48e380-9af4-11ea-8514-4e18a063d8eb.jpg) + +3) Configure the generator: + +In cmake-gui, from the upper menu select `Tools/Configure`. + +**Warning**: if you have set `CC` and `CXX` always choose the `use default native compilers` option. This picks `CC` and `CXX`. Don't change the compiler at this stage! + +
+Windows - MinGW Makefiles + +Choose MinGW Makefiles as the generator: + +mingw + +
+ +
+Windows - Visual Studio generator and compiler + +You should have already set `C` and `CXX` to `cl.exe`. + +Choose "Visual Studio 16 2019" as the generator: + +default_vs + +
+ +
+ +Windows - Visual Studio generator and Clang Compiler + +You should have already set `C` and `CXX` to `clang.exe` and `clang++.exe`. + +Choose "Visual Studio 16 2019" as the generator. To tell Visual studio to use `clang-cl.exe`: +- If you use the LLVM that is shipped with Visual Studio: write `ClangCl` under "optional toolset to use". + +visual_studio + +- If you use an external LLVM: write [`LLVM_v142`](https://github.com/zufuliu/llvm-utils#llvm-for-visual-studio-2017-and-2019) + under "optional toolset to use". + +visual_studio + +
+
+ +4) Choose the Cmake options and then generate: + +![generate](https://user-images.githubusercontent.com/16418197/82781591-c97feb80-9e1f-11ea-86c8-f2748b96f516.png) + +#### **Configure via ccmake**: +with the Cmake Curses Dialog Command Line tool: + + ccmake -S . -B ./build + +Once `ccmake` has finished setting up, press 'c' to configure the project, +press 'g' to generate, and 'q' to quit. + +### Build +Once you have selected all the options you would like to use, you can build the +project (all targets): + + cmake --build ./build + +For Visual Studio, give the build configuration (Release, RelWithDeb, Debug, etc) like the following: + + cmake --build ./build -- /p:configuration=Release + +## Troubleshooting + +### Update Conan +Many problems that users have can be resolved by updating Conan, so if you are +having any trouble with this project, you should start by doing that. + +To update conan: + + $ pip install --user --upgrade conan + +You may need to use `pip3` instead of `pip` in this command, depending on your +platform. + +### Clear Conan cache +If you continue to have trouble with your Conan dependencies, you can try +clearing your Conan cache: + + $ conan remove -f '*' + +The next time you run `cmake` or `cmake --build`, your Conan dependencies will +be rebuilt. If you aren't using your system's default compiler, don't forget to +set the CC, CXX, CMAKE_C_COMPILER, and CMAKE_CXX_COMPILER variables, as +described in the 'Build using an alternate compiler' section above. + +### Identifying misconfiguration of Conan dependencies + +If you have a dependency 'A' that requires a specific version of another +dependency 'B', and your project is trying to use the wrong version of +dependency 'B', Conan will produce warnings about this configuration error +when you run CMake. These warnings can easily get lost between a couple +hundred or thousand lines of output, depending on the size of your project. + +If your project has a Conan configuration error, you can use `conan info` to +find it. `conan info` displays information about the dependency graph of your +project, with colorized output in some terminals. + + $ cd build + $ conan info . + +In my terminal, the first couple lines of `conan info`'s output show all of the +project's configuration warnings in a bright yellow font. + +For example, the package `spdlog/1.5.0` depends on the package `fmt/6.1.2`. +If you were to modify the file `cmake/Conan.cmake` so that it requires an +earlier version of `fmt`, such as `fmt/6.0.0`, and then run: + + $ conan remove -f '*' # clear Conan cache + $ rm -rf build # clear previous CMake build + $ mkdir build && cd build + $ cmake .. # rebuild Conan dependencies + $ conan info . + +...the first line of output would be a warning that `spdlog` needs a more recent +version of `fmt`. + +## Testing +See [Catch2 tutorial](https://github.com/catchorg/Catch2/blob/master/docs/tutorial.md) + +## Fuzz testing + +See [libFuzzer Tutorial](https://github.com/google/fuzzing/blob/master/tutorial/libFuzzerTutorial.md) diff --git a/cmake/Cache.cmake b/cmake/Cache.cmake new file mode 100644 index 0000000..31f5e7e --- /dev/null +++ b/cmake/Cache.cmake @@ -0,0 +1,29 @@ +option(ENABLE_CACHE "Enable cache if available" ON) +if(NOT ENABLE_CACHE) + return() +endif() + +set(CACHE_OPTION + "ccache" + CACHE STRING "Compiler cache to be used") +set(CACHE_OPTION_VALUES "ccache" "sccache") +set_property(CACHE CACHE_OPTION PROPERTY STRINGS ${CACHE_OPTION_VALUES}) +list( + FIND + CACHE_OPTION_VALUES + ${CACHE_OPTION} + CACHE_OPTION_INDEX) + +if(${CACHE_OPTION_INDEX} EQUAL -1) + message( + STATUS + "Using custom compiler cache system: '${CACHE_OPTION}', explicitly supported entries are ${CACHE_OPTION_VALUES}") +endif() + +find_program(CACHE_BINARY ${CACHE_OPTION}) +if(CACHE_BINARY) + message(STATUS "${CACHE_OPTION} found and enabled") + set(CMAKE_CXX_COMPILER_LAUNCHER ${CACHE_BINARY}) +else() + message(WARNING "${CACHE_OPTION} is enabled but was not found. Not using it") +endif() diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake new file mode 100644 index 0000000..8243154 --- /dev/null +++ b/cmake/CompilerWarnings.cmake @@ -0,0 +1,78 @@ +# from here: +# +# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md + +function(set_project_warnings project_name) + option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE) + + set(MSVC_WARNINGS + /W4 # Baseline reasonable warnings + /w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss of data + /w14254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data + /w14263 # 'function': member function does not override any base class virtual member function + /w14265 # 'classname': class has virtual functions, but destructor is not virtual instances of this class may not + # be destructed correctly + /w14287 # 'operator': unsigned/negative constant mismatch + /we4289 # nonstandard extension used: 'variable': loop control variable declared in the for-loop is used outside + # the for-loop scope + /w14296 # 'operator': expression is always 'boolean_value' + /w14311 # 'variable': pointer truncation from 'type1' to 'type2' + /w14545 # expression before comma evaluates to a function which is missing an argument list + /w14546 # function call before comma missing argument list + /w14547 # 'operator': operator before comma has no effect; expected operator with side-effect + /w14549 # 'operator': operator before comma has no effect; did you intend 'operator'? + /w14555 # expression has no effect; expected expression with side- effect + /w14619 # pragma warning: there is no warning number 'number' + /w14640 # Enable warning on thread un-safe static member initialization + /w14826 # Conversion from 'type1' to 'type_2' is sign-extended. This may cause unexpected runtime behavior. + /w14905 # wide string literal cast to 'LPSTR' + /w14906 # string literal cast to 'LPWSTR' + /w14928 # illegal copy-initialization; more than one user-defined conversion has been implicitly applied + /permissive- # standards conformance mode for MSVC compiler. + ) + + set(CLANG_WARNINGS + -Wall + -Wextra # reasonable and standard + -Wshadow # warn the user if a variable declaration shadows one from a parent context + -Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. This helps + # catch hard to track down memory errors + -Wold-style-cast # warn for c-style casts + -Wcast-align # warn for potential performance problem casts + -Wunused # warn on anything being unused + -Woverloaded-virtual # warn if you overload (not override) a virtual function + -Wpedantic # warn if non-standard C++ is used + -Wconversion # warn on type conversions that may lose data + -Wsign-conversion # warn on sign conversions + -Wnull-dereference # warn if a null dereference is detected + -Wdouble-promotion # warn if float is implicit promoted to double + -Wformat=2 # warn on security issues around functions that format output (ie printf) + ) + + if(WARNINGS_AS_ERRORS) + set(CLANG_WARNINGS ${CLANG_WARNINGS} -Werror) + set(MSVC_WARNINGS ${MSVC_WARNINGS} /WX) + endif() + + set(GCC_WARNINGS + ${CLANG_WARNINGS} + -Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist + -Wduplicated-cond # warn if if / else chain has duplicated conditions + -Wduplicated-branches # warn if if / else branches have duplicated code + -Wlogical-op # warn about logical operations being used where bitwise were probably wanted + -Wuseless-cast # warn if you perform a cast to the same type + ) + + if(MSVC) + set(PROJECT_WARNINGS ${MSVC_WARNINGS}) + elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + set(PROJECT_WARNINGS ${CLANG_WARNINGS}) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + set(PROJECT_WARNINGS ${GCC_WARNINGS}) + else() + message(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.") + endif() + + target_compile_options(${project_name} INTERFACE ${PROJECT_WARNINGS}) + +endfunction() diff --git a/cmake/Conan.cmake b/cmake/Conan.cmake new file mode 100644 index 0000000..d885b50 --- /dev/null +++ b/cmake/Conan.cmake @@ -0,0 +1,24 @@ +macro(run_conan) + # Download automatically, you can also just copy the conan.cmake file + if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake") + message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan") + file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.16.1/conan.cmake" "${CMAKE_BINARY_DIR}/conan.cmake") + endif() + + include(${CMAKE_BINARY_DIR}/conan.cmake) + + conan_cmake_run( + REQUIRES + ${CONAN_EXTRA_REQUIRES} + catch2/2.13.6 + docopt.cpp/0.6.3 + fmt/7.1.3 + spdlog/1.8.5 + ctre/3.3.4 + OPTIONS + ${CONAN_EXTRA_OPTIONS} + BASIC_SETUP + CMAKE_TARGETS # individual targets to link to + BUILD + missing) +endmacro() diff --git a/cmake/Doxygen.cmake b/cmake/Doxygen.cmake new file mode 100644 index 0000000..4dad807 --- /dev/null +++ b/cmake/Doxygen.cmake @@ -0,0 +1,11 @@ +function(enable_doxygen) + option(ENABLE_DOXYGEN "Enable doxygen doc builds of source" OFF) + if(ENABLE_DOXYGEN) + set(DOXYGEN_CALLER_GRAPH YES) + set(DOXYGEN_CALL_GRAPH YES) + set(DOXYGEN_EXTRACT_ALL YES) + find_package(Doxygen REQUIRED dot) + doxygen_add_docs(doxygen-docs ${PROJECT_SOURCE_DIR}) + + endif() +endfunction() diff --git a/cmake/PreventInSourceBuilds.cmake b/cmake/PreventInSourceBuilds.cmake new file mode 100644 index 0000000..57d9c59 --- /dev/null +++ b/cmake/PreventInSourceBuilds.cmake @@ -0,0 +1,18 @@ +# +# This function will prevent in-source builds +function(AssureOutOfSourceBuilds) + # make sure the user doesn't play dirty with symlinks + get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH) + get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH) + + # disallow in-source builds + if("${srcdir}" STREQUAL "${bindir}") + message("######################################################") + message("Warning: in-source builds are disabled") + message("Please create a separate build directory and run cmake from there") + message("######################################################") + message(FATAL_ERROR "Quitting configuration") + endif() +endfunction() + +assureoutofsourcebuilds() diff --git a/cmake/Sanitizers.cmake b/cmake/Sanitizers.cmake new file mode 100644 index 0000000..6c6ff8f --- /dev/null +++ b/cmake/Sanitizers.cmake @@ -0,0 +1,66 @@ +function(enable_sanitizers project_name) + + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + option(ENABLE_COVERAGE "Enable coverage reporting for gcc/clang" FALSE) + + if(ENABLE_COVERAGE) + target_compile_options(${project_name} INTERFACE --coverage -O0 -g) + target_link_libraries(${project_name} INTERFACE --coverage) + endif() + + set(SANITIZERS "") + + option(ENABLE_SANITIZER_ADDRESS "Enable address sanitizer" FALSE) + if(ENABLE_SANITIZER_ADDRESS) + list(APPEND SANITIZERS "address") + endif() + + option(ENABLE_SANITIZER_LEAK "Enable leak sanitizer" FALSE) + if(ENABLE_SANITIZER_LEAK) + list(APPEND SANITIZERS "leak") + endif() + + option(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "Enable undefined behavior sanitizer" FALSE) + if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR) + list(APPEND SANITIZERS "undefined") + endif() + + option(ENABLE_SANITIZER_THREAD "Enable thread sanitizer" FALSE) + if(ENABLE_SANITIZER_THREAD) + if("address" IN_LIST SANITIZERS OR "leak" IN_LIST SANITIZERS) + message(WARNING "Thread sanitizer does not work with Address and Leak sanitizer enabled") + else() + list(APPEND SANITIZERS "thread") + endif() + endif() + + option(ENABLE_SANITIZER_MEMORY "Enable memory sanitizer" FALSE) + if(ENABLE_SANITIZER_MEMORY AND CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + if("address" IN_LIST SANITIZERS + OR "thread" IN_LIST SANITIZERS + OR "leak" IN_LIST SANITIZERS) + message(WARNING "Memory sanitizer does not work with Address, Thread and Leak sanitizer enabled") + else() + list(APPEND SANITIZERS "memory") + endif() + endif() + + list( + JOIN + SANITIZERS + "," + LIST_OF_SANITIZERS) + + endif() + + if(LIST_OF_SANITIZERS) + if(NOT + "${LIST_OF_SANITIZERS}" + STREQUAL + "") + target_compile_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS}) + target_link_options(${project_name} INTERFACE -fsanitize=${LIST_OF_SANITIZERS}) + endif() + endif() + +endfunction() diff --git a/cmake/StandardProjectSettings.cmake b/cmake/StandardProjectSettings.cmake new file mode 100644 index 0000000..76418c1 --- /dev/null +++ b/cmake/StandardProjectSettings.cmake @@ -0,0 +1,42 @@ +# Set a default build type if none was specified +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + message(STATUS "Setting build type to 'RelWithDebInfo' as none was specified.") + set(CMAKE_BUILD_TYPE + RelWithDebInfo + CACHE STRING "Choose the type of build." FORCE) + # Set the possible values of build type for cmake-gui, ccmake + set_property( + CACHE CMAKE_BUILD_TYPE + PROPERTY STRINGS + "Debug" + "Release" + "MinSizeRel" + "RelWithDebInfo") +endif() + +# Generate compile_commands.json to make it easier to work with clang based tools +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +option(ENABLE_IPO "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" OFF) + +if(ENABLE_IPO) + include(CheckIPOSupported) + check_ipo_supported( + RESULT + result + OUTPUT + output) + if(result) + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) + else() + message(SEND_ERROR "IPO is not supported: ${output}") + endif() +endif() +if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + add_compile_options(-fcolor-diagnostics) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + add_compile_options(-fdiagnostics-color=always) +else() + message(STATUS "No colored compiler diagnostic set for '${CMAKE_CXX_COMPILER_ID}' compiler.") +endif() + diff --git a/cmake/StaticAnalyzers.cmake b/cmake/StaticAnalyzers.cmake new file mode 100644 index 0000000..4396444 --- /dev/null +++ b/cmake/StaticAnalyzers.cmake @@ -0,0 +1,37 @@ +option(ENABLE_CPPCHECK "Enable static analysis with cppcheck" OFF) +option(ENABLE_CLANG_TIDY "Enable static analysis with clang-tidy" OFF) +option(ENABLE_INCLUDE_WHAT_YOU_USE "Enable static analysis with include-what-you-use" OFF) + +if(ENABLE_CPPCHECK) + find_program(CPPCHECK cppcheck) + if(CPPCHECK) + set(CMAKE_CXX_CPPCHECK + ${CPPCHECK} + --suppress=missingInclude + --enable=all + --inline-suppr + --inconclusive + -i + ${CMAKE_SOURCE_DIR}/imgui/lib) + else() + message(SEND_ERROR "cppcheck requested but executable not found") + endif() +endif() + +if(ENABLE_CLANG_TIDY) + find_program(CLANGTIDY clang-tidy) + if(CLANGTIDY) + set(CMAKE_CXX_CLANG_TIDY ${CLANGTIDY} -extra-arg=-Wno-unknown-warning-option) + else() + message(SEND_ERROR "clang-tidy requested but executable not found") + endif() +endif() + +if(ENABLE_INCLUDE_WHAT_YOU_USE) + find_program(INCLUDE_WHAT_YOU_USE include-what-you-use) + if(INCLUDE_WHAT_YOU_USE) + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${INCLUDE_WHAT_YOU_USE}) + else() + message(SEND_ERROR "include-what-you-use requested but executable not found") + endif() +endif() diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..c8c0fcc --- /dev/null +++ b/compile.sh @@ -0,0 +1 @@ +avr-gcc $1 -Wall -Wextra -mtiny-stack -c -o- -S -O3 -I ~/avr-libstdcpp/include/ -std=c++20 | tee $1.asm | ./avr-to-6502 | tee $1.asm && xa -O PETSCREEN -M $1.prg diff --git a/examples/16bit_counter.cpp b/examples/16bit_counter.cpp new file mode 100644 index 0000000..162ec55 --- /dev/null +++ b/examples/16bit_counter.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include + +enum Colors : uint8_t { WHITE = 0x01 }; + +inline volatile uint8_t &memory_loc(const uint16_t loc) { + return *reinterpret_cast(loc); +} + +inline void poke(const uint16_t loc, const uint8_t value) { + memory_loc(loc) = value; +} + +inline std::uint8_t peek(const std::uint16_t loc) { return memory_loc(loc); } + +inline void decrement_border_color() { --memory_loc(0xd020); } + +inline void increment_border_color() { ++memory_loc(0xd020); } + +inline bool joystick_down() { + uint8_t joystick_state = memory_loc(0xDC00); + return (joystick_state & 2) == 0; +} + +void use_data(std::array &data); + +inline void puts(uint8_t x, uint8_t y, std::string_view str) { + const auto start = 0x400 + (y * 40 + x); + + std::memcpy(const_cast(&memory_loc(start)), str.data(), + str.size()); +} + +inline void putc(uint8_t x, uint8_t y, uint8_t c) { + const auto start = 0x400 + (y * 40 + x); + poke(start, c); +} + +inline void put_hex(uint8_t x, uint8_t y, uint8_t value) { + const auto put_nibble = [](auto x, auto y, uint8_t nibble) { + if (nibble <= 9) { + putc(x, y, nibble + 48); + } else { + putc(x, y, nibble - 9); + } + }; + + put_nibble(x + 1, y, 0xF & value); + put_nibble(x, y, 0xF & (value >> 4)); +} + +inline void put_hex(uint8_t x, uint8_t y, uint16_t value) { + put_hex(x+2,y, static_cast(0xFF & value)); + put_hex(x,y, static_cast(0xFF & (value >> 8))); +} + +struct Clock { + using milliseconds = std::chrono::duration; + + // return elapsed time since last restart + [[nodiscard]] milliseconds restart() { + // stop Timer A + poke(0xDC0E, 0b00000000); + + // last value + const auto previous_value = static_cast( + peek(0xDC04) | (static_cast(peek(0xDC05)) << 8)); + + // reset timer + poke(0xDC04, 0xFF); + poke(0xDC05, 0xFF); + + // restart timer A + poke(0xDC0E, 0b00010001); + + return milliseconds{0xFFFF - previous_value}; + } + + Clock() { [[maybe_unused]] const auto value = restart(); } +}; + +int main() { + + // static constexpr std::array data{0}; + // std::memcpy(const_cast(&memory_loc(0x400)), data.data(), + // data.size()); + + /* + puts(5, 5, "hello world"); + puts(10, 10, "hellooooo world"); +*/ + Clock game_clock{}; + + + std::uint16_t counter = 0; + + std::uint8_t y = 15; + while (true) { + const auto us_elapsed = game_clock.restart().count(); + + put_hex(5, y, us_elapsed); + put_hex(11, y, counter); + + if (y++ == 20) { + y = 15; + } + + ++counter; + increment_border_color(); + } + + /* + const auto background_color = [](Colors col) { + memory_loc(0xd021) = static_cast(col); + }; + + background_color(Colors::WHITE); + + while(true) { + if (joystick_down()) { + increment_border_color(); + } else { + decrement_border_color(); + } + } + */ +} diff --git a/examples/16bit_counter_with_map_and_strings.cpp b/examples/16bit_counter_with_map_and_strings.cpp new file mode 100644 index 0000000..634607f --- /dev/null +++ b/examples/16bit_counter_with_map_and_strings.cpp @@ -0,0 +1,214 @@ +#include +#include +#include +#include +#include + +enum Colors : uint8_t { WHITE = 0x01 }; + +static volatile uint8_t &memory_loc(const uint16_t loc) { + return *reinterpret_cast(loc); +} + +static void poke(const uint16_t loc, const uint8_t value) { + memory_loc(loc) = value; +} + +static std::uint8_t peek(const std::uint16_t loc) { return memory_loc(loc); } + +static void decrement_border_color() { --memory_loc(0xd020); } + +static void increment_border_color() { ++memory_loc(0xd020); } + +static bool joystick_down() { + uint8_t joystick_state = peek(0xDC00); + return (joystick_state & 2) == 0; +} + +void use_data(std::array &data); + +static void puts(uint8_t x, uint8_t y, std::string_view str) { + const auto start = 0x400 + (y * 40 + x); + + std::memcpy(const_cast(&memory_loc(start)), str.data(), + str.size()); +} + + +template +struct Graphic +{ + std::array data; + + static constexpr auto width() noexcept { + return Width; + } + + static constexpr auto height() noexcept { + return Height; + } + + constexpr Graphic() = default; + + constexpr Graphic(std::array data_) noexcept : data(data_) {} + constexpr Graphic(std::initializer_list data_) noexcept { + std::copy(begin(data_), end(data_), begin(data)); + } + + constexpr auto &operator()(const std::uint8_t x, const std::uint8_t y) noexcept { + return data[y * Width + x]; + } + + constexpr const auto &operator()(const std::uint8_t x, const std::uint8_t y) const noexcept { + return data[y * Width + x]; + } +}; + +static void putc(uint8_t x, uint8_t y, uint8_t c) { + const auto start = 0x400 + (y * 40 + x); + poke(start, c); +} + +static void put_hex(uint8_t x, uint8_t y, uint8_t value) { + const auto put_nibble = [](auto x, auto y, uint8_t nibble) { + if (nibble <= 9) { + putc(x, y, nibble + 48); + } else { + putc(x, y, nibble - 9); + } + }; + + put_nibble(x + 1, y, 0xF & value); + put_nibble(x, y, 0xF & (value >> 4)); +} + +static void put_hex(uint8_t x, uint8_t y, uint16_t value) { + put_hex(x+2,y, static_cast(0xFF & value)); + put_hex(x,y, static_cast(0xFF & (value >> 8))); +} + +static void put_graphic(uint8_t x, uint8_t y, const auto &graphic) +{ + for (uint8_t cur_y = 0; cur_y < graphic.height(); ++cur_y) { + for (uint8_t cur_x = 0; cur_x < graphic.width(); ++cur_x) { + putc(cur_x + x, cur_y + y, graphic(cur_x, cur_y)); + } + } +} + +struct Clock { + using milliseconds = std::chrono::duration; + + // return elapsed time since last restart + [[nodiscard]] milliseconds restart() { + // stop Timer A + poke(0xDC0E, 0b00000000); + + // last value + const auto previous_value = static_cast( + peek(0xDC04) | (static_cast(peek(0xDC05)) << 8)); + + // reset timer + poke(0xDC04, 0xFF); + poke(0xDC05, 0xFF); + + // restart timer A + poke(0xDC0E, 0b00010001); + + return milliseconds{0xFFFF - previous_value}; + } + + Clock() { [[maybe_unused]] const auto value = restart(); } +}; + +int main() { + + // static constexpr std::array data{0}; + // std::memcpy(const_cast(&memory_loc(0x400)), data.data(), + // data.size()); + + static constexpr auto pic = + Graphic<5,4>{ + 78,119,77,32,32, + 101,32,32,80,32, + 101,79,101,103,32, + 76,101,76,122,88 + }; + + static constexpr auto map1 = + Graphic<4, 2>{ + 1,0,1,0, + 1,1,1,1 + }; + + static constexpr auto map2 = + Graphic<6, 3>{ + 1,0,1,0,0,0, + 1,1,1,1,0,1, + 0,0,0,1,0,0 + }; + +/* + static constexpr auto map = + Graphic<10, 2>{ + 0,1,0,1,0,0,0,1,0,0, + 1,0,0,0,0,0,1,0,0,0, + }; +*/ + // put_graphic(10,10,pic); + + + const auto draw_map = [](const auto &map) { + for (std::uint8_t y=0; y < map.height(); ++y) { + for (std::uint8_t x = 0; x < map.width(); ++x) { + if (map(x, y) == 1) { + put_graphic(x*4, y*4, pic); + } + } + } + }; + + puts(5, 17, "timing history"); + puts(21, 17, "16bit counter"); + + +// draw_map(map1); + draw_map(map2); + + + Clock game_clock{}; + + + std::uint16_t counter = 0; + std::uint8_t y = 19; + + while (true) { + const auto us_elapsed = game_clock.restart().count(); + + put_hex(5, y, us_elapsed); + put_hex(21, y, counter); + + if (y++ == 24) { + y = 19; + } + + ++counter; + increment_border_color(); + } + + /* + const auto background_color = [](Colors col) { + memory_loc(0xd021) = static_cast(col); + }; + + background_color(Colors::WHITE); + + while(true) { + if (joystick_down()) { + increment_border_color(); + } else { + decrement_border_color(); + } + } + */ +} diff --git a/examples/test.cpp b/examples/test.cpp index 56d2785..af2685f 100644 --- a/examples/test.cpp +++ b/examples/test.cpp @@ -1,26 +1,26 @@ #include -enum class Colors : uint8_t +enum Colors : uint8_t { WHITE=0x01 }; -volatile uint8_t &memory_loc(const uint16_t loc) +static volatile uint8_t &memory_loc(const uint16_t loc) { return *reinterpret_cast(loc); } -void decrement_border_color() +static void decrement_border_color() { --memory_loc(0xd020); } -void increment_border_color() +static void increment_border_color() { ++memory_loc(0xd020); } -bool joystick_down() +static bool joystick_down() { uint8_t joystick_state = memory_loc(0xDC00); return (joystick_state & 0x2) == 0; @@ -36,7 +36,7 @@ int main() while(true) { if (joystick_down()) { - increment_border_color(); +// increment_border_color(); } else { decrement_border_color(); } diff --git a/examples/test2.cpp b/examples/test2.cpp index 2001fc1..744e29b 100644 --- a/examples/test2.cpp +++ b/examples/test2.cpp @@ -6,22 +6,22 @@ enum class Colors : uint8_t BLACK=0x00 }; -volatile uint8_t &memory_loc(const uint16_t loc) +inline volatile uint8_t &memory_loc(const uint16_t loc) { return *reinterpret_cast(loc); } -void decrement_border_color() +inline void decrement_border_color() { --memory_loc(0xd020); } -void increment_border_color() +inline void increment_border_color() { ++memory_loc(0xd020); } -bool joystick_down() +inline bool joystick_down() { uint8_t joystick_state = memory_loc(0xDC00); return (joystick_state & 0x2) == 0; diff --git a/include/6502.hpp b/include/6502.hpp new file mode 100644 index 0000000..cc8914e --- /dev/null +++ b/include/6502.hpp @@ -0,0 +1,256 @@ +#ifndef INC_6502_CPP_6502_HPP +#define INC_6502_CPP_6502_HPP + +#include "assembly.hpp" + +struct mos6502 : ASMLine +{ + enum class OpCode { + unknown, + + adc, + AND, + asl, + + bcc, + bcs, + beq, + bit, + bmi, + bne, + bpl, + + cpy, + cmp, + clc, + + dec, + + eor, + + inc, + + jmp, + jsr, + + lda, + ldx, + ldy, + lsr, + + ORA, + + pha, + php, + pla, + plp, + + rol, + ror, + rts, + + sbc, + sec, + sta, + stx, + sty, + + tax, + tay, + tsx, + txa, + txs, + tya, + }; + + static bool get_is_branch(const OpCode o) + { + switch (o) { + case OpCode::beq: + case OpCode::bne: + case OpCode::bmi: + case OpCode::bpl: + case OpCode::bcc: + case OpCode::bcs: + return true; + case OpCode::adc: + case OpCode::AND: + case OpCode::asl: + case OpCode::bit: + case OpCode::cpy: + case OpCode::cmp: + case OpCode::clc: + case OpCode::dec: + case OpCode::eor: + case OpCode::inc: + case OpCode::jmp: + case OpCode::jsr: + case OpCode::lda: + case OpCode::ldx: + case OpCode::ldy: + case OpCode::lsr: + case OpCode::ORA: + case OpCode::pha: + case OpCode::php: + case OpCode::pla: + case OpCode::plp: + case OpCode::rol: + case OpCode::ror: + case OpCode::rts: + case OpCode::sbc: + case OpCode::sec: + case OpCode::sta: + case OpCode::sty: + case OpCode::stx: + case OpCode::tax: + case OpCode::tay: + case OpCode::tsx: + case OpCode::txa: + case OpCode::txs: + case OpCode::tya: + + case OpCode::unknown: + break; + } + return false; + } + + static bool get_is_comparison(const OpCode o) + { + switch (o) { + case OpCode::bit: + case OpCode::cmp: + case OpCode::cpy: + return true; + case OpCode::adc: + case OpCode::AND: + case OpCode::asl: + case OpCode::beq: + case OpCode::bne: + case OpCode::bmi: + case OpCode::bpl: + case OpCode::bcc: + case OpCode::bcs: + case OpCode::clc: + case OpCode::dec: + case OpCode::eor: + case OpCode::inc: + case OpCode::jmp: + case OpCode::jsr: + case OpCode::lda: + case OpCode::ldx: + case OpCode::ldy: + case OpCode::lsr: + case OpCode::ORA: + case OpCode::pha: + case OpCode::php: + case OpCode::pla: + case OpCode::plp: + case OpCode::rol: + case OpCode::ror: + case OpCode::rts: + case OpCode::sbc: + case OpCode::sec: + case OpCode::sta: + case OpCode::stx: + case OpCode::sty: + case OpCode::tax: + case OpCode::tay: + case OpCode::tsx: + case OpCode::txa: + case OpCode::txs: + case OpCode::tya: + case OpCode::unknown: + break; + } + return false; + } + + + explicit mos6502(const OpCode o) + : ASMLine(Type::Instruction, std::string{ to_string(o) }), opcode(o), is_branch(get_is_branch(o)), is_comparison(get_is_comparison(o)) + { + } + + mos6502(const Type t, std::string s) + : ASMLine(t, std::move(s)) + { + } + + mos6502(const OpCode o, Operand t_o) + : ASMLine(Type::Instruction, std::string{ to_string(o) }), opcode(o), op(std::move(t_o)), is_branch(get_is_branch(o)), is_comparison(get_is_comparison(o)) + { + } + + constexpr static std::string_view to_string(const OpCode o) + { + switch (o) { + case OpCode::lda: return "lda"; + case OpCode::asl: return "asl"; + case OpCode::rol: return "rol"; + case OpCode::ldx: return "ldx"; + case OpCode::ldy: return "ldy"; + case OpCode::tay: return "tay"; + case OpCode::tya: return "tya"; + case OpCode::tax: return "tax"; + case OpCode::tsx: return "tsx"; + case OpCode::txa: return "txa"; + case OpCode::txs: return "txs"; + case OpCode::cpy: return "cpy"; + case OpCode::eor: return "eor"; + case OpCode::sta: return "sta"; + case OpCode::sty: return "sty"; + case OpCode::stx: return "stx"; + case OpCode::pha: return "pha"; + case OpCode::pla: return "pla"; + case OpCode::php: return "php"; + case OpCode::plp: return "plp"; + case OpCode::lsr: return "lsr"; + case OpCode::ror: return "ror"; + case OpCode::AND: return "and"; + case OpCode::inc: return "inc"; + case OpCode::dec: return "dec"; + case OpCode::ORA: return "ora"; + case OpCode::cmp: return "cmp"; + case OpCode::bne: return "bne"; + case OpCode::bmi: return "bmi"; + case OpCode::beq: return "beq"; + case OpCode::jmp: return "jmp"; + case OpCode::adc: return "adc"; + case OpCode::sbc: return "sbc"; + case OpCode::rts: return "rts"; + case OpCode::clc: return "clc"; + case OpCode::sec: return "sec"; + case OpCode::bit: return "bit"; + case OpCode::jsr: return "jsr"; + case OpCode::bpl: return "bpl"; + case OpCode::bcc: return "bcc"; + case OpCode::bcs: return "bcs"; + case OpCode::unknown: return ""; + } + + return ""; + } + + [[nodiscard]] std::string to_string() const + { + switch (type) { + case ASMLine::Type::Label: + return text;// + ':'; + case ASMLine::Type::Directive: + case ASMLine::Type::Instruction: { + return fmt::format("\t{} {:15}; {}", text, op.value, comment); + } + } + throw std::runtime_error("Unable to render: " + text); + } + + + OpCode opcode = OpCode::unknown; + Operand op; + std::string comment; + bool is_branch = false; + bool is_comparison = false; +}; + +#endif//INC_6502_CPP_6502_HPP diff --git a/include/assembly.hpp b/include/assembly.hpp new file mode 100644 index 0000000..3a5a107 --- /dev/null +++ b/include/assembly.hpp @@ -0,0 +1,51 @@ +#ifndef INC_6502_CPP_ASSEMBLY_HPP +#define INC_6502_CPP_ASSEMBLY_HPP + +struct Operand +{ + enum class Type { + empty, + literal, + reg /*ister*/ + }; + + Type type = Type::empty; + int reg_num = 0; + std::string value; + + Operand() = default; + + bool operator==(const Operand &other) const + { + return type == other.type && reg_num == other.reg_num && value == other.value; + } + + Operand(const Type t, std::string v) + : type(t), value(std::move(v)) + { + assert(type == Type::literal); + } + + Operand(const Type t, const int num) + : type(t), reg_num(num) + { + assert(type == Type::reg); + } +}; + + +struct ASMLine +{ + enum class Type { + Label, + Instruction, + Directive + }; + + ASMLine(Type t, std::string te) : type(t), text(std::move(te)) {} + + Type type; + std::string text; +}; + +#endif//INC_6502_CPP_ASSEMBLY_HPP diff --git a/include/optimizer.hpp b/include/optimizer.hpp new file mode 100644 index 0000000..74f836c --- /dev/null +++ b/include/optimizer.hpp @@ -0,0 +1,150 @@ +#ifndef INC_6502_CPP_OPTIMIZER_HPP +#define INC_6502_CPP_OPTIMIZER_HPP + +#include "6502.hpp" +#include + +bool optimize(std::vector &instructions) +{ + // return false; + + if (instructions.size() < 2) { + return false; + } + + const auto next_instruction = [&instructions](auto i) { + do { + ++i; + } while (i < instructions.size() && instructions[i].type == ASMLine::Type::Directive); + return i; + }; + + + // remove unused flag-fix-up blocks + // it might make sense in the future to only insert these if determined they are needed? + for (size_t op = 10; op < instructions.size(); ++op) { + if (instructions[op].opcode == mos6502::OpCode::lda) { + if (instructions[op - 1].text == "; END remove if next is lda") { + for (size_t inner_op = op - 1; inner_op > 1; --inner_op) { + instructions[inner_op] = mos6502(ASMLine::Type::Directive, + "; removed unused flag fix-up: " + instructions[inner_op].to_string()); + + if (instructions[inner_op].text.find("; BEGIN") != std::string::npos) { + return true; + } + } + } + } + } + + + // look for redundant load of lda after a tax + for (size_t op = 0; op < instructions.size() - 3; ++op) { + if (instructions[op].opcode == mos6502::OpCode::sta + && instructions[op + 1].opcode == mos6502::OpCode::tax + && instructions[op + 2].opcode == mos6502::OpCode::lda + && instructions[op].op.value == instructions[op + 2].op.value) { + instructions[op + 2] = mos6502(ASMLine::Type::Directive, "; removed redundant lda: " + instructions[op + 2].to_string()); + return true; + } + } + + // look for redundant stores to 0-page registers with sta + for (size_t op = 0; op < instructions.size(); ++op) { + // todo, make sure this is in the register map + if (instructions[op].opcode == mos6502::OpCode::sta + && instructions[op].op.value.size() == 3) { + for (size_t next_op = op + 1; next_op < instructions.size(); ++next_op) { + if (instructions[next_op].opcode != mos6502::OpCode::sta && instructions[next_op].op.value == instructions[op].op.value) { + // we just found a use of ourselves back, abort the search, there's probably something else going on + break; + } + if (instructions[next_op].opcode == mos6502::OpCode::lda && instructions[next_op].op.value != instructions[op].op.value) { + // someone just loaded lda with a different value, so we need to abort! + break; + } + + // abort at label + if (instructions[next_op].type == ASMLine::Type::Label) { + break; + } + + if (instructions[next_op].opcode == mos6502::OpCode::sta + && instructions[next_op].op.value == instructions[op].op.value) { + // looks like we found a redundant store, remove the first one + instructions[op] = mos6502(ASMLine::Type::Directive, + "; removed redundant sta: " + instructions[op].to_string()); + return true; + } + } + } + } + + for (size_t op = 0; op < instructions.size() - 1; ++op) { + // look for a transfer of Y -> A immediately followed by A -> Y + if (instructions[op].opcode == mos6502::OpCode::tya) { + next_instruction(op); + if (instructions[op].opcode == mos6502::OpCode::tay) { + instructions[op] = mos6502(ASMLine::Type::Directive, + "; removed redundant tay: " + instructions[op].to_string()); + return true; + } + } + } + + for (size_t op = 0; op < instructions.size() - 1; ++op) { + // look for a store A -> loc immediately followed by loc -> A + if (instructions[op].opcode == mos6502::OpCode::sta) { + const auto next = next_instruction(op); + if (instructions[next].opcode == mos6502::OpCode::lda + && instructions[next].op == instructions[op].op) { + instructions[next] = mos6502(ASMLine::Type::Directive, + "; removed redundant lda: " + instructions[next].to_string()); + return true; + } + } + } + + // todo: fix this ldy redundant move, right now it doesn't + // take into account if Y has been used + + /* + for (size_t op = 0; op < instructions.size() - 1; ++op) { + if (instructions[op].opcode == mos6502::OpCode::ldy && instructions[op].op.type == Operand::Type::literal) { + auto op2 = op + 1; + + while (op2 < instructions.size() && (instructions[op2].type != ASMLine::Type::Label)) { + // while inside this label + if (instructions[op2].opcode == mos6502::OpCode::ldy && instructions[op2].op.value == instructions[op].op.value) { + instructions[op2] = mos6502(ASMLine::Type::Directive, "; removed redundant ldy: " + instructions[op2].to_string()); + return true; + } + ++op2; + } + } + } + */ + + + for (size_t op = 0; op < instructions.size() - 1; ++op) { + if (instructions[op].opcode == mos6502::OpCode::lda + && instructions[op].op.type == Operand::Type::literal) { + const auto operand = instructions[op].op; + auto op2 = op + 1; + // look for multiple stores of the same value + while (op2 < instructions.size() && (instructions[op2].opcode == mos6502::OpCode::sta || instructions[op2].type == ASMLine::Type::Directive)) { + ++op2; + } + if (instructions[op2].opcode == mos6502::OpCode::lda + && operand == instructions[op2].op) { + instructions[op2] = mos6502(ASMLine::Type::Directive, + "; removed redundant lda: " + instructions[op2].to_string()); + return true; + } + } + } + + return false; +} + +#endif//INC_6502_CPP_OPTIMIZER_HPP diff --git a/include/personalities/c64.hpp b/include/personalities/c64.hpp new file mode 100644 index 0000000..2863579 --- /dev/null +++ b/include/personalities/c64.hpp @@ -0,0 +1,108 @@ +#ifndef INC_6502_C_C64_HPP +#define INC_6502_C_C64_HPP + +#include "../personality.hpp" + +struct C64 : Personality +{ + + void insert_autostart_sequence(std::vector &new_instructions) const override + { + constexpr static auto start_address = 0x0801; + + // first 2 bytes is the load address for a PRG file. + new_instructions.emplace_back(ASMLine::Type::Directive, ".word " + std::to_string(start_address)); + new_instructions.emplace_back(ASMLine::Type::Directive, "* = " + std::to_string(start_address)); + new_instructions.emplace_back(ASMLine::Type::Directive, "; jmp to start of program with BASIC"); + new_instructions.emplace_back(ASMLine::Type::Directive, ".byt $0B,$08,$0A,$00,$9E,$32,$30,$36,$31,$00,$00,$00"); + + // load start of stack space into stack address pointers + new_instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "#$FF")); + new_instructions.emplace_back(mos6502::OpCode::sta, Operand(Operand::Type::literal, std::string{stack_low_address()})); + new_instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "#$CF")); + new_instructions.emplace_back(mos6502::OpCode::sta, Operand(Operand::Type::literal, std::string{ stack_high_address() })); + } + + [[nodiscard]] std::string_view stack_low_address() const override + { + return "$02"; + } + [[nodiscard]] std::string_view stack_high_address() const override + { + return "$03"; + } + + [[nodiscard]] Operand get_register(const int reg_num) const override + { + switch (reg_num) { + // http://sta.c64.org/cbm64mem.html + case 0: + return Operand(Operand::Type::literal, "$a7");// bit buffer for rs232 + case 1: + return Operand(Operand::Type::literal, "$a8");// counter for rs232 + case 2: + return Operand(Operand::Type::literal, "$05");// unused, int->fp routine pointer + case 3: + return Operand(Operand::Type::literal, "$06"); + case 4: + return Operand(Operand::Type::literal, "$fb");// unused + case 5: + return Operand(Operand::Type::literal, "$fc");// unused + case 6: + return Operand(Operand::Type::literal, "$fd");// unused + case 7: + return Operand(Operand::Type::literal, "$fe");// unused + case 8: + return Operand(Operand::Type::literal, "$22");// unused + case 9: + return Operand(Operand::Type::literal, "$23");// unused + case 10: + return Operand(Operand::Type::literal, "$39");// Current BASIC line number + case 11: + return Operand(Operand::Type::literal, "$3a");// Current BASIC line number + case 12: + return Operand(Operand::Type::literal, "$61");// arithmetic register #1 + case 13: + return Operand(Operand::Type::literal, "$62"); + case 14: + return Operand(Operand::Type::literal, "$63"); + case 15: + return Operand(Operand::Type::literal, "$64"); + case 16: + return Operand(Operand::Type::literal, "$65"); + case 17: + return Operand(Operand::Type::literal, "$69");// arithmetic register #2 + case 18: + return Operand(Operand::Type::literal, "$6a"); + case 19: + return Operand(Operand::Type::literal, "$6b"); + case 20: + return Operand(Operand::Type::literal, "$6c"); + case 21: + return Operand(Operand::Type::literal, "$6d"); + case 22: + return Operand(Operand::Type::literal, "$57");// arithmetic register #3 + case 23: + return Operand(Operand::Type::literal, "$58"); + case 24: + return Operand(Operand::Type::literal, "$59"); + case 25: + return Operand(Operand::Type::literal, "$5a"); + case 26: + return Operand(Operand::Type::literal, "$5b"); + case 27: + return Operand(Operand::Type::literal, "$5c");// arithmetic register #4 + case 28: + return Operand(Operand::Type::literal, "$5d"); + case 29: + return Operand(Operand::Type::literal, "$5e"); + case 30: + return Operand(Operand::Type::literal, "$5f"); + case 31: + return Operand(Operand::Type::literal, "$60"); + } + throw std::runtime_error("Unhandled register number: " + std::to_string(reg_num)); + } +}; + +#endif//INC_6502_C_C64_HPP diff --git a/include/personality.hpp b/include/personality.hpp new file mode 100644 index 0000000..2586eb7 --- /dev/null +++ b/include/personality.hpp @@ -0,0 +1,25 @@ +#ifndef INC_6502_CPP_PERSONALITY_HPP +#define INC_6502_CPP_PERSONALITY_HPP + +#include +#include "6502.hpp" + +class Personality +{ +public: + virtual void insert_autostart_sequence(std::vector &new_instructions) const = 0; + [[nodiscard]] virtual Operand get_register(const int reg_num) const = 0; + [[nodiscard]] virtual std::string_view stack_low_address() const = 0; + [[nodiscard]] virtual std::string_view stack_high_address() const= 0; + + virtual ~Personality() = default; + Personality(const Personality &) = delete; + Personality(Personality &&) = delete; + Personality &operator=(const Personality &) = delete; + Personality &operator=(Personality &&) = delete; + +protected: + Personality() = default; +}; + +#endif//INC_6502_CPP_PERSONALITY_HPP diff --git a/src/6502-c++.cpp b/src/6502-c++.cpp new file mode 100644 index 0000000..f06546e --- /dev/null +++ b/src/6502-c++.cpp @@ -0,0 +1,1026 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../include/6502.hpp" +#include "../include/assembly.hpp" +#include "../include/optimizer.hpp" +#include "../include/personalities/c64.hpp" + + +int to_int(const std::string_view sv) +{ + int result{}; + std::from_chars(sv.begin(), sv.end(), result); + return result; +} + +int parse_8bit_literal(const std::string_view s) +{ + return to_int(s.substr(1)); +} + +std::string_view strip_lo_hi(std::string_view s) +{ + const auto matcher = ctre::match; + + if (const auto results = matcher(s); results) { + return results.get<2>(); + } + + return s; +} + +std::string_view strip_negate(std::string_view s) +{ + const auto matcher = ctre::match; + + if (const auto results = matcher(s); results) { + return results.get<1>(); + } + + return s; +} + + +std::string fixup_8bit_literal(const std::string &s) +{ + if (s[0] == '$') { + return "#" + std::to_string(static_cast(parse_8bit_literal(s))); + } + + if (s.starts_with("0x")) { + return "#$" + s.substr(2); + } + + if (s.starts_with("lo8(")) { + return fmt::format("#<{}", strip_lo_hi(s)); + } + if (s.starts_with("hi8(")) { + return fmt::format("#>{}", strip_lo_hi(s)); + } + + const auto is_num = std::all_of(begin(s), end(s), [](const auto c) { return (c >= '0' && c <= '9') || c == '-'; }); + + if (is_num) { + return "#<" + s; + } + + return s; +} + + +struct AVR : ASMLine +{ + enum class OpCode { + unknown, + + adc, + adiw, + add, + andi, + + breq, + brlo, + brne, + brsh, + + clr, + com, + cp, + cpc, + cpi, + cpse, + dec, + + eor, + + in, + + ld, + ldd, + ldi, + lds, + lsl, + lsr, + + mov, + + out, + + pop, + push, + + rcall, + ret, + rjmp, + rol, + + sbc, + sbci, + sbiw, + sbrc, + sbrs, + st, + std, + sts, + sub, + subi, + swap, + }; + + [[nodiscard]] static constexpr OpCode parse_opcode(Type t, std::string_view o) + { + switch (t) { + case Type::Label: + case Type::Directive: + return OpCode::unknown; + case Type::Instruction: { + if (o == "ldi") { return OpCode::ldi; } + if (o == "sts") { return OpCode::sts; } + if (o == "ret") { return OpCode::ret; } + if (o == "mov") { return OpCode::mov; } + if (o == "lsl") { return OpCode::lsl; } + if (o == "rol") { return OpCode::rol; } + if (o == "rcall") { return OpCode::rcall; } + if (o == "ld") { return OpCode::ld; } + if (o == "sub") { return OpCode::sub; } + if (o == "subi") { return OpCode::subi; } + if (o == "sbc") { return OpCode::sbc; } + if (o == "sbci") { return OpCode::sbci; } + if (o == "st") { return OpCode::st; } + if (o == "std") { return OpCode::std; } + if (o == "ldd") { return OpCode::ldd; } + if (o == "lds") { return OpCode::lds; } + if (o == "lsr") { return OpCode::lsr; } + if (o == "andi") { return OpCode::andi; } + if (o == "eor") { return OpCode::eor; } + if (o == "sbrc") { return OpCode::sbrc; } + if (o == "rjmp") { return OpCode::rjmp; } + if (o == "sbrs") { return OpCode::sbrs; } + if (o == "brne") { return OpCode::brne; } + if (o == "dec") { return OpCode::dec; } + if (o == "adiw") { return OpCode::adiw; } + if (o == "sbiw") { return OpCode::sbiw; } + if (o == "push") { return OpCode::push; } + if (o == "pop") { return OpCode::pop; } + if (o == "com") { return OpCode::com; } + if (o == "swap") { return OpCode::swap; } + if (o == "clr") { return OpCode::clr; } + if (o == "cpse") { return OpCode::cpse; } + if (o == "cpi") { return OpCode::cpi; } + if (o == "brlo") { return OpCode::brlo; } + if (o == "add") { return OpCode::add; } + if (o == "adc") { return OpCode::adc; } + if (o == "cpc") { return OpCode::cpc; } + if (o == "cp") { return OpCode::cp; } + if (o == "brsh") { return OpCode::brsh; } + if (o == "breq") { return OpCode::breq; } + if (o == "in") { return OpCode::in; } + if (o == "out") { return OpCode::out; } + } + } + throw std::runtime_error(fmt::format("Unknown opcode: {}", o)); + } + + static int get_register_number(const char reg_name) + { + if (reg_name == 'X') { + return 26; + } + if (reg_name == 'Y') { + return 28; + } + if (reg_name == 'Z') { + return 30; + } + + throw std::runtime_error("Unknown register name"); + } + + static Operand parse_operand(std::string_view o) + { + if (o.empty()) { + return Operand(); + } + + if (o[0] == 'r' && o.size() > 1) { + return Operand(Operand::Type::reg, to_int(o.substr(1))); + } else { + return Operand(Operand::Type::literal, std::string{ o }); + } + } + + AVR(const int t_line_num, std::string_view t_line_text, Type t, std::string_view t_opcode, std::string_view o1 = "", std::string_view o2 = "") + : ASMLine(t, std::string(t_opcode)), line_num(t_line_num), line_text(std::string(t_line_text)), + opcode(parse_opcode(t, t_opcode)), operand1(parse_operand(o1)), operand2(parse_operand(o2)) + { + } + + int line_num; + std::string line_text; + OpCode opcode; + Operand operand1; + Operand operand2; +}; + +void indirect_load(std::vector &instructions, const std::string &from_address_low_byte, const std::string &to_address, const int offset = 0) +{ + instructions.emplace_back(mos6502::OpCode::ldy, Operand(Operand::Type::literal, fmt::format("#{}", offset))); + instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "(" + from_address_low_byte + "), Y")); + instructions.emplace_back(mos6502::OpCode::sta, Operand(Operand::Type::literal, to_address)); +} + +void indirect_store(std::vector &instructions, const std::string &from_address, const std::string &to_address_low_byte, const int offset = 0) +{ + instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, from_address)); + instructions.emplace_back(mos6502::OpCode::ldy, Operand(Operand::Type::literal, fmt::format("#{}", offset))); + instructions.emplace_back(mos6502::OpCode::sta, Operand(Operand::Type::literal, "(" + to_address_low_byte + "), Y")); +} + +void fixup_16_bit_N_Z_flags(std::vector &instructions) +{ + + // need to get both Z and N set appropriately + // assuming A contains higher order byte and X contains lower order byte + instructions.emplace_back(ASMLine::Type::Directive, "; BEGIN remove if next is lda"); + instructions.emplace_back(ASMLine::Type::Directive, "; set CPU flags assuming A holds the higher order byte already"); + std::string set_flag_label = "flags_set_after_16_bit_op_" + std::to_string(instructions.size()); + // if high order is negative, we know it's not 0 and it is negative + instructions.emplace_back(mos6502::OpCode::bmi, Operand(Operand::Type::literal, set_flag_label)); + // if it's not 0, then branch down, we know the result is not 0 and not negative + instructions.emplace_back(mos6502::OpCode::bne, Operand(Operand::Type::literal, set_flag_label)); + // if the higher order byte is 0, test the lower order byte, which was saved for us in Y + instructions.emplace_back(mos6502::OpCode::txa); + // if low order is not negative, we know it's 0 or not 0 + instructions.emplace_back(mos6502::OpCode::bpl, Operand(Operand::Type::literal, set_flag_label)); + // if low order byte is negative, shift right by one bit, then we'll get the proper Z/N flags + instructions.emplace_back(mos6502::OpCode::lsr); + instructions.emplace_back(ASMLine::Type::Label, set_flag_label); + instructions.emplace_back(ASMLine::Type::Directive, "; END remove if next is lda"); +} + +void add_16_bit(const Personality &personality, std::vector &instructions, int reg, const std::uint16_t value) +{ + instructions.emplace_back(mos6502::OpCode::clc); + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(reg)); + instructions.emplace_back(mos6502::OpCode::adc, Operand(Operand::Type::literal, "#" + std::to_string((value & 0xFFu)))); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(reg)); + instructions.emplace_back(mos6502::OpCode::tax); + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(reg + 1)); + instructions.emplace_back(mos6502::OpCode::adc, Operand(Operand::Type::literal, "#" + std::to_string((value >> 8u) & 0xFFu))); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(reg + 1)); + + fixup_16_bit_N_Z_flags(instructions); +} + +void subtract_16_bit(const Personality &personality, std::vector &instructions, int reg, const std::uint16_t value) +{ + instructions.emplace_back(mos6502::OpCode::sec); + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(reg)); + instructions.emplace_back(mos6502::OpCode::sbc, Operand(Operand::Type::literal, "#" + std::to_string((value & 0xFFu)))); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(reg)); + instructions.emplace_back(mos6502::OpCode::tax); + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(reg + 1)); + instructions.emplace_back(mos6502::OpCode::sbc, Operand(Operand::Type::literal, "#" + std::to_string((value >> 8u) & 0xFFu))); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(reg + 1)); + + fixup_16_bit_N_Z_flags(instructions); +} + +void increment_16_bit(const Personality &personality, std::vector &instructions, int reg) +{ + //instructions.emplace_back(mos6502::OpCode::sta, Operand(Operand::Type::literal, address_low_byte)); + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(reg)); + instructions.emplace_back(mos6502::OpCode::clc); + instructions.emplace_back(mos6502::OpCode::adc, Operand(Operand::Type::literal, "#1")); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(reg)); + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(reg + 1)); + instructions.emplace_back(mos6502::OpCode::adc, Operand(Operand::Type::literal, "#0")); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(reg + 1)); +} + +void translate_instruction(const Personality &personality, std::vector &instructions, const AVR::OpCode op, const Operand &o1, const Operand &o2) +{ + const auto translate_register_number = [](const Operand ®) { + if (reg.value == "__zero_reg__") { + return 1; + } else if (reg.value == "__temp_reg__") { + return 0; + } else { + return reg.reg_num; + } + }; + + const auto o1_reg_num = translate_register_number(o1); + const auto o2_reg_num = translate_register_number(o2); + + switch (op) { + case AVR::OpCode::dec: + instructions.emplace_back(mos6502::OpCode::dec, personality.get_register(o1_reg_num)); + return; + case AVR::OpCode::ldi: + instructions.emplace_back(mos6502::OpCode::lda, Operand(o2.type, fixup_8bit_literal(o2.value))); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + case AVR::OpCode::sts: + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o2_reg_num)); + instructions.emplace_back(mos6502::OpCode::sta, Operand(o1.type, o1.value)); + return; + case AVR::OpCode::ret: + instructions.emplace_back(mos6502::OpCode::rts); + return; + case AVR::OpCode::mov: + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o2_reg_num)); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + case AVR::OpCode::lsl: + instructions.emplace_back(mos6502::OpCode::asl, personality.get_register(o1_reg_num)); + return; + case AVR::OpCode::rol: + instructions.emplace_back(mos6502::OpCode::rol, personality.get_register(o1_reg_num)); + return; + case AVR::OpCode::rcall: + if (o1.value != ".") { + instructions.emplace_back(mos6502::OpCode::jsr, o1); + } + + return; + case AVR::OpCode::ld: { + if (o2.value == "Z" || o2.value == "X" || o2.value == "Y") { + indirect_load(instructions, personality.get_register(AVR::get_register_number(o2.value[0])).value, personality.get_register(o1_reg_num).value); + return; + } + if (o2.value == "Z+" || o2.value == "X+" || o2.value == "Y+") { + indirect_load(instructions, personality.get_register(AVR::get_register_number(o2.value[0])).value, personality.get_register(o1_reg_num).value); + increment_16_bit(personality, instructions, AVR::get_register_number(o2.value[0])); + return; + } + throw std::runtime_error("Unknown ld indexing"); + } + case AVR::OpCode::ldd: { + if (o2.value[1] == '+') { + indirect_load(instructions, personality.get_register(AVR::get_register_number(o2.value[0])).value, personality.get_register(o1_reg_num).value, to_int(o2.value.substr(2))); + return; + } + + throw std::runtime_error("Unhandled 'ldd'"); + } + case AVR::OpCode::std: { + if (o1.value[1] == '+') { + indirect_store(instructions, personality.get_register(o2_reg_num).value, personality.get_register(AVR::get_register_number(o1.value[0])).value, to_int(o1.value.substr(2))); + return; + } + + throw std::runtime_error("Unhandled 'std'"); + } + case AVR::OpCode::sub: { + instructions.emplace_back(mos6502::OpCode::sec); + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::sbc, personality.get_register(o2_reg_num)); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + fixup_16_bit_N_Z_flags(instructions); + return; + } + case AVR::OpCode::sbc: { + // we want to utilize the carry flag, however it was set previously + // (it's really a borrow flag on the 6502) + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::sbc, personality.get_register(o2_reg_num)); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + fixup_16_bit_N_Z_flags(instructions); + return; + } + case AVR::OpCode::sbci: { + // we want to utilize the carry flag, however it was set previously + // (it's really a borrow flag on the 6502) + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::sbc, Operand(o2.type, fixup_8bit_literal(o2.value))); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + fixup_16_bit_N_Z_flags(instructions); + return; + } + case AVR::OpCode::subi: { + // to do: deal with Carry bit (and other flags) nonsense from AVR + // if |x| < |y| -> x-y +carry + // for these special cases with -(1) and -(-(1)) + if (o2.value == "lo8(-(-1))") { + instructions.emplace_back(mos6502::OpCode::dec, personality.get_register(o1_reg_num)); + return; + } + if (o2.value == "lo8(-(1))") { + instructions.emplace_back(mos6502::OpCode::inc, personality.get_register(o1_reg_num)); + return; + } + + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + // have to set carry flag, since it gets inverted by sbc + instructions.emplace_back(mos6502::OpCode::sec); + instructions.emplace_back(mos6502::OpCode::sbc, Operand(o2.type, fixup_8bit_literal(o2.value))); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + // temporarily store lower order (not carried substraction) byte into Y for checking + // later if this is a two byte subtraction operation + instructions.emplace_back(mos6502::OpCode::tax); + return; + } + case AVR::OpCode::st: { + if (o1.value == "Z" || o1.value == "Y" || o1.value == "X") { + indirect_store(instructions, personality.get_register(o2_reg_num).value, personality.get_register(AVR::get_register_number(o1.value[0])).value); + return; + } + if (o1.value == "Z+" || o1.value == "Y+" || o1.value == "X+") { + indirect_store(instructions, personality.get_register(o2_reg_num).value, personality.get_register(AVR::get_register_number(o1.value[0])).value); + increment_16_bit(personality, instructions, AVR::get_register_number(o1.value[0])); + return; + } + throw std::runtime_error("Unhandled st"); + } + case AVR::OpCode::lds: { + instructions.emplace_back(mos6502::OpCode::lda, o2); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + } + case AVR::OpCode::lsr: { + instructions.emplace_back(mos6502::OpCode::lsr, personality.get_register(o1_reg_num)); + return; + } + case AVR::OpCode::andi: { + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::AND, Operand(o2.type, fixup_8bit_literal(o2.value))); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + } + case AVR::OpCode::eor: { + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::eor, personality.get_register(o2_reg_num)); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + } + case AVR::OpCode::cpse: { + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::bit, personality.get_register(o2_reg_num)); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + instructions.emplace_back(mos6502::OpCode::beq, Operand(Operand::Type::literal, new_label_name)); + instructions.emplace_back(ASMLine::Type::Directive, new_label_name); + return; + } + case AVR::OpCode::sbrc: { + instructions.emplace_back(mos6502::OpCode::lda, Operand(o2.type, fixup_8bit_literal("$" + std::to_string(1 << (to_int(o2.value)))))); + instructions.emplace_back(mos6502::OpCode::bit, personality.get_register(o1_reg_num)); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + instructions.emplace_back(mos6502::OpCode::beq, Operand(Operand::Type::literal, new_label_name)); + instructions.emplace_back(ASMLine::Type::Directive, new_label_name); + return; + } + case AVR::OpCode::sbrs: { + instructions.emplace_back(mos6502::OpCode::lda, Operand(o2.type, fixup_8bit_literal("$" + std::to_string(1 << (to_int(o2.value.c_str())))))); + instructions.emplace_back(mos6502::OpCode::bit, personality.get_register(o1_reg_num)); + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + instructions.emplace_back(mos6502::OpCode::bne, Operand(Operand::Type::literal, new_label_name)); + instructions.emplace_back(ASMLine::Type::Directive, new_label_name); + return; + } + case AVR::OpCode::brne: { + if (o1.value == "0b") { + instructions.emplace_back(mos6502::OpCode::bne, Operand(Operand::Type::literal, "memcpy_0")); + return; + } else if (o1.value == ".+2") { + // assumes 6502 'borrow' for Carry flag instead of carry, so bcc instead of bcs + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + instructions.emplace_back(mos6502::OpCode::bne, Operand(Operand::Type::literal, new_label_name)); + instructions.emplace_back(ASMLine::Type::Directive, new_label_name); + return; + } else { + instructions.emplace_back(mos6502::OpCode::bne, o1); + return; + } + + throw std::runtime_error("Unhanled brne format"); + } + + case AVR::OpCode::rjmp: { + instructions.emplace_back(mos6502::OpCode::jmp, o1); + return; + } + + case AVR::OpCode::sbiw: { + subtract_16_bit(personality, instructions, o1_reg_num, static_cast(to_int(o2.value))); + return; + } + + case AVR::OpCode::adiw: { + add_16_bit(personality, instructions, o1_reg_num, static_cast(to_int(o2.value))); + return; + } + + case AVR::OpCode::push: { + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::pha); + return; + } + case AVR::OpCode::pop: { + instructions.emplace_back(mos6502::OpCode::pla); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + } + case AVR::OpCode::com: { + // We're doing this in the same way the AVR does it, to make sure the C flag is set properly + instructions.emplace_back(mos6502::OpCode::clc); + instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "#$FF")); + instructions.emplace_back(mos6502::OpCode::sbc, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + } + case AVR::OpCode::clr: { + instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "#$00")); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + } + case AVR::OpCode::cpi: { + // note that this will leave the C flag in the 6502 borrow state, not normal carry state + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::cmp, Operand(o2.type, fixup_8bit_literal(o2.value))); + return; + } + case AVR::OpCode::brlo: { + + if (o1.value == ".+2") { + // assumes 6502 'borrow' for Carry flag instead of carry, so bcc instead of bcs + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + instructions.emplace_back(mos6502::OpCode::bcc, Operand(Operand::Type::literal, new_label_name)); + instructions.emplace_back(ASMLine::Type::Directive, new_label_name); + return; + } else { + instructions.emplace_back(mos6502::OpCode::bcc, o1); + return; + } + } + case AVR::OpCode::swap: { + // from http://www.6502.org/source/general/SWN.html + // ASL A + // ADC #$80 + // ROL A + // ASL A + // ADC #$80 + // ROL A + + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::asl); + instructions.emplace_back(mos6502::OpCode::adc, Operand(Operand::Type::literal, "#$80")); + instructions.emplace_back(mos6502::OpCode::rol); + instructions.emplace_back(mos6502::OpCode::asl); + instructions.emplace_back(mos6502::OpCode::adc, Operand(Operand::Type::literal, "#$80")); + instructions.emplace_back(mos6502::OpCode::rol); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + + return; + } + case AVR::OpCode::add: { + instructions.emplace_back(mos6502::OpCode::clc); + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::adc, personality.get_register(o2_reg_num)); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + } + case AVR::OpCode::adc: { + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::adc, personality.get_register(o2_reg_num)); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + } + case AVR::OpCode::cp: { + // note that this will leave the C flag in the 6502 borrow state, not normal carry state + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::cmp, personality.get_register(o2_reg_num)); + return; + } + case AVR::OpCode::cpc: { + // this instruction seems to need to be used in the case after a sbc operation, where the + // carry flag is set to the appropriate borrow state, so I'm going to not invert + // the carry flag here, and assume that it's set how the 6502 wants it to be + // set from the previous operation + + instructions.emplace_back(mos6502::OpCode::lda, personality.get_register(o1_reg_num)); + instructions.emplace_back(mos6502::OpCode::sbc, personality.get_register(o2_reg_num)); + return; + } + + case AVR::OpCode::brsh: { + if (o1.value == ".+2") { + // assumes 6502 'borrow' for Carry flag instead of carry, so bcs instead of bcc + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + instructions.emplace_back(mos6502::OpCode::bcs, Operand(Operand::Type::literal, new_label_name)); + instructions.emplace_back(ASMLine::Type::Directive, new_label_name); + return; + } else { + instructions.emplace_back(mos6502::OpCode::bcs, o1); + return; + } + } + case AVR::OpCode::out: { + if (o1.value == "__SP_L__") { + instructions.emplace_back(mos6502::OpCode::ldx, personality.get_register(o2_reg_num)); + instructions.emplace_back(mos6502::OpCode::txs); + return; + } + + if (o1.value == "__SP_H__") { + // officially nothing to do - we cannot change the high byte of the SP on 6502 + return; + } + + throw std::runtime_error("Could not translate unknown 'out' instruction"); + } + + case AVR::OpCode::in: { + if (o2.value == "__SP_L__") { + instructions.emplace_back(mos6502::OpCode::tsx); + instructions.emplace_back(mos6502::OpCode::stx, personality.get_register(o1_reg_num)); + return; + } + + if (o2.value == "__SP_H__") { + instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "$01")); + instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(o1_reg_num)); + return; + } + + throw std::runtime_error("Could not translate unknown 'in' instruction"); + } + + case AVR::OpCode::breq: { + if (o1.value == ".+2") + { + // assumes 6502 'borrow' for Carry flag instead of carry, so bcc instead of bcs + std::string new_label_name = "skip_next_instruction_" + std::to_string(instructions.size()); + instructions.emplace_back(mos6502::OpCode::beq, Operand(Operand::Type::literal, new_label_name)); + instructions.emplace_back(ASMLine::Type::Directive, new_label_name); + return; + } else { + instructions.emplace_back(mos6502::OpCode::beq, o1); + return; + } + } + case AVR::OpCode::unknown: { + throw std::runtime_error("Could not translate 'unknown' instruction"); + } + } + + throw std::runtime_error("Could not translate unhandled instruction"); +} + +void to_mos6502(const Personality &personality, const AVR &from_instruction, std::vector &instructions) +{ + try { + switch (from_instruction.type) { + case ASMLine::Type::Label: + if (from_instruction.text == "0") { + instructions.emplace_back(from_instruction.type, "-memcpy_0"); + } else { + instructions.emplace_back(from_instruction.type, from_instruction.text); + } + return; + case ASMLine::Type::Directive: + if (from_instruction.text.starts_with(".string")) { + instructions.emplace_back(ASMLine::Type::Directive, ".asc " + from_instruction.text.substr(7)); + } else if (from_instruction.text.starts_with(".byte")) { + instructions.emplace_back(ASMLine::Type::Directive, ".byt " + from_instruction.text.substr(5)); + } else if (from_instruction.text.starts_with(".zero")) { + const auto count = std::stoull(&*std::next(from_instruction.text.begin(), 6), nullptr, 10); + + std::string zeros; + for (std::size_t i = 0; i < count; ++i) { + if ((i % 40) == 0) { + if (!zeros.empty()) { + instructions.emplace_back(ASMLine::Type::Directive, zeros); + zeros.clear(); + } + zeros += ".byt 0"; + } else { + zeros += ",0"; + } + } + + if (!zeros.empty()) { + instructions.emplace_back(ASMLine::Type::Directive, zeros); + } + } else if (from_instruction.text[0] == ';') { + // it's a comment + instructions.emplace_back(ASMLine::Type::Directive, from_instruction.text); + } else { + instructions.emplace_back(ASMLine::Type::Directive, "; Unknown directive: " + from_instruction.text); + } + return; + case ASMLine::Type::Instruction: + const auto head = instructions.size(); + + try { + translate_instruction(personality, instructions, from_instruction.opcode, from_instruction.operand1, from_instruction.operand2); + } catch (const std::exception &e) { + instructions.emplace_back(ASMLine::Type::Directive, "; Unhandled opcode: '" + from_instruction.text + "' " + e.what()); + spdlog::error("[{}]: Unhandled instruction: '{}': {}", from_instruction.line_num, from_instruction.line_text, e.what()); + } + + auto text = from_instruction.line_text; + if (text[0] == '\t') { + text.erase(0, 1); + } + for_each(std::next(instructions.begin(), static_cast(head)), instructions.end(), [text](auto &ins) { + ins.comment = text; + }); + return; + } + } catch (const std::exception &e) { + spdlog::error("[{}]: Unhandled instruction: '{}': {}", from_instruction.line_num, from_instruction.line_text, e.what()); + } +} + + +bool fix_long_branches(std::vector &instructions, int &branch_patch_count) +{ + std::map labels; + for (size_t op = 0; op < instructions.size(); ++op) { + if (instructions[op].type == ASMLine::Type::Label) { + labels[instructions[op].text] = op; + } + } + + for (size_t op = 0; op < instructions.size(); ++op) { + if (instructions[op].is_branch && std::abs(static_cast(labels[instructions[op].op.value]) - static_cast(op)) * 3 > 255) { + ++branch_patch_count; + const auto going_to = instructions[op].op.value; + const auto new_pos = "patch_" + std::to_string(branch_patch_count); + // uh-oh too long of a branch, have to convert this to a jump... + + std::map branch_mapping; + + branch_mapping[mos6502::OpCode::bne] = mos6502::OpCode::beq; + branch_mapping[mos6502::OpCode::beq] = mos6502::OpCode::bne; + branch_mapping[mos6502::OpCode::bcc] = mos6502::OpCode::bcs; + branch_mapping[mos6502::OpCode::bcs] = mos6502::OpCode::bcc; + + const auto mapping = branch_mapping.find(instructions[op].opcode); + + if (mapping != branch_mapping.end()) { + const auto comment = instructions[op].comment; + instructions[op] = mos6502(mapping->second, Operand(Operand::Type::literal, new_pos)); + instructions.insert(std::next(std::begin(instructions), static_cast(op + 1)), mos6502(mos6502::OpCode::jmp, Operand(Operand::Type::literal, going_to))); + instructions.insert(std::next(std::begin(instructions), static_cast(op + 2)), mos6502(ASMLine::Type::Label, new_pos)); + instructions[op].comment = instructions[op + 1].comment = instructions[op + 2].comment = comment; + return true; + } + + throw std::runtime_error("Don't know how to reorg this branch: " + instructions[op].to_string()); + } + } + return false; +} + + +bool fix_overwritten_flags(std::vector &instructions) +{ + if (instructions.size() < 3) { + return false; + } + + for (size_t op = 0; op < instructions.size(); ++op) { + if (instructions[op].is_comparison) { + auto op2 = op + 1; + while (op2 < instructions.size() + && !instructions[op2].is_comparison + && !instructions[op2].is_branch) { + ++op2; + } + + if (op2 < instructions.size() + && (op2 - op) > 1 + && instructions[op2 - 1].opcode != mos6502::OpCode::plp) { + if (instructions[op2].is_comparison) { + continue; + } + + if (instructions[op2].is_branch) { + // insert a pull of processor status before the branch + instructions.insert(std::next(std::begin(instructions), static_cast(op2)), mos6502(mos6502::OpCode::plp)); + // insert a push of processor status after the comparison + instructions.insert(std::next(std::begin(instructions), static_cast(op + 1)), mos6502(mos6502::OpCode::php)); + + return true; + } + } + } + } + + return false; +} + +void run(const Personality &personality, std::istream &input) +{ + std::regex Comment(R"(\s*(\#|;)(.*))"); + std::regex Label(R"(^\s*(\S+):.*)"); + std::regex Directive(R"(^\s*(\..+))"); + std::regex UnaryInstruction(R"(^\s+(\S+)\s+(\S+).*)"); + std::regex BinaryInstruction(R"(^\s+(\S+)\s+(\S+),\s*(\S+).*)"); + std::regex Instruction(R"(^\s+(\S+).*)"); + + std::size_t lineno = 0; + + + std::vector instructions; + + while (input.good()) { + std::string line; + getline(input, line); + try { + std::smatch match; + if (std::regex_match(line, match, Label)) { + instructions.emplace_back(lineno, line, ASMLine::Type::Label, match[1].str()); + } else if (std::regex_match(line, match, Comment)) { + // save comments! + instructions.emplace_back(lineno, line, ASMLine::Type::Directive, "; " + match[2].str()); + } else if (std::regex_match(line, match, Directive)) { + instructions.emplace_back(lineno, line, ASMLine::Type::Directive, match[1].str()); + } else if (std::regex_match(line, match, BinaryInstruction)) { + instructions.emplace_back(lineno, line, ASMLine::Type::Instruction, match[1].str(), match[2].str(), match[3].str()); + } else if (std::regex_match(line, match, UnaryInstruction)) { + instructions.emplace_back(lineno, line, ASMLine::Type::Instruction, match[1].str(), match[2].str()); + } else if (std::regex_match(line, match, Instruction)) { + instructions.emplace_back(lineno, line, ASMLine::Type::Instruction, match[1].str()); + } else if (line.empty()) { + // skip empty lines + } + } catch (const std::exception &e) { + spdlog::error("[{}]: parse exception with '{}': {}", lineno, line, e.what()); + } + + ++lineno; + } + + std::set labels; + + for (const auto &i : instructions) { + if (i.type == ASMLine::Type::Label) { + labels.insert(i.text); + } + } + + std::set used_labels{ "main" }; + + for (const auto &i : instructions) { + if (i.type == ASMLine::Type::Instruction) { + + const auto check_label = [&](const std::string &value) { + if (labels.count(value) != 0) { + used_labels.insert(value); + } + }; + + check_label(i.operand1.value); + check_label(i.operand2.value); + check_label(std::string{ strip_negate(strip_lo_hi(i.operand1.value)) }); + check_label(std::string{ strip_negate(strip_lo_hi(i.operand2.value)) }); + } + } + + const auto new_labels = + [&used_labels]() { + std::map result; + for (const auto &l : used_labels) { +// auto newl = l; +// std::transform(newl.begin(), newl.end(), newl.begin(), [](const auto c) { return std::tolower(c); }); +// newl.erase(std::remove_if(newl.begin(), newl.end(), [](const auto c) { return !std::isalnum(c); }), std::end(newl)); + + // strip just first \. + if (l[0] == '.') { + result.emplace(std::make_pair(l, l.substr(1))); + } else { + result.emplace(std::make_pair(l, l)); + } + } + return result; + }(); + + for (const auto &label : new_labels) { + spdlog::trace("used label: '{}':'{}'", label.first, label.second); + } + + for (auto &i : instructions) { + if (i.type == ASMLine::Type::Label) { + if (i.text == "0") { + i.text = "-memcpy_0"; + } else { + try { + i.text = new_labels.at(i.text); + } catch (...) { + spdlog::error("Error looking up label name: '{}'", i.text); + throw; + } + } + } + + if (i.operand2.value.starts_with("lo8(") || i.operand2.value.starts_with("hi8(")) { + const auto potential_label = strip_lo_hi(i.operand2.value); + const auto itr1 = new_labels.find(std::string{ potential_label }); + if (itr1 != new_labels.end()) { + i.operand2.value.replace(4, potential_label.size(), itr1->second); + } + } + + const auto itr1 = new_labels.find(i.operand1.value); + if (itr1 != new_labels.end()) { + i.operand1.value = itr1->second; + } + + const auto itr2 = new_labels.find(i.operand2.value); + if (itr2 != new_labels.end()) { + i.operand2.value = itr2->second; + } + } + + + std::vector new_instructions; + + personality.insert_autostart_sequence(new_instructions); + // set __zero_reg__ (reg 1 on AVR) to 0 + new_instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "#$00")); + new_instructions.emplace_back(mos6502::OpCode::sta, personality.get_register(1)); + new_instructions.emplace_back(mos6502::OpCode::jmp, Operand(Operand::Type::literal, "main")); + + + int instructions_to_skip = 0; + std::string next_label_name; + for (const auto &i : instructions) { + to_mos6502(personality, i, new_instructions); + + // intentionally copy so we don't invalidate the reference + const auto last_instruction = new_instructions.back(); + const auto last_instruction_loc = new_instructions.size() - 1; + + if (instructions_to_skip == 1) { + new_instructions.emplace_back(ASMLine::Type::Label, next_label_name); + } + + --instructions_to_skip; + + if (last_instruction.type == ASMLine::Type::Directive && last_instruction.text.starts_with("skip_next_instruction")) { + instructions_to_skip = 1; + next_label_name = last_instruction.text; + new_instructions.erase(std::next(new_instructions.begin(), static_cast(last_instruction_loc))); + } + if (last_instruction.type == ASMLine::Type::Directive && last_instruction.text.starts_with("skip_next_2_instructions")) { + instructions_to_skip = 2; + next_label_name = last_instruction.text; + new_instructions.erase(std::next(new_instructions.begin(), static_cast(last_instruction_loc))); + } + } + + // it seems that with the move to AVR for the base, this + // fixup no longer makes sense, but I'm not going to remove it just yet + // until we have more complex C++ examples working +// while (fix_overwritten_flags(new_instructions)) { + // do it however many times it takes +// } + + while (optimize(new_instructions)) { + // do it however many times it takes + } + + int branch_patch_count = 0; + while (fix_long_branches(new_instructions, branch_patch_count)) { + // do it however many times it takes + } + + + for (const auto &i : new_instructions) { + std::cout << i.to_string() << '\n'; + } +} + +int main(const int argc, const char **argv) +{ + std::ifstream input_file; + + std::istream &input = [&]() -> std::istream & { + if (argc > 1) { + input_file.open(argv[1]); + return input_file; + } else { + return std::cin; + } + }(); + + C64 personality; + + std::cout << "; AVR Mode\n"; + run(personality, input); +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..c0103c0 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,18 @@ +option(CPP_STARTER_USE_SDL "Enable compilation of SDL sample" OFF) + +# sdl +if(CPP_STARTER_USE_SDL) + message("Using SDL2") + add_subdirectory(sdl) +endif() + +# Generic test that uses conan libs +add_executable(6502-c++ 6502-c++.cpp ../include/optimizer.hpp ../include/assembly.hpp ../include/6502.hpp ../include/personality.hpp ../include/personalities/c64.hpp) +target_link_libraries( + 6502-c++ + PRIVATE project_options + project_warnings + CONAN_PKG::ctre + CONAN_PKG::docopt.cpp + CONAN_PKG::fmt + CONAN_PKG::spdlog) diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 718e323..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,1096 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -struct ASMLine -{ - enum class Type - { - Label, - Instruction, - Directive - }; - - ASMLine(Type t, std::string te) : type(t), text(std::move(te)) {} - - Type type; - std::string text; -}; - -int parse_8bit_literal(const std::string &s) -{ - return std::stoi(std::string(std::next(std::begin(s)), std::end(s))); -} - -std::string fixup_8bit_literal(const std::string &s) -{ - if (s[0] == '$') - { - return "#" + std::to_string(static_cast(parse_8bit_literal(s))); - } else { - return s; - } -} - -struct Operand -{ - enum class Type - { - empty, - literal, - reg /*ister*/ - }; - - Type type = Type::empty; - int reg_num = 0; - std::string value; - - Operand() = default; - - bool operator==(const Operand &other) const { - return type == other.type && reg_num == other.reg_num && value == other.value; - } - - Operand(const Type t, std::string v) - : type(t), value(std::move(v)) - { - assert(type == Type::literal); - } - - Operand(const Type t, const int num) - : type(t), reg_num(num) - { - assert(type == Type::reg); - } -}; - -Operand get_register(const int reg_num, const int offset = 0) { - switch (reg_num) { - // http://sta.c64.org/cbm64mem.html - case 0x00: return Operand(Operand::Type::literal, "$03"); // unused, fp->int routine pointer - case 0x01: return Operand(Operand::Type::literal, "$04"); - case 0x02: return Operand(Operand::Type::literal, "$05"); // unused, int->fp routine pointer - case 0x03: return Operand(Operand::Type::literal, "$06"); - case 0x04: return Operand(Operand::Type::literal, "$fb"); // unused - case 0x05: return Operand(Operand::Type::literal, "$fc"); // unused - case 0x06: return Operand(Operand::Type::literal, "$fd"); // unused - case 0x07: return Operand(Operand::Type::literal, "$fe"); // unused - case 0x08: return Operand(Operand::Type::literal, "$22"); // unused - case 0x09: return Operand(Operand::Type::literal, "$23"); // unused - case 0x0A: return Operand(Operand::Type::literal, "$39"); // Current BASIC line number - case 0x0B: return Operand(Operand::Type::literal, "$3a"); // Current BASIC line number - case 0x10: return get_register(0x00 + offset); - case 0x11: return get_register(0x02 + offset); - case 0x12: return get_register(0x04 + offset); - case 0x13: return get_register(0x06 + offset); - case 0x14: return get_register(0x08 + offset); - case 0x15: return get_register(0x0A + offset); - }; - throw std::runtime_error("Unhandled register number: " + std::to_string(reg_num)); -} - -struct mos6502 : ASMLine -{ - enum class OpCode - { - unknown, - lda, - ldy, - tay, - tya, - cpy, - eor, - sta, - sty, - pha, - pla, - php, - plp, - lsr, - ror, - AND, - inc, - dec, - ORA, - cmp, - bne, - beq, - bmi, - jmp, - adc, - sbc, - rts, - clc, - sec, - bit - }; - - static bool get_is_branch(const OpCode o) { - switch (o) { - case OpCode::beq: - case OpCode::bne: - case OpCode::bmi: - return true; - case OpCode::lda: - case OpCode::ldy: - case OpCode::tay: - case OpCode::tya: - case OpCode::cpy: - case OpCode::eor: - case OpCode::sta: - case OpCode::sty: - case OpCode::pha: - case OpCode::pla: - case OpCode::php: - case OpCode::plp: - case OpCode::lsr: - case OpCode::ror: - case OpCode::AND: - case OpCode::inc: - case OpCode::dec: - case OpCode::ORA: - case OpCode::cmp: - case OpCode::jmp: - case OpCode::adc: - case OpCode::sbc: - case OpCode::rts: - case OpCode::clc: - case OpCode::sec: - case OpCode::bit: - case OpCode::unknown: - break; - } - return false; - } - - static bool get_is_comparison(const OpCode o) { - switch (o) { - case OpCode::cmp: - case OpCode::cpy: - case OpCode::bit: - return true; - case OpCode::lda: - case OpCode::ldy: - case OpCode::tay: - case OpCode::tya: - case OpCode::eor: - case OpCode::sta: - case OpCode::sty: - case OpCode::pha: - case OpCode::pla: - case OpCode::php: - case OpCode::plp: - case OpCode::lsr: - case OpCode::ror: - case OpCode::AND: - case OpCode::inc: - case OpCode::dec: - case OpCode::ORA: - case OpCode::jmp: - case OpCode::bne: - case OpCode::bmi: - case OpCode::beq: - case OpCode::adc: - case OpCode::sbc: - case OpCode::rts: - case OpCode::clc: - case OpCode::sec: - case OpCode::unknown: - break; - } - return false; - } - - - explicit mos6502(const OpCode o) - : ASMLine(Type::Instruction, to_string(o)), opcode(o), is_branch(get_is_branch(o)), is_comparison(get_is_comparison(o)) - { - } - - mos6502(const Type t, std::string s) - : ASMLine(t, std::move(s)) - { - } - - mos6502(const OpCode o, Operand t_o) - : ASMLine(Type::Instruction, to_string(o)), opcode(o), op(std::move(t_o)), is_branch(get_is_branch(o)), is_comparison(get_is_comparison(o)) - { - } - - static std::string to_string(const OpCode o) - { - switch (o) { - case OpCode::lda: - return "lda"; - case OpCode::ldy: - return "ldy"; - case OpCode::tay: - return "tay"; - case OpCode::tya: - return "tya"; - case OpCode::cpy: - return "cpy"; - case OpCode::eor: - return "eor"; - case OpCode::sta: - return "sta"; - case OpCode::sty: - return "sty"; - case OpCode::pha: - return "pha"; - case OpCode::pla: - return "pla"; - case OpCode::php: - return "php"; - case OpCode::plp: - return "plp"; - case OpCode::lsr: - return "lsr"; - case OpCode::ror: - return "ror"; - case OpCode::AND: - return "and"; - case OpCode::inc: - return "inc"; - case OpCode::dec: - return "dec"; - case OpCode::ORA: - return "ora"; - case OpCode::cmp: - return "cmp"; - case OpCode::bne: - return "bne"; - case OpCode::bmi: - return "bmi"; - case OpCode::beq: - return "beq"; - case OpCode::jmp: - return "jmp"; - case OpCode::adc: - return "adc"; - case OpCode::sbc: - return "sbc"; - case OpCode::rts: - return "rts"; - case OpCode::clc: - return "clc"; - case OpCode::sec: - return "sec"; - case OpCode::bit: - return "bit"; - case OpCode::unknown: - return ""; - }; - - return ""; - } - - std::string to_string() const - { - switch (type) { - case ASMLine::Type::Label: - return text; // + ':'; - case ASMLine::Type::Directive: - case ASMLine::Type::Instruction: - { - const std::string line = '\t' + text + ' ' + op.value; - return line + std::string(static_cast(std::max(15 - static_cast(line.size()), 1)), ' ') + "; " + comment; - } - }; - throw std::runtime_error("Unable to render: " + text); - } - - - OpCode opcode = OpCode::unknown; - Operand op; - std::string comment; - bool is_branch = false; - bool is_comparison = false; -}; - - - -struct i386 : ASMLine -{ - enum class OpCode - { - unknown, - movzbl, - movzwl, - shrb, - shrl, - xorl, - andl, - andb, - addb, - ret, - movb, - cmpb, - movl, - jmp, - jne, - je, - js, - testb, - incl, - incb, - decl, - decb, - sarl, - addl, - subl, - subb, - sall, - orl, - orb, - rep, - pushl, - sbbb, - negb, - notb, - retl - }; - - static OpCode parse_opcode(Type t, const std::string &o) - { - switch(t) - { - case Type::Label: - return OpCode::unknown; - case Type::Directive: - return OpCode::unknown; - case Type::Instruction: - { - if (o == "movzwl") return OpCode::movzwl; - if (o == "movzbl") return OpCode::movzbl; - if (o == "shrb") return OpCode::shrb; - if (o == "shrl") return OpCode::shrl; - if (o == "xorl") return OpCode::xorl; - if (o == "andl") return OpCode::andl; - if (o == "ret") return OpCode::ret; - if (o == "movb") return OpCode::movb; - if (o == "cmpb") return OpCode::cmpb; - if (o == "movl") return OpCode::movl; - if (o == "jmp") return OpCode::jmp; - if (o == "testb") return OpCode::testb; - if (o == "incl") return OpCode::incl; - if (o == "sarl") return OpCode::sarl; - if (o == "decl") return OpCode::decl; - if (o == "jne") return OpCode::jne; - if (o == "je") return OpCode::je; - if (o == "js") return OpCode::js; - if (o == "subl") return OpCode::subl; - if (o == "subb") return OpCode::subb; - if (o == "addl") return OpCode::addl; - if (o == "addb") return OpCode::addb; - if (o == "sall") return OpCode::sall; - if (o == "orl") return OpCode::orl; - if (o == "andb") return OpCode::andb; - if (o == "orb") return OpCode::orb; - if (o == "decb") return OpCode::decb; - if (o == "incb") return OpCode::incb; - if (o == "rep") return OpCode::rep; - if (o == "notb") return OpCode::notb; - if (o == "negb") return OpCode::negb; - if (o == "sbbb") return OpCode::sbbb; - if (o == "pushl") return OpCode::pushl; - if (o == "retl") return OpCode::retl; - } - } - throw std::runtime_error("Unknown opcode: " + o); - } - - static Operand parse_operand(std::string o) - { - if (o.empty()) { - return Operand(); - } - - if (o[0] == '%') { - if (o == "%al") { - return Operand(Operand::Type::reg, 0x00); - } else if (o == "%ah") { - return Operand(Operand::Type::reg, 0x01); - } else if (o == "%bl") { - return Operand(Operand::Type::reg, 0x02); - } else if (o == "%bh") { - return Operand(Operand::Type::reg, 0x03); - } else if (o == "%cl") { - return Operand(Operand::Type::reg, 0x04); - } else if (o == "%ch") { - return Operand(Operand::Type::reg, 0x05); - } else if (o == "%dl") { - return Operand(Operand::Type::reg, 0x06); - } else if (o == "%dh") { - return Operand(Operand::Type::reg, 0x07); - } else if (o == "%sil") { - return Operand(Operand::Type::reg, 0x08); - } else if (o == "%dil") { - return Operand(Operand::Type::reg, 0x0A); - } else if (o == "%ax" || o == "%eax") { - return Operand(Operand::Type::reg, 0x10); - } else if (o == "%bx" || o == "%ebx") { - return Operand(Operand::Type::reg, 0x11); - } else if (o == "%cx" || o == "%ecx") { - return Operand(Operand::Type::reg, 0x12); - } else if (o == "%dx" || o == "%edx") { - return Operand(Operand::Type::reg, 0x13); - } else if (o == "%si" || o == "%esi") { - return Operand(Operand::Type::reg, 0x14); - } else if (o == "%di" || o == "%edi") { - return Operand(Operand::Type::reg, 0x15); - } else { - throw std::runtime_error("Unknown register operand: '" + o + "'"); - } - } else { - return Operand(Operand::Type::literal, std::move(o)); - } - } - - i386(const int t_line_num, std::string t_line_text, Type t, std::string t_opcode, std::string o1="", std::string o2="") - : ASMLine(t, t_opcode), line_num(t_line_num), line_text(std::move(t_line_text)), - opcode(parse_opcode(t, t_opcode)), operand1(parse_operand(o1)), operand2(parse_operand(o2)) - { - } - - int line_num; - std::string line_text; - OpCode opcode; - Operand operand1; - Operand operand2; -}; - -void translate_instruction(std::vector &instructions, const i386::OpCode op, const Operand &o1, const Operand &o2) -{ - switch(op) - { - case i386::OpCode::ret: - instructions.emplace_back(mos6502::OpCode::rts); - break; - case i386::OpCode::retl: - /// \todo I don't know if this is completely correct for retl translation - instructions.emplace_back(mos6502::OpCode::rts); - break; - case i386::OpCode::movl: - if (o1.type == Operand::Type::reg && o2.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num, 1)); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num, 1)); - } else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, "#<" + o1.value)); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); - instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, "#>" + o1.value)); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num, 1)); - } else { - throw std::runtime_error("Cannot translate movl instruction"); - } - break; - case i386::OpCode::xorl: - if (o1.type == Operand::Type::reg && o2.type == Operand::Type::reg - && o1.reg_num == o2.reg_num) { - instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "#$00")); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num, 1)); - } else { - throw std::runtime_error("Cannot translate xorl instruction"); - } - break; - case i386::OpCode::movb: - if (o1.type == Operand::Type::literal && o2.type == Operand::Type::literal) { - instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::sta, o2); - } else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); - } else if (o1.type == Operand::Type::reg && o2.type == Operand::Type::literal) { - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::sta, o2); - } else if (o1.type == Operand::Type::reg && o2.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); - } else { - throw std::runtime_error("Cannot translate movb instruction"); - } - break; - case i386::OpCode::orb: - if (o1.type == Operand::Type::literal && o2.type == Operand::Type::literal) { - instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::ORA, o2); - instructions.emplace_back(mos6502::OpCode::sta, o2); - } else if (o1.type == Operand::Type::reg && o2.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::ORA, get_register(o2.reg_num)); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); - } else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::ORA, get_register(o2.reg_num)); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); - } else { - throw std::runtime_error("Cannot translate orb instruction"); - } - break; - - case i386::OpCode::movzbl: - if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, o1); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); - } else { - throw std::runtime_error("Cannot translate movzbl instruction"); - } - break; - case i386::OpCode::shrb: - if (o1.type == Operand::Type::reg || o2.type == Operand::Type::reg) { - const auto do_shift = [&instructions](const int reg_num) { - instructions.emplace_back(mos6502::OpCode::lsr, get_register(reg_num)); - }; - - if (o1.type == Operand::Type::literal) { - const auto count = parse_8bit_literal(o1.value); - for (int i = 0; i < count; ++i) { - do_shift(o2.reg_num); - } - } else { - do_shift(o1.reg_num); - } - } else { - throw std::runtime_error("Cannot translate shrb instruction"); - } - break; - case i386::OpCode::shrl: - if (o1.type == Operand::Type::reg || o2.type == Operand::Type::reg) { - const auto do_shift = [&instructions](const int reg_num) { - instructions.emplace_back(mos6502::OpCode::lsr, get_register(reg_num, 1)); - instructions.emplace_back(mos6502::OpCode::ror, get_register(reg_num)); - }; - - if (o1.type == Operand::Type::literal) { - const auto count = parse_8bit_literal(o1.value); - for (int i = 0; i < count; ++i) { - do_shift(o2.reg_num); - } - } else { - do_shift(o1.reg_num); - } - } else { - throw std::runtime_error("Cannot translate shrl instruction"); - } - break; - case i386::OpCode::testb: - if (o1.type == Operand::Type::reg && o2.type == Operand::Type::reg && o1.reg_num == o2.reg_num) { - // this just tests the register for 0 - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); -// instructions.emplace_back(mos6502::OpCode::bit, Operand(Operand::Type::literal, "#$00")); - } else if (o1.type == Operand::Type::reg && o2.type == Operand::Type::reg) { - // ands the values - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::bit, get_register(o2.reg_num)); - } else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg) { - // ands the values - instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::bit, get_register(o2.reg_num)); - } else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::literal) { - // ands the values - instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::bit, o2); - } else { - throw std::runtime_error("Cannot translate testb instruction"); - } - break; - case i386::OpCode::decb: - if (o1.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::dec, get_register(o1.reg_num)); - } else { - instructions.emplace_back(mos6502::OpCode::dec, o1); - } - break; - case i386::OpCode::incb: - if (o1.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::inc, get_register(o1.reg_num)); - } else { - instructions.emplace_back(mos6502::OpCode::inc, o1); - } - break; - case i386::OpCode::jne: - instructions.emplace_back(mos6502::OpCode::bne, o1); - break; - case i386::OpCode::je: - instructions.emplace_back(mos6502::OpCode::beq, o1); - break; - case i386::OpCode::js: - instructions.emplace_back(mos6502::OpCode::bmi, o1); - break; - case i386::OpCode::jmp: - instructions.emplace_back(mos6502::OpCode::jmp, o1); - break; - case i386::OpCode::addb: - if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, get_register(o2.reg_num)); - instructions.emplace_back(mos6502::OpCode::clc); - instructions.emplace_back(mos6502::OpCode::adc, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); - } else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::literal) { - instructions.emplace_back(mos6502::OpCode::lda, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::clc); - instructions.emplace_back(mos6502::OpCode::adc, o2); - instructions.emplace_back(mos6502::OpCode::sta, o2); - } else if (o1.type == Operand::Type::reg && o2.type == Operand::Type::literal) { - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::clc); - instructions.emplace_back(mos6502::OpCode::adc, o2); - instructions.emplace_back(mos6502::OpCode::sta, o2); - } else { - throw std::runtime_error("Cannot translate addb instruction"); - } - break; - case i386::OpCode::cmpb: - if (o1.type == Operand::Type::literal && o2.type == Operand::Type::literal) { - instructions.emplace_back(mos6502::OpCode::lda, o2); - instructions.emplace_back(mos6502::OpCode::cmp, Operand(o1.type, fixup_8bit_literal(o1.value))); - } else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, get_register(o2.reg_num)); - instructions.emplace_back(mos6502::OpCode::cmp, Operand(o1.type, fixup_8bit_literal(o1.value))); - } else { - throw std::runtime_error("Cannot translate cmpb instruction"); - } - break; - case i386::OpCode::andb: - if (o1.type == Operand::Type::literal && o2.type == Operand::Type::reg) { - const auto reg = get_register(o2.reg_num); - instructions.emplace_back(mos6502::OpCode::lda, reg); - instructions.emplace_back(mos6502::OpCode::AND, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::sta, reg); - } else if (o1.type == Operand::Type::literal && o2.type == Operand::Type::literal) { - const auto reg = get_register(o2.reg_num); - instructions.emplace_back(mos6502::OpCode::lda, o2); - instructions.emplace_back(mos6502::OpCode::AND, Operand(o1.type, fixup_8bit_literal(o1.value))); - instructions.emplace_back(mos6502::OpCode::sta, o2); - } else { - throw std::runtime_error("Cannot translate andb instruction"); - } - break; - case i386::OpCode::negb: - if (o1.type == Operand::Type::reg) { - // perform a two's complement of the register location - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::eor, Operand(Operand::Type::literal, "#$ff")); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::inc, get_register(o1.reg_num)); - } else { - throw std::runtime_error("Cannot translate negb instruction"); - } - break; - case i386::OpCode::notb: - if (o1.type == Operand::Type::reg) { - // exclusive or against 0xff to perform a logical not - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::eor, Operand(Operand::Type::literal, "#$ff")); - instructions.emplace_back(mos6502::OpCode::sta, get_register(o1.reg_num)); - } else { - throw std::runtime_error("Cannot translate notb instruction"); - } - break; - case i386::OpCode::subb: - // DEST <- DEST - SRC - // o2 = o2 - o1 - // Ensure that we set the carry flag before performing the subtraction - if (o1.type == Operand::Type::reg && o2.type == Operand::Type::literal) { - instructions.emplace_back(mos6502::OpCode::lda, o2); - instructions.emplace_back(mos6502::OpCode::sec); - instructions.emplace_back(mos6502::OpCode::sbc, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::sta, o2); - } else { - throw std::runtime_error("Cannot translate subb instruction"); - } - break; - case i386::OpCode::pushl: - if (o1.type == Operand::Type::reg) { - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num)); - instructions.emplace_back(mos6502::OpCode::pha); - instructions.emplace_back(mos6502::OpCode::lda, get_register(o1.reg_num, 1)); - instructions.emplace_back(mos6502::OpCode::pha); - } else { - throw std::runtime_error("Cannot translate pushl instruction"); - } - break; - - case i386::OpCode::sbbb: - // DEST <- (DEST – (SRC + CF)) - // o2 <- (o2 - (o1 + cf)) - // if o1 and o2 are the same we get - // o2 <- (o2 - (o2 + cf)) - // o2 <- -cf - if (o1.type == Operand::Type::reg && o2.type == Operand::Type::reg - && o1.reg_num == o2.reg_num) { - instructions.emplace_back(mos6502::OpCode::lda, Operand(Operand::Type::literal, "#$00")); // reset a - instructions.emplace_back(mos6502::OpCode::sbc, Operand(Operand::Type::literal, "#$00")); // subtract out the carry flag - instructions.emplace_back(mos6502::OpCode::eor, Operand(Operand::Type::literal, "#$ff")); // invert the bits - instructions.emplace_back(mos6502::OpCode::sta, get_register(o2.reg_num)); // place the value - } else { - throw std::runtime_error("Cannot translate sbbb instruction"); - } - break; - - default: - throw std::runtime_error("Cannot translate unhandled instruction"); - - }; - -} - -enum class LogLevel -{ - Warning, - Error -}; - -std::string to_string(const LogLevel ll) -{ - switch (ll) - { - case LogLevel::Warning: - return "warning"; - case LogLevel::Error: - return "error"; - } - return "unknown"; -} - -void log(LogLevel ll, const i386 &i, const std::string &message) -{ - std::cout << to_string(ll) << ": " << i.line_num << ": " << message << ": `" << i.line_text << "`\n"; -} - -void log(LogLevel ll, const int line_no, const std::string &line, const std::string &message) -{ - std::cout << to_string(ll) << ": " << line_no << ": " << message << ": `" << line << "`\n"; -} - -void to_mos6502(const i386 &i, std::vector &instructions) -{ - try { - switch(i.type) - { - case ASMLine::Type::Label: - instructions.emplace_back(i.type, i.text); - return; - case ASMLine::Type::Directive: - instructions.emplace_back(i.type, i.text); - return; - case ASMLine::Type::Instruction: -// instructions.emplace_back(ASMLine::Type::Directive, "; " + i.line_text); - - const auto head = instructions.size(); - translate_instruction(instructions, i.opcode, i.operand1, i.operand2); - - auto text = i.line_text; - if (text[0] == '\t') { - text.erase(0, 1); - } - for_each(std::next(instructions.begin(), head), instructions.end(), - [ text ](auto &ins){ - - ins.comment = text; - } - ); - return; - } - } catch (const std::exception &e) { - log(LogLevel::Error, i, e.what()); - } -} - -bool optimize(std::vector &instructions) -{ - if (instructions.size() < 2) { - return false; - } - - const auto next_instruction = [&instructions](auto i) { - do { - ++i; - } while (i < instructions.size() && instructions[i].type == ASMLine::Type::Directive); - return i; - }; - - for (size_t op = 0; op < instructions.size() - 1; ++op) - { - // look for a transfer of Y -> A immediately followed by A -> Y - if (instructions[op].opcode == mos6502::OpCode::tya) - { - next_instruction(op); - if (instructions[op].opcode == mos6502::OpCode::tay) { - instructions.erase(std::next(std::begin(instructions), op), std::next(std::begin(instructions), op+1)); - return true; - } - } - } - - for (size_t op = 0; op < instructions.size() - 1; ++op) - { - // look for a store A -> loc immediately followed by loc -> A - if (instructions[op].opcode == mos6502::OpCode::sta) - { - const auto next = next_instruction(op); - if (instructions[next].opcode == mos6502::OpCode::lda - && instructions[next].op == instructions[op].op) - { - instructions.erase(std::next(std::begin(instructions), next), std::next(std::begin(instructions), next+1)); - return true; - } - } - } - - for (size_t op = 0; op < instructions.size() - 1; ++op) - { - if (instructions[op].opcode == mos6502::OpCode::lda - && instructions[op].op.type == Operand::Type::literal) - { - const auto operand = instructions[op].op; - auto op2 = op+1; - // look for multiple stores of the same value - while (op2 < instructions.size() && (instructions[op2].opcode == mos6502::OpCode::sta - || instructions[op2].type == ASMLine::Type::Directive)) { - ++op2; - } - if (instructions[op2].opcode == mos6502::OpCode::lda - && operand == instructions[op2].op) - { - instructions.erase(std::next(std::begin(instructions), op2), std::next(std::begin(instructions), op2+1)); - return true; - } - - } - } - - - return false; -} - -bool fix_long_branches(std::vector &instructions, int &branch_patch_count) -{ - std::map labels; - for (size_t op = 0; op < instructions.size(); ++op) - { - if (instructions[op].type == ASMLine::Type::Label) { - labels[instructions[op].text] = op; - } - } - - for (size_t op = 0; op < instructions.size(); ++op) - { - if (instructions[op].is_branch && std::abs(static_cast(labels[instructions[op].op.value]) - static_cast(op)) * 3 > 255) - { - ++branch_patch_count; - const auto going_to = instructions[op].op.value; - const auto new_pos = "patch_" + std::to_string(branch_patch_count); - // uh-oh too long of a branch, have to convert this to a jump... - if (instructions[op].opcode == mos6502::OpCode::bne) { - const auto comment = instructions[op].comment; - instructions[op] = mos6502(mos6502::OpCode::beq, Operand(Operand::Type::literal, new_pos)); - instructions.insert(std::next(std::begin(instructions), op + 1), mos6502(mos6502::OpCode::jmp, Operand(Operand::Type::literal, going_to))); - instructions.insert(std::next(std::begin(instructions), op + 2), mos6502(ASMLine::Type::Label, new_pos)); - instructions[op].comment = instructions[op+1].comment = instructions[op+2].comment = comment; - return true; - } else { - throw std::runtime_error("Don't know how to reorg this branch"); - } - } - } - return false; -} - - -bool fix_overwritten_flags(std::vector &instructions) -{ - if (instructions.size() < 3) { - return false; - } - - for (size_t op = 0; op < instructions.size(); ++op) - { - if (instructions[op].is_comparison) { - auto op2 = op + 1; - while (op2 < instructions.size() - && !instructions[op2].is_comparison - && !instructions[op2].is_branch) - { - ++op2; - } - - if (op2 < instructions.size() - && (op2 - op) > 1 - && instructions[op2-1].opcode != mos6502::OpCode::plp) { - if (instructions[op2].is_comparison) { - continue; - } - - if (instructions[op2].is_branch) { - // insert a pull of processor status before the branch - instructions.insert(std::next(std::begin(instructions), op2), mos6502(mos6502::OpCode::plp)); - // insert a push of processor status after the comparison - instructions.insert(std::next(std::begin(instructions), op+1), mos6502(mos6502::OpCode::php)); - - return true; - } - - } - } - } - - return false; -} - - -int main() -{ - std::regex Comment(R"(\s*\#.*)"); - std::regex Label(R"(^(\S+):.*)"); - std::regex Directive(R"(^\t(\..+))"); - std::regex UnaryInstruction(R"(^\t(\S+)\s+(\S+))"); - std::regex BinaryInstruction(R"(^\t(\S+)\s+(\S+),\s+(\S+))"); - std::regex Instruction(R"(^\t(\S+))"); - - int lineno = 0; - - std::vector instructions; - - while (std::cin.good()) - { - std::string line; - getline(std::cin, line); - - try { - std::smatch match; - if (std::regex_match(line, match, Label)) - { - instructions.emplace_back(lineno, line, ASMLine::Type::Label, match[1]); - } else if (std::regex_match(line, match, Comment)) { - // don't care about comments - } else if (std::regex_match(line, match, Directive)) { - instructions.emplace_back(lineno, line, ASMLine::Type::Directive, match[1]); - } else if (std::regex_match(line, match, UnaryInstruction)) { - instructions.emplace_back(lineno, line, ASMLine::Type::Instruction, match[1], match[2]); - } else if (std::regex_match(line, match, BinaryInstruction)) { - instructions.emplace_back(lineno, line, ASMLine::Type::Instruction, match[1], match[2], match[3]); - } else if (std::regex_match(line, match, Instruction)) { - instructions.emplace_back(lineno, line, ASMLine::Type::Instruction, match[1]); - } else if (line == "") { - //std::cout << "EmptyLine\n"; - } else { - throw std::runtime_error("Unparsed Input, Line: " + std::to_string(lineno)); - } - } catch (const std::exception &e) { - log(LogLevel::Error, lineno, line, e.what()); - } - - ++lineno; - } - - std::set labels; - - for (const auto i : instructions) - { - if (i.type == ASMLine::Type::Label) { - labels.insert(i.text); - } - } - - std::set used_labels{"main"}; - - for (const auto i : instructions) - { - if (i.type == ASMLine::Type::Instruction) - { - if (labels.count(i.operand1.value) != 0) { - used_labels.insert(i.operand1.value); - used_labels.insert(i.operand2.value); - } - } - } - - // remove all labels and directives that we don't need - instructions.erase( - std::remove_if(std::begin(instructions), std::end(instructions), - [&used_labels](const auto &i){ - if (i.type == ASMLine::Type::Label) { - if (used_labels.count(i.text) == 0) { - // remove all unused labels that aren't 'main' - return true; - } - } - return false; - } - ), - std::end(instructions) - ); - - - - const auto new_labels = - [&used_labels](){ - std::map result; - for (const auto &l : used_labels) { - auto newl = l; - std::transform(newl.begin(), newl.end(), newl.begin(), [](const auto c) { return std::tolower(c); }); - newl.erase(std::remove_if(newl.begin(), newl.end(), [](const auto c){ return !std::isalnum(c); }), std::end(newl)); - result.emplace(std::make_pair(l, newl)); - } - return result; - }(); - - - - for (auto &i : instructions) - { - if (i.type == ASMLine::Type::Label) - { - i.text = new_labels.at(i.text); - } - - const auto itr1 = new_labels.find(i.operand1.value); - if (itr1 != new_labels.end()) { - i.operand1.value = itr1->second; - } - - const auto itr2 = new_labels.find(i.operand2.value); - if (itr2 != new_labels.end()) { - i.operand2.value = itr2->second; - } - } - - - std::vector new_instructions; - - for (const auto &i : instructions) - { - to_mos6502(i, new_instructions); - } - - while (fix_overwritten_flags(new_instructions)) - { - // do it however many times it takes - } - - while (optimize(new_instructions)) - { - // do it however many times it takes - } - - int branch_patch_count = 0; - while (fix_long_branches(new_instructions, branch_patch_count)) - { - // do it however many times it takes - } - - - for (const auto i : new_instructions) - { - std::cout << i.to_string() << '\n'; - } -} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..a69c7c7 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,64 @@ +# Automatically enable catch2 to generate ctest targets +if(CONAN_CATCH2_ROOT_DEBUG) + include(${CONAN_CATCH2_ROOT_DEBUG}/lib/cmake/Catch2/Catch.cmake) +else() + include(${CONAN_CATCH2_ROOT}/lib/cmake/Catch2/Catch.cmake) +endif() + +add_library(catch_main STATIC catch_main.cpp) +target_link_libraries(catch_main PUBLIC CONAN_PKG::catch2) +target_link_libraries(catch_main PRIVATE project_options) + +add_executable(tests tests.cpp) +target_link_libraries(tests PRIVATE project_warnings project_options catch_main) + +# automatically discover tests that are defined in catch based test files you can modify the unittests. Set TEST_PREFIX +# to whatever you want, or use different for different binaries +catch_discover_tests( + tests + TEST_PREFIX + "unittests." + REPORTER + xml + OUTPUT_DIR + . + OUTPUT_PREFIX + "unittests." + OUTPUT_SUFFIX + .xml) + +# Add a file containing a set of constexpr tests +add_executable(constexpr_tests constexpr_tests.cpp) +target_link_libraries(constexpr_tests PRIVATE project_options project_warnings catch_main) + +catch_discover_tests( + constexpr_tests + TEST_PREFIX + "constexpr." + REPORTER + xml + OUTPUT_DIR + . + OUTPUT_PREFIX + "constexpr." + OUTPUT_SUFFIX + .xml) + +# Disable the constexpr portion of the test, and build again this allows us to have an executable that we can debug when +# things go wrong with the constexpr testing +add_executable(relaxed_constexpr_tests constexpr_tests.cpp) +target_link_libraries(relaxed_constexpr_tests PRIVATE project_options project_warnings catch_main) +target_compile_definitions(relaxed_constexpr_tests PRIVATE -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE) + +catch_discover_tests( + relaxed_constexpr_tests + TEST_PREFIX + "relaxed_constexpr." + REPORTER + xml + OUTPUT_DIR + . + OUTPUT_PREFIX + "relaxed_constexpr." + OUTPUT_SUFFIX + .xml) diff --git a/test/catch_main.cpp b/test/catch_main.cpp new file mode 100644 index 0000000..d8d2eca --- /dev/null +++ b/test/catch_main.cpp @@ -0,0 +1,5 @@ +#define CATCH_CONFIG_MAIN // This tells the catch header to generate a main + +#include + + diff --git a/test/constexpr_tests.cpp b/test/constexpr_tests.cpp new file mode 100644 index 0000000..2bb5ea5 --- /dev/null +++ b/test/constexpr_tests.cpp @@ -0,0 +1,14 @@ +#include + +constexpr unsigned int Factorial(unsigned int number) +{ + return number <= 1 ? number : Factorial(number - 1) * number; +} + +TEST_CASE("Factorials are computed with constexpr", "[factorial]") +{ + STATIC_REQUIRE(Factorial(1) == 1); + STATIC_REQUIRE(Factorial(2) == 2); + STATIC_REQUIRE(Factorial(3) == 6); + STATIC_REQUIRE(Factorial(10) == 3628800); +} diff --git a/test/tests.cpp b/test/tests.cpp new file mode 100644 index 0000000..9c40b28 --- /dev/null +++ b/test/tests.cpp @@ -0,0 +1,14 @@ +#include + +unsigned int Factorial(unsigned int number) +{ + return number <= 1 ? number : Factorial(number - 1) * number; +} + +TEST_CASE("Factorials are computed", "[factorial]") +{ + REQUIRE(Factorial(1) == 1); + REQUIRE(Factorial(2) == 2); + REQUIRE(Factorial(3) == 6); + REQUIRE(Factorial(10) == 3628800); +}