diff --git a/Board.cpp b/Board.cpp index 2ff72fe..b7adfa5 100644 --- a/Board.cpp +++ b/Board.cpp @@ -31,6 +31,7 @@ Board::Board() { // set up the board grid with smart pointers // initialize each square with a Piece, set to PIECE_EMPTY + playerTurn = PL_WHITE; boardGrid.resize(8); for (int i = 0; i < 8; i++) { boardGrid[i].resize(8); @@ -92,5 +93,10 @@ void Board::movePiece(Square from, Square to) { } void Board::deserializeBoard(uint64_t incomingBoard) { + uint8_t boardRows[8]; + for (int i = 0; i < 8; i++) { + boardRows[i] = (incomingBoard >> (8 * i)) & 0xFF; + } + // how do we then figure out what has moved? return; } \ No newline at end of file diff --git a/Board.hpp b/Board.hpp index cf22035..c50e242 100644 --- a/Board.hpp +++ b/Board.hpp @@ -16,24 +16,30 @@ // why do I have to forward declare all these?! class Piece; -enum Players { PL_WHITE, PL_BLACK }; + +enum Players { + PL_WHITE, PL_BLACK +}; struct Square; class Board { -friend class Piece; + friend class Piece; +private: + Players playerTurn; public: - Board(); - virtual ~Board(); - void setupInitialPosition(); - Piece *getPieceAt(Square square) const; - void movePiece(Square from, Square to); - bool isInBounds(Square square) const; - bool isEmpty(Square square) const; - uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position - void deserializeBoard(uint64_t incomingBoard); - // this should be protected, but even when Piece is declared as a friend, - // accessing it in Piece.cpp threw an error - std::vector>> boardGrid; + // this should be protected, but even when Piece is declared as a friend, + // accessing it in Piece.cpp threw an error + std::vector>> boardGrid; + Board(); + virtual ~Board(); + void setupInitialPosition(); + Piece *getPieceAt(Square square); + void movePiece(Square from, Square to); + bool isInBounds(Square square) const; + bool isEmpty(Square square) const; + uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position + void deserializeBoard(uint64_t incomingBoard); + }; #endif // BOARD_HPP diff --git a/Piece.cpp b/Piece.cpp index 2ced144..35903f8 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -125,13 +125,13 @@ std::vector Pawn::getLegalMoves(const Square &from, const Board &board) co if (!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour moveList.push_back({from, targetSquare}); else - break; + continue; } else if (abs(dir[1]) == 1) { // attempted capture (diagonal) if (target && target->getColour() != this->getColour()) { // legal capture moveList.push_back({from, targetSquare}); } else { - break; + continue; } } else { // normal one square forward if (!target) @@ -153,16 +153,16 @@ std::vector Pawn::getLegalMoves(const Square &from, const Board &board) co if (dir[0] == 2 && !this->checkIfMoved()) { // then 2 is potentially legal - if (!target && !(boardGrid[r - 1][f])) + if (!target && !(board.boardGrid[r - 1][f])) moveList.push_back({from, targetSquare}); else - break; + continue; } else if (abs(dir[1]) == 1) { // attempted capture (diagonal) if (target && target->getColour() != this->getColour()) { // can only move there if it's a capture // legal capture moveList.push_back({from, targetSquare}); } else { - break; + continue; } } else { // normal one square forward if (!target) diff --git a/Piece.hpp b/Piece.hpp index 7e3e9dc..084ac10 100644 --- a/Piece.hpp +++ b/Piece.hpp @@ -51,6 +51,9 @@ class Piece { friend class Board; protected: PieceColour colour; + PieceType pieceType; + std::string pieceName; + char pieceSymbol; bool hasMoved = false; public: @@ -63,6 +66,18 @@ public: PieceColour getColour() const { return colour; } + + std::string getPieceName() const { + return pieceName; + } + + char getPieceSymbol() const { + return pieceSymbol; + } + + PieceType getPieceType() const { + return pieceType; + } bool checkIfMoved() const { return hasMoved; @@ -74,6 +89,9 @@ class King : public Piece { public: King(PieceColour colour) : Piece(colour) { + pieceName = "King"; + pieceSymbol = 'K'; + pieceType = KING; } virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; @@ -94,6 +112,9 @@ class Rook : public Piece { public: Rook(PieceColour colour) : Piece(colour) { + pieceName = "Rook"; + pieceSymbol = 'R'; + pieceType = ROOK; } virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; @@ -103,6 +124,9 @@ class Queen : public Piece { public: Queen(PieceColour colour) : Piece(colour) { + pieceName = "Queen"; + pieceSymbol = 'Q'; + pieceType = QUEEN; } virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; @@ -112,6 +136,9 @@ class Knight : public Piece { public: Knight(PieceColour colour) : Piece(colour) { + pieceName = "Knight"; + pieceSymbol = 'N'; + pieceType = KNIGHT; } virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; @@ -121,6 +148,9 @@ class Bishop : public Piece { public: Bishop(PieceColour colour) : Piece(colour) { + pieceName = "Bishop"; + pieceSymbol = 'B'; + pieceType = BISHOP; } virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; @@ -130,6 +160,9 @@ class Pawn : public Piece { public: Pawn(PieceColour colour) : Piece(colour) { + pieceName = "Pawn"; + pieceSymbol = 'P'; + pieceType = PAWN; } virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; diff --git a/graphics.cpp b/graphics.cpp new file mode 100644 index 0000000..3fdb3e1 --- /dev/null +++ b/graphics.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +/* Chess_Queen_White */ +#define Chess_Queen_White_width 45 +#define Chess_Queen_White_height 45 +const unsigned char 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, 0xe0, 0x00, 0x00, 0x00, + 0x00, 0x60, 0xf0, 0xc1, 0x00, 0x00, 0x00, 0xf0, 0x90, 0xe1, 0x01, 0x00, + 0x00, 0x98, 0x91, 0x31, 0x03, 0x00, 0x60, 0x98, 0xf1, 0x31, 0xc3, 0x00, + 0xf0, 0xf0, 0xe0, 0xe0, 0xe1, 0x01, 0x98, 0x61, 0x60, 0xe0, 0x30, 0x03, + 0x98, 0xe1, 0x60, 0x60, 0x30, 0x03, 0xf0, 0xe0, 0xe0, 0x60, 0xe0, 0x01, + 0xe0, 0xe0, 0xe0, 0x70, 0xe0, 0x00, 0xc0, 0xe0, 0xb1, 0x70, 0x70, 0x00, + 0xc0, 0xe1, 0xb1, 0x50, 0x38, 0x00, 0xc0, 0x63, 0xb1, 0x59, 0x38, 0x00, + 0x80, 0x62, 0x93, 0xc9, 0x3c, 0x00, 0x80, 0x66, 0x93, 0xcd, 0x36, 0x00, + 0x80, 0x6c, 0x1e, 0xcd, 0x36, 0x00, 0x80, 0x6d, 0x1e, 0xc7, 0x13, 0x00, + 0x80, 0x79, 0x0c, 0xc7, 0x11, 0x00, 0x00, 0x71, 0x0c, 0xc7, 0x19, 0x00, + 0x00, 0xf1, 0xff, 0xff, 0x19, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, + 0x00, 0x0f, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x0c, 0x00, + 0x00, 0x06, 0xff, 0x1f, 0x0e, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, + 0x00, 0x18, 0x00, 0x80, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x01, 0x00, + 0x00, 0xf8, 0xff, 0xff, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, + 0x00, 0x0c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, + 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x04, 0x00, + 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0xe0, 0xff, 0xff, 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 +}; + +/* Chess King White */ +#define Chess_King_White_width 45 +#define Chess_King_White_height 45 +const unsigned char 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, 0x40, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, + 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x03, 0x00, 0x00, + 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x78, 0x08, 0xc2, 0x03, 0x00, + 0x00, 0xff, 0x0f, 0xfe, 0x1f, 0x00, 0x80, 0x03, 0x0f, 0x1e, 0x38, 0x00, + 0xc0, 0x00, 0x1c, 0x07, 0x60, 0x00, 0xc0, 0x00, 0x18, 0x03, 0x60, 0x00, + 0x60, 0x00, 0xb0, 0x01, 0xc0, 0x00, 0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, + 0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0x60, 0x00, 0x40, 0x00, 0xc0, 0x00, + 0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, + 0x80, 0x01, 0x40, 0x00, 0x30, 0x00, 0x00, 0x03, 0xfe, 0x0f, 0x18, 0x00, + 0x00, 0xce, 0xff, 0x7f, 0x0e, 0x00, 0x00, 0xfc, 0x00, 0xe0, 0x07, 0x00, + 0x00, 0x30, 0xf0, 0x81, 0x01, 0x00, 0x00, 0x90, 0xff, 0x3f, 0x01, 0x00, + 0x00, 0xf0, 0x03, 0xf8, 0x01, 0x00, 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00, + 0x00, 0x10, 0xfe, 0x0f, 0x01, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, + 0x00, 0xf0, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00, + 0x00, 0xc0, 0x07, 0x7c, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 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 +}; + +/* Chess Rook White */ +#define Chess_Rook_White_width 45 +#define Chess_Rook_White_height 45 +const unsigned char 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, 0xfc, 0xf8, 0xe3, 0x07, 0x00, + 0x00, 0xcc, 0x18, 0x63, 0x06, 0x00, 0x00, 0xcc, 0x1f, 0x7f, 0x06, 0x00, + 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, + 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, + 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, + 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, + 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, + 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x18, 0x00, + 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 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 +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp index d25cd2d..59306a8 100644 --- a/main.cpp +++ b/main.cpp @@ -58,6 +58,7 @@ #define SPI_BAUD 1000000 // 1MHz hopefully volatile uint8_t spi_rx_buffer[8]; +volatile uint32_t usbBDT[512] __attribute__((aligned(512))); // USB buffer description table // dummy open to get rid of linker error @@ -130,7 +131,7 @@ uint8_t initSystem(void) { SPI1CONbits.ON = 1; // Enable SPI /* set up UART */ - U1BRG = 19; // 9600 baud (was 38 @ 24MHz) + U1BRG = 9; // 9600 baud (was 38 @ 24MHz) U1STAbits.UTXEN = 1; // enable transmitter U1MODEbits.ON = 1; // enable UART @@ -199,7 +200,7 @@ extern "C" int main(void) { // === Interrupt Service Routine for DMA0 (RX complete) === -extern "C" void __ISR(_DMA0_VECTOR, IPL3SOFT) DMA0Handler(void) { +extern "C" void __ISR(_DMA0_VECTOR, IPL3AUTO) DMA0Handler(void) { __builtin_disable_interrupts(); // stop additional ints from firing if (DCH0INTbits.CHBCIF) { DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag @@ -207,4 +208,14 @@ extern "C" void __ISR(_DMA0_VECTOR, IPL3SOFT) DMA0Handler(void) { } IFS1CLR = _IFS1_DMA0IF_MASK; // Clear global DMA0 IRQ flag +} + +extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) { + if (USBE2CSR1bits.RXPKTRDY) { + int count = USB_receive_EP2(); + // Echo back + USB_send_EP2(EP[2].rx_buffer, count); + USBCSR1bits.EP2RXIF = 0; + } + IFS1bits.USBIF = 0; // clear USB interrupt flag } \ No newline at end of file diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index fb8d13d..02c0777 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -37,6 +37,7 @@ Board.cpp Piece.cpp NeoPixel.cpp + graphics.cpp @@ -472,5 +473,218 @@ + + + localhost + PIC32MM0256GPM028 + + + noID + XC32 + 4.60 + 2 + + + + + + + + + + + + + + + false + true + + + + + + + false + false + + false + + false + false + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/project.xml b/nbproject/project.xml index 23e2470..e9182ac 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -21,6 +21,10 @@ Production 2 + + PIC32MM + 2 + false