| 9120e874 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include "support/harness.h" |
| 30 |
|
| 31 |
#include "via.h" |
| 32 |
|
| 33 |
#include <stdint.h> |
| 34 |
|
| 35 |
#define VIA_T1C_L 4 |
| 36 |
#define VIA_T1C_H 5 |
| 37 |
#define VIA_T1L_L 6 |
| 38 |
#define VIA_T1L_H 7 |
| 39 |
#define VIA_T2C_L 8 |
| 40 |
#define VIA_T2C_H 9 |
| 41 |
#define VIA_ACR 11 |
| 42 |
#define VIA_IFR 13 |
| 43 |
|
| 44 |
#define IFR_T2 0x20 |
| 45 |
#define IFR_T1 0x40 |
| 46 |
|
| 47 |
#define ACR_ONE_SHOT 0x00 |
| 48 |
#define ACR_FREE_RUN 0x40 |
| 49 |
#define ACR_T2_PULSE 0x20 |
| 50 |
|
| 51 |
static uint8_t |
| 52 |
ifr(void) |
| 53 |
{ |
| 54 |
return via2_read(VIA_IFR, true); |
| 55 |
} |
| 56 |
|
| 57 |
static uint16_t |
| 58 |
t1_count(void) |
| 59 |
{ |
| 60 |
return (uint16_t)(via2_read(VIA_T1C_L, true) | (via2_read(VIA_T1C_H, true) << 8)); |
| 61 |
} |
| 62 |
|
| 63 |
static uint16_t |
| 64 |
t2_count(void) |
| 65 |
{ |
| 66 |
return (uint16_t)(via2_read(VIA_T2C_L, true) | (via2_read(VIA_T2C_H, true) << 8)); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static void |
| 74 |
test_writing_t1_high_loads_the_counter_from_the_latch(void) |
| 75 |
{ |
| 76 |
via2_init(); |
| 77 |
via2_write(VIA_ACR, ACR_ONE_SHOT); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
via2_write(VIA_T1L_L, 0xFF); |
| 83 |
via2_write(VIA_T1C_H, 0xFF); |
| 84 |
check_eq(t1_count(), 0xFFFFu, "the counter holds a known value"); |
| 85 |
|
| 86 |
via2_write(VIA_T1L_L, 0x34); |
| 87 |
check_eq(t1_count(), 0xFFFFu, "the low latch alone does not load the counter"); |
| 88 |
|
| 89 |
via2_write(VIA_T1C_H, 0x12); |
| 90 |
check_eq(t1_count(), 0x1234u, "writing the high byte loads both halves at once"); |
| 91 |
} |
| 92 |
|
| 93 |
static void |
| 94 |
test_t1_counts_down_at_the_clock_rate(void) |
| 95 |
{ |
| 96 |
via2_init(); |
| 97 |
via2_write(VIA_ACR, ACR_ONE_SHOT); |
| 98 |
via2_write(VIA_T1L_L, 0x00); |
| 99 |
via2_write(VIA_T1C_H, 0x20); |
| 100 |
|
| 101 |
via2_step(0x100); |
| 102 |
check_eq(t1_count(), 0x1F00u, "T1 decrements once per clock"); |
| 103 |
|
| 104 |
via2_step(0x0F00); |
| 105 |
check_eq(t1_count(), 0x1000u, "and keeps going"); |
| 106 |
check_eq(ifr() & IFR_T1, 0, "without raising its flag before zero"); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
static void |
| 117 |
test_one_shot_raises_the_flag_once_and_free_run_repeats(void) |
| 118 |
{ |
| 119 |
via2_init(); |
| 120 |
via2_write(VIA_ACR, ACR_ONE_SHOT); |
| 121 |
via2_write(VIA_T1L_L, 0x10); |
| 122 |
via2_write(VIA_T1C_H, 0x00); |
| 123 |
|
| 124 |
via2_step(0x40); |
| 125 |
check_eq(ifr() & IFR_T1, IFR_T1, "one-shot raises the flag at zero"); |
| 126 |
|
| 127 |
via2_write(VIA_IFR, IFR_T1); |
| 128 |
via2_step(0x4000); |
| 129 |
check_eq(ifr() & IFR_T1, 0, "and never again without another load"); |
| 130 |
|
| 131 |
|
| 132 |
via2_init(); |
| 133 |
via2_write(VIA_ACR, ACR_FREE_RUN); |
| 134 |
via2_write(VIA_T1L_L, 0x10); |
| 135 |
via2_write(VIA_T1C_H, 0x00); |
| 136 |
|
| 137 |
via2_step(0x40); |
| 138 |
check_eq(ifr() & IFR_T1, IFR_T1, "free-run raises the flag at zero"); |
| 139 |
|
| 140 |
via2_write(VIA_IFR, IFR_T1); |
| 141 |
via2_step(0x40); |
| 142 |
check_eq(ifr() & IFR_T1, IFR_T1, "and raises it again with no reload"); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
static void |
| 150 |
test_loading_the_latches_does_not_disturb_a_count_in_progress(void) |
| 151 |
{ |
| 152 |
via2_init(); |
| 153 |
via2_write(VIA_ACR, ACR_FREE_RUN); |
| 154 |
via2_write(VIA_T1L_L, 0x00); |
| 155 |
via2_write(VIA_T1C_H, 0x20); |
| 156 |
via2_step(0x100); |
| 157 |
|
| 158 |
const uint16_t before = t1_count(); |
| 159 |
check_eq(before, 0x1F00u, "a count is in progress"); |
| 160 |
|
| 161 |
via2_write(VIA_T1L_L, 0x55); |
| 162 |
check_eq(t1_count(), before, "writing the low latch leaves the counter alone"); |
| 163 |
|
| 164 |
via2_write(VIA_T1L_H, 0x66); |
| 165 |
check_eq(t1_count(), before, "and so does writing the high latch"); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
static void |
| 174 |
test_t2_loads_counts_and_fires_once(void) |
| 175 |
{ |
| 176 |
via2_init(); |
| 177 |
via2_write(VIA_ACR, ACR_ONE_SHOT); |
| 178 |
|
| 179 |
via2_write(VIA_T2C_L, 0xFF); |
| 180 |
via2_write(VIA_T2C_H, 0xFF); |
| 181 |
check_eq(t2_count(), 0xFFFFu, "the counter holds a known value"); |
| 182 |
|
| 183 |
via2_write(VIA_T2C_L, 0x78); |
| 184 |
check_eq(t2_count(), 0xFFFFu, "the low latch alone does not load T2"); |
| 185 |
|
| 186 |
via2_write(VIA_T2C_H, 0x56); |
| 187 |
check_eq(t2_count(), 0x5678u, "writing the high byte loads both halves"); |
| 188 |
|
| 189 |
via2_step(0x78); |
| 190 |
check_eq(t2_count(), 0x5600u, "T2 decrements once per clock"); |
| 191 |
check_eq(ifr() & IFR_T2, 0, "with no flag before zero"); |
| 192 |
|
| 193 |
via2_step(0x5700); |
| 194 |
check_eq(ifr() & IFR_T2, IFR_T2, "and raises its flag at zero"); |
| 195 |
|
| 196 |
|
| 197 |
via2_write(VIA_IFR, IFR_T2); |
| 198 |
via2_step(0x10000); |
| 199 |
check_eq(ifr() & IFR_T2, 0, "T2 has no free-run mode to raise it again"); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
static void |
| 207 |
test_acr_bit_5_takes_t2_off_the_clock(void) |
| 208 |
{ |
| 209 |
via2_init(); |
| 210 |
via2_write(VIA_ACR, ACR_T2_PULSE); |
| 211 |
via2_write(VIA_T2C_L, 0x00); |
| 212 |
via2_write(VIA_T2C_H, 0x10); |
| 213 |
|
| 214 |
via2_step(0x4000); |
| 215 |
check_eq(t2_count(), 0x1000u, "T2 in pulse mode ignores the clock"); |
| 216 |
check_eq(ifr() & IFR_T2, 0, "and never times out on it"); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
via2_init(); |
| 221 |
via2_write(VIA_ACR, ACR_ONE_SHOT); |
| 222 |
via2_write(VIA_T2C_L, 0x00); |
| 223 |
via2_write(VIA_T2C_H, 0x10); |
| 224 |
|
| 225 |
via2_step(0x4000); |
| 226 |
check_eq(ifr() & IFR_T2, IFR_T2, "while on the clock the same count times out"); |
| 227 |
} |
| 228 |
|
| 229 |
int |
| 230 |
main(void) |
| 231 |
{ |
| 232 |
test_writing_t1_high_loads_the_counter_from_the_latch(); |
| 233 |
test_t1_counts_down_at_the_clock_rate(); |
| 234 |
test_one_shot_raises_the_flag_once_and_free_run_repeats(); |
| 235 |
test_loading_the_latches_does_not_disturb_a_count_in_progress(); |
| 236 |
test_t2_loads_counts_and_fires_once(); |
| 237 |
test_acr_bit_5_takes_t2_off_the_clock(); |
| 238 |
return x16_test_summary("via_timers"); |
| 239 |
} |