MIXAL
machine_misc.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "machine.h"
3 
9 namespace mixal {
10 
12 void Computer::executeSLA(const InstructionWord& instruction) {
13  int32_t address = getIndexedAddress(instruction);
14  int32_t shift = (address + 10000) % 5;
15  if (shift) {
16  for (int i = 1; i <= (5 - shift); ++i) {
17  rA[i] = rA[i + shift];
18  }
19  for (int i = 6 - shift; i <=5; ++i) {
20  rA[i] = 0;
21  }
22  }
23 }
24 
26 void Computer::executeSRA(const InstructionWord& instruction) {
27  int32_t address = getIndexedAddress(instruction);
28  int32_t shift = (address + 10000) % 5;
29  if (shift) {
30  for (int i = 5; i > shift; --i) {
31  rA[i] = rA[i - shift];
32  }
33  for (int i = shift; i > 0; --i) {
34  rA[i] = 0;
35  }
36  }
37 }
38 
40 void Computer::executeSLAX(const InstructionWord& instruction) {
41  int32_t address = getIndexedAddress(instruction);
42  int32_t shift = (address + 10000) % 10;
43  if (shift) {
44  for (int i = 1; i <= (10 - shift); ++i) {
45  setAX(i, getAX(i + shift));
46  }
47  for (int i = 11 - shift; i <= 10; ++i) {
48  setAX(i, 0);
49  }
50  }
51 }
52 
54 void Computer::executeSRAX(const InstructionWord& instruction) {
55  int32_t address = getIndexedAddress(instruction);
56  int32_t shift = (address + 10000) % 10;
57  if (shift) {
58  for (int i = 10; i > shift; --i) {
59  setAX(i, getAX(i - shift));
60  }
61  for (int i = shift; i > 0; --i) {
62  setAX(i, 0);
63  }
64  }
65 }
66 
68 void Computer::executeSLC(const InstructionWord& instruction) {
69  int32_t address = getIndexedAddress(instruction);
70  int32_t shift = (address + 10000) % 10;
71  if (shift) {
72  auto swap = [&](int start, int stop) {
73  int mid = start + (stop - start) / 2;
74  for (int i = start; i < mid; ++i) {
75  int oppsite = stop - 1 - (i - start);
76  uint8_t temp = getAX(i);
77  setAX(i, getAX(oppsite));
78  setAX(oppsite, temp);
79  }
80  };
81  swap(1, shift + 1);
82  swap(shift + 1, 11);
83  swap(1, 11);
84  }
85 }
86 
88 void Computer::executeSRC(const InstructionWord& instruction) {
89  int32_t address = getIndexedAddress(instruction);
90  int32_t shift = (address + 10000) % 10;
91  if (shift) {
92  auto swap = [&](int start, int stop) {
93  int mid = start + (stop - start) / 2;
94  for (int i = start; i < mid; ++i) {
95  int oppsite = stop - 1 - (i - start);
96  uint8_t temp = getAX(i);
97  setAX(i, getAX(oppsite));
98  setAX(oppsite, temp);
99  }
100  };
101  swap(1, 11 - shift);
102  swap(11 - shift, 11);
103  swap(1, 11);
104  }
105 }
106 
112 void Computer::executeMOVE(const InstructionWord& instruction) {
113  int32_t originAddress = getIndexedAddress(instruction);
114  int32_t targetAddress = rI1.value();
115  uint8_t amount = instruction.field();
116  for (uint8_t i = 0; i < amount; ++i) {
117  int32_t target = targetAddress + i;
118  int32_t origin = originAddress + i;
119  if (target < 0 || target >= NUM_MEMORY) {
120  continue;
121  }
122  memory[target] = memory[origin];
123  }
124  rI1.set(targetAddress + amount);
125 }
126 
127 }; // namespace mixal
mixal::Register2::set
void set(int16_t value)
Definition: registers.cpp:46
mixal::Computer::rI1
Register2 rI1
Definition: machine.h:33
mixal::Computer::NUM_MEMORY
static const int NUM_MEMORY
Definition: machine.h:29
mixal::Register2::value
int16_t value() const
Definition: registers.cpp:41
mixal::Computer::memory
ComputerWord memory[NUM_MEMORY]
Definition: machine.h:38
machine.h
The virtual machine.
mixal::Computer::rA
Register5 rA
Definition: machine.h:32