diff --git a/.clang-format b/.clang-format index e984571..7d4507c 100644 --- a/.clang-format +++ b/.clang-format @@ -62,7 +62,7 @@ SpaceBeforeAssignmentOperators: true SpaceBeforeParens: ControlStatements SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 1 -SpacesInAngles: false +SpacesInAngles: true SpacesInContainerLiterals: true SpacesInCStyleCastParentheses: false SpacesInParentheses: true diff --git a/app-qtwidget/src/IoDeviceImpl.cpp b/app-qtwidget/src/IoDeviceImpl.cpp index 4737e24..7a5f7a4 100644 --- a/app-qtwidget/src/IoDeviceImpl.cpp +++ b/app-qtwidget/src/IoDeviceImpl.cpp @@ -4,30 +4,32 @@ namespace detail { -const std::map keymap = { { Qt::Key_X, 0x0 }, { Qt::Key_1, 0x1 }, { Qt::Key_2, 0x2 }, { Qt::Key_3, 0x3 }, - { Qt::Key_Q, 0x4 }, { Qt::Key_W, 0x5 }, { Qt::Key_E, 0x6 }, { Qt::Key_A, 0x7 }, - { Qt::Key_S, 0x8 }, { Qt::Key_D, 0x9 }, { Qt::Key_Z, 0xa }, { Qt::Key_C, 0xb }, - { Qt::Key_4, 0xc }, { Qt::Key_R, 0xd }, { Qt::Key_F, 0xe }, { Qt::Key_V, 0xf } }; +const std::map< Qt::Key, int > keymap = { { Qt::Key_X, 0x0 }, { Qt::Key_1, 0x1 }, { Qt::Key_2, 0x2 }, + { Qt::Key_3, 0x3 }, { Qt::Key_Q, 0x4 }, { Qt::Key_W, 0x5 }, + { Qt::Key_E, 0x6 }, { Qt::Key_A, 0x7 }, { Qt::Key_S, 0x8 }, + { Qt::Key_D, 0x9 }, { Qt::Key_Z, 0xa }, { Qt::Key_C, 0xb }, + { Qt::Key_4, 0xc }, { Qt::Key_R, 0xd }, { Qt::Key_F, 0xe }, + { Qt::Key_V, 0xf } }; } // namespace detail void IoDeviceImpl::set( const int key, const bool pressed ) { - if ( const auto it = detail::keymap.find( static_cast( key ) ); it != detail::keymap.end() ) + if ( const auto it = detail::keymap.find( static_cast< Qt::Key >( key ) ); it != detail::keymap.end() ) { - m_keypad[static_cast( it->second )] = pressed; + m_keypad[static_cast< size_t >( it->second )] = pressed; } } bool IoDeviceImpl::isKeyPressed( const model::chip8::key key ) const { - return m_keypad[static_cast( key )]; + return m_keypad[static_cast< size_t >( key )]; } -std::optional IoDeviceImpl::pressedKey() const +std::optional< model::chip8::key > IoDeviceImpl::pressedKey() const { if ( const auto it = std::ranges::find( m_keypad, std::true_type::value ); it != m_keypad.end() ) { - return static_cast( std::distance( std::begin( m_keypad ), it ) ); + return static_cast< model::chip8::key >( std::distance( std::begin( m_keypad ), it ) ); } return std::nullopt; diff --git a/app-qtwidget/src/IoDeviceImpl.hpp b/app-qtwidget/src/IoDeviceImpl.hpp index 540e2c8..2cb04ab 100644 --- a/app-qtwidget/src/IoDeviceImpl.hpp +++ b/app-qtwidget/src/IoDeviceImpl.hpp @@ -13,10 +13,10 @@ class IoDeviceImpl : public model::IoDevice bool isKeyPressed( model::chip8::key key ) const override; - std::optional pressedKey() const override; + std::optional< model::chip8::key > pressedKey() const override; void set( int key, bool pressed ); private: - std::array m_keypad = {}; + std::array< bool, model::chip8::keypad_size > m_keypad = {}; }; diff --git a/app-qtwidget/src/ScreenWidget.cpp b/app-qtwidget/src/ScreenWidget.cpp index 209fcc7..6e735dd 100644 --- a/app-qtwidget/src/ScreenWidget.cpp +++ b/app-qtwidget/src/ScreenWidget.cpp @@ -8,7 +8,7 @@ ScreenWidget::ScreenWidget( const std::uint32_t* buffer, int w, int h ) : QWidget{ nullptr } -, m_buffer{ reinterpret_cast( buffer ) } +, m_buffer{ reinterpret_cast< const uchar* >( buffer ) } , m_screenWidth{ w } , m_screenHeight{ h } { diff --git a/model/include/model/CPU.hpp b/model/include/model/CPU.hpp index df3a6b8..3d8a348 100644 --- a/model/include/model/CPU.hpp +++ b/model/include/model/CPU.hpp @@ -14,8 +14,8 @@ namespace model class CPU { public: - using Stack = std::array; - using VideoBuffer = std::array; + using Stack = std::array< chip8::word_t, chip8::stack_size >; + using VideoBuffer = std::array< std::uint32_t, chip8::display_size >; CPU( MMU& mmu, IoDevice& ioDevice, RandomNumberGenerator& rndGenerator ); @@ -24,7 +24,7 @@ class CPU chip8::byte_t readRegister( chip8::reg id ) const; void writeRegister( chip8::reg id, chip8::byte_t value ); - void loadToRegisters( const std::vector& values ); + void loadToRegisters( const std::vector< chip8::byte_t >& values ); chip8::word_t pc() const { @@ -141,9 +141,9 @@ class CPU bool m_drawFlag{ false }; bool m_isInterrupted{ false }; - std::array m_registers = {}; - Stack m_stack = {}; - VideoBuffer m_frameBuffer; + std::array< chip8::byte_t, chip8::num_registers > m_registers = {}; + Stack m_stack = {}; + VideoBuffer m_frameBuffer; MMU& m_mmu; IoDevice& m_ioDevice; diff --git a/model/include/model/Chip8.hpp b/model/include/model/Chip8.hpp index 5351044..3daf6e2 100644 --- a/model/include/model/Chip8.hpp +++ b/model/include/model/Chip8.hpp @@ -101,7 +101,7 @@ enum class key kf }; -const std::vector font_set{ +const std::vector< byte_t > font_set{ 0xF0, 0x90, 0x90, 0x90, 0xF0, // 0 0x20, 0x60, 0x20, 0x20, 0x70, // 1 0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2 diff --git a/model/include/model/IoDevice.hpp b/model/include/model/IoDevice.hpp index c5e5770..9b3bc63 100644 --- a/model/include/model/IoDevice.hpp +++ b/model/include/model/IoDevice.hpp @@ -14,7 +14,7 @@ class IoDevice virtual bool isKeyPressed( chip8::key key ) const = 0; - virtual std::optional pressedKey() const = 0; + virtual std::optional< chip8::key > pressedKey() const = 0; }; } // namespace model diff --git a/model/include/model/MMU.hpp b/model/include/model/MMU.hpp index e59a844..4f7c50f 100644 --- a/model/include/model/MMU.hpp +++ b/model/include/model/MMU.hpp @@ -28,8 +28,9 @@ class MMU [[nodiscard]] chip8::word_t readWord( const chip8::word_t address ) const { - const auto msb = static_cast( m_memory[address] << std::numeric_limits::digits ); - const auto lsb = static_cast( m_memory[address + 1] ); + const auto msb = + static_cast< chip8::word_t >( m_memory[address] << std::numeric_limits< chip8::byte_t >::digits ); + const auto lsb = static_cast< chip8::word_t >( m_memory[address + 1] ); return msb | lsb; } @@ -39,7 +40,7 @@ class MMU m_memory[address] = byte; } - template + template < typename T > void load( const T& rom, chip8::word_t address ) { const auto length = std::min( size() - address, std::size( rom ) ); @@ -78,7 +79,7 @@ class MMU } private: - std::array m_memory = {}; + std::array< chip8::byte_t, chip8::ram_size > m_memory = {}; }; } // namespace model diff --git a/model/include/model/MersenneByteTwister.hpp b/model/include/model/MersenneByteTwister.hpp index b241b14..af90f6a 100644 --- a/model/include/model/MersenneByteTwister.hpp +++ b/model/include/model/MersenneByteTwister.hpp @@ -19,8 +19,8 @@ class MersenneByteTwister : public RandomNumberGenerator } private: - mutable std::mt19937 m_generator{ std::random_device{}() }; - mutable std::uniform_int_distribution m_distribution{ 0x00u, 0xFFu }; + mutable std::mt19937 m_generator{ std::random_device{}() }; + mutable std::uniform_int_distribution< unsigned short > m_distribution{ 0x00u, 0xFFu }; }; } // namespace model diff --git a/model/include/model/WordDecoder.hpp b/model/include/model/WordDecoder.hpp index e2e0ed9..46ff759 100644 --- a/model/include/model/WordDecoder.hpp +++ b/model/include/model/WordDecoder.hpp @@ -8,25 +8,25 @@ namespace model::WordDecoder /// Reads byte value of X on pattern vXvv. inline chip8::byte_t readX( const chip8::word_t instr ) { - return static_cast( ( instr & 0x0F00 ) >> 8 ); + return static_cast< chip8::byte_t >( ( instr & 0x0F00 ) >> 8 ); } /// Reads byte value of Y on pattern vvYv. inline chip8::byte_t readY( const chip8::word_t instr ) { - return static_cast( ( instr & 0x00F0 ) >> 4 ); + return static_cast< chip8::byte_t >( ( instr & 0x00F0 ) >> 4 ); } /// Reads byte value of N on pattern vvvN. inline chip8::byte_t readN( const chip8::word_t instr ) { - return static_cast( instr & 0x000F ); + return static_cast< chip8::byte_t >( instr & 0x000F ); } /// Reads byte value of NN on pattern vvNN. inline chip8::byte_t readNN( const chip8::word_t instr ) { - return static_cast( instr & 0x00FF ); + return static_cast< chip8::byte_t >( instr & 0x00FF ); } /// Reads word value of NNN on pattern vNNN. diff --git a/model/src/CPU.cpp b/model/src/CPU.cpp index 756a8fa..9f18201 100644 --- a/model/src/CPU.cpp +++ b/model/src/CPU.cpp @@ -29,15 +29,15 @@ CPU::CPU( MMU& mmu, IoDevice& ioDevice, RandomNumberGenerator& rndGenerator ) chip8::byte_t CPU::readRegister( chip8::reg id ) const { - return m_registers.at( static_cast( id ) ); + return m_registers.at( static_cast< std::size_t >( id ) ); } void CPU::writeRegister( chip8::reg id, chip8::byte_t value ) { - m_registers.at( static_cast( id ) ) = value; + m_registers.at( static_cast< std::size_t >( id ) ) = value; } -void CPU::loadToRegisters( const std::vector& values ) +void CPU::loadToRegisters( const std::vector< chip8::byte_t >& values ) { const auto size = std::min( values.size(), m_registers.size() ); std::copy_n( std::begin( values ), size, std::begin( m_registers ) ); @@ -315,7 +315,7 @@ void CPU::bitwiseVxXorVy() void CPU::shiftVxRight() { const auto x = WordDecoder::readX( m_instruction ); - const auto mask = static_cast( 0x1 ); + const auto mask = static_cast< chip8::byte_t >( 0x1 ); auto& vx = m_registers.at( x ); writeRegister( chip8::reg::vf, vx & mask ); @@ -429,8 +429,8 @@ void CPU::draw() { if ( ( rowPixels & ( 0x80 >> row ) ) != 0 ) { - const auto offset = - static_cast( ( x + row + ( ( y + line ) * chip8::display_width ) ) % chip8::display_size ); + const auto offset = static_cast< size_t >( ( x + row + ( ( y + line ) * chip8::display_width ) ) + % chip8::display_size ); auto& pixel = m_frameBuffer.at( offset ); @@ -450,7 +450,7 @@ void CPU::draw() void CPU::executeSkipIfVxIsPressed() { - const auto key = static_cast( m_registers.at( WordDecoder::readX( m_instruction ) ) ); + const auto key = static_cast< chip8::key >( m_registers.at( WordDecoder::readX( m_instruction ) ) ); if ( m_ioDevice.isKeyPressed( key ) ) { @@ -460,7 +460,7 @@ void CPU::executeSkipIfVxIsPressed() void CPU::executeSkipIfVxIsNotPressed() { - const auto key = static_cast( m_registers.at( WordDecoder::readX( m_instruction ) ) ); + const auto key = static_cast< chip8::key >( m_registers.at( WordDecoder::readX( m_instruction ) ) ); if ( !m_ioDevice.isKeyPressed( key ) ) { @@ -475,7 +475,7 @@ void CPU::executeWaitPressedKeyToVx() if ( const auto pressedKey = m_ioDevice.pressedKey() ) { const auto x = WordDecoder::readX( m_instruction ); - m_registers.at( x ) = static_cast( *pressedKey ); + m_registers.at( x ) = static_cast< chip8::byte_t >( *pressedKey ); m_isInterrupted = false; } } @@ -497,7 +497,7 @@ void CPU::executeLoadRandomToVx() { const auto x = WordDecoder::readX( m_instruction ); const auto nn = WordDecoder::readNN( m_instruction ); - m_registers.at( x ) = nn & static_cast( m_rndGenerator.get() ); + m_registers.at( x ) = nn & static_cast< chip8::byte_t >( m_rndGenerator.get() ); } } // namespace model diff --git a/model/src/VM.cpp b/model/src/VM.cpp index bd5c512..581082b 100644 --- a/model/src/VM.cpp +++ b/model/src/VM.cpp @@ -22,8 +22,8 @@ bool VM::loadRom( const std::string& fileName ) { std::noskipws( in ); - const std::vector data{ std::istream_iterator{ in }, - std::istream_iterator{} }; + const std::vector< chip8::byte_t > data{ std::istream_iterator< chip8::byte_t >{ in }, + std::istream_iterator< chip8::byte_t >{} }; in.close(); m_mmu.load( data, chip8::init_rom_load_address ); diff --git a/model/tests/CPU.memory.test.cpp b/model/tests/CPU.memory.test.cpp index 9345962..98d2a89 100644 --- a/model/tests/CPU.memory.test.cpp +++ b/model/tests/CPU.memory.test.cpp @@ -46,8 +46,8 @@ SCENARIO_METHOD( CpuFixture, const model::chip8::word_t address{ 1024 }; cpu.iaddr( address ); - const std::vector bytes{ 0x10, 0x11, 0x12, 0x13, 0x24, 0x25, 0x26, 0x27, - 0x38, 0x39, 0x3A, 0x3B, 0x4C, 0x4D, 0x4E, 0x4F }; + const std::vector< model::chip8::byte_t > bytes{ 0x10, 0x11, 0x12, 0x13, 0x24, 0x25, 0x26, 0x27, + 0x38, 0x39, 0x3A, 0x3B, 0x4C, 0x4D, 0x4E, 0x4F }; cpu.loadToRegisters( bytes ); WHEN( "the CPU executes a FX55 opcode" ) @@ -58,7 +58,7 @@ SCENARIO_METHOD( CpuFixture, { for ( model::chip8::word_t i = 0u; i <= 0xFu; ++i ) { - const auto r = cpu.readRegister( static_cast( i ) ); + const auto r = cpu.readRegister( static_cast< model::chip8::reg >( i ) ); const auto m = mmu.readByte( address + i ); REQUIRE( r == m ); } @@ -77,7 +77,7 @@ SCENARIO_METHOD( CpuFixture, const model::chip8::word_t address{ 1024 }; cpu.iaddr( address ); - const std::vector bytes = { 0x10, 0x11, 0x12, 0x13, 0x24, 0x25 }; + const std::vector< model::chip8::byte_t > bytes = { 0x10, 0x11, 0x12, 0x13, 0x24, 0x25 }; mmu.load( bytes, address ); WHEN( "the CPU executes a FX65 opcode" ) @@ -88,7 +88,7 @@ SCENARIO_METHOD( CpuFixture, { for ( model::chip8::word_t i = 0u; i <= 0x5u; ++i ) { - const auto r = cpu.readRegister( static_cast( i ) ); + const auto r = cpu.readRegister( static_cast< model::chip8::reg >( i ) ); const auto m = mmu.readByte( address + i ); REQUIRE( r == m ); } diff --git a/model/tests/MMU.test.cpp b/model/tests/MMU.test.cpp index e1c4943..ef507b0 100644 --- a/model/tests/MMU.test.cpp +++ b/model/tests/MMU.test.cpp @@ -38,8 +38,8 @@ SCENARIO( "Comparing two identical MMUs using the equal operator", "[mmu]" ) { model::MMU mmu1; model::MMU mmu2; - std::ranges::fill( mmu1, static_cast( 0xFFu ) ); - std::ranges::fill( mmu2, static_cast( 0xFFu ) ); + std::ranges::fill( mmu1, static_cast< model::chip8::byte_t >( 0xFFu ) ); + std::ranges::fill( mmu2, static_cast< model::chip8::byte_t >( 0xFFu ) ); WHEN( "the MMUs are compared using the equal operator" ) { @@ -143,8 +143,8 @@ SCENARIO( "MMU loads a rom that fits at the given valid address", "[mmu]" ) { GIVEN( "A rom and a MMU" ) { - std::vector data{ 0xFF, 0x11, 0xCC, 0x33 }; - model::MMU mmu; + std::vector< std::uint8_t > data{ 0xFF, 0x11, 0xCC, 0x33 }; + model::MMU mmu; WHEN( "the MMU loads the rom at a valid address with enough memory space" ) { @@ -165,8 +165,8 @@ SCENARIO( "MMU loads a rom that does not fit at the given valid address", "[mmu] { GIVEN( "A rom and a MMU" ) { - std::vector data{ 0xFF, 0x11, 0xCC, 0x33 }; - model::MMU mmu; + std::vector< std::uint8_t > data{ 0xFF, 0x11, 0xCC, 0x33 }; + model::MMU mmu; WHEN( "the MMU loads the rom at a valid address without enough space" ) { @@ -186,9 +186,9 @@ SCENARIO( "MMU loads a rom to an invalid memory address", "[mmu]" ) { GIVEN( "A rom and a MMU" ) { - std::vector data{ 0xFF, 0x11, 0xCC, 0x33 }; - model::MMU mmu; - model::MMU originalMmu{ mmu }; + std::vector< std::uint8_t > data{ 0xFF, 0x11, 0xCC, 0x33 }; + model::MMU mmu; + model::MMU originalMmu{ mmu }; WHEN( "the MMU loads the rom at an invalid address" ) { @@ -206,7 +206,7 @@ SCENARIO( "MMU clears its memory", "[mmu]" ) GIVEN( "A MMU with initialized values" ) { model::MMU mmu; - std::ranges::fill( mmu, static_cast( 0xFFu ) ); + std::ranges::fill( mmu, static_cast< model::chip8::byte_t >( 0xFFu ) ); WHEN( "the MMU clears the memory" ) { diff --git a/model/tests/mock/Helper.hpp b/model/tests/mock/Helper.hpp index ff8bcf9..c887094 100644 --- a/model/tests/mock/Helper.hpp +++ b/model/tests/mock/Helper.hpp @@ -26,7 +26,7 @@ struct TestKit class ByteStream : public std::istream { public: - ByteStream( std::vector& data ) + ByteStream( std::vector< std::uint8_t >& data ) : std::istream{ &byteBuffer } , byteBuffer{ data.data(), data.size() } { @@ -34,14 +34,14 @@ class ByteStream : public std::istream } private: - class ByteBuffer : public std::basic_streambuf + class ByteBuffer : public std::basic_streambuf< char > { public: ByteBuffer( std::uint8_t* data, std::size_t size ) { - setg( reinterpret_cast( data ), - reinterpret_cast( data ), - reinterpret_cast( data ) + size ); + setg( reinterpret_cast< char* >( data ), + reinterpret_cast< char* >( data ), + reinterpret_cast< char* >( data ) + size ); } }; diff --git a/model/tests/mock/IoDeviceMock.hpp b/model/tests/mock/IoDeviceMock.hpp index f77ab6b..9acf29a 100644 --- a/model/tests/mock/IoDeviceMock.hpp +++ b/model/tests/mock/IoDeviceMock.hpp @@ -23,7 +23,7 @@ class IoDeviceMock : public model::IoDevice return false; } - std::optional pressedKey() const override + std::optional< model::chip8::key > pressedKey() const override { return m_pressedKey; } @@ -34,7 +34,7 @@ class IoDeviceMock : public model::IoDevice } private: - std::optional m_pressedKey = std::nullopt; + std::optional< model::chip8::key > m_pressedKey = std::nullopt; }; } // namespace Mock