Work on file parsing
MODIFY
src/main.cpp
src/main.cpp
using namespace std;
int main(int argc, char **argv) {
- /*
if (argc != 2) {
// insert error msg
return 1;
}
-*/
uint8_t a[4] = {1, 2, 3, 4};
- Parser parser(argv[1]);
+ Parser parser;
+ // For now, initialize parser with input argument
+ if (parser.init(argv[1])) {
+ return 1;
+ }
+
Runner runner(&parser);
+ runner.exec();
QApplication app(argc, argv);
MainWindow m;
m.show();

MODIFY
src/parser.cpp
src/parser.cpp
#include "parser.h"
-#include <fstream>
-Parser::Parser(char *fileName) {}
-Parser::~Parser() {}
+#include <iostream>
-uint32_t Parser::readWord() { return 0; }
+Parser::Parser() {}
-bool Parser::parseInstruction(int pc) {
- // Given a program counter, parse instruction from binary file. Return false
- // if instruction was not able to be parsed (or EOF)
+bool Parser::init(char *filename) {
+ // Open binary file
+ const string fname = string(filename);
+ m_fileStream = ifstream(fname.c_str(), ios::binary);
+ if (!(m_fileStream.good())) {
+ return 1;
+ }
- // m_currentInstruction = ...
+ // Create filestream iterator
+ m_fileIter = istreambuf_iterator<char>(m_fileStream);
- // For first time, ceate object. Check object, and report error if
- // incorrect file or path
- /*
-if (!binaryFile) {
- ifstream binaryFile(*fileName, ios::binary);
-}
-if (!binaryFile.good()) {
- return false;
+ // get file size
+ m_fileStream.seekg(0, ios::end);
+ m_fileSize = m_fileStream.tellg();
+ m_fileStream.clear();
+ m_fileStream.seekg(0, ios::beg);
+ return 0;
}
-// Read file with respect to pc, and put into uint32. If not possible, return
-// false.
-if (binaryFile.read(readWord(), pc)) {
- binaryFile.close();
- return true;
-} else {
- return false;
+Parser::~Parser() {}
+
+void Parser::parseFile(uint8_t *textPtr) {
+ // Parse the file in 8-bit segments and write to textPtr
+ while (m_fileIter != istreambuf_iterator<char>()) {
+ *textPtr = *m_fileIter;
+ textPtr++;
+ m_fileIter++;
}
-*/
- return false;
}
-instrType Parser::getOpType(uint32_t word) { return INVALID; }
-
-Instruction Parser::getInstruction() const { return m_currentInstruction; }
+int Parser::getFileSize() { return m_fileSize; }

MODIFY
src/parser.h
src/parser.h
#define PARSER_H
#include "defines.h"
+#include <fstream>
+
+using namespace std;
class Parser {
public:
- Parser(char *fileName);
+ Parser();
~Parser();
- bool parseInstruction(int byteOffset = 4);
- uint32_t readWord();
- instrType getOpType(uint32_t word);
+ int getFileSize();
- Instruction getInstruction() const;
+public:
+ bool init(char *filename);
+ void parseFile(uint8_t *textPtr);
private:
- Instruction m_currentInstruction;
- // FILE *filePtr;
+ ifstream m_fileStream;
+ istreambuf_iterator<char> m_fileIter;
+ int m_fileSize;
};
#endif // PARSER_H

MODIFY
src/runner.cpp
src/runner.cpp
#include "parser.h"
-Runner::Runner(Parser *parser) { m_parser = parser; }
+Runner::Runner(Parser *parser) {
+ m_parser = parser;
+
+ // memory allocation
+ // temporary allocation method - get filesize from parser, and allocate same
+ // amount of memory
+ m_textSize = m_parser->getFileSize();
+ m_text = new uint8_t[m_textSize];
+
+ // file parsing
+ m_parser->parseFile(m_text);
+}
Runner::~Runner() {}
@@ -12,14 +23,24 @@ int Runner::exec() {
// if successfull, execute the read instruction. Loop until parser
// unsuccesfully parses an instruction.
error err;
- while (m_parser->parseInstruction(m_pc)) {
- if ((err = execInstruction(m_parser->getInstruction())) != SUCCESS) {
+ while (getInstruction(m_pc)) {
+ if ((err = execInstruction(m_currentInstruction)) != SUCCESS) {
handleError(err);
}
}
return 0;
}
+bool Runner::getInstruction(int pc) {
+ if (pc < m_textSize) {
+ auto word = *((uint32_t *)m_text[pc]);
+ m_currentInstruction.word = word;
+ m_currentInstruction.type = static_cast<instrType>(word & 0x7f);
+ return 0;
+ }
+ return 1;
+}
+
error Runner::execInstruction(Instruction instr) {
switch (instr.type) {
case LUI:
@@ -54,15 +75,19 @@ error Runner::execLuiInstr(Instruction instr) {
error Runner::execJalInstr(Instruction instr) {
std::vector<uint32_t> fields = decodeJInstr(instr);
- m_pc += fields[0] << 20 | fields[1] << 1 | fields[2] << 11 | fields[3] << 12; // must be signed!
- m_reg[fields[4]] = m_pc + 4; // rd = pc + 4 // is rd equal to pc+4 before or after pc increment?
+ m_pc += fields[0] << 20 | fields[1] << 1 | fields[2] << 11 |
+ fields[3] << 12; // must be signed!
+ m_reg[fields[4]] =
+ m_pc +
+ 4; // rd = pc + 4 // is rd equal to pc+4 before or after pc increment?
return SUCCESS;
}
error Runner::execJalrInstr(Instruction instr) {
std::vector<uint32_t> fields = decodeIInstr(instr);
m_reg[fields[3]] = m_pc + 4; // store return address
- m_pc = ((int32_t)fields[0] + fields[1]) & 0xfffe; // set LSB of result to zero // shouldnt this be 0xfffffffe?
+ m_pc = ((int32_t)fields[0] + fields[1]) &
+ 0xfffe; // set LSB of result to zero // shouldnt this be 0xfffffffe?
return SUCCESS;
}
@@ -137,7 +162,10 @@ error Runner::execStoreInstr(Instruction instr) {
m_mem[target] = (uint8_t)m_reg[fields[1]];
break;
case 0b001: // SH
- m_mem[target] = (uint16_t)m_reg[fields[1]]; // will this work? m_mem[target] is uint8_t. I think we need (uint16_t)((m_mem + target)*), not sure though
+ m_mem[target] = (uint16_t)m_reg[fields[1]]; // will this work? m_mem[target]
+ // is uint8_t. I think we need
+ // (uint16_t)((m_mem +
+ // target)*), not sure though
break;
case 0b010: // SW
m_mem[target] = m_reg[fields[1]];

MODIFY
src/runner.h
src/runner.h
#ifndef RUNNER_H
#define RUNNER_H
+#include <memory>
#include <vector>
#include "defines.h"
@@ -18,16 +19,20 @@ private:
error execInstruction(Instruction instr);
void handleError(error err) const;
- int m_pc; // program counter
+ int m_pc = 0; // program counter
uint32_t m_reg[32]; // Internal registers
uint8_t *m_mem; // Stack/Heap
uint8_t *m_text; // text segment
+ int m_textSize;
uint8_t *m_data; // data segment
int m_memsize;
Parser *m_parser;
+ bool getInstruction(int pc);
+ Instruction m_currentInstruction;
+
// Instruction execution functions
error execLuiInstr(Instruction instr);
error execJalInstr(Instruction instr);

Work on file parsing