-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulate.c
208 lines (196 loc) · 5.3 KB
/
emulate.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
/*
* file: emulate-soln.c
* description: solution to Lab 1
*/
#include <stdio.h>
#include <stdlib.h>
#include "lab1.h"
/*struct cpu {
uint8_t *memory;
uint16_t R[8]; R[1] r[2]
uint16_t PC;
uint16_t SP;
int Z;
int N;
};
*/
void store2(struct cpu *cpu, uint16_t data, uint16_t addr) {
cpu->memory[addr] = data & 0xFF;
cpu->memory[addr+1] = (data >> 8) & 0xFF;
}
uint16_t load2(struct cpu *cpu, uint16_t addr) {
return (cpu->memory[addr] | (cpu->memory[addr+1] << 8));
}
/* emulate(struct cpu*) - emulate a single instruction
* - returns 1 for halt, 0 for no halt
*/
int emulate(struct cpu *cpu)
{
uint16_t insn = load2(cpu, cpu->PC);
int op = (insn >> 9) & 7; //operation
int c = (insn >> 6) & 7;
int b = (insn >> 3) & 7;
int a = insn & 7;
printf("instruction is : 0x%04X\n",(insn & 0xF000));
if ((insn & 0xF000) == 0x1000) {
/* SET */
//load value from r1 into
cpu->R[a] = load2(cpu,cpu->PC);
cpu->PC = cpu->PC+4;
return 0;
//set
}
//LOAD x2000 if loading constant from 16 bit word
else if ((insn & 0xF000) == 0x2000) {
int is_indirect = ((insn & 0x0800) != 0);
int is_byte = ((insn & 0x0400) != 0);
//LOAD
//int is_indirect = ((insn & 0x0800) != 0);
//int is_byte = ((insn & 0x0400) != 0);
//printf("indirect: %d isByte: %d",is_indirect,is_byte);
printf("op: %d c:%d b:%d a:%d",op,c,b,a);
//LOAD
if(is_indirect == 0 && is_byte == 0){
cpu->R[a] = load2(cpu,cpu->PC+2);
}
else if(is_byte == 1 && is_indirect == 0){
cpu->R[a] = cpu->memory[(cpu->PC+2)+1]<<8;
}
else if(is_byte == 0 && is_indirect == 1 ){
//0x2800
cpu->R[a] = load2(cpu,cpu->R[b]);
}
else{
//if indirec and is byte are 0 then 0x2C00
cpu->R[a] = cpu->memory[(cpu->R[b])+1] << 8;
}
cpu->PC = cpu->PC+4;
return 0;
}
else if((insn & 0xF000) == 0x3000){
//STORE
if((insn & 0xF000) == 0x3000){
cpu->memory[cpu->PC+2] = load2(cpu,cpu->R[a]);
cpu->PC = cpu->PC + 4;
}
else if(((insn & 0xFF00) >> 8) == 0x34){
cpu->memory[cpu->PC+2] = cpu->memory[cpu->R[a]+1] << 8;
cpu->PC = cpu->PC + 4;
}
else if(((insn & 0xFF00) >> 8) == 0x3C){
cpu->R[b] = cpu->memory[(cpu->R[a])+1] << 8;
cpu->PC = cpu->PC + 2;
}
else{
cpu->R[b] = load2(cpu,cpu->R[a]);
cpu->PC = cpu->PC + 2;
}
return 0;
}
else if((insn & 0xF000) == 0x4000){
//MOVE
printf("s:%d d:%d",a,b);
int copy = cpu->R[a];
cpu->R[b] = copy;
return 0;
}
else if((insn & 0xF000) == 0x5000){
//ALU
int aluOp = (insn & 0x0E00);
switch (aluOp) {
case 0x0200:
//SUB
cpu->R[c] = cpu->R[a] - cpu->R[b];
break;
case 0x0400:
//AND
cpu->R[c] = cpu->R[a] & cpu->R[b];
break;
case 0x0600:
//OR
cpu->R[b] = cpu->R[a] | cpu->R[b];
break;
case 0x0800:
//XOR
cpu->R[c] = cpu->R[a] ^ cpu->R[b];
break;
case 0x0A00:
//SHIFT R
cpu->R[b] = cpu->R[a] >> cpu->R[b];
break;
case 0x0C00:
//CMP
//uint16_t val = cpu->R[a] - cpu->R[b];
//int is_negative = (val & 0x8000) != 0;
if(cpu->R[a] - cpu->R[b] < 0){
cpu->Z = 0;
cpu->N = 1;
}
else if(cpu->R[a] - cpu->R[b] == 0){
cpu->Z = 1;
cpu->N = 0;
}
else{
cpu->Z = 0;
cpu->N = 0;
}
break;
case 0x0E00:
//TEST
if(cpu->R[a] > 0){
cpu->Z = 0;
cpu->N = 0;
}
else if(cpu->R[a] == 0){
cpu->Z = 1;
cpu->N = 0;
}
else{
cpu->Z = 0;
cpu->N = 1;
}
break;
default:
//ADD
cpu->R[c] = cpu->R[a] + cpu->R[b];
break;
}
cpu->PC = cpu->PC+2;
return 0;
}
else if((insn & 0xF000) == 0x6000){
//JMP_ABS
return 0;
}
else if((insn & 0xF000) == 0x8000){
//CALL absolute
return 0;
}
else if((insn & 0xF000) == 0x9000){
//CALL register
return 0;
}
else if((insn & 0xF000) == 0xA ){
//RET
return 0;
}
else if((insn & 0xF000) == 0xB){
//PUSH
}
else if((insn & 0xF000) == 0xC){
//POP
}
else if((insn & 0xF000) == 0xD){
//IN
}
else if((insn & 0xF000) == 0xE){
//OUT
}
else if((insn & 0xF000) == 0xF){
//HALT
return 1;
}
//
return 1;
/* your code here */
}