-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmulator Base.cpp
executable file
·357 lines (279 loc) · 7.05 KB
/
Emulator Base.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Emulator Base.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Memory.h"
#include "MemoryBlock.h"
#include "MemoryMap.h"
#include "CPU8080.h"
#include "UART.h"
#include <conio.h>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <time.h>
#include <Windows.h>
class Timer : public InterruptSource
{
public:
Timer() : Logger("TIMER") {}
virtual bool IsInterrupting() override
{
DWORD ticks = GetTickCount();
if (m_start == 0)
{
m_start = ticks;
return false;
}
DWORD delta = ticks - m_start;
if (delta > 100)
{
m_start = ticks;
return true;
}
return false;
}
DWORD m_start = 0;
};
void LogCallback(const char *str)
{
fprintf(stderr, str);
}
const char HexNumbers[] = "0123456789ABCDEF";
BYTE hexToByte(const std::string &hexStr)
{
if (hexStr.length() < 2)
return 0;
const char* n1 = strchr(HexNumbers, toupper(hexStr[0]));
const char* n2 = strchr(HexNumbers, toupper(hexStr[1]));
if (!n1 || !n2)
throw std::exception("Invalid hex digit");
return BYTE(n1-HexNumbers)*16 + BYTE(n2-HexNumbers);
}
WORD hexToWord(const std::string &hexStr)
{
if (hexStr.length() < 4)
return 0;
return hexToByte(hexStr.substr(0, 2)) * 256 +
hexToByte(hexStr.substr(2, 2));
}
void SaveROMBlock(std::vector<MemoryBlock> &out, int addr, std::vector<BYTE> &buffer)
{
fprintf(stdout, "Saving block 0x%04X-0x%04X, size: %d\n", addr, addr + (int)buffer.size() - 1, (int)buffer.size());
out.push_back(MemoryBlock(addr, buffer, ROM));
}
// ASLINK map file
bool readMapFile(const std::string &fileName, MemoryMap &mmap)
{
std::ifstream file(fileName.c_str(), std::ios::in);
if (!file)
{
std::cerr << "Error opening file: " << fileName << std::endl;
return false;
}
int lastAddr = -1;
int items = 0;
std::vector<BYTE> currBuffer;
while (file)
{
std::string line;
std::getline(file, line);
if (!file)
break;
// Lines with map info start with 10 spaces (" ")
size_t pos = line.find_first_not_of(' ');
if (pos != 10)
{
continue;
}
std::istringstream is(line);
MemoryMapItem item;
is >> std::hex >> item.address >> item.label >> item.module;
mmap.Add(item);
++items;
}
std::cout << "MemoryMap: Added " << items << " labels" << std::endl;
return true;
}
bool readIntelHex(const std::string &fileName, std::vector<MemoryBlock> &data)
{
std::ifstream file(fileName.c_str(), std::ios::in);
if (!file)
{
std::cerr << "Error opening file: " << fileName << std::endl;
return false;
}
int lastAddr = -1;
std::vector<BYTE> currBuffer;
while (file)
{
std::string line;
std::getline(file, line);
if (!file)
break;
if (line[0] != ':') // Lines should begin with S
goto abort;
BYTE nbBytes = hexToByte(line.substr(1, 2));
if (line.length() - 11 < nbBytes * 2)
{
std::cerr << "Unexpected record length" << std::endl;
goto abort;
}
WORD blockAddr = hexToWord(line.substr(3, 4));
BYTE type = hexToByte(line.substr(7, 1));
BYTE checksum = hexToByte(line.substr((nbBytes*2)+9, 2));
if (type == 1) // record type = eof
{
if (lastAddr != -1) // save previous block, if any
{
SaveROMBlock(data, lastAddr, currBuffer);
}
break;
}
if (type != 0) // record type should be 'data'
{
std::cerr << "Unexpected record type" << std::endl;
goto abort;
}
if (lastAddr + currBuffer.size() != blockAddr) // new block
{
if (lastAddr != -1) // save previous block
{
SaveROMBlock(data, lastAddr, currBuffer);
}
lastAddr = blockAddr;
currBuffer.clear();
}
BYTE sum = 0;
for (int i = 0; i<nbBytes + 4; i++)
{
BYTE currByte = hexToByte(line.substr(i * 2 + 1, 2));
sum += currByte;
if (i >= 4) {
currBuffer.push_back(currByte);
}
}
sum += checksum;
if (sum != 0)
{
std::cerr << "Checksum error" << std::endl;
goto abort;
}
}
file.close();
return true;
abort:
file.close();
std::cerr << "Abort." << std::endl;
return false;
}
bool readSRecord(const std::string &fileName, std::vector<MemoryBlock> &data)
{
std::ifstream file(fileName.c_str(), std::ios::in);
if (!file)
{
std::cerr << "Error opening file: " << fileName << std::endl;
return false;
}
int lastAddr = -1;
std::vector<BYTE> currBuffer;
while (file)
{
std::string temp;
std::getline(file, temp);
if (!file)
break;
if (temp[0] != 'S') // Lines should begin with S
goto abort;
if (temp[1] == '9') // record type = eof
{
if (lastAddr != -1) // save previous block, if any
{
SaveROMBlock(data, lastAddr, currBuffer);
}
break;
}
if (temp[1] != '1') // record type should be 'data'
goto abort;
BYTE nbBytes = hexToByte(temp.substr(2,2));
if (temp.length()-4 < nbBytes*2)
goto abort;
nbBytes -= 3;
WORD blockAddr = hexToWord(temp.substr(4, 4));
if (lastAddr+currBuffer.size() != blockAddr) // new block
{
if (lastAddr != -1) // save previous block
{
SaveROMBlock(data, lastAddr, currBuffer);
}
lastAddr = blockAddr;
currBuffer.clear();
}
for (int i=0; i<nbBytes; i++)
{
currBuffer.push_back(hexToByte(temp.substr(i*2+8,2)));
}
}
file.close();
return true;
abort:
file.close();
std::cerr << "Error reading data" << std::endl;
return false;
}
unsigned long elapsed;
void onCall(CPU* cpu, WORD addr)
{
elapsed = cpu->getTime();
}
void onRet(CPU* cpu, WORD addr)
{
fprintf(stderr, "\tELAPSED: %ul\n", cpu->getTime()-elapsed);
}
int main(void)
{
Logger::RegisterLogCallback(LogCallback);
Memory memory;
memory.EnableLog(false);
MemoryMap mmap;
mmap.EnableLog(false);
std::vector<MemoryBlock> monitorRom;
if (readIntelHex("../basic/main/main.ihx", monitorRom))
//if (readIntelHex("../basic/integer/integer.ihx", monitorRom))
{
for (int i=0; i<monitorRom.size(); i++)
memory.Allocate(&(monitorRom[i]));
}
if (!readMapFile("../basic/main/main.map", mmap))
{
fprintf(stderr, "Error reading map file\n");
}
MemoryBlock buffer_memory(0x8000, 0x8000, RAM);
memory.Allocate(&buffer_memory);
Timer timer;
UART uart(0x60);
uart.Init();
uart.EnableLog(false);
Interrupts interrupts;
interrupts.Allocate(CPU8080::RST65, &uart);
interrupts.Allocate(CPU8080::TRAP, &timer);
CPU8080 cpu(memory, mmap, interrupts);
cpu.AddWatch("EXECUTE", onCall, onRet);
cpu.AddDevice(uart);
cpu.Reset();
fprintf(stderr, "Press any key to continue\n");
_getch();
time_t startTime, stopTime;
time(&startTime);
while (cpu.Step() && !uart.IsEscape()) {};
time(&stopTime);
fprintf(stderr, "Time elapsed: %I64u\n", stopTime-startTime);
cpu.getTime();
fprintf(stderr, "CPU ticks: %u\n", cpu.getTime());
if (stopTime - startTime > 1)
{
fprintf(stderr, "Avg speed: %I64u ticks/s\n", cpu.getTime() / (stopTime - startTime));
}
return 0;
}