MIXAL
machine.h
Go to the documentation of this file.
1 #ifndef INCLUDE_MACHINE_H_
2 #define INCLUDE_MACHINE_H_
3 
4 #include <map>
5 #include <vector>
6 #include <string>
7 #include <memory>
8 #include <unordered_map>
9 #include "registers.h"
10 #include "flags.h"
11 #include "memory.h"
12 #include "parser.h"
13 #include "instructions.h"
14 #include "io.h"
15 #include "errors.h"
16 
22 namespace mixal {
23 
24 using InstructionWord = ComputerWord;
25 
26 class Computer {
27  public:
28  static const int NUM_INDEX_REGISTER = 6;
29  static const int NUM_MEMORY = 4000;
30  static const int NUM_IO_DEVICE = 21;
33  Register2 rI1 , rI2, rI3, rI4, rI5, rI6, rJ;
35  bool overflow;
39  std::vector<IODevice*> devices;
42  Computer();
43  ~Computer();
44 
46  Register2& rI(int index);
48  const ComputerWord& memoryAt(int16_t index) const;
50  ComputerWord& memoryAt(int16_t index);
52  IODevice* getDevice(int32_t index);
54  void waitDevice(IODevice* device);
56  void waitDevices();
58  ComputerWord& getDeviceWordAt(int32_t device, int32_t index);
59 
61  void reset();
63  inline int line() const { return _lineOffset; }
65  inline int elapsed() const { return _elapsed; }
66 
68  std::string getSingleLineSymbol();
70  void executeSingle();
72  void executeUntilSelfLoop();
74  void executeUntilHalt();
76  void executeSingle(ParsedResult* instruction);
78  void executeSingle(const InstructionWord& instruction);
80  void executeSinglePesudo(ParsedResult* instruction);
81 
83  void loadCodes(const std::string& codes, bool addHalt = true);
85  void loadCodes(const std::vector<std::string>& codes, bool addHalt = true);
86 
87  private:
88  int32_t _pesudoVarIndex;
89  int32_t _lineOffset;
90  int32_t _elapsed;
92  std::unordered_map<std::string, AtomicValue> _constants;
93 
95  std::string getPesudoSymbolname();
96 
98  int32_t getIndexedAddress(const InstructionWord& instruction, bool checkRange = false);
100  void copyToRegister5(const InstructionWord& instruction, const ComputerWord& word, Register5* reg);
102  void copyFromRegister5(const InstructionWord& instruction, const Register5& reg, ComputerWord* word);
104  void copyToRegister2(const InstructionWord& instruction, const ComputerWord& word, Register2* reg);
105 
106  int32_t checkRange(int32_t value, int bytes = 5);
107 
109  uint8_t getAX(int index) const;
111  void setAX(int index, uint8_t value);
112 
113  void executeADD(const InstructionWord& instruction);
114  void executeSUB(const InstructionWord& instruction);
115  void executeMUL(const InstructionWord& instruction);
116  void executeDIV(const InstructionWord& instruction);
117 
118  void executeNUM();
119  void executeCHAR();
120 
121  void executeSLA(const InstructionWord& instruction);
122  void executeSRA(const InstructionWord& instruction);
123  void executeSLAX(const InstructionWord& instruction);
124  void executeSRAX(const InstructionWord& instruction);
125  void executeSLC(const InstructionWord& instruction);
126  void executeSRC(const InstructionWord& instruction);
127  void executeMOVE(const InstructionWord& instruction);
128 
129  void executeLD(const InstructionWord& instruction, Register5* reg);
130  void executeLDi(const InstructionWord& instruction);
131  void executeLDN(const InstructionWord& instruction, Register5* reg);
132  void executeLDiN(const InstructionWord& instruction);
133  void executeST(const InstructionWord& instruction, Register5* reg);
134  void executeSTi(const InstructionWord& instruction);
135  void executeSTJ(const InstructionWord& instruction);
136  void executeSTZ(const InstructionWord& instruction);
137 
138  void executeJBUS(const InstructionWord& instruction);
139  void executeIOC(const InstructionWord& instruction);
140  void executeIN(const InstructionWord& instruction);
141  void executeOUT(const InstructionWord& instruction);
142  void executeJRED(const InstructionWord& instruction);
143 
144  void executeJMP(const InstructionWord& instruction);
145  void executeJSJ(const InstructionWord& instruction);
146  void executeJOV(const InstructionWord& instruction);
147  void executeJNOV(const InstructionWord& instruction);
148  void executeJL(const InstructionWord& instruction);
149  void executeJE(const InstructionWord& instruction);
150  void executeJG(const InstructionWord& instruction);
151  void executeJGE(const InstructionWord& instruction);
152  void executeJNE(const InstructionWord& instruction);
153  void executeJLE(const InstructionWord& instruction);
154  void executeJN(const InstructionWord& instruction, Register5* reg);
155  void executeJZ(const InstructionWord& instruction, Register5* reg);
156  void executeJP(const InstructionWord& instruction, Register5* reg);
157  void executeJNN(const InstructionWord& instruction, Register5* reg);
158  void executeJNZ(const InstructionWord& instruction, Register5* reg);
159  void executeJNP(const InstructionWord& instruction, Register5* reg);
160  void executeJiN(const InstructionWord& instruction);
161  void executeJiZ(const InstructionWord& instruction);
162  void executeJiP(const InstructionWord& instruction);
163  void executeJiNN(const InstructionWord& instruction);
164  void executeJiNZ(const InstructionWord& instruction);
165  void executeJiNP(const InstructionWord& instruction);
166 
167  void executeINC(const InstructionWord& instruction, Register5* reg);
168  void executeDEC(const InstructionWord& instruction, Register5* reg);
169  void executeENT(const InstructionWord& instruction, Register5* reg);
170  void executeENN(const InstructionWord& instruction, Register5* reg);
171  void executeINCi(const InstructionWord& instruction);
172  void executeDECi(const InstructionWord& instruction);
173  void executeENTi(const InstructionWord& instruction);
174  void executeENNi(const InstructionWord& instruction);
175 
176  void executeCMP(const InstructionWord& instruction, Register5* reg);
177  void executeCMPi(const InstructionWord& instruction);
178 
179  void executeEQU(ParsedResult* instruction);
180  void executeORIG(ParsedResult* instruction);
181  void executeCON(ParsedResult* instruction);
182 };
183 
184 }; // namespace mixal
185 
186 
187 #endif // INCLUDE_MACHINE_H_
mixal::Computer::NUM_INDEX_REGISTER
static const int NUM_INDEX_REGISTER
Definition: machine.h:28
mixal::Computer::Computer
Computer()
Definition: machine.cpp:11
mixal::Computer::waitDevice
void waitDevice(IODevice *device)
Definition: machine_io.cpp:42
mixal::Computer::loadCodes
void loadCodes(const std::string &codes, bool addHalt=true)
Definition: machine.cpp:329
mixal::Computer
Definition: machine.h:26
mixal::ComparisonIndicator
ComparisonIndicator
Definition: flags.h:14
mixal::Computer::reset
void reset()
Definition: machine.cpp:42
mixal::Computer::executeSingle
void executeSingle()
Definition: machine.cpp:69
mixal::ComputerWord
Definition: memory.h:25
mixal::Computer::rI1
Register2 rI1
Definition: machine.h:33
errors.h
The definition of some errors.
mixal::Computer::NUM_MEMORY
static const int NUM_MEMORY
Definition: machine.h:29
mixal::Computer::line
int line() const
Definition: machine.h:63
mixal::Computer::rX
Register5 rX
Definition: machine.h:32
mixal::Computer::getSingleLineSymbol
std::string getSingleLineSymbol()
Definition: machine.cpp:63
mixal::Computer::memory
ComputerWord memory[NUM_MEMORY]
Definition: machine.h:38
mixal::Computer::memoryAt
const ComputerWord & memoryAt(int16_t index) const
Definition: machine.cpp:34
mixal::Computer::executeUntilHalt
void executeUntilHalt()
Definition: machine.cpp:88
mixal::Computer::comparison
ComparisonIndicator comparison
Definition: machine.h:36
mixal::Computer::executeUntilSelfLoop
void executeUntilSelfLoop()
Definition: machine.cpp:73
mixal::Computer::waitDevices
void waitDevices()
Definition: machine_io.cpp:48
mixal::Computer::elapsed
int elapsed() const
Definition: machine.h:65
mixal::Computer::overflow
bool overflow
Definition: machine.h:35
mixal::Register2
Definition: registers.h:23
mixal::Computer::executeSinglePesudo
void executeSinglePesudo(ParsedResult *instruction)
Definition: machine.cpp:315
mixal::Computer::getDeviceWordAt
ComputerWord & getDeviceWordAt(int32_t device, int32_t index)
Definition: machine_io.cpp:56
mixal::ParsedResult
Definition: parser.h:32
mixal::Computer::devices
std::vector< IODevice * > devices
Definition: machine.h:39
mixal::Computer::getDevice
IODevice * getDevice(int32_t index)
Definition: machine_io.cpp:11
instructions.h
Definitions of instructions.
parser.h
Parse one line of code.
mixal::Computer::NUM_IO_DEVICE
static const int NUM_IO_DEVICE
Definition: machine.h:30
mixal::Computer::rI
Register2 & rI(int index)
Definition: machine.cpp:16
io.h
IO Devices.
mixal::Computer::rA
Register5 rA
Definition: machine.h:32
mixal::IODevice
Definition: io.h:40
flags.h
The definition of special flags.
registers.h
The definition of registers.
memory.h
The definition of a computer word.
mixal::Computer::rJ
Register2 rJ
Definition: machine.h:33