-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBus.hpp
90 lines (71 loc) · 1.97 KB
/
Bus.hpp
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
#pragma once
#include <memory>
#include <stdexcept>
class Bus
{
public:
class BusDevice
{
public:
virtual bool is_address_for_device(unsigned int address) = 0;
virtual unsigned char get_byte(unsigned int address)
{
throw std::logic_error("not implemented");
return 0;
}
virtual unsigned short get_halfword(unsigned int address)
{
unsigned short result = 0;
result = (get_byte(address + 1) << 8) |
get_byte(address);
return result;
}
virtual unsigned int get_word(unsigned int address)
{
unsigned int result = 0;
result = (get_byte(address + 3) << 24) |
(get_byte(address + 2) << 16) |
(get_byte(address + 1) << 8) |
get_byte(address);
return result;
}
virtual void set_byte(unsigned int address, unsigned char value)
{
throw std::logic_error("not implemented");
}
virtual void set_halfword(unsigned int address, unsigned short value)
{
for (int offset = 0; offset < 2; offset++)
{
unsigned int current_address = address + offset;
unsigned char byte_value = value & 0xFF;
value >>= 8;
set_byte(current_address, byte_value);
}
}
virtual void set_word(unsigned int address, unsigned int value)
{
for (int offset = 0; offset < 4; offset++)
{
unsigned int current_address = address + offset;
unsigned char byte_value = value & 0xFF;
value >>= 8;
set_byte(current_address, byte_value);
}
}
};
static Bus* get_instance();
void register_device(BusDevice * device);
unsigned char get_byte(unsigned int address);
unsigned short get_halfword(unsigned int address);
unsigned int get_word(unsigned int address);
void set_byte(unsigned int address, unsigned char value);
void set_halfword(unsigned int address, unsigned short value);
void set_word(unsigned int address, unsigned int value);
private:
Bus() = default;
~Bus() = default;
BusDevice * get_bus_device_for_address(unsigned int address);
int num_devices = 0;
BusDevice * bus_devices[20] = { nullptr };
};