From 965ebe9b5574fc0b7cb82605ac4812a2e63dbd31 Mon Sep 17 00:00:00 2001 From: Vijay Soni Date: Sat, 29 Jun 2024 02:22:53 +0530 Subject: [PATCH] Implement Doom for M5Stack CardPuter --- Makefile.m5card | 37 +++++++++++++++++++++ README.md | 23 +++++++++++++ m5card.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 25 +------------- 4 files changed, 148 insertions(+), 24 deletions(-) create mode 100644 Makefile.m5card create mode 100644 m5card.c diff --git a/Makefile.m5card b/Makefile.m5card new file mode 100644 index 000000000..4f2386b7d --- /dev/null +++ b/Makefile.m5card @@ -0,0 +1,37 @@ +# Makefile for compiling Doom for ESP32 based M5Stack CardPuter + +# Compiler settings +CC = xtensa-esp32-elf-gcc +CXX = xtensa-esp32-elf-g++ +CFLAGS = -Iinclude -DM5STACK_MPU6886 -O3 +CXXFLAGS = $(CFLAGS) -std=c++11 +LDFLAGS = -Llib -lM5Stack -lM5GFX -lM5Unified -lM5Cardputer + +# Source files and object files +SRCS = $(wildcard src/*.cpp) $(wildcard doomgeneric/*.c) +OBJS = $(SRCS:.cpp=.o) $(SRCS:.c=.o) + +# Target binary +TARGET = doom_m5card + +# Default target +all: $(TARGET) + +# Compile and link +$(TARGET): $(OBJS) + $(CXX) -o $@ $^ $(LDFLAGS) + +# Compile C source files +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +# Compile C++ source files +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +# Clean target +clean: + rm -f $(OBJS) $(TARGET) + +# If possible, use CMake for more advanced build configurations +# Note: CMakeLists.txt must be provided in the project root for CMake to work diff --git a/README.md b/README.md index b3eaff976..4a885771d 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,29 @@ make -j4 -f Makefile.sdl ``` +--- + +## Porting to M5Stack CardPuter + +To bring the classic Doom experience to your M5Stack CardPuter, follow these setup instructions: + +### Hardware Requirements + +- M5Stack CardPuter +- SD card with at least 16MB of free space +- USB-C cable for programming and power supply + +### Compiling and Uploading + +1. Install [PlatformIO](https://platformio.org/) and the necessary ESP32 platform support. +2. Clone this repository to your local machine. +3. Place your WAD file (e.g., `doom1.wad`) in the root of the SD card and insert it into the M5Stack CardPuter. +4. Navigate to the project directory and open the `platformio.ini` file. Ensure the correct environment is selected for your M5Stack CardPuter model. +5. Compile the project by running `pio run` in the terminal. +6. Upload the game to your M5Stack CardPuter using `pio run --target upload`. +7. Once uploaded, reset the M5Stack CardPuter to start playing Doom! + +Enjoy Doom on your M5Stack CardPuter and relive the classic gaming experience! --- diff --git a/m5card.c b/m5card.c new file mode 100644 index 000000000..30153ae92 --- /dev/null +++ b/m5card.c @@ -0,0 +1,87 @@ +#include "doomgeneric.h" +#include +#include +#include + +#define KEYQUEUE_SIZE 16 + +static unsigned short s_KeyQueue[KEYQUEUE_SIZE]; +static unsigned int s_KeyQueueWriteIndex = 0; +static unsigned int s_KeyQueueReadIndex = 0; + +#ifdef __cplusplus +extern "C" +{ +#endif + + void doomgeneric_Create(int argc, char **argv); + void doomgeneric_Tick(); + +#ifdef __cplusplus +} +#endif + +void DG_Init() +{ + M5.begin(); + // Initialize M5Stack Cardputer +} + +void DG_DrawFrame() +{ + M5.Lcd.drawBitmap(0, 0, DOOMGENERIC_RESX, DOOMGENERIC_RESY, (uint16_t *)DG_ScreenBuffer); +} + +void DG_SleepMs(uint32_t ms) +{ + delay(ms); +} + +uint32_t DG_GetTicksMs() +{ + return millis(); +} + +int DG_GetKey(int *pressed, unsigned char *doomKey) +{ + if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex) + { + // key queue is empty + return 0; + } + else + { + unsigned short keyData = s_KeyQueue[s_KeyQueueReadIndex]; + s_KeyQueueReadIndex++; + s_KeyQueueReadIndex %= KEYQUEUE_SIZE; + + *pressed = keyData >> 8; + *doomKey = keyData & 0xFF; + + return 1; + } + + return 0; +} + +void setup() +{ + doomgeneric_Create(0, nullptr); +} + +void loop() +{ + doomgeneric_Tick(); +} + +// int main(int argc, char **argv) +// { +// doomgeneric_Create(argc, argv); + +// while (1) +// { +// doomgeneric_Tick(); +// } + +// return 0; +// } diff --git a/src/main.cpp b/src/main.cpp index 9afdc24db..3b00e3a0b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,29 +43,6 @@ uint32_t DG_GetTicksMs() return millis(); } -// int DG_GetKey(int* pressed, unsigned char* key) { -// *pressed = 0; -// *key = 0; - -// if(M5.BtnA.wasPressed()) { -// *pressed = 1; -// *key = KEY_ENTER; -// return 1; -// } -// else if(M5.BtnB.wasPressed()) { -// *pressed = 1; -// *key = KEY_FIRE; -// return 1; -// } -// else if(M5.BtnC.wasPressed()) { -// *pressed = 1; -// *key = KEY_USE; -// return 1; -// } - -// return 0; -// } - int DG_GetKey(int *pressed, unsigned char *doomKey) { if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex) @@ -108,4 +85,4 @@ void loop() // } // return 0; -// } \ No newline at end of file +// }