-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALU.hpp
68 lines (65 loc) · 1.91 KB
/
ALU.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
#pragma once
#include "Logic.hpp"
#include "Reg.hpp"
#include "Wire.hpp"
#include <type_traits>
#include <cassert>
template<typename T>
class ALU{
public:
enum OP{
ADD, SUB, SLL, SRL, SRA,
AND, OR, XOR,
LT, LTU
};
private:
const T* a;
const T* b;
const OP* op;
Reg<T> rres;
public:
ALU()=delete;
ALU(const T* ia, const T* ib, const OP* iop, T** ores):a(ia), b(ib), op(iop){
rres.in(WireFunc{return ALU::calc(*a, *b, *op);});
(*ores) = &rres;
}
ALU(const Reg<T>& ira, const Reg<T>& irb, const Reg<OP>& irop, Reg<T>& orc){
orc = WireFunc{return ALU::calc(*ira, *irb, *irop);};
}
static T calc(const T& A, const T& B, const OP& op){
switch (op) {
case ADD:
return static_cast<std::make_signed_t<T>>(A)+static_cast<std::make_signed_t<T>>(B);
break;
case SUB:
return static_cast<std::make_signed_t<T>>(A)-static_cast<std::make_signed_t<T>>(B);
break;
case SLL:
return static_cast<std::make_unsigned_t<T>>(A)<<B;
break;
case SRL:
return static_cast<std::make_unsigned_t<T>>(A)>>B;
break;
case SRA:
return static_cast<std::make_signed_t<T>>(A)>>B;
break;
case AND:
return A&B;
break;
case OR:
return A|B;
break;
case XOR:
return A^B;
break;
case LT:
return static_cast<std::make_signed_t<T>>(A) < static_cast<std::make_signed_t<T>>(B);
break;
case LTU:
return static_cast<std::make_unsigned_t<T>>(A) < static_cast<std::make_unsigned_t<T>>(B);
break;
default:
assert(0);
}
}
};