-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntel8080.cpp
179 lines (143 loc) · 3.64 KB
/
Intel8080.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
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
#include "Intel8080.h"
Intel8080::Intel8080(char *gameDir)
{
m_gameDir = gameDir;
ResetState();
GenerateBuffer();
}
uint8_t Intel8080::Parity(uint8_t val)
{
int counter;
int i = 0;
for (i = 0; i < 16; i++)
{
counter += (val >> i) & 0x1;
}
return (counter % 2) == 0 ? 1 : 0;
}
void Intel8080::ResetState()
{
m_state.a = 0;
m_state.b = 0;
m_state.c = 0;
m_state.d = 0;
m_state.e = 0;
m_state.h = 0;
m_state.l = 0;
m_state.sp = 0;
m_state.pc = 0;
m_state.memory = NULL; /////////////////To change when I know the initial Memory position
m_state.int_enable = 0; // To be changed later on when starting the emulator ig.
//Reset the ConditionCodes
m_state.cc.z = 0;
m_state.cc.s = 0;
m_state.cc.p = 0;
m_state.cc.pad = 0;
m_state.cc.cy = 0;
m_state.cc.ac = 0;
}
void Intel8080::GenerateBuffer()
{
FILE *f = fopen(m_gameDir, "rb");
if (f == NULL)
{
std::cout << "error: Couldn't open " << m_gameDir << std::endl;
}
//Get the file size and read it into a memory buffer
fseek(f, 0L, SEEK_END);
int fsize = ftell(f);
m_gameBufferLen = fsize;
fseek(f, 0L, SEEK_SET);
m_gameBuffer = (unsigned char *)malloc(fsize);
fread(m_gameBuffer, fsize, 1, f);
fclose(f);
}
void Intel8080::UnimplementedInstruction(unsigned char *instruction)
{
//pc will have advanced one, so undo that
printf("Error: Instruction $#%02x not yet implemented\n", m_gameBuffer[m_state.pc]);
exit(1);
}
int Intel8080::run()
{
while (m_state.pc < m_gameBufferLen)
{
Emulate8080OpCode();
}
}
int Intel8080::Emulate8080OpCode()
{
unsigned char *opcode = &m_state.memory[m_state.pc];
switch (*opcode)
{
case 0x00:
printf("NOP");
break;
case 0x01:
{ //LXI B,D16 B <- byte 3, C <- byte 2
m_state.b = opcode[2];
m_state.c = opcode[1];
m_state.pc += 2;
break;
}
case 0x02:
{
m_state.memory[(m_state.b << 8) | (m_state.c)] = m_state.a;
break;
}
case 0x03:
printf("INX B");
break;
case 0x04:
printf("INR B");
break;
case 0x05:
m_state.b = m_state.b - 1;
m_state.cc.z = (m_state.b & 0xFF) == 0; //Zero flag
m_state.cc.s = (m_state.b & 0x80) == 1; // Sign flag
m_state.cc.ac = (m_state.b < 0xF) == 1; //Aux Carry Flag
m_state.cc.p = Parity(m_state.b & 0xFF); //Parity
break;
case 0x06:
m_state.b = opcode[1];
break;
case 0x09:
uint8_t temp = ((m_state.h << 8) | (m_state.l)) + ((m_state.b << 8) | (m_state.c));
m_state.h = (temp >> 8) & 0xFF;
m_state.l = temp & 0xFF;
m_state.cc.cy = (temp > 0xFF); //Carry flag
break;
case 0x0d:
m_state.c = m_state.c - 1;
m_state.cc.z = (m_state.c & 0xFF) == 0; //Zero flag
m_state.cc.s = (m_state.c & 0x80) == 1; // Sign flag
m_state.cc.ac = (m_state.c < 0xF) == 1; //Aux Carry Flag
m_state.cc.p = Parity(m_state.c & 0xFF); //Parity
break;
case 0x0e:
m_state.c = opcode[1];
break;
case 0x0f:
int8_t tempA = m_state.a;
int8_t prev0 = tempA & 0x01;
m_state.a = (m_state.a >> 1) & ((prev0 << 7) | 0x7F);
m_state.cc.cy = prev0;
break;
case 0x11:
m_state.d = opcode[2];
m_state.e = opcode[1];
m_state.pc += 2;
break;
default:
{
UnimplementedInstruction(opcode);
}
}
m_state.pc += 1;
}
int main()
{
int wow = Parity(26);
std::cout << wow;
return 0;
}