diff options
Diffstat (limited to 'docs/history/02-hardware/notes.md')
| -rw-r--r-- | docs/history/02-hardware/notes.md | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/docs/history/02-hardware/notes.md b/docs/history/02-hardware/notes.md new file mode 100644 index 0000000..e5ad911 --- /dev/null +++ b/docs/history/02-hardware/notes.md @@ -0,0 +1,46 @@ +# closer to hardware + +no code from this stage survives except where a code/ folder sits next to this file. these are the notes as they were written in the transcript. + +## message 2 + +> your cpu should also be realistically close to hardware (translating it to logical circuitry hardware should be fairly easy) and sport an acceptably fast execution (if hypothetically ported to hardware, not in software) +> +> assume i/o is handled by two external functions, `char Q()` (getchar) and `void G(char)` (putchar) and a state variable J (boolean, shows if input is empty). +> +> don't be afraid to use macros. + +**summary:** no reply was sent to this message at the time. it was answered together with message 3. + +## message 3 + +> pro tip: if you define a macro as `!o--`, you can save 2 characters per if condition +> oh and btw, newlines are free + +**summary:** used the tip as `#define X !--o?`, with pre-decrement because opcode 0 (halt) is already handled by the loop, and with the `?` folded into the macro and free newlines used as separators. the hardware-oriented redesign: bit 0 of each word is a literal flag, so literals take one cycle like the j1 cpu. div and mod were dropped because they can't finish in one cycle and were replaced by an arithmetic shift right, giving 8 alu ops that decode from 3 bits. every instruction does a fixed amount of work with at most one data memory access, and call and jump share a datapath (`r-=o`). i/o now goes through `Q`, `G` and a `?` op that reads `J`. the result was the vm at 339 bytes and the assembler at 346. the final golfed code: + +```c +#define X !--o? +int m[1<<16],p,s=65e3,r=6e4,o,a,b;main(c,v)char**v;{read(open(v[1],0),m,s);while(o=m[p++])a=m[s],b=m[s+1],o&1?m[--s]=o>>1:(o/=2,X +m[s]=m[a]:X +m[a]=b,s+=2:X +m[s]=m[s-~a]:X +s++:X +m[s]=b,m[s+1]=a:(o-=2)<1?m[r]=p,r-=o,p=a,s++:X +p=b?p:a,s+=2:X +p=m[--r]:X +m[--s]=Q():X +m[--s]=J:X +G(a),s++:(m[++s]=X +b+a:X +b-a:X +b*a:X +b&a:X +b|a:X +b^a:X +b>>a:b<a));} +``` + +```c +char t[9999][99],*o="q@!$_~cjzr,?.+-*&|^><",*q;A[9999],k,i,j,n,P;E(y){n++;write(P,&y,4);}V(char*s){for(j=k;j--&&(*t[j]-58||strcmp(t[j]+1,s)););return~j?A[j]:atoi(s);}main(){while(~scanf("%s",t[k]))k++;for(;P<2;P++)for(n=i=0;i<k;i++)if(*(q=t[i])==34)while(*++q)E(*q);else*q==58?A[i]=n:!q[1]&&o[j=strcspn(o,q)]?E(j*2):*q==35?E(V(q+1)):E(V(q)*2+1);} +``` |
