Implement kernel call hooks

Fix Linux builds, tidy up
This commit is contained in:
Lycoder
2023-06-05 23:14:55 -03:00
parent a214d47e8f
commit e9408c21c7
6 changed files with 71 additions and 28 deletions
-7
View File
@@ -1,12 +1,5 @@
#include "screen.h"
/*
screen->width = 320;
screen->height = 240;
screen->format = SDL_PIXELFORMAT_BGR555;
fe_data.m = 60;
screen->scale = 2;
*/
psxe_screen_t* psxe_screen_create() {
return (psxe_screen_t*)malloc(sizeof(psxe_screen_t));
}
+1 -1
View File
@@ -5,7 +5,7 @@
#include <string.h>
#include "SDL.h"
#include "SDL2/SDL.h"
typedef struct {
SDL_Window* window;
+56
View File
@@ -42,9 +42,15 @@ psx_cpu_t* psx_cpu_create() {
return (psx_cpu_t*)malloc(sizeof(psx_cpu_t));
}
void cpu_a_kcall_hook(psx_cpu_t*);
void cpu_b_kcall_hook(psx_cpu_t*);
void psx_cpu_init(psx_cpu_t* cpu, psx_bus_t* bus) {
memset(cpu, 0, sizeof(psx_cpu_t));
psx_cpu_set_a_kcall_hook(cpu, cpu_a_kcall_hook);
psx_cpu_set_b_kcall_hook(cpu, cpu_b_kcall_hook);
cpu->bus = bus;
cpu->pc = 0xbfc00000;
@@ -175,6 +181,7 @@ psx_cpu_instruction_t g_psx_cpu_bxx_table[] = {
#define DO_PENDING_LOAD \
cpu->r[cpu->load_d] = cpu->load_v; \
R_R0 = 0; \
cpu->load_v = 0xffffffff; \
cpu->load_d = 0;
#define DEBUG_ALL \
@@ -226,6 +233,50 @@ const char* g_psx_cpu_syscall_function_symbol_table[] = {
// DeliverEvent (invalid)
};
void cpu_a_kcall_hook(psx_cpu_t* cpu) {
switch (cpu->r[9]) {
case 0x09: putc(R_A0, stdout); break;
case 0x3c: putchar(R_A0); break;
case 0x3e: {
uint32_t src = R_A0;
char c = psx_bus_read8(cpu->bus, src++);
while (c) {
putchar(c);
c = psx_bus_read8(cpu->bus, src++);
}
} break;
}
}
void cpu_b_kcall_hook(psx_cpu_t* cpu) {
switch (cpu->r[9]) {
case 0x3b: putc(R_A0, stdout); break;
case 0x3d: putchar(R_A0); break;
case 0x3f: {
uint32_t src = R_A0;
char c = psx_bus_read8(cpu->bus, src++);
while (c) {
putchar(c);
c = psx_bus_read8(cpu->bus, src++);
}
} break;
}
}
void psx_cpu_set_a_kcall_hook(psx_cpu_t* cpu, psx_cpu_kcall_hook_t hook) {
cpu->a_function_hook = hook;
}
void psx_cpu_set_b_kcall_hook(psx_cpu_t* cpu, psx_cpu_kcall_hook_t hook) {
cpu->b_function_hook = hook;
}
void psx_cpu_save_state(psx_cpu_t* cpu, FILE* file) {
fwrite((char*)cpu, sizeof(*cpu) - sizeof(psx_bus_t*), 1, file);
}
@@ -235,6 +286,11 @@ void psx_cpu_load_state(psx_cpu_t* cpu, FILE* file) {
}
void psx_cpu_cycle(psx_cpu_t* cpu) {
if ((cpu->pc & 0x3fffffff) == 0x000000a4)
if (cpu->a_function_hook) cpu->a_function_hook(cpu);
if ((cpu->pc & 0x3fffffff) == 0x000000b4)
if (cpu->a_function_hook) cpu->b_function_hook(cpu);
cpu->buf[1] = cpu->buf[0];
psx_cpu_fetch(cpu);
+14 -2
View File
@@ -8,7 +8,13 @@
#define PSX_CPU_SPEED 33868800 // 33.868800 MHz
typedef struct {
struct psx_cpu_t;
typedef struct psx_cpu_t psx_cpu_t;
typedef void (*psx_cpu_kcall_hook_t)(psx_cpu_t*);
struct psx_cpu_t {
// Registers (including $zero and $ra)
uint32_t r[32];
@@ -81,7 +87,11 @@ typedef struct {
// Pointer to bus structure
psx_bus_t* bus;
} psx_cpu_t;
// Optional hooks for kernel calls
psx_cpu_kcall_hook_t a_function_hook;
psx_cpu_kcall_hook_t b_function_hook;
};
/*
0 IEc Current Interrupt Enable (0=Disable, 1=Enable) ;rfe pops IUp here
@@ -159,6 +169,8 @@ void psx_cpu_exception(psx_cpu_t*, uint32_t);
void psx_cpu_load_state(psx_cpu_t*, FILE*);
void psx_cpu_save_state(psx_cpu_t*, FILE*);
void psx_cpu_fetch(psx_cpu_t*);
void psx_cpu_set_a_kcall_hook(psx_cpu_t*, psx_cpu_kcall_hook_t);
void psx_cpu_set_b_kcall_hook(psx_cpu_t*, psx_cpu_kcall_hook_t);
/*
00h INT Interrupt
-5
View File
@@ -15,12 +15,7 @@ void psx_gpu_init(psx_gpu_t* gpu) {
gpu->io_base = PSX_GPU_BEGIN;
gpu->io_size = PSX_GPU_SIZE;
gpu->cmd_a0_receiving_data = 0;
gpu->cmd_a0_receiving_size = 0;
gpu->cmd_a0_receiving_pos = 0;
gpu->vram = (uint8_t*)malloc(PSX_GPU_VRAM_SIZE);
gpu->state = GPU_STATE_RECV_CMD;
}
-13
View File
@@ -40,25 +40,12 @@ struct psx_gpu_t {
int cmd_args_remaining;
int cmd_data_remaining;
int words_remaining;
int read_done;
uint32_t color;
uint32_t xpos, ypos;
uint32_t xsiz, ysiz;
uint32_t addr;
int cmd_a0_receiving_pos;
int cmd_a0_receiving_size;
int cmd_a0_receiving_data;
uint32_t cmd_a0_xpos;
uint32_t cmd_a0_ypos;
uint32_t cmd_a0_xsiz;
uint32_t cmd_a0_ysiz;
uint32_t xcnt, ycnt;
uint32_t state;
uint32_t display_mode;
uint32_t gpuread;