Skip to content

Commit

Permalink
Add Free Running Timer implementation
Browse files Browse the repository at this point in the history
Doesn't work in Fusion, only useful for profiling.

Major thanks to Chilly Willy
  • Loading branch information
viciious committed May 11, 2021
1 parent 3fbbda7 commit 9492189
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
8 changes: 8 additions & 0 deletions 32x.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@

#define SH2_DMA_DMAOR (*(volatile unsigned long *)0xFFFFFFB0)

#define SH2_INT_ICR (*(volatile unsigned short *)0xFFFFFEE0)
#define SH2_INT_IPRA (*(volatile unsigned short *)0xFFFFFEE2)
#define SH2_INT_IPRB (*(volatile unsigned short *)0xFFFFFE60)
#define SH2_INT_VCRA (*(volatile unsigned short *)0xFFFFFE62)
#define SH2_INT_VCRB (*(volatile unsigned short *)0xFFFFFE64)
#define SH2_INT_VCRC (*(volatile unsigned short *)0xFFFFFE66)
#define SH2_INT_VCRD (*(volatile unsigned short *)0xFFFFFE68)
#define SH2_INT_VCRWDT (*(volatile unsigned short *)0xFFFFFEE4)
#define SH2_INT_VCRDIV (*(volatile unsigned long *)0xFFFFFF0C)

#define SEGA_CTRL_UP 0x0001
#define SEGA_CTRL_DOWN 0x0002
Expand Down
34 changes: 34 additions & 0 deletions crt0.s
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ master_vbr:
.long master_hbi /* H Blank interupt */
.long master_vbi /* V Blank interupt */
.long master_rst /* Reset Button */
.long master_frt /* FRT overflow */

!-----------------------------------------------------------------------
! Slave Vector Base Table
Expand Down Expand Up @@ -440,6 +441,39 @@ master_lvl4_5:
rte
nop

!-----------------------------------------------------------------------
! Master FRT Overflow IRQ handler
!-----------------------------------------------------------------------

master_frt:
mov.l r0,@-r15
mov.l r1,@-r15

mov.l sh2_frt_ftcsr,r1
mov.b @r1,r0
tst #2,r0
bt 9f
/* overflow */
mov #0,r0
mov.w r0,@r1 /* clear OVF IRQ */

mov.l frt_ovf_count_ptr,r1
mov.l @r1,r0
add #1,r0
mov.l r0,@r1
9:
mov.l @r15+,r1
mov.l @r15+,r0
rte
nop

.align 2
sh2_frt_ftcsr:
.long 0xFFFFFE11

frt_ovf_count_ptr:
.long _frt_ovf_count

!-----------------------------------------------------------------------
! Master V Blank IRQ handler
!-----------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions doomdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ unsigned I_NetTransfer (unsigned buttons);
boolean I_RefreshCompleted (void) ATTR_DATA_CACHE_ALIGN;
boolean I_RefreshLatched (void);
int I_GetTime (void) ATTR_DATA_CACHE_ALIGN;
int I_GetFRTCounter (void) ATTR_DATA_CACHE_ALIGN;

void I_Update (void);

Expand Down
38 changes: 34 additions & 4 deletions marsnew.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ const int COLOR_WHITE = 0x04;
int activescreen = 0;
short *dc_colormaps;

int vblank_count = 0;
unsigned vblank_count = 0;
unsigned frt_ovf_count = 0;
static float frt_counter2msec = 0;

const int NTSC_CLOCK_SPEED = 23011360; // HZ
const int PAL_CLOCK_SPEED = 22801467; // HZ

extern int debugmode;

Expand Down Expand Up @@ -81,10 +86,26 @@ void Mars_Init(void)
int i, j;
volatile unsigned short *lines = &MARS_FRAMEBUFFER;
volatile unsigned short *palette;
boolean NTSC;

while ((MARS_SYS_INTMSK & MARS_SH2_ACCESS_VDP) == 0);

MARS_VDP_DISPMODE = MARS_224_LINES | MARS_VDP_MODE_256;
NTSC = (MARS_VDP_DISPMODE & MARS_NTSC_FORMAT) != 0;

/* init hires timer system */
SH2_FRT_TCR = 2; /* TCR set to count at SYSCLK/128 */
SH2_FRT_FRCH = 0;
SH2_FRT_FRCL = 0;
SH2_INT_IPRB = (SH2_INT_IPRB & 0xF0FF) | 0x0E00; /* set FRT INT to priority 14 */
SH2_INT_VCRD = 72 << 8; /* set exception vector for FRT overflow */
SH2_FRT_FTCSR = 0; /* clear any int status */
SH2_FRT_TIER = 3; /* enable overflow interrupt */

MARS_SYS_COMM4 = 0;

// change 128.0f to something else if SH2_FRT_TCR is changed!
frt_counter2msec = 128.0f * 1000.0f / (NTSC ? NTSC_CLOCK_SPEED : PAL_CLOCK_SPEED);

activescreen = MARS_VDP_FBCTL;

Expand All @@ -101,15 +122,13 @@ void Mars_Init(void)
Mars_FlipFrameBuffers(true);
}

ticrate = (MARS_VDP_DISPMODE & MARS_NTSC_FORMAT) ? 4 : 3;
ticrate = NTSC ? 4 : 3;

/* set a two color palette */
palette = &MARS_CRAM;
for (i = 0; i < 256; i++)
palette[i] = 0;
palette[COLOR_WHITE] = 0x7fff;

MARS_SYS_COMM4 = 0;
}

int Mars_ToDoomControls(int ctrl)
Expand Down Expand Up @@ -312,6 +331,17 @@ int I_GetTime (void)
return *(int *)((intptr_t)&vblank_count | 0x20000000);
}

int I_GetFRTCounter(void)
{
unsigned cnt = (SH2_FRT_FRCH << 8) | SH2_FRT_FRCL;
return (int)((frt_ovf_count << 16) | cnt);
}

int I_FRTCounter2Msec(int c)
{
return (int)((float)c * frt_counter2msec);
}

/*
====================
=
Expand Down
1 change: 1 addition & 0 deletions marsonly.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void Mars_Init(void);
void Mars_Slave(void);

void I_ClearWorkBuffer();
int I_FRTCounter2Msec(int c);

/*
================
Expand Down

0 comments on commit 9492189

Please sign in to comment.