Initial Project Setup

This commit is contained in:
Samuel Walker 2025-01-30 14:30:19 -07:00
parent a47ca7dcbf
commit 7e0b305d17
Signed by: piwalker
GPG Key ID: BE1F84BF85111255
50 changed files with 3227 additions and 0 deletions

1
.gitignore vendored
View File

@ -10,6 +10,7 @@ install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
build/
# ---> C++
# Prerequisites

10
.gitmodules vendored Normal file
View File

@ -0,0 +1,10 @@
[submodule "deps/check"]
path = deps/check
url = https://github.com/libcheck/check.git
[submodule "deps/SDL"]
path = deps/SDL
url = https://github.com/libsdl-org/SDL.git
branch = SDL2
[submodule "deps/SDL_ttf"]
path = deps/SDL_ttf
url = https://github.com/libsdl-org/SDL_ttf.git

16
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/build/gbemu/Debug/gbemu.exe",
"args": [ "${workspaceFolder}/roms/cpu_instrs.gb" ],
"cwd": "${workspaceFolder}"
}
]
}

10
CMakeLists.txt Normal file
View File

@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(deps/SDL)
add_subdirectory(deps/SDL_ttf)
add_subdirectory(lib/)
add_subdirectory(gbemu/)
add_subdirectory(deps/check/)
enable_testing()
add_subdirectory(tests/)
project(top)

66
cmake/FindCheck.cmake Normal file
View File

@ -0,0 +1,66 @@
# - Try to find the CHECK libraries
# Once done this will define
#
# CHECK_FOUND - system has check
# CHECK_INCLUDE_DIR - the check include directory
# CHECK_LIBRARIES - check library
#
# This configuration file for finding libcheck is originally from
# the opensync project. The originally was downloaded from here:
# opensync.org/browser/branches/3rd-party-cmake-modules/modules/FindCheck.cmake
#
# Copyright (c) 2007 Daniel Gollub <dgollub@suse.de>
# Copyright (c) 2007 Bjoern Ricks <b.ricks@fh-osnabrueck.de>
#
# Redistribution and use is allowed according to the terms of the New
# BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#
# Take care about check.pc settings
#
if(WIN32)
SET (CHECK_FOUND 1)
SET (CHECK_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/deps/windows)
SET (CHECK_LIBRARIES ${PROJECT_SOURCE_DIR}/deps/windows/check.lib)
else()
INCLUDE( FindPkgConfig )
PKG_SEARCH_MODULE( CHECK Check )
endif()
# Look for CHECK include dir and libraries
IF( NOT CHECK_FOUND )
IF ( CHECK_INSTALL_DIR )
MESSAGE ( STATUS "Using override CHECK_INSTALL_DIR to find Check" )
SET ( CHECK_INCLUDE_DIR "${CHECK_INSTALL_DIR}/include" )
SET ( CHECK_INCLUDE_DIRS "${CHECK_INCLUDE_DIR}" )
FIND_LIBRARY( CHECK_LIBRARY NAMES check PATHS "${CHECK_INSTALL_DIR}/lib" )
FIND_LIBRARY( COMPAT_LIBRARY NAMES compat PATHS "${CHECK_INSTALL_DIR}/lib" )
SET ( CHECK_LIBRARIES "${CHECK_LIBRARY}" "${COMPAT_LIBRARY}" )
ELSE ( CHECK_INSTALL_DIR )
FIND_PATH( CHECK_INCLUDE_DIR check.h )
FIND_LIBRARY( CHECK_LIBRARIES NAMES check )
ENDIF ( CHECK_INSTALL_DIR )
IF ( CHECK_INCLUDE_DIR AND CHECK_LIBRARIES )
SET( CHECK_FOUND 1 )
IF ( NOT Check_FIND_QUIETLY )
MESSAGE ( STATUS "Found CHECK: ${CHECK_LIBRARIES}" )
ENDIF ( NOT Check_FIND_QUIETLY )
ELSE ( CHECK_INCLUDE_DIR AND CHECK_LIBRARIES )
IF ( Check_FIND_REQUIRED )
MESSAGE( FATAL_ERROR "Could NOT find CHECK" )
ELSE ( Check_FIND_REQUIRED )
IF ( NOT Check_FIND_QUIETLY )
MESSAGE( STATUS "Could NOT find CHECK" )
ENDIF ( NOT Check_FIND_QUIETLY )
ENDIF ( Check_FIND_REQUIRED )
ENDIF ( CHECK_INCLUDE_DIR AND CHECK_LIBRARIES )
ENDIF( NOT CHECK_FOUND )
# Hide advanced variables from CMake GUIs
MARK_AS_ADVANCED( CHECK_INCLUDE_DIR CHECK_LIBRARIES )

1
deps/SDL vendored Submodule

@ -0,0 +1 @@
Subproject commit 934d6954e21a816c1b28f8499e5b7d976cecd2b8

1
deps/SDL_ttf vendored Submodule

@ -0,0 +1 @@
Subproject commit 1dc162336f680c459aadef299b02c099a5533b34

1
deps/check vendored Submodule

@ -0,0 +1 @@
Subproject commit 455005dc29dc6727de7ee36fee4b49a13b39f73f

2366
deps/windows/check.h vendored Normal file

File diff suppressed because it is too large Load Diff

39
deps/windows/check_error.h vendored Normal file
View File

@ -0,0 +1,39 @@
/*
* Check: a unit test framework for C
* Copyright (C) 2001, 2002 Arien Malec
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef ERROR_H
#define ERROR_H
#include "../lib/libcompat.h"
#include <setjmp.h>
extern jmp_buf error_jmp_buffer;
/* Include stdlib.h beforehand */
/* Print error message and die
If fmt ends in colon, include system error information */
void eprintf(const char *fmt, const char *file, int line,
...) CK_ATTRIBUTE_NORETURN CK_ATTRIBUTE_FORMAT(printf, 1, 4);
/* malloc or die */
void *emalloc(size_t n);
void *erealloc(void *, size_t n);
#endif /*ERROR_H */

140
deps/windows/check_impl.h vendored Normal file
View File

@ -0,0 +1,140 @@
/*
* Check: a unit test framework for C
* Copyright (C) 2001, 2002 Arien Malec
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef CHECK_IMPL_H
#define CHECK_IMPL_H
/* This header should be included by any module that needs
to know the implementation details of the check structures
Include stdio.h, time.h, & list.h before this header
*/
#define US_PER_SEC 1000000
#define NANOS_PER_SECONDS 1000000000
/** calculate the difference in useconds out of two "struct timespec"s */
#define DIFF_IN_USEC(begin, end) \
( (((end).tv_sec - (begin).tv_sec) * US_PER_SEC) + \
((end).tv_nsec/1000) - ((begin).tv_nsec/1000) )
typedef struct TF
{
const TTest * ttest;
int loop_start;
int loop_end;
int signal;
signed char allowed_exit_value;
} TF;
struct Suite
{
const char *name;
List *tclst; /* List of test cases */
};
typedef struct Fixture
{
int ischecked;
SFun fun;
} Fixture;
struct TCase
{
const char *name;
struct timespec timeout;
List *tflst; /* list of test functions */
List *unch_sflst;
List *unch_tflst;
List *ch_sflst;
List *ch_tflst;
List *tags;
};
typedef struct TestStats
{
int n_checked;
int n_failed;
int n_errors;
} TestStats;
struct TestResult
{
enum test_result rtype; /* Type of result */
enum ck_result_ctx ctx; /* When the result occurred */
char *file; /* File where the test occured */
int line; /* Line number where the test occurred */
int iter; /* The iteration value for looping tests */
int duration; /* duration of this test in microseconds */
const char *tcname; /* Test case that generated the result */
const char *tname; /* Test that generated the result */
char *msg; /* Failure message */
};
TestResult *tr_create(void);
void tr_reset(TestResult * tr);
void tr_free(TestResult * tr);
enum cl_event
{
CLINITLOG_SR, /* Initialize log file */
CLENDLOG_SR, /* Tests are complete */
CLSTART_SR, /* Suite runner start */
CLSTART_S, /* Suite start */
CLEND_SR, /* Suite runner end */
CLEND_S, /* Suite end */
CLSTART_T, /* A test case is about to run */
CLEND_T /* Test case end */
};
typedef void (*LFun) (SRunner *, FILE *, enum print_output,
void *, enum cl_event);
typedef struct Log
{
FILE *lfile;
LFun lfun;
int close;
enum print_output mode;
} Log;
struct SRunner
{
List *slst; /* List of Suite objects */
TestStats *stats; /* Run statistics */
List *resultlst; /* List of unit test results */
const char *log_fname; /* name of log file */
const char *xml_fname; /* name of xml output file */
const char *tap_fname; /* name of tap output file */
List *loglst; /* list of Log objects */
enum fork_status fstat; /* controls if suites are forked or not
NOTE: Don't use this value directly,
instead use srunner_fork_status */
};
void set_fork_status(enum fork_status fstat);
enum fork_status cur_fork_status(void);
clockid_t check_get_clockid(void);
unsigned int tcase_matching_tag(TCase *tc, List *check_for);
List *tag_string_to_list(const char *tags_string);
#endif /* CHECK_IMPL_H */

59
deps/windows/check_list.h vendored Normal file
View File

@ -0,0 +1,59 @@
/*
* Check: a unit test framework for C
* Copyright (C) 2001, 2002 Arien Malec
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef CHECK_LIST_H
#define CHECK_LIST_H
typedef struct List List;
/* Create an empty list */
List *check_list_create(void);
/* Is list at end? */
int check_list_at_end(List * lp);
/* Position list at front */
void check_list_front(List * lp);
/* Add a value to the front of the list,
positioning newly added value as current value.
More expensive than list_add_end, as it uses memmove. */
void check_list_add_front(List * lp, void *val);
/* Add a value to the end of the list,
positioning newly added value as current value */
void check_list_add_end(List * lp, void *val);
/* Give the value of the current node */
void *check_list_val(List * lp);
/* Position the list at the next node */
void check_list_advance(List * lp);
/* Free a list, but don't free values */
void check_list_free(List * lp);
void check_list_apply(List * lp, void (*fp) (void *));
/* Return true if the list contains the value, false otherwise */
int check_list_contains(List * lp, void *val);
#endif /* CHECK_LIST_H */

55
deps/windows/check_log.h vendored Normal file
View File

@ -0,0 +1,55 @@
/*
* Check: a unit test framework for C
* Copyright (C) 2001, 2002 Arien Malec
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef CHECK_LOG_H
#define CHECK_LOG_H
void log_srunner_start(SRunner * sr);
void log_srunner_end(SRunner * sr);
void log_suite_start(SRunner * sr, Suite * s);
void log_suite_end(SRunner * sr, Suite * s);
void log_test_end(SRunner * sr, TestResult * tr);
void log_test_start(SRunner * sr, TCase * tc, TF * tfun);
void stdout_lfun(SRunner * sr, FILE * file, enum print_output,
void *obj, enum cl_event evt);
void lfile_lfun(SRunner * sr, FILE * file, enum print_output,
void *obj, enum cl_event evt);
void xml_lfun(SRunner * sr, FILE * file, enum print_output,
void *obj, enum cl_event evt);
void tap_lfun(SRunner * sr, FILE * file, enum print_output,
void *obj, enum cl_event evt);
void subunit_lfun(SRunner * sr, FILE * file, enum print_output,
void *obj, enum cl_event evt);
void srunner_register_lfun(SRunner * sr, FILE * lfile, int close,
LFun lfun, enum print_output);
FILE *srunner_open_lfile(SRunner * sr);
FILE *srunner_open_xmlfile(SRunner * sr);
FILE *srunner_open_tapfile(SRunner * sr);
void srunner_init_logging(SRunner * sr, enum print_output print_mode);
void srunner_end_logging(SRunner * sr);
#endif /* CHECK_LOG_H */

39
deps/windows/check_msg.h vendored Normal file
View File

@ -0,0 +1,39 @@
/*
* Check: a unit test framework for C
* Copyright (C) 2001, 2002 Arien Malec
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef CHECK_MSG_NEW_H
#define CHECK_MSG_NEW_H
/* Functions implementing messaging during test runs */
void send_failure_info(const char *msg);
void send_loc_info(const char *file, int line);
void send_ctx_info(enum ck_result_ctx ctx);
void send_duration_info(int duration);
TestResult *receive_test_result(int waserror);
void setup_messaging(void);
void teardown_messaging(void);
FILE *open_tmp_file(char **name);
#endif /*CHECK_MSG_NEW_H */

84
deps/windows/check_pack.h vendored Normal file
View File

@ -0,0 +1,84 @@
/*
* Check: a unit test framework for C
* Copyright (C) 2001, 2002 Arien Malec
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef CHECK_PACK_H
#define CHECK_PACK_H
enum ck_msg_type
{
CK_MSG_CTX,
CK_MSG_FAIL,
CK_MSG_LOC,
CK_MSG_DURATION,
CK_MSG_LAST
};
typedef struct CtxMsg
{
enum ck_result_ctx ctx;
} CtxMsg;
typedef struct LocMsg
{
int line;
char *file;
} LocMsg;
typedef struct FailMsg
{
char *msg;
} FailMsg;
typedef struct DurationMsg
{
int duration;
} DurationMsg;
typedef union
{
CtxMsg ctx_msg;
FailMsg fail_msg;
LocMsg loc_msg;
DurationMsg duration_msg;
} CheckMsg;
typedef struct RcvMsg
{
enum ck_result_ctx lastctx;
enum ck_result_ctx failctx;
char *fixture_file;
int fixture_line;
char *test_file;
int test_line;
char *msg;
int duration;
} RcvMsg;
void rcvmsg_free(RcvMsg * rmsg);
int pack(enum ck_msg_type type, char **buf, CheckMsg * msg);
int upack(char *buf, CheckMsg * msg, enum ck_msg_type *type);
void ppack(FILE * fdes, enum ck_msg_type type, CheckMsg * msg);
RcvMsg *punpack(FILE * fdes);
#endif /*CHECK_PACK_H */

32
deps/windows/check_print.h vendored Normal file
View File

@ -0,0 +1,32 @@
/*
* Check: a unit test framework for C
* Copyright (C) 2001, 2002 Arien Malec
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef CHECK_PRINT_H
#define CHECK_PRINT_H
/* escape XML special characters (" ' < > &) in str and print to file */
void fprint_xml_esc(FILE * file, const char *str);
void tr_fprint(FILE * file, TestResult * tr, enum print_output print_mode);
void tr_xmlprint(FILE * file, TestResult * tr, enum print_output print_mode);
void srunner_fprint(FILE * file, SRunner * sr, enum print_output print_mode);
enum print_output get_env_printmode(void);
#endif /* CHECK_PRINT_H */

41
deps/windows/check_stdint.h vendored Normal file
View File

@ -0,0 +1,41 @@
/*-*- mode:C; -*- */
/*
* Check: a unit test framework for C
*
* Copyright (C) 2013 Branden Archer
* Copyright (C) 2019 Mikko Johannes Koivunalho
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef _CHECK_CHECK_STDINT_H
#define _CHECK_CHECK_STDINT_H 1
#ifndef _GENERATED_STDINT_H
#define _GENERATED_STDINT_H "Check 0.15.2"
/* generated using CMake 3.20.2 from file cmake/check_stdint.h.in */
/* Imported CMake variables created during build. */
#define HAVE_STDINT_H 1
#ifdef HAVE_STDINT_H
#define _STDINT_HAVE_STDINT_H 1
#include <stdint.h>
#undef HAVE_STDINT_H
#endif /* defined HAVE_STDINT_H */
/* Define only once */
#endif /* _GENERATED_STDINT_H */
#endif /* _CHECK_CHECK_STDINT_H */

44
deps/windows/check_str.h vendored Normal file
View File

@ -0,0 +1,44 @@
/*
* Check: a unit test framework for C
* Copyright (C) 2001, 2002 Arien Malec
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef CHECK_STR_H
#define CHECK_STR_H
#include "../lib/libcompat.h"
/* Return a string representation of the given TestResult. Return
value has been malloc'd, and must be freed by the caller */
char *tr_str(TestResult * tr);
/* Return a string representation of the given TestResult message
without the test id or result type. This is suitable for separate
formatting of the test and the message. Return value has been
malloc'd, and must be freed by the caller */
char *tr_short_str(TestResult * tr);
/* Return a string representation of the given SRunner's run
statistics (% passed, num run, passed, errors, failures). Return
value has been malloc'd, and must be freed by the caller
*/
char *sr_stat_str(SRunner * sr);
char *ck_strdup_printf(const char *fmt, ...) CK_ATTRIBUTE_FORMAT(printf, 1, 2);
#endif /* CHECK_STR_H */

6
gbemu/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(gbemu)
FILE(GLOB SRCS *.c)
add_executable(gbemu ${SRCS})
target_include_directories(gbemu PUBLIC ../include)
target_link_libraries(gbemu PUBLIC Lib)

5
gbemu/main.c Normal file
View File

@ -0,0 +1,5 @@
#include <emu.h>
int main(int argc, char **argv) {
return emu_run(argc, argv);
}

6
include/bus.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <common.h>
u8 bus_read(u16 address);
void bus_write(u16 address, u8 value);

22
include/cart.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <common.h>
typedef struct {
u8 entry[4];
u8 logo[0x30];
char title[16];
u16 new_lic_code;
u8 sgb_flag;
u8 type;
u8 rom_size;
u8 ram_size;
u8 dest_code;
u8 lic_code;
u8 version;
u8 checksum;
u16 global_checksum;
} rom_header;
bool cart_load(char *cart);

17
include/common.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
#define BIT(a, n) ((a & (1 << n)) ? 1 : 0)
#define BIT_SET(a, n, on) (on ? (a) |= (1 << n) : (a) &= !(1 << n))
#define BETWEEN(a, b, c) ((a >= b) && (a <= c))
void delay(u32 ms);

6
include/cpu.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <common.h>
void cpu_init();
bool cpu_step();

12
include/emu.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <common.h>
typedef struct {
bool paused;
bool running;
u64 ticks;
} emu_context;
int emu_run(int, char**);
emu_context *emu_get_context();

6
include/ppu.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <common.h>
void ppu_init();
void ppu_tick();

6
include/timer.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <common.h>
void timer_init();
void timer_tick();

6
lib/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(Lib)
file(GLOB SRCS *.c)
add_library(Lib STATIC ${SRCS})
target_include_directories(Lib PUBLIC ../include ../deps/SDL/include ../deps/SDL_ttf)
target_link_libraries(Lib SDL2-static SDL2_ttf::SDL2_ttf-static)

0
lib/bus.c Normal file
View File

6
lib/cart.c Normal file
View File

@ -0,0 +1,6 @@
#include <cart.h>
bool cart_load(char *cart) {
printf("Cart loading not implemented yet\n");
return false;
}

10
lib/cpu.c Normal file
View File

@ -0,0 +1,10 @@
#include <cpu.h>
void cpu_init() {
}
bool cpu_step() {
printf("Cpu not yet implemented.\n");
return false;
}

57
lib/emu.c Normal file
View File

@ -0,0 +1,57 @@
#include <emu.h>
#include <stdio.h>
#include <cart.h>
#include <cpu.h>
#include <SDL.h>
#include <SDL_ttf.h>
static emu_context ctx;
emu_context *emu_get_context() {
return &ctx;
}
void delay(u32 ms) {
SDL_Delay(ms);
}
int emu_run(int argc, char **argv) {
if (argc < 2) {
printf("Usage: gbemu <rom_file>\n");
return -1;
}
if(!cart_load(argv[1])) {
printf("Failed to load ROM file: %s\n", argv[1]);
return -2;
}
printf("Cart loaded..\n");
SDL_Init(SDL_INIT_VIDEO);
printf("SDL INIT\n");
TTF_Init();
printf("TTF INIT\n");
cpu_init();
ctx.running = true;
ctx.paused = false;
ctx.ticks = 0;
while (ctx.running) {
if (ctx.paused) {
delay(10);
continue;
}
if (!cpu_step()) {
printf("CPU stopped\n");
return -3;
}
ctx.ticks++;
}
return 0;
}

9
lib/ppu.c Normal file
View File

@ -0,0 +1,9 @@
#include <ppu.h>
void ppu_init() {
}
void ppu_tick() {
}

10
lib/timer.c Normal file
View File

@ -0,0 +1,10 @@
#include <timer.h>
void timer_init() {
}
void timer_tick() {
}

BIN
roms/01-special.gb Normal file

Binary file not shown.

BIN
roms/02-interrupts.gb Normal file

Binary file not shown.

BIN
roms/03-op sp,hl.gb Normal file

Binary file not shown.

BIN
roms/04-op r,imm.gb Normal file

Binary file not shown.

BIN
roms/05-op rp.gb Normal file

Binary file not shown.

BIN
roms/06-ld r,r.gb Normal file

Binary file not shown.

Binary file not shown.

BIN
roms/08-misc instrs.gb Normal file

Binary file not shown.

BIN
roms/09-op r,r.gb Normal file

Binary file not shown.

BIN
roms/10-bit ops.gb Normal file

Binary file not shown.

BIN
roms/11-op a,(hl).gb Normal file

Binary file not shown.

BIN
roms/cpu_instrs.gb Normal file

Binary file not shown.

BIN
roms/dmg-acid2.gb Normal file

Binary file not shown.

BIN
roms/mem_timing.gb Normal file

Binary file not shown.

7
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(tests)
FILE(GLOB SRCS *.c)
add_executable(tests ${SRCS})
target_include_directories(tests PUBLIC ../include ../build/deps/check/src ../build/deps/check)
target_link_libraries(tests PUBLIC Lib check)
add_test(NAME mytests COMMAND tests)

31
tests/test.c Normal file
View File

@ -0,0 +1,31 @@
#include <check.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <emu.h>
#include <cpu.h>
START_TEST(test_nothing) {
bool b = cpu_step();
ck_assert_uint_eq(b, false);
} END_TEST
Suite *stack_suite() {
Suite *s = suite_create("emu");
TCase *tc = tcase_create("core");
tcase_add_test(tc, test_nothing);
suite_add_tcase(s, tc);
return s;
}
int main(int argc, char **argv) {
Suite *s = stack_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
int nf = srunner_ntests_failed(sr);
srunner_free(sr);
return nf == 0 ? 0 : -1;
}