summaryrefslogtreecommitdiff
path: root/tb/mem.cpp
blob: bfbc3ea7f1877b69d66ae8faae236d1482a9c81f (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
#include <cassert>
#include <cstdint>
#include <memory>

#include "mem.hpp"

namespace taller::avalon
{
	mem::mem(std::uint32_t base, std::uint32_t size)
	: base(base), mask(~(size - 1)),
	  block(std::make_unique<std::uint32_t[]>(size >> 2))
	{
		assert(!(size & 0b11) && !((size - 1) & size));
	}

	bool mem::read(std::uint32_t addr, std::uint32_t &data)
	{
		data = block[addr];
		return true;
	}

	bool mem::write(std::uint32_t addr, std::uint32_t data, unsigned byte_enable)
	{
		std::uint32_t bytes = 0;

		if(byte_enable & 0b1000)
		{
			bytes |= 0xff << 24;
		}

		if(byte_enable & 0b0100)
		{
			bytes |= 0xff << 16;
		}

		if(byte_enable & 0b0010)
		{
			bytes |= 0xff << 8;
		}

		if(byte_enable & 0b0001)
		{
			bytes |= 0xff;
		}

		block[addr] = (data & bytes) | (block[addr] & ~bytes);
		return true;
	}
}