-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.cpp
126 lines (104 loc) · 3.01 KB
/
cpu.cpp
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
#include "../include/cpu.hpp"
#include <cstring>
#ifdef DEBUG
#include <iostream>
#include <iomanip>
#include <bitset>
#endif
uint32_t swap_uint32( uint32_t val )
{
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
return (val << 16) | (val >> 16);
}
RISCAL_CPU::RISCAL_CPU() {
/* Clear registers */
pc, sp, flags = 0;
/* Clear error bit */
error = false;
for (int i = 0; i < N_REGISTERS; i++) reg[i] = 0;
/* Clear memory space */
memset(address_space, 0x00, ADDRESS_SPACE_SIZE);
/* Allocate and clear return stack */
return_stack = new unsigned char[RETURN_STACK_SIZE];
memset(return_stack, 0x00, sizeof(char)*RETURN_STACK_SIZE);
}
RISCAL_CPU::~RISCAL_CPU() {
}
void RISCAL_CPU::load_rom(unsigned char *rom, int rom_size) {
/* ROM too large for address space constraint, trigger FAULT */
if (rom_size > ADDRESS_SPACE_SIZE) {
address_space[0] = {0x03};
error = true;
#ifdef DEBUG
std::cout << "ERROR: PROM too large. Exiting with fault." << std::endl;
#endif
} else {
memcpy(address_space, rom, rom_size);
#ifdef DEBUG
rom_dump(rom_size);
#endif
}
}
void RISCAL_CPU::load_stack(unsigned char *data, int data_size) {
/* data too large for stack space constraint, trigger FAULT */
if (data_size > RETURN_STACK_SIZE) {
address_space[0] = {0x03};
error = true;
#ifdef DEBUG
std::cout << "ERROR: Passed stack data too large. Exiting with fault." << std::endl;
#endif
} else {
memcpy(return_stack, data, data_size);
sp = data_size;
#ifdef DEBUG
stack_dump();
#endif
}
}
unsigned char *RISCAL_CPU::run() {
#ifdef DEBUG
std::cout << "[DBG] EXECUTION START" << std::endl;
#endif
bool returned = false;
while(!returned){
returned = execute_instruction();
#ifdef DEBUG
register_dump();
stack_dump();
#endif
pc = pc + sizeof(cpu_word);
}
/* In case RISCAL had a fault, it will return a null ptr */
if (error) {
return nullptr;
}
/* Otherwise, RISCAL will return the return stack */
return return_stack;
}
bool RISCAL_CPU::execute_instruction() {
cpu_word data_be;
memcpy(&data_be, &address_space[0] + pc, sizeof(cpu_word));
cpu_word data = swap_uint32(data_be);
int opcode = (data >> 20) & 0xFFF;
if (opcode == HALT) {
#ifdef DEBUG
std::cout << "[DBG] HALT" << std::endl;
#endif
return true;
}
if (opcode == FAULT) {
op_fault(data);
}
if (error) return true;
if(op_function_map.find(opcode) == op_function_map.end()) {
#ifdef DEBUG
std::cout << "[DBG] Invalid opcode! Symbol: " <<std::bitset<32>(data) << std::endl;
#endif
op_fault(data);
return true;
}else {
op_f fp = op_function_map[opcode];
(this->*fp)(data);
}
return false;
}