-
Notifications
You must be signed in to change notification settings - Fork 8
/
j1.c
186 lines (180 loc) · 5.18 KB
/
j1.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
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
#if defined(unix) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#include <termios.h>
int getch(void) { /* reads from keypress, doesn't echo */
struct termios oldattr, newattr;
int ch;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_iflag &= ~( ICRNL );
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
// printf("%d\n", ch);
if(ch==0x1b) exit(0);
return ch==127 ? 8 : ch;
}
int putch(int c) { /* output character to sstdout & flush */
int res=putchar(c);
fflush(stdout);
return res;
}
#endif
int len = 0;
static pcap_t* handle = NULL;
static void pcapdev_init(void) {
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t* devices;
if (pcap_findalldevs(&devices, errbuf) == -1) {
fprintf(stderr, "error pcap_findalldevs: %s\n", errbuf);
return;
}
pcap_if_t* device;
for(device = devices; device; device = device->next) {
if (device->description) {
printf(" (%s)\n", device->description);
}
else {
fprintf(stderr, "no device\n");
return;
}
}
device = devices->next->next;
if (NULL == (handle= pcap_open_live(device->name
, 65536, 1, 10 , errbuf))) {
fprintf(stderr, "\nUnable to open the adapter. %s is not supported by WinPcap\n");
pcap_freealldevs(devices);
return;
}
pcap_freealldevs(devices);
}
static unsigned short t;
static unsigned short s;
static unsigned short d[0x20]; /* data stack */
static unsigned short r[0x20]; /* return stack */
static unsigned short pc; /* program counter, counts cells */
static unsigned char dsp, rsp; /* point to top entry */
static unsigned short* memory; /* ram */
static int sx[4] = { 0, 1, -2, -1 }; /* 2-bit sign extension */
static void push(int v) // push v on the data stack
{
dsp = 0x1f & (dsp + 1);
d[dsp] = t;
t = v;
}
static int pop(void) // pop value from the data stack and return it
{
int v = t;
t = d[dsp];
dsp = 0x1f & (dsp - 1);
return v;
}
char eth_poll() {
const u_char* packet;
struct pcap_pkthdr* header;
int res = 0;
while (res == 0)
{
res = pcap_next_ex(handle, &header, &packet);
}
len = (int)header->len;
memcpy(&memory[0x2000], packet, len);
return len;
}
void eth_transmit(void) {
if ((pcap_sendpacket(handle, (char *)(&memory[0x2000]), len) == -1))
{
printf("sorry send error\n");
exit(1);
}
}
static void execute(int entrypoint)
{
int _pc, _t;
int insn = 0x4000 | entrypoint; // first insn: "call entrypoint"
pcapdev_init();
do {
_pc = pc + 1;
if (insn & 0x8000) { // literal
push(insn & 0x7fff);
} else {
int target = insn & 0x1fff;
switch (insn >> 13) {
case 0: // jump
_pc = target;
break;
case 1: // conditional jump
if (pop() == 0)
_pc = target;
break;
case 2: // call
rsp = 31 & (rsp + 1);
r[rsp] = _pc << 1;
_pc = target;
break;
case 3: // alu
if (insn & 0x1000) {/* r->pc */
_pc = r[rsp] >> 1;
}
s = d[dsp];
switch ((insn >> 8) & 0xf) {
case 0: _t = t; break; /* noop */
case 1: _t = s; break; /* copy */
case 2: _t = t+s; break; /* + */
case 3: _t = t&s; break; /* and */
case 4: _t = t|s; break; /* or */
case 5: _t = t^s; break; /* xor */
case 6: _t = ~t; break; /* invert */
case 7: _t = -(t==s); break; /* = */
case 8: _t = -((signed short)s < (signed short)t); break; /* < */
case 9: _t = s>>t; break; /* rshift */
case 0xa: _t = t-1; break; /* 1- */
case 0xb: _t = r[rsp]; break; /* r@ */
case 0xc: _t = (t==0xf008)?eth_poll():(t==0xf001)?1:(t==0xf000)?getch():memory[t>>1]; break; /* @ */
case 0xd: _t = s<<t; break; /* lshift */
case 0xe: _t = (rsp<<8) + dsp; break; /* dsp */
case 0xf: _t = -(s<t); break; /* u< */
}
dsp = 31 & (dsp + sx[insn & 3]); /* dstack+- */
rsp = 31 & (rsp + sx[(insn >> 2) & 3]); /* rstack+- */
if (insn & 0x80) /* t->s */
d[dsp] = t;
if (insn & 0x40) /* t->r */
r[rsp] = t;
if (insn & 0x20) /* s->[t] */
(t==0xf008)?eth_transmit(): (t==0xf002)?(rsp=0):(t==0xf000)?putch(s):(memory[t>>1]=s); /* ! */
t = _t;
break;
}
}
pc = _pc;
insn = memory[pc];
} while (1);
}
/* end of cpu */
/* start of i/o demo */
int main(int argc , char *argv[])
{
unsigned short m[0x4000]; /* 32kb or RAM */
FILE *f = fopen("j1.bin", "rb");
fread(m, 0x2000, sizeof(m[0]), f); /* 0kb - 16kb data and code */
fclose(f);
if (argc>1) { // program name is counted as one
struct stat st;
f = fopen(argv[1], "r");
stat(argv[1], &st);
(&m[0x2000])[0] = st.st_size; /* 16kb - 32kb memory mapped i/o */
fread(&m[0x2001], 0x2000, sizeof(m[0]), f);
fclose(f);
}
memory = m;
execute(0x00);
return 0;
}