Initial commit

This commit is contained in:
Lycoder
2023-05-26 00:54:14 -03:00
commit babfdd8423
13 changed files with 1225 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
.vscode/
bin/
build-win32/
imgui/
res/
sdl2/
*.bin
*.dll
*.exe
+2
View File
@@ -0,0 +1,2 @@
rmdir -Recurse build-win32
rmdir -Recurse bin
+26
View File
@@ -0,0 +1,26 @@
git clone https://github.com/ocornut/imgui
$IMGUI_DIR = "..\imgui"
$SDL2_DIR = "..\sdl2\x86_64-w64-mingw32"
mkdir -Force -Path build-win32 > $null
Set-Location build-win32
Write-Output "Building ImGui..."
c++ -I"`"$($IMGUI_DIR)`"" `
-I"`"$($IMGUI_DIR)\backends`"" `
-I"`"$($SDL2_DIR)\include\SDL2`"" `
"`"$($IMGUI_DIR)\backends\imgui_impl_sdl2.cpp`"" `
"`"$($IMGUI_DIR)\backends\imgui_impl_sdlrenderer.cpp`"" `
"`"$($IMGUI_DIR)\*.cpp`"" `
-c -m64
Set-Location ..
Write-Output "Building debugger fonts..."
ld -r -b binary -o "build-win32\font.o" "res\UbuntuMono-Regular.ttf"
gcc -c psx/log.c
+15
View File
@@ -0,0 +1,15 @@
$SDL2_DIR = "sdl2\x86_64-w64-mingw32"
$PSX_DIR = "."
md -Force -Path bin > $null
gcc -I"`"$($PSX_DIR)`"" `
-I"`"$($SDL2_DIR)\include`"" `
-I"`"$($SDL2_DIR)\include\SDL2`"" `
"build-win32\*.o" `
"psx\*.c" `
"main.c" `
-o "bin\psxe.exe" `
-L"`"$($SDL2_DIR)\lib`"" `
-m64 -lSDL2main -lSDL2 `
-g -O3 -Wall -pedantic -DLOG_USE_COLOR
+28
View File
@@ -0,0 +1,28 @@
#include "psx/bios.h"
#include "psx/cpu.h"
#include "psx/log.h"
int main() {
log_set_level(LOG_TRACE);
psx_bios_t* bios = psx_bios_create();
psx_bios_init(bios);
psx_bios_load(bios, "scph1001.bin");
psx_bus_t* bus = psx_bus_create();
psx_bus_init(bus, bios);
psx_cpu_t* cpu = psx_cpu_create();
psx_cpu_init(cpu, bus);
while (true) {
psx_cpu_cycle(cpu);
}
psx_cpu_destroy(cpu);
psx_bios_destroy(bios);
psx_bus_destroy(bus);
}
+55
View File
@@ -0,0 +1,55 @@
#include "bios.h"
#include "log.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
psx_bios_t* psx_bios_create() {
return (psx_bios_t*)malloc(sizeof(psx_bios_t));
}
void psx_bios_init(psx_bios_t* bios) {
bios->buf = (uint8_t*)malloc(PSX_BIOS_SIZE);
}
void psx_bios_load(psx_bios_t* bios, const char* path) {
FILE* file = fopen(path, "rb");
fread(bios->buf, 1, PSX_BIOS_SIZE, file);
#ifdef PSXE_DEBUG_BIOS
log_info("Loaded BIOS file \"%s\"", path);
#endif
fclose(file);
}
uint32_t psx_bios_read32(psx_bios_t* bios, uint32_t offset) {
#ifdef PSXE_DEBUG_BIOS
//log_debug("32-bit read at BIOS address %08x", offset);
#endif
return *((uint32_t*)(bios->buf + offset));
}
uint16_t psx_bios_read16(psx_bios_t* bios, uint32_t offset) {
#ifdef PSXE_DEBUG_BIOS
//log_debug("16-bit read at BIOS address %08x", offset);
#endif
return *((uint16_t*)(bios->buf + offset));
}
uint8_t psx_bios_read8(psx_bios_t* bios, uint32_t offset) {
#ifdef PSXE_DEBUG_BIOS
//log_debug("8-bit read at BIOS address %08x", offset);
#endif
return bios->buf[offset];
}
void psx_bios_destroy(psx_bios_t* bios) {
free(bios->buf);
free(bios);
}
+28
View File
@@ -0,0 +1,28 @@
#ifndef BIOS_H
#define BIOS_H
#include <stdint.h>
#define PSXE_DEBUG_BIOS 1
#ifdef PSXE_DEBUG_BIOS
#include "log.h"
#endif
#define PSX_BIOS_SIZE 0x80000
#define PSX_BIOS_BEGIN 0xbfc00000
#define PSX_BIOS_END 0xbfc7ffff
typedef struct {
uint8_t* buf;
} psx_bios_t;
psx_bios_t* psx_bios_create();
void psx_bios_init(psx_bios_t*);
void psx_bios_load(psx_bios_t*, const char*);
uint32_t psx_bios_read32(psx_bios_t*, uint32_t);
uint16_t psx_bios_read16(psx_bios_t*, uint32_t);
uint8_t psx_bios_read8(psx_bios_t*, uint32_t);
void psx_bios_destroy(psx_bios_t*);
#endif
+83
View File
@@ -0,0 +1,83 @@
#include <stdint.h>
#include <stdlib.h>
#include "bus.h"
#include "log.h"
#define RANGE(v, s, e) ((v >= s) && (v <= e))
psx_bus_t* psx_bus_create() {
return (psx_bus_t*)malloc(sizeof(psx_bus_t));
}
void psx_bus_init(psx_bus_t* bus, psx_bios_t* bios) {
bus->bios = bios;
}
void psx_bus_destroy(psx_bus_t* bus) {
free(bus);
}
uint32_t psx_bus_read32(psx_bus_t* bus, uint32_t addr) {
if (addr & 0x3) {
log_warn("Unaligned 32-bit read from %08x", addr);
}
if (RANGE(addr, PSX_BIOS_BEGIN, PSX_BIOS_END))
return psx_bios_read32(bus->bios, addr - PSX_BIOS_BEGIN);
if (RANGE(addr, PSX_IO_RAM_SIZE_BEGIN, PSX_IO_RAM_SIZE_END))
return bus->ram_size;
log_warn("Unhandled 32-bit read from %08x", addr);
return 0xffffffff;
}
uint16_t psx_bus_read16(psx_bus_t* bus, uint32_t addr) {
if (addr & 0x1) {
log_warn("Unaligned 16-bit read from %08x", addr);
}
if (RANGE(addr, PSX_BIOS_BEGIN, PSX_BIOS_END))
return psx_bios_read16(bus->bios, addr - PSX_BIOS_BEGIN);
log_warn("Unhandled 16-bit read from %08x", addr);
return 0xffff;
}
uint8_t psx_bus_read8(psx_bus_t* bus, uint32_t addr) {
if (RANGE(addr, PSX_BIOS_BEGIN, PSX_BIOS_END))
return psx_bios_read8(bus->bios, addr - PSX_BIOS_BEGIN);
log_warn("Unhandled 8-bit read from %08x", addr);
return 0xff;
}
void psx_bus_write32(psx_bus_t* bus, uint32_t addr, uint32_t value) {
if (addr & 0x3) {
log_warn("Unaligned 32-bit write to %08x (%08x)", addr, value);
}
if (RANGE(addr, PSX_IO_RAM_SIZE_BEGIN, PSX_IO_RAM_SIZE_END)) {
bus->ram_size = value;
return;
}
log_warn("Unhandled 32-bit write to %08x (%08x)", addr, value);
}
void psx_bus_write16(psx_bus_t* bus, uint32_t addr, uint16_t value) {
if (addr & 0x1) {
log_warn("Unaligned 16-bit write to %08x (%08x)", addr, value);
}
log_warn("Unhandled 32-bit write to %08x (%04x)", addr, value);
}
void psx_bus_write8(psx_bus_t* bus, uint32_t addr, uint8_t value) {
log_warn("Unhandled 32-bit write to %08x (%02x)", addr, value);
}
+28
View File
@@ -0,0 +1,28 @@
#ifndef BUS_H
#define BUS_H
#include <stdint.h>
#include "bios.h"
typedef struct {
psx_bios_t* bios;
// Unhandled I/O ports
uint32_t ram_size;
} psx_bus_t;
#define PSX_IO_RAM_SIZE_BEGIN 0x1f801060
#define PSX_IO_RAM_SIZE_END 0x1f801063
psx_bus_t* psx_bus_create();
void psx_bus_init(psx_bus_t*, psx_bios_t*);
void psx_bus_destroy(psx_bus_t*);
uint32_t psx_bus_read32(psx_bus_t*, uint32_t);
uint16_t psx_bus_read16(psx_bus_t*, uint32_t);
uint8_t psx_bus_read8(psx_bus_t*, uint32_t);
void psx_bus_write32(psx_bus_t*, uint32_t, uint32_t);
void psx_bus_write16(psx_bus_t*, uint32_t, uint16_t);
void psx_bus_write8(psx_bus_t*, uint32_t, uint8_t);
#endif
+622
View File
File diff suppressed because it is too large Load Diff
+114
View File
@@ -0,0 +1,114 @@
#ifndef CPU_H
#define CPU_H
#include <stdint.h>
#include "bus.h"
typedef struct {
psx_bus_t* bus;
uint32_t r[32];
uint32_t buf[2], pc;
uint32_t hi, lo;
} psx_cpu_t;
psx_cpu_t* psx_cpu_create();
void psx_cpu_init(psx_cpu_t*, psx_bus_t*);
void psx_cpu_destroy(psx_cpu_t*);
void psx_cpu_cycle(psx_cpu_t*);
void psx_cpu_i_invalid(psx_cpu_t*);
// Primary
void psx_cpu_i_special(psx_cpu_t*);
void psx_cpu_i_bcz(psx_cpu_t*);
void psx_cpu_i_j(psx_cpu_t*);
void psx_cpu_i_jal(psx_cpu_t*);
void psx_cpu_i_beq(psx_cpu_t*);
void psx_cpu_i_bne(psx_cpu_t*);
void psx_cpu_i_blez(psx_cpu_t*);
void psx_cpu_i_bgtz(psx_cpu_t*);
void psx_cpu_i_addi(psx_cpu_t*);
void psx_cpu_i_addiu(psx_cpu_t*);
void psx_cpu_i_slti(psx_cpu_t*);
void psx_cpu_i_sltiu(psx_cpu_t*);
void psx_cpu_i_andi(psx_cpu_t*);
void psx_cpu_i_ori(psx_cpu_t*);
void psx_cpu_i_xori(psx_cpu_t*);
void psx_cpu_i_lui(psx_cpu_t*);
void psx_cpu_i_cop0(psx_cpu_t*);
void psx_cpu_i_cop1(psx_cpu_t*);
void psx_cpu_i_cop2(psx_cpu_t*);
void psx_cpu_i_cop3(psx_cpu_t*);
void psx_cpu_i_lb(psx_cpu_t*);
void psx_cpu_i_lh(psx_cpu_t*);
void psx_cpu_i_lwl(psx_cpu_t*);
void psx_cpu_i_lw(psx_cpu_t*);
void psx_cpu_i_lbu(psx_cpu_t*);
void psx_cpu_i_lhu(psx_cpu_t*);
void psx_cpu_i_lwr(psx_cpu_t*);
void psx_cpu_i_sb(psx_cpu_t*);
void psx_cpu_i_sh(psx_cpu_t*);
void psx_cpu_i_swl(psx_cpu_t*);
void psx_cpu_i_sw(psx_cpu_t*);
void psx_cpu_i_swr(psx_cpu_t*);
void psx_cpu_i_lwc0(psx_cpu_t*);
void psx_cpu_i_lwc1(psx_cpu_t*);
void psx_cpu_i_lwc2(psx_cpu_t*);
void psx_cpu_i_lwc3(psx_cpu_t*);
void psx_cpu_i_swc0(psx_cpu_t*);
void psx_cpu_i_swc1(psx_cpu_t*);
void psx_cpu_i_swc2(psx_cpu_t*);
void psx_cpu_i_swc3(psx_cpu_t*);
// Secondary
void psx_cpu_i_sll(psx_cpu_t*);
void psx_cpu_i_srl(psx_cpu_t*);
void psx_cpu_i_sra(psx_cpu_t*);
void psx_cpu_i_sllv(psx_cpu_t*);
void psx_cpu_i_srlv(psx_cpu_t*);
void psx_cpu_i_srav(psx_cpu_t*);
void psx_cpu_i_jr(psx_cpu_t*);
void psx_cpu_i_jalr(psx_cpu_t*);
void psx_cpu_i_syscall(psx_cpu_t*);
void psx_cpu_i_break(psx_cpu_t*);
void psx_cpu_i_mfhi(psx_cpu_t*);
void psx_cpu_i_mthi(psx_cpu_t*);
void psx_cpu_i_mflo(psx_cpu_t*);
void psx_cpu_i_mtlo(psx_cpu_t*);
void psx_cpu_i_mult(psx_cpu_t*);
void psx_cpu_i_multu(psx_cpu_t*);
void psx_cpu_i_div(psx_cpu_t*);
void psx_cpu_i_divu(psx_cpu_t*);
void psx_cpu_i_add(psx_cpu_t*);
void psx_cpu_i_addu(psx_cpu_t*);
void psx_cpu_i_sub(psx_cpu_t*);
void psx_cpu_i_subu(psx_cpu_t*);
void psx_cpu_i_and(psx_cpu_t*);
void psx_cpu_i_or(psx_cpu_t*);
void psx_cpu_i_xor(psx_cpu_t*);
void psx_cpu_i_nor(psx_cpu_t*);
void psx_cpu_i_slt(psx_cpu_t*);
void psx_cpu_i_sltu(psx_cpu_t*);
// COP0
void psx_cpu_i_mfc0(psx_cpu_t*);
void psx_cpu_i_cfc0(psx_cpu_t*);
void psx_cpu_i_mtc0(psx_cpu_t*);
void psx_cpu_i_ctc0(psx_cpu_t*);
void psx_cpu_i_bc0c(psx_cpu_t*);
// Unimplemented
// void psx_cpu_i_cop0_tlbr(psx_cpu_t*);
// void psx_cpu_i_cop0_tlbwi(psx_cpu_t*);
// void psx_cpu_i_cop0_tlbwr(psx_cpu_t*);
// void psx_cpu_i_cop0_tlbp(psx_cpu_t*);
// COP0-specific
void psx_cpu_i_rfe(psx_cpu_t*);
typedef void (*psx_cpu_instruction_t)(psx_cpu_t*);
#endif
+166
View File
@@ -0,0 +1,166 @@
/*
* Copyright (c) 2020 rxi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "log.h"
#define MAX_CALLBACKS 32
typedef struct {
log_LogFn fn;
void *udata;
int level;
} Callback;
static struct {
void *udata;
log_LockFn lock;
int level;
bool quiet;
Callback callbacks[MAX_CALLBACKS];
} L;
static const char *level_strings[] = {
"trace", "debug", "info", "warn", "error", "fatal"
};
#ifdef LOG_USE_COLOR
static const char *level_colors[] = {
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
};
#endif
static void stdout_callback(log_Event *ev) {
#ifdef LOG_USE_COLOR
fprintf(
ev->udata, "psx: %s%-5s\x1b[0m \x1b[90m%s:\x1b[0m ",
level_colors[ev->level], level_strings[ev->level],
ev->file);
#else
fprintf(
ev->udata, "psx: %-5s %s: ",
level_strings[ev->level], ev->file);
#endif
vfprintf(ev->udata, ev->fmt, ev->ap);
fprintf(ev->udata, "\n");
fflush(ev->udata);
}
static void file_callback(log_Event *ev) {
char buf[64];
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
fprintf(
ev->udata, "%s %-5s %s:%d: ",
buf, level_strings[ev->level], ev->file, ev->line);
vfprintf(ev->udata, ev->fmt, ev->ap);
fprintf(ev->udata, "\n");
fflush(ev->udata);
}
static void lock(void) {
if (L.lock) { L.lock(true, L.udata); }
}
static void unlock(void) {
if (L.lock) { L.lock(false, L.udata); }
}
const char* log_level_string(int level) {
return level_strings[level];
}
void log_set_lock(log_LockFn fn, void *udata) {
L.lock = fn;
L.udata = udata;
}
void log_set_level(int level) {
L.level = level;
}
void log_set_quiet(bool enable) {
L.quiet = enable;
}
int log_add_callback(log_LogFn fn, void *udata, int level) {
for (int i = 0; i < MAX_CALLBACKS; i++) {
if (!L.callbacks[i].fn) {
L.callbacks[i] = (Callback) { fn, udata, level };
return 0;
}
}
return -1;
}
int log_add_fp(FILE *fp, int level) {
return log_add_callback(file_callback, fp, level);
}
static void init_event(log_Event *ev, void *udata) {
if (!ev->time) {
time_t t = time(NULL);
ev->time = localtime(&t);
}
ev->udata = udata;
}
void log_log(int level, const char *file, int line, const char *fmt, ...) {
log_Event ev = {
.fmt = fmt,
.file = file,
.line = line,
.level = level,
};
lock();
if (!L.quiet && level >= L.level) {
init_event(&ev, stderr);
va_start(ev.ap, fmt);
stdout_callback(&ev);
va_end(ev.ap);
}
for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) {
Callback *cb = &L.callbacks[i];
if (level >= cb->level) {
init_event(&ev, cb->udata);
va_start(ev.ap, fmt);
cb->fn(&ev);
va_end(ev.ap);
}
}
unlock();
}
+49
View File
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2020 rxi
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See `log.c` for details.
*/
#ifndef LOG_H
#define LOG_H
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <time.h>
#define LOG_VERSION "0.1.0"
typedef struct {
va_list ap;
const char *fmt;
const char *file;
struct tm *time;
void *udata;
int line;
int level;
} log_Event;
typedef void (*log_LogFn)(log_Event *ev);
typedef void (*log_LockFn)(bool lock, void *udata);
enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
const char* log_level_string(int level);
void log_set_lock(log_LockFn fn, void *udata);
void log_set_level(int level);
void log_set_quiet(bool enable);
int log_add_callback(log_LogFn fn, void *udata, int level);
int log_add_fp(FILE *fp, int level);
void log_log(int level, const char *file, int line, const char *fmt, ...);
#endif