Skip to content

Commit

Permalink
document 15625
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenhael-le-moine committed Apr 10, 2024
1 parent 18bc490 commit 12aadf3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
63 changes: 31 additions & 32 deletions src/emu_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@

#define NR_TIMERS 4

#define RAM_BASE_SX 0x70000
#define ACCESSTIME_SX ( 0x70052 - RAM_BASE_SX )
#define ACCESSCRC_SX ( 0x7005F - RAM_BASE_SX )
#define TIMEOUT_SX ( 0x70063 - RAM_BASE_SX )
#define TIMEOUTCLK_SX ( 0x70070 - RAM_BASE_SX )

#define RAM_BASE_GX 0x80000
#define ACCESSTIME_GX ( 0x80058 - RAM_BASE_GX )
#define ACCESSCRC_GX ( 0x80065 - RAM_BASE_GX )
#define TIMEOUT_GX ( 0x80069 - RAM_BASE_GX )
#define TIMEOUTCLK_GX ( 0x80076 - RAM_BASE_GX )

#define calc_crc( nib ) ( crc = ( crc >> 4 ) ^ ( ( ( crc ^ ( nib ) ) & 0xf ) * 0x1081 ) )

typedef struct x48_timer_t {
word_1 run;
word_64 start;
Expand All @@ -24,6 +38,8 @@ static x48_timer_t timers[ NR_TIMERS ];

static long systime_offset = 0;

static word_64 zero = 0;

/*
* Ticks for THU 01.01.1970 00:00:00
*/
Expand All @@ -40,20 +56,6 @@ word_64 set_0_time = 0x0;
*/
word_64 time_offset = 0x0;

#define RAM_BASE_SX 0x70000
#define ACCESSTIME_SX ( 0x70052 - RAM_BASE_SX )
#define ACCESSCRC_SX ( 0x7005F - RAM_BASE_SX )
#define TIMEOUT_SX ( 0x70063 - RAM_BASE_SX )
#define TIMEOUTCLK_SX ( 0x70070 - RAM_BASE_SX )

#define RAM_BASE_GX 0x80000
#define ACCESSTIME_GX ( 0x80058 - RAM_BASE_GX )
#define ACCESSCRC_GX ( 0x80065 - RAM_BASE_GX )
#define TIMEOUT_GX ( 0x80069 - RAM_BASE_GX )
#define TIMEOUTCLK_GX ( 0x80076 - RAM_BASE_GX )

#define calc_crc( nib ) ( crc = ( crc >> 4 ) ^ ( ( ( crc ^ ( nib ) ) & 0xf ) * 0x1081 ) )

/*
* Set ACCESSTIME: (on startup)
*
Expand Down Expand Up @@ -95,7 +97,7 @@ void set_accesstime( void )

ticks = tv.tv_sec;
ticks <<= 13;
ticks += ( tv.tv_usec << 7 ) / 15625;
ticks += ( tv.tv_usec << 7 ) / USEC_PER_FRAME;

time_offset = unix_0_time + set_0_time;
ticks += time_offset;
Expand Down Expand Up @@ -164,11 +166,11 @@ void start_timer( int timer )
timers[ timer ].run = 1;
if ( timer == T1_TIMER ) {
timers[ timer ].start = ( tv.tv_sec << 9 );
timers[ timer ].start += ( tv.tv_usec / 15625 ) >> 3;
timers[ timer ].start += ( tv.tv_usec / USEC_PER_FRAME ) >> 3;
} else {
timers[ timer ].start = tv.tv_sec;
timers[ timer ].start <<= 13;
timers[ timer ].start += ( tv.tv_usec << 7 ) / 15625;
timers[ timer ].start += ( tv.tv_usec << 7 ) / USEC_PER_FRAME;
}
}

Expand All @@ -191,11 +193,11 @@ void restart_timer( int timer )
timers[ timer ].run = 1;
if ( timer == T1_TIMER ) {
timers[ timer ].start = ( tv.tv_sec << 9 );
timers[ timer ].start += ( tv.tv_usec / 15625 ) >> 3;
timers[ timer ].start += ( tv.tv_usec / USEC_PER_FRAME ) >> 3;
} else {
timers[ timer ].start = tv.tv_sec;
timers[ timer ].start <<= 13;
timers[ timer ].start += ( tv.tv_usec << 7 ) / 15625;
timers[ timer ].start += ( tv.tv_usec << 7 ) / USEC_PER_FRAME;
}
}

Expand All @@ -217,30 +219,27 @@ void stop_timer( int timer )
timers[ timer ].run = 0;
if ( timer == T1_TIMER ) {
timers[ timer ].stop = ( tv.tv_sec << 9 );
timers[ timer ].stop += ( tv.tv_usec / 15625 ) >> 3;
timers[ timer ].stop += ( tv.tv_usec / USEC_PER_FRAME ) >> 3;
} else {
timers[ timer ].stop = tv.tv_sec;
timers[ timer ].stop <<= 13;
timers[ timer ].stop += ( tv.tv_usec << 7 ) / 15625;
timers[ timer ].stop += ( tv.tv_usec << 7 ) / USEC_PER_FRAME;
}

timers[ timer ].value += timers[ timer ].stop - timers[ timer ].start;
// add_sub_64(&timers[timer].stop, &timers[timer].start,
// &timers[timer].value);
}

void reset_timer( int timer )
{
if ( timer > NR_TIMERS )
return;

timers[ timer ].run = 0;
timers[ timer ].start = 0;
timers[ timer ].stop = 0;
timers[ timer ].value = 0;
}

static word_64 zero = 0;

word_64 get_timer( int timer )
{
struct timeval tv;
Expand All @@ -259,11 +258,11 @@ word_64 get_timer( int timer )

if ( timer == T1_TIMER ) {
stop = ( tv.tv_sec << 9 );
stop += ( tv.tv_usec / 15625 ) >> 3;
stop += ( tv.tv_usec / USEC_PER_FRAME ) >> 3;
} else {
stop = tv.tv_sec;
stop <<= 13;
stop += ( tv.tv_usec << 7 ) / 15625;
stop += ( tv.tv_usec << 7 ) / USEC_PER_FRAME;
}
timers[ timer ].value += stop - timers[ timer ].start;
}
Expand Down Expand Up @@ -302,18 +301,18 @@ t1_t2_ticks get_t1_t2( void )

if ( timers[ T1_TIMER ].run ) {
stop = ( tv.tv_sec << 9 );
stop += ( tv.tv_usec / 15625 ) >> 3;
if ( timers[ T1_TIMER ].start <= stop ) {
stop += ( tv.tv_usec / USEC_PER_FRAME ) >> 3;

if ( timers[ T1_TIMER ].start <= stop )
timers[ T1_TIMER ].value += stop - timers[ T1_TIMER ].start;
} else {
else
fprintf( stderr, "clock running backwards\n" );
}
}
ticks.t1_ticks = timers[ T1_TIMER ].value;

stop = tv.tv_sec;
stop <<= 13;
stop += ( tv.tv_usec << 7 ) / 15625;
stop += ( tv.tv_usec << 7 ) / USEC_PER_FRAME;

stop += time_offset;

Expand Down
3 changes: 3 additions & 0 deletions src/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#define RUN_TIMER 2
#define IDLE_TIMER 3

/* LCD refresh rate is 64Hz according to https://www.hpcalc.org/hp48/docs/faq/48faq-6.html */
#define USEC_PER_FRAME ( 1000000 / 64 )

// Keys
#define NB_KEYS 49

Expand Down
7 changes: 3 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "debugger.h"
#include "emulator.h"
#include "runtime_options.h"
#include "ui.h" /* init_ui(); */
#include "ui.h" /* setup_frontend(); init_ui(); */

void signal_handler( int sig )
{
Expand Down Expand Up @@ -76,11 +76,10 @@ int main( int argc, char** argv )
In emulate() sigalarm_triggered triggers LCD refresh and UI event handling
*/
struct itimerval it;
int interval = 15625; /* 64Hz according to https://www.hpcalc.org/hp48/docs/faq/48faq-6.html */
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = interval;
it.it_interval.tv_usec = USEC_PER_FRAME;
it.it_value.tv_sec = 0;
it.it_value.tv_usec = interval;
it.it_value.tv_usec = USEC_PER_FRAME;
setitimer( ITIMER_REAL, &it, ( struct itimerval* )0 );

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

0 comments on commit 12aadf3

Please sign in to comment.