-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignals.cpp
More file actions
77 lines (66 loc) · 1.38 KB
/
signals.cpp
File metadata and controls
77 lines (66 loc) · 1.38 KB
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
#include "signals.hpp"
int check_index(int index, int size)
{
if (index < 0 || index >= size) {
std::cerr << "Error: Index out of bounds." << std::endl;
return 1;
}
return 0;
}
bit get_bit(bus b, int index) {
check_index(index, BUS_SIZE);
return (b >> index) & 0x0001;
}
void set_bit(bus &b, int index, bit a)
{
check_index(index, BUS_SIZE);
if (a)
b |= (1 << index);
else
b &= ~(1 << index);
}
// think of this as an 8 bit bus
bit combine_bits_three(bit a, bit b, bit c)
{
return c | (b << 1) | (a << 2);
}
void combine_bits(bit &a, bit b, int index)
{
a |= (b << index);
}
bit uncombine_bits(bit a, int index)
{
check_index(index, COMB_BIT_SIZE);
return (a >> index) & 0x0001;
}
// Done using wires, not gates so represented this way
bus sign_extend(bus b, int len)
{
bit a = get_bit(b, len - 1);
for (int i = len; i < BUS_SIZE; i++)
{
set_bit(b, i, a);
}
return b;
}
// Done using wires, not gates so represented this way
bus zero_extend(bus b, int len)
{
for (int i = len; i < BUS_SIZE; i++)
{
set_bit(b, i, 0);
}
return b;
}
string bit_str(bit a)
{
return std::to_string(static_cast<int>(a));
}
string bus_str(bus b)
{
return std::bitset<sizeof(bus) * 8>(b).to_string();
}
string bit_str_binary(bit a)
{
return std::bitset<sizeof(bit) * 8>(a).to_string();
}