Mercurial > repos > simple16
comparison 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 |
comparison
equal
deleted
inserted
replaced
24:4c9dbfa30a66 | 25:fb14515266f4 |
---|---|
1 #include <stdint.h> | |
2 #include <string.h> | |
3 #include <limits.h> | |
4 #include "timer.h" | |
5 | |
6 void timer_init(timer *context, uint32_t clock_div) | |
7 { | |
8 memset(context, 0, sizeof(timer)); | |
9 context->clock_inc = clock_div; | |
10 } | |
11 | |
12 void timer_run(timer *context, uint32_t target) | |
13 { | |
14 while (context->cycles < target) | |
15 { | |
16 if (context->current) { | |
17 context->current--; | |
18 if (!context->current) { | |
19 context->pending = 1; | |
20 } | |
21 } else { | |
22 context->current = context->load; | |
23 } | |
24 context->cycles += context->clock_inc; | |
25 } | |
26 } | |
27 | |
28 uint32_t timer_next_interrupt(timer *context) | |
29 { | |
30 if (context->pending) { | |
31 return 0; | |
32 } | |
33 if (context->current) { | |
34 return context->cycles + context->current * context->clock_inc; | |
35 } | |
36 return UINT_MAX; | |
37 } | |
38 | |
39 void timer_write(timer *context, uint16_t value) | |
40 { | |
41 context->load = context->current = value; | |
42 context->pending = 0; | |
43 } |