forked from whoisnian/AnalogCPU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalogCPU.c
239 lines (229 loc) · 6.67 KB
/
AnalogCPU.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
/*************************************************************************
> File Name: AnalogCPU.c
> Author: nian
> Blog: https://whoisnian.com
> Mail: zhuchangbao2017@gmail.com
> Created Time: 2018年07月10日 星期二 23时54分29秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
void PrintfColorfulMessage(HANDLE cmd, char *message, int type);
int main(void)
{
// 用于在 cmd 中的颜色显示
HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE);
// 打开串口通信端口
HANDLE uart = CreateFileA("COM3", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(uart == INVALID_HANDLE_VALUE)
{
PrintfColorfulMessage(cmd, "Open COM3 failed.\n\n", 1);
system("pause");
return 1;
}
else
{
PrintfColorfulMessage(cmd, "Open COM3 successfully.\n\n", 0);
}
// 设置缓冲区大小
SetupComm(uart, 1024, 1024);
// 设置超时
COMMTIMEOUTS timeout;
timeout.ReadIntervalTimeout = 1000;
timeout.ReadTotalTimeoutMultiplier = 500;
timeout.ReadTotalTimeoutConstant = 5000;
timeout.WriteTotalTimeoutMultiplier = 500;
timeout.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(uart, &timeout);
// 设置串口通信波特率 9600bps,8 位数据位,无校验位,1 位停止位
DCB dcb;
GetCommState(uart, &dcb);
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.fParity = FALSE;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
SetCommState(uart, &dcb);
// 汇编指令转换为二进制操作码
int num = 0;
char ram[32];
char command[5], temp[5];
while(scanf("%s", command) != EOF)
{
if(!strcmp(command, "mov"))
{
scanf("%s", temp);
if(!strcmp(temp, "ax"))
{
scanf("%s", temp);
if(!strcmp(temp, "bx"))
{
ram[num++] = 0x03;
}
else
{
ram[num++] = 0x01;
if(strtoul(temp, NULL, 0) > 255)
ram[num++] = 0x00;
else
ram[num++] = strtoul(temp, NULL, 0);
}
}
else if(!strcmp(temp, "bx"))
{
scanf("%s", temp);
if(!strcmp(temp, "ax"))
{
ram[num++] = 0x04;
}
else
{
ram[num++] = 0x02;
if(strtoul(temp, NULL, 0) > 255)
ram[num++] = 0x00;
else
ram[num++] = strtoul(temp, NULL, 0);
}
}
else
{
PrintfColorfulMessage(cmd, "\nInvalid parameter.\n\n", 1);
system("pause");
return 1;
}
}
else if(!strcmp(command, "add"))
{
scanf("%s", temp);
if(!strcmp(temp, "ax"))
{
scanf("%s", temp);
if(!strcmp(temp, "bx"))
{
ram[num++] = 0x07;
}
else
{
ram[num++] = 0x05;
if(strtoul(temp, NULL, 0) > 255)
ram[num++] = 0x00;
else
ram[num++] = strtoul(temp, NULL, 0);
}
}
else if(!strcmp(temp, "bx"))
{
scanf("%s", temp);
if(!strcmp(temp, "ax"))
{
ram[num++] = 0x08;
}
else
{
ram[num++] = 0x06;
if(strtoul(temp, NULL, 0) > 255)
ram[num++] = 0x00;
else
ram[num++] = strtoul(temp, NULL, 0);
}
}
else
{
PrintfColorfulMessage(cmd, "\nInvalid parameter.\n\n", 1);
system("pause");
return 1;
}
}
else if(!strcmp(command, "shr"))
{
scanf("%s", temp);
if(!strcmp(temp, "ax"))
{
ram[num++] = 0x09;
}
else if(!strcmp(temp, "bx"))
{
ram[num++] = 0x0a;
}
else
{
PrintfColorfulMessage(cmd, "\nInvalid parameter.\n\n", 1);
system("pause");
return 1;
}
}
else if(!strcmp(command, "shl"))
{
scanf("%s", temp);
if(!strcmp(temp, "ax"))
{
ram[num++] = 0x0b;
}
else if(!strcmp(temp, "bx"))
{
ram[num++] = 0x0c;
}
else
{
PrintfColorfulMessage(cmd, "\nInvalid parameter.\n\n", 1);
system("pause");
return 1;
}
}
else if(!strcmp(command, "xchg"))
{
ram[num++] = 0x0d;
}
else if(!strcmp(command, "halt"))
{
ram[num++] = 0x0e;
}
else
{
PrintfColorfulMessage(cmd, "\nCommand not found.\n\n", 1);
system("pause");
return 1;
}
if(num > 16)
{
PrintfColorfulMessage(cmd, "\nToo many commands.\n\n", 1);
system("pause");
return 1;
}
if(!strcmp(command, "halt"))
{
break;
}
}
PrintfColorfulMessage(cmd, "\nBEGIN\n\n", 0);
// 依次发送输入的所有指令
char data[2];
DWORD WriteNum;
for(int i = 0;i < num;i++)
{
PurgeComm(uart, PURGE_TXCLEAR);
data[0] = i;
data[1] = ram[i];
WriteNum = 0;
if(WriteFile(uart, data, sizeof(data), &WriteNum, 0))
{
printf("%2d/%-2d Writing to RAM: %02x\n", i, num-1, ram[i]);
}
PurgeComm(uart, PURGE_TXCLEAR);
// 发送两次 8 位数据后等待 200 毫秒,用于存储器从接收模块读取并写入数据
Sleep(200);
}
PrintfColorfulMessage(cmd, "\nEND\n\n", 0);
system("pause");
CloseHandle(uart);
return 0;
}
void PrintfColorfulMessage(HANDLE cmd, char *message, int type)
{
if(type == 0)
SetConsoleTextAttribute(cmd, FOREGROUND_GREEN | FOREGROUND_INTENSITY); // 正常
else if(type == 1)
SetConsoleTextAttribute(cmd, FOREGROUND_RED | FOREGROUND_INTENSITY); // 报错
printf("%s", message);
SetConsoleTextAttribute(cmd, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}