mirror of
https://github.com/lefticus/6502-cpp.git
synced 2024-10-15 00:24:24 +00:00
commit
47d4ce2342
98
.clang-format
Normal file
98
.clang-format
Normal file
@ -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
|
||||
|
7
.clang-tidy
Normal file
7
.clang-tidy
Normal file
@ -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
|
||||
|
||||
|
19
.cmake-format.yaml
Normal file
19
.cmake-format.yaml
Normal 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
|
||||
|
@ -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
|
||||
<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
24
LICENSE
Normal 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
467
README.md
Normal 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
29
cmake/Cache.cmake
Normal 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()
|
78
cmake/CompilerWarnings.cmake
Normal file
78
cmake/CompilerWarnings.cmake
Normal 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()
|
24
cmake/Conan.cmake
Normal file
24
cmake/Conan.cmake
Normal file
@ -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()
|
11
cmake/Doxygen.cmake
Normal file
11
cmake/Doxygen.cmake
Normal 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()
|
18
cmake/PreventInSourceBuilds.cmake
Normal file
18
cmake/PreventInSourceBuilds.cmake
Normal 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
66
cmake/Sanitizers.cmake
Normal 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()
|
42
cmake/StandardProjectSettings.cmake
Normal file
42
cmake/StandardProjectSettings.cmake
Normal 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()
|
||||
|
37
cmake/StaticAnalyzers.cmake
Normal file
37
cmake/StaticAnalyzers.cmake
Normal 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()
|
1
compile.sh
Executable file
1
compile.sh
Executable file
@ -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
|
130
examples/16bit_counter.cpp
Normal file
130
examples/16bit_counter.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string_view>
|
||||
|
||||
enum Colors : uint8_t { WHITE = 0x01 };
|
||||
|
||||
inline volatile uint8_t &memory_loc(const uint16_t loc) {
|
||||
return *reinterpret_cast<volatile uint8_t *>(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<char, 1024> &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<uint8_t *>(&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<std::uint8_t>(0xFF & value));
|
||||
put_hex(x,y, static_cast<std::uint8_t>(0xFF & (value >> 8)));
|
||||
}
|
||||
|
||||
struct Clock {
|
||||
using milliseconds = std::chrono::duration<std::uint16_t, std::milli>;
|
||||
|
||||
// return elapsed time since last restart
|
||||
[[nodiscard]] milliseconds restart() {
|
||||
// stop Timer A
|
||||
poke(0xDC0E, 0b00000000);
|
||||
|
||||
// last value
|
||||
const auto previous_value = static_cast<std::uint16_t>(
|
||||
peek(0xDC04) | (static_cast<uint16_t>(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<std::uint8_t, 1000> data{0};
|
||||
// std::memcpy(const_cast<uint8_t*>(&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<uint8_t>(col);
|
||||
};
|
||||
|
||||
background_color(Colors::WHITE);
|
||||
|
||||
while(true) {
|
||||
if (joystick_down()) {
|
||||
increment_border_color();
|
||||
} else {
|
||||
decrement_border_color();
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
214
examples/16bit_counter_with_map_and_strings.cpp
Normal file
214
examples/16bit_counter_with_map_and_strings.cpp
Normal file
@ -0,0 +1,214 @@
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string_view>
|
||||
|
||||
enum Colors : uint8_t { WHITE = 0x01 };
|
||||
|
||||
static volatile uint8_t &memory_loc(const uint16_t loc) {
|
||||
return *reinterpret_cast<volatile uint8_t *>(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<char, 1024> &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<uint8_t *>(&memory_loc(start)), str.data(),
|
||||
str.size());
|
||||
}
|
||||
|
||||
|
||||
template<std::uint8_t Width, std::uint8_t Height>
|
||||
struct Graphic
|
||||
{
|
||||
std::array<std::uint8_t, Width * Height> data;
|
||||
|
||||
static constexpr auto width() noexcept {
|
||||
return Width;
|
||||
}
|
||||
|
||||
static constexpr auto height() noexcept {
|
||||
return Height;
|
||||
}
|
||||
|
||||
constexpr Graphic() = default;
|
||||
|
||||
constexpr Graphic(std::array<uint8_t, Width * Height> data_) noexcept : data(data_) {}
|
||||
constexpr Graphic(std::initializer_list<uint8_t> 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<std::uint8_t>(0xFF & value));
|
||||
put_hex(x,y, static_cast<std::uint8_t>(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<std::uint16_t, std::milli>;
|
||||
|
||||
// return elapsed time since last restart
|
||||
[[nodiscard]] milliseconds restart() {
|
||||
// stop Timer A
|
||||
poke(0xDC0E, 0b00000000);
|
||||
|
||||
// last value
|
||||
const auto previous_value = static_cast<std::uint16_t>(
|
||||
peek(0xDC04) | (static_cast<uint16_t>(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<std::uint8_t, 1000> data{0};
|
||||
// std::memcpy(const_cast<uint8_t*>(&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<uint8_t>(col);
|
||||
};
|
||||
|
||||
background_color(Colors::WHITE);
|
||||
|
||||
while(true) {
|
||||
if (joystick_down()) {
|
||||
increment_border_color();
|
||||
} else {
|
||||
decrement_border_color();
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
@ -1,26 +1,26 @@
|
||||
#include <cstdint>
|
||||
|
||||
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<volatile uint8_t *>(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();
|
||||
}
|
||||
|
@ -6,22 +6,22 @@ enum class Colors : uint8_t
|
||||