summaryrefslogtreecommitdiff
path: root/src/cli/cli.cc
blob: fea44d7f4582cd8740589f1215a71c30a86d16e3 (plain)
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
#include "cli.h"
#include "cache.h"
#include "definitions.h"
#include "dram.h"
#include <iostream>
#include <sstream>
#include <vector>

Cli::Cli()
{
	Dram *d = new Dram(MEM_SIZE, MEM_DELAY);
	this->cache = new Cache(d, L1_CACHE_DELAY);

	commands["l"] = [this](std::vector<std::string> args) {
		if (args.size() >= 1) {
			try {
				load(std::stoi(args[0]));
			} catch (const std::exception &e) {
				std::cerr << "Invalid input: " << e.what() << std::endl;
			}
		} else {
			std::cout << "Usage: l <memory-address>\n";
		}
		return;
	};

	commands["s"] = [this](std::vector<std::string> args) {
		if (args.size() >= 2) {
			try {
				store(std::stoi(args[0]), std::stoi(args[1]));
			} catch (const std::exception &e) {
				std::cerr << "Invalid input: " << e.what() << std::endl;
			}
		} else {
			std::cout << "Usage: s <memory-address> <data>\n";
		}
		return;
	};

	commands["r"] = [this](std::vector<std::string> args) {
		reset();
		return;
	};

	commands["v"] = [this](std::vector<std::string> args) {
		if (args.size() >= 3) {
			try {
				view(
					std::stoi(args[0]), std::stoi(args[1]), std::stoi(args[2]));
			} catch (const std::exception &e) {
				std::cerr << "Invalid input: " << e.what() << std::endl;
			}
		} else {
			std::cout << "Usage: v <storage-level> <base> <lines>\n";
		}
		return;
	};

	commands["h"] = [this](std::vector<std::string> args) {
		help();
		return;
	};
}

Cli::~Cli() { delete this->cache; }

void Cli::help()
{
	std::cout
		<< "Available commands:\n"
		<< "  l <memory-address> - Load data from memory at the specified "
		   "address\n"
		<< "  s <memory-address> <data> - Stores data into "
		   "memory at specified address\n"
		<< "  c - manually advances the clock\n"
		<< "  r - side door function that resets the memory configuration and "
		   "cycles\n"
		<< "  u <memory-address> <data> - side door function that updates "
		   "the memory at the specified address with data provided\n"
		<< "  v <storage-level> <base> <lines> - side door function that views "
		   "the current status of the entire memory subsystem\n"
		<< "  h - Prints this help text\n"
		<< "  q - Quits the program\n";
}

void Cli::load(int memory_address)
{
	std::cout << "Loading data from memory address " << memory_address;
}

void Cli::store(int memory_address, int data)
{
	std::cout << "Storing " << data << " into memory address "
			  << memory_address;
}

void Cli::update(int memory_address, int data)
{
	std::cout << "Resetting memory configuration and cycles.\n";
};

void Cli::clock()
{
	std::cout << "Resetting memory configuration and cycles.\n";
}

void Cli::reset()
{
	std::cout << "Resetting memory configuration and cycles.\n";
}

void Cli::view(int level, int base, int lines)
{
	Storage *curr = this->cache;
	for (int i = 0; i < level; ++i) {
		if (!curr) {
			std::cerr << "Level " << level << " of storage does not exist.\n";
			return;
		}
		curr = curr->get_lower();
	}

	std::vector<std::array<signed int, LINE_SIZE>> data =
		curr->view(base, lines);

	if (dynamic_cast<const Cache *>(curr)) {

	} else {
		std::cout << "dram";
	}
}

void Cli::run()
{
	std::cout << "Memory Command Processor Started. Type 'h' for a list of "
				 "commands.\n";

	std::string input;
	while (true) {
		std::cout << "> ";
		std::getline(std::cin, input);
		std::istringstream iss(input);
		std::vector<std::string> tokens;
		std::string word;

		while (iss >> word)
			tokens.push_back(word);
		if (tokens.empty())
			continue;

		std::string command = tokens[0];
		tokens.erase(tokens.begin());

		if (command == "q") {
			std::cout << "Exiting...\n";
			break;
		}

		auto it = commands.find(command);
		if (it != commands.end()) {
			it->second(tokens);
		} else {
			std::cout
				<< "Unknown command. Type 'help' for available commands.\n";
		}
	}
}

void Cli::initialize()
{
	if (this->cache)
		delete this->cache;
}