Mercurial > repos > simple16
diff src/timer.c @ 25:fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 31 Mar 2016 23:25:52 -0700 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/timer.c Thu Mar 31 23:25:52 2016 -0700 @@ -0,0 +1,43 @@ +#include <stdint.h> +#include <string.h> +#include <limits.h> +#include "timer.h" + +void timer_init(timer *context, uint32_t clock_div) +{ + memset(context, 0, sizeof(timer)); + context->clock_inc = clock_div; +} + +void timer_run(timer *context, uint32_t target) +{ + while (context->cycles < target) + { + if (context->current) { + context->current--; + if (!context->current) { + context->pending = 1; + } + } else { + context->current = context->load; + } + context->cycles += context->clock_inc; + } +} + +uint32_t timer_next_interrupt(timer *context) +{ + if (context->pending) { + return 0; + } + if (context->current) { + return context->cycles + context->current * context->clock_inc; + } + return UINT_MAX; +} + +void timer_write(timer *context, uint16_t value) +{ + context->load = context->current = value; + context->pending = 0; +}