diff --git a/Makefile b/Makefile index d911607..4f38aea 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,12 @@ UNAME := $(shell uname) ifeq ($(UNAME),Linux) CC=gcc -elf_i386 + CXX=g++ -elf_i386 AS=as --32 LD=ld -m elf_i386 else CC=i386-elf-gcc + CXX=i386-elf-g++ AS=i386-elf-as LD=i386-elf-ld endif @@ -15,6 +17,10 @@ CCFLAGS=-m32 -std=c11 -O2 -g -Wall -Wextra -Wpedantic -Wstrict-aliasing CCFLAGS+=-Wno-pointer-arith -Wno-unused-parameter CCFLAGS+=-nostdlib -nostdinc -ffreestanding -fno-pie -fno-stack-protector CCFLAGS+=-fno-builtin-function -fno-builtin +CXXFLAGS=-m32 -std=c++2a -O2 -g -Wall -Wextra -Wpedantic -Wstrict-aliasing +CXXFLAGS+=-Wno-pointer-arith -Wno-unused-parameter +CXXFLAGS+=-lstdc++ -ffreestanding -fno-pie -fno-stack-protector +CXXFLAGS+=-fno-rtti -fno-exceptions ASFLAGS= LDFLAGS= @@ -24,8 +30,9 @@ BOOTSECT_SRCS=\ BOOTSECT_OBJS=$(BOOTSECT_SRCS:.S=.o) KERNEL_C_SRCS=$(wildcard src/*.c) +KERNEL_CXX_SRCS=$(wildcard src/*.cpp) KERNEL_S_SRCS=$(filter-out $(BOOTSECT_SRCS), $(wildcard src/*.S)) -KERNEL_OBJS=$(KERNEL_C_SRCS:.c=.o) $(KERNEL_S_SRCS:.S=.o) +KERNEL_OBJS=$(KERNEL_C_SRCS:.c=.o) $(KERNEL_CXX_SRCS:.cpp=.o) $(KERNEL_S_SRCS:.S=.o) BOOTSECT=bootsect.bin KERNEL=kernel.bin @@ -42,6 +49,9 @@ clean: %.o: %.c $(CC) -o $@ -c $< $(GFLAGS) $(CCFLAGS) +%.o: %.cpp + $(CXX) -o $@ -c $< $(GFLAGS) $(CXXFLAGS) + %.o: %.S $(AS) -o $@ -c $< $(GFLAGS) $(ASFLAGS) diff --git a/src/font.h b/src/font.h index 9e695e4..820c1db 100644 --- a/src/font.h +++ b/src/font.h @@ -4,6 +4,10 @@ #include "util.h" #include "screen.h" +#ifdef __cplusplus +extern "C" { +#endif + #define font_width(_s) (strlen((_s)) * 8) #define font_height() (8) #define font_str_doubled(_s, _x, _y, _c) do {\ @@ -18,4 +22,8 @@ void font_char(char c, size_t x, size_t y, u8 color); void font_str(const char *s, size_t x, size_t y, u8 color); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/fpu.h b/src/fpu.h index 5b1fa7b..6a87047 100644 --- a/src/fpu.h +++ b/src/fpu.h @@ -3,6 +3,14 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + void fpu_init(); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/idt.h b/src/idt.h index 9f4ca2e..bd46a1a 100644 --- a/src/idt.h +++ b/src/idt.h @@ -4,7 +4,15 @@ #include "util.h" #include "isr.h" +#ifdef __cplusplus +extern "C" { +#endif + void idt_set(u8 index, void (*base)(struct Registers*), u16 selector, u8 flags); void idt_init(); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/irq.h b/src/irq.h index 27a23b6..385f79e 100644 --- a/src/irq.h +++ b/src/irq.h @@ -4,7 +4,15 @@ #include "util.h" #include "isr.h" +#ifdef __cplusplus +extern "C" { +#endif + void irq_install(size_t i, void (*handler)(struct Registers*)); void irq_init(); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/isr.h b/src/isr.h index 0507ee8..38f4449 100644 --- a/src/isr.h +++ b/src/isr.h @@ -3,6 +3,10 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + struct Registers { u32 __ignored, fs, es, ds; u32 edi, esi, ebp, esp, ebx, edx, ecx, eax; @@ -13,4 +17,8 @@ struct Registers { void isr_install(size_t i, void (*handler)(struct Registers*)); void isr_init(); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/keyboard.h b/src/keyboard.h index 2beceb2..5f86ab1 100644 --- a/src/keyboard.h +++ b/src/keyboard.h @@ -3,6 +3,10 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + // TODO: some of this it 100% wrong lmao #define KEY_NULL 0 #define KEY_ESC 27 @@ -84,4 +88,8 @@ extern struct Keyboard keyboard; void keyboard_init(); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/main.c b/src/main.cpp similarity index 96% rename from src/main.c rename to src/main.cpp index acd9211..ac795e5 100644 --- a/src/main.c +++ b/src/main.cpp @@ -1,5 +1,8 @@ // remove to disable music, useful when building for hardware without an SB16 -#define ENABLE_MUSIC +//#define ENABLE_MUSIC + +#include +#include #include "util.h" #include "screen.h" @@ -18,18 +21,17 @@ #include "music.h" #endif -#define FPS 30 -#define LEVELS 30 +constexpr auto FPS = 30; +constexpr auto LEVELS = 30; -#define TILE_SIZE 10 +constexpr auto TILE_SIZE = 10; -#define LOGO_HEIGHT 5 -static const char *LOGO[LOGO_HEIGHT] = { - "AAA BBB CCC DD EEE FFF", - " A B C D D E F ", - " A BBB C DD E FFF", - " A B C D D E F", - " A BBB C D D EEE FFF", +constexpr std::array LOGO = { + "AAA D D E ", + "A B C D D D E ", + "A BBB CCC D D E ", + "A B C D D ", + "AAA D E ", }; #define NUM_TILES (BORDER + 1) @@ -368,8 +370,8 @@ static bool rotate(bool right) { } static void generate_sprites() { - for (enum Tile t = 0; t < NUM_TILES; t++) { - if (t == NONE) { + for (u8 t = 0; t < NUM_TILES; t++) { + if (Tile(t) == NONE) { continue; } @@ -421,7 +423,7 @@ static void render_board() { for (size_t y = 0; y < BOARD_HEIGHT; y++) { for (size_t x = 0; x < BOARD_WIDTH; x++) { u8 data = state.board[y][x]; - enum Tile tile = data & TILE_MASK; + auto tile = Tile(data & TILE_MASK); size_t xs = BOARD_X + (x * TILE_SIZE), ys = BOARD_Y + (y * TILE_SIZE); @@ -662,10 +664,10 @@ void render_menu() { for (i32 x = -1; x < (i32) logo_width + 1; x++) { render_tile(BORDER, logo_x + (x * TILE_SIZE), logo_y - (TILE_SIZE * 2)); - render_tile(BORDER, logo_x + (x * TILE_SIZE), logo_y + (TILE_SIZE * (1 + LOGO_HEIGHT))); + render_tile(BORDER, logo_x + (x * TILE_SIZE), logo_y + (TILE_SIZE * (1 + std::size(LOGO)))); } - for (size_t y = 0; y < LOGO_HEIGHT; y++) { + for (size_t y = 0; y < std::size(LOGO); y++) { for (size_t x = 0; x < logo_width; x++) { char c = LOGO[y][x]; @@ -674,7 +676,7 @@ void render_menu() { } render_tile( - GREEN + ((((state.frames / 10) + (6 - (c - 'A'))) / 6) % 8), + Tile(GREEN + ((((state.frames / 10) + (6 - (c - 'A'))) / 6) % 8)), logo_x + (x * TILE_SIZE), logo_y + (y * TILE_SIZE) ); @@ -685,14 +687,14 @@ void render_menu() { font_str_doubled( play, (SCREEN_WIDTH - font_width(play)) / 2, - logo_y + ((LOGO_HEIGHT + 6) * TILE_SIZE), + logo_y + ((std::size(LOGO) + 6) * TILE_SIZE), (state.frames / 6) % 2 == 0 ? COLOR(6, 6, 2) : COLOR(7, 7, 3) ); } -void _main(u32 magic) { +extern "C" void _main(u32 magic) { idt_init(); isr_init(); fpu_init(); diff --git a/src/math.h b/src/math.h index dbf1a6f..203a3ff 100644 --- a/src/math.h +++ b/src/math.h @@ -3,6 +3,10 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + #define E 2.71828 #define PI 3.14159265358979323846264338327950 @@ -12,4 +16,8 @@ f64 sin(f64 x); f64 cos(f64 x); f64 pow(f64 x, f64 y); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/music.h b/src/music.h index fd28a90..f92df46 100644 --- a/src/music.h +++ b/src/music.h @@ -3,7 +3,15 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + void music_tick(); void music_init(); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/screen.h b/src/screen.h index ddee993..0a4776f 100644 --- a/src/screen.h +++ b/src/screen.h @@ -3,6 +3,10 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 200 #define SCREEN_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT) @@ -50,4 +54,8 @@ void screen_swap(); void screen_clear(u8 color); void screen_init(); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/sound.h b/src/sound.h index d1f3e95..4d69157 100644 --- a/src/sound.h +++ b/src/sound.h @@ -3,6 +3,10 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + #define NUM_NOTES 8 #define NUM_OCTAVES 7 @@ -47,4 +51,8 @@ void sound_master(u8 v); void sound_volume(u8 index, u8 v); void sound_wave(u8 index, u8 wave); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/speaker.h b/src/speaker.h index 464c370..4eb66d2 100644 --- a/src/speaker.h +++ b/src/speaker.h @@ -3,8 +3,16 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + void speaker_note(u8 octave, u8 note); void speaker_play(u32 hz); void speaker_pause(); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/system.h b/src/system.h index d7eed35..4e64ab8 100644 --- a/src/system.h +++ b/src/system.h @@ -3,6 +3,10 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + #define _assert_0() __error_illegal_macro__ #define _assert_1(_e) do { if (!(_e)) panic(NULL); } while (0) #define _assert_2(_e, _m) do { if (!(_e)) panic((_m)); } while (0) @@ -18,4 +22,8 @@ void panic(const char *err); u32 rand(); void seed(u32 s); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/timer.h b/src/timer.h index 0980c0e..30ee81c 100644 --- a/src/timer.h +++ b/src/timer.h @@ -3,10 +3,18 @@ #include "util.h" +#ifdef __cplusplus +extern "C" { +#endif + // number chosen to be integer divisor of PIC frequency #define TIMER_TPS 363 u64 timer_get(); void timer_init(); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/util.h b/src/util.h index 87373c4..5f56a73 100644 --- a/src/util.h +++ b/src/util.h @@ -1,6 +1,10 @@ #ifndef UTIL_H #define UTIL_H +#ifdef __cplusplus +extern "C" { +#endif + // fixed width integer types typedef unsigned char u8; typedef unsigned short u16; @@ -15,9 +19,11 @@ typedef u32 uintptr_t; typedef float f32; typedef double f64; +#ifndef __cplusplus typedef u8 bool; #define true (1) #define false (0) +#endif #define NULL (0) @@ -121,7 +127,7 @@ static inline char *itoa(i32 x, char *s, size_t sz) { } static inline void memset(void *dst, u8 value, size_t n) { - u8 *d = dst; + u8 *d = (u8 *)dst; while (n-- > 0) { *d++ = value; @@ -129,8 +135,8 @@ static inline void memset(void *dst, u8 value, size_t n) { } static inline void *memcpy(void *dst, const void *src, size_t n) { - u8 *d = dst; - const u8 *s = src; + u8 *d = (u8 *)dst; + const u8 *s = (const u8 *)src; while (n-- > 0) { *d++ = *s++; @@ -145,8 +151,8 @@ static inline void *memmove(void *dst, const void *src, size_t n) { return memcpy(dst, src, n); } - u8 *d = dst; - const u8 *s = src; + u8 *d = (u8 *)dst; + const u8 *s = (const u8 *)src; for (size_t i = n; i > 0; i--) { d[i - 1] = s[i - 1]; @@ -199,4 +205,8 @@ static inline size_t strlcpy(char *dst, const char *src, size_t n) { return s - src - 1; } +#ifdef __cplusplus +} +#endif + #endif