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
|
#define inline
#include <ctype.h>
#include <omp.h>
#include <stdio.h>
#include <string.h>
#pragma clang diagnostic ignored "-Wreturn-type"
typedef char* string;
// opcodes!
// 0: halt
// 1: nop
// 2: mov
// 3: movi
// 4: add
// 5: addi
// 6: sub
// 7: subi
// 8: moveq -> moves m[B] to m[A] if m[Q] == 0
// program counter is m[0]
// Q is set to the previous A in memory
// error handling for unknown ops
#define W 0
// clang-format off
#define J while
#define S 1<<20
#define H !K--
m[S]={0},c,N,Q,E;
ppu(){E=1;_Pragma("omp parallel")J(E){_Pragma("omp single")N=m[m[0]],c=m[0],m[0]+=++N*4;int i;_Pragma("omp for")for(i=0;i<N;i++){int t=c+i*4,Y=m[t++],K=m[t++],A=m[t++],B=m[t++];H?E=0:H?:H?m[A]=m[B]:H?m[A]=B:H?m[A]+=m[B]:H?m[A]+=m[B]:H?m[A]-=m[B]?H:m[Q]?:(m[A]=m[B]):W;Q=A;}}}
#define X J(isspace(*I))I++
char*t="halt\0\0nop\0\0mov\0\2movi\0\2add\0\2addi\0\2sub\0\2subi\0\2moveq\0\2\1";*D,Z;
char*pas(char*I){J(*I){X;if(*I==64)Z=strtol(++I,&I,10);X;char*O=t,F=0;J(*O!=1){int G=strlen(O),L=O[G+1],V=L,B=strncmp(I,O,G);F++,O+=G+2;if(B||!isspace(I[G]))continue;I+=G;*D++=Z,Z=0;*D++=F-1;J(V--){X;*D++=strtol(I,&I,10);}J(2-L++)*D++=0;goto N;}return I;N:;}return 0;}
// clang-format on
main() {
D = &m[ 1 ];
pas("@4 movi 10 2");
// int C;
// for (C = 0; C < 16; C++) printf("%i ", m[ C + 1 ]);
// fflush(stdout);
int x[] = { 1,
3, 3, 5, 1,
0, 3, 5, 2,
0, 3, 5, 3,
0, 3, 5, 4,
0, 0, 0, 0 };
memcpy(m, x, sizeof(x));
ppu();
printf("result: %i\n", m[ 5 ]);
}
|