-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
322 lines (278 loc) · 9.26 KB
/
main.cpp
File metadata and controls
322 lines (278 loc) · 9.26 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <iostream>
#include <cstdarg>
#include <cstring>
#include <arpa/inet.h>
#include <gtest/gtest.h>
struct Memory {
private:
static constexpr __uint32_t MEMORY_SIZE = 0x10000;
__uint8_t memory[MEMORY_SIZE] { };
public:
/**
* \brief Assigns memory to 0 in other words just resets it
*/
auto mem_reset() -> void {
std::memset(memory, 0x0, MEMORY_SIZE);
}
/**
* \brief Overloads the '[]' operator for memory
* @param index Program counter's value
* @return memory[index] if the condition is met
*/
auto operator[](std::size_t index) -> __uint8_t& {
return (index >= MEMORY_SIZE) ? throw std::out_of_range("index out of bounds") : memory[index];
}
/**
* \brief Function takes a 16-bit value and writes it to a specified memory address
* @param value 16-bit value to be written to memory
* @param address Memory address where the value should be written
* @param cycles Reference to a 32-bit integer that represents the number of cycles taken by the operation
*/
auto write_two(__uint16_t value, __uint32_t address, __uint32_t& cycles) -> void {
std::memcpy(memory + address, &value, sizeof(value));
cycles -= 0x2;
}
};
class CPU {
public:
/* Registers */
[[maybe_unused]] __uint8_t AC; //Accumulator
[[maybe_unused]] __uint8_t SP; //Stack Pointer
[[maybe_unused]] __uint8_t X; //X Register
[[maybe_unused]] __uint8_t Y; //Y Register
[[maybe_unused]] __uint8_t SR; //Status Register [NV-BDIZC]
[[maybe_unused]] __uint16_t PC; //Program Counter
/* Status Flags */
[[maybe_unused]] __uint8_t N : 0x1; //Negative
[[maybe_unused]] __uint8_t V : 0x1; //Overflow
[[maybe_unused]] __uint8_t G : 0x1; //Ignored
[[maybe_unused]] __uint8_t B : 0x1; //Break
[[maybe_unused]] __uint8_t D : 0x1; //Decimal
[[maybe_unused]] __uint8_t I : 0x1; //Interrupt (IRQ disable)
[[maybe_unused]] __uint8_t Z : 0x1; //Zero
[[maybe_unused]] __uint8_t C : 0x1; //Carry
/**
* \brief Resets the processor with assigning program counter to certain address,
* assigning decimal flag to 0, and assigning memory to 0
* @param mem Instance of Memory object which will be used for resetting memory
*/
auto reset(Memory& mem) -> void {
PC = 0xFFFC;
D = 0x0;
SP = 0xFF;
mem.mem_reset();
}
/**
* \brief Fatal error indicator
* @param format Type
* @param ... (Variadic function)
*/
[[noreturn]] static auto fatal_error(char const * format, ...) noexcept -> void {
std::fflush(stdout);
std::va_list var_args;
va_start(var_args, format);
std::vfprintf(stderr, format, var_args);
std::exit(0x1);
}
/**
* \brief Fetches an instruction from memory for 8 bits (1 byte)
* @param cycles Number of Cycles
* @param memory Instance of Memory object for using memory array
* @return Program counter's value in memory array
*/
auto fetch(__uint32_t& cycles, Memory& memory) -> __uint8_t {
__uint8_t instruction = memory[PC];
PC++;
cycles--;
return instruction;
}
/**
* \brief Fetches an instruction from memory for 16 bits (2 bytes)
* @param cycles Number of Cycles
* @param memory Instance of Memory object for using memory array
* @return Program counter's value in memory array and make sure that it will be in little endian
*/
auto fetch_two(__uint32_t& cycles, Memory& memory) -> __uint16_t {
__uint16_t instruction = memory[PC];
PC++;
cycles--;
instruction |= (memory[PC] << 0x8);
PC++;
cycles -= 0x2;
return ntohs(instruction);
}
/**
* \brief Executes the instructions from memory
* @param cycles Number of Cycles
* @param memory Instance of Memory object for using memory array
* @return Number of cycles that were used
*/
auto execute(__uint32_t cycles, Memory& memory) -> __uint32_t {
const __uint32_t cycles_before = cycles;
while (cycles > 0x0) {
__uint8_t instructions = fetch(cycles, memory);
switch (instructions) {
/* Load Accumulator with Memory (LDA) */
case 0xA5 : { //Zero-page LDA
__uint8_t zero_page = fetch(cycles, memory);
AC = read_memory(cycles, zero_page ,memory);
zero_out();
break;
}
case 0xB5 : { //Zero-page-X LDA
__uint8_t zero_page = fetch(cycles, memory);
zero_page += X;
cycles--;
AC = read_memory(cycles, zero_page ,memory);
zero_out();
break;
}
case 0xAD : { //Absolute LDA
__uint16_t byte = fetch_two(cycles, memory);
AC = byte;
zero_out();
break;
}
case 0xA9 : { //Immediate LDA
__uint8_t byte = fetch(cycles, memory);
AC = byte;
zero_out();
break;
}
/* Jump To Subroutine (JSR) */
case 0x20 : { //JSR Jump To Subroutine
__uint16_t sub_address = fetch_two(cycles, memory);
memory.write_two(PC - 0x1, SP, cycles);
PC = sub_address;
SP += 2;
cycles--;
}
default : fatal_error("Error occurred");
}
return cycles_before - cycles;
}
}
/**
* \brief Sets false to Z (Zero) flag, if accumulator is equals to 0
* also sets the N (Negative) flag, if the most significant bit (bit 7) of the accumulator is set
*/
auto zero_out() -> void {
Z = (AC == 0x0);
N = (AC & 0x80) != 0x0;
}
/**
* \brief Reads memory from address
* @param address Memory address
* @return Instruction
*/
static auto read_memory(__uint32_t& cycles, __uint8_t address, Memory& memory) -> __uint8_t {
__uint8_t instruction = memory[address];
cycles--;
return instruction;
}
};
class M_TEST : public testing::Test {
public:
Memory memory;
CPU cpu { };
void SetUp() override {
cpu.reset(memory);
}
~M_TEST() override = default;
};
auto static unmodified_flags_LDA(const CPU& cpu, const CPU& _default) -> void {
EXPECT_EQ(cpu.C, _default.C);
EXPECT_EQ(cpu.I, _default.I);
EXPECT_EQ(cpu.D, _default.D);
EXPECT_EQ(cpu.B, _default.B);
EXPECT_EQ(cpu.V, _default.V);
}
/* --------------------------------- START TEST --------------------------------- */
TEST_F(M_TEST, EqualsNullExecutesZeroCycles) {
//Given:
constexpr __uint32_t CYCLES = 0x0;
//When:
__uint32_t executed = cpu.execute(CYCLES, memory);
//Then:
EXPECT_EQ(executed, CYCLES);
}
TEST_F(M_TEST, ImmediateLDA) {
//Given:
memory[0xFFFC] = 0xA9;
memory[0xFFFD] = 0x84;
//When:
CPU _default { };
constexpr __uint32_t EXPECTED_CYCLES = 0x2;
__uint32_t executed = cpu.execute(0x2, memory);
//Then:
EXPECT_TRUE(cpu.N);
EXPECT_FALSE(cpu.Z);
EXPECT_EQ(cpu.AC, 0x84);
EXPECT_EQ(executed, EXPECTED_CYCLES);
unmodified_flags_LDA(cpu, _default);
}
TEST_F(M_TEST, AbsoluteLDA) {
//Given:
memory[0xFFFC] = 0xAD;
memory[0xFFFD] = 0x42;
memory[0xFFFE] = 0x84;
memory[0x8442] = 0x33;
//When:
CPU _default { };
constexpr __uint32_t EXPECTED_CYCLES = 0x4;
__uint32_t executed = cpu.execute(0x4, memory);
//Then:
EXPECT_FALSE(cpu.Z);
EXPECT_FALSE(cpu.N);
EXPECT_EQ(cpu.AC, 0x33);
EXPECT_EQ(executed, EXPECTED_CYCLES);
unmodified_flags_LDA(cpu, _default);
}
TEST_F(M_TEST, AbsoluteXLDA) {
//Given:
cpu.X = 0x5;
memory[0xFFFC] = 0xAD;
memory[0xFFFD] = 0x42;
memory[0xFFFE] = 0x84;
memory[0x8447] = 0x33;
//When:
CPU _default { };
constexpr __uint32_t EXPECTED_CYCLES = 0x4;
__uint32_t executed = cpu.execute(0x4, memory);
//Then:
EXPECT_FALSE(cpu.Z);
EXPECT_FALSE(cpu.N);
EXPECT_EQ(cpu.AC, 0x33);
EXPECT_EQ(executed, EXPECTED_CYCLES);
unmodified_flags_LDA(cpu, _default);
}
TEST_F(M_TEST, ZeroPageLDA) {
//Given:
memory[0xFFFC] = 0xA5;
memory[0xFFFD] = 0x42;
memory[0x0042] = 0x84;
//When:
CPU _default { };
constexpr __uint32_t EXPECTED_CYCLES = 0x3;
__uint32_t executed = cpu.execute(0x3, memory);
//Then:
EXPECT_EQ(cpu.AC, 0x84);
EXPECT_EQ(executed, EXPECTED_CYCLES);
unmodified_flags_LDA(cpu, _default);
}
TEST_F(M_TEST, ZeroPageXLDA) {
//Given:
cpu.X = 0x5;
memory[0xFFFC] = 0xB5;
memory[0xFFFD] = 0x30;
memory[0x0035] = 0x85;
//When:
CPU _default { };
constexpr __uint32_t EXPECTED_CYCLES = 0x4;
__uint32_t executed = cpu.execute(0x4, memory);
//Then:
EXPECT_EQ(cpu.AC, 0x85);
EXPECT_EQ(executed, EXPECTED_CYCLES);
unmodified_flags_LDA(cpu, _default);
}
/* ---------------------------------- END TEST ---------------------------------- */