-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiles.cpp
More file actions
76 lines (63 loc) · 2.19 KB
/
Files.cpp
File metadata and controls
76 lines (63 loc) · 2.19 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
#include "Files.h"
LC3Memory memory(0xFFFF); // Create memory with size 0xFFFF (64KB)
File::File()
{
}
File::File(QString filename)
{
binaryFile.setFileName(filename);
}
File::File(QString filename, int t)
{
assemblyFile.setFileName(filename);
}
// Function to write machine code to an output file
void File::writeToBinaryFile(const LC3Memory& memory, uint16_t startAddress, uint16_t endAddress) {
if (!binaryFile.open(QIODevice::WriteOnly)) {
qWarning() << "Cannot open file for writing:" << qPrintable("MEMORY.bin");
return;
}
QDataStream output(&binaryFile);
output.setVersion(QDataStream::Qt_5_0); // Set the data stream version if necessary
for (uint16_t address = startAddress; address <= endAddress; address++) {
uint16_t value = memory.read(address);
output << value; // Write the machine code value to the binary file
}
binaryFile.close();
}
// Function to read instructions from a binary file and fill the memory
bool File::readFromBinaryFile(uint16_t startAddress) {
if (!binaryFile.open(QIODevice::ReadOnly)) {
qWarning() << "Cannot open file for reading:" << qPrintable("MEMORY.bin");
return false;
}
QDataStream input(&binaryFile);
input.setVersion(QDataStream::Qt_5_0); // Set the data stream version if necessary
uint16_t address = startAddress;
while (!input.atEnd()) {
uint16_t value;
input >> value; // Use the stream extraction operator to read a uint16_t value
memory.write(address, value); // Write the value to memory at the current address
address++;
}
binaryFile.close();
return true;
}
// Function to read an assembly file and return its lines
QVector<QString> File::readFromassemblyFile()
{
QVector<QString> lines;
if (!assemblyFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning() << "Cannot open file for reading, " << "Error:" << assemblyFile.errorString();
return lines;
}
QTextStream in(&assemblyFile);
while (!in.atEnd())
{
QString line = in.readLine();
lines.append(line.trimmed());
}
assemblyFile.close(); // Always close the file after reading
return lines;
}