-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1049MicroprocessorSimulation.cpp
More file actions
87 lines (87 loc) · 1.36 KB
/
1049MicroprocessorSimulation.cpp
File metadata and controls
87 lines (87 loc) · 1.36 KB
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
#include<stdio.h>
#include<string.h>
char mem[300];
int A,B;
int ip;
int instr;
int ti(char c){
if(c<='9' && c>='0') return c-'0';
return c-'A'+10;
}
int get_addr(){
int p = ti(mem[ip])*16+ti(mem[ip+1]);
ip+=2;
return p;
}
char tc(int i){
if(i<=9) return '0'+i;
return i-10+'A';
}
void get_instr(){
instr = ti(mem[ip]);
ip++;
}
void f0(){
int p = get_addr();
A = ti(mem[p]);
}
void f1(){
int p = get_addr();
mem[p] = tc(A);
}
void f2(){
int C= A;
A=B, B=C;
}
void f3(){
//add
int C = A+B;
A = C%16;
B = C/16;
}
void f4(){
A++;
if(A>=16) A-=16;
}
void f5(){
A--;
if(A<0) A+=16;
}
void f6(){
//BZ
int p = get_addr();
if(!A) ip = p;
}
void f7(){
int p = get_addr();
ip = p;
}
int main()
{
while(scanf("%s",mem),mem[0]!='8'){
A = B = 0;
ip = 0;
while(get_instr(),instr!=8){
switch(instr){
case 0:f0();
break;
case 1:f1();
break;
case 2:f2();
break;
case 3:f3();
break;
case 4:f4();
break;
case 5:f5();
break;
case 6:f6();
break;
case 7:f7();
break;
}
}
printf("%s\n",mem);
}
return 0;
}