-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
145 lines (128 loc) · 3.01 KB
/
main.cpp
File metadata and controls
145 lines (128 loc) · 3.01 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
#include <stdio.h>
#include <stdlib.h>
/*
===========================================
= Program made by: Michael Crawford
= Credits goes to Dave Poo on Youtube
= -for making that tutorial
= Date Created 08/09/2021 (August 9, 2021)
= Last Edited on 08/21/2021 at 012:12 AM
= Last Edit from Michael Crawford
===========================================
*/
using Byte = unsigned char;
using Word = unsigned short;
using u32 = unsigned int;
struct Mem
{
static constexpr u32 MAX_MEM = 1024 * 64;
Byte Data[MAX_MEM];
void Initialise()
{
for (u32 i = 0; i < MAX_MEM; i++)
{
Data[i] = 0;
}
}
// read 1 byte
Byte operator[](u32 Address) const
{
// assert here is < MAX_MEM
return Data[Address];
}
// write 1 byte
Byte &operator[](u32 Address)
{
// assert here is < MAX_MEM
return Data[Address];
}
};
//* the main thing
struct CPU
{
Word PC; //program pointer
Byte SP; // stack pointer
Byte A, X, Y; //registers
Byte C : 1; // status flag
Byte Z : 1; // status flag
Byte I : 1; // status flag
Byte D : 1; // status flag
Byte B : 1; // status flag
Byte V : 1; // status flag
Byte N : 1; // status flag
void Reset(Mem &memory)
{
PC = 0xFFFC;
SP = 0x0100;
C = Z = I = D = B = V = N = 0;
A = X = Y = 0;
memory.Initialise();
}
Byte FetchByte(u32 Cycles, Mem &memory)
{
Byte Data = memory[PC];
PC++;
Cycles--;
return Data;
}
Byte ReadByte(u32 &Cycles, Byte Address, Mem &memory)
{
Byte Data = memory[Address];
Cycles--;
return Data;
}
// opcode
static constexpr Byte
INS_LDA_IM = 0xA9;
INS_LDA_ZP
void LDASetStatus()
{
Z = (A == 0);
N = (A & 0b10000000) > 0;
};
void ZeroPageAddress(u32 &Cycles, Byte &Address, Mem &memory)
{
FetchByte(Cycles, memory);
A = ReadByte(Cycles, Address, memory);
}
// Executes instructions
void Execute(u32 Cycles, Mem &memory)
{
while (Cycles > 0)
{
Byte Ins = FetchByte(Cycles, memory);
Byte ZeroPageAddress = 0;
switch (Ins)
{
case INS_LDA_IM:
{
Byte Value =
FetchByte(Cycles, memory);
A = Value;
LDASetStatus();
}
break;
case INS_LDA_ZP:
ZeroPageAddress = FetchByte(Cycles, memory);
A = ReadByte(Cycles, ZeroPageAddress, memory);
LDASetStatus();
break;
default:
printf("Instruction Not handled %d", Ins);
}
}
}
};
int main()
{
Mem mem;
CPU cpu;
cpu.Reset(mem);
printf("CPU Reset~");
mem[0xFFFC] = CPU::INS_LDA_ZP;
mem[0xFFFD] = 0x42;
mem[0x0042] = 0x84;
cpu.Execute(3, mem);
printf("CPU reset!");
return 0;
}