summaryrefslogtreecommitdiff
path: root/tb/top/conspiracion/avalon.hpp
blob: b184999e0af5e7c2443e9c2ac563a72d74adedf0 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#ifndef TALLER_AVALON_HPP
#define TALLER_AVALON_HPP

#include <cassert>
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include <vector>

#include <verilated.h>

namespace taller::avalon
{
	union line
	{
		__int128  qword;
		VlWide<4> verilated;

		struct
		{
			std::uint64_t lo, hi;
		};

		struct
		{
			std::uint32_t words[4];
		};

		inline line() noexcept
		: lo{0}, hi{0}
		{}

		inline line(VlWide<4> verilated) noexcept
		: verilated(verilated)
		{}

		inline operator VlWide<4>() const noexcept
		{
			return this->verilated;
		}

		inline bool operator==(const VlWide<4> &verilated) const noexcept
		{
			line verilated_line{verilated};
			return this->hi == verilated_line.hi && this->lo == verilated_line.lo;
		}

		inline bool operator!=(const VlWide<4> &verilated) const noexcept
		{
			return !(*this == verilated);
		}
	};

	static_assert(sizeof(line) == 16);

	class slave
	{
		public:
			inline slave(std::uint32_t base, std::uint32_t size, std::size_t word_size) noexcept
			: base(base),
			  mask(~(size - 1)),
			  word(log2i(word_size))
			{
				assert(!((word_size - 1) & word_size));
				assert(!(base & word_mask()) && !(size & word_mask()) && !((size - 1) & size));
			}

			inline std::uint32_t base_address() noexcept
			{
				return base;
			}

			inline std::uint32_t address_mask() noexcept
			{
				return mask;
			}

			inline std::uint32_t word_mask() noexcept
			{
				return (1 << word) - 1;
			}

			inline std::size_t word_size() noexcept
			{
				return 1 << word;
			}

			inline unsigned word_bits() noexcept
			{
				return word;
			}

			inline std::uint32_t address_span() noexcept
			{
				return ~mask + 1;
			}

			inline virtual void tick() noexcept
			{}

			inline virtual void tick_falling() noexcept
			{}

			inline virtual void bail() noexcept
			{}

			virtual bool read(std::uint32_t addr, std::uint32_t &data)
			{
				line line_data;
				if (!this->read_line(addr >> 2, line_data, 0b1111 << ((addr & 0b11) * 4)))
					return false;

				data = line_data.words[addr & 0b11];
				return true;
			}

			virtual bool read_line(std::uint32_t addr, line &data, unsigned byte_enable)
			{
				//XXX: Realmente es esto lo que genera qsys? Avalon spec no es clara
				if (byte_enable & 0x000f)
					return this->read(addr << 2, data.words[0]);
				else if (byte_enable & 0x00f0)
				    return this->read((addr << 2) + 1, data.words[1]);
				else if (byte_enable & 0x0f00)
				    return this->read((addr << 2) + 2, data.words[2]);
				else if (byte_enable & 0xf000)
				    return this->read((addr << 2) + 3, data.words[3]);

				return true;
			}

			virtual bool write
			(
			 	std::uint32_t addr, std::uint32_t data, unsigned byte_enable = 0b1111
			) {
				line line_data;
				line_data.words[addr & 0b11] = data;

				return this->write_line(addr >> 2, line_data, byte_enable << ((addr & 0b11) * 4));
			}

			virtual bool write_line(std::uint32_t addr, const line &data, unsigned byte_enable)
			{
				unsigned offset = 0;
				if (byte_enable & 0x00f0)
					offset = 1;
				else if (byte_enable & 0x0f00)
					offset = 2;
				else if (byte_enable & 0xf000)
					offset = 3;

				return this->write
				(
					(addr << 2) + offset,
					data.words[offset],
					(byte_enable >> (offset * 4)) & 0b1111
				);
			}

			inline virtual bool irq() noexcept
			{
				return false;
			}

		private:
			std::uint32_t base;
			std::uint32_t mask;
			unsigned      word;

			static inline int log2i(int i)
			{
		    	return sizeof(int) * 8 - __builtin_clz(i) - 1;
			}
	};

	struct irq_lines
	{
		slave *timer    = nullptr;
		slave *jtaguart = nullptr;
	};

	class interrupt_controller : private slave
	{
		public:
			interrupt_controller(std::uint32_t base) noexcept;

			virtual bool read(std::uint32_t addr, std::uint32_t &data) noexcept final override;
			virtual bool write(std::uint32_t addr, std::uint32_t data, unsigned byte_enable) noexcept final override;

			virtual bool irq() noexcept final override;

			inline slave &as_slave() noexcept
			{
				return *this;
			}

			inline irq_lines &lines() noexcept
			{
				return irqs;
			}

		private:
			irq_lines     irqs;
			std::uint32_t mask = 0;

			std::uint32_t status() noexcept;
	};

	class avl_bus_error : public std::runtime_error
	{
		public:
			using std::runtime_error::runtime_error;
	};

	template<class Platform>
	class interconnect
	{
		public:
			interconnect(Platform &plat) noexcept;

			bool tick(bool clk) noexcept;
			void tick_rising();
			void tick_falling() noexcept;

			void attach(slave &dev);
			void attach_intc(interrupt_controller &intc);
			void bail() noexcept;

			bool dump(std::uint32_t addr, std::uint32_t &word);
			bool patch(std::uint32_t addr, std::uint32_t readdata);

		private:
			struct binding
			{
				std::uint32_t base;
				std::uint32_t mask;
				slave         &dev;
			};

			Platform             &plat;
			slave*                active = nullptr;
			std::vector<binding>  devices;
			interrupt_controller *root_intc      = nullptr;
			std::uint32_t         avl_address    = 0;
			line                  avl_writedata;
			unsigned              avl_byteenable = 0;
			bool                  avl_read       = false;
			bool                  avl_write      = false;

			slave *resolve_external(std::uint32_t avl_address);
	};
}

#include "avalon.impl.hpp"

#endif