-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
91 lines (67 loc) · 2.01 KB
/
main.cpp
File metadata and controls
91 lines (67 loc) · 2.01 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
/*
RISC-V.cpp : Main entry point for the RISC-V emulator
This file contains the 'main' function which initializes the
DRAM memory and the Machine (CPU) and starts the emulator.
The program currently initializes 1 MiB of RAM and a Machine
with registers and a program counter set to the entry point.
Sources used for help:
****Mashayekhi, F. (n.d.). RISC-V Emulator in C.
Retrieved July 2025,
from https://fmash16.github.io/content/posts/riscv-emulator-in-c.html
**** "ELF loader part 1: ELF reader."
Retrieved July 2025.
from https://0xc0ffee.netlify.app/osdev/21-elf-loader-p1#elf-reader.
***** "CHIP-8 Emulator." Austin Morlan, Retrieved August 2025.
from https://austinmorlan.com/posts/chip8_emulator/
*Helped me created the instruction table.
Author: Quanayzia Garden
Date: 8-2025
Course: COSC530
Part5 Execute*/
#include <iostream>
#include <fstream>
#include "DRAM.h"
#include "Machine.h"
//#define DUMPFILE
int main(int argc, char* argv[])
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <input.elf>\n";
return 1;
}
std::string elfPath = argv[1];
#ifdef DUMPFILE
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <input.elf> <output.dump>\n";
return 1;
}
std::string dumpPath = argv[2];
#endif
try {
// 1 MiB RAM uninitialized
DRAM memory;
// Initialize Machine
Machine cpu(memory);
cpu.loadProgram(elfPath);
#ifdef DUMPFILE
// Dump memory to binary file
std::ofstream out(dumpPath, std::ios::binary);
if (!out) {
throw std::runtime_error("Failed to open output dump file.");
}
cpu.dumpMemory(out);
std::cout << "Memory dumped to: " << dumpPath << "\n";
#endif
//Emulation loop.
while (true) {
cpu.fetch();
cpu.decode();
cpu.execute();
}
}
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << "\n";
return 1;
}
return 0;
}