-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuCPU.c
312 lines (216 loc) · 8.96 KB
/
uCPU.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
how can we incorporate project1 to convert the assembly input into hexcode for storage in
memory? Is there a way we can exploit ./asm < input.txt > output.txt so that we don't
have to repurpose that code or rewrite it (although a slight rewrite would be more
efficient so that we do not "print" but rather just store directly into memory)
ANSWER: input is in hex so there is no need for this. We get the binary
data directly.
the instructions state that the memory should be dumped with memory addresses 0x0000 - 0x000F
all in one line. But the prof's output only has 8 bytes per line. I thought each memory
address pointed to a byte? How are memory addresses being counted?
i am confused on what is the best approach for storing the information in memory.using
one "element" per each 2 digits of hex code sounds like it will complicate proccessing
but having one "element" per digit also sounds kind of bad in it's own right.
how would we process the 3 hex digit constant? if it is 0xA?? or greater it must be
negative but I think if we just store it in an int it won't actually treat it as
negative?? would we need to convert that value manually?
how to read in hex digits
its not any faster to do bit mask and shifting over div and remainder
the div and remainder will just be converted into shift and bitwise &
bitwise operations can be easier to understand the intention then div and
remainder once your
how to read a 3 hex digit constant in a 4 hex digit instruction?
this is a good chunk of the project
think about the bit shifts
get down and dirty with bitwise operators
we increment PC after instruction is completed. When you get a branch instruction the
the branch will account for the increment
WARNING: The following structure was explained with bugs on purpose so maybe dont
follow this structure but take it with a grain of salt
fetch function can be used to fetch the current instruction (while incrementing
PC)
decode function breaks up the instruction into its tokens
execute function executes the instruction
writeback write any memory that needs to be written
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#define NUM_REGISTERS 16
#define NUM_MEMORY 2048
void printDump();
void loadInstructions();
//declare registers as global
int16_t registers[NUM_REGISTERS] = {0}; //this should initialize all registers to 0
//declare memory as global
uint8_t memory[NUM_MEMORY] = {0}; //this should initialize all registers to 0
//declare PC
short pc = 0;
int main() {
loadInstructions();
//holds 1 or 0
int zeroFlag = 0;
while(pc < NUM_MEMORY) {
//obtain 16 bit instruction
int instruction = memory[pc] * pow(16, 2) + memory[pc + 1];
//get opcode
int opcode = (instruction / (int) pow(16, 3)) % 16;
//procces ADD operation
if(opcode == 0) {
int dest = (instruction / (int)pow(16,2)) % 16;
int operand1 = (instruction / 16) % 16;
int operand2 = instruction % 16;
registers[dest] = registers[operand1] + registers[operand2];
}
//procces SUB operation
else if(opcode == 1) {
int dest = (instruction / (int)pow(16,2)) % 16;
int operand1 = (instruction / 16) % 16;
int operand2 = instruction % 16;
registers[dest] = registers[operand1] - registers[operand2];
}
//procces AND operation
else if(opcode == 2) {
int dest = (instruction / (int)pow(16,2)) % 16;
int operand1 = (instruction / 16) % 16;
int operand2 = instruction % 16;
registers[dest] = registers[operand1] & registers[operand2];
}
//procces ORR operation
else if(opcode == 3) {
int dest = (instruction / (int)pow(16,2)) % 16;
int operand1 = (instruction / 16) % 16;
int operand2 = instruction % 16;
registers[dest] = registers[operand1] | registers[operand2];
}
//procces EOR operation
else if(opcode == 4) {
int dest = (instruction / (int)pow(16,2)) % 16;
int operand1 = (instruction / 16) % 16;
int operand2 = instruction % 16;
registers[dest] = registers[operand1] ^ registers[operand2];
}
//procces LSL operation
else if(opcode == 5) {
int dest = (instruction / (int)pow(16,2)) % 16;
int operand1 = (instruction / 16) % 16;
int operand2 = instruction % 16;
registers[dest] = registers[operand1] << registers[operand2];
}
//procces LSR operation
else if(opcode == 6) {
int dest = (instruction / (int)pow(16,2)) % 16;
int operand1 = (instruction / 16) % 16;
int operand2 = instruction % 16;
//cast to uint16_t so that the value being shifted is considered unsigned meaning that shift will occur logically. Then shift the desired amount and let the compiler recast
//to a (signed) int16_t
registers[dest] = ((uint16_t) registers[operand1] >> registers[operand2]);
}
//procces ASR operation
else if(opcode == 7) {
int dest = (instruction / (int)pow(16,2)) % 16;
int operand1 = (instruction / 16) % 16;
int operand2 = instruction % 16;
// no special processing needed b/c right shift will be performed arithetically b/c registers are declared as signed
registers[dest] = registers[operand1] >> registers[operand2];
}
//process LDR
else if(opcode == 8) {
int dest = (instruction / (int)pow(16,2)) % 16;
int memRegister = (instruction / 16) % 16; //specifies the register containing the memory address to be loaded
/*
For the LDR and STR operations, how many bytes of memory do we load into the destination register? Or do we just load a single byte of memory?
Answer: load in a word [2 bytes]
*/
//get the memory address stored in the register "memRegister"
int16_t memAddress = registers[memRegister];
//store the value in memory[memAddress] through memory[Address+1] in the destination register
registers[dest] = (memory[memAddress] * pow(16, 2) ) + memory[memAddress + 1]; //converts two seperate consecutive bytes into one word
}
//process STR
else if(opcode == 9) {
int sourceReg = (instruction / (int)pow(16,2)) % 16; //specifies the register holding our source data
int destReg = (instruction / 16) % 16; //specifies the register containing the memory address where we will store our data
int16_t memAddress = registers[destReg];
memory[memAddress] = (registers[sourceReg] & 0xFF00) >> 8; //store first byte in "starting" memAddress
memory[memAddress + 1] = registers[sourceReg] & 0x00FF; //store second byte in the address after the "starting" address (ie start address + 1)
}
//process CMP instruction
else if(opcode == 10) {
int reg1 = (instruction / (int)pow(16,2)) % 16;
int reg2 = (instruction / 16) % 16; //specifies the register containing the memory address to be loaded
//assign 1 to zero flag (meaning it is on) if operand1 and operand2 are equal. Assign 0 otherwise.
zeroFlag = (registers[reg1] == registers[reg2]);
}
//Process MOV operation
else if(opcode == 11){
int dest = (instruction / (int) pow(16,2)) % 16;
int constant = instruction & 0xFF; //get last 2 hex digits of the instruction
if(( (instruction / 16) % 16) >= 8)
constant = constant | 0xFFFFFF00;
registers[dest] = constant & 0xFFFF;
}
//B instruction
else if(opcode == 12){
//get constant
int constant = instruction & 0xFFF; //get last 3 hex digits of the instruction
if(( (instruction / (int) pow(16,2) ) % 16) >= 8 ) //if constant should be interpreted as negative (first hex digit of the 3 is 8 or greater meaning there is a 1 in the sign bit
constant = constant | 0xFFFFF000;
pc += constant;
//break to prevent changing pc further
continue;
}
//BEQ Instruction
else if(opcode == 13) {
//get constant
int constant = instruction & 0xFFF; //get last 3 hex digits of the instruction
if(( (instruction / (int) pow(16,2) ) % 16) >= 8 ) //if constant should be interpreted as negative (first hex digit of the 3 is 8 or greater meaning there is a 1 in the sign bit
constant = constant | 0xFFFFF000;
if(zeroFlag) {
pc += constant;
continue;
}
}
//END instruction
else if(opcode == 14) {
break;
}
//Process NOP
//do literally nothing
//move PC to next instruction [two bytes fowards]
pc += 2;
}
printDump();
}
void loadInstructions() {
int count = 0;
char buffer[10];
while( (count < NUM_MEMORY) && ( (scanf("%2s", buffer) != EOF ) )) {
memory[count] = (uint8_t)strtol(buffer, NULL, 16);
count++;
}
//terminate if we ran out of memory
if(count >= NUM_MEMORY) {
printf("ERROR: out of memory");
exit(0);
}
}
void printDump() {
//print 16 registers
for(int count = 0; count < NUM_REGISTERS; count++) {
printf("register %3d: 0x%.4X \n", count, registers[count] & 0xFFFF);
}
//print PC and extra new line to seperate memory dump
printf("register PC: 0x%.4X \n\n", pc);
//print memory
for(int count = 0; count < NUM_MEMORY; count++) {
if((count % 16) == 0) {
//print new line and next lines memory address "header"
printf("\n0x%.4X: ", count);
}
printf("%.2X", memory[count]);
if( ((count % 2) == 1))
printf(" ");
}
}