Skip to content

Commit

Permalink
Merge pull request #3 from sozud/counter
Browse files Browse the repository at this point in the history
Match counter
  • Loading branch information
sozud authored Sep 6, 2024
2 parents bbfed89 + 7646747 commit 6842351
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
7 changes: 7 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ def build_35():

add_lib_263(gs_srcs, "build/3.5/gs", "./psy-q/3.5/PSX/LIB/LIBGS.LIB", "-DVERSION=35", "3.5")

api_srcs = [
'src/api/counter.c'
]

add_lib_263(api_srcs, "build/3.5/api", "./psy-q/3.5/PSX/LIB/LIBAPI.LIB", "-DVERSION=35", "3.5")


def build_36():
snd_srcs = [
# 'src/snd/next.c',
Expand Down
82 changes: 82 additions & 0 deletions src/api/counter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "../types.h"

typedef struct {
u16 rootCounter;
s16 unk2;
s16 mode;
s16 : 16;
s16 target;
s32 : 32;
} Counter;

// https://psx-spx.consoledev.net/timers/
static volatile s32 *_interrupt_status_register = 0x1F801070;

static volatile Counter* _counters = 0x1F801100;

static volatile s32 _interrupt_status_masks[4] = {
0x00000010,
0x00000020,
0x00000040,
0x00000001
};

s32 SetRCnt(s32 spec, s16 target, s32 flags) {
s32 i = spec & 0xFFFF;
s32 final_mode = 0x48;
if (i >= 3) {
return 0;
}
_counters[i].mode = 0;
_counters[i].target = target;

if (i < 2u) {
if (flags & 0x10) {
final_mode = 0x49;
}
if (!(flags & 1)) {
final_mode |= 0x100;
}
} else if (i == 2u) { // nb: `else` is redundant here
if (!(flags & 1)) {
final_mode = 0x248;
}
}
if ((flags & 0x1000) != 0) {
final_mode |= 0x10;
}

_counters[i].mode = final_mode;
return 1;
}

long GetRCnt(long spec) {
s32 i = spec & 0xFFFF;
if (i >= 3) {
return 0;
}
return _counters[i].rootCounter;
}

s32 StartRCnt(s32 spec) {
s32 i = spec & 0xFFFF;
long* mask = _interrupt_status_masks;
_interrupt_status_register[1] |= mask[i];
return i < 3;
}

long StopRCnt(long spec) {
s32 i = spec & 0xFFFF;
long* mask = _interrupt_status_masks;
_interrupt_status_register[1] &= ~mask[i];
return 1;
}

long ResetRCnt(long spec) {
s32 i = spec & 0xFFFF;
if (i >= 3) {
return 0;
}
_counters[i].rootCounter = 0;
return 1;
}

0 comments on commit 6842351

Please sign in to comment.