-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc3vm.hpp
More file actions
91 lines (74 loc) · 2.09 KB
/
Copy pathlc3vm.hpp
File metadata and controls
91 lines (74 loc) · 2.09 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
#pragma once
#include <memory>
#include <cstdint> // uint16_t
#include <array>
#include <string>
namespace lc3 {
class Memory;
class Registers;
class ConditionFlags;
class InstructionRegistry;
/**
* @class LC3VM
* @brief The main orchestrator of the LC3 Virtual Machine.
* This class represents the hardware state and the execution engine of the emulator.
* It strictly owns the hardware components (Memory, Registers, Condition Flags)
* and manages the fetch-decode-execute lifecycle of the program.
* Instruction execution is delegated to concrete executors via a polymorphic dispatch table.
*/
class LC3VM {
private:
// note: memory is allocated with unique_ptr to prevent stack overflow.
// for consistency and uniform ownership,
// registers and condition flags are also managed with unique_ptr
std::unique_ptr<Memory> m_memory;
std::unique_ptr<Registers> m_registers;
std::unique_ptr<ConditionFlags> m_flags;
uint16_t m_pc;
bool m_is_running;
std::unique_ptr<InstructionRegistry> m_registry;
public:
// ctor
LC3VM();
// dtor
~LC3VM();
/**
* @brief Loads an LC3 machine code program into memory.
* @param filename The path to the program file.
* @return true if the program was loaded successfully, false otherwise.
*/
bool load_program(std::string const& filename);
/**
* @brief Starts the execution loop (fetch, decode, execute) of the VM.
*/
void run();
/**
* @brief Halts the execution of the VM.
*/
void halt();
/**
* @brief Provides direct access to the VM's Memory.
* @return A reference to the Memory object.
*/
Memory& memory();
/**
* @brief Provides direct access to the VM's Registers.
* @return A reference to the Registers object.
*/
Registers& registers();
/**
* @brief Provides direct access to the VM's Condition Flags.
* @return A reference to the ConditionFlags object.
*/
ConditionFlags& flags();
/**
* @brief Gets the current value of the Program Counter.
* @return The 16 bit PC value.
*/
uint16_t pc() const;
/**
* @brief Allows updating the Program Counter.
*/
void set_pc(uint16_t address);
};
} // namespace lc3