-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhl_shell.c
More file actions
129 lines (126 loc) · 3.3 KB
/
hl_shell.c
File metadata and controls
129 lines (126 loc) · 3.3 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
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
//
// Created by wvv on 2019/10/27.
//
#include "hl_shell.h"
#include "hl_vm_core.h"
#include "hl_vm_debug.h"
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <io.h>
typedef enum{
SHELL_IDLE,SHELL_INPUT
}SHELL_STATE;
void hl_vm_input(struct HLVM *vm)
{
int i=0;
int op,opd;
hl_vm_init(vm);
puts("INPUT FORMAT:A BBB");
puts("A is the operator, BBB is the operand, both in hex format, without 0x");
puts("e to exit");
while (1)
{
printf("%03d?",i);/*input hint*/
char buf[40];
gets(buf);/*for robust input, line input*/
int ret=sscanf(buf,"%x %x",&op,&opd); /*if input valid, ret equal 2*/
if(ret!=2) /*input finished*/
{
break;
}
printf("(%03d) 0x%02x%06x\n",i,op,opd); /*echo to user*/
vm->_mems[i]= (op << 24) | opd; /*op(8bit) operand(24bit)*/
i++;
}
puts("input finished.");
}
void hl_shell_menu()
{
printf("\tHLVM v%04x\n",HLVM_VER);
puts("type [command] to use.");
puts("1. [vm] input code manual");
puts("2. [ram] view ram");
puts("3. [run] run vm");
puts("4. [ls] list .vm code");
puts("5. [load xx] load xx.vm");
puts("");
}
void hl_shell_main()
{
struct HLVM vm;
hl_vm_init(&vm);
while (1)
{
hl_shell_menu();
printf("input?");
char buf[40],op[20],op2[20];
gets(buf);/*for robust input, line input*/
int ret=sscanf(buf,"%s %s",&op,&op2); /*if input valid, ret equal 2*/
if(ret<1) /*input finished*/
{
break;
}
else if(strcmp(op,"ram")==0)
{
hl_vm_dump(&vm);
}
else if(strcmp(op,"vm")==0)
{
hl_vm_input(&vm);
}
else if(strcmp(op,"run")==0)
{
hl_vm_start(&vm);
while(hl_vm_step(&vm)==HLVM_OK)
{
system("cls");
printf("STEP %6ld\n",vm.ticks);
printf("PC->>%08x<<-\n",vm._mems[vm.pc]);
hl_vm_dump(&vm);
fflush(stdout);
usleep(100000); /*1ms*/
}
puts("HALTED.\n");
}
else if(strcmp(op,"ls")==0)
{
system("dir /b *.vm");
}
else if(strcmp(op,"load")==0)
{
int len=strlen(op2);
op2[len]='.';
op2[len+1]='v';
op2[len+2]='m';
op2[len+3]=0;
FILE *pf=fopen(op2,"r");
if(pf == NULL){
printf("file %s open failed", op2);
continue;
}
char buf[30];
int i=0;
hl_vm_init(&vm);
while(!feof(pf))
{
char *p=fgets(buf,30,pf);
if(p!=NULL)
{
int a,b;
int ret=sscanf(buf,"%x %x",&a,&b); /*if input valid, ret equal 2*/
if(ret!=2) /*input finished*/
{
break;
}
printf("(%03d) < 0x%02x%06x\n",i,a,b); /*echo to user*/
vm._mems[i]= (a << 24) | b; /*op(8bit) operand(24bit)*/
i++;
}
}
puts("done.");
}
}
puts("exit.");
}