Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
84 changes: 54 additions & 30 deletions linux/makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
APP_NAME = Kiwi8

# Use VERSION if set, otherwise fall back to GITHUB_REF_NAME, then default to unknown
# Use VERSION if set, otherwise fall back to GITHUB_REF_NAME, then default to develop
VERSION ?= $(GITHUB_REF_NAME)
ifeq ($(VERSION),)
VERSION = develop
endif

# Use SUB_VERSION if set, otherwise fall back to GITHUB_SHA, then default to unknown
SUB_VERSION ?= $(GITHUB_SHA)
ifeq ($(SUB_VERSION),)
SUB_VERSION = unknown
endif

APP_EXE = $(APP_NAME)

CC = g++
CFLAGS = -std=c++11 -Wall -MMD -MP -DAPP_NAME='"$(APP_NAME)"' -DVERSION='"$(VERSION)"' -DSUB_VERSION='"$(SUB_VERSION)"'
LFLAGS = -Wl,-rpath,'$$ORIGIN'
CC = gcc
CXX = g++
CFLAGS = -std=gnu11
CPPFLAGS = -Wall \
-MMD \
-MP \
-DAPP_NAME='"$(APP_NAME)"' \
-DVERSION='"$(VERSION)"' \
-DSUB_VERSION='"$(SUB_VERSION)"'

CXXFLAGS = -std=gnu++11

INCLUDE_DIRS = -I../external/sdl/build/include/ \
-I../external/imgui/ \
-I../external/stb/ \
-I../external/lekkit/

LDFLAGS = -L../external/sdl/build/lib \
-lSDL2-2.0 \
-lGL \
-lm \
-ldl \
-lpthread \
-Wl,-rpath,'$$ORIGIN'

# Use pkg-config to get GTK3 and audio backend flags
GTK3_CFLAGS = $(shell pkg-config --cflags gtk+-3.0)
Expand All @@ -31,32 +53,31 @@ PIPEWIRE_LIBS = $(shell pkg-config --libs libpipewire-0.3)
JACK_CFLAGS = $(shell pkg-config --cflags jack)
JACK_LIBS = $(shell pkg-config --libs jack)

CFLAGS += $(GTK3_CFLAGS) $(ALSA_CFLAGS) $(PULSE_CFLAGS) $(PIPEWIRE_CFLAGS) $(JACK_CFLAGS)
CPPFLAGS += $(GTK3_CFLAGS) $(ALSA_CFLAGS) $(PULSE_CFLAGS) $(PIPEWIRE_CFLAGS) $(JACK_CFLAGS)
LDFLAGS += $(GTK3_LIBS) $(ALSA_LIBS) $(PULSE_LIBS) $(PIPEWIRE_LIBS) $(JACK_LIBS)

INCS = -I../external/sdl/build/include/ -I../external/imgui/ -I../external/stb/ -I../external/lekkit/
LIBS = -L../external/sdl/build/lib -lSDL2-2.0 -lGL $(GTK3_LIBS) $(ALSA_LIBS) $(PULSE_LIBS) $(PIPEWIRE_LIBS) $(JACK_LIBS) $(ESD_LIBS) $(SNDIO_LIBS) -lm -ldl -lpthread

# SDL dependency files
SDL_LIB = ../external/sdl/build/lib/libSDL2-2.0.so.0

# Generated headers
LICENSE_HEADER = ../shared/license.h
BOOTROM_HEADER = ../shared/bootrom.h
BOOTROM_SOURCE = ../roms/Kiwi8_logo_2.ch8

# Source files
CORE_SRCS = ../shared/audio.cc ../shared/chip8.cc ../shared/display.cc ../shared/gui.cc \
../shared/input.cc ../shared/main.cc \
../shared/toast.cc ../shared/open_file_dialog.cc ../shared/profiles.cc
IMGUI_SRCS = ../external/imgui/imgui.cpp ../external/imgui/imgui_draw.cpp ../external/imgui/imgui_impl_sdl.cpp
LINUX_SRCS = src/file_dialog.cc
SHA256_SRC = ../external/lekkit/sha256.cc

# Object files (convert source paths to .o in current directory)
OBJS = $(notdir $(CORE_SRCS:.cc=.o)) \
$(notdir $(IMGUI_SRCS:.cpp=.o)) \
$(notdir $(LINUX_SRCS:.cc=.o)) \
$(notdir $(SHA256_SRC:.cc=.o))
OBJS = audio.o \
chip8.o \
display.o \
gui.o \
input.o \
main.o \
toast.o \
open_file_dialog.o \
profiles.o \
imgui.o \
imgui_draw.o \
imgui_impl_sdl.o \
sha256.o \
file_dialog.o

DEPS = $(OBJS:.o=.d)

PROFILES_INI = ../shared/profiles.ini
Expand All @@ -66,7 +87,7 @@ PROFILES_INI = ../shared/profiles.ini

.DEFAULT_GOAL := all

all: debug/profiles.ini debug/$(APP_EXE) release/profiles. release/$(APP_EXE)
all: debug/profiles.ini debug/$(APP_EXE) release/profiles.ini release/$(APP_EXE)

# Build SDL (force rebuild with: make sdl)
sdl:
Expand All @@ -86,16 +107,19 @@ $(BOOTROM_HEADER): $(BOOTROM_SOURCE)

# Pattern rules for incremental compilation
%.o: src/%.cc
$(CC) $(CFLAGS) $(INCS) -c $< -o $@
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

%.o: ../external/imgui/%.cpp
$(CC) $(CFLAGS) $(INCS) -c $< -o $@
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

%.o: ../external/lekkit/%.c
$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

%.o: ../external/lekkit/%.cc
$(CC) $(CFLAGS) $(INCS) -c $< -o $@
%.o: ../shared/%.c $(LICENSE_HEADER) $(BOOTROM_HEADER)
$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

%.o: ../shared/%.cc $(LICENSE_HEADER) $(BOOTROM_HEADER)
$(CC) $(CFLAGS) $(INCS) -c $< -o $@
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

debug/profiles.ini: $(PROFILES_INI)
mkdir -p debug
Expand All @@ -109,13 +133,13 @@ release/profiles.ini: $(PROFILES_INI)
debug/$(APP_EXE): $(SDL_LIB) $(OBJS)
mkdir -p debug
cp $(SDL_LIB) debug/libSDL2-2.0.so.0
$(CC) $(CFLAGS) -g $(INCS) $(LIBDIRS) $(OBJS) -o $@ $(LIBS) $(LFLAGS)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -g $(INCLUDE_DIRS) $(OBJS) -o $@ $(LDFLAGS)

# release executable
release/$(APP_EXE): $(SDL_LIB) $(OBJS)
mkdir -p release
cp $(SDL_LIB) release/libSDL2-2.0.so.0
$(CC) $(CFLAGS) $(INCS) $(LIBDIRS) $(OBJS) -o $@ $(LIBS) $(LFLAGS)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) $(OBJS) -o $@ $(LDFLAGS)

run-debug: debug/$(APP_EXE)
./debug/$(APP_EXE)
Expand Down
74 changes: 47 additions & 27 deletions macos/makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
APP_NAME = Kiwi8

# Use VERSION if set, otherwise fall back to GITHUB_REF_NAME, then default to unknown
# Use VERSION if set, otherwise fall back to GITHUB_REF_NAME, then default to develop
VERSION ?= $(GITHUB_REF_NAME)
ifeq ($(VERSION),)
VERSION = develop
endif

# Use SUB_VERSION if set, otherwise fall back to GITHUB_SHA, then default to unknown
SUB_VERSION ?= $(GITHUB_SHA)
ifeq ($(SUB_VERSION),)
SUB_VERSION = unknown
Expand All @@ -15,33 +16,49 @@ APP_MANIFEST = src/Info.plist
APP_BUNDLE = $(APP_NAME).app
APP_EXE = $(APP_NAME)

CC = clang++
CFLAGS = -std=c++11 -Wall -mmacosx-version-min=11.0 -MMD -MP -DAPP_NAME='"$(APP_NAME)"' -DVERSION='"$(VERSION)"' -DSUB_VERSION='"$(SUB_VERSION)"'
LFLAGS = -mmacosx-version-min=11.0
INCS = -I../external/sdl/build/include/ -I../external/imgui/ -I../external/stb/ -I../external/lekkit/
LIBS = -L../external/sdl/build/lib/ -lSDL2-2.0.0 -framework Cocoa -framework OpenGL
CC = clang
CXX = clang++
CFLAGS = -std=c11
CPPFLAGS = -Wall \
-mmacosx-version-min=11.0 \
-MMD -MP -DAPP_NAME='"$(APP_NAME)"' \
-DVERSION='"$(VERSION)"' \
-DSUB_VERSION='"$(SUB_VERSION)"'

CXXFLAGS = -std=c++11
INCLUDE_DIRS = -I../external/sdl/build/include/ \
-I../external/imgui/ \
-I../external/stb/ \
-I../external/lekkit/

LDFLAGS = -mmacosx-version-min=11.0 \
-L../external/sdl/build/lib/ \
-lSDL2-2.0.0 \
-framework Cocoa \
-framework OpenGL

# SDL dependency files
SDL_LIB = ../external/sdl/build/lib/libSDL2-2.0.0.dylib

# Generated headers
LICENSE_HEADER = ../shared/license.h
BOOTROM_HEADER = ../shared/bootrom.h
BOOTROM_SOURCE = ../roms/Kiwi8_logo_2.ch8

# Source files
CORE_SRCS = ../shared/audio.cc ../shared/chip8.cc ../shared/display.cc ../shared/gui.cc \
../shared/input.cc ../shared/main.cc \
../shared/toast.cc ../shared/open_file_dialog.cc ../shared/profiles.cc
IMGUI_SRCS = ../external/imgui/imgui.cpp ../external/imgui/imgui_draw.cpp ../external/imgui/imgui_impl_sdl.cpp
SHA256_SRC = ../external/lekkit/sha256.cc
MACOS_SRCS = src/file_dialog.mm

# Object files (convert source paths to .o in current directory)
OBJS = $(notdir $(CORE_SRCS:.cc=.o)) \
$(notdir $(IMGUI_SRCS:.cpp=.o)) \
$(notdir $(MACOS_SRCS:.mm=.o)) \
$(notdir $(SHA256_SRC:.cc=.o))
OBJS = audio.o \
chip8.o \
display.o \
gui.o \
input.o \
main.o \
toast.o \
open_file_dialog.o \
profiles.o \
imgui.o \
imgui_draw.o \
imgui_impl_sdl.o \
sha256.o \
file_dialog.o

DEPS = $(OBJS:.o=.d)

PROFILES_INI = ../shared/profiles.ini
Expand Down Expand Up @@ -75,16 +92,19 @@ $(APP_MANIFEST): $(APP_MANIFEST).in

# Pattern rules for incremental compilation
%.o: src/%.mm
$(CC) $(CFLAGS) $(INCS) -c $< -o $@
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

%.o: ../external/imgui/%.cpp
$(CC) $(CFLAGS) $(INCS) -c $< -o $@
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

%.o: ../external/lekkit/%.c
$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

%.o: ../external/lekkit/%.cc
$(CC) $(CFLAGS) $(INCS) -c $< -o $@
%.o: ../shared/%.c $(LICENSE_HEADER) $(BOOTROM_HEADER)
$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

%.o: ../shared/%.cc $(LICENSE_HEADER) $(BOOTROM_HEADER)
$(CC) $(CFLAGS) $(INCS) -c $< -o $@
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) -c $< -o $@

debug/profiles.ini: $(PROFILES_INI)
mkdir -p debug
Expand All @@ -98,7 +118,7 @@ release/$(APP_BUNDLE)/Contents/Resources/profiles.ini: $(PROFILES_INI)
debug/$(APP_EXE): $(SDL_LIB) $(OBJS)
mkdir -p debug
cp $(SDL_LIB) debug/libSDL2-2.0.0.dylib
$(CC) $(CFLAGS) -g $(LIBS) $(INCS) $(OBJS) -o $@ $(LFLAGS)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -g $(INCLUDE_DIRS) $(OBJS) -o $@ $(LDFLAGS)

# Set the dylib's install name to be relative to the bundle
install_name_tool -id @executable_path/libSDL2-2.0.0.dylib debug/libSDL2-2.0.0.dylib
Expand All @@ -109,7 +129,7 @@ debug/$(APP_EXE): $(SDL_LIB) $(OBJS)
# release executable (inside .app bundle)
release/$(APP_BUNDLE)/Contents/MacOS/$(APP_EXE): $(SDL_LIB) $(OBJS)
mkdir -p release/$(APP_BUNDLE)/Contents/MacOS
$(CC) $(CFLAGS) $(LIBS) $(INCS) $(OBJS) -o $@ $(LFLAGS)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDE_DIRS) $(OBJS) -o $@ $(LDFLAGS)

# Fix executable's reference to SDL (from @rpath to bundle-relative path)
install_name_tool -change @rpath/libSDL2-2.0.0.dylib @executable_path/../Frameworks/libSDL2-2.0.0.dylib release/$(APP_BUNDLE)/Contents/MacOS/$(APP_EXE)
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions shared/audio.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#ifndef AUDIO_H
#define AUDIO_H

#ifdef __cplusplus
extern "C" {
#endif

#include <SDL2/SDL.h>
#include <math.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define TAU (2.0 * M_PI)
#define AMPLITUDE 28000 // Max amplitude for signed 16-bit
#define SAMPLE_RATE 48000
Expand All @@ -25,4 +33,8 @@ extern struct audio audio;
void audio_destroy(void);
int audio_init(void);

#ifdef __cplusplus
}
#endif

#endif // AUDIO_H
4 changes: 2 additions & 2 deletions shared/chip8.cc → shared/chip8.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int chip8_init(

/* load fontset */
for(int i = 0; i < FONTS_SIZE; ++i) {
chip8.memory[i] = chip8.chip8_fontset[i];
chip8.memory[i] = chip8_fontset[i];
}

/* Initialize ROM profile database */
Expand Down Expand Up @@ -222,7 +222,7 @@ void chip8_soft_reset() {

/* load fontset */
for(int i = 0; i < FONTS_SIZE; ++i) {
chip8.memory[i] = chip8.chip8_fontset[i];
chip8.memory[i] = chip8_fontset[i];
}

/* copy the entire rom to memory starting from 0x200 */
Expand Down
45 changes: 28 additions & 17 deletions shared/chip8.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#ifndef CHIP8_H
#define CHIP8_H

#ifdef __cplusplus
extern "C" {
#endif

#include "bootrom.h" // Generated at build time from roms/Kiwi8_logo_2.ch8
#include "quirks.h"
#include <stdlib.h>
#include <stdbool.h>

#define MEM_SIZE 4096
#define NUM_REGISTERS 16
Expand All @@ -15,6 +20,24 @@
#define MAX_CYCLES_PER_STEP 50
#define TICKS 60 /* hz - Timer count down rate */

static const unsigned char chip8_fontset[FONTS_SIZE] = {
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0x20, 0x60, 0x20, 0x20, 0x70, // B
0xF0, 0x80, 0xF0, 0x80, 0xF0, // C
0xF0, 0x80, 0xF0, 0x80, 0x80, // D
0xF0, 0x90, 0x90, 0xF0, 0x90 // F
};

struct chip8 {
/* number of cycles per step */
int cycles;
Expand Down Expand Up @@ -63,23 +86,7 @@ struct chip8 {
/* 1-bit encoded screen pixels (64x32) */
unsigned char **vram;

const unsigned char chip8_fontset[FONTS_SIZE] = {
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
0x20, 0x60, 0x20, 0x20, 0x70, // 1
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
0x20, 0x60, 0x20, 0x20, 0x70, // B
0xF0, 0x80, 0xF0, 0x80, 0xF0, // C
0xF0, 0x80, 0xF0, 0x80, 0x80, // D
0xF0, 0x90, 0x90, 0xF0, 0x90 // F
};

};

/* Global chip8 instance */
Expand Down Expand Up @@ -138,4 +145,8 @@ void execFX55(void);
void execFX65(void);
void execUnknown(void);

#ifdef __cplusplus
}
#endif

#endif // CHIP8_H
File renamed without changes.
Loading
Loading