Skip to content

Commit

Permalink
More scheduler fixes
Browse files Browse the repository at this point in the history
Try to standardize on int64_t for time everywhere.
  • Loading branch information
phire committed Jul 13, 2022
1 parent 1c8f33e commit 14898fd
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions centurion.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
static unsigned finch; /* Finch or original FDC */

volatile unsigned int emulator_done;
static uint64_t cpu_timestamp_ns = 0;
static int64_t cpu_timestamp_ns = 0;

#define TRACE_MEM_RD 1
#define TRACE_MEM_WR 2
Expand Down Expand Up @@ -638,7 +638,7 @@ void mem_write16_debug(uint32_t addr, uint16_t val)
mem_write8_debug(addr+1, val & 0xff);
}

uint64_t get_current_time() {
int64_t get_current_time() {
return cpu_timestamp_ns;
}

Expand Down
1 change: 0 additions & 1 deletion cpu6.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ extern void cpu6_init(void);
extern void cpu_assert_irq(unsigned ipl);
extern void cpu_deassert_irq(unsigned ipl);
extern void advance_time(uint64_t nanoseconds);
extern uint64_t get_current_time();
extern uint16_t cpu6_dma_count(void);
10 changes: 5 additions & 5 deletions hawk.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ static void hawk_rotation_event_cb(struct event_t* event, int64_t late_ns)

struct hawk_unit* unit = rotation_event.unit;

uint64_t now = get_current_time();
hawk_update(unit, now);
int64_t time = get_current_time() - late_ns;
hawk_update(unit, time);

// Copy current head position to read/write pointer
unit->data_ptr = unit->head_pos;
Expand All @@ -194,9 +194,9 @@ static void hawk_rotation_event_cb(struct event_t* event, int64_t late_ns)
}

void hawk_wait_sector(struct hawk_unit* unit, unsigned sector) {
uint64_t now = get_current_time();
uint64_t rotation = (now + unit->rotation_offset) % (uint64_t)HAWK_ROTATION_NS;
uint64_t desired_rotation = HAWK_SECTOR_NS * sector;
int64_t now = get_current_time();
int64_t rotation = (now + unit->rotation_offset) % (uint64_t)HAWK_ROTATION_NS;
int64_t desired_rotation = HAWK_SECTOR_NS * sector;

int64_t delta = desired_rotation - rotation;
if (delta < 0)
Expand Down
2 changes: 1 addition & 1 deletion hawk.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ int hawk_remaining_bits(struct hawk_unit* unit, uint64_t time);
void hawk_read_bits(struct hawk_unit* unit, int count, uint8_t *dest);
void hawk_rewind(struct hawk_unit* unit, int count); // cheating
void hawk_wait_sector(struct hawk_unit* unit, unsigned sector);
void hawk_update(struct hawk_unit* unit, uint64_t now);
void hawk_update(struct hawk_unit* unit, int64_t now);

// Callback to dsk
void dsk_hawk_changed(unsigned unit);
2 changes: 1 addition & 1 deletion mux.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void mux_set_read_ready(unsigned unit, unsigned trace)
}

void mux_process_events(unsigned unit, unsigned trace) {
uint64_t time = get_current_time();
int64_t time = get_current_time();

if (mux[unit].rx_ready_time && mux[unit].rx_ready_time <= time) {
assert(mux[unit].in_fd != -1);
Expand Down
4 changes: 2 additions & 2 deletions mux.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ struct MuxUnit
unsigned char lastc;
int baud;
unsigned char tx_done;
uint64_t rx_ready_time;
uint64_t tx_done_time;
int64_t rx_ready_time;
int64_t tx_done_time;
};

/* Status register bits */
Expand Down
22 changes: 17 additions & 5 deletions scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ static void update_next_event()

void schedule_event(struct event_t *event)
{
uint64_t now = get_current_time();
int64_t now = get_current_time();
int64_t scheduled = get_current_time() + event->delta_ns;
assert(scheduled >= now);

if (trace_schedule) {
long now_seconds = now / ONE_SECOND_NS;
long now_us = (now % (int64_t)ONE_SECOND_NS) / ONE_MICROSECOND_NS;
double delta = (double)event->delta_ns / ONE_MICROSECOND_NS;
fprintf(stderr, "%li.%06li: Scheduling %s in %.3f us\n",
now_seconds, now_us, event->name, delta);

if (scheduled <= now) {
fprintf(stderr, "%li.%06li: Scheduling %s immediately\n",
now_seconds, now_us, event->name);
} else {
fprintf(stderr, "%li.%06li: Scheduling %s in %.3f us\n",
now_seconds, now_us, event->name, delta);
}
}

if (event->next != NULL) {
Expand Down Expand Up @@ -62,7 +67,7 @@ void run_scheduler(uint64_t current_time, unsigned trace)
while (next_event <= current_time) {
// Pop event
struct event_t* event = event_list;
event_list = event_list->next;
event_list = event->next;
event->next = NULL;
update_next_event();

Expand Down Expand Up @@ -104,4 +109,11 @@ void cancel_event(struct event_t *event)
}
next_ptr = &next->next;
}
}

int64_t scheduler_next()
{
if (event_list == NULL)
return -1;
return next_event;
}
1 change: 1 addition & 0 deletions scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ struct event_t {
void schedule_event(struct event_t *event);
void cancel_event(struct event_t *event);
void run_scheduler(uint64_t current_time, unsigned trace);
int64_t scheduler_next();
int64_t get_current_time();

0 comments on commit 14898fd

Please sign in to comment.