summaryrefslogtreecommitdiffstats
path: root/riscv/firmware.c
blob: c39ebbd81211b2add877434ae766409afcf9c433 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdint.h>

#ifndef SHIFT_COUNTER_BITS
#error SHIFT_COUNTER_BITS must be defined as 4 (for simulation) or 18 (for hardware bitstreams)!
#endif

void output(uint8_t c)
{
	*(volatile char*)0x10000000 = c;
}

uint8_t gray_encode_simple(uint8_t c)
{
	return c ^ (c >> 1);
}

uint8_t gray_encode_bitwise(uint8_t c)
{
	unsigned int in_buf = c, out_buf = 0, bit = 1;
	for (int i = 0; i < 8; i++) {
		if ((in_buf & 1) ^ ((in_buf >> 1) & 1))
			out_buf |= bit;
		in_buf = in_buf >> 1;
		bit = bit << 1;
	}
	return out_buf;
}

uint8_t gray_decode(uint8_t c)
{
	uint8_t t = c >> 1;
	while (t) {
		c = c ^ t;
		t = t >> 1;
	}
	return c;
}

void gray(uint8_t c)
{
	uint8_t gray_simple = gray_encode_simple(c);
	uint8_t gray_bitwise = gray_encode_bitwise(c);
	uint8_t gray_decoded = gray_decode(gray_simple);

	if (gray_simple != gray_bitwise || gray_decoded != c)
		while (1) asm volatile ("ebreak");

	output(101);
}

void main()
{
	while (1)
      output(0xAA);
}