-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
100 lines (84 loc) · 3.08 KB
/
main.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
// ==================================================
// ## Chandler's CPU Emulator (v1.0) ##
// ## ##
// ## Author: Chandler Klüser Chantre ##
// ## Date: 04/23/2021 ##
// ==================================================
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include "includes/mem.cpp"
#include "includes/cpu.cpp"
using namespace std;
void clrscr(){ // clears the screen
cout << "\033[2J\033[1;1H";
}
int IntDialog(string str,bool show = 1){ // test console input dialog
int a;
if (show){
cout<<endl<<str<<endl;
}
cin>>a;
return a;
}
int main(int argc, char** argv){
// Read HEX file given as argument
// ifstream input("test.hex", ios::binary );
ifstream input(argv[1], ios::binary );
// copies all data into buffer
vector<unsigned char> buffer(istreambuf_iterator<char>(input), {});
int ExtClock = 0; // Generate an external clock counter
CPU cpu;
cpu.mem.Initialise();
// =========================== TO DO LIST ======================================
// TO DO: Add menu to manual clocks or automatic clocks (S to do a single clock, I to
// do a 6 clock to skip instruction debugging, number to make manual clocks... )
// TO DO: Fix the assembler rules
// TO DO: Add console print functionality to print hex numbers lesser than 0x10
// to print one non significant digit.
// Example:
// How it actually is: || How I wish it should be:
// 1F || 1F
// A || 0A
// 3 || 03
// FF || FF
// TO DO: Print the Instruction Argument as hex not in dec
// Example:
// How it actually is: || How I wish it should be:
// 10 || 0A
// 16 || FF
// TO DO: Add more args to the main function and a parser for those, suggestions:
// -h --help : display some help, argument entering
// -dm --dump-memory <txtfile> : dump memory in a txt file in every clock cycle
// -di --dump-instr <txtfile> : dump instructions executed in a txt file
// TO DO: Develop a quick disassembler for this CPU (hex to assembly)
// TO DO: Make a brief GUI with Qt for the emulator
// =============================================================================
// Write Memory from HEX file
for (int i=0;i<0x20;i++){
cpu.mem.Write(i,int(buffer[i]));
}
cpu.Reset( ExtClock );
int count=0;
unsigned char a=1;
while(1)
{
if (count%6==0)
{
if (cpu.Cycles>0) a = IntDialog("Show next instruction? (1: yes /0: no /-1: exit)");
if (a==1){
clrscr();
cpu.mem.Debug();
cpu.Debug();
}
else if (a!=0) break;
}
count++;
cpu.Execute( ExtClock );
ExtClock+=1;
}
return 0;
}