| 9120e874 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "support/video_fixture.h" |
| 22 |
#include "support/harness.h" |
| 23 |
|
| 24 |
#include "video.h" |
| 25 |
|
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#define VERA_ADDR_L 0x00 |
| 29 |
#define VERA_ADDR_M 0x01 |
| 30 |
#define VERA_ADDR_H 0x02 |
| 31 |
#define VERA_DATA0 0x03 |
| 32 |
#define VERA_DATA1 0x04 |
| 33 |
#define VERA_CTRL 0x05 |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static uint8_t |
| 44 |
addr_h(unsigned incr_code, bool decrement, unsigned addr_bit16) |
| 45 |
{ |
| 46 |
return (uint8_t)((incr_code << 4) | ((decrement ? 1u : 0u) << 3) | (addr_bit16 & 1u)); |
| 47 |
} |
| 48 |
|
| 49 |
static void |
| 50 |
set_address(uint8_t sel, uint32_t addr, unsigned incr_code, bool decrement) |
| 51 |
{ |
| 52 |
video_write(VERA_CTRL, sel & 1); |
| 53 |
video_write(VERA_ADDR_L, addr & 0xFF); |
| 54 |
video_write(VERA_ADDR_M, (addr >> 8) & 0xFF); |
| 55 |
video_write(VERA_ADDR_H, addr_h(incr_code, decrement, addr >> 16)); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static const int expected_increment[16] = { |
| 67 |
0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 40, 80, 160, 320, 640 |
| 68 |
}; |
| 69 |
|
| 70 |
static void |
| 71 |
test_reset_clears_the_address_registers(void) |
| 72 |
{ |
| 73 |
video_reset(); |
| 74 |
check_eq(video_get_address(0), 0u, "reset clears the first address register"); |
| 75 |
check_eq(video_get_address(1), 0u, "reset clears the second"); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
static void |
| 80 |
test_every_increment_code(void) |
| 81 |
{ |
| 82 |
for (unsigned code = 0; code < 16; code++) { |
| 83 |
video_reset(); |
| 84 |
set_address(0, 0x1000, code, false); |
| 85 |
video_write(VERA_DATA0, 0xAA); |
| 86 |
check_eq(video_get_address(0), 0x1000u + expected_increment[code], |
| 87 |
"the increment code advances the address by its documented step"); |
| 88 |
} |
| 89 |
|
| 90 |
for (unsigned code = 0; code < 16; code++) { |
| 91 |
video_reset(); |
| 92 |
set_address(0, 0x1000, code, true); |
| 93 |
video_write(VERA_DATA0, 0xAA); |
| 94 |
check_eq(video_get_address(0), 0x1000u - expected_increment[code], |
| 95 |
"and the decrement flag reverses it"); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static void |
| 102 |
test_address_wraps_at_128k(void) |
| 103 |
{ |
| 104 |
video_reset(); |
| 105 |
set_address(0, 0x1FFFF, 1, false); |
| 106 |
video_write(VERA_DATA0, 0x55); |
| 107 |
check_eq(video_get_address(0), 0u, "incrementing past the end wraps to zero"); |
| 108 |
|
| 109 |
video_reset(); |
| 110 |
set_address(0, 0x00000, 1, true); |
| 111 |
video_write(VERA_DATA0, 0x55); |
| 112 |
check_eq(video_get_address(0), 0x1FFFFu, "and decrementing below zero wraps to the top"); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static void |
| 119 |
test_the_two_address_registers_are_independent(void) |
| 120 |
{ |
| 121 |
video_reset(); |
| 122 |
set_address(0, 0x0100, 1, false); |
| 123 |
set_address(1, 0x0200, 2, false); |
| 124 |
|
| 125 |
video_write(VERA_CTRL, 0); |
| 126 |
video_write(VERA_DATA0, 0x11); |
| 127 |
check_eq(video_get_address(0), 0x0101u, "the first register uses its own increment"); |
| 128 |
check_eq(video_get_address(1), 0x0200u, "and leaves the second alone"); |
| 129 |
|
| 130 |
video_write(VERA_CTRL, 1); |
| 131 |
video_write(VERA_DATA1, 0x22); |
| 132 |
check_eq(video_get_address(1), 0x0202u, "the second uses its own"); |
| 133 |
check_eq(video_get_address(0), 0x0101u, "and leaves the first alone"); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
static void |
| 141 |
test_reading_the_data_port_advances_the_address(void) |
| 142 |
{ |
| 143 |
video_reset(); |
| 144 |
set_address(0, 0x0040, 1, false); |
| 145 |
|
| 146 |
video_read(VERA_DATA0, false); |
| 147 |
check_eq(video_get_address(0), 0x0041u, "a real read advances the address"); |
| 148 |
|
| 149 |
video_read(VERA_DATA0, false); |
| 150 |
check_eq(video_get_address(0), 0x0042u, "and again on the next read"); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static void |
| 157 |
test_data_port_round_trip(void) |
| 158 |
{ |
| 159 |
video_reset(); |
| 160 |
|
| 161 |
set_address(0, 0x0400, 1, false); |
| 162 |
for (unsigned i = 0; i < 16; i++) { |
| 163 |
video_write(VERA_DATA0, (uint8_t)(0xF0 + i)); |
| 164 |
} |
| 165 |
check_eq(video_get_address(0), 0x0410u, "sixteen writes advanced sixteen bytes"); |
| 166 |
|
| 167 |
set_address(0, 0x0400, 1, false); |
| 168 |
bool ok = true; |
| 169 |
for (unsigned i = 0; i < 16; i++) { |
| 170 |
if (video_read(VERA_DATA0, false) != (uint8_t)(0xF0 + i)) { |
| 171 |
ok = false; |
| 172 |
} |
| 173 |
} |
| 174 |
check(ok, "the bytes read back in the order they were written"); |
| 175 |
} |
| 176 |
|
| 177 |
int |
| 178 |
main(void) |
| 179 |
{ |
| 180 |
test_reset_clears_the_address_registers(); |
| 181 |
test_every_increment_code(); |
| 182 |
test_address_wraps_at_128k(); |
| 183 |
test_the_two_address_registers_are_independent(); |
| 184 |
test_reading_the_data_port_advances_the_address(); |
| 185 |
test_data_port_round_trip(); |
| 186 |
return x16_test_summary("vera_video"); |
| 187 |
} |