-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActions.c
278 lines (237 loc) · 7.54 KB
/
Actions.c
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**************************************************************
*
* Actions.c
*
* Assignment: um
* Authors: Caleb Pekowsky, Labib Afia
* Date: 04//2022
*
* summary:
*
* this is the .c file for our actions implementation. This stores the
implementation for the 14 commands/actions, and manipulates words to call
the correct action.
*
**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "Actions.h"
#include "bitpack.h"
#include "Memory.h"
#include "assert.h"
//define 2^32 here
typedef enum Um_opcode {
CMOV = 0, SLOAD, SSTORE, ADD, MUL, DIV,
NAND, HALT, ACTIVATE, INACTIVATE, OUT, IN, LOADP, LV
} Um_opcode;
/* getABC
* Purpose: get bits from 32-bit word that correspond to 3-bit A, B and C
* respectively
* Parameters:
* int**ABC: pointer to array that will store A, then B, then C
* uint32_t word: the word we want to get from
* Return: nothing.
*/
void getABC(uint32_t word, int** ABC) {
(*ABC)[0] = Bitpack_getu(word, 3, 6);
(*ABC)[1] = Bitpack_getu(word, 3, 3);
(*ABC)[2] = Bitpack_getu(word, 3, 0);
}
/* getValue
* Purpose: gets 25-bit value from a 32-bit word
* Parameters:
* uint32_t word: the word we want to get value from
* Return: nothing.
*/
uint32_t getValue(uint32_t word) {
//printf("currently loading value: %lu \n", Bitpack_getu(word, 25, 0));
return Bitpack_getu(word, 25, 0);
}
/* doCommand
* Purpose: executes appropriate action encoded by word using opcode
* Parameters:
* uint32_t currentWord: the word we want to execute
* Memory_T currMemory: the memory struct we want to manipulate
* uint32_t* counter: pointer to counter
* uint32_t** registers: pointer to array of 8 registers
* Return: nothing.
*/
void doCommand(uint32_t currentWord, Memory_T currMemory,
uint32_t** registers, uint32_t* counter) {
int opcode = currentWord >> 28;
int *ABC = malloc(3*sizeof(*ABC));
getABC(currentWord, &ABC);
int A = ABC[0];
int B = ABC[1];
int C = ABC[2];
if(opcode == LV) {
A = Bitpack_getu(currentWord, 3, 25);
}
UArray_T segmentZero = Seq_get((currMemory->memory), 0);
switch(opcode) {
case CMOV :
ConditionalMove(registers, A, B, C);
break;
case SLOAD :
(*registers)[A] = Memory_get_Specific_word(currMemory, (*registers)[B],
(*registers)[C]);
break;
case SSTORE :
Memory_put_Specific_word(currMemory, (*registers)[A], (*registers)[B],
(*registers)[C]);
break;
case ADD :
Add(registers, A, B, C);
break;
case MUL :
Multiply(registers, A, B, C);
break;
case DIV :
Divide(registers, A, B, C);
break;
case NAND :
BitNAND(registers, A, B, C);
break;
case HALT :
*counter = UArray_length(segmentZero);
break;
case ACTIVATE:
(*registers)[B] = Memory_map_segment(currMemory, (*registers)[C] );
break;
case INACTIVATE:
Memory_unmap_segment(currMemory, (*registers)[C]);
break;
case OUT:
assert((*registers)[C] <= 255);
putchar((*registers)[C]);
break;
case IN:
In(registers, C);
break;
case LOADP:
LoadProgram(currMemory, registers, B, C, counter);
break;
case LV:
LoadValue(registers, A, getValue(currentWord));
break;
default:
fprintf(stderr,"invalid opcode. Quitting. \n");
*counter = UArray_length(segmentZero);
}
if (opcode != LOADP) {
(*counter)++;
}
free(ABC);
}
/* ConditionalMove
* Purpose: execute the cmov command
* Parameters:
* uint32_t** registers: pointer to array of 8 registers
* int A: decimal value in 3 bits that represent A
* int B: decimal value in 3 bits that represent B
* int C: decimal value in 3 bits that represent C
* Return: nothing.
*/
void ConditionalMove(uint32_t** registers, int A, int B, int C) {
if( (*registers)[C] != 0) {
(*registers)[A] = (*registers)[B];
}
}
/* Add
* Purpose: execute the add command
* Parameters:
* uint32_t** registers: pointer to array of 8 registers
* int A: decimal value in 3 bits that represent A
* int B: decimal value in 3 bits that represent B
* int C: decimal value in 3 bits that represent C
* Return: nothing.
*/
void Add(uint32_t** registers, int A, int B, int C) {
(*registers)[A] = ((*registers)[B] + (*registers)[C] ) % 4294967296;
}
/* Multiply
* Purpose: execute the multiply command
* Parameters:
* uint32_t** registers: pointer to array of 8 registers
* int A: decimal value in 3 bits that represent A
* int B: decimal value in 3 bits that represent B
* int C: decimal value in 3 bits that represent C
* Return: nothing.
*/
void Multiply(uint32_t** registers, int A, int B, int C) {
(*registers)[A] = ((*registers)[B] * (*registers)[C] ) % 4294967296;
}
/* Divide
* Purpose: execute the divide command
* Parameters:
* uint32_t** registers: pointer to array of 8 registers
* int A: decimal value in 3 bits that represent A
* int B: decimal value in 3 bits that represent B
* int C: decimal value in 3 bits that represent C
* Return: nothing.
*/
void Divide(uint32_t** registers, int A, int B, int C) {
(*registers)[A] = ((*registers)[B] / (*registers)[C] ) % 4294967296;
}
/* BitNAND
* Purpose: execute the bitnand command
* Parameters:
* uint32_t** registers: pointer to array of 8 registers
* int A: decimal value in 3 bits that represent A
* int B: decimal value in 3 bits that represent B
* int C: decimal value in 3 bits that represent C
* Return: nothing.
*/
void BitNAND(uint32_t** registers, int A, int B, int C) {
(*registers)[A] = ~( (*registers)[B] & (*registers)[C] );
}
/* In
* Purpose: execute the input command
* Parameters:
* uint32_t** registers: pointer to array of 8 registers
* int C: decimal value in 3 bits that represent C
* Return: nothing.
*/
void In(uint32_t** registers, int C) {
int currInput = getc(stdin);
if(currInput == EOF) {
(*registers)[C] = ~(uint32_t)0;
} else {
assert(0 <= currInput && currInput <= 255);
(*registers)[C] = currInput;
}
}
/* LoadProgram
* Purpose: execute the loadprogram command
* Parameters:
* Memory_T currMemory: the memory struct we want to manipulate
* uint32_t** registers: pointer to array of 8 registers
* int A: decimal value in 3 bits that represent A
* int B: decimal value in 3 bits that represent B
* int C: decimal value in 3 bits that represent C
* uint32_t* counter: pointer to counter
* Return: nothing.
*/
void LoadProgram(Memory_T currMemory, uint32_t** registers, int B, int C,
uint32_t* counter) {
if( (*registers)[B] != 0) {
UArray_T NewCommands = Seq_get(currMemory->memory, (*registers)[B]);
UArray_T NewSeqZero = UArray_copy(NewCommands,
UArray_length(NewCommands));
UArray_T toReplace = Seq_get(currMemory->memory, 0);
UArray_free(&toReplace);
Seq_put(currMemory->memory, 0, NewSeqZero);
}
*counter = (*registers)[C];
}
/* LoadValue
* Purpose: execute the loadvalue command
* Parameters:
* uint32_t** registers: pointer to array of 8 registers
* int value: decimal value in 25 bits that represent value
* Return: nothing.
*/
void LoadValue(uint32_t** registers, int A, uint32_t value) {
(*registers)[A] = value;
}