Skip to content

Commit

Permalink
Merge pull request #18 from ckormanyos/clock_sync_at_start
Browse files Browse the repository at this point in the history
Sync clock at startup and clean up
  • Loading branch information
ckormanyos authored Jan 8, 2024
2 parents 342b1d1 + 5902595 commit 33f0610
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 74 deletions.
4 changes: 1 addition & 3 deletions build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,12 @@ $(PATH_BIN)/refapp.8xp :
@-$(ECHO)
@-$(ECHO) +++ assembling $(notdir $(PATH_SRC)/startup/crt0.s)
@-$(AS) $(AFLAGS) -c -o $(PATH_OBJ)/crt0.rel $(PATH_SRC)/startup/crt0.s
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/app/app_dummy.c)
@-$(CC) $(CFLAGS) -c -o $(PATH_OBJ)/app_dummy.rel $(PATH_SRC)/app/app_dummy.c
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/app/app_util.c)
@-$(CC) $(CFLAGS) -c -o $(PATH_OBJ)/app_util.rel $(PATH_SRC)/app/app_util.c
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/startup/clock.c)
@-$(CC) $(CFLAGS) -c -o $(PATH_OBJ)/clock.rel $(PATH_SRC)/startup/clock.c
@-$(ECHO) +++ compiling $(notdir $(PATH_SRC)/app/app_led.c) and linking/extracting to $(notdir $(PATH_BIN)/refapp.ihx)
@-$(CC) $(CFLAGS) -o $(PATH_BIN)/refapp.ihx $(PATH_SRC)/app/app_led.c $(PATH_OBJ)/app_dummy.rel $(PATH_OBJ)/app_util.rel $(PATH_OBJ)/clock.rel $(PATH_OBJ)/crt0.rel
@-$(CC) $(CFLAGS) -o $(PATH_BIN)/refapp.ihx $(PATH_SRC)/app/app_led.c $(PATH_OBJ)/app_util.rel $(PATH_OBJ)/clock.rel $(PATH_OBJ)/crt0.rel
@-$(ECHO) +++ creating binary $(notdir $(PATH_BIN)/refapp.bin)
@-$(OBJCOPY) -Iihex -Obinary $(PATH_BIN)/refapp.ihx $(PATH_BIN)/refapp.bin
@-$(ECHO) +++ creating on-calc app $(notdir $(PATH_BIN)/refapp.8xp)
Expand Down
1 change: 0 additions & 1 deletion build/ti84-ref_app.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<None Include="Makefile" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\app\app_dummy.c" />
<ClCompile Include="..\src\app\app_led.c" />
<ClCompile Include="..\src\app\app_util.c" />
</ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions build/ti84-ref_app.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
<ClCompile Include="..\src\app\app_util.c">
<Filter>src\app</Filter>
</ClCompile>
<ClCompile Include="..\src\app\app_dummy.c">
<Filter>src\app</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\app\app_util.h">
Expand Down
6 changes: 0 additions & 6 deletions src/app/app_dummy.c

This file was deleted.

58 changes: 33 additions & 25 deletions src/app/app_led.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,41 @@
#include <app/app_util.h>
#include <startup/clock.h>

static void app_led_on (void);
static void app_led_off(void);
static void app_led_clr(void);
static void app_led_on (void);
static void app_led_off (void);
static void app_led_clr (void);
static void app_led_delay(void);

static uint8_t app_led_current_tick;
static bool app_led_exit_flag;

void main(void)
{
app_util_init();

bool exit_flag = false;
clock_seconds_start();

uint8_t current_tick = clock_seconds_get();
app_led_current_tick = clock_seconds_get();

for(;;)
{
app_led_clr();
app_led_on();
app_led_delay();

uint8_t next_tick;

while
(
((next_tick = clock_seconds_get()) == current_tick)
&& ((exit_flag = app_util_wants_exit()) == false)
) { ; }

if(exit_flag) { break; }

current_tick = next_tick;
if(app_led_exit_flag)
{
break;
}

app_led_clr();
app_led_off();
app_led_delay();

while
(
((next_tick = clock_seconds_get()) == current_tick)
&& ((exit_flag = app_util_wants_exit()) == false)
) { ; }

if(exit_flag) { break; }

current_tick = next_tick;
if(app_led_exit_flag)
{
break;
}
}

clock_seconds_stop();
Expand Down Expand Up @@ -107,3 +101,17 @@ static void app_led_clr(void)
__asm__("ld a,#0x20\n");
__asm__("rst 0x28\n" ".dw #0x4504\n");
}

static void app_led_delay(void)
{
uint8_t next_tick;

do
{
next_tick = clock_seconds_get();
app_led_exit_flag = app_util_wants_exit();
}
while((next_tick == app_led_current_tick) && (!app_led_exit_flag));

app_led_current_tick = next_tick;
}
24 changes: 0 additions & 24 deletions src/app/app_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,10 @@
#include <app/app_util.h>
#include <startup/clock.h>

static bool app_util_check_crt0(void);

extern uint8_t app_dummy_test_non_initialized;
extern uint8_t app_dummy_test_initialized;

void app_util_init(void)
{
while(!app_util_check_crt0())
{
;
}

__asm__("rst 0x28\n" ".dw #0x4540\n");
__asm__("rst 0x28\n" ".dw #0x4558\n");

clock_seconds_start();
}

bool app_util_wants_exit(void)
Expand All @@ -44,15 +32,3 @@ bool app_util_wants_exit(void)

return exit_flag;
}

static bool app_util_check_crt0(void)
{
// Check static initialization.

const bool result_crt0_non_init_is_ok = ((app_dummy_test_non_initialized == (uint8_t) UINT8_C(0)) ? true : false);
const bool result_crt0_init_is_ok = ((app_dummy_test_initialized == (uint8_t) UINT8_C(42)) ? true : false);

const bool result_check_crt0_is_ok = ((result_crt0_non_init_is_ok && result_crt0_init_is_ok) ? true : false);

return result_check_crt0_is_ok;
}
53 changes: 44 additions & 9 deletions src/startup/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
// Distributed under The Unlicense
//

#include <stdbool.h>

#include <startup/clock.h>

static bool clock_seconds_run_bit_initial_state;

static uint8_t clock_seconds_get_port40 (void) ATTRIBUTE_NAKED;
static void clock_seconds_do_start (void) ATTRIBUTE_NAKED;
static void clock_seconds_do_stop (void) ATTRIBUTE_NAKED;
static uint8_t clock_seconds_do_get (void) ATTRIBUTE_NAKED;

void _clock(void) ATTRIBUTE_NAKED
{
// Mysterious _clock() method.
Expand All @@ -13,13 +22,39 @@ void _clock(void) ATTRIBUTE_NAKED
__asm__("ret\n");
}

void clock_seconds_start(void) ATTRIBUTE_NAKED
void clock_seconds_start(void)
{
const uint8_t clock_seconds_run_bit_value = (uint8_t) (clock_seconds_get_port40() & (uint8_t) UINT8_C(1));

clock_seconds_run_bit_initial_state = ((clock_seconds_run_bit_value != (uint8_t) UINT8_C(0)) ? true : false);

clock_seconds_do_start();
}

void clock_seconds_stop(void)
{
if(!clock_seconds_run_bit_initial_state)
{
clock_seconds_do_stop();
}
}

uint8_t clock_seconds_get(void)
{
return clock_seconds_do_get();
}

static uint8_t clock_seconds_get_port40(void) ATTRIBUTE_NAKED
{
__asm__("in a, (0x40)\n");
__asm__("ld h, a\n");
__asm__("ret\n");
}

static void clock_seconds_do_start(void) ATTRIBUTE_NAKED
{
// This will set the clock back to its start time.
// Reset the lower byte of the 32-bit clock.
__asm__("xor a\n");
__asm__("out (0x41), a\n");
__asm__("out (0x42), a\n");
__asm__("out (0x43), a\n");
__asm__("out (0x44), a\n");

// Ensure that the set command bit is off since its trtansition
Expand All @@ -28,20 +63,20 @@ void clock_seconds_start(void) ATTRIBUTE_NAKED
__asm__("out (0x40), a\n");

// Set and start the clock.
__asm__("ld a, #0x3\n");
__asm__("ld a, #0x3\n");
__asm__("out (0x40), a\n");
__asm__("ret\n");
}

void clock_seconds_stop(void) ATTRIBUTE_NAKED
static void clock_seconds_do_stop(void) ATTRIBUTE_NAKED
{
// Stop the clock.
__asm__("ld a, #0x0\n");
__asm__("ld a, #0x0\n");
__asm__("out (0x40), a\n");
__asm__("ret\n");
}

uint8_t clock_seconds_get(void) ATTRIBUTE_NAKED
static uint8_t clock_seconds_do_get(void) ATTRIBUTE_NAKED
{
// Return the lower 8-bits of a 1hz counter function.

Expand Down
6 changes: 3 additions & 3 deletions src/startup/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

#include <startup/asm_util.h>

uint8_t clock_seconds_get (void) ATTRIBUTE_NAKED;
void clock_seconds_start(void) ATTRIBUTE_NAKED;
void clock_seconds_stop (void) ATTRIBUTE_NAKED;
void clock_seconds_start(void);
void clock_seconds_stop (void);
uint8_t clock_seconds_get (void);


#endif //CLOCK_2024_01_7_H

0 comments on commit 33f0610

Please sign in to comment.