-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
259 lines (228 loc) · 7.54 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "../cuppu86/src/cpu.h"
#include "../cuppu86/src/extras.h"
#include "../cuppu86/src/memory.h"
#include "../cuppu86/src/flagops.h"
#include "../cuppu86/src/inc_dec.h"
#include "../cuppu86/src/add_sub.h"
cpu* c;
int k;
void w8(u8 instr) {
cpu_write_u8_at(c, base_offset(c->cs, c->ip + k), instr);
k++;
}
void w16(u16 instr) {
cpu_write_u16_at(c, base_offset(c->cs, c->ip + k), instr);
k+=2;
}
void flagPrint(){
unsigned i;
printf(" _ _ _ _ O D I T S Z _ A _ P _ C\n");
for (i = 1 << 15; i > 0; i = i / 2)
(c->flags & i) ? printf(" 1") : printf(" 0");
printf("\n");
}
void clrscr()
{
const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 11);
}
void instr_det(u32 instr) {
printf("\nOpcode: [%x]", instr);
switch (instr) {
case 0x00 : printf(" add r/m8, r8");break;
case 0x01 : printf(" add r/m16, r16");break;
case 0x02 : printf(" add r8, r/m8");break;
case 0x03 : printf(" add r16, r/m16");break;
case 0x04 : printf(" add AL, imm8");break;
case 0x05 : printf(" add AX, imm16");break;
case 0x06 : printf(" push ES");break;
case 0x07 : printf(" pop ES");break;
case 0x08 : printf(" or r/m8, r8");break;
case 0x09 : printf(" or r/m16, r16");break;
case 0x0A : printf(" or r8, r/m8");break;
case 0x0B : printf(" or r16, r/m16");break;
case 0x0C : printf(" or AL, imm8");break;
case 0x0D : printf(" or AX, imm16");break;
case 0x0E : printf(" push CS");break;
/*case 0x0F : to be updated*/
case 0x10 : printf(" adc r/m8, r8");break;
case 0x11 : printf(" adc r/m16, r16");break;
case 0x12 : printf(" adc r8, r/m8");break;
case 0x13 : printf(" adc r16, r/m16");break;
case 0x14 : printf(" adc AL, imm8");break;
case 0x15 : printf(" adc AX, imm16");break;
case 0x16 : printf(" push SS");break;
case 0x17 : printf(" pop SS");break;
case 0x18 : printf(" sbb r/m8, r8");break;
case 0x19 : printf(" sbb r/m16, r16");break;
case 0x1A : printf(" sbb r8, r/m8");break;
case 0x1B : printf(" sbb r16, r/m16");break;
case 0x1C : printf(" sbb AL, imm8");break;
case 0x1D : printf(" sbb AX, imm16");break;
case 0x1E : printf(" push DS");break;
case 0x1F : printf(" pop DS");break;
case 0x20 : printf(" and r/m8, r8");break;
case 0x21 : printf(" and r/m16, r16");break;
case 0x22 : printf(" and r8, r/m8");break;
case 0x23 : printf(" and r16, r/m16");break;
case 0x24 : printf(" and AL, imm8");break;
case 0x25 : printf(" and AX, imm16");break;
/*case 0x26 : to be updated*/
case 0x27 : printf(" daa AL");break;
case 0x28 : printf(" sub r/m8, r8");break;
case 0x29 : printf(" sub r/m16, r16");break;
case 0x2A : printf(" sub r8, r/m8");break;
case 0x2B : printf(" sub r16, r/m16");break;
case 0x2C : printf(" sub AL, imm8");break;
case 0x2D : printf(" sub AX, imm16");break;
/*case 0x2E : to be updated*/
case 0x2F : printf(" das AL");break;
case 0x30 : printf(" xor r/m8, r8");break;
case 0x31 : printf(" xor r/m16, r16");break;
case 0x32 : printf(" xor r8, r/m8");break;
case 0x33 : printf(" xor r16, r/m16");break;
case 0x34 : printf(" xor AL, imm8");break;
case 0x35 : printf(" xor AX, imm16");break;
/*case 0x36 : to be updated*/
case 0x37 : printf(" aaa AL, AH");break;
case 0x38 : printf(" cmp r/m8, r8");break;
case 0x39 : printf(" cmp r/m16, r16");break;
case 0x3A : printf(" cmp r8, r/m8");break;
case 0x3B : printf(" cmp r16, r/m16");break;
case 0x3C : printf(" cmp AL, imm8");break;
case 0x3D : printf(" cmp AX, imm16");break;
/*case 0x3E : to be updated*/
case 0x3F : printf(" aas AL, AH");break;
/*case 0x40 - 0x5F* : to be updated*/
case 0x60 : printf(" pusha");break;
case 0x61 : printf(" popa");break;
case 0x62 : printf(" bound");break;
case 0x63 : printf(" arpl");break;
/*case 0x64-0x67 : to be updated*/
case 0x68 : printf(" push imm16");break;
case 0x69 : printf(" imul r16, imm16");break;
case 0x6A : printf(" push imm8");break;
case 0x6B : printf(" imul r16, imm8");break;
/*case 0x6C-0x7F : to be updated*/
case 0x88 : printf(" mov r8 to r/m8\n");break;
case 0x89 : printf(" mov r16 to r16\n");break;
case 0x8a : printf(" mov r/m8 to r8\n");break;
case 0x8b : printf(" mov r/m16 to r16\n");break;
case 0x8c : printf(" mov Sreg to r/m16\n");break;
case 0x8e : printf(" mov r/m16 to Sreg\n");break;
case 0xb0 : printf(" mov imm8 to r8 (Low)\n");break;
case 0xb4 : printf(" mov imm8 to r8 (high)\n");break;
case 0xb9 : printf(" mov imm16 to r16\n");break;
case 0xfe : printf(" inc/dec r/m8\n");break;
case 0xff : printf(" inc/dec r/m16\n");break;
}
}
char alternate(cpu*c,char ch) {
char dum;
int a,b;
switch(ch) {
case 'd':scanf("%c",&dum);
if(dum == 10) {
cpu_dump_mem(c, base_offset(c->cs, c->ip), base_offset(c->cs, c->ip+40));
}
else if(dum == 32) {
scanf("%d",&a);
scanf("%c",&dum);
if(dum == 10) {
cpu_dump_mem(c, a, a+112);
}
else {
scanf("%d",&b);
cpu_dump_mem(c, a, b);
scanf("%c",&dum);
}
}
break;
case 'h':scanf("%x ",&a);
scanf("%x",&b);
printf("%x %x\n",add16(c,a,b,0),sub16(c,a,b,0));
break;
case 'p':clrscr();
u32 instr;
while(instr!=0) {
instr = cpu_fetch(c);
cpu_exec(c,instr);
}
instr_det(instr);
cpu_dump(c);
flagPrint();
break;
case 'r':cpu_dump(c);
break;
}
}
void test_move_binary(cpu *c) {
u32 instr;
char ch;
printf("\n-");
scanf("%c",&ch);
while(ch != 'q') {
if(ch == 't') {
clrscr();
instr = cpu_fetch(c);
instr_det(instr);
cpu_exec(c, instr);
cpu_dump(c);
flagPrint();
}
else alternate(c,ch);
printf("-");
scanf("\n%c",&ch);
}
clrscr();
printf("fetch/execute test done.\n------------------------------------------\n");
}
int main(int argc, char* argv[]) {
u32 addr;
u8 mem[MAX_MEMORY];
u8 iop[MAX_MEMORY];
k = 0;
c = (cpu*) malloc(sizeof(cpu));
cpu_init(c);
cpu_setmem(c, mem,iop);
w8(0xb0);
w8(0xff);
w8(0xb4);
w8(0xaa);
w8(0xb9);
w16(0x22da);
w8(0x88);
w8(0x2f);
w8(0x89);
w8(0x06);
w16(0x5000);
w8(0x8b);
w8(0xc3);
w8(0x8a);
w8(0xf1);
w8(0x8b);
w8(0x1e);
w16(0x0100);
w8(0x8e);
w8(0xd2);
w8(0x8c);
w8(0xd0);
w8(0xff);
w8(0xca);
w8(0xff);
w8(0xac);
w8(0xff);
w8(0xc4);
w8(0xff);
w8(0x37);
w8(0xfe); //fe
w8(0xc0);
w8(0xff);
w8(0x07);
test_move_binary(c);
return 0;
}