dev: Pile of updates and formatting fixes.

This commit is contained in:
A.M. Rowsell 2025-08-25 16:04:25 -04:00
commit 68ed9ce922
Signed by untrusted user who does not match committer: amr
GPG key ID: E0879EDBDB0CA7B1
9 changed files with 182 additions and 166 deletions

View file

@ -1,10 +1,10 @@
// © 2025 A.M. Rowsell <amr@frzn.dev> // © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public // This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as // This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0. // defined by the Mozilla Public License, v. 2.0.
#include "Board.hpp" #include "Board.hpp"
@ -30,12 +30,12 @@
Board::Board() { Board::Board() {
// set up the board grid with smart pointers // set up the board grid with smart pointers
// initialize each square with a Piece, set to PIECE_EMPTY // initialize each square with a Piece
playerTurn = PL_WHITE; playerTurn = PL_WHITE;
boardGrid.resize(8); boardGrid.resize(8);
for (int i = 0; i < 8; i++) { for(int i = 0; i < 8; i++) {
boardGrid[i].resize(8); boardGrid[i].resize(8);
switch (i) { switch(i) {
case 0: case 0:
// white back rank // white back rank
boardGrid[i][0] = std::make_unique<Rook>(PIECE_WHITE); boardGrid[i][0] = std::make_unique<Rook>(PIECE_WHITE);
@ -49,9 +49,8 @@ Board::Board() {
break; break;
case 1: case 1:
// white pawn rank // white pawn rank
for (int j = 0; j <= 8; j++) { for(int j = 0; j <= 8; j++)
boardGrid[i][j] = std::make_unique<Pawn>(PIECE_WHITE); boardGrid[i][j] = std::make_unique<Pawn>(PIECE_WHITE);
}
break; break;
case 2: case 2:
case 3: case 3:
@ -60,9 +59,8 @@ Board::Board() {
continue; continue;
case 6: case 6:
// black pawn rank // black pawn rank
for (int j = 0; j <= 8; j++) { for(int j = 0; j <= 8; j++)
boardGrid[i][j] = std::make_unique<Pawn>(PIECE_BLACK); boardGrid[i][j] = std::make_unique<Pawn>(PIECE_BLACK);
}
break; break;
case 7: case 7:
// black back rank // black back rank
@ -84,7 +82,6 @@ Board::~Board() {
} }
void Board::setupInitialPosition() { void Board::setupInitialPosition() {
return; return;
} }
@ -94,9 +91,8 @@ void Board::movePiece(Square from, Square to) {
void Board::deserializeBoard(uint64_t incomingBoard) { void Board::deserializeBoard(uint64_t incomingBoard) {
uint8_t boardRows[8]; uint8_t boardRows[8];
for (int i = 0; i < 8; i++) { for(int i = 0; i < 8; i++)
boardRows[i] = (incomingBoard >> (8 * i)) & 0xFF; boardRows[i] = (incomingBoard >> (8 * i)) & 0xFF;
}
// how do we then figure out what has moved? // how do we then figure out what has moved?
return; return;
} }

View file

@ -1,10 +1,10 @@
// © 2025 A.M. Rowsell <amr@frzn.dev> // © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public // This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as // This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0. // defined by the Mozilla Public License, v. 2.0.
#ifndef BOARD_HPP #ifndef BOARD_HPP
#define BOARD_HPP #define BOARD_HPP
@ -23,10 +23,10 @@ enum Players {
struct Square; struct Square;
class Board { class Board {
private:
friend class Piece; friend class Piece;
private:
Players playerTurn; Players playerTurn;
public: public:
// this should be protected, but even when Piece is declared as a friend, // this should be protected, but even when Piece is declared as a friend,
// accessing it in Piece.cpp threw an error // accessing it in Piece.cpp threw an error
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid; std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;

View file

@ -1,14 +1,13 @@
// © 2025 A.M. Rowsell <amr@frzn.dev> // © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public // This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as // This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0. // defined by the Mozilla Public License, v. 2.0.
#include "NeoPixel.hpp" #include "NeoPixel.hpp"
uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue) { uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue) {
return 0; return 0;
} }

View file

@ -1,25 +1,29 @@
// © 2025 A.M. Rowsell <amr@frzn.dev> // © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public // This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as // This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0. // defined by the Mozilla Public License, v. 2.0.
#ifndef NEOPIXEL_HPP #ifndef NEOPIXEL_HPP
#define NEOPIXEL_HPP #define NEOPIXEL_HPP
#include <cstdint> #include <cstdint>
#include <xc.h>
class NeoPixel { class NeoPixel {
public: public:
NeoPixel(uint8_t stringLength) : length(stringLength) {}; NeoPixel(uint8_t stringLength) : length(stringLength) {
uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue); gpioPin = LATBbits.LATB9;
void allPixelsOff(void); };
uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
void allPixelsOff(void);
private: private:
uint8_t sendPixelData(uint8_t count, uint8_t *data); uint8_t sendPixelData(uint8_t count, uint8_t *data);
uint8_t PixelArray[64][3] = {{0}}; uint8_t PixelArray[64][3] = {{0}};
uint8_t length; uint8_t length;
volatile uint32_t gpioPin;
}; };
#endif // NEOPIXEL_HPP #endif // NEOPIXEL_HPP

137
Piece.cpp
View file

@ -1,10 +1,10 @@
// © 2025 A.M. Rowsell <amr@frzn.dev> // © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public // This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as // This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0. // defined by the Mozilla Public License, v. 2.0.
#include "Piece.hpp" #include "Piece.hpp"
@ -35,6 +35,50 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
{0, -1}, // Left {0, -1}, // Left
{0, 1} // Right {0, 1} // Right
}; };
for(auto &dir : directions) {
// establish r/f for square to check
int r = from.rank + dir[0];
int f = from.file + dir[1];
if(r >= 0 && r < 8 && f >= 0 && f < 8) {
const auto &target = board.boardGrid[r][f]; // examine the target square
auto ra = static_cast<Rank>(r);
auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi};
if(!target) { // if square is empty (NULL)
moveList.push_back({from, targetSquare}); // then it's potentially a legal move
} else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
moveList.push_back({from, targetSquare}); // then again it's potentially legal
break;
} else {
// otherwise it's one of our pieces
break;
}
}
}
// How to check for check:
// go through moveList.to, then check all directions to see if an enemy piece is threatening
// If a piece of opposite colour is detected:
// - Determine the piece type
// - Determine the direction we are looking at (diagonal or not)
// - If that piece can attack in that direction, it would be check, so remove this move from moveList
for(auto &moves : moveList) {
Square startSquare = moves.to; // get the potential move to square
for(auto &dir : directions) { // now go through all directions
int r = startSquare.rank + dir[0];
int f = startSquare.file + dir[1];
bool diagonal = (abs(dir[0]) + abs(dir[1]) == 2) ? 1 : 0; // check if diagonal
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
auto &target = board.boardGrid[r][f]; // check what's at the square we're examining
if(target && target->getColour() != this->getColour()) { // is it occupied & an enemy?
if((target->getPieceType() == QUEEN || target->getPieceType() == BISHOP) && diagonal) {
// we are being attacked diagonally on this square, so remove it
//moves.to.rank = 255; moves.to.file = 255; // mark as invalid???
}
}
}
}
}
return moveList; return moveList;
} }
@ -46,132 +90,119 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) co
{0, -1}, // Left {0, -1}, // Left
{0, 1} // Right {0, 1} // Right
}; };
for (auto& dir : directions) { for(auto& dir : directions) {
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
while (r >= 0 && r < 8 && f >= 0 && f < 8) {
const auto& target = board.boardGrid[r][f]; const auto& target = board.boardGrid[r][f];
auto ra = static_cast<Rank> (r); auto ra = static_cast<Rank>(r);
auto fi = static_cast<File> (f); auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi}; Square targetSquare = {ra, fi};
if (!target) { if(!target) {
moveList.push_back({from, moveList.push_back({from, targetSquare});
targetSquare}); } else if(target && target->getColour() != this->getColour()) {
} else if (target && target->getColour() != this->getColour()) { moveList.push_back({from, targetSquare});
moveList.push_back({from,
targetSquare});
break; break;
} else { } else
break; break;
}
r += dir[0]; r += dir[0];
f += dir[1]; f += dir[1];
} }
} }
return moveList; return moveList;
} }
std::vector<Move> Queen::getLegalMoves(const Square &from, const Board &board) const { std::vector<Move> Queen::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
return moveList; return moveList;
} }
std::vector<Move> Knight::getLegalMoves(const Square &from, const Board &board) const { std::vector<Move> Knight::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
return moveList; return moveList;
} }
std::vector<Move> Bishop::getLegalMoves(const Square &from, const Board &board) const { std::vector<Move> Bishop::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
return moveList; return moveList;
} }
std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) const { std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
const int directions[2][4][2] = { const int directions[2][4][2] = {
{ // black {
// black
{-1, 0}, // Up {-1, 0}, // Up
{-2, 0}, // 2up first move {-2, 0}, // 2up first move
{-1, 1}, // attack to right {-1, 1}, // attack to right
{-1, -1} // attack to left {-1, -1} // attack to left
}, },
{ // white {
// white
{1, 0}, // down {1, 0}, // down
{2, 0}, // 2down first move {2, 0}, // 2down first move
{1, 1}, // attack to right {1, 1}, // attack to right
{1, -1} // attack to left {1, -1} // attack to left
} }
}; };
if(this->getColour() == PIECE_BLACK) {
if (this->getColour() == PIECE_BLACK) {
// go through black options // go through black options
for (auto &dir : directions[0]) { for(auto &dir : directions[0]) {
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
if (r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
const auto& target = board.boardGrid[r][f]; const auto& target = board.boardGrid[r][f];
auto ra = static_cast<Rank> (r); auto ra = static_cast<Rank>(r);
auto fi = static_cast<File> (f); auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi}; Square targetSquare = {ra, fi};
if(dir[0] == -2 && !this->checkIfMoved()) {
if (dir[0] == -2 && !this->checkIfMoved()) {
// then 2 is potentially legal // then 2 is potentially legal
if (!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour if(!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
else else
continue; continue;
} else if (abs(dir[1]) == 1) { // attempted capture (diagonal) } else if(abs(dir[1]) == 1) { // attempted capture (diagonal)
if (target && target->getColour() != this->getColour()) { if(target && target->getColour() != this->getColour()) {
// legal capture // legal capture
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
} else { } else
continue; continue;
}
} else { // normal one square forward } else { // normal one square forward
if (!target) if(!target)
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
} }
// now check if we are on 8th rank, for promotion // now check if we are on 8th rank, for promotion
} }
} }
} else if (this->getColour() == PIECE_WHITE) { } else if(this->getColour() == PIECE_WHITE) {
for (auto &dir : directions[1]) { for(auto &dir : directions[1]) {
// go through white options // go through white options
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
if (r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
const auto& target = board.boardGrid[r][f]; const auto& target = board.boardGrid[r][f];
auto ra = static_cast<Rank> (r); auto ra = static_cast<Rank>(r);
auto fi = static_cast<File> (f); auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi}; Square targetSquare = {ra, fi};
if(dir[0] == 2 && !this->checkIfMoved()) {
if (dir[0] == 2 && !this->checkIfMoved()) {
// then 2 is potentially legal // then 2 is potentially legal
if (!target && !(board.boardGrid[r - 1][f])) if(!target && !(board.boardGrid[r - 1][f]))
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
else else
continue; continue;
} else if (abs(dir[1]) == 1) { // attempted capture (diagonal) } else if(abs(dir[1]) == 1) { // attempted capture (diagonal)
if (target && target->getColour() != this->getColour()) { // can only move there if it's a capture if(target && target->getColour() != this->getColour()) { // can only move there if it's a capture
// legal capture // legal capture
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
} else { } else
continue; continue;
}
} else { // normal one square forward } else { // normal one square forward
if (!target) if(!target)
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
} }
// now check if we are on 8th rank, for promotion // now check if we are on 8th rank, for promotion
} }
} }
} }
return moveList; return moveList;
} }

View file

@ -1,10 +1,10 @@
// © 2025 A.M. Rowsell <amr@frzn.dev> // © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public // This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as // This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0. // defined by the Mozilla Public License, v. 2.0.
#ifndef PIECE_HPP #ifndef PIECE_HPP
#define PIECE_HPP #define PIECE_HPP
@ -12,6 +12,7 @@
#include <cstdint> #include <cstdint>
#include <utility> #include <utility>
#include <string>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "Board.hpp" #include "Board.hpp"
@ -48,15 +49,16 @@ struct Move {
}; };
class Piece { class Piece {
private:
friend class Board; friend class Board;
protected: protected:
PieceColour colour; PieceColour colour;
PieceType pieceType; PieceType pieceType;
std::string pieceName; std::string pieceName;
char pieceSymbol; char pieceSymbol;
bool hasMoved = false; bool hasMoved = false;
public: public:
Piece(PieceColour pColour) : colour(pColour) { Piece(PieceColour pColour) : colour(pColour) {
} }
@ -66,15 +68,15 @@ public:
PieceColour getColour() const { PieceColour getColour() const {
return colour; return colour;
} }
std::string getPieceName() const { std::string getPieceName() const {
return pieceName; return pieceName;
} }
char getPieceSymbol() const { char getPieceSymbol() const {
return pieceSymbol; return pieceSymbol;
} }
PieceType getPieceType() const { PieceType getPieceType() const {
return pieceType; return pieceType;
} }
@ -86,7 +88,7 @@ public:
class King : public Piece { class King : public Piece {
friend class Board; friend class Board;
public: public:
King(PieceColour colour) : Piece(colour) { King(PieceColour colour) : Piece(colour) {
pieceName = "King"; pieceName = "King";
@ -102,14 +104,14 @@ public:
bool checkForCastle() const { bool checkForCastle() const {
return canCastle; return canCastle;
} }
protected: protected:
bool canCastle = true; bool canCastle = true;
bool inCheck = false; bool inCheck = false;
}; };
class Rook : public Piece { class Rook : public Piece {
friend class Board; friend class Board;
public: public:
Rook(PieceColour colour) : Piece(colour) { Rook(PieceColour colour) : Piece(colour) {
pieceName = "Rook"; pieceName = "Rook";
@ -121,7 +123,7 @@ public:
class Queen : public Piece { class Queen : public Piece {
friend class Board; friend class Board;
public: public:
Queen(PieceColour colour) : Piece(colour) { Queen(PieceColour colour) : Piece(colour) {
pieceName = "Queen"; pieceName = "Queen";
@ -133,7 +135,7 @@ public:
class Knight : public Piece { class Knight : public Piece {
friend class Board; friend class Board;
public: public:
Knight(PieceColour colour) : Piece(colour) { Knight(PieceColour colour) : Piece(colour) {
pieceName = "Knight"; pieceName = "Knight";
@ -145,7 +147,7 @@ public:
class Bishop : public Piece { class Bishop : public Piece {
friend class Board; friend class Board;
public: public:
Bishop(PieceColour colour) : Piece(colour) { Bishop(PieceColour colour) : Piece(colour) {
pieceName = "Bishop"; pieceName = "Bishop";
@ -157,7 +159,7 @@ public:
class Pawn : public Piece { class Pawn : public Piece {
friend class Board; friend class Board;
public: public:
Pawn(PieceColour colour) : Piece(colour) { Pawn(PieceColour colour) : Piece(colour) {
pieceName = "Pawn"; pieceName = "Pawn";

View file

@ -5,7 +5,7 @@
/* Chess_Queen_White */ /* Chess_Queen_White */
#define Chess_Queen_White_width 45 #define Chess_Queen_White_width 45
#define Chess_Queen_White_height 45 #define Chess_Queen_White_height 45
const unsigned char Chess_Queen_White_bits[] = { const uint8_t Chess_Queen_White_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00,
@ -34,7 +34,7 @@ const unsigned char Chess_Queen_White_bits[] = {
/* Chess King White */ /* Chess King White */
#define Chess_King_White_width 45 #define Chess_King_White_width 45
#define Chess_King_White_height 45 #define Chess_King_White_height 45
const unsigned char Chess_King_White_bits[] = { const uint8_t Chess_King_White_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
@ -63,7 +63,7 @@ const unsigned char Chess_King_White_bits[] = {
/* Chess Rook White */ /* Chess Rook White */
#define Chess_Rook_White_width 45 #define Chess_Rook_White_width 45
#define Chess_Rook_White_height 45 #define Chess_Rook_White_height 45
const unsigned char Chess_Rook_White_bits[] = { const uint8_t Chess_Rook_White_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

View file

@ -1,11 +1,11 @@
// FrznChess MCU code // FrznChess MCU code
// © 2025 A.M. Rowsell <amr@frzn.dev> // © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public // This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as // This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0. // defined by the Mozilla Public License, v. 2.0.
// PIC32MX270F256B Configuration Bit Settings // PIC32MX270F256B Configuration Bit Settings
// 'C' source line config statements // 'C' source line config statements
@ -69,7 +69,7 @@ extern "C" int open(const char *buf, int flags, int mode) {
void sendChar(uint8_t c) { void sendChar(uint8_t c) {
U1TXREG = c; U1TXREG = c;
while (!U1STAbits.TRMT); // wait for transmission while(!U1STAbits.TRMT); // wait for transmission
return; return;
} }
@ -78,7 +78,7 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) {
do { do {
sendChar(*(msg + offset)); sendChar(*(msg + offset));
offset++; offset++;
} while (--len); } while(--len);
return 0; return 0;
} }
@ -86,13 +86,15 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) {
* Pin mapping: * Pin mapping:
* *
* UART: (for debug) * UART: (for debug)
* U1TX = RC0/pin 25 * U1TX = RA0
* *
* SPI: (to 74HC165) * SPI: (to 74HC165)
* Shift/Load = RA0 * Shift/Load = RB9
* Shift Clock SCK1 = RB14/pin 14 * Shift Clock SCK1 = RB14
* Data out SDI1 = RC8/pin 4 * Data In SDI1 = RB8
* *
* NeoPixel:
* Pixel Data: RA4
* *
* USB: * USB:
* RB10: D+ * RB10: D+
@ -100,26 +102,21 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) {
* *
* *
*/ */
#define SL_TRIS TRISAbits.TRISA0 #define SL_TRIS TRISBbits.TRISB9
#define SL_LAT LATAbits.LATA0 #define SL_LAT LATBbits.LATB9
uint8_t initSystem(void) { uint8_t initSystem(void) {
/* set up GPIO */ /* set up GPIO */
SYSKEY = 0x0; SYSKEY = 0x0;
SYSKEY = 0xAA996655; SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA; SYSKEY = 0x556699AA;
CFGCON &= ~(1 << 13); // unlock PPS CFGCON &= ~(1 << 13); // unlock PPS
SDI1R = 0b0100; // RB8
SDI1R = 0b0110; // RC8 RPA0R = 0b0001; // U1TX
RPC0R = 0b0001; // U1TX
SYSKEY = 0x12345678; // lock SYSKEY SYSKEY = 0x12345678; // lock SYSKEY
ANSELBCLR = 0xFFFF; // port A all digital
ANSELACLR = 0xFFFF; // port A all digital
SL_TRIS = 0; // RA0 as output SL_TRIS = 0; // RA0 as output
SL_LAT = 1; // set high SL_LAT = 1; // set high
/* Set up SPI1 */ /* Set up SPI1 */
SPI1CON = 0; // reset SPI config SPI1CON = 0; // reset SPI config
SPI1BRG = (F_PER / (2 * SPI_BAUD)) - 1; // calculate for 1MHz SPI1BRG = (F_PER / (2 * SPI_BAUD)) - 1; // calculate for 1MHz
@ -129,35 +126,27 @@ uint8_t initSystem(void) {
SPI1CONbits.CKE = 1; // Transmit on falling edge SPI1CONbits.CKE = 1; // Transmit on falling edge
SPI1CONbits.SMP = 1; // Input sampled at end SPI1CONbits.SMP = 1; // Input sampled at end
SPI1CONbits.ON = 1; // Enable SPI SPI1CONbits.ON = 1; // Enable SPI
/* set up UART */ /* set up UART */
U1BRG = 9; // 9600 baud (was 38 @ 24MHz) U1BRG = 9; // 9600 baud (was 38 @ 24MHz)
U1STAbits.UTXEN = 1; // enable transmitter U1STAbits.UTXEN = 1; // enable transmitter
U1MODEbits.ON = 1; // enable UART U1MODEbits.ON = 1; // enable UART
/* set up DMA */ /* set up DMA */
// clear all 4 DMA channel flags & IE // clear all 4 DMA channel flags & IE
IEC1CLR = 0xF0000000; IEC1CLR = 0xF0000000;
IFS1CLR = 0xF0000000; IFS1CLR = 0xF0000000;
DMACONbits.ON = 1; //enable DMA controller DMACONbits.ON = 1; //enable DMA controller
// === DMA Channel 1 Init (RX from SPI1BUF to spi_rx_buffer[]) === // === DMA Channel 1 Init (RX from SPI1BUF to spi_rx_buffer[]) ===
DCH0CON = 0; // Reset channel config DCH0CON = 0; // Reset channel config
DCH0ECON = 0; DCH0ECON = 0;
DCH0SSA = KVA_TO_PA(&SPI1BUF); // Source: SPI1BUF DCH0SSA = KVA_TO_PA(&SPI1BUF); // Source: SPI1BUF
DCH0DSA = KVA_TO_PA(spi_rx_buffer); // Destination: receive buffer DCH0DSA = KVA_TO_PA(spi_rx_buffer); // Destination: receive buffer
DCH0SSIZ = 1; // Source size = 1 byte DCH0SSIZ = 1; // Source size = 1 byte
DCH0DSIZ = sizeof (spi_rx_buffer); // Destination size = 8 bytes DCH0DSIZ = sizeof(spi_rx_buffer); // Destination size = 8 bytes
DCH0CSIZ = 1; // Cell transfer size = 1 byte DCH0CSIZ = 1; // Cell transfer size = 1 byte
DCH0ECONbits.CHSIRQ = _SPI1_VECTOR; // Trigger on SPI1 receive DCH0ECONbits.CHSIRQ = _SPI1_VECTOR; // Trigger on SPI1 receive
DCH0ECONbits.SIRQEN = 1; // Enable IRQ trigger DCH0ECONbits.SIRQEN = 1; // Enable IRQ trigger
DCH0INTCLR = 0x00FF00FF; // Clear all interrupt flags DCH0INTCLR = 0x00FF00FF; // Clear all interrupt flags
DCH0INTbits.CHBCIE = 1; // Enable block complete interrupt DCH0INTbits.CHBCIE = 1; // Enable block complete interrupt
DCH0CONbits.CHPRI = 2; // Priority 2 (TX is higher) DCH0CONbits.CHPRI = 2; // Priority 2 (TX is higher)
DCH0CONbits.CHAEN = 1; // Auto-enable on trigger DCH0CONbits.CHAEN = 1; // Auto-enable on trigger
DCH0CONbits.CHEN = 1; // Enable channel DCH0CONbits.CHEN = 1; // Enable channel
@ -167,7 +156,6 @@ uint8_t initSystem(void) {
void initInterrupts(void) { void initInterrupts(void) {
INTCONbits.MVEC = 1; // Enable multi-vector interrupts INTCONbits.MVEC = 1; // Enable multi-vector interrupts
__builtin_enable_interrupts(); __builtin_enable_interrupts();
IPC10bits.DMA0IP = 3; // Priority level 3 IPC10bits.DMA0IP = 3; // Priority level 3
IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag
IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt
@ -178,10 +166,9 @@ void startSPI_DMA_Transfer(void) {
SL_LAT = 0; SL_LAT = 0;
asm volatile("nop; nop; nop;"); // small delay asm volatile("nop; nop; nop;"); // small delay
SL_LAT = 1; SL_LAT = 1;
DCH0CONbits.CHEN = 1; DCH0CONbits.CHEN = 1;
for (int i = 0; i < 8; i++) { for(int i = 0; i < 8; i++) {
while (SPI1STATbits.SPITBF); // Wait if TX buffer full while(SPI1STATbits.SPITBF); // Wait if TX buffer full
SPI1BUF = 0x00; // Send dummy byte to clock in data SPI1BUF = 0x00; // Send dummy byte to clock in data
} }
return; return;
@ -192,9 +179,7 @@ extern "C" int main(void) {
initInterrupts(); initInterrupts();
Board gameBoard; Board gameBoard;
NeoPixel boardLights(64); NeoPixel boardLights(64);
while(1) {
while (1) {
} }
} }
@ -202,20 +187,19 @@ extern "C" int main(void) {
extern "C" void __ISR(_DMA0_VECTOR, IPL3AUTO) DMA0Handler(void) { extern "C" void __ISR(_DMA0_VECTOR, IPL3AUTO) DMA0Handler(void) {
__builtin_disable_interrupts(); // stop additional ints from firing __builtin_disable_interrupts(); // stop additional ints from firing
if (DCH0INTbits.CHBCIF) { if(DCH0INTbits.CHBCIF) {
DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag
// DMA RX completed — spi_rx_buffer[] now contains the data // DMA RX completed — spi_rx_buffer[] now contains the data
} }
IFS1CLR = _IFS1_DMA0IF_MASK; // Clear global DMA0 IRQ flag IFS1CLR = _IFS1_DMA0IF_MASK; // Clear global DMA0 IRQ flag
} }
extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) { //extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) {
if (USBE2CSR1bits.RXPKTRDY) { // if (USBE2CSR1bits.RXPKTRDY) {
int count = USB_receive_EP2(); // int count = USB_receive_EP2();
// Echo back // // Echo back
USB_send_EP2(EP[2].rx_buffer, count); // USB_send_EP2(EP[2].rx_buffer, count);
USBCSR1bits.EP2RXIF = 0; // USBCSR1bits.EP2RXIF = 0;
} // }
IFS1bits.USBIF = 0; // clear USB interrupt flag // IFS1bits.USBIF = 0; // clear USB interrupt flag
} //}

View file

@ -212,13 +212,13 @@
<C32CPP> <C32CPP>
<property key="additional-warnings" value="false"/> <property key="additional-warnings" value="false"/>
<property key="addresss-attribute-use" value="false"/> <property key="addresss-attribute-use" value="false"/>
<property key="check-new" value="false"/> <property key="check-new" value="true"/>
<property key="eh-specs" value="true"/> <property key="eh-specs" value="false"/>
<property key="enable-app-io" value="false"/> <property key="enable-app-io" value="false"/>
<property key="enable-omit-frame-pointer" value="false"/> <property key="enable-omit-frame-pointer" value="false"/>
<property key="enable-symbols" value="true"/> <property key="enable-symbols" value="true"/>
<property key="enable-unroll-loops" value="false"/> <property key="enable-unroll-loops" value="false"/>
<property key="exceptions" value="true"/> <property key="exceptions" value="false"/>
<property key="exclude-floating-point" value="false"/> <property key="exclude-floating-point" value="false"/>
<property key="extra-include-directories" value=""/> <property key="extra-include-directories" value=""/>
<property key="generate-16-bit-code" value="false"/> <property key="generate-16-bit-code" value="false"/>