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

@ -30,7 +30,7 @@
Board::Board() {
// 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;
boardGrid.resize(8);
for(int i = 0; i < 8; i++) {
@ -49,9 +49,8 @@ Board::Board() {
break;
case 1:
// 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);
}
break;
case 2:
case 3:
@ -60,9 +59,8 @@ Board::Board() {
continue;
case 6:
// 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);
}
break;
case 7:
// black back rank
@ -84,7 +82,6 @@ Board::~Board() {
}
void Board::setupInitialPosition() {
return;
}
@ -94,9 +91,8 @@ void Board::movePiece(Square from, Square to) {
void Board::deserializeBoard(uint64_t incomingBoard) {
uint8_t boardRows[8];
for (int i = 0; i < 8; i++) {
for(int i = 0; i < 8; i++)
boardRows[i] = (incomingBoard >> (8 * i)) & 0xFF;
}
// how do we then figure out what has moved?
return;
}

View file

@ -23,8 +23,8 @@ enum Players {
struct Square;
class Board {
friend class Piece;
private:
friend class Piece;
Players playerTurn;
public:
// this should be protected, but even when Piece is declared as a friend,

View file

@ -9,6 +9,5 @@
#include "NeoPixel.hpp"
uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue) {
return 0;
}

View file

@ -9,10 +9,13 @@
#ifndef NEOPIXEL_HPP
#define NEOPIXEL_HPP
#include <cstdint>
#include <xc.h>
class NeoPixel {
public:
NeoPixel(uint8_t stringLength) : length(stringLength) {};
NeoPixel(uint8_t stringLength) : length(stringLength) {
gpioPin = LATBbits.LATB9;
};
uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
void allPixelsOff(void);
@ -20,6 +23,7 @@ private:
uint8_t sendPixelData(uint8_t count, uint8_t *data);
uint8_t PixelArray[64][3] = {{0}};
uint8_t length;
volatile uint32_t gpioPin;
};
#endif // NEOPIXEL_HPP

View file

@ -35,6 +35,50 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
{0, -1}, // Left
{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;
}
@ -49,66 +93,58 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) co
for(auto& dir : directions) {
int r = from.rank + dir[0];
int f = from.file + dir[1];
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
const auto& target = board.boardGrid[r][f];
auto ra = static_cast<Rank>(r);
auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi};
if(!target) {
moveList.push_back({from,
targetSquare});
moveList.push_back({from, targetSquare});
} else if(target && target->getColour() != this->getColour()) {
moveList.push_back({from,
targetSquare});
moveList.push_back({from, targetSquare});
break;
} else {
} else
break;
}
r += dir[0];
f += dir[1];
}
}
return moveList;
}
std::vector<Move> Queen::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> Knight::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> Bishop::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
const int directions[2][4][2] = {
{ // black
{
// black
{-1, 0}, // Up
{-2, 0}, // 2up first move
{-1, 1}, // attack to right
{-1, -1} // attack to left
},
{ // white
{
// white
{1, 0}, // down
{2, 0}, // 2down first move
{1, 1}, // attack to right
{1, -1} // attack to left
}
};
if(this->getColour() == PIECE_BLACK) {
// go through black options
for(auto &dir : directions[0]) {
@ -119,7 +155,6 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) co
auto ra = static_cast<Rank>(r);
auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi};
if(dir[0] == -2 && !this->checkIfMoved()) {
// then 2 is potentially legal
if(!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour
@ -130,9 +165,8 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) co
if(target && target->getColour() != this->getColour()) {
// legal capture
moveList.push_back({from, targetSquare});
} else {
} else
continue;
}
} else { // normal one square forward
if(!target)
moveList.push_back({from, targetSquare});
@ -150,7 +184,6 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) co
auto ra = static_cast<Rank>(r);
auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi};
if(dir[0] == 2 && !this->checkIfMoved()) {
// then 2 is potentially legal
if(!target && !(board.boardGrid[r - 1][f]))
@ -161,9 +194,8 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) co
if(target && target->getColour() != this->getColour()) { // can only move there if it's a capture
// legal capture
moveList.push_back({from, targetSquare});
} else {
} else
continue;
}
} else { // normal one square forward
if(!target)
moveList.push_back({from, targetSquare});
@ -172,6 +204,5 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) co
}
}
}
return moveList;
}

View file

@ -12,6 +12,7 @@
#include <cstdint>
#include <utility>
#include <string>
#include <memory>
#include <vector>
#include "Board.hpp"
@ -48,6 +49,7 @@ struct Move {
};
class Piece {
private:
friend class Board;
protected:
PieceColour colour;

View file

@ -5,7 +5,7 @@
/* Chess_Queen_White */
#define Chess_Queen_White_width 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, 0xe0, 0x00, 0x00, 0x00,
@ -34,7 +34,7 @@ const unsigned char Chess_Queen_White_bits[] = {
/* Chess King White */
#define Chess_King_White_width 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, 0x40, 0x00, 0x00, 0x00,
@ -63,7 +63,7 @@ const unsigned char Chess_King_White_bits[] = {
/* Chess Rook White */
#define Chess_Rook_White_width 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,

View file

@ -86,13 +86,15 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) {
* Pin mapping:
*
* UART: (for debug)
* U1TX = RC0/pin 25
* U1TX = RA0
*
* SPI: (to 74HC165)
* Shift/Load = RA0
* Shift Clock SCK1 = RB14/pin 14
* Data out SDI1 = RC8/pin 4
* Shift/Load = RB9
* Shift Clock SCK1 = RB14
* Data In SDI1 = RB8
*
* NeoPixel:
* Pixel Data: RA4
*
* USB:
* RB10: D+
@ -100,26 +102,21 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) {
*
*
*/
#define SL_TRIS TRISAbits.TRISA0
#define SL_LAT LATAbits.LATA0
#define SL_TRIS TRISBbits.TRISB9
#define SL_LAT LATBbits.LATB9
uint8_t initSystem(void) {
/* set up GPIO */
SYSKEY = 0x0;
SYSKEY = 0xAA996655;
SYSKEY = 0x556699AA;
CFGCON &= ~(1 << 13); // unlock PPS
SDI1R = 0b0110; // RC8
RPC0R = 0b0001; // U1TX
SDI1R = 0b0100; // RB8
RPA0R = 0b0001; // U1TX
SYSKEY = 0x12345678; // lock SYSKEY
ANSELACLR = 0xFFFF; // port A all digital
ANSELBCLR = 0xFFFF; // port A all digital
SL_TRIS = 0; // RA0 as output
SL_LAT = 1; // set high
/* Set up SPI1 */
SPI1CON = 0; // reset SPI config
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.SMP = 1; // Input sampled at end
SPI1CONbits.ON = 1; // Enable SPI
/* set up UART */
U1BRG = 9; // 9600 baud (was 38 @ 24MHz)
U1STAbits.UTXEN = 1; // enable transmitter
U1MODEbits.ON = 1; // enable UART
/* set up DMA */
// clear all 4 DMA channel flags & IE
IEC1CLR = 0xF0000000;
IFS1CLR = 0xF0000000;
DMACONbits.ON = 1; //enable DMA controller
// === DMA Channel 1 Init (RX from SPI1BUF to spi_rx_buffer[]) ===
DCH0CON = 0; // Reset channel config
DCH0ECON = 0;
DCH0SSA = KVA_TO_PA(&SPI1BUF); // Source: SPI1BUF
DCH0DSA = KVA_TO_PA(spi_rx_buffer); // Destination: receive buffer
DCH0SSIZ = 1; // Source size = 1 byte
DCH0DSIZ = sizeof(spi_rx_buffer); // Destination size = 8 bytes
DCH0CSIZ = 1; // Cell transfer size = 1 byte
DCH0ECONbits.CHSIRQ = _SPI1_VECTOR; // Trigger on SPI1 receive
DCH0ECONbits.SIRQEN = 1; // Enable IRQ trigger
DCH0INTCLR = 0x00FF00FF; // Clear all interrupt flags
DCH0INTbits.CHBCIE = 1; // Enable block complete interrupt
DCH0CONbits.CHPRI = 2; // Priority 2 (TX is higher)
DCH0CONbits.CHAEN = 1; // Auto-enable on trigger
DCH0CONbits.CHEN = 1; // Enable channel
@ -167,7 +156,6 @@ uint8_t initSystem(void) {
void initInterrupts(void) {
INTCONbits.MVEC = 1; // Enable multi-vector interrupts
__builtin_enable_interrupts();
IPC10bits.DMA0IP = 3; // Priority level 3
IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag
IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt
@ -178,7 +166,6 @@ void startSPI_DMA_Transfer(void) {
SL_LAT = 0;
asm volatile("nop; nop; nop;"); // small delay
SL_LAT = 1;
DCH0CONbits.CHEN = 1;
for(int i = 0; i < 8; i++) {
while(SPI1STATbits.SPITBF); // Wait if TX buffer full
@ -192,9 +179,7 @@ extern "C" int main(void) {
initInterrupts();
Board gameBoard;
NeoPixel boardLights(64);
while(1) {
}
}
@ -206,16 +191,15 @@ extern "C" void __ISR(_DMA0_VECTOR, IPL3AUTO) DMA0Handler(void) {
DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag
// DMA RX completed — spi_rx_buffer[] now contains the data
}
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
}
//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
//}

View file

@ -212,13 +212,13 @@
<C32CPP>
<property key="additional-warnings" value="false"/>
<property key="addresss-attribute-use" value="false"/>
<property key="check-new" value="false"/>
<property key="eh-specs" value="true"/>
<property key="check-new" value="true"/>
<property key="eh-specs" value="false"/>
<property key="enable-app-io" value="false"/>
<property key="enable-omit-frame-pointer" value="false"/>
<property key="enable-symbols" value="true"/>
<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="extra-include-directories" value=""/>
<property key="generate-16-bit-code" value="false"/>