1
0
mirror of https://github.com/lefticus/6502-cpp.git synced 2024-06-15 14:29:33 +00:00

change name to 6502-c++ and merge in files from cpp_starter_project

This commit is contained in:
Jason Turner 2021-05-06 14:04:59 -06:00
parent 21d23dfcb9
commit 4ffae19177
19 changed files with 1064 additions and 61 deletions

7
.clang-tidy Normal file
View File

@ -0,0 +1,7 @@
---
Checks: '*,-fuchsia-*,-google-*,-zircon-*,-abseil-*,-modernize-use-trailing-return-type,-llvm*'
WarningsAsErrors: '*'
HeaderFilterRegex: ''
FormatStyle: none

19
.cmake-format.yaml Normal file
View File

@ -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

View File

@ -1,11 +1,82 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.15)
project(x86-to-6502)
# 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)
add_executable(x86-to-6502 src/main.cpp)
# 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)
target_compile_features(x86-to-6502 PRIVATE cxx_std_20)
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()
if (CMAKE_COMPILER_IS_GNUCC)
target_compile_options(x86-to-6502 PRIVATE -Wall -Wextra -Wconversion -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wcast-qual -Wunused -Woverloaded-virtual -Wpedantic)
endif ()
# 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
<vector>
<string>
<map>
<utility>)
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)

24
LICENSE Normal file
View File

@ -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 <https://unlicense.org>

467
README.md Normal file
View File

@ -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/<user>/<your_new_repo>.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/<unnecessary_framework>
## 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/)
<details>
<summary>Install command</summary>
- Debian/Ubuntu:
sudo apt install build-essential
- Windows:
choco install mingw -y
- MacOS:
brew install gcc
</details>
* [clang 6+](https://clang.llvm.org/)
<details>
<summary>Install command</summary>
- 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
</details>
* [Visual Studio 2019 or higher](https://visualstudio.microsoft.com/)
<details>
<summary>Install command + Environment setup</summary>
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
</details>
2. [Conan](https://conan.io/)
<details>
<summary>Install Command</summary>
- 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
</details>
3. [CMake 3.15+](https://cmake.org/)
<details>
<summary>Install Command</summary>
- Debian/Ubuntu:
sudo apt-get install cmake
- Windows:
choco install cmake -y
- MacOS:
brew install cmake
</details>
### Optional Dependencies
#### C++ Tools
* [Doxygen](http://doxygen.nl/)
<details>
<summary>Install Command</summary>
- 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
</details>
* [ccache](https://ccache.dev/)
<details>
<summary>Install Command</summary>
- Debian/Ubuntu:
sudo apt-get install ccache
- Windows:
choco install ccache -y
- MacOS:
brew install ccache
</details>
* [Cppcheck](http://cppcheck.sourceforge.net/)
<details>
<summary>Install Command</summary>
- Debian/Ubuntu:
sudo apt-get install cppcheck
- Windows:
choco install cppcheck -y
- MacOS:
brew install cppcheck
</details>
* [include-what-you-use](https://include-what-you-use.org/)
<details>
<summary>Install Command</summary>
Follow instructions here:
https://github.com/include-what-you-use/include-what-you-use#how-to-install
</details>
#### 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.
<details>
<summary>Commands for setting the compilers </summary>
- 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"
</details>
### 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!
<details>
<summary>Windows - MinGW Makefiles</summary>
Choose MinGW Makefiles as the generator:
<img src="https://user-images.githubusercontent.com/16418197/82769479-616ade80-9dfa-11ea-899e-3a8c31d43032.png" alt="mingw">
</details>
<details>
<summary>Windows - Visual Studio generator and compiler</summary>
You should have already set `C` and `CXX` to `cl.exe`.
Choose "Visual Studio 16 2019" as the generator:
<img src="https://user-images.githubusercontent.com/16418197/82524696-32502680-9af5-11ea-9697-a42000e900a6.jpg" alt="default_vs">
</details>
<details>
<summary>Windows - Visual Studio generator and Clang Compiler</summary>
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".
<img src="https://user-images.githubusercontent.com/16418197/82781142-ae60ac00-9e1e-11ea-8c77-222b005a8f7e.png" alt="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".
<img src="https://user-images.githubusercontent.com/16418197/82769558-b3136900-9dfa-11ea-9f73-02ab8f9b0ca4.png" alt="visual_studio">
</details>
<br/>
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)

29
cmake/Cache.cmake Normal file
View File

@ -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()

View File

@ -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()

23
cmake/Conan.cmake Normal file
View File

@ -0,0 +1,23 @@
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
OPTIONS
${CONAN_EXTRA_OPTIONS}
BASIC_SETUP
CMAKE_TARGETS # individual targets to link to
BUILD
missing)
endmacro()

11
cmake/Doxygen.cmake Normal file
View File

@ -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()

View File

@ -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()

66
cmake/Sanitizers.cmake Normal file
View File

@ -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()

View File

@ -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()

View File

@ -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()

View File

@ -218,6 +218,9 @@ struct mos6502 : ASMLine
case OpCode::bne:
case OpCode::bmi:
case OpCode::beq:
case OpCode::bpl:
case OpCode::bcs:
case OpCode::bcc:
case OpCode::adc:
case OpCode::sbc:
case OpCode::rts:
@ -976,7 +979,7 @@ void translate_instruction(std::vector<mos6502> &instructions, const AVR::OpCode
}
case AVR::OpCode::sbiw: {
subtract_16_bit(instructions, o1_reg_num, atoi(o2.value.c_str()));
subtract_16_bit(instructions, o1_reg_num, static_cast<std::uint16_t>(std::stoi(o2.value)));
return;
}
@ -1083,6 +1086,9 @@ void translate_instruction(std::vector<mos6502> &instructions, const AVR::OpCode
instructions.emplace_back(mos6502::OpCode::beq, o1);
return;
}
case AVR::OpCode::unknown: {
throw std::runtime_error("Could not translate 'unknown' instruction");
}
}
@ -1397,26 +1403,26 @@ void log(LogLevel ll, const int line_no, const std::string &line, const std::str
}
template<typename FromArch>
void to_mos6502(const FromArch &i, std::vector<mos6502> &instructions)
void to_mos6502(const FromArch &from_instruction, std::vector<mos6502> &instructions)
{
try {
switch (i.type) {
switch (from_instruction.type) {
case ASMLine::Type::Label:
if (i.text == "0") {
instructions.emplace_back(i.type, "-memcpy_0");
if (from_instruction.text == "0") {
instructions.emplace_back(from_instruction.type, "-memcpy_0");
} else {
instructions.emplace_back(i.type, i.text);
instructions.emplace_back(from_instruction.type, from_instruction.text);
}
return;
case ASMLine::Type::Directive:
if (i.text.starts_with(".string")) {
instructions.emplace_back(ASMLine::Type::Directive, ".asc " + i.text.substr(7));
} else if (i.text.starts_with(".zero")) {
const auto count = atoi(i.text.data() + 6);
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(".zero")) {
const auto count = std::stoull(&*std::next(from_instruction.text.begin(), 6), nullptr, 10);
std::string zeros;
for (int i = 0; i < count; ++i) {
if ((i % 20) == 0) {
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();
@ -1432,30 +1438,30 @@ void to_mos6502(const FromArch &i, std::vector<mos6502> &instructions)
}
} else {
instructions.emplace_back(ASMLine::Type::Directive, "; Unknown directive: " + i.text);
instructions.emplace_back(ASMLine::Type::Directive, "; Unknown directive: " + from_instruction.text);
}
return;
case ASMLine::Type::Instruction:
const auto head = instructions.size();
try {
translate_instruction(instructions, i.opcode, i.operand1, i.operand2);
translate_instruction(instructions, from_instruction.opcode, from_instruction.operand1, from_instruction.operand2);
} catch (const std::exception &e) {
instructions.emplace_back(ASMLine::Type::Directive, "; Unhandled opcode: '" + i.text + "' " + e.what());
log(LogLevel::Error, i, e.what());
instructions.emplace_back(ASMLine::Type::Directive, "; Unhandled opcode: '" + from_instruction.text + "' " + e.what());
log(LogLevel::Error, from_instruction, e.what());
}
auto text = i.line_text;
auto text = from_instruction.line_text;
if (text[0] == '\t') {
text.erase(0, 1);
}
for_each(std::next(instructions.begin(), head), instructions.end(), [text](auto &ins) {
for_each(std::next(instructions.begin(), static_cast<std::ptrdiff_t>(head)), instructions.end(), [text](auto &ins) {
ins.comment = text;
});
return;
}
} catch (const std::exception &e) {
log(LogLevel::Error, i, e.what());
log(LogLevel::Error, from_instruction, e.what());
}
}
@ -1515,6 +1521,10 @@ bool optimize(std::vector<mos6502> &instructions)
// 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) {
@ -1609,39 +1619,26 @@ bool fix_long_branches(std::vector<mos6502> &instructions, int &branch_patch_cou
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) {
std::map<mos6502::OpCode, mos6502::OpCode> 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(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] = mos6502(mapping->second, Operand(Operand::Type::literal, new_pos));
instructions.insert(std::next(std::begin(instructions), static_cast<std::ptrdiff_t>(op + 1)), mos6502(mos6502::OpCode::jmp, Operand(Operand::Type::literal, going_to)));
instructions.insert(std::next(std::begin(instructions), static_cast<std::ptrdiff_t>(op + 2)), mos6502(ASMLine::Type::Label, new_pos));
instructions[op].comment = instructions[op + 1].comment = instructions[op + 2].comment = comment;
return true;
} else if (instructions[op].opcode == mos6502::OpCode::beq) {
const auto comment = instructions[op].comment;
instructions[op] = mos6502(mos6502::OpCode::bne, 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 if (instructions[op].opcode == mos6502::OpCode::bcc) {
const auto comment = instructions[op].comment;
instructions[op] = mos6502(mos6502::OpCode::bcs, 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 if (instructions[op].opcode == mos6502::OpCode::bcs) {
const auto comment = instructions[op].comment;
instructions[op] = mos6502(mos6502::OpCode::bcc, 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: " + instructions[op].to_string());
}
throw std::runtime_error("Don't know how to reorg this branch: " + instructions[op].to_string());
}
}
return false;
@ -1672,9 +1669,9 @@ bool fix_overwritten_flags(std::vector<mos6502> &instructions)
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));
instructions.insert(std::next(std::begin(instructions), static_cast<std::ptrdiff_t>(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));
instructions.insert(std::next(std::begin(instructions), static_cast<std::ptrdiff_t>(op + 1)), mos6502(mos6502::OpCode::php));
return true;
}
@ -1860,7 +1857,7 @@ void run(std::istream &input)
}
for (const auto i : new_instructions) {
for (const auto &i : new_instructions) {
std::cout << i.to_string() << '\n';
}
}
@ -1878,16 +1875,16 @@ int main([[maybe_unused]] const int argc, const char *argv[])
}
}();
const bool is_avr = [&]() {
const bool is_386 = [&]() {
for (std::size_t index = 0; index < static_cast<std::size_t>(argc); ++index) {
if (strstr(argv[index], "avr") != nullptr) {
if (strstr(argv[index], "x86") != nullptr) {
return true;
}
}
return false;
}();
if (is_avr) {
if (!is_386) {
std::cout << "; AVR Mode\n";
run<AVR>(input);
} else {

17
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,17 @@
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)
target_link_libraries(
6502-c++
PRIVATE project_options
project_warnings
CONAN_PKG::docopt.cpp
CONAN_PKG::fmt
CONAN_PKG::spdlog)

64
test/CMakeLists.txt Normal file
View File

@ -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)

5
test/catch_main.cpp Normal file
View File

@ -0,0 +1,5 @@
#define CATCH_CONFIG_MAIN // This tells the catch header to generate a main
#include <catch2/catch.hpp>

14
test/constexpr_tests.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <catch2/catch.hpp>
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);
}

14
test/tests.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <catch2/catch.hpp>
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);
}