summaryrefslogtreecommitdiff
path: root/src/cli/cli.cc
blob: 58575cbb6352bc7c0c01744473b44c0ea050109d (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
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
#include "cli.h"
#include "cache.h"
#include "definitions.h"
#include "dram.h"
#include "response.h"
#include "accessor.h"
#include "utils.h"
#include <iostream>

Cli::Cli()
{
	this->cache = nullptr;
	this->cycle = 0;
	this->initialize();

	commands['l'] = [this](std::vector<std::string> args) {
		Accessor a;
		if (args.size() >= 2) {
			try {
				a = match_accessor_or_die(args[0]);
				load(a, std::stoi(args[1]));
			} catch (const std::invalid_argument &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) {
		Accessor a;
		if (args.size() >= 3) {
			try {
				a = match_accessor_or_die(args[0]);
				store(a, std::stoi(args[1]), std::stoi(args[2]));
			} catch (const std::invalid_argument &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) {
		(void)args;
		reset();
		return;
	};

	commands['p'] = [this](std::vector<std::string> args) {
		if (args.size() >= 1) {
			try {
				peek(std::stoi(args[0]));
			} catch (const std::invalid_argument &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) {
		(void)args;
		help();
		return;
	};
}

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

void Cli::help()
{
	std::cout
		<< "Available commands:" << std::endl
		<< "  [l]oad <address> - Load data from memory at the specified "
		   "address"
		<< std::endl
		<< "  [s]tore <accessor> <data> <address> - Stores data into memory at "
		   "specified address. Acessor must be one of: [f]etch, [m]em"
		<< "  [p]eek <storage-level> <base> <lines> - side door function that "
		   "peeks the current status of the entire memory subsystem"
		<< std::endl
		<< "  [r]eset - side door function that resets the memory "
		   "configuration and "
		   "cycles"
		<< std::endl
		<< "  [h]elp  - prints this help text" << std::endl
		<< "  [q]uit  - quits the program" << std::endl;
}

void Cli::load(Accessor accessor, int address)
{
	address = wrap_address(address);
	const auto default_flags = std::cout.flags();
	const auto default_fill = std::cout.fill();

	signed int data;
	Response r = this->cache->read_word(accessor, address, data);
	std::cout << r << " to " << accessor << " reading " << address << std::endl;
	if (r == OK)
		std::cout << "  Got: " << std::hex << data << std::endl;

	std::cout.flags(default_flags);
	std::cout.fill(default_fill);
}

void Cli::store(Accessor accessor, int data, int address)
{
	address = wrap_address(address);
	Response r = this->cache->write_word(accessor, data, address);
	std::cout << r << " to " << accessor << " storing " << data << " in "
			  << address << std::endl;
}

void Cli::reset()
{
	this->initialize();
	std::cout << "Done." << std::endl;
}

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

	Cache *c = dynamic_cast<Cache *>(curr);
	if (c) {
		std::cout << *c << std::endl;
	} else {
		std::cout << *dynamic_cast<Dram *>(curr) << std::endl;
		;
	}
}

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

	bool run = true;
	while (run) {
		std::cout << this->cycle << "> ";
		std::getline(std::cin, input);

		std::istringstream iss1(input);
		std::vector<std::string> words;
		std::string sentence;
		std::string word;

		while (std::getline(iss1, sentence, ';')) {
			words.clear();
			std::istringstream iss2(sentence);

			while (iss2 >> word) {
				words.push_back(word);
			}
			if (words.empty())
				continue;

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

			if (command == "q") {
				run = false;
				break;
			}

			auto it = commands.find(tolower(command[0]));
			if (it != commands.end()) {
				it->second(words);
			} else {
				std::cout << "Unknown command: '" << command
						  << "'. Type 'help' for available commands."
						  << std::endl;
			}
		}
	}
}

void Cli::initialize()
{
	Logger *global_log = Logger::getInstance();

	global_log->log(INFO, "Resetting memory configuration and cycle.");

	if (this->cache != nullptr)
		delete this->cache;

	Dram *d = new Dram(MEM_DELAY);
	this->cache = new Cache(d, L1_CACHE_DELAY);
	this->cycle = 1;
}

Accessor Cli::match_accessor_or_die(std::string s)
{
	if (tolower(s[0]) == 'f')
		return FETCH;
	else if (tolower(s[0]) == 'm')
		return MEM;
	else
		throw std::invalid_argument(s);
}