1 ! Copyright (C) 2007 Gavin Harrison
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math sequences kernel.private namespaces arrays io
4 io.files splitting grouping io.binary math.functions vectors
5 quotations combinators io.encodings.binary ;
13 : reg-val ( m -- n ) regs get nth ;
15 : set-reg ( val n -- ) regs get set-nth ;
17 : arr-val ( index loc -- z )
20 : set-arr ( val index loc -- )
21 arrays get nth set-nth ;
23 : get-op ( num -- op )
24 -28 shift BIN: 1111 bitand ;
26 : get-value ( platter -- register )
29 : >32bit ( m -- n ) HEX: ffffffff bitand ; inline
31 : get-a ( platter -- register )
32 -6 shift BIN: 111 bitand ; inline
34 : get-b ( platter -- register )
35 -3 shift BIN: 111 bitand ; inline
37 : get-c ( platter -- register )
38 BIN: 111 bitand ; inline
40 : get-cb ( platter -- b c ) [ get-c ] keep get-b ;
41 : get-cba ( platter -- c b a ) [ get-cb ] keep get-a ;
42 : get-special ( platter -- register )
43 -25 shift BIN: 111 bitand ; inline
46 get-cba rot reg-val zero? [
49 [ reg-val ] dip set-reg
52 : binary-op ( quot -- ? )
54 swap [ [ [ reg-val ] bi@ swap ] dip call ] dip
58 [ swap arr-val ] binary-op ;
61 get-cba [ [ reg-val ] bi@ ] dip reg-val set-arr f ;
64 [ + >32bit ] binary-op ;
67 [ * >32bit ] binary-op ;
73 [ bitand HEX: ffffffff swap - ] binary-op ;
75 : new-array ( size location -- )
76 [ 0 <array> ] dip arrays get set-nth ;
78 : ?grow-storage ( -- )
79 open-arrays get dup empty? [
80 [ arrays get length ] dip push
87 get-cb [ reg-val open-arrays get pop [ new-array ] keep ] dip
91 get-c reg-val dup open-arrays get push
92 f swap arrays get set-nth f ;
94 : op10 ( opcode -- ? )
95 get-c reg-val write1 flush f ;
97 : op11 ( opcode -- ? )
100 : op12 ( opcode -- ? )
101 get-cb reg-val dup zero? [
104 arrays get [ nth clone 0 ] keep set-nth
105 ] if reg-val finger set f ;
107 : op13 ( opcode -- ? )
108 [ get-value ] keep get-special set-reg f ;
110 : advance ( -- val opcode )
111 finger get arrays get first nth
112 finger inc dup get-op ;
133 : exec-loop ( bool -- )
134 [ run-op exec-loop ] unless ;
136 : load-platters ( path -- )
137 binary file-contents 4 group [ be> ] map
138 0 arrays get set-nth ;
142 2 16 ^ <vector> arrays set
144 V{ } clone open-arrays set
147 : run-prog ( path -- )
151 "resource:extra/icfp/2006/sandmark.umz" run-prog ;