-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
151 lines (129 loc) · 6.52 KB
/
Copy pathmain.cpp
File metadata and controls
151 lines (129 loc) · 6.52 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
#include <iostream>
#include <string>
#include <fstream>
#include "parser.h"
#include "simulator.h"
// Helper: print the current config file to stdout
void showConfig(const std::string& configFile) {
std::ifstream f(configFile);
if (!f.is_open()) {
std::cout << "WARNING: Could not open '" << configFile << "'!\n";
return;
}
std::cout << "\n--- Current Config (" << configFile << ") ---\n";
std::string line;
while (std::getline(f, line)) std::cout << line << "\n";
std::cout << "--------------------------------------\n";
}
// =============================================================================
// main — Interactive CLI
//
// Mode 1: Assembly Replay (Phases 1 & 2)
// Prompts for an .asm file, forwarding toggle, single-step toggle.
// Behaviour identical to original Phase 1/2 simulator.
//
// Mode 2: Trace Replay (Phase 3)
// Prompts for a trace file path.
// All L/S addresses are treated as virtual addresses.
// VM subsystem (TLB + Page Table) is activated automatically.
// No forwarding/single-step prompts (trace mode always runs to completion).
// =============================================================================
int main() {
std::cout << "==================================================\n";
std::cout << " RISC-V PIPELINE SIMULATOR — Phase 3 Edition \n";
std::cout << " Team: __BLANK__ | CS24B055 & CS24B057 \n";
std::cout << "==================================================\n";
const std::string configFile = "config.txt";
while (true) {
// ── Mode Selection ────────────────────────────────────────────────────
std::cout << "\nSelect mode:\n"
<< " [1] Assembly Mode (Phases 1 & 2 — .asm files)\n"
<< " [2] Trace Replay (Phase 3 — .trace files with VM)\n"
<< " [0] Exit\n"
<< "Choice: ";
std::string choice_str;
std::getline(std::cin, choice_str);
if (choice_str == "0" || choice_str == "exit" || choice_str == "quit") {
std::cout << "Exiting simulator. Goodbye!\n";
return 0;
}
bool trace_mode = (choice_str == "2");
// ── File selection ────────────────────────────────────────────────────
std::string inputFile;
std::ifstream fileCheck;
if (!trace_mode) {
while (true) {
std::cout << "\nEnter assembly filename (or 'back' to return): ";
std::getline(std::cin, inputFile);
if (inputFile == "back") break;
fileCheck.open(inputFile);
if (fileCheck.is_open()) { fileCheck.close(); break; }
std::cout << "Error: File '" << inputFile << "' not found.\n";
}
if (inputFile == "back") continue;
} else {
while (true) {
std::cout << "\nEnter trace file path (or 'back' to return):\n"
<< " [Hint: traces are in the phase3_traces folder]\n"
<< "Path: ";
std::getline(std::cin, inputFile);
if (inputFile == "back") break;
fileCheck.open(inputFile);
if (fileCheck.is_open()) { fileCheck.close(); break; }
std::cout << "Error: File '" << inputFile << "' not found.\n";
}
if (inputFile == "back") continue;
}
// ── Show config & allow modification ─────────────────────────────────
showConfig(configFile);
std::cout << "Edit '" << configFile
<< "' if needed, then press [ENTER] to continue...";
std::string dummy;
std::getline(std::cin, dummy);
// ── Parse input file ──────────────────────────────────────────────────
Parser p;
std::vector<Instruction> program;
if (trace_mode) {
program = p.parseTraceFile(inputFile);
std::cout << "\n[Trace mode] Loaded " << program.size()
<< " instructions from: " << inputFile << "\n";
} else {
program = p.parseFile(inputFile);
}
if (program.empty()) {
std::cout << "ERROR: No instructions loaded. Check the file.\n";
continue;
}
// ── Assembly-mode options (not shown in trace mode) ───────────────────
bool enable_forwarding = true;
bool single_step = false;
if (!trace_mode) {
std::cout << "\nEnable Pipeline Forwarding? (1=ON, 0=OFF) [Default: 1]: ";
std::string fwd_input;
std::getline(std::cin, fwd_input);
if (fwd_input == "0") enable_forwarding = false;
std::cout << "Enable Single Step Execution? (y/n) [Default: n]: ";
std::string step_input;
std::getline(std::cin, step_input);
if (step_input == "y" || step_input == "Y") single_step = true;
}
// ── Build and configure simulator ─────────────────────────────────────
Simulator sim(program);
sim.loadConfig(configFile);
sim.setForwarding(enable_forwarding);
sim.setTraceMode(trace_mode);
// ── Run ───────────────────────────────────────────────────────────────
std::cout << "\n==================================================\n";
std::cout << "Executing: " << inputFile << "\n";
std::cout << "Mode: " << (trace_mode ? "Trace Replay (Phase 3)" : "Assembly") << "\n";
std::cout << "Instructions:" << program.size() << "\n";
if (!trace_mode) {
std::cout << "Forwarding: " << (enable_forwarding ? "ON" : "OFF") << "\n";
std::cout << "Single Step: " << (single_step ? "ON" : "OFF") << "\n";
}
std::cout << "==================================================\n";
sim.run(single_step);
std::cout << "\nSimulation complete.\n";
}
return 0;
}