diff options
Diffstat (limited to 'ue3/mycpu/instructions.cpp')
| -rw-r--r-- | ue3/mycpu/instructions.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/ue3/mycpu/instructions.cpp b/ue3/mycpu/instructions.cpp index c2ce096..ef9e944 100644 --- a/ue3/mycpu/instructions.cpp +++ b/ue3/mycpu/instructions.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | #include <map> | 8 | #include <map> |
| 9 | #include <assert.h> | ||
| 9 | #include "instructions.h" | 10 | #include "instructions.h" |
| 10 | 11 | ||
| 11 | using namespace std; | 12 | using namespace std; |
| @@ -22,6 +23,8 @@ void CInstructionInc::compile(std::list<std::string>& params) | |||
| 22 | 23 | ||
| 23 | void CInstructionInc::execute(CCPU *cpu) | 24 | void CInstructionInc::execute(CCPU *cpu) |
| 24 | { | 25 | { |
| 26 | assert(cpu != NULL); | ||
| 27 | assert(cpu->getRegisters() != NULL); | ||
| 25 | checkRegister(cpu, m_regidx1); | 28 | checkRegister(cpu, m_regidx1); |
| 26 | cpu->getRegisters()[ m_regidx1 ]++; | 29 | cpu->getRegisters()[ m_regidx1 ]++; |
| 27 | } | 30 | } |
| @@ -40,6 +43,8 @@ void CInstructionDec::compile(std::list<std::string>& params) | |||
| 40 | 43 | ||
| 41 | void CInstructionDec::execute(CCPU *cpu) | 44 | void CInstructionDec::execute(CCPU *cpu) |
| 42 | { | 45 | { |
| 46 | assert(cpu != NULL); | ||
| 47 | assert(cpu->getRegisters() != NULL); | ||
| 43 | checkRegister(cpu, m_regidx1); | 48 | checkRegister(cpu, m_regidx1); |
| 44 | cpu->getRegisters()[ m_regidx1 ]--; | 49 | cpu->getRegisters()[ m_regidx1 ]--; |
| 45 | } | 50 | } |
| @@ -62,6 +67,8 @@ void CInstructionAdd::compile(std::list<std::string>& params) | |||
| 62 | 67 | ||
| 63 | void CInstructionAdd::execute(CCPU *cpu) | 68 | void CInstructionAdd::execute(CCPU *cpu) |
| 64 | { | 69 | { |
| 70 | assert(cpu != NULL); | ||
| 71 | assert(cpu->getRegisters() != NULL); | ||
| 65 | checkRegister(cpu, m_regidx1); | 72 | checkRegister(cpu, m_regidx1); |
| 66 | checkRegister(cpu, m_regidx2); | 73 | checkRegister(cpu, m_regidx2); |
| 67 | checkRegister(cpu, m_regidx3); | 74 | checkRegister(cpu, m_regidx3); |
| @@ -87,6 +94,8 @@ void CInstructionSub::compile(std::list<std::string>& params) | |||
| 87 | 94 | ||
| 88 | void CInstructionSub::execute(CCPU *cpu) | 95 | void CInstructionSub::execute(CCPU *cpu) |
| 89 | { | 96 | { |
| 97 | assert(cpu != NULL); | ||
| 98 | assert(cpu->getRegisters() != NULL); | ||
| 90 | checkRegister(cpu, m_regidx1); | 99 | checkRegister(cpu, m_regidx1); |
| 91 | checkRegister(cpu, m_regidx2); | 100 | checkRegister(cpu, m_regidx2); |
| 92 | checkRegister(cpu, m_regidx3); | 101 | checkRegister(cpu, m_regidx3); |
| @@ -137,6 +146,8 @@ void CInstructionDiv::compile(std::list<std::string>& params) | |||
| 137 | 146 | ||
| 138 | void CInstructionDiv::execute(CCPU *cpu) | 147 | void CInstructionDiv::execute(CCPU *cpu) |
| 139 | { | 148 | { |
| 149 | assert(cpu != NULL); | ||
| 150 | assert(cpu->getRegisters() != NULL); | ||
| 140 | checkRegister(cpu, m_regidx1); | 151 | checkRegister(cpu, m_regidx1); |
| 141 | checkRegister(cpu, m_regidx2); | 152 | checkRegister(cpu, m_regidx2); |
| 142 | checkRegister(cpu, m_regidx3); | 153 | checkRegister(cpu, m_regidx3); |
| @@ -160,6 +171,9 @@ void CInstructionLoad::compile(std::list<std::string>& params) | |||
| 160 | 171 | ||
| 161 | void CInstructionLoad::execute(CCPU *cpu) | 172 | void CInstructionLoad::execute(CCPU *cpu) |
| 162 | { | 173 | { |
| 174 | assert(cpu != NULL); | ||
| 175 | assert(cpu->getRegisters() != NULL); | ||
| 176 | assert(cpu->getMemory() != NULL); | ||
| 163 | checkRegister(cpu, m_regidx1); | 177 | checkRegister(cpu, m_regidx1); |
| 164 | checkRegister(cpu, m_regidx2); | 178 | checkRegister(cpu, m_regidx2); |
| 165 | CDat val(cpu->getRegisters()[ m_regidx2 ]); | 179 | CDat val(cpu->getRegisters()[ m_regidx2 ]); |
| @@ -182,6 +196,9 @@ void CInstructionStore::compile(std::list<std::string>& params) | |||
| 182 | 196 | ||
| 183 | void CInstructionStore::execute(CCPU *cpu) | 197 | void CInstructionStore::execute(CCPU *cpu) |
| 184 | { | 198 | { |
| 199 | assert(cpu != NULL); | ||
| 200 | assert(cpu->getRegisters() != NULL); | ||
| 201 | assert(cpu->getMemory() != NULL); | ||
| 185 | checkRegister(cpu, m_regidx1); | 202 | checkRegister(cpu, m_regidx1); |
| 186 | checkRegister(cpu, m_regidx2); | 203 | checkRegister(cpu, m_regidx2); |
| 187 | CDat val(cpu->getRegisters()[ m_regidx2 ]); | 204 | CDat val(cpu->getRegisters()[ m_regidx2 ]); |
| @@ -202,6 +219,8 @@ void CInstructionTest::compile(std::list<std::string>& params) | |||
| 202 | 219 | ||
| 203 | void CInstructionTest::execute(CCPU *cpu) | 220 | void CInstructionTest::execute(CCPU *cpu) |
| 204 | { | 221 | { |
| 222 | assert(cpu != NULL); | ||
| 223 | assert(cpu->getRegisters() != NULL); | ||
| 205 | checkRegister(cpu, m_regidx1); | 224 | checkRegister(cpu, m_regidx1); |
| 206 | if (cpu->getRegisters()[ m_regidx1 ] == CDat(0)) | 225 | if (cpu->getRegisters()[ m_regidx1 ] == CDat(0)) |
| 207 | cpu->setFlagZero(true); | 226 | cpu->setFlagZero(true); |
| @@ -223,6 +242,9 @@ void CInstructionJumpA::compile(std::list<std::string>& params) | |||
| 223 | 242 | ||
| 224 | void CInstructionJumpA::execute(CCPU *cpu) | 243 | void CInstructionJumpA::execute(CCPU *cpu) |
| 225 | { | 244 | { |
| 245 | assert(cpu != NULL); | ||
| 246 | assert(cpu->getRegisters() != NULL); | ||
| 247 | assert(cpu->getProgram() != NULL); | ||
| 226 | if (m_addr.empty()) | 248 | if (m_addr.empty()) |
| 227 | throw runtime_error("Empty address"); | 249 | throw runtime_error("Empty address"); |
| 228 | cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); | 250 | cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); |
| @@ -242,8 +264,13 @@ void CInstructionJumpZ::compile(std::list<std::string>& params) | |||
| 242 | 264 | ||
| 243 | void CInstructionJumpZ::execute(CCPU *cpu) | 265 | void CInstructionJumpZ::execute(CCPU *cpu) |
| 244 | { | 266 | { |
| 267 | assert(cpu != NULL); | ||
| 268 | assert(cpu->getRegisters() != NULL); | ||
| 269 | assert(cpu->getProgram() != NULL); | ||
| 245 | if (!cpu->getFlagZero()) | 270 | if (!cpu->getFlagZero()) |
| 246 | return; | 271 | return; |
| 272 | if (m_addr.empty()) | ||
| 273 | throw runtime_error("Empty address"); | ||
| 247 | cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); | 274 | cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); |
| 248 | } | 275 | } |
| 249 | 276 | ||
| @@ -261,8 +288,13 @@ void CInstructionJumpS::compile(std::list<std::string>& params) | |||
| 261 | 288 | ||
| 262 | void CInstructionJumpS::execute(CCPU *cpu) | 289 | void CInstructionJumpS::execute(CCPU *cpu) |
| 263 | { | 290 | { |
| 291 | assert(cpu != NULL); | ||
| 292 | assert(cpu->getRegisters() != NULL); | ||
| 293 | assert(cpu->getProgram() != NULL); | ||
| 264 | if (!cpu->getFlagSign()) | 294 | if (!cpu->getFlagSign()) |
| 265 | return; | 295 | return; |
| 296 | if (m_addr.empty()) | ||
| 297 | throw runtime_error("Empty address"); | ||
| 266 | cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); | 298 | cpu->getRegisters()[ 0 ] = cpu->getProgram()->findLabel(m_addr); |
| 267 | } | 299 | } |
| 268 | 300 | ||
| @@ -282,6 +314,8 @@ void CInstructionWrite::compile(std::list<std::string>& params) | |||
| 282 | 314 | ||
| 283 | void CInstructionWrite::execute(CCPU *cpu) | 315 | void CInstructionWrite::execute(CCPU *cpu) |
| 284 | { | 316 | { |
| 317 | assert(cpu != NULL); | ||
| 318 | assert(cpu->getRegisters() != NULL); | ||
| 285 | checkRegister(cpu, m_regidx1); | 319 | checkRegister(cpu, m_regidx1); |
| 286 | if (m_dev.empty()) | 320 | if (m_dev.empty()) |
| 287 | throw runtime_error("Empty device"); | 321 | throw runtime_error("Empty device"); |
